@travetto/model-mongo 3.1.3 → 3.1.5

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.3",
3
+ "version": "3.1.5",
4
4
  "description": "Mongo backing for the travetto model module.",
5
5
  "keywords": [
6
6
  "mongo",
@@ -26,12 +26,12 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "@travetto/config": "^3.1.2",
29
- "@travetto/model": "^3.1.5",
30
- "@travetto/model-query": "^3.1.3",
29
+ "@travetto/model": "^3.1.7",
30
+ "@travetto/model-query": "^3.1.5",
31
31
  "mongodb": "^5.0.1"
32
32
  },
33
33
  "peerDependencies": {
34
- "@travetto/command": "^3.1.1"
34
+ "@travetto/command": "^3.1.2"
35
35
  },
36
36
  "peerDependenciesMeta": {
37
37
  "@travetto/command": {
@@ -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
@@ -7,7 +7,7 @@ import {
7
7
  ModelCrudSupport, ModelStorageSupport, ModelStreamSupport,
8
8
  ModelExpirySupport, ModelBulkSupport, ModelIndexedSupport,
9
9
  StreamMeta, BulkOp, BulkResponse,
10
- NotFoundError, ExistsError, IndexConfig
10
+ NotFoundError, ExistsError, IndexConfig, PartialStream
11
11
  } from '@travetto/model';
12
12
  import {
13
13
  ModelQuery, ModelQueryCrudSupport, ModelQueryFacetSupport, ModelQuerySupport,
@@ -26,7 +26,7 @@ import { ModelQuerySuggestUtil } from '@travetto/model-query/src/internal/servic
26
26
  import { PointImpl } from '@travetto/model-query/src/internal/model/point';
27
27
  import { ModelQueryExpiryUtil } from '@travetto/model-query/src/internal/service/expiry';
28
28
  import { ModelExpiryUtil } from '@travetto/model/src/internal/service/expiry';
29
- import { StreamModel, STREAMS } from '@travetto/model/src/internal/service/stream';
29
+ import { ModelStreamUtil, StreamModel, STREAMS } from '@travetto/model/src/internal/service/stream';
30
30
  import { AllViewⲐ } from '@travetto/schema/src/internal/types';
31
31
  import { ModelBulkUtil } from '@travetto/model/src/internal/service/bulk';
32
32
 
@@ -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);
@@ -308,6 +308,20 @@ export class MongoModelService implements
308
308
  return res;
309
309
  }
310
310
 
311
+ async getStreamPartial(location: string, start: number, end?: number): Promise<PartialStream> {
312
+ const meta = await this.describeStream(location);
313
+
314
+ end ??= meta.size - 1;
315
+
316
+ ModelStreamUtil.checkRange(start, end, meta.size);
317
+
318
+ const res = await this.#bucket.openDownloadStreamByName(location, { start, end: end + 1 });
319
+ if (!res) {
320
+ throw new NotFoundError(STREAMS, location);
321
+ }
322
+ return { stream: res, range: [start, end] };
323
+ }
324
+
311
325
  async describeStream(location: string): Promise<StreamMeta> {
312
326
  return (await this.#describeStreamRaw(location)).metadata;
313
327
  }
@@ -497,6 +511,19 @@ export class MongoModelService implements
497
511
  }
498
512
 
499
513
  // Query Crud
514
+ async updateOneWithQuery<T extends ModelType>(cls: Class<T>, data: T, query: ModelQuery<T>): Promise<T> {
515
+ const col = await this.getStore(cls);
516
+ const item = await ModelCrudUtil.preStore(cls, data, this);
517
+ query = ModelQueryUtil.getQueryWithId(cls, data, query);
518
+
519
+ const { filter } = MongoUtil.prepareQuery(cls, query);
520
+ const res = await col.replaceOne(filter, item);
521
+ if (res.matchedCount === 0) {
522
+ throw new NotFoundError(cls, item.id);
523
+ }
524
+ return item;
525
+ }
526
+
500
527
  async deleteByQuery<T extends ModelType>(cls: Class<T>, query: ModelQuery<T>): Promise<number> {
501
528
  const col = await this.getStore(cls);
502
529
  const { filter } = MongoUtil.prepareQuery(cls, query, false);