@atscript/moost-mongo 0.0.18 → 0.0.20

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
@@ -104,6 +104,28 @@ Decorator that glues your subclass to Moost:
104
104
  export class UsersController extends AsMongoController<typeof User> {}
105
105
  ```
106
106
 
107
+ ### Injecting the collection in services
108
+
109
+ When you need raw collection access outside the generated controller,
110
+ use `@InjectCollection`:
111
+
112
+ ```ts
113
+ @Injectable()
114
+ export class AuditService {
115
+ constructor(
116
+ @InjectCollection(User)
117
+ private users: AsCollection<typeof User>
118
+ ) {}
119
+
120
+ async purgeSoftDeleted() {
121
+ await this.users.collection.deleteMany({ deleted: true })
122
+ }
123
+ }
124
+ ```
125
+
126
+ AsMongo is resolved automatically from DI, so make sure it is provided
127
+ globally (see Quick start).
128
+
107
129
  ---
108
130
 
109
131
  ## Route reference
package/dist/index.cjs CHANGED
@@ -79,6 +79,10 @@ const COLLECTION_DEF = "__atscript_mongo_collection_def";
79
79
  const CollectionController = (type, prefix) => {
80
80
  return (0, moost.ApplyDecorators)((0, moost.Provide)(COLLECTION_DEF, () => type), (0, moost.Controller)(prefix || type.metadata.get("mongo.collection") || type.name), (0, moost.Inherit)());
81
81
  };
82
+ const InjectCollection = (type) => (0, moost.Resolve)(async ({ instantiate }) => {
83
+ const asMongo = await instantiate(__atscript_mongo.AsMongo);
84
+ return asMongo.getCollection(type);
85
+ });
82
86
 
83
87
  //#endregion
84
88
  //#region packages/moost-mongo/src/as-mongo.controller.ts
@@ -462,4 +466,5 @@ Object.defineProperty(exports, 'AsMongoController', {
462
466
  }
463
467
  });
464
468
  exports.COLLECTION_DEF = COLLECTION_DEF
465
- exports.CollectionController = CollectionController
469
+ exports.CollectionController = CollectionController
470
+ exports.InjectCollection = InjectCollection
package/dist/index.d.ts CHANGED
@@ -162,19 +162,19 @@ declare class AsMongoController<T extends TAtscriptAnnotatedTypeConstructor> {
162
162
  *
163
163
  * @param payload - Raw request body to be inserted.
164
164
  */
165
- insert(payload: any): Promise<HttpError | InsertOneResult | InsertManyResult>;
165
+ insert(payload: Parameters<AsCollection<T>['prepareInsert']>[0]): Promise<HttpError | InsertOneResult | InsertManyResult>;
166
166
  /**
167
167
  * **PUT /** – fully replaces a document matched by `_id`.
168
168
  *
169
169
  * @param payload - Object containing `_id` plus full replacement document.
170
170
  */
171
- replace(payload: any): Promise<HttpError | UpdateResult<InstanceType<T>>>;
171
+ replace(payload: Parameters<AsCollection<T>['prepareReplace']>[0]): Promise<HttpError | UpdateResult<InstanceType<T>>>;
172
172
  /**
173
173
  * **PATCH /** – updates one document using MongoDB update operators.
174
174
  *
175
175
  * @param payload - Update payload produced by `asCollection.prepareUpdate`.
176
176
  */
177
- update(payload: any): Promise<HttpError | UpdateResult>;
177
+ update(payload: Parameters<AsCollection<T>['prepareUpdate']>[0]): Promise<HttpError | UpdateResult>;
178
178
  /**
179
179
  * **DELETE /:id** – removes a single document by `_id`.
180
180
  *
@@ -229,5 +229,27 @@ declare const COLLECTION_DEF = "__atscript_mongo_collection_def";
229
229
  * ```
230
230
  */
231
231
  declare const CollectionController: (type: TAtscriptAnnotatedTypeConstructor, prefix?: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
232
+ /**
233
+ * Parameter decorator that injects the lazily-resolved {@link AsCollection}
234
+ * instance for a given AtScript model.
235
+ *
236
+ * > `AsMongo` **must** be provided in the current DI scope
237
+ * > (e.g. `@Provide(AsMongo, () => new AsMongo(url))`).
238
+ *
239
+ * @param type AtScript-annotated constructor produced by
240
+ * `@mongo.collection`.
241
+ *
242
+ * @example
243
+ * ```ts
244
+ * ‎@Injectable()
245
+ * export class SomeProvider {
246
+ * constructor(
247
+ * ‎@InjectCollection(User)
248
+ * private users: AsCollection<typeof User>
249
+ * ) {}
250
+ * }
251
+ * ```
252
+ */
253
+ declare const InjectCollection: (type: TAtscriptAnnotatedTypeConstructor) => ParameterDecorator & PropertyDecorator;
232
254
 
233
- export { AsMongoController, COLLECTION_DEF, CollectionController };
255
+ export { AsMongoController, COLLECTION_DEF, CollectionController, InjectCollection };
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Body, Delete, Get, HttpError, Patch, Post, Put, Url } from "@moostjs/event-http";
2
- import { ApplyDecorators, Controller, Inherit, Inject, Moost, Param, Provide } from "moost";
2
+ import { ApplyDecorators, Controller, Inherit, Inject, Moost, Param, Provide, Resolve } from "moost";
3
3
  import { parseUrlql } from "urlql";
4
4
  import { ValidatorError, defineAnnotatedType } from "@atscript/typescript";
5
5
  import { AsMongo } from "@atscript/mongo";
@@ -55,6 +55,10 @@ const COLLECTION_DEF = "__atscript_mongo_collection_def";
55
55
  const CollectionController = (type, prefix) => {
56
56
  return ApplyDecorators(Provide(COLLECTION_DEF, () => type), Controller(prefix || type.metadata.get("mongo.collection") || type.name), Inherit());
57
57
  };
58
+ const InjectCollection = (type) => Resolve(async ({ instantiate }) => {
59
+ const asMongo = await instantiate(AsMongo);
60
+ return asMongo.getCollection(type);
61
+ });
58
62
 
59
63
  //#endregion
60
64
  //#region packages/moost-mongo/src/as-mongo.controller.ts
@@ -431,4 +435,4 @@ AsMongoController = _ts_decorate([
431
435
  ], AsMongoController);
432
436
 
433
437
  //#endregion
434
- export { AsMongoController, COLLECTION_DEF, CollectionController };
438
+ export { AsMongoController, COLLECTION_DEF, CollectionController, InjectCollection };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/moost-mongo",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
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.30",
44
+ "@moostjs/event-http": "^0.5.31",
45
45
  "mongodb": "^6.17.0",
46
- "moost": "^0.5.30",
47
- "@atscript/mongo": "^0.0.18",
48
- "@atscript/typescript": "^0.0.18"
46
+ "moost": "^0.5.31",
47
+ "@atscript/mongo": "^0.0.20",
48
+ "@atscript/typescript": "^0.0.20"
49
49
  },
50
50
  "scripts": {
51
51
  "pub": "pnpm publish --access public",