-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathmongo.ts
More file actions
36 lines (33 loc) · 1.14 KB
/
mongo.ts
File metadata and controls
36 lines (33 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* eslint-disable no-console */
import { ObjectId } from 'mongodb';
import {
STATEMENTS_COLLECTION_NAME,
STORE_STATEMENT_WRITE_CONCERN_DEFAULT,
} from '../utils/mongoModels/constants';
import FacadeConfig from '../utils/mongoModels/FacadeConfig';
import { encodeDotsInStatement } from '../utils/mongoModels/replaceDotsInStatement';
import Signature from './Signature';
export default (config: FacadeConfig): Signature => {
return async (opts) => {
if (opts.models.length === 0) {
return [];
}
const documents = opts.models.map((model) => {
const statement = encodeDotsInStatement(model.statement);
return {
...model,
organisation: new ObjectId(model.organisation),
lrs_id: new ObjectId(model.lrs_id),
client: new ObjectId(model.client),
statement,
};
});
const collection = (await config.db()).collection(STATEMENTS_COLLECTION_NAME);
console.time('STATEMENT INSERTION');
await collection.insertMany(documents, {
writeConcern: { w: STORE_STATEMENT_WRITE_CONCERN_DEFAULT },
});
console.timeEnd('STATEMENT INSERTION');
return opts.models;
};
};