# Processor that saves the data in a database
## First proposal
A processor should be agnostic of a database and does not always save data in a database.
```typescript
import { saveAnalysis } from '@mylims/base-processor';
import { fromB1505, toJcamp } from 'iv-spectrum';
export async function processB1505(filename, content) {
const value = content.toString();
const analyses = fromB1505(value);
const username = filename.split('_')[0];
const sampleCode = filename.split('_').slice(1);
for (const analysis of analyses) {
const jcamp = toJcamp(analysis);
// Set the status of this data to error
if (!analysis.meta.resistance) throw new Error('test');
await saveMeasurement({
username,
sampleId,
collection: 'iv',
derived,
file: jcamp,
});
}
// if return then change the status to success
}
```
Example of a not-saving process
```typescript
import { saveAnalysis } from '@mylims/base-processor';
import { fromB1505, toJcamp } from 'iv-spectrum';
export async function processB1505(filename, content) {
const value = content.toString();
const analyses = fromB1505(value);
const username = filename.split('_')[0];
const sampleCode = filename.split('_').slice(1);
for (const analysis of analyses) {
const jcamp = toJcamp(analysis);
// Set the status of this data to error
if (!analysis.meta.resistance) throw new Error('test');
await fetch('test').then(res => res.text());
}
// if return then change the status to success
return true;
}
```
## Second proposal
Saving on sample
```typescript
import type { ProcessorType } from '@mylims/base-processor/lib/types';
import { fromB1505, toJcamp } from 'iv-spectrum';
export async function processB1505(Processor: ProcessorType) {
const processor = new Processor({ topic: 'b1505', collection: 'iv' });
// username can be specified from CLI
processor.setUsername(username => username ?? filename);
const { content, filename } = await processor.getNextFile();
const value = content.toString('utf-9');
const analyses = fromB1505(value);
// Set the status of this data to error
if (analyses.length < 1) throw new Error('Missing analyses');
for (const analysis of analyses) {
const jcamp = toJcamp(analysis);
await processor.addFile({
file: jcamp,
filename: `${filename}.jdx`,
sampleCode: [filename],
collection: 'iv'
});
}
return processor.done();
};
```
Example of a not-saving process
```typescript
import type { ProcessorType } from '@mylims/base-processor/lib/types';
import { fromB1505, toJcamp } from 'iv-spectrum';
export async function processB1505(Processor: ProcessorType) {
const processor = Processor({ topic: 'b1505', collection: 'iv' });
const { content, filename } = await processor.getNextFile();
const value = content.toString();
const analyses = fromB1505(value);
// Set the status of this data to error
if (analyses.length < 1) throw new Error('Missing analyses');
for (const analysis of analyses) {
const jcamp = toJcamp(analysis);
await fetch('test').then(res => res.text())
}
return processor.done();
};
```
## Third proposal
Saving on sample
```typescript
import { Processor, FileError } from '@mylims/base-processor';
import { fromB1505, toJcamp } from 'iv-spectrum';
export default {
topic: 'b1505',
process: processB1505,
autoCreateSample: false,
} as ProcessorConfig;
async function processB1505(processor: Processor) {
if (! processor.file) {
throw new FileError('Missing file');
}
const {filename, dirname, extension} = processor.file;
// filename: 'patiny_LP1234_F1.jdx'
const userId = filename.split('_')[0];
const sampleCode = filename.split('_').slice(1);
const content = await processor.file.read(); // There could be another method to get a stream.
const analyses = fromB1505(content: TextData); // ensure-string
// type is coming from: https://github.com/cheminfo/cheminfo-types/blob/main/src/core/TextData.d.ts
const uuid10 = base62
// Set the status of this data to error
if (analyses.length < 1) throw new FileError('Missing analyses');
for (const analysis of analyses) {
const jcamp = toJcamp(analysis);
processor.addMeasurement({
file: {
content: jcamp, // Think about referring to a path on disk to avoid too many big files in memory
absolutePath, // alternative to content
filename: `${filename}.jdx`,
mimetype?: 'chemical/x-jcamp-dx',
}
measurementType: 'iv',
derivedData,
sampleCode,
userId,
// or uuid10 (alternative to sampleCode + userId)
}, {
autoCreateSample: true,
});
}
};
```