@travetto/model-mongo 8.0.5 → 8.0.6
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/package.json +4 -4
- package/src/service.ts +29 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-mongo",
|
|
3
|
-
"version": "8.0.
|
|
3
|
+
"version": "8.0.6",
|
|
4
4
|
"description": "Mongo backing for the travetto model module.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"database",
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@travetto/config": "^8.0.3",
|
|
33
|
-
"@travetto/model": "^8.0.
|
|
34
|
-
"@travetto/model-indexed": "^8.0.
|
|
35
|
-
"@travetto/model-query": "^8.0.
|
|
33
|
+
"@travetto/model": "^8.0.4",
|
|
34
|
+
"@travetto/model-indexed": "^8.0.4",
|
|
35
|
+
"@travetto/model-query": "^8.0.6",
|
|
36
36
|
"mongodb": "^7.6.0"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
package/src/service.ts
CHANGED
|
@@ -104,6 +104,13 @@ const handleDuplicateKeyError = (cls: Class, id: string, error: unknown): unknow
|
|
|
104
104
|
return error;
|
|
105
105
|
};
|
|
106
106
|
|
|
107
|
+
function isNotFoundError(error: unknown): error is MongoServerError {
|
|
108
|
+
return (
|
|
109
|
+
(error instanceof MongoServerError && (error.code === 26 || error.codeName === 'NamespaceNotFound')) ||
|
|
110
|
+
(error instanceof Error && /ns not found/i.test(error.message))
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
107
114
|
export const ModelBlobNamespace = '__blobs';
|
|
108
115
|
|
|
109
116
|
/**
|
|
@@ -256,13 +263,15 @@ export class MongoModelService
|
|
|
256
263
|
async createStorage(): Promise<void> {}
|
|
257
264
|
|
|
258
265
|
async deleteStorage(): Promise<void> {
|
|
259
|
-
await this.#db.dropDatabase();
|
|
266
|
+
await ModelStorageUtil.runAndIgnoreNotFound(() => this.#db.dropDatabase(), isNotFoundError);
|
|
260
267
|
}
|
|
261
268
|
|
|
262
269
|
async upsertModel(cls: Class): Promise<void> {
|
|
263
270
|
const col = await this.getStore(cls);
|
|
264
271
|
const indices = [...ModelRegistryIndex.getIndices(cls).map(idx => MongoUtil.getIndex(cls, idx)), ...MongoUtil.getExtraIndices(cls)];
|
|
265
|
-
const existingIndices = (await
|
|
272
|
+
const existingIndices = ((await ModelStorageUtil.runAndIgnoreNotFound(() => col.indexes(), isNotFoundError)) ?? []).filter(
|
|
273
|
+
idx => idx.name !== '_id_'
|
|
274
|
+
);
|
|
266
275
|
|
|
267
276
|
const pendingMap = Object.fromEntries(indices.map(pair => [pair[1].name!, pair]));
|
|
268
277
|
const existingMap = Object.fromEntries(existingIndices.map(idx => [idx.name!, idx.key]));
|
|
@@ -289,13 +298,17 @@ export class MongoModelService
|
|
|
289
298
|
}
|
|
290
299
|
}
|
|
291
300
|
|
|
301
|
+
async deleteModel<T extends ModelType>(cls: Class<T>): Promise<void> {
|
|
302
|
+
await ModelStorageUtil.runAndIgnoreNotFound(() => this.#db.collection(ModelRegistryIndex.getStoreName(cls)).drop(), isNotFoundError);
|
|
303
|
+
}
|
|
304
|
+
|
|
292
305
|
async truncateModel<T extends ModelType>(cls: Class<T>): Promise<void> {
|
|
293
306
|
const col = await this.getStore(cls);
|
|
294
307
|
await col.deleteMany({});
|
|
295
308
|
}
|
|
296
309
|
|
|
297
310
|
async truncateBlob(): Promise<void> {
|
|
298
|
-
await this.#bucket.drop()
|
|
311
|
+
await this.#bucket.drop();
|
|
299
312
|
}
|
|
300
313
|
|
|
301
314
|
/**
|
|
@@ -746,27 +759,26 @@ export class MongoModelService
|
|
|
746
759
|
|
|
747
760
|
const collection = await this.getStore(cls);
|
|
748
761
|
|
|
749
|
-
const where = ModelQueryUtil.getWhereClause(cls,
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
if (where) {
|
|
753
|
-
queryObject = { $and: [queryObject, MongoUtil.extractWhereFilter(cls, where)] };
|
|
754
|
-
}
|
|
762
|
+
const where = ModelQueryUtil.getWhereClause(cls, {
|
|
763
|
+
$and: [query?.where ?? {}, { [field]: { $exists: true } }]
|
|
764
|
+
});
|
|
755
765
|
|
|
756
|
-
const isDate = SchemaRegistryIndex.getNestedFieldConfig(cls, field)
|
|
766
|
+
const isDate = SchemaRegistryIndex.getNestedFieldConfig(cls, field)?.type === Date;
|
|
757
767
|
|
|
758
768
|
const groupFields: Record<string, unknown> = {
|
|
759
769
|
_id: null,
|
|
760
770
|
count: { $sum: 1 },
|
|
761
|
-
min: { $min: `$${
|
|
762
|
-
max: { $max: `$${
|
|
771
|
+
min: { $min: `$${field}` },
|
|
772
|
+
max: { $max: `$${field}` },
|
|
773
|
+
...(isDate
|
|
774
|
+
? {}
|
|
775
|
+
: {
|
|
776
|
+
avg: { $avg: `$${field}` },
|
|
777
|
+
sum: { $sum: `$${field}` }
|
|
778
|
+
})
|
|
763
779
|
};
|
|
764
|
-
if (!isDate) {
|
|
765
|
-
groupFields.avg = { $avg: `$${String(field)}` };
|
|
766
|
-
groupFields.sum = { $sum: `$${String(field)}` };
|
|
767
|
-
}
|
|
768
780
|
|
|
769
|
-
const aggregations: object[] = [{ $match:
|
|
781
|
+
const aggregations: object[] = [{ $match: MongoUtil.extractWhereFilter(cls, where) }, { $group: groupFields }];
|
|
770
782
|
|
|
771
783
|
const result = await collection.aggregate<NumberFieldAggregateResult>(aggregations).toArray();
|
|
772
784
|
|