@orion-js/models 3.0.28 → 3.0.37
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
|
@@ -20,6 +20,12 @@ const createModel = modelOptions => {
|
|
|
20
20
|
return resolvedSchema;
|
|
21
21
|
const schema = (0, resolveParam_1.default)(modelOptions.schema);
|
|
22
22
|
resolvedSchema = (0, modelToSchema_1.modelToSchemaWithModel)(schema, model);
|
|
23
|
+
if (modelOptions.clean) {
|
|
24
|
+
resolvedSchema.__clean = modelOptions.clean;
|
|
25
|
+
}
|
|
26
|
+
if (modelOptions.validate) {
|
|
27
|
+
resolvedSchema.__clean = modelOptions.validate;
|
|
28
|
+
}
|
|
23
29
|
return resolvedSchema;
|
|
24
30
|
};
|
|
25
31
|
const getCleanSchema = () => {
|
|
@@ -28,7 +34,13 @@ const createModel = modelOptions => {
|
|
|
28
34
|
if (resolvedCleanSchema)
|
|
29
35
|
return resolvedCleanSchema;
|
|
30
36
|
const schema = (0, resolveParam_1.default)(modelOptions.schema);
|
|
31
|
-
resolvedCleanSchema = (0, modelToSchema_1.
|
|
37
|
+
resolvedCleanSchema = (0, modelToSchema_1.modelToSchemaClean)(schema);
|
|
38
|
+
if (modelOptions.clean) {
|
|
39
|
+
resolvedCleanSchema.__clean = modelOptions.clean;
|
|
40
|
+
}
|
|
41
|
+
if (modelOptions.validate) {
|
|
42
|
+
resolvedCleanSchema.__clean = modelOptions.validate;
|
|
43
|
+
}
|
|
32
44
|
return resolvedCleanSchema;
|
|
33
45
|
};
|
|
34
46
|
const getResolvers = () => {
|
|
@@ -52,16 +64,12 @@ const createModel = modelOptions => {
|
|
|
52
64
|
getResolvers,
|
|
53
65
|
initItem: modelInitItem,
|
|
54
66
|
validate: async (doc) => {
|
|
55
|
-
const schema =
|
|
56
|
-
if (modelOptions.validate) {
|
|
57
|
-
await modelOptions.validate(doc);
|
|
58
|
-
}
|
|
67
|
+
const schema = getSchema();
|
|
59
68
|
return await (0, schema_1.validate)(schema, doc);
|
|
60
69
|
},
|
|
61
70
|
clean: async (doc) => {
|
|
62
|
-
const schema =
|
|
63
|
-
|
|
64
|
-
return await (0, schema_1.clean)(schema, cleanedDoc);
|
|
71
|
+
const schema = getSchema();
|
|
72
|
+
return await (0, schema_1.clean)(schema, doc);
|
|
65
73
|
},
|
|
66
74
|
clone: (cloneOptions) => {
|
|
67
75
|
return (0, clone_1.default)({
|
|
@@ -5,5 +5,5 @@ export declare function modelToSchema(modelSchema: ModelSchema, { cleanSchema }?
|
|
|
5
5
|
}): Schema;
|
|
6
6
|
export declare function modelToSchemaWithModel(modelSchema: ModelSchema, model?: Model): Schema | {
|
|
7
7
|
__model: Model;
|
|
8
|
-
__clean: (item: any) => Promise<any>;
|
|
9
8
|
};
|
|
9
|
+
export declare function modelToSchemaClean(modelSchema: ModelSchema): Schema;
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.modelToSchemaWithModel = exports.modelToSchema = void 0;
|
|
6
|
+
exports.modelToSchemaClean = exports.modelToSchemaWithModel = exports.modelToSchema = void 0;
|
|
7
7
|
const isArray_1 = __importDefault(require("lodash/isArray"));
|
|
8
8
|
function isModelSchema(type) {
|
|
9
9
|
return type && typeof type === 'object' && '__isModel' in type;
|
|
@@ -14,6 +14,8 @@ function isModelArraySchema(type) {
|
|
|
14
14
|
function modelToSchema(modelSchema, { cleanSchema = true } = {}) {
|
|
15
15
|
const compiledSchema = {};
|
|
16
16
|
for (const key in modelSchema) {
|
|
17
|
+
if (key.startsWith('__'))
|
|
18
|
+
continue;
|
|
17
19
|
const fieldSchema = modelSchema[key];
|
|
18
20
|
let currNode;
|
|
19
21
|
if (isModelSchema(fieldSchema.type)) {
|
|
@@ -44,8 +46,12 @@ function modelToSchemaWithModel(modelSchema, model) {
|
|
|
44
46
|
return schema;
|
|
45
47
|
return {
|
|
46
48
|
...schema,
|
|
47
|
-
__model: model
|
|
48
|
-
__clean: model.clean
|
|
49
|
+
__model: model
|
|
49
50
|
};
|
|
50
51
|
}
|
|
51
52
|
exports.modelToSchemaWithModel = modelToSchemaWithModel;
|
|
53
|
+
function modelToSchemaClean(modelSchema) {
|
|
54
|
+
const schema = modelToSchema(modelSchema, { cleanSchema: true });
|
|
55
|
+
return schema;
|
|
56
|
+
}
|
|
57
|
+
exports.modelToSchemaClean = modelToSchemaClean;
|
|
@@ -47,3 +47,16 @@ it('can clean a schema with nested models', async () => {
|
|
|
47
47
|
primitive: 'Primitive'
|
|
48
48
|
});
|
|
49
49
|
});
|
|
50
|
+
it('Should return a schema with __clean if clean param is passed to model', async () => {
|
|
51
|
+
const clean = () => ({ name: 'hello' });
|
|
52
|
+
const model = (0, _1.default)({
|
|
53
|
+
name: 'test',
|
|
54
|
+
schema: {
|
|
55
|
+
name: { type: String },
|
|
56
|
+
age: { type: Number }
|
|
57
|
+
},
|
|
58
|
+
clean
|
|
59
|
+
});
|
|
60
|
+
const schema = model.getSchema();
|
|
61
|
+
expect(await schema.__clean()).toEqual(await clean());
|
|
62
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/models",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.37",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -17,15 +17,15 @@
|
|
|
17
17
|
"upgrade-interactive": "yarn upgrade-interactive"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@orion-js/helpers": "^3.0.
|
|
21
|
-
"@orion-js/resolvers": "^3.0.
|
|
22
|
-
"@orion-js/schema": "^3.0.
|
|
20
|
+
"@orion-js/helpers": "^3.0.36",
|
|
21
|
+
"@orion-js/resolvers": "^3.0.37",
|
|
22
|
+
"@orion-js/schema": "^3.0.37"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
25
|
"@orion-js/cache": "^3.0.0-alpha.10"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@orion-js/cache": "^3.0.
|
|
28
|
+
"@orion-js/cache": "^3.0.36",
|
|
29
29
|
"@shelf/jest-mongodb": "^2.1.0",
|
|
30
30
|
"@types/jest": "^27.0.2",
|
|
31
31
|
"@types/lodash": "4.14.176",
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
"publishConfig": {
|
|
37
37
|
"access": "public"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "741691d7603f3d42e6e3e8fa9dc0f9de51a29424"
|
|
40
40
|
}
|