@orion-js/models 3.0.24 → 3.0.26
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/lib/createModel/index.js
CHANGED
|
@@ -11,6 +11,7 @@ const modelToSchema_1 = __importDefault(require("./modelToSchema"));
|
|
|
11
11
|
const createModel = modelOptions => {
|
|
12
12
|
const name = modelOptions.name;
|
|
13
13
|
let resolvedSchema = null;
|
|
14
|
+
let resolvedCleanSchema = null;
|
|
14
15
|
let resolvedResolvers = null;
|
|
15
16
|
const getSchema = () => {
|
|
16
17
|
if (!modelOptions.schema)
|
|
@@ -24,11 +25,11 @@ const createModel = modelOptions => {
|
|
|
24
25
|
const getCleanSchema = () => {
|
|
25
26
|
if (!modelOptions.schema)
|
|
26
27
|
return {};
|
|
27
|
-
if (
|
|
28
|
-
return
|
|
28
|
+
if (resolvedCleanSchema)
|
|
29
|
+
return resolvedCleanSchema;
|
|
29
30
|
const schema = (0, resolveParam_1.default)(modelOptions.schema);
|
|
30
|
-
|
|
31
|
-
return
|
|
31
|
+
resolvedCleanSchema = (0, modelToSchema_1.default)(schema);
|
|
32
|
+
return resolvedCleanSchema;
|
|
32
33
|
};
|
|
33
34
|
const getResolvers = () => {
|
|
34
35
|
if (!modelOptions.resolvers)
|
|
@@ -51,12 +52,16 @@ const createModel = modelOptions => {
|
|
|
51
52
|
getResolvers,
|
|
52
53
|
initItem: modelInitItem,
|
|
53
54
|
validate: async (doc) => {
|
|
54
|
-
const schema =
|
|
55
|
+
const schema = getCleanSchema();
|
|
56
|
+
if (modelOptions.validate) {
|
|
57
|
+
await modelOptions.validate(doc);
|
|
58
|
+
}
|
|
55
59
|
return await (0, schema_1.validate)(schema, doc);
|
|
56
60
|
},
|
|
57
61
|
clean: async (doc) => {
|
|
58
|
-
const schema =
|
|
59
|
-
|
|
62
|
+
const schema = getCleanSchema();
|
|
63
|
+
const cleanedDoc = modelOptions.clean ? await modelOptions.clean(doc) : doc;
|
|
64
|
+
return await (0, schema_1.clean)(schema, cleanedDoc);
|
|
60
65
|
},
|
|
61
66
|
clone: (cloneOptions) => {
|
|
62
67
|
return (0, clone_1.default)({
|
|
@@ -29,3 +29,27 @@ it('should call the resolver', async () => {
|
|
|
29
29
|
await (0, helpers_1.sleep)(100);
|
|
30
30
|
expect(await item.res({ p: 1 })).toBe(3);
|
|
31
31
|
});
|
|
32
|
+
it('should call the custom clean function if present', async () => {
|
|
33
|
+
const AModel = (0, index_1.default)({
|
|
34
|
+
name: 'AModel',
|
|
35
|
+
schema: {
|
|
36
|
+
someValue: {
|
|
37
|
+
type: String
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
clean: doc => ({ someValue: 'hello world' })
|
|
41
|
+
});
|
|
42
|
+
const aResolver = (0, resolvers_1.resolver)({
|
|
43
|
+
params: {
|
|
44
|
+
model: {
|
|
45
|
+
type: AModel
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
resolve: ({ model }) => {
|
|
49
|
+
return model;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
const doc = AModel.initItem({ someValue: 'hello' });
|
|
53
|
+
const result = await aResolver.resolve({ model: doc });
|
|
54
|
+
expect(result.someValue).toBe('hello world');
|
|
55
|
+
});
|
package/lib/types/index.d.ts
CHANGED
|
@@ -25,6 +25,17 @@ export interface CreateModelOptions {
|
|
|
25
25
|
resolvers?: ModelResolversMap | (() => {
|
|
26
26
|
default: ModelResolversMap;
|
|
27
27
|
});
|
|
28
|
+
/**
|
|
29
|
+
* Optional function that will process the document before being returned.
|
|
30
|
+
* @param doc The current document
|
|
31
|
+
* @return The processed document promise
|
|
32
|
+
*/
|
|
33
|
+
clean?: (doc: any) => Promise<any> | any;
|
|
34
|
+
/**
|
|
35
|
+
* Optional function that will validate the document before being returned.
|
|
36
|
+
* @param doc The current document
|
|
37
|
+
*/
|
|
38
|
+
validate?: (doc: any) => Promise<void> | void;
|
|
28
39
|
}
|
|
29
40
|
export interface ModelResolversMap {
|
|
30
41
|
[key: string]: Resolver<ModelResolverResolve, true>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/models",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.26",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
"publishConfig": {
|
|
37
37
|
"access": "public"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "1a23db17e115fe86aefce8f496b063ecaa7d37e7"
|
|
40
40
|
}
|