@atscript/moost-mongo 0.1.17 → 0.1.18
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 +5 -2
- package/dist/index.cjs +22 -22
- package/dist/index.d.ts +4 -4
- package/dist/index.mjs +22 -22
- package/package.json +25 -25
package/README.md
CHANGED
|
@@ -40,7 +40,9 @@ export interface User {
|
|
|
40
40
|
|
|
41
41
|
```ts
|
|
42
42
|
import { AsMongoController, CollectionController } from '@atscript/moost-mongo'
|
|
43
|
-
import
|
|
43
|
+
import { AsMongo } from '@atscript/mongo'
|
|
44
|
+
import { Provide } from 'moost'
|
|
45
|
+
import { User } from '../collections/user.collection.as'
|
|
44
46
|
|
|
45
47
|
/* Provide AsMongo connection for the controller */
|
|
46
48
|
@Provide(AsMongo, () => new AsMongo(process.env.MONGO_URI!))
|
|
@@ -85,6 +87,7 @@ Base class you extend. **T** is the atscript constructor exported from the
|
|
|
85
87
|
| --------------------------- | ------------------------------------ | --------------------------------------- |
|
|
86
88
|
| `protected init()` | Once at controller creation | Create indexes, seed data |
|
|
87
89
|
| `transformProjection()` | Before running `find()` | Force whitelist / blacklist projections |
|
|
90
|
+
| `transformFilter(filter)` | Before every query | Rewrite or restrict query filters |
|
|
88
91
|
| `validate*Controls()` | Per endpoint | Custom URLQL control validation |
|
|
89
92
|
| `onRemove(id, opts)` | Before `deleteOne` | Soft‑delete or veto |
|
|
90
93
|
| `onWrite(action,data,opts)` | Before any insert / update / replace | Auto‑populate fields, audit, veto |
|
|
@@ -171,4 +174,4 @@ export class UsersController extends AsMongoController<typeof User> {
|
|
|
171
174
|
|
|
172
175
|
## License
|
|
173
176
|
|
|
174
|
-
|
|
177
|
+
ISC © 2025 Artem Maltsev
|
package/dist/index.cjs
CHANGED
|
@@ -22,12 +22,21 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
22
22
|
}) : target, mod));
|
|
23
23
|
|
|
24
24
|
//#endregion
|
|
25
|
+
const __atscript_typescript_utils = __toESM(require("@atscript/typescript/utils"));
|
|
25
26
|
const __moostjs_event_http = __toESM(require("@moostjs/event-http"));
|
|
26
27
|
const moost = __toESM(require("moost"));
|
|
27
28
|
const urlql = __toESM(require("urlql"));
|
|
28
|
-
const __atscript_typescript_utils = __toESM(require("@atscript/typescript/utils"));
|
|
29
29
|
const __atscript_mongo = __toESM(require("@atscript/mongo"));
|
|
30
30
|
|
|
31
|
+
//#region packages/moost-mongo/src/decorators.ts
|
|
32
|
+
const COLLECTION_DEF = "__atscript_mongo_collection_def";
|
|
33
|
+
const CollectionController = (type, prefix) => (0, moost.ApplyDecorators)((0, moost.Provide)(COLLECTION_DEF, () => type), (0, moost.Controller)(prefix || type.metadata.get("mongo.collection") || type.name), (0, moost.Inherit)());
|
|
34
|
+
const InjectCollection = (type) => (0, moost.Resolve)(async ({ instantiate }) => {
|
|
35
|
+
const asMongo = await instantiate(__atscript_mongo.AsMongo);
|
|
36
|
+
return asMongo.getCollection(type);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
31
40
|
//#region packages/moost-mongo/src/dto/controls.dto.as.js
|
|
32
41
|
function _define_property$1(obj, key, value) {
|
|
33
42
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
@@ -93,15 +102,6 @@ _define_property$1(SelectControlDto, "metadata", new Map());
|
|
|
93
102
|
(0, __atscript_typescript_utils.defineAnnotatedType)("object", SortControlDto).propPattern(/./, (0, __atscript_typescript_utils.defineAnnotatedType)("union").item((0, __atscript_typescript_utils.defineAnnotatedType)().designType("number").value(1).$type).item((0, __atscript_typescript_utils.defineAnnotatedType)().designType("number").value(-1).$type).$type);
|
|
94
103
|
(0, __atscript_typescript_utils.defineAnnotatedType)("object", SelectControlDto).propPattern(/./, (0, __atscript_typescript_utils.defineAnnotatedType)("union").item((0, __atscript_typescript_utils.defineAnnotatedType)().designType("number").value(1).$type).item((0, __atscript_typescript_utils.defineAnnotatedType)().designType("number").value(0).$type).$type);
|
|
95
104
|
|
|
96
|
-
//#endregion
|
|
97
|
-
//#region packages/moost-mongo/src/decorators.ts
|
|
98
|
-
const COLLECTION_DEF = "__atscript_mongo_collection_def";
|
|
99
|
-
const CollectionController = (type, prefix) => (0, moost.ApplyDecorators)((0, moost.Provide)(COLLECTION_DEF, () => type), (0, moost.Controller)(prefix || type.metadata.get("mongo.collection") || type.name), (0, moost.Inherit)());
|
|
100
|
-
const InjectCollection = (type) => (0, moost.Resolve)(async ({ instantiate }) => {
|
|
101
|
-
const asMongo = await instantiate(__atscript_mongo.AsMongo);
|
|
102
|
-
return asMongo.getCollection(type);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
105
|
//#endregion
|
|
106
106
|
//#region packages/moost-mongo/src/as-mongo.controller.ts
|
|
107
107
|
function _define_property(obj, key, value) {
|
|
@@ -205,14 +205,14 @@ var AsMongoController = class {
|
|
|
205
205
|
try {
|
|
206
206
|
const error = await controlsValidators[controlsType](parsed.controls);
|
|
207
207
|
if (error) return new __moostjs_event_http.HttpError(400, error);
|
|
208
|
-
} catch (
|
|
209
|
-
return new __moostjs_event_http.HttpError(400,
|
|
208
|
+
} catch (error) {
|
|
209
|
+
return new __moostjs_event_http.HttpError(400, error.message);
|
|
210
210
|
}
|
|
211
211
|
try {
|
|
212
212
|
const error = await this.validateInsights(parsed.insights);
|
|
213
213
|
if (error) return new __moostjs_event_http.HttpError(400, error);
|
|
214
|
-
} catch (
|
|
215
|
-
return new __moostjs_event_http.HttpError(400,
|
|
214
|
+
} catch (error) {
|
|
215
|
+
return new __moostjs_event_http.HttpError(400, error.message);
|
|
216
216
|
}
|
|
217
217
|
}
|
|
218
218
|
/**
|
|
@@ -354,7 +354,7 @@ else pipeline.push({ $limit: 1e3 });
|
|
|
354
354
|
const idValidator = this.asCollection.flatMap.get("_id")?.validator();
|
|
355
355
|
const query = url.split("?").slice(1).join("?");
|
|
356
356
|
const parsed = (0, urlql.parseUrlql)(query);
|
|
357
|
-
if (Object.keys(parsed.filter).length) return new __moostjs_event_http.HttpError(400, "Filtering is not allowed for \"one\" endpoint");
|
|
357
|
+
if (Object.keys(parsed.filter).length > 0) return new __moostjs_event_http.HttpError(400, "Filtering is not allowed for \"one\" endpoint");
|
|
358
358
|
const error = await this.validateUrlql(parsed, "getOne");
|
|
359
359
|
if (error) return error;
|
|
360
360
|
if (idValidator?.validate(id, true)) return this.returnOne(this.asCollection.collection.find(this.transformFilter({ _id: this.asCollection.prepareId(id) }), this.prepareQueryOptions(parsed.controls)).toArray());
|
|
@@ -468,12 +468,12 @@ else return new __moostjs_event_http.HttpError(500, "Not saved");
|
|
|
468
468
|
this.logger.info(`Initializing Collection`);
|
|
469
469
|
try {
|
|
470
470
|
const p = this.init();
|
|
471
|
-
if (p instanceof Promise) p.catch((
|
|
472
|
-
this.logger.error(
|
|
471
|
+
if (p instanceof Promise) p.catch((error) => {
|
|
472
|
+
this.logger.error(error);
|
|
473
473
|
});
|
|
474
|
-
} catch (
|
|
475
|
-
this.logger.error(
|
|
476
|
-
throw
|
|
474
|
+
} catch (error) {
|
|
475
|
+
this.logger.error(error);
|
|
476
|
+
throw error;
|
|
477
477
|
}
|
|
478
478
|
}
|
|
479
479
|
};
|
|
@@ -531,9 +531,9 @@ AsMongoController = _ts_decorate([
|
|
|
531
531
|
_ts_param(1, (0, moost.Inject)(COLLECTION_DEF)),
|
|
532
532
|
_ts_metadata("design:type", Function),
|
|
533
533
|
_ts_metadata("design:paramtypes", [
|
|
534
|
-
typeof
|
|
534
|
+
typeof AsMongo === "undefined" ? Object : AsMongo,
|
|
535
535
|
typeof T === "undefined" ? Object : T,
|
|
536
|
-
typeof
|
|
536
|
+
typeof Moost === "undefined" ? Object : Moost
|
|
537
537
|
])
|
|
538
538
|
], AsMongoController);
|
|
539
539
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { HttpError } from '@moostjs/event-http';
|
|
2
|
-
import { TConsoleBase, Moost } from 'moost';
|
|
3
|
-
import { UrlqlQuery } from 'urlql';
|
|
4
1
|
import { AsMongo, AsCollection } from '@atscript/mongo';
|
|
5
2
|
import { TAtscriptAnnotatedType, Validator, ValidatorError } from '@atscript/typescript/utils';
|
|
3
|
+
import { HttpError } from '@moostjs/event-http';
|
|
6
4
|
import { Document, Filter, WithId, InsertOneResult, InsertManyResult, UpdateResult, DeleteResult, DeleteOptions, ObjectId, InsertOneOptions, BulkWriteOptions, ReplaceOptions, UpdateFilter, UpdateOptions } from 'mongodb';
|
|
5
|
+
import { TConsoleBase, Moost } from 'moost';
|
|
6
|
+
import { UrlqlQuery } from 'urlql';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Generic **Moost** controller that exposes a full REST‑style CRUD surface over a
|
|
@@ -192,7 +192,7 @@ declare class AsMongoController<T extends TAtscriptAnnotatedType = TAtscriptAnno
|
|
|
192
192
|
* @param result - Promise resolving to an array of documents.
|
|
193
193
|
* @returns Document, 400 or 404 { @link HttpError }.
|
|
194
194
|
*/
|
|
195
|
-
protected returnOne(result: Promise<WithId<DataType
|
|
195
|
+
protected returnOne(result: Promise<Array<WithId<DataType>>>): Promise<DataType | HttpError>;
|
|
196
196
|
/**
|
|
197
197
|
* **POST /** – inserts one or many documents.
|
|
198
198
|
*
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
|
+
import { ValidatorError, defineAnnotatedType } from "@atscript/typescript/utils";
|
|
1
2
|
import { Body, Delete, Get, HttpError, Patch, Post, Put, Url } from "@moostjs/event-http";
|
|
2
|
-
import { ApplyDecorators, Controller, Inherit, Inject,
|
|
3
|
+
import { ApplyDecorators, Controller, Inherit, Inject, Param, Provide, Resolve } from "moost";
|
|
3
4
|
import { parseUrlql } from "urlql";
|
|
4
|
-
import {
|
|
5
|
-
|
|
5
|
+
import { AsMongo as AsMongo$1 } from "@atscript/mongo";
|
|
6
|
+
|
|
7
|
+
//#region packages/moost-mongo/src/decorators.ts
|
|
8
|
+
const COLLECTION_DEF = "__atscript_mongo_collection_def";
|
|
9
|
+
const CollectionController = (type, prefix) => ApplyDecorators(Provide(COLLECTION_DEF, () => type), Controller(prefix || type.metadata.get("mongo.collection") || type.name), Inherit());
|
|
10
|
+
const InjectCollection = (type) => Resolve(async ({ instantiate }) => {
|
|
11
|
+
const asMongo = await instantiate(AsMongo$1);
|
|
12
|
+
return asMongo.getCollection(type);
|
|
13
|
+
});
|
|
6
14
|
|
|
15
|
+
//#endregion
|
|
7
16
|
//#region packages/moost-mongo/src/dto/controls.dto.as.js
|
|
8
17
|
function _define_property$1(obj, key, value) {
|
|
9
18
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
@@ -69,15 +78,6 @@ defineAnnotatedType("object", GetOneControlsDto).prop("$select", defineAnnotated
|
|
|
69
78
|
defineAnnotatedType("object", SortControlDto).propPattern(/./, defineAnnotatedType("union").item(defineAnnotatedType().designType("number").value(1).$type).item(defineAnnotatedType().designType("number").value(-1).$type).$type);
|
|
70
79
|
defineAnnotatedType("object", SelectControlDto).propPattern(/./, defineAnnotatedType("union").item(defineAnnotatedType().designType("number").value(1).$type).item(defineAnnotatedType().designType("number").value(0).$type).$type);
|
|
71
80
|
|
|
72
|
-
//#endregion
|
|
73
|
-
//#region packages/moost-mongo/src/decorators.ts
|
|
74
|
-
const COLLECTION_DEF = "__atscript_mongo_collection_def";
|
|
75
|
-
const CollectionController = (type, prefix) => ApplyDecorators(Provide(COLLECTION_DEF, () => type), Controller(prefix || type.metadata.get("mongo.collection") || type.name), Inherit());
|
|
76
|
-
const InjectCollection = (type) => Resolve(async ({ instantiate }) => {
|
|
77
|
-
const asMongo = await instantiate(AsMongo);
|
|
78
|
-
return asMongo.getCollection(type);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
81
|
//#endregion
|
|
82
82
|
//#region packages/moost-mongo/src/as-mongo.controller.ts
|
|
83
83
|
function _define_property(obj, key, value) {
|
|
@@ -181,14 +181,14 @@ var AsMongoController = class {
|
|
|
181
181
|
try {
|
|
182
182
|
const error = await controlsValidators[controlsType](parsed.controls);
|
|
183
183
|
if (error) return new HttpError(400, error);
|
|
184
|
-
} catch (
|
|
185
|
-
return new HttpError(400,
|
|
184
|
+
} catch (error) {
|
|
185
|
+
return new HttpError(400, error.message);
|
|
186
186
|
}
|
|
187
187
|
try {
|
|
188
188
|
const error = await this.validateInsights(parsed.insights);
|
|
189
189
|
if (error) return new HttpError(400, error);
|
|
190
|
-
} catch (
|
|
191
|
-
return new HttpError(400,
|
|
190
|
+
} catch (error) {
|
|
191
|
+
return new HttpError(400, error.message);
|
|
192
192
|
}
|
|
193
193
|
}
|
|
194
194
|
/**
|
|
@@ -330,7 +330,7 @@ else pipeline.push({ $limit: 1e3 });
|
|
|
330
330
|
const idValidator = this.asCollection.flatMap.get("_id")?.validator();
|
|
331
331
|
const query = url.split("?").slice(1).join("?");
|
|
332
332
|
const parsed = parseUrlql(query);
|
|
333
|
-
if (Object.keys(parsed.filter).length) return new HttpError(400, "Filtering is not allowed for \"one\" endpoint");
|
|
333
|
+
if (Object.keys(parsed.filter).length > 0) return new HttpError(400, "Filtering is not allowed for \"one\" endpoint");
|
|
334
334
|
const error = await this.validateUrlql(parsed, "getOne");
|
|
335
335
|
if (error) return error;
|
|
336
336
|
if (idValidator?.validate(id, true)) return this.returnOne(this.asCollection.collection.find(this.transformFilter({ _id: this.asCollection.prepareId(id) }), this.prepareQueryOptions(parsed.controls)).toArray());
|
|
@@ -444,12 +444,12 @@ else return new HttpError(500, "Not saved");
|
|
|
444
444
|
this.logger.info(`Initializing Collection`);
|
|
445
445
|
try {
|
|
446
446
|
const p = this.init();
|
|
447
|
-
if (p instanceof Promise) p.catch((
|
|
448
|
-
this.logger.error(
|
|
447
|
+
if (p instanceof Promise) p.catch((error) => {
|
|
448
|
+
this.logger.error(error);
|
|
449
449
|
});
|
|
450
|
-
} catch (
|
|
451
|
-
this.logger.error(
|
|
452
|
-
throw
|
|
450
|
+
} catch (error) {
|
|
451
|
+
this.logger.error(error);
|
|
452
|
+
throw error;
|
|
453
453
|
}
|
|
454
454
|
}
|
|
455
455
|
};
|
package/package.json
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/moost-mongo",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.18",
|
|
4
4
|
"description": "Atscript Mongo for Moost.",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "dist/index.mjs",
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"types": "./dist/index.d.ts",
|
|
11
|
-
"import": "./dist/index.mjs",
|
|
12
|
-
"require": "./dist/index.cjs"
|
|
13
|
-
},
|
|
14
|
-
"./package.json": "./package.json"
|
|
15
|
-
},
|
|
16
|
-
"files": [
|
|
17
|
-
"dist"
|
|
18
|
-
],
|
|
19
5
|
"keywords": [
|
|
20
|
-
"atscript",
|
|
21
6
|
"annotations",
|
|
22
|
-
"
|
|
7
|
+
"atscript",
|
|
8
|
+
"mongo",
|
|
23
9
|
"moost",
|
|
24
|
-
"
|
|
10
|
+
"typescript"
|
|
25
11
|
],
|
|
12
|
+
"homepage": "https://github.com/moostjs/atscript/tree/main/packages/moost-mongo#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/moostjs/atscript/issues"
|
|
15
|
+
},
|
|
16
|
+
"license": "ISC",
|
|
26
17
|
"author": "Artem Maltsev",
|
|
27
18
|
"repository": {
|
|
28
19
|
"type": "git",
|
|
29
20
|
"url": "git+https://github.com/moostjs/atscript.git",
|
|
30
21
|
"directory": "packages/moost-mongo"
|
|
31
22
|
},
|
|
32
|
-
"
|
|
33
|
-
"
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"main": "dist/index.mjs",
|
|
28
|
+
"types": "dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"import": "./dist/index.mjs",
|
|
33
|
+
"require": "./dist/index.cjs"
|
|
34
|
+
},
|
|
35
|
+
"./package.json": "./package.json"
|
|
34
36
|
},
|
|
35
|
-
"homepage": "https://github.com/moostjs/atscript/tree/main/packages/moost-mongo#readme",
|
|
36
|
-
"license": "ISC",
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"urlql": "^0.0.5"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"vitest": "3.2.4",
|
|
42
|
-
"@atscript/core": "^0.1.
|
|
42
|
+
"@atscript/core": "^0.1.18"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"@moostjs/event-http": "^0.5.32",
|
|
46
46
|
"mongodb": "^6.17.0",
|
|
47
47
|
"moost": "^0.5.32",
|
|
48
|
-
"@atscript/mongo": "^0.1.
|
|
49
|
-
"@atscript/typescript": "^0.1.
|
|
48
|
+
"@atscript/mongo": "^0.1.18",
|
|
49
|
+
"@atscript/typescript": "^0.1.18"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"pub": "pnpm publish --access public",
|