@travetto/model-mongo 3.0.0-rc.9 → 3.0.1-rc.1

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 CHANGED
@@ -6,6 +6,10 @@
6
6
  **Install: @travetto/model-mongo**
7
7
  ```bash
8
8
  npm install @travetto/model-mongo
9
+
10
+ # or
11
+
12
+ yarn add @travetto/model-mongo
9
13
  ```
10
14
 
11
15
  This module provides an [mongodb](https://mongodb.com)-based implementation for the [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations."). This source allows the [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") module to read, write and query against [mongodb](https://mongodb.com).. Given the dynamic nature of [mongodb](https://mongodb.com), during development when models are modified, nothing needs to be done to adapt to the latest schema.
@@ -46,9 +50,9 @@ export class Init {
46
50
 
47
51
  **Code: Structure of MongoModelConfig**
48
52
  ```typescript
49
- import * as mongo from 'mongodb';
53
+ import type mongo from 'mongodb';
50
54
 
51
- import { CommonFileResourceProvider, TimeSpan } from '@travetto/base';
55
+ import { FileResourceProvider, TimeSpan } from '@travetto/base';
52
56
  import { Config } from '@travetto/config';
53
57
  import { Field } from '@travetto/schema';
54
58
 
@@ -90,8 +94,7 @@ export class MongoModelConfig {
90
94
  * Mongo client options
91
95
  */
92
96
  @Field(Object)
93
- options: mongo.MongoClientOptions = {
94
- };
97
+ options: mongo.MongoClientOptions = {};
95
98
 
96
99
  /**
97
100
  * Should we auto create the db
@@ -107,7 +110,7 @@ export class MongoModelConfig {
107
110
  * Load all the ssl certs as needed
108
111
  */
109
112
  async postConstruct(): Promise<void> {
110
- const resources = new CommonFileResourceProvider();
113
+ const resources = new FileResourceProvider({ includeCommon: true });
111
114
  const resolve = (file: string): Promise<string> => resources.describe(file).then(({ path }) => path, () => file);
112
115
 
113
116
  const opts = this.options;
@@ -149,4 +152,4 @@ export class MongoModelConfig {
149
152
  standard [Configuration](https://github.com/travetto/travetto/tree/main/module/config#readme "Configuration support")resolution paths.
150
153
 
151
154
 
152
- The SSL file options in `clientOptions` will automatically be resolved to files when given a path. This path can be a [FileResourceProvider](https://github.com/travetto/travetto/tree/main/module/base/src/resource.ts#L40) path or just a standard file path.
155
+ The SSL file options in `clientOptions` will automatically be resolved to files when given a path. This path can be a [FileResourceProvider](https://github.com/travetto/travetto/tree/main/module/base/src/resource.ts#L46) path or just a standard file path.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-mongo",
3
- "version": "3.0.0-rc.9",
3
+ "version": "3.0.1-rc.1",
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.0.0-rc.9",
29
- "@travetto/model": "^3.0.0-rc.9",
30
- "@travetto/model-query": "^3.0.0-rc.9",
31
- "mongodb": "^4.13.0"
28
+ "@travetto/config": "^3.0.1-rc.1",
29
+ "@travetto/model": "^3.0.1-rc.1",
30
+ "@travetto/model-query": "^3.0.1-rc.1",
31
+ "mongodb": "^5.0.1"
32
32
  },
33
33
  "peerDependencies": {
34
- "@travetto/command": "^3.0.0-rc.7"
34
+ "@travetto/command": "^3.0.1-rc.1"
35
35
  },
36
36
  "peerDependenciesMeta": {
37
37
  "@travetto/command": {
package/src/config.ts CHANGED
@@ -1,4 +1,4 @@
1
- import * as mongo from 'mongodb';
1
+ import type mongo from 'mongodb';
2
2
 
3
3
  import { FileResourceProvider, TimeSpan } from '@travetto/base';
4
4
  import { Config } from '@travetto/config';
@@ -42,8 +42,7 @@ export class MongoModelConfig {
42
42
  * Mongo client options
43
43
  */
44
44
  @Field(Object)
45
- options: mongo.MongoClientOptions = {
46
- };
45
+ options: mongo.MongoClientOptions = {};
47
46
 
48
47
  /**
49
48
  * Should we auto create the db
@@ -49,7 +49,7 @@ export class MongoUtil {
49
49
  } else if (id instanceof mongo.ObjectId) {
50
50
  return id.toHexString();
51
51
  } else {
52
- return id.buffer.toString('hex');
52
+ return Buffer.from(id.buffer).toString('hex');
53
53
  }
54
54
  }
55
55
 
package/src/service.ts CHANGED
@@ -1,4 +1,4 @@
1
- /* eslint-disable @typescript-eslint/consistent-type-assertions */
1
+ // Wildcard import needed here due to packaging issues
2
2
  import * as mongo from 'mongodb';
3
3
  import { Readable } from 'stream';
4
4
 
@@ -194,6 +194,7 @@ export class MongoModelService implements
194
194
  if (!result.insertedId) {
195
195
  throw new ExistsError(cls, cleaned.id);
196
196
  }
197
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
197
198
  delete (cleaned as { _id?: unknown })._id;
198
199
  return cleaned;
199
200
  }
@@ -245,9 +246,11 @@ export class MongoModelService implements
245
246
  .entries(items)
246
247
  .reduce<Record<string, unknown>>((acc, [k, v]) => {
247
248
  if (v === null || v === undefined) {
249
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
248
250
  const o = (acc.$unset ??= {}) as Record<string, unknown>;
249
251
  o[k] = v;
250
252
  } else {
253
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
251
254
  const o = (acc.$set ??= {}) as Record<string, unknown>;
252
255
  o[k] = v;
253
256
  }
@@ -347,6 +350,7 @@ export class MongoModelService implements
347
350
 
348
351
  for (const op of operations) {
349
352
  if (op.insert) {
353
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
350
354
  bulk.insert(MongoUtil.preInsertId(op.insert as T));
351
355
  } else if (op.upsert) {
352
356
  bulk.find({ _id: MongoUtil.uuid(op.upsert.id!) }).upsert().updateOne({ $set: op.upsert });
@@ -361,9 +365,11 @@ export class MongoModelService implements
361
365
 
362
366
  for (const op of operations) {
363
367
  if (op.insert) {
368
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
364
369
  MongoUtil.postLoadId(op.insert as T);
365
370
  }
366
371
  }
372
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
367
373
  for (const { index, _id } of res.getUpsertedIds() as { index: number, _id: mongo.ObjectId }[]) {
368
374
  out.insertedIds.set(index, MongoUtil.idToString(_id));
369
375
  }
@@ -379,6 +385,7 @@ export class MongoModelService implements
379
385
  out.errors = res.getWriteErrors();
380
386
  for (const err of out.errors) {
381
387
  const op = operations[err.index];
388
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
382
389
  const k = Object.keys(op)[0] as keyof BulkResponse['counts'];
383
390
  out.counts[k] -= 1;
384
391
  }
@@ -400,6 +407,7 @@ export class MongoModelService implements
400
407
  const result = await store.findOne(
401
408
  this.getWhere(
402
409
  cls,
410
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
403
411
  ModelIndexedUtil.projectIndex(cls, idx, body) as WhereClause<T>
404
412
  )
405
413
  );
@@ -415,6 +423,7 @@ export class MongoModelService implements
415
423
  const result = await store.deleteOne(
416
424
  this.getWhere(
417
425
  cls,
426
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
418
427
  ModelIndexedUtil.projectIndex(cls, idx, body) as WhereClause<T>
419
428
  )
420
429
  );
@@ -436,14 +445,17 @@ export class MongoModelService implements
436
445
  throw new AppError('Cannot list on unique indices', 'data');
437
446
  }
438
447
 
448
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
439
449
  const where = this.getWhere(
440
450
  cls,
451
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
441
452
  ModelIndexedUtil.projectIndex(cls, idx, body, { emptySortValue: { $exists: true } }) as WhereClause<T>
442
- ) as mongo.Filter<T>;
453
+ ) as mongo.Filter<mongo.Document>;
443
454
 
444
455
  const cursor = store.find(where, { timeout: true }).batchSize(100).sort(asFielded(idxCfg)[IdxFieldsⲐ]);
445
456
 
446
457
  for await (const el of cursor) {
458
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
447
459
  yield (await MongoUtil.postLoadId(await ModelCrudUtil.load(cls, el))) as T;
448
460
  }
449
461
  }
@@ -505,9 +517,11 @@ export class MongoModelService implements
505
517
  const items = MongoUtil.extractSimple(data);
506
518
  const final = Object.entries(items).reduce<Record<string, unknown>>((acc, [k, v]) => {
507
519
  if (v === null || v === undefined) {
520
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
508
521
  const o = (acc.$unset = acc.$unset ?? {}) as Record<string, unknown>;
509
522
  o[k] = v;
510
523
  } else {
524
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
511
525
  const o = (acc.$set = acc.$set ?? {}) as Record<string, unknown>;
512
526
  o[k] = v;
513
527
  }
@@ -524,6 +538,7 @@ export class MongoModelService implements
524
538
  const col = await this.getStore(cls);
525
539
  const pipeline: object[] = [{
526
540
  $group: {
541
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
527
542
  _id: `$${field as string}`,
528
543
  count: {
529
544
  $sum: 1
@@ -539,6 +554,7 @@ export class MongoModelService implements
539
554
 
540
555
  pipeline.unshift({ $match: q });
541
556
 
557
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
542
558
  const result = (await col.aggregate(pipeline).toArray()) as { _id: mongo.ObjectId, count: number }[];
543
559
 
544
560
  return result.map(val => ({
@@ -1,9 +1,9 @@
1
1
  import { Env } from '@travetto/base';
2
- import type { Service } from '@travetto/command/support/bin/service';
2
+ import type { CommandService } from '@travetto/command';
3
3
 
4
- const version = Env.get('MONGO_VERSION', '4.4');
4
+ const version = Env.get('MONGO_VERSION', '6.0');
5
5
 
6
- export const service: Service = {
6
+ export const service: CommandService = {
7
7
  name: 'mongodb',
8
8
  version,
9
9
  port: 27017,