@travetto/model-mongo 8.0.0-alpha.13 → 8.0.0-alpha.15
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 +6 -6
- package/src/internal/util.ts +1 -1
- package/src/service.ts +13 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-mongo",
|
|
3
|
-
"version": "8.0.0-alpha.
|
|
3
|
+
"version": "8.0.0-alpha.15",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Mongo backing for the travetto model module.",
|
|
6
6
|
"keywords": [
|
|
@@ -26,14 +26,14 @@
|
|
|
26
26
|
"directory": "module/model-mongo"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@travetto/config": "^8.0.0-alpha.
|
|
30
|
-
"@travetto/model": "^8.0.0-alpha.
|
|
31
|
-
"@travetto/model-indexed": "^8.0.0-alpha.
|
|
32
|
-
"@travetto/model-query": "^8.0.0-alpha.
|
|
29
|
+
"@travetto/config": "^8.0.0-alpha.13",
|
|
30
|
+
"@travetto/model": "^8.0.0-alpha.13",
|
|
31
|
+
"@travetto/model-indexed": "^8.0.0-alpha.15",
|
|
32
|
+
"@travetto/model-query": "^8.0.0-alpha.14",
|
|
33
33
|
"mongodb": "^7.1.1"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@travetto/cli": "^8.0.0-alpha.
|
|
36
|
+
"@travetto/cli": "^8.0.0-alpha.18"
|
|
37
37
|
},
|
|
38
38
|
"peerDependenciesMeta": {
|
|
39
39
|
"@travetto/cli": {
|
package/src/internal/util.ts
CHANGED
|
@@ -190,7 +190,7 @@ export class MongoUtil {
|
|
|
190
190
|
(acc, field) => ({ ...acc, ...flattenKeys(castTo(field)) }),
|
|
191
191
|
castTo<Record<string, -1 | 0 | 1>>({}));
|
|
192
192
|
|
|
193
|
-
return [out, { name, unique: idx.
|
|
193
|
+
return [out, { name, unique: !!idx.unique }];
|
|
194
194
|
} else if (isModelIndexedIndex(idx)) {
|
|
195
195
|
const filter = Object.fromEntries([
|
|
196
196
|
...idx.keyTemplate.map(({ path }) => [path.join('.'), 1]),
|
package/src/service.ts
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
type ObjectId, type RootFilterOperators, type Filter,
|
|
5
5
|
type WithId as MongoWithId,
|
|
6
6
|
type FindCursor,
|
|
7
|
+
MongoServerError,
|
|
7
8
|
} from 'mongodb';
|
|
8
9
|
|
|
9
10
|
import {
|
|
@@ -38,6 +39,9 @@ type BlobRaw = GridFSFile & { metadata?: BinaryMetadata };
|
|
|
38
39
|
|
|
39
40
|
type MongoTextSearch = RootFilterOperators<unknown>['$text'];
|
|
40
41
|
|
|
42
|
+
const isDuplicateKeyError = (error: unknown): boolean =>
|
|
43
|
+
error instanceof MongoServerError && error.message.includes('duplicate key error');
|
|
44
|
+
|
|
41
45
|
export const ModelBlobNamespace = '__blobs';
|
|
42
46
|
|
|
43
47
|
/**
|
|
@@ -249,11 +253,15 @@ export class MongoModelService implements
|
|
|
249
253
|
const id = this.preUpdate(cleaned);
|
|
250
254
|
|
|
251
255
|
const store = await this.getStore(cls);
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
256
|
+
try {
|
|
257
|
+
const result = await store.insertOne(castTo(cleaned));
|
|
258
|
+
if (!result.insertedId) {
|
|
259
|
+
throw new ExistsError(cls, id);
|
|
260
|
+
}
|
|
261
|
+
return this.postUpdate(cleaned, id);
|
|
262
|
+
} catch (error) {
|
|
263
|
+
throw isDuplicateKeyError(error) ? new ExistsError(cls, id) : error;
|
|
255
264
|
}
|
|
256
|
-
return this.postUpdate(cleaned, id);
|
|
257
265
|
}
|
|
258
266
|
|
|
259
267
|
async update<T extends ModelType>(cls: Class<T>, item: T): Promise<T> {
|
|
@@ -279,11 +287,7 @@ export class MongoModelService implements
|
|
|
279
287
|
{ upsert: true }
|
|
280
288
|
);
|
|
281
289
|
} catch (error) {
|
|
282
|
-
|
|
283
|
-
throw new ExistsError(cls, id);
|
|
284
|
-
} else {
|
|
285
|
-
throw error;
|
|
286
|
-
}
|
|
290
|
+
throw isDuplicateKeyError(error) ? new ExistsError(cls, id) : error;
|
|
287
291
|
}
|
|
288
292
|
return this.postUpdate(cleaned, id);
|
|
289
293
|
}
|