@travetto/model-mongo 3.1.2 → 3.1.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-mongo",
3
- "version": "3.1.2",
3
+ "version": "3.1.4",
4
4
  "description": "Mongo backing for the travetto model module.",
5
5
  "keywords": [
6
6
  "mongo",
@@ -25,9 +25,9 @@
25
25
  "directory": "module/model-mongo"
26
26
  },
27
27
  "dependencies": {
28
- "@travetto/config": "^3.1.1",
29
- "@travetto/model": "^3.1.2",
30
- "@travetto/model-query": "^3.1.2",
28
+ "@travetto/config": "^3.1.2",
29
+ "@travetto/model": "^3.1.6",
30
+ "@travetto/model-query": "^3.1.4",
31
31
  "mongodb": "^5.0.1"
32
32
  },
33
33
  "peerDependencies": {
@@ -134,7 +134,7 @@ export class MongoUtil {
134
134
  /**
135
135
  * Convert `'a.b.c'` to `{ a: { b: { c: ... }}}`
136
136
  */
137
- static extractSimple<T>(o: T, path: string = ''): Record<string, unknown> {
137
+ static extractSimple<T>(o: T, path: string = '', recursive: boolean = true): Record<string, unknown> {
138
138
  const out: Record<string, unknown> = {};
139
139
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
140
140
  const sub = o as Record<string, unknown>;
@@ -150,7 +150,11 @@ export class MongoUtil {
150
150
  const isPlain = v && ObjectUtil.isPlainObject(v);
151
151
  const firstKey = isPlain ? Object.keys(v)[0] : '';
152
152
  if ((isPlain && !firstKey.startsWith('$')) || v?.constructor?.Ⲑid) {
153
- Object.assign(out, this.extractSimple(v, `${subpath}.`));
153
+ if (recursive) {
154
+ Object.assign(out, this.extractSimple(v, `${subpath}.`));
155
+ } else {
156
+ out[subpath] = v;
157
+ }
154
158
  } else {
155
159
  if (firstKey === '$gt' || firstKey === '$lt' || firstKey === '$gte' || firstKey === '$lte') {
156
160
  for (const [sk, sv] of Object.entries(v)) {
package/src/service.ts CHANGED
@@ -234,7 +234,7 @@ export class MongoModelService implements
234
234
 
235
235
  let final: Record<string, unknown> = item;
236
236
 
237
- const items = MongoUtil.extractSimple(final);
237
+ const items = MongoUtil.extractSimple(final, undefined, false);
238
238
  final = Object
239
239
  .entries(items)
240
240
  .reduce<Record<string, unknown>>((acc, [k, v]) => {
@@ -258,7 +258,7 @@ export class MongoModelService implements
258
258
  );
259
259
 
260
260
  if (!res.value) {
261
- new NotFoundError(cls, id);
261
+ throw new NotFoundError(cls, id);
262
262
  }
263
263
 
264
264
  return this.get(cls, id);
@@ -497,6 +497,19 @@ export class MongoModelService implements
497
497
  }
498
498
 
499
499
  // Query Crud
500
+ async updateOneWithQuery<T extends ModelType>(cls: Class<T>, data: T, query: ModelQuery<T>): Promise<T> {
501
+ const col = await this.getStore(cls);
502
+ const item = await ModelCrudUtil.preStore(cls, data, this);
503
+ query = ModelQueryUtil.getQueryWithId(cls, data, query);
504
+
505
+ const { filter } = MongoUtil.prepareQuery(cls, query);
506
+ const res = await col.replaceOne(filter, item);
507
+ if (res.matchedCount === 0) {
508
+ throw new NotFoundError(cls, item.id);
509
+ }
510
+ return item;
511
+ }
512
+
500
513
  async deleteByQuery<T extends ModelType>(cls: Class<T>, query: ModelQuery<T>): Promise<number> {
501
514
  const col = await this.getStore(cls);
502
515
  const { filter } = MongoUtil.prepareQuery(cls, query, false);