@atscript/moost-mongo 0.0.21 → 0.0.23

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/dist/index.cjs CHANGED
@@ -297,9 +297,9 @@ else pipeline.push({ $limit: 1e3 });
297
297
  meta: [{ $count: "count" }]
298
298
  } });
299
299
  const result = await this.asCollection.collection.aggregate(pipeline).toArray();
300
- const totalDocuments = result[0].meta[0].count;
300
+ const totalDocuments = result[0]?.meta[0]?.count || 0;
301
301
  return {
302
- documents: result[0].documents,
302
+ documents: result[0]?.documents || [],
303
303
  page,
304
304
  size,
305
305
  totalPages: Math.ceil(totalDocuments / size),
@@ -322,11 +322,11 @@ else pipeline.push({ $limit: 1e3 });
322
322
  if (Object.keys(parsed.filter).length) return new __moostjs_event_http.HttpError(400, "Filtering is not allowed for \"one\" endpoint");
323
323
  const error = await this.validateUrlql(parsed, "getOne");
324
324
  if (error) return error;
325
- if (idValidator?.validate(id, true)) return this.returnOne(this.asCollection.collection.find({ _id: this.asCollection.prepareId(id) }, this.prepareQueryOptions(parsed.controls)).toArray());
325
+ if (idValidator?.validate(id, true)) return this.returnOne(this.asCollection.collection.find(this.transformFilter({ _id: this.asCollection.prepareId(id) }), this.prepareQueryOptions(parsed.controls)).toArray());
326
326
  else if (this.asCollection.uniqueProps.size > 0) {
327
327
  const filter = [];
328
328
  for (const prop of this.asCollection.uniqueProps) filter.push({ [prop]: id });
329
- return this.returnOne(this.asCollection.collection.find({ $or: filter }, this.prepareQueryOptions(parsed.controls)).toArray());
329
+ return this.returnOne(this.asCollection.collection.find(this.transformFilter({ $or: filter }), this.prepareQueryOptions(parsed.controls)).toArray());
330
330
  }
331
331
  if (idValidator) return new __atscript_typescript.ValidatorError(idValidator.errors);
332
332
  return new __moostjs_event_http.HttpError(500, "Unknown error");
@@ -369,7 +369,7 @@ else return new __moostjs_event_http.HttpError(500, "Not saved");
369
369
  */ async replace(payload) {
370
370
  const args = this.asCollection.prepareReplace(payload).toArgs();
371
371
  const newData = await this.onWrite("replace", args[1], args[2]);
372
- if (newData) return this.asCollection.collection.replaceOne(args[0], newData, args[2]);
372
+ if (newData) return this.asCollection.collection.replaceOne(this.transformFilter(args[0]), newData, args[2]);
373
373
  return new __moostjs_event_http.HttpError(500, "Not saved");
374
374
  }
375
375
  /**
@@ -379,7 +379,7 @@ else return new __moostjs_event_http.HttpError(500, "Not saved");
379
379
  */ async update(payload) {
380
380
  const args = this.asCollection.prepareUpdate(payload).toArgs();
381
381
  const newData = await this.onWrite("update", args[1], args[2]);
382
- if (newData) return this.asCollection.collection.updateOne(args[0], newData, args[2]);
382
+ if (newData) return this.asCollection.collection.updateOne(this.transformFilter(args[0]), newData, args[2]);
383
383
  return new __moostjs_event_http.HttpError(500, "Not saved");
384
384
  }
385
385
  /**
@@ -390,7 +390,7 @@ else return new __moostjs_event_http.HttpError(500, "Not saved");
390
390
  const opts = {};
391
391
  id = await this.onRemove(id, opts);
392
392
  if (id !== undefined) {
393
- const result = await this.asCollection.collection.deleteOne({ _id: this.asCollection.prepareId(id) }, opts);
393
+ const result = await this.asCollection.collection.deleteOne(this.transformFilter({ _id: this.asCollection.prepareId(id) }), opts);
394
394
  if (result.deletedCount < 1) throw new __moostjs_event_http.HttpError(404);
395
395
  return result;
396
396
  }
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ import { TConsoleBase, Moost } from 'moost';
3
3
  import { UrlqlQuery } from 'urlql';
4
4
  import { AsMongo, AsCollection } from '@atscript/mongo';
5
5
  import { TAtscriptAnnotatedTypeConstructor, ValidatorError } from '@atscript/typescript';
6
- import { Document, WithId, InsertOneResult, InsertManyResult, UpdateResult, DeleteResult, DeleteOptions, ObjectId, OptionalUnlessRequiredId, InsertOneOptions, BulkWriteOptions, WithoutId, ReplaceOptions, UpdateFilter, UpdateOptions } from 'mongodb';
6
+ import { Document, Filter, WithId, InsertOneResult, InsertManyResult, UpdateResult, DeleteResult, DeleteOptions, ObjectId, OptionalUnlessRequiredId, InsertOneOptions, BulkWriteOptions, WithoutId, ReplaceOptions, UpdateFilter, UpdateOptions } from 'mongodb';
7
7
 
8
8
  /**
9
9
  * Generic **Moost** controller that exposes a full REST‑style CRUD surface over a
@@ -112,7 +112,7 @@ declare class AsMongoController<T extends TAtscriptAnnotatedTypeConstructor> {
112
112
  * @param filter - The original filter object.
113
113
  * @returns The transformed filter object (may return `Promise`).
114
114
  */
115
- protected transformFilter(filter: Document): Document;
115
+ protected transformFilter(filter: Document): Filter<InstanceType<T>>;
116
116
  /**
117
117
  * Builds MongoDB `FindOptions` object out of URLQL controls.
118
118
  *
package/dist/index.mjs CHANGED
@@ -273,9 +273,9 @@ else pipeline.push({ $limit: 1e3 });
273
273
  meta: [{ $count: "count" }]
274
274
  } });
275
275
  const result = await this.asCollection.collection.aggregate(pipeline).toArray();
276
- const totalDocuments = result[0].meta[0].count;
276
+ const totalDocuments = result[0]?.meta[0]?.count || 0;
277
277
  return {
278
- documents: result[0].documents,
278
+ documents: result[0]?.documents || [],
279
279
  page,
280
280
  size,
281
281
  totalPages: Math.ceil(totalDocuments / size),
@@ -298,11 +298,11 @@ else pipeline.push({ $limit: 1e3 });
298
298
  if (Object.keys(parsed.filter).length) return new HttpError(400, "Filtering is not allowed for \"one\" endpoint");
299
299
  const error = await this.validateUrlql(parsed, "getOne");
300
300
  if (error) return error;
301
- if (idValidator?.validate(id, true)) return this.returnOne(this.asCollection.collection.find({ _id: this.asCollection.prepareId(id) }, this.prepareQueryOptions(parsed.controls)).toArray());
301
+ if (idValidator?.validate(id, true)) return this.returnOne(this.asCollection.collection.find(this.transformFilter({ _id: this.asCollection.prepareId(id) }), this.prepareQueryOptions(parsed.controls)).toArray());
302
302
  else if (this.asCollection.uniqueProps.size > 0) {
303
303
  const filter = [];
304
304
  for (const prop of this.asCollection.uniqueProps) filter.push({ [prop]: id });
305
- return this.returnOne(this.asCollection.collection.find({ $or: filter }, this.prepareQueryOptions(parsed.controls)).toArray());
305
+ return this.returnOne(this.asCollection.collection.find(this.transformFilter({ $or: filter }), this.prepareQueryOptions(parsed.controls)).toArray());
306
306
  }
307
307
  if (idValidator) return new ValidatorError(idValidator.errors);
308
308
  return new HttpError(500, "Unknown error");
@@ -345,7 +345,7 @@ else return new HttpError(500, "Not saved");
345
345
  */ async replace(payload) {
346
346
  const args = this.asCollection.prepareReplace(payload).toArgs();
347
347
  const newData = await this.onWrite("replace", args[1], args[2]);
348
- if (newData) return this.asCollection.collection.replaceOne(args[0], newData, args[2]);
348
+ if (newData) return this.asCollection.collection.replaceOne(this.transformFilter(args[0]), newData, args[2]);
349
349
  return new HttpError(500, "Not saved");
350
350
  }
351
351
  /**
@@ -355,7 +355,7 @@ else return new HttpError(500, "Not saved");
355
355
  */ async update(payload) {
356
356
  const args = this.asCollection.prepareUpdate(payload).toArgs();
357
357
  const newData = await this.onWrite("update", args[1], args[2]);
358
- if (newData) return this.asCollection.collection.updateOne(args[0], newData, args[2]);
358
+ if (newData) return this.asCollection.collection.updateOne(this.transformFilter(args[0]), newData, args[2]);
359
359
  return new HttpError(500, "Not saved");
360
360
  }
361
361
  /**
@@ -366,7 +366,7 @@ else return new HttpError(500, "Not saved");
366
366
  const opts = {};
367
367
  id = await this.onRemove(id, opts);
368
368
  if (id !== undefined) {
369
- const result = await this.asCollection.collection.deleteOne({ _id: this.asCollection.prepareId(id) }, opts);
369
+ const result = await this.asCollection.collection.deleteOne(this.transformFilter({ _id: this.asCollection.prepareId(id) }), opts);
370
370
  if (result.deletedCount < 1) throw new HttpError(404);
371
371
  return result;
372
372
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/moost-mongo",
3
- "version": "0.0.21",
3
+ "version": "0.0.23",
4
4
  "description": "Atscript Mongo for Moost.",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",
@@ -41,11 +41,11 @@
41
41
  "vitest": "3.2.4"
42
42
  },
43
43
  "peerDependencies": {
44
- "@moostjs/event-http": "^0.5.31",
44
+ "@moostjs/event-http": "^0.5.32",
45
45
  "mongodb": "^6.17.0",
46
- "moost": "^0.5.31",
47
- "@atscript/mongo": "^0.0.21",
48
- "@atscript/typescript": "^0.0.21"
46
+ "moost": "^0.5.32",
47
+ "@atscript/mongo": "^0.0.23",
48
+ "@atscript/typescript": "^0.0.23"
49
49
  },
50
50
  "scripts": {
51
51
  "pub": "pnpm publish --access public",