@unchainedshop/mongodb 4.0.0-rc.9 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/build-db-indexes.d.ts +3 -1
- package/lib/build-db-indexes.d.ts.map +1 -1
- package/lib/build-db-indexes.js +25 -21
- package/lib/build-db-indexes.js.map +1 -1
- package/lib/check-id.d.ts +5 -0
- package/lib/check-id.d.ts.map +1 -0
- package/lib/check-id.js +6 -0
- package/lib/check-id.js.map +1 -0
- package/lib/email-regex-operator.d.ts +4 -0
- package/lib/email-regex-operator.d.ts.map +1 -0
- package/lib/email-regex-operator.js +12 -0
- package/lib/email-regex-operator.js.map +1 -0
- package/lib/find-preserving-ids.d.ts +2 -2
- package/lib/find-preserving-ids.d.ts.map +1 -1
- package/lib/find-preserving-ids.js.map +1 -1
- package/lib/generate-db-filter-by-id.d.ts +1 -1
- package/lib/generate-db-filter-by-id.d.ts.map +1 -1
- package/lib/generate-db-mutations.d.ts +11 -0
- package/lib/generate-db-mutations.d.ts.map +1 -0
- package/lib/generate-db-mutations.js +73 -0
- package/lib/generate-db-mutations.js.map +1 -0
- package/lib/initDb.d.ts.map +1 -1
- package/lib/initDb.js +25 -19
- package/lib/initDb.js.map +1 -1
- package/lib/mongodb-index.d.ts +15 -5
- package/lib/mongodb-index.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/build-db-indexes.ts +29 -21
- package/src/find-preserving-ids.ts +3 -3
- package/src/generate-db-filter-by-id.ts +1 -1
- package/src/initDb.ts +28 -23
- package/src/mongodb-index.ts +21 -8
- package/tsconfig.json +2 -1
|
@@ -3,5 +3,7 @@ export type Indexes = {
|
|
|
3
3
|
index: IndexSpecification;
|
|
4
4
|
options?: CreateIndexesOptions;
|
|
5
5
|
}[];
|
|
6
|
-
export declare const buildDbIndexes: <T extends Document>(collection: Collection<T>, indexes: Indexes
|
|
6
|
+
export declare const buildDbIndexes: <T extends Document>(collection: Collection<T>, indexes: Indexes, { rebuild }?: {
|
|
7
|
+
rebuild?: boolean;
|
|
8
|
+
}) => Promise<boolean>;
|
|
7
9
|
//# sourceMappingURL=build-db-indexes.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build-db-indexes.d.ts","sourceRoot":"","sources":["../src/build-db-indexes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAKzF,MAAM,MAAM,OAAO,GAAG;IACpB,KAAK,EAAE,kBAAkB,CAAC;IAC1B,OAAO,CAAC,EAAE,oBAAoB,CAAC;CAChC,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"build-db-indexes.d.ts","sourceRoot":"","sources":["../src/build-db-indexes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAKzF,MAAM,MAAM,OAAO,GAAG;IACpB,KAAK,EAAE,kBAAkB,CAAC;IAC1B,OAAO,CAAC,EAAE,oBAAoB,CAAC;CAChC,EAAE,CAAC;AAwBJ,eAAO,MAAM,cAAc,GAAU,CAAC,SAAS,QAAQ,EACrD,YAAY,UAAU,CAAC,CAAC,CAAC,EACzB,SAAS,OAAO,EAChB,cAAa;IAAE,OAAO,CAAC,EAAE,OAAO,CAAA;CAAO,qBAwBxC,CAAC"}
|
package/lib/build-db-indexes.js
CHANGED
|
@@ -1,35 +1,39 @@
|
|
|
1
1
|
import { createLogger } from '@unchainedshop/logger';
|
|
2
2
|
const logger = createLogger('unchained:mongodb');
|
|
3
|
-
const buildIndexes = (collection, indexes) =>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
3
|
+
const buildIndexes = async (collection, indexes) => {
|
|
4
|
+
const errors = (await Promise.all(indexes.map(async (indexOptions) => {
|
|
5
|
+
if (!indexOptions)
|
|
6
|
+
return null;
|
|
7
|
+
const { index, options } = indexOptions;
|
|
8
|
+
try {
|
|
9
|
+
await collection.createIndex(index, options);
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
logger.error(e);
|
|
14
|
+
return e;
|
|
15
|
+
}
|
|
16
|
+
}))).filter(Boolean);
|
|
17
|
+
return errors;
|
|
18
|
+
};
|
|
19
|
+
export const buildDbIndexes = async (collection, indexes, { rebuild } = {}) => {
|
|
17
20
|
let success = true;
|
|
18
21
|
const buildErrors = (await buildIndexes(collection, indexes)).filter(Boolean);
|
|
19
22
|
if (buildErrors.length) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
if (rebuild) {
|
|
24
|
+
try {
|
|
25
|
+
await collection.dropIndexes();
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
/* */
|
|
29
|
+
}
|
|
30
|
+
}
|
|
26
31
|
const rebuildErrors = (await buildIndexes(collection, indexes)).filter(Boolean);
|
|
27
32
|
if (rebuildErrors.length) {
|
|
28
33
|
logger.error(`Error building some indexes for ${collection.collectionName}`);
|
|
29
34
|
logger.debug(rebuildErrors);
|
|
30
35
|
success = false;
|
|
31
36
|
}
|
|
32
|
-
// }
|
|
33
37
|
}
|
|
34
38
|
return success;
|
|
35
39
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build-db-indexes.js","sourceRoot":"","sources":["../src/build-db-indexes.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAErD,MAAM,MAAM,GAAG,YAAY,CAAC,mBAAmB,CAAC,CAAC;AAOjD,MAAM,YAAY,GAAG,
|
|
1
|
+
{"version":3,"file":"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ACP,CAAC;QACH,CAAC;QAED,MAAM,aAAa,GAAG,CAAC,MAAM,YAAY,CAAI,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAEnF,IAAI,aAAa,CAAC,MAAM,EAAE,CAAC;YACzB,MAAM,CAAC,KAAK,CAAC,mCAAmC,UAAU,CAAC,cAAc,EAAE,CAAC,CAAC;YAC7E,MAAM,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC;YAC5B,OAAO,GAAG,KAAK,CAAC;QAClB,CAAC;IACH,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"check-id.d.ts","sourceRoot":"","sources":["../src/check-id.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,UACX,MAAM,UAET;IACE,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC3B,GACD,SAAS,KACZ,IAIF,CAAC"}
|
package/lib/check-id.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"check-id.js","sourceRoot":"","sources":["../src/check-id.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,CACrB,KAAa,EACb,KAKa,EACP,EAAE;IACR,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,MAAM,KAAK,IAAI,EAAE,OAAO,EAAE,YAAY,EAAE,CAAC;IAC3C,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"email-regex-operator.d.ts","sourceRoot":"","sources":["../src/email-regex-operator.ts"],"names":[],"mappings":"AAAA,wBAAgB,kBAAkB,CAAC,MAAM,KAAA;;EAUxC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function emailRegexOperator(string) {
|
|
2
|
+
if (typeof string !== 'string') {
|
|
3
|
+
throw new TypeError('Expected a string');
|
|
4
|
+
}
|
|
5
|
+
const escapped = string.trim().replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
6
|
+
if (escapped.length === 0)
|
|
7
|
+
throw new Error('String is empty after trimming');
|
|
8
|
+
if (escapped.length > 255)
|
|
9
|
+
throw new Error('String exceeds maximum allowed length');
|
|
10
|
+
return { $regex: new RegExp(`^${escapped}$`, 'i') };
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=email-regex-operator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"email-regex-operator.js","sourceRoot":"","sources":["../src/email-regex-operator.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,kBAAkB,CAAC,MAAM;IACvC,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,MAAM,IAAI,SAAS,CAAC,mBAAmB,CAAC,CAAC;IAC3C,CAAC;IACD,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,uBAAuB,EAAE,MAAM,CAAC,CAAC;IAExE,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;IAC7E,IAAI,QAAQ,CAAC,MAAM,GAAG,GAAG;QAAE,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;IAEpF,OAAO,EAAE,MAAM,EAAE,IAAI,MAAM,CAAC,IAAI,QAAQ,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC;AACtD,CAAC"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { Collection, FindOptions, Filter } from 'mongodb';
|
|
2
|
-
export declare const findPreservingIds: <T>(collection: Collection<T>) => ((selector: Filter<T>, ids: string[], options?: FindOptions) => Promise<T[]>);
|
|
1
|
+
import { Collection, FindOptions, Filter, Document } from 'mongodb';
|
|
2
|
+
export declare const findPreservingIds: <T extends Document>(collection: Collection<T>) => ((selector: Filter<T>, ids: string[], options?: FindOptions) => Promise<T[]>);
|
|
3
3
|
//# sourceMappingURL=find-preserving-ids.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"find-preserving-ids.d.ts","sourceRoot":"","sources":["../src/find-preserving-ids.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"find-preserving-ids.d.ts","sourceRoot":"","sources":["../src/find-preserving-ids.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAQpE,eAAO,MAAM,iBAAiB,GAC3B,CAAC,SAAS,QAAQ,EACjB,YAAY,UAAU,CAAC,CAAC,CAAC,KACxB,CAAC,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,EAAE,CAAC,CA0B9E,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"find-preserving-ids.js","sourceRoot":"","sources":["../src/find-preserving-ids.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,GAAG;IAClB,KAAK,EAAE,CAAC;CACT,CAAC;AAEF,MAAM,WAAW,GAAG,WAAW,CAAC;AAEhC,MAAM,CAAC,MAAM,iBAAiB,GAC5B,CACE,UAAyB,EACsD,EAAE,CACnF,KAAK,EAAE,QAAmB,EAAE,GAAa,EAAE,OAAqB,EAAgB,EAAE;IAChF,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,GAAG,WAAW,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;IAC1D,MAAM,gBAAgB,GAAG;QACvB,GAAG,QAAQ;QACX,GAAG,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE;KAClB,CAAC;IAEF,MAAM,gBAAgB,
|
|
1
|
+
{"version":3,"file":"find-preserving-ids.js","sourceRoot":"","sources":["../src/find-preserving-ids.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,GAAG;IAClB,KAAK,EAAE,CAAC;CACT,CAAC;AAEF,MAAM,WAAW,GAAG,WAAW,CAAC;AAEhC,MAAM,CAAC,MAAM,iBAAiB,GAC5B,CACE,UAAyB,EACsD,EAAE,CACnF,KAAK,EAAE,QAAmB,EAAE,GAAa,EAAE,OAAqB,EAAgB,EAAE;IAChF,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,GAAG,WAAW,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;IAC1D,MAAM,gBAAgB,GAAG;QACvB,GAAG,QAAQ;QACX,GAAG,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE;KAClB,CAAC;IAEF,MAAM,gBAAgB,GAAQ;QAC5B;YACE,MAAM,EAAE,gBAAgB;SACzB;QACD,OAAO,IAAI,KAAK,QAAQ;YACtB,OAAO,IAAI,IAAI,IAAI;YACjB,UAAU,EAAE;gBACV,KAAK,EAAE,EAAE,aAAa,EAAE,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE;aACxC;SACF;QACH,IAAI,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE;QACvB,IAAI,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE;QACvB,KAAK,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE;KAC3B,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAElB,MAAM,kBAAkB,GAAG,UAAU,CAAC,SAAS,CAAC,gBAAgB,CAAC,CAAC;IAClE,MAAM,KAAK,GAAG,MAAM,kBAAkB,CAAC,OAAO,EAAE,CAAC;IACjD,OAAO,KAAY,CAAC;AACtB,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate-db-filter-by-id.d.ts","sourceRoot":"","sources":["../src/generate-db-filter-by-id.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC,eAAO,MAAM,oBAAoB,GAAI,CAAC,SAAS;IAAE,GAAG,
|
|
1
|
+
{"version":3,"file":"generate-db-filter-by-id.d.ts","sourceRoot":"","sources":["../src/generate-db-filter-by-id.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC,eAAO,MAAM,oBAAoB,GAAI,CAAC,SAAS;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,EAC5D,IAAI,OAAO,EACX,QAAO,MAAM,CAAC,CAAC,CAAM,KACpB,MAAM,CAAC,CAAC,CAGV,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import SimpleSchema from 'simpl-schema';
|
|
2
|
+
import { TimestampFields } from '@unchainedshop/types/common.js';
|
|
3
|
+
import { ModuleCreateMutation, ModuleMutations } from '@unchainedshop/types/core.js';
|
|
4
|
+
import { Collection } from 'mongodb';
|
|
5
|
+
export declare const generateDbMutations: <T extends TimestampFields & {
|
|
6
|
+
_id?: string;
|
|
7
|
+
}>(collection: Collection<T>, schema: SimpleSchema, options?: {
|
|
8
|
+
hasCreateOnly?: boolean;
|
|
9
|
+
permanentlyDeleteByDefault?: boolean;
|
|
10
|
+
}) => ModuleMutations<T> | ModuleCreateMutation<T>;
|
|
11
|
+
//# sourceMappingURL=generate-db-mutations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate-db-mutations.d.ts","sourceRoot":"","sources":["../src/generate-db-mutations.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,cAAc,CAAC;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AACjE,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AACrF,OAAO,EAAE,UAAU,EAAgB,MAAM,SAAS,CAAC;AAKnD,eAAO,MAAM,mBAAmB,GAAI,CAAC,SAAS,eAAe,GAAG;IAAE,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,cAClE,UAAU,CAAC,CAAC,CAAC,UACjB,YAAY,YACV;IACR,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,0BAA0B,CAAC,EAAE,OAAO,CAAC;CACtC,KACA,eAAe,CAAC,CAAC,CAAC,GAAG,oBAAoB,CAAC,CAAC,CA6E7C,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { checkId } from './check-id.js';
|
|
2
|
+
import { generateDbObjectId } from './generate-db-object-id.js';
|
|
3
|
+
import { generateDbFilterById } from './generate-db-filter-by-id.js';
|
|
4
|
+
export const generateDbMutations = (collection, schema, options) => {
|
|
5
|
+
if (!collection)
|
|
6
|
+
throw new Error('Collection is missing');
|
|
7
|
+
if (!schema)
|
|
8
|
+
throw new Error('Schema is missing');
|
|
9
|
+
const { hasCreateOnly, permanentlyDeleteByDefault } = options || {
|
|
10
|
+
hasCreateOnly: false,
|
|
11
|
+
permanentlyDeleteByDefault: false,
|
|
12
|
+
};
|
|
13
|
+
const deletePermanently = async (_id) => {
|
|
14
|
+
checkId(_id);
|
|
15
|
+
const filter = generateDbFilterById(_id);
|
|
16
|
+
const result = await collection.deleteOne(filter);
|
|
17
|
+
return result.deletedCount;
|
|
18
|
+
};
|
|
19
|
+
return {
|
|
20
|
+
create: async (doc) => {
|
|
21
|
+
const values = schema.clean(doc);
|
|
22
|
+
values.created = new Date();
|
|
23
|
+
schema.validate(values);
|
|
24
|
+
values._id = doc._id || generateDbObjectId();
|
|
25
|
+
const result = await collection.insertOne(values);
|
|
26
|
+
return result.insertedId;
|
|
27
|
+
},
|
|
28
|
+
update: hasCreateOnly
|
|
29
|
+
? undefined
|
|
30
|
+
: async (_id, doc) => {
|
|
31
|
+
checkId(_id);
|
|
32
|
+
let modifier;
|
|
33
|
+
if (doc?.$set) {
|
|
34
|
+
const values = schema.clean(doc, { isModifier: true });
|
|
35
|
+
modifier = {
|
|
36
|
+
...values,
|
|
37
|
+
$set: {
|
|
38
|
+
...(values.$set || {}),
|
|
39
|
+
updated: new Date(),
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
const values = schema.clean(doc);
|
|
45
|
+
modifier = {
|
|
46
|
+
$set: {
|
|
47
|
+
...values,
|
|
48
|
+
updated: new Date(),
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
schema.validate(modifier, { modifier: true });
|
|
53
|
+
const filter = generateDbFilterById(_id, { deleted: null });
|
|
54
|
+
await collection.updateOne(filter, modifier);
|
|
55
|
+
return _id;
|
|
56
|
+
},
|
|
57
|
+
deletePermanently: hasCreateOnly ? undefined : deletePermanently,
|
|
58
|
+
delete: hasCreateOnly
|
|
59
|
+
? undefined
|
|
60
|
+
: async (_id) => {
|
|
61
|
+
if (permanentlyDeleteByDefault) {
|
|
62
|
+
return deletePermanently(_id);
|
|
63
|
+
}
|
|
64
|
+
checkId(_id);
|
|
65
|
+
const filter = generateDbFilterById(_id, { deleted: null });
|
|
66
|
+
const modifier = { $set: { deleted: new Date() } };
|
|
67
|
+
const values = schema.clean(modifier, { isModifier: true });
|
|
68
|
+
const result = await collection.updateOne(filter, values);
|
|
69
|
+
return result.modifiedCount;
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
//# sourceMappingURL=generate-db-mutations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate-db-mutations.js","sourceRoot":"","sources":["../src/generate-db-mutations.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AAErE,MAAM,CAAC,MAAM,mBAAmB,GAAG,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AAI,EAAE,EAAE,OAAO,EAAE,IAAI,IAAI,EAAE,EAAE,EAAE,CAAC;gBACnD,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC;gBAC5D,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;gBAE1D,OAAO,MAAM,CAAC,aAAa,CAAC;YAC9B,CAAC;KACN,CAAC;AACJ,CAAC,CAAC"}
|
package/lib/initDb.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initDb.d.ts","sourceRoot":"","sources":["../src/initDb.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAe,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"initDb.d.ts","sourceRoot":"","sources":["../src/initDb.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAe,MAAM,SAAS,CAAC;AAiB1C,eAAO,MAAM,OAAO,uBA8BnB,CAAC;AAEF,eAAO,MAAM,MAAM,qBAOlB,CAAC;AAEF,QAAA,MAAM,MAAM,QAAa,OAAO,CAAC,EAAE,CAQlC,CAAC;AAEF,OAAO,EAAE,MAAM,EAAE,CAAC"}
|
package/lib/initDb.js
CHANGED
|
@@ -2,6 +2,7 @@ import { MongoClient } from 'mongodb';
|
|
|
2
2
|
let zstdEnabled = false;
|
|
3
3
|
let mongod;
|
|
4
4
|
let mongoClient;
|
|
5
|
+
const { PORT = '4010' } = process.env;
|
|
5
6
|
try {
|
|
6
7
|
// eslint-disable-next-line
|
|
7
8
|
// @ts-expect-error
|
|
@@ -20,29 +21,34 @@ export const startDb = async () => {
|
|
|
20
21
|
catch {
|
|
21
22
|
/* */
|
|
22
23
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
24
|
+
try {
|
|
25
|
+
mongod = MongoMemoryServer.create({
|
|
26
|
+
instance: process.env.NODE_ENV === 'test'
|
|
27
|
+
? { dbName: 'test', port: parseInt(PORT, 10) + 1 }
|
|
28
|
+
: {
|
|
29
|
+
dbPath: `${process.cwd()}/.db`,
|
|
30
|
+
storageEngine: 'wiredTiger',
|
|
31
|
+
port: parseInt(PORT, 10) + 1,
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
const mongoInstance = await mongod;
|
|
35
|
+
if (mongoInstance) {
|
|
36
|
+
return `${mongoInstance.getUri()}${process.env.NODE_ENV === 'test' ? 'test' : 'unchained'}`;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
34
40
|
/* */
|
|
35
|
-
});
|
|
36
|
-
const mongoInstance = await mongod;
|
|
37
|
-
if (!mongoInstance) {
|
|
38
|
-
throw new Error("Can't connect to MongoDB: could not start mongodb-memory-server and MONGO_URL env is not set");
|
|
39
41
|
}
|
|
40
|
-
|
|
42
|
+
throw new Error("Can't connect to MongoDB: Could not start mongodb-memory-server and MONGO_URL env is not set");
|
|
41
43
|
};
|
|
42
44
|
export const stopDb = async () => {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
45
|
+
try {
|
|
46
|
+
await mongoClient?.close();
|
|
47
|
+
await (await mongod)?.stop();
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
/* */
|
|
51
|
+
}
|
|
46
52
|
};
|
|
47
53
|
const initDb = async () => {
|
|
48
54
|
const url = process.env.MONGO_URL || (await startDb());
|
package/lib/initDb.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initDb.js","sourceRoot":"","sources":["../src/initDb.ts"],"names":[],"mappings":"AAAA,OAAO,EAAM,WAAW,EAAE,MAAM,SAAS,CAAC;AAE1C,IAAI,WAAW,GAAG,KAAK,CAAC;AACxB,IA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AK,IAAiB,EAAE;IACrC,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,CAAC,MAAM,OAAO,EAAE,CAAC,CAAC;IACvD,WAAW,GAAG,IAAI,WAAW,CAAC,GAAG,EAAE;QACjC,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS;KAC9C,CAAC,CAAC;IACH,MAAM,WAAW,CAAC,OAAO,EAAE,CAAC;IAC5B,MAAM,EAAE,GAAG,WAAW,CAAC,EAAE,EAAE,CAAC;IAC5B,OAAO,EAAE,CAAC;AACZ,CAAC,CAAC;AAEF,OAAO,EAAE,MAAM,EAAE,CAAC"}
|
package/lib/mongodb-index.d.ts
CHANGED
|
@@ -10,16 +10,20 @@ export { insensitiveTrimmedRegexOperator } from './insensitive-trimmed-regex-ope
|
|
|
10
10
|
export * from './documentdb-compat-mode.js';
|
|
11
11
|
export { mongodb };
|
|
12
12
|
export interface LogFields {
|
|
13
|
-
log: {
|
|
13
|
+
log: ({
|
|
14
|
+
date: Date;
|
|
15
|
+
status: string | null;
|
|
16
|
+
info?: string;
|
|
17
|
+
} | {
|
|
14
18
|
date: Date;
|
|
15
19
|
status?: string;
|
|
16
20
|
info: string;
|
|
17
|
-
}[];
|
|
21
|
+
})[];
|
|
18
22
|
}
|
|
19
23
|
export interface TimestampFields {
|
|
20
|
-
created
|
|
24
|
+
created: Date;
|
|
21
25
|
updated?: Date;
|
|
22
|
-
deleted?: Date;
|
|
26
|
+
deleted?: Date | null;
|
|
23
27
|
}
|
|
24
28
|
export interface Address {
|
|
25
29
|
addressLine?: string;
|
|
@@ -37,6 +41,12 @@ export interface Contact {
|
|
|
37
41
|
emailAddress?: string;
|
|
38
42
|
}
|
|
39
43
|
export interface Migration<Context = unknown> {
|
|
44
|
+
/**
|
|
45
|
+
* Migration ID must be between 19700000000000 and 99999999999999
|
|
46
|
+
* YYYYMMDDHHMMSS format is recommended.
|
|
47
|
+
* @minimum 19700000000000
|
|
48
|
+
* @maximum 99999999999999
|
|
49
|
+
*/
|
|
40
50
|
id: number;
|
|
41
51
|
name: string;
|
|
42
52
|
up: (params: {
|
|
@@ -52,7 +62,7 @@ export interface MigrationRepository<Context = unknown> {
|
|
|
52
62
|
}
|
|
53
63
|
export interface ModuleInput<Options extends Record<string, any>> {
|
|
54
64
|
db: mongodb.Db;
|
|
55
|
-
migrationRepository
|
|
65
|
+
migrationRepository: MigrationRepository;
|
|
56
66
|
options?: Options;
|
|
57
67
|
}
|
|
58
68
|
//# sourceMappingURL=mongodb-index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mongodb-index.d.ts","sourceRoot":"","sources":["../src/mongodb-index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAEnC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAMtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AACrE,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,+BAA+B,EAAE,MAAM,yCAAyC,CAAC;AAC1F,cAAc,6BAA6B,CAAC;AAE5C,OAAO,EAAE,OAAO,EAAE,CAAC;AAEnB,MAAM,WAAW,SAAS;IACxB,GAAG,EAAE;
|
|
1
|
+
{"version":3,"file":"mongodb-index.d.ts","sourceRoot":"","sources":["../src/mongodb-index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAEnC,OAAO,EAAE,MAAM,EAAE,OAAO,E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AE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,OAAO;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,SAAS,CAAC,OAAO,GAAG,OAAO;IAC1C;;;;;OAKG;IACH,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,CAAC,MAAM,EAAE;QAAE,MAAM,EAAE,GAAG,GAAG,OAAO,CAAC;QAAC,YAAY,EAAE,OAAO,CAAA;KAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACjF;AAED,MAAM,WAAW,mBAAmB,CAAC,OAAO,GAAG,OAAO;IACpD,EAAE,EAAE,OAAO,CAAC,EAAE,CAAC;IACf,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;IAC5C,QAAQ,EAAE,CAAC,SAAS,EAAE,SAAS,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC;IAClD,aAAa,EAAE,MAAM,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC;CAC3C;AAED,MAAM,WAAW,WAAW,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAC9D,EAAE,EAAE,OAAO,CAAC,EAAE,CAAC;IACf,mBAAmB,EAAE,mBAAmB,CAAC;IACzC,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/mongodb",
|
|
3
|
-
"version": "4.0.0
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "MongoDB provider for unchained platform",
|
|
5
5
|
"main": "lib/mongodb-index.js",
|
|
6
6
|
"types": "lib/mongodb-index.d.ts",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
},
|
|
32
32
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@unchainedshop/utils": "^4.0.0
|
|
34
|
+
"@unchainedshop/utils": "^4.0.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"@mongodb-js/zstd": ">= 2.0 < 3",
|
package/src/build-db-indexes.ts
CHANGED
|
@@ -8,36 +8,45 @@ export type Indexes = {
|
|
|
8
8
|
options?: CreateIndexesOptions;
|
|
9
9
|
}[];
|
|
10
10
|
|
|
11
|
-
const buildIndexes =
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
11
|
+
const buildIndexes = async <T extends Document>(
|
|
12
|
+
collection: Collection<T>,
|
|
13
|
+
indexes: Indexes,
|
|
14
|
+
): Promise<Error[]> => {
|
|
15
|
+
const errors = (
|
|
16
|
+
await Promise.all(
|
|
17
|
+
indexes.map(async (indexOptions) => {
|
|
18
|
+
if (!indexOptions) return null;
|
|
19
|
+
const { index, options } = indexOptions;
|
|
20
|
+
try {
|
|
21
|
+
await collection.createIndex(index, options);
|
|
22
|
+
return null;
|
|
23
|
+
} catch (e: any) {
|
|
24
|
+
logger.error(e);
|
|
25
|
+
return e as Error;
|
|
26
|
+
}
|
|
27
|
+
}),
|
|
28
|
+
)
|
|
29
|
+
).filter(Boolean) as Error[];
|
|
30
|
+
return errors;
|
|
31
|
+
};
|
|
25
32
|
|
|
26
33
|
export const buildDbIndexes = async <T extends Document>(
|
|
27
34
|
collection: Collection<T>,
|
|
28
35
|
indexes: Indexes,
|
|
36
|
+
{ rebuild }: { rebuild?: boolean } = {},
|
|
29
37
|
) => {
|
|
30
38
|
let success = true;
|
|
31
39
|
const buildErrors = (await buildIndexes<T>(collection, indexes)).filter(Boolean);
|
|
32
40
|
|
|
33
41
|
if (buildErrors.length) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
42
|
+
if (rebuild) {
|
|
43
|
+
try {
|
|
44
|
+
await collection.dropIndexes();
|
|
45
|
+
} catch {
|
|
46
|
+
/* */
|
|
47
|
+
}
|
|
48
|
+
}
|
|
39
49
|
|
|
40
|
-
// if (!dropError) {
|
|
41
50
|
const rebuildErrors = (await buildIndexes<T>(collection, indexes)).filter(Boolean);
|
|
42
51
|
|
|
43
52
|
if (rebuildErrors.length) {
|
|
@@ -45,7 +54,6 @@ export const buildDbIndexes = async <T extends Document>(
|
|
|
45
54
|
logger.debug(rebuildErrors);
|
|
46
55
|
success = false;
|
|
47
56
|
}
|
|
48
|
-
// }
|
|
49
57
|
}
|
|
50
58
|
|
|
51
59
|
return success;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Collection, FindOptions, Filter } from 'mongodb';
|
|
1
|
+
import { Collection, FindOptions, Filter, Document } from 'mongodb';
|
|
2
2
|
|
|
3
3
|
const sortByIndex = {
|
|
4
4
|
index: 1,
|
|
@@ -7,7 +7,7 @@ const sortByIndex = {
|
|
|
7
7
|
const defaultSort = sortByIndex;
|
|
8
8
|
|
|
9
9
|
export const findPreservingIds =
|
|
10
|
-
<T>(
|
|
10
|
+
<T extends Document>(
|
|
11
11
|
collection: Collection<T>,
|
|
12
12
|
): ((selector: Filter<T>, ids: string[], options?: FindOptions) => Promise<T[]>) =>
|
|
13
13
|
async (selector: Filter<T>, ids: string[], options?: FindOptions): Promise<T[]> => {
|
|
@@ -17,7 +17,7 @@ export const findPreservingIds =
|
|
|
17
17
|
_id: { $in: ids },
|
|
18
18
|
};
|
|
19
19
|
|
|
20
|
-
const filteredPipeline = [
|
|
20
|
+
const filteredPipeline: any = [
|
|
21
21
|
{
|
|
22
22
|
$match: filteredSelector,
|
|
23
23
|
},
|
package/src/initDb.ts
CHANGED
|
@@ -4,6 +4,8 @@ let zstdEnabled = false;
|
|
|
4
4
|
let mongod;
|
|
5
5
|
let mongoClient: MongoClient | null;
|
|
6
6
|
|
|
7
|
+
const { PORT = '4010' } = process.env;
|
|
8
|
+
|
|
7
9
|
try {
|
|
8
10
|
// eslint-disable-next-line
|
|
9
11
|
// @ts-expect-error
|
|
@@ -21,34 +23,37 @@ export const startDb = async () => {
|
|
|
21
23
|
} catch {
|
|
22
24
|
/* */
|
|
23
25
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
26
|
+
try {
|
|
27
|
+
mongod = MongoMemoryServer.create({
|
|
28
|
+
instance:
|
|
29
|
+
process.env.NODE_ENV === 'test'
|
|
30
|
+
? { dbName: 'test', port: parseInt(PORT, 10) + 1 }
|
|
31
|
+
: {
|
|
32
|
+
dbPath: `${process.cwd()}/.db`,
|
|
33
|
+
storageEngine: 'wiredTiger',
|
|
34
|
+
port: parseInt(PORT, 10) + 1,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
const mongoInstance = await mongod;
|
|
38
|
+
if (mongoInstance) {
|
|
39
|
+
return `${mongoInstance.getUri()}${process.env.NODE_ENV === 'test' ? 'test' : 'unchained'}`;
|
|
40
|
+
}
|
|
41
|
+
} catch {
|
|
36
42
|
/* */
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
const mongoInstance = await mongod;
|
|
40
|
-
if (!mongoInstance) {
|
|
41
|
-
throw new Error(
|
|
42
|
-
"Can't connect to MongoDB: could not start mongodb-memory-server and MONGO_URL env is not set",
|
|
43
|
-
);
|
|
44
43
|
}
|
|
45
|
-
|
|
44
|
+
|
|
45
|
+
throw new Error(
|
|
46
|
+
"Can't connect to MongoDB: Could not start mongodb-memory-server and MONGO_URL env is not set",
|
|
47
|
+
);
|
|
46
48
|
};
|
|
47
49
|
|
|
48
50
|
export const stopDb = async () => {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
try {
|
|
52
|
+
await mongoClient?.close();
|
|
53
|
+
await (await mongod)?.stop();
|
|
54
|
+
} catch {
|
|
55
|
+
/* */
|
|
56
|
+
}
|
|
52
57
|
};
|
|
53
58
|
|
|
54
59
|
const initDb = async (): Promise<Db> => {
|
package/src/mongodb-index.ts
CHANGED
|
@@ -18,17 +18,24 @@ export * from './documentdb-compat-mode.js';
|
|
|
18
18
|
export { mongodb };
|
|
19
19
|
|
|
20
20
|
export interface LogFields {
|
|
21
|
-
log:
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
log: (
|
|
22
|
+
| {
|
|
23
|
+
date: Date;
|
|
24
|
+
status: string | null;
|
|
25
|
+
info?: string;
|
|
26
|
+
}
|
|
27
|
+
| {
|
|
28
|
+
date: Date;
|
|
29
|
+
status?: string;
|
|
30
|
+
info: string;
|
|
31
|
+
}
|
|
32
|
+
)[];
|
|
26
33
|
}
|
|
27
34
|
|
|
28
35
|
export interface TimestampFields {
|
|
29
|
-
created
|
|
36
|
+
created: Date;
|
|
30
37
|
updated?: Date;
|
|
31
|
-
deleted?: Date;
|
|
38
|
+
deleted?: Date | null;
|
|
32
39
|
}
|
|
33
40
|
|
|
34
41
|
export interface Address {
|
|
@@ -49,6 +56,12 @@ export interface Contact {
|
|
|
49
56
|
}
|
|
50
57
|
|
|
51
58
|
export interface Migration<Context = unknown> {
|
|
59
|
+
/**
|
|
60
|
+
* Migration ID must be between 19700000000000 and 99999999999999
|
|
61
|
+
* YYYYMMDDHHMMSS format is recommended.
|
|
62
|
+
* @minimum 19700000000000
|
|
63
|
+
* @maximum 99999999999999
|
|
64
|
+
*/
|
|
52
65
|
id: number;
|
|
53
66
|
name: string;
|
|
54
67
|
up: (params: { logger: any | Console; unchainedAPI: Context }) => Promise<void>;
|
|
@@ -63,6 +76,6 @@ export interface MigrationRepository<Context = unknown> {
|
|
|
63
76
|
|
|
64
77
|
export interface ModuleInput<Options extends Record<string, any>> {
|
|
65
78
|
db: mongodb.Db;
|
|
66
|
-
migrationRepository
|
|
79
|
+
migrationRepository: MigrationRepository;
|
|
67
80
|
options?: Options;
|
|
68
81
|
}
|