@travetto/model-mongo 6.0.0 → 7.0.0-rc.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/README.md +1 -2
- package/package.json +6 -6
- package/src/config.ts +0 -2
- package/src/internal/util.ts +5 -5
- package/src/service.ts +5 -5
- package/support/service.mongo.ts +1 -1
package/README.md
CHANGED
|
@@ -43,7 +43,7 @@ export class Init {
|
|
|
43
43
|
}
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
where the [MongoModelConfig](https://github.com/travetto/travetto/tree/main/module/model-mongo/src/config.ts#
|
|
46
|
+
where the [MongoModelConfig](https://github.com/travetto/travetto/tree/main/module/model-mongo/src/config.ts#L10) is defined by:
|
|
47
47
|
|
|
48
48
|
**Code: Structure of MongoModelConfig**
|
|
49
49
|
```typescript
|
|
@@ -81,7 +81,6 @@ export class MongoModelConfig {
|
|
|
81
81
|
/**
|
|
82
82
|
* Mongo client options
|
|
83
83
|
*/
|
|
84
|
-
@Field(Object)
|
|
85
84
|
options: mongo.MongoClientOptions = {};
|
|
86
85
|
|
|
87
86
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-mongo",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0-rc.0",
|
|
4
4
|
"description": "Mongo backing for the travetto model module.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mongo",
|
|
@@ -25,11 +25,11 @@
|
|
|
25
25
|
"directory": "module/model-mongo"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@travetto/cli": "^
|
|
29
|
-
"@travetto/config": "^
|
|
30
|
-
"@travetto/model": "^
|
|
31
|
-
"@travetto/model-query": "^
|
|
32
|
-
"mongodb": "^
|
|
28
|
+
"@travetto/cli": "^7.0.0-rc.0",
|
|
29
|
+
"@travetto/config": "^7.0.0-rc.0",
|
|
30
|
+
"@travetto/model": "^7.0.0-rc.0",
|
|
31
|
+
"@travetto/model-query": "^7.0.0-rc.0",
|
|
32
|
+
"mongodb": "^7.0.0"
|
|
33
33
|
},
|
|
34
34
|
"travetto": {
|
|
35
35
|
"displayName": "MongoDB Model Support"
|
package/src/config.ts
CHANGED
|
@@ -2,7 +2,6 @@ import type mongo from 'mongodb';
|
|
|
2
2
|
|
|
3
3
|
import { TimeSpan, TimeUtil, RuntimeResources, Runtime } from '@travetto/runtime';
|
|
4
4
|
import { Config } from '@travetto/config';
|
|
5
|
-
import { Field } from '@travetto/schema';
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
7
|
* Mongo model config
|
|
@@ -41,7 +40,6 @@ export class MongoModelConfig {
|
|
|
41
40
|
/**
|
|
42
41
|
* Mongo client options
|
|
43
42
|
*/
|
|
44
|
-
@Field(Object)
|
|
45
43
|
options: mongo.MongoClientOptions = {};
|
|
46
44
|
|
|
47
45
|
/**
|
package/src/internal/util.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
import { AppError, castTo, Class, toConcrete, TypedObject } from '@travetto/runtime';
|
|
6
6
|
import { type DistanceUnit, type PageableModelQuery, type WhereClause, ModelQueryUtil } from '@travetto/model-query';
|
|
7
7
|
import type { ModelType, IndexField, IndexConfig } from '@travetto/model';
|
|
8
|
-
import { DataUtil,
|
|
8
|
+
import { DataUtil, SchemaRegistryIndex, type Point } from '@travetto/schema';
|
|
9
9
|
|
|
10
10
|
const PointImpl = toConcrete<Point>();
|
|
11
11
|
|
|
@@ -80,14 +80,14 @@ export class MongoUtil {
|
|
|
80
80
|
|
|
81
81
|
/**/
|
|
82
82
|
static extractSimple<T>(base: Class<T> | undefined, o: Record<string, unknown>, path: string = '', recursive: boolean = true): Record<string, unknown> {
|
|
83
|
-
const
|
|
83
|
+
const fields = base ? SchemaRegistryIndex.getOptionalConfig(base)?.fields : undefined;
|
|
84
84
|
const out: Record<string, unknown> = {};
|
|
85
85
|
const sub = o;
|
|
86
86
|
const keys = Object.keys(sub);
|
|
87
87
|
for (const key of keys) {
|
|
88
88
|
const subpath = `${path}${key}`;
|
|
89
89
|
const v: Record<string, unknown> = castTo(sub[key]);
|
|
90
|
-
const subField =
|
|
90
|
+
const subField = fields?.[key];
|
|
91
91
|
|
|
92
92
|
const isPlain = v && DataUtil.isPlainObject(v);
|
|
93
93
|
const firstKey = isPlain ? Object.keys(v)[0] : '';
|
|
@@ -131,7 +131,7 @@ export class MongoUtil {
|
|
|
131
131
|
} else if (firstKey && '$geoWithin' in v) {
|
|
132
132
|
const coords: [number, number][] = castTo(v.$geoWithin);
|
|
133
133
|
const first = coords[0];
|
|
134
|
-
const last = coords
|
|
134
|
+
const last = coords.at(-1)!;
|
|
135
135
|
// Connect if not
|
|
136
136
|
if (first[0] !== last[0] || first[1] !== last[1]) {
|
|
137
137
|
coords.push(first);
|
|
@@ -152,7 +152,7 @@ export class MongoUtil {
|
|
|
152
152
|
static getExtraIndices<T extends ModelType>(cls: Class<T>): BasicIdx[] {
|
|
153
153
|
const out: BasicIdx[] = [];
|
|
154
154
|
const textFields: string[] = [];
|
|
155
|
-
|
|
155
|
+
SchemaRegistryIndex.visitFields(cls, (field, path) => {
|
|
156
156
|
if (field.type === PointImpl) {
|
|
157
157
|
const name = [...path, field].map(x => x.name).join('.');
|
|
158
158
|
out.push({ [name]: '2d' });
|
package/src/service.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
} from 'mongodb';
|
|
8
8
|
|
|
9
9
|
import {
|
|
10
|
-
|
|
10
|
+
ModelRegistryIndex, ModelType, OptionalId, ModelCrudSupport, ModelStorageSupport,
|
|
11
11
|
ModelExpirySupport, ModelBulkSupport, ModelIndexedSupport, BulkOp, BulkResponse,
|
|
12
12
|
NotFoundError, ExistsError, ModelBlobSupport,
|
|
13
13
|
ModelCrudUtil, ModelIndexedUtil, ModelStorageUtil, ModelExpiryUtil, ModelBulkUtil, ModelBlobUtil,
|
|
@@ -107,7 +107,7 @@ export class MongoModelService implements
|
|
|
107
107
|
writeConcern: { w: 1 }
|
|
108
108
|
});
|
|
109
109
|
await ModelStorageUtil.registerModelChangeListener(this);
|
|
110
|
-
ShutdownManager.onGracefulShutdown(() => this.client.close()
|
|
110
|
+
ShutdownManager.onGracefulShutdown(() => this.client.close());
|
|
111
111
|
ModelExpiryUtil.registerCull(this);
|
|
112
112
|
}
|
|
113
113
|
|
|
@@ -128,7 +128,7 @@ export class MongoModelService implements
|
|
|
128
128
|
|
|
129
129
|
async establishIndices<T extends ModelType>(cls: Class<T>): Promise<void> {
|
|
130
130
|
const col = await this.getStore(cls);
|
|
131
|
-
const creating = MongoUtil.getIndices(cls,
|
|
131
|
+
const creating = MongoUtil.getIndices(cls, ModelRegistryIndex.getConfig(cls).indices);
|
|
132
132
|
if (creating.length) {
|
|
133
133
|
console.debug('Creating indexes', { indices: creating });
|
|
134
134
|
for (const el of creating) {
|
|
@@ -158,7 +158,7 @@ export class MongoModelService implements
|
|
|
158
158
|
* Get mongo collection
|
|
159
159
|
*/
|
|
160
160
|
async getStore<T extends ModelType>(cls: Class<T>): Promise<Collection<T>> {
|
|
161
|
-
return this.#db.collection(
|
|
161
|
+
return this.#db.collection(ModelRegistryIndex.getStoreName(cls).toLowerCase().replace(/[^A-Za-z0-9_]+/g, '_'));
|
|
162
162
|
}
|
|
163
163
|
|
|
164
164
|
// Crud
|
|
@@ -430,7 +430,7 @@ export class MongoModelService implements
|
|
|
430
430
|
|
|
431
431
|
async * listByIndex<T extends ModelType>(cls: Class<T>, idx: string, body?: DeepPartial<T>): AsyncIterable<T> {
|
|
432
432
|
const store = await this.getStore(cls);
|
|
433
|
-
const idxCfg =
|
|
433
|
+
const idxCfg = ModelRegistryIndex.getIndex(cls, idx, ['sorted', 'unsorted']);
|
|
434
434
|
|
|
435
435
|
const where = this.getWhereFilter(
|
|
436
436
|
cls,
|
package/support/service.mongo.ts
CHANGED