@orion-js/models 3.0.15 → 3.0.24
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/LICENSE +1 -1
- package/lib/createModel/index.js +10 -0
- package/lib/createModel/modelToSchema.d.ts +2 -1
- package/lib/createModel/modelToSchema.js +7 -2
- package/lib/createModel/modelToSchema.test.d.ts +1 -0
- package/lib/createModel/modelToSchema.test.js +17 -0
- package/lib/createModel/validation.test.js +26 -0
- package/lib/types/index.d.ts +7 -1
- package/package.json +8 -7
package/LICENSE
CHANGED
package/lib/createModel/index.js
CHANGED
|
@@ -13,6 +13,15 @@ const createModel = modelOptions => {
|
|
|
13
13
|
let resolvedSchema = null;
|
|
14
14
|
let resolvedResolvers = null;
|
|
15
15
|
const getSchema = () => {
|
|
16
|
+
if (!modelOptions.schema)
|
|
17
|
+
return {};
|
|
18
|
+
if (resolvedSchema)
|
|
19
|
+
return resolvedSchema;
|
|
20
|
+
const schema = (0, resolveParam_1.default)(modelOptions.schema);
|
|
21
|
+
resolvedSchema = (0, modelToSchema_1.default)(schema, model);
|
|
22
|
+
return resolvedSchema;
|
|
23
|
+
};
|
|
24
|
+
const getCleanSchema = () => {
|
|
16
25
|
if (!modelOptions.schema)
|
|
17
26
|
return {};
|
|
18
27
|
if (resolvedSchema)
|
|
@@ -38,6 +47,7 @@ const createModel = modelOptions => {
|
|
|
38
47
|
__isModel: true,
|
|
39
48
|
name,
|
|
40
49
|
getSchema,
|
|
50
|
+
getCleanSchema,
|
|
41
51
|
getResolvers,
|
|
42
52
|
initItem: modelInitItem,
|
|
43
53
|
validate: async (doc) => {
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { Model } from '..';
|
|
2
|
+
export default function (schema: any, model?: Model): any;
|
|
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const isArray_1 = __importDefault(require("lodash/isArray"));
|
|
7
7
|
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
|
|
8
|
-
function default_1(schema) {
|
|
8
|
+
function default_1(schema, model) {
|
|
9
9
|
schema = (0, cloneDeep_1.default)(schema);
|
|
10
10
|
const keys = Object.keys(schema);
|
|
11
11
|
for (const key of keys) {
|
|
@@ -18,6 +18,11 @@ function default_1(schema) {
|
|
|
18
18
|
schema[key].type = schema[key].type.getSchema();
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
if (!model)
|
|
22
|
+
return schema;
|
|
23
|
+
return {
|
|
24
|
+
...schema,
|
|
25
|
+
__model: model
|
|
26
|
+
};
|
|
22
27
|
}
|
|
23
28
|
exports.default = default_1;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const _1 = __importDefault(require("."));
|
|
7
|
+
it('should add the __model field when converting to schema', async () => {
|
|
8
|
+
const model = (0, _1.default)({
|
|
9
|
+
name: 'test',
|
|
10
|
+
schema: {
|
|
11
|
+
name: { type: String },
|
|
12
|
+
age: { type: Number }
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
const schema = model.getSchema();
|
|
16
|
+
expect(schema.__model.name).toEqual('test');
|
|
17
|
+
});
|
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const index_1 = __importDefault(require("./index"));
|
|
7
|
+
const resolvers_1 = require("@orion-js/resolvers");
|
|
7
8
|
it('should validate a schema', async () => {
|
|
8
9
|
const model = (0, index_1.default)({
|
|
9
10
|
name: 'AModel',
|
|
@@ -44,3 +45,28 @@ it('should allow deep model validation', async () => {
|
|
|
44
45
|
expect(error.code).toBe('validationError');
|
|
45
46
|
}
|
|
46
47
|
});
|
|
48
|
+
it('[regression test]: should allow correct doc cleaning for resolver params', async () => {
|
|
49
|
+
const Point = (0, index_1.default)({
|
|
50
|
+
name: 'Point',
|
|
51
|
+
schema: {
|
|
52
|
+
latitude: {
|
|
53
|
+
type: Number
|
|
54
|
+
},
|
|
55
|
+
longitude: {
|
|
56
|
+
type: Number
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
const resolver1 = (0, resolvers_1.resolver)({
|
|
61
|
+
params: {
|
|
62
|
+
point: {
|
|
63
|
+
type: Point
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
async resolve({ point }) {
|
|
67
|
+
return point;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
const doc = await resolver1.resolve({ point: { latitude: '11', longitude: '12' } });
|
|
71
|
+
expect(doc).toEqual({ latitude: 11, longitude: 12 });
|
|
72
|
+
});
|
package/lib/types/index.d.ts
CHANGED
|
@@ -49,7 +49,13 @@ export interface Model {
|
|
|
49
49
|
/**
|
|
50
50
|
* Returns the schema of the model
|
|
51
51
|
*/
|
|
52
|
-
getSchema: () => Schema
|
|
52
|
+
getSchema: () => Schema & {
|
|
53
|
+
__model: Model;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Returns the schema without adding __model to the schema
|
|
57
|
+
*/
|
|
58
|
+
getCleanSchema: () => Schema;
|
|
53
59
|
/**
|
|
54
60
|
* Returns the model resolvers
|
|
55
61
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/models",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.24",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -13,18 +13,19 @@
|
|
|
13
13
|
"prepare": "yarn run build",
|
|
14
14
|
"clean": "rm -rf ./lib",
|
|
15
15
|
"build": "yarn run clean && tsc",
|
|
16
|
-
"watch": "
|
|
16
|
+
"watch": "tsc -w",
|
|
17
|
+
"upgrade-interactive": "yarn upgrade-interactive"
|
|
17
18
|
},
|
|
18
19
|
"dependencies": {
|
|
19
|
-
"@orion-js/helpers": "^3.0.
|
|
20
|
-
"@orion-js/resolvers": "^3.0.
|
|
21
|
-
"@orion-js/schema": "^3.0.
|
|
20
|
+
"@orion-js/helpers": "^3.0.17",
|
|
21
|
+
"@orion-js/resolvers": "^3.0.24",
|
|
22
|
+
"@orion-js/schema": "^3.0.17"
|
|
22
23
|
},
|
|
23
24
|
"peerDependencies": {
|
|
24
25
|
"@orion-js/cache": "^3.0.0-alpha.10"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
|
-
"@orion-js/cache": "^3.0.
|
|
28
|
+
"@orion-js/cache": "^3.0.17",
|
|
28
29
|
"@shelf/jest-mongodb": "^2.1.0",
|
|
29
30
|
"@types/jest": "^27.0.2",
|
|
30
31
|
"@types/lodash": "4.14.176",
|
|
@@ -35,5 +36,5 @@
|
|
|
35
36
|
"publishConfig": {
|
|
36
37
|
"access": "public"
|
|
37
38
|
},
|
|
38
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "5ac1c68e72af3dbeaf8eb5c2a04d5325da2a7e3f"
|
|
39
40
|
}
|