@travetto/model-mongo 3.1.13 → 3.1.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-mongo",
3
- "version": "3.1.13",
3
+ "version": "3.1.15",
4
4
  "description": "Mongo backing for the travetto model module.",
5
5
  "keywords": [
6
6
  "mongo",
@@ -25,13 +25,13 @@
25
25
  "directory": "module/model-mongo"
26
26
  },
27
27
  "dependencies": {
28
- "@travetto/config": "^3.1.7",
29
- "@travetto/model": "^3.1.13",
30
- "@travetto/model-query": "^3.1.13",
28
+ "@travetto/config": "^3.1.9",
29
+ "@travetto/model": "^3.1.15",
30
+ "@travetto/model-query": "^3.1.15",
31
31
  "mongodb": "^5.5.0"
32
32
  },
33
33
  "peerDependencies": {
34
- "@travetto/command": "^3.1.4"
34
+ "@travetto/command": "^3.1.5"
35
35
  },
36
36
  "peerDependenciesMeta": {
37
37
  "@travetto/command": {
@@ -3,7 +3,9 @@ import * as mongo from 'mongodb';
3
3
  import { Class, DataUtil, ObjectUtil } from '@travetto/base';
4
4
  import { DistanceUnit, ModelQuery, Query, WhereClause } from '@travetto/model-query';
5
5
  import type { ModelType, IndexField } from '@travetto/model';
6
+ import { SchemaRegistry } from '@travetto/schema';
6
7
  import { ModelQueryUtil } from '@travetto/model-query/src/internal/service/query';
8
+ import { AllViewⲐ } from '@travetto/schema/src/internal/types';
7
9
 
8
10
  /**
9
11
  * Converting units to various radians
@@ -76,7 +78,7 @@ export class MongoUtil {
76
78
  const q = ModelQueryUtil.getQueryAndVerify(cls, query, checkExpiry);
77
79
  return {
78
80
  query: q,
79
- filter: q.where ? this.extractWhereClause(q.where) : {}
81
+ filter: q.where ? this.extractWhereClause(cls, q.where) : {}
80
82
  };
81
83
  }
82
84
 
@@ -90,15 +92,15 @@ export class MongoUtil {
90
92
  /**
91
93
  * Build mongo where clause
92
94
  */
93
- static extractWhereClause<T>(o: WhereClause<T>): Record<string, unknown> {
95
+ static extractWhereClause<T>(cls: Class<T>, o: WhereClause<T>): Record<string, unknown> {
94
96
  if (this.has$And(o)) {
95
- return { $and: o.$and.map(x => this.extractWhereClause<T>(x)) };
97
+ return { $and: o.$and.map(x => this.extractWhereClause<T>(cls, x)) };
96
98
  } else if (this.has$Or(o)) {
97
- return { $or: o.$or.map(x => this.extractWhereClause<T>(x)) };
99
+ return { $or: o.$or.map(x => this.extractWhereClause<T>(cls, x)) };
98
100
  } else if (this.has$Not(o)) {
99
- return { $nor: [this.extractWhereClause<T>(o.$not)] };
101
+ return { $nor: [this.extractWhereClause<T>(cls, o.$not)] };
100
102
  } else {
101
- return this.extractSimple(o);
103
+ return this.extractSimple(cls, o);
102
104
  }
103
105
  }
104
106
 
@@ -128,10 +130,9 @@ export class MongoUtil {
128
130
  }
129
131
  }
130
132
 
131
- /**
132
- * Convert `'a.b.c'` to `{ a: { b: { c: ... }}}`
133
- */
134
- static extractSimple<T>(o: T, path: string = '', recursive: boolean = true): Record<string, unknown> {
133
+ /**/
134
+ static extractSimple<T>(base: Class<T> | undefined, o: Record<string, unknown>, path: string = '', recursive: boolean = true): Record<string, unknown> {
135
+ const schema = base ? SchemaRegistry.get(base) : undefined;
135
136
  const out: Record<string, unknown> = {};
136
137
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
137
138
  const sub = o as Record<string, unknown>;
@@ -140,15 +141,17 @@ export class MongoUtil {
140
141
  const subpath = `${path}${key}`;
141
142
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
142
143
  const v = sub[key] as Record<string, unknown>;
144
+ const subField = schema?.views[AllViewⲐ].schema[key];
143
145
 
144
146
  if (subpath === 'id') { // Handle ids directly
145
147
  out._id = this.replaceId(v);
146
148
  } else {
147
149
  const isPlain = v && ObjectUtil.isPlainObject(v);
148
150
  const firstKey = isPlain ? Object.keys(v)[0] : '';
149
- if ((isPlain && !firstKey.startsWith('$')) || v?.constructor?.Ⲑid) {
151
+
152
+ if ((isPlain && !firstKey.startsWith('$')) || v.constructor?.Ⲑid) {
150
153
  if (recursive) {
151
- Object.assign(out, this.extractSimple(v, `${subpath}.`));
154
+ Object.assign(out, this.extractSimple(subField?.type, v, `${subpath}.`, recursive));
152
155
  } else {
153
156
  out[subpath] = v;
154
157
  }
@@ -157,15 +160,15 @@ export class MongoUtil {
157
160
  for (const [sk, sv] of Object.entries(v)) {
158
161
  v[sk] = ModelQueryUtil.resolveComparator(sv);
159
162
  }
160
- } else if (firstKey === '$empty') {
161
- const isEmpty = v.$empty;
162
- if (isEmpty) {
163
+ } else if (firstKey === '$exists' && subField?.array) {
164
+ const exists = v.$exists;
165
+ if (!exists) {
166
+ delete v.$exists;
163
167
  v.$in = [null, []];
164
168
  } else {
165
169
  v.$exists = true;
166
170
  v.$nin = [null, []];
167
171
  }
168
- delete v.$empty;
169
172
  } else if (firstKey === '$regex') {
170
173
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
171
174
  v.$regex = DataUtil.toRegex(v.$regex as string | RegExp);
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, undefined, false);
237
+ const items = MongoUtil.extractSimple(cls, final, undefined, false);
238
238
  final = Object
239
239
  .entries(items)
240
240
  .reduce<Record<string, unknown>>((acc, [k, v]) => {
@@ -472,7 +472,7 @@ export class MongoModelService implements
472
472
  let cursor = col.find<T>(filter, {});
473
473
  if (query.select) {
474
474
  const selectKey = Object.keys(query.select)[0];
475
- const select = typeof selectKey === 'string' && selectKey.startsWith('$') ? query.select : MongoUtil.extractSimple(query.select);
475
+ const select = typeof selectKey === 'string' && selectKey.startsWith('$') ? query.select : MongoUtil.extractSimple(cls, query.select);
476
476
  // Remove id if not explicitly defined, and selecting fields directly
477
477
  if (!select['_id']) {
478
478
  const values = new Set([...Object.values(select)]);
@@ -484,7 +484,7 @@ export class MongoModelService implements
484
484
  }
485
485
 
486
486
  if (query.sort) {
487
- cursor = cursor.sort(Object.assign({}, ...query.sort.map(x => MongoUtil.extractSimple(x))));
487
+ cursor = cursor.sort(Object.assign({}, ...query.sort.map(x => MongoUtil.extractSimple(cls, x))));
488
488
  }
489
489
 
490
490
  cursor = cursor.limit(Math.trunc(query.limit ?? 200));
@@ -532,7 +532,7 @@ export class MongoModelService implements
532
532
  async updateByQuery<T extends ModelType>(cls: Class<T>, query: ModelQuery<T>, data: Partial<T>): Promise<number> {
533
533
  const col = await this.getStore(cls);
534
534
 
535
- const items = MongoUtil.extractSimple(data);
535
+ const items = MongoUtil.extractSimple(cls, data);
536
536
  const final = Object.entries(items).reduce<Record<string, unknown>>((acc, [k, v]) => {
537
537
  if (v === null || v === undefined) {
538
538
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions