@orion-js/models 3.12.0 → 3.13.1
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 +21 -0
- package/lib/createModel/clone.d.ts +9 -0
- package/lib/createModel/clone.js +57 -0
- package/lib/createModel/clone.test.d.ts +1 -0
- package/lib/createModel/clone.test.js +49 -0
- package/lib/createModel/index.d.ts +2 -0
- package/lib/createModel/index.js +92 -0
- package/lib/createModel/initItem.d.ts +8 -0
- package/lib/createModel/initItem.js +52 -0
- package/lib/createModel/initItem.test.d.ts +1 -0
- package/lib/createModel/initItem.test.js +28 -0
- package/lib/createModel/modelToSchema.d.ts +9 -0
- package/lib/createModel/modelToSchema.js +57 -0
- package/lib/createModel/modelToSchema.test.d.ts +1 -0
- package/lib/createModel/modelToSchema.test.js +64 -0
- package/lib/createModel/resolveParam.d.ts +1 -0
- package/lib/createModel/resolveParam.js +11 -0
- package/lib/createModel/resolvers.test.d.ts +1 -0
- package/lib/createModel/resolvers.test.js +55 -0
- package/lib/createModel/validation.test.d.ts +1 -0
- package/lib/createModel/validation.test.js +72 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +22 -0
- package/lib/types/index.d.ts +99 -0
- package/lib/types/index.js +2 -0
- package/lib/types/index.test.d.ts +1 -0
- package/lib/types/index.test.js +35 -0
- package/package.json +19 -24
- package/dist/index.cjs +0 -5180
- package/dist/index.d.ts +0 -113
- package/dist/index.js +0 -5152
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Orionjs Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { CreateModel, CloneOptions, CreateModelOptions } from '../types';
|
|
2
|
+
interface CloneInfo {
|
|
3
|
+
createModel: CreateModel;
|
|
4
|
+
getSchema: () => any;
|
|
5
|
+
getResolvers: () => any;
|
|
6
|
+
modelOptions: CreateModelOptions;
|
|
7
|
+
}
|
|
8
|
+
declare const clone: (cloneInfo: CloneInfo, options: CloneOptions) => import("../types").Model<any>;
|
|
9
|
+
export default clone;
|
|
@@ -0,0 +1,57 @@
|
|
|
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 includes_1 = __importDefault(require("lodash/includes"));
|
|
7
|
+
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
|
|
8
|
+
const clone = (cloneInfo, options) => {
|
|
9
|
+
const { createModel, getSchema, getResolvers, modelOptions } = cloneInfo;
|
|
10
|
+
return createModel({
|
|
11
|
+
name: options.name,
|
|
12
|
+
clean: modelOptions.clean,
|
|
13
|
+
validate: modelOptions.validate,
|
|
14
|
+
resolvers: () => {
|
|
15
|
+
if (!options.extendResolvers)
|
|
16
|
+
return getResolvers();
|
|
17
|
+
return {
|
|
18
|
+
default: {
|
|
19
|
+
...getResolvers(),
|
|
20
|
+
...options.extendResolvers
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
schema: () => {
|
|
25
|
+
const oldSchema = (0, cloneDeep_1.default)(getSchema());
|
|
26
|
+
const newSchema = {};
|
|
27
|
+
const keys = Object.keys(oldSchema)
|
|
28
|
+
.filter(key => {
|
|
29
|
+
if (!options.omitFields)
|
|
30
|
+
return true;
|
|
31
|
+
return !(0, includes_1.default)(options.omitFields, key);
|
|
32
|
+
})
|
|
33
|
+
.filter(key => {
|
|
34
|
+
if (!options.pickFields)
|
|
35
|
+
return true;
|
|
36
|
+
return (0, includes_1.default)(options.pickFields, key);
|
|
37
|
+
});
|
|
38
|
+
for (const key of keys) {
|
|
39
|
+
const field = oldSchema[key];
|
|
40
|
+
if (options.mapFields) {
|
|
41
|
+
newSchema[key] = options.mapFields(field, key);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
newSchema[key] = field;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (!options.extendSchema)
|
|
48
|
+
return { default: newSchema };
|
|
49
|
+
const clonedSchema = {
|
|
50
|
+
...newSchema,
|
|
51
|
+
...options.extendSchema
|
|
52
|
+
};
|
|
53
|
+
return { default: clonedSchema };
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
exports.default = clone;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
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 index_1 = __importDefault(require("./index"));
|
|
7
|
+
describe('Cloning models', () => {
|
|
8
|
+
it('cloned model should pick fields correctly', async () => {
|
|
9
|
+
const type = {
|
|
10
|
+
type: String
|
|
11
|
+
};
|
|
12
|
+
const model1 = (0, index_1.default)({
|
|
13
|
+
name: 'AModel',
|
|
14
|
+
schema: {
|
|
15
|
+
a: type,
|
|
16
|
+
b: type,
|
|
17
|
+
c: type
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
const model2 = model1.clone({
|
|
21
|
+
name: 'Cloned2',
|
|
22
|
+
pickFields: ['a', 'b']
|
|
23
|
+
});
|
|
24
|
+
const model3 = model2.clone({
|
|
25
|
+
name: 'Cloned3',
|
|
26
|
+
omitFields: ['b']
|
|
27
|
+
});
|
|
28
|
+
const schema = model3.getSchema();
|
|
29
|
+
const keys = Object.keys(schema).filter(key => !key.startsWith('__'));
|
|
30
|
+
expect(keys).toEqual(['a']);
|
|
31
|
+
});
|
|
32
|
+
it('should pass __clean and __validate to the cloned model schema', () => {
|
|
33
|
+
const clean = name => `clean ${name}`;
|
|
34
|
+
const model1 = (0, index_1.default)({
|
|
35
|
+
name: 'Model1',
|
|
36
|
+
schema: {
|
|
37
|
+
name: { type: String }
|
|
38
|
+
},
|
|
39
|
+
clean
|
|
40
|
+
});
|
|
41
|
+
const model2 = model1.clone({
|
|
42
|
+
name: 'Model2'
|
|
43
|
+
});
|
|
44
|
+
const schema1 = model1.getSchema();
|
|
45
|
+
const schema2 = model2.getSchema();
|
|
46
|
+
expect(schema1.__clean).toBe(clean);
|
|
47
|
+
expect(schema2.__clean).toBe(clean);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
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 initItem_1 = __importDefault(require("./initItem"));
|
|
7
|
+
const resolveParam_1 = __importDefault(require("./resolveParam"));
|
|
8
|
+
const schema_1 = require("@orion-js/schema");
|
|
9
|
+
const clone_1 = __importDefault(require("./clone"));
|
|
10
|
+
const modelToSchema_1 = require("./modelToSchema");
|
|
11
|
+
function createModel(modelOptions) {
|
|
12
|
+
const name = modelOptions.name;
|
|
13
|
+
let resolvedSchema = null;
|
|
14
|
+
let resolvedCleanSchema = null;
|
|
15
|
+
let resolvedResolvers = null;
|
|
16
|
+
const getSchema = () => {
|
|
17
|
+
if (!modelOptions.schema)
|
|
18
|
+
return {};
|
|
19
|
+
if (resolvedSchema)
|
|
20
|
+
return resolvedSchema;
|
|
21
|
+
const schema = (0, resolveParam_1.default)(modelOptions.schema);
|
|
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
|
+
}
|
|
29
|
+
return resolvedSchema;
|
|
30
|
+
};
|
|
31
|
+
const getCleanSchema = () => {
|
|
32
|
+
if (!modelOptions.schema)
|
|
33
|
+
return {};
|
|
34
|
+
if (resolvedCleanSchema)
|
|
35
|
+
return resolvedCleanSchema;
|
|
36
|
+
const schema = (0, resolveParam_1.default)(modelOptions.schema);
|
|
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
|
+
}
|
|
44
|
+
return resolvedCleanSchema;
|
|
45
|
+
};
|
|
46
|
+
const getResolvers = () => {
|
|
47
|
+
if (!modelOptions.resolvers)
|
|
48
|
+
return {};
|
|
49
|
+
if (resolvedResolvers)
|
|
50
|
+
return resolvedResolvers;
|
|
51
|
+
resolvedResolvers = (0, resolveParam_1.default)(modelOptions.resolvers);
|
|
52
|
+
return resolvedResolvers;
|
|
53
|
+
};
|
|
54
|
+
const modelInitItem = (item) => {
|
|
55
|
+
const schema = getSchema();
|
|
56
|
+
const resolvers = getResolvers();
|
|
57
|
+
return (0, initItem_1.default)({ schema, resolvers, name }, item);
|
|
58
|
+
};
|
|
59
|
+
const model = {
|
|
60
|
+
__isModel: true,
|
|
61
|
+
name,
|
|
62
|
+
getSchema,
|
|
63
|
+
getCleanSchema,
|
|
64
|
+
getResolvers,
|
|
65
|
+
initItem: modelInitItem,
|
|
66
|
+
validate: async (doc) => {
|
|
67
|
+
const schema = getSchema();
|
|
68
|
+
return await (0, schema_1.validate)(schema, doc);
|
|
69
|
+
},
|
|
70
|
+
clean: async (doc) => {
|
|
71
|
+
const schema = getSchema();
|
|
72
|
+
return await (0, schema_1.clean)(schema, doc);
|
|
73
|
+
},
|
|
74
|
+
cleanAndValidate: async (doc) => {
|
|
75
|
+
const schema = getSchema();
|
|
76
|
+
const cleaned = await (0, schema_1.clean)(schema, doc);
|
|
77
|
+
await (0, schema_1.validate)(schema, cleaned);
|
|
78
|
+
return cleaned;
|
|
79
|
+
},
|
|
80
|
+
clone: (cloneOptions) => {
|
|
81
|
+
return (0, clone_1.default)({
|
|
82
|
+
createModel,
|
|
83
|
+
getSchema,
|
|
84
|
+
getResolvers,
|
|
85
|
+
modelOptions
|
|
86
|
+
}, cloneOptions);
|
|
87
|
+
},
|
|
88
|
+
type: null
|
|
89
|
+
};
|
|
90
|
+
return model;
|
|
91
|
+
}
|
|
92
|
+
exports.default = createModel;
|
|
@@ -0,0 +1,52 @@
|
|
|
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 isArray_1 = __importDefault(require("lodash/isArray"));
|
|
7
|
+
const isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
|
|
8
|
+
const isNil_1 = __importDefault(require("lodash/isNil"));
|
|
9
|
+
function default_1({ schema, resolvers, name }, item) {
|
|
10
|
+
if ((0, isNil_1.default)(item)) {
|
|
11
|
+
return item;
|
|
12
|
+
}
|
|
13
|
+
if (!(0, isPlainObject_1.default)(item)) {
|
|
14
|
+
console.warn(`When initializing an item in ${name} received a non object value`, item);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (resolvers) {
|
|
18
|
+
for (const key of Object.keys(resolvers)) {
|
|
19
|
+
const resolver = resolvers[key];
|
|
20
|
+
item[key] = function (params, viewer) {
|
|
21
|
+
return resolver.execute({
|
|
22
|
+
parent: item,
|
|
23
|
+
params,
|
|
24
|
+
viewer
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (schema) {
|
|
30
|
+
const keys = Object.keys(schema).filter(key => !key.startsWith('__'));
|
|
31
|
+
for (const key of keys) {
|
|
32
|
+
const fieldSchema = schema[key];
|
|
33
|
+
if (!fieldSchema.type)
|
|
34
|
+
continue;
|
|
35
|
+
const fieldModel = (0, isArray_1.default)(fieldSchema.type) ? fieldSchema.type[0] : fieldSchema.type;
|
|
36
|
+
if (!fieldModel.__model)
|
|
37
|
+
continue;
|
|
38
|
+
if (!item[key])
|
|
39
|
+
continue;
|
|
40
|
+
if ((0, isArray_1.default)(fieldSchema.type)) {
|
|
41
|
+
if (!(0, isArray_1.default)(item[key]))
|
|
42
|
+
continue;
|
|
43
|
+
item[key] = item[key].map(item => fieldModel.__model.initItem(item));
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
item[key] = fieldModel.__model.initItem(item[key]);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return item;
|
|
51
|
+
}
|
|
52
|
+
exports.default = default_1;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
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 resolvers_1 = require("@orion-js/resolvers");
|
|
7
|
+
const _1 = __importDefault(require("."));
|
|
8
|
+
describe('Test init item', () => {
|
|
9
|
+
// Tests if the resolvers are passed to the item
|
|
10
|
+
it('Should pass the resolvers to the item', async () => {
|
|
11
|
+
const Person = (0, _1.default)({
|
|
12
|
+
name: 'PersonModel',
|
|
13
|
+
schema: {
|
|
14
|
+
name: { type: String }
|
|
15
|
+
},
|
|
16
|
+
resolvers: {
|
|
17
|
+
title: (0, resolvers_1.resolver)({
|
|
18
|
+
resolve: async (person, { title }) => {
|
|
19
|
+
return `${title} ${person.name}`;
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
const doc = { name: 'Nico' };
|
|
25
|
+
const person = Person.initItem(doc);
|
|
26
|
+
expect(await person.title({ title: 'Mr' })).toBe('Mr Nico');
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Model, ModelSchema } from '..';
|
|
2
|
+
import { Schema } from '@orion-js/schema';
|
|
3
|
+
export declare function modelToSchema(modelSchema: ModelSchema, { cleanSchema }?: {
|
|
4
|
+
cleanSchema?: boolean;
|
|
5
|
+
}): Schema;
|
|
6
|
+
export declare function modelToSchemaWithModel(modelSchema: ModelSchema, model?: Model): Schema | {
|
|
7
|
+
__model: Model<any>;
|
|
8
|
+
};
|
|
9
|
+
export declare function modelToSchemaClean(modelSchema: ModelSchema): Schema;
|
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
exports.modelToSchemaClean = exports.modelToSchemaWithModel = exports.modelToSchema = void 0;
|
|
7
|
+
const isArray_1 = __importDefault(require("lodash/isArray"));
|
|
8
|
+
function isModelSchema(type) {
|
|
9
|
+
return type && typeof type === 'object' && '__isModel' in type;
|
|
10
|
+
}
|
|
11
|
+
function isModelArraySchema(type) {
|
|
12
|
+
return type && (0, isArray_1.default)(type) && typeof type[0] === 'object' && '__isModel' in type[0];
|
|
13
|
+
}
|
|
14
|
+
function modelToSchema(modelSchema, { cleanSchema = true } = {}) {
|
|
15
|
+
const compiledSchema = {};
|
|
16
|
+
for (const key in modelSchema) {
|
|
17
|
+
if (key.startsWith('__'))
|
|
18
|
+
continue;
|
|
19
|
+
const fieldSchema = modelSchema[key];
|
|
20
|
+
let currNode;
|
|
21
|
+
if (isModelSchema(fieldSchema.type)) {
|
|
22
|
+
currNode = {
|
|
23
|
+
...fieldSchema,
|
|
24
|
+
type: cleanSchema ? fieldSchema.type.getCleanSchema() : fieldSchema.type.getSchema()
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
else if (isModelArraySchema(fieldSchema.type)) {
|
|
28
|
+
currNode = {
|
|
29
|
+
...fieldSchema,
|
|
30
|
+
type: cleanSchema
|
|
31
|
+
? [fieldSchema.type[0].getCleanSchema()]
|
|
32
|
+
: [fieldSchema.type[0].getSchema()]
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
currNode = { ...fieldSchema, type: fieldSchema.type };
|
|
37
|
+
}
|
|
38
|
+
compiledSchema[key] = currNode;
|
|
39
|
+
}
|
|
40
|
+
return compiledSchema;
|
|
41
|
+
}
|
|
42
|
+
exports.modelToSchema = modelToSchema;
|
|
43
|
+
function modelToSchemaWithModel(modelSchema, model) {
|
|
44
|
+
const schema = modelToSchema(modelSchema, { cleanSchema: !model });
|
|
45
|
+
if (!model)
|
|
46
|
+
return schema;
|
|
47
|
+
return {
|
|
48
|
+
...schema,
|
|
49
|
+
__model: model
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
exports.modelToSchemaWithModel = modelToSchemaWithModel;
|
|
53
|
+
function modelToSchemaClean(modelSchema) {
|
|
54
|
+
const schema = modelToSchema(modelSchema, { cleanSchema: true });
|
|
55
|
+
return schema;
|
|
56
|
+
}
|
|
57
|
+
exports.modelToSchemaClean = modelToSchemaClean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
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
|
+
const schema_1 = require("@orion-js/schema");
|
|
8
|
+
it('should add the __model field when converting to schema', async () => {
|
|
9
|
+
const model = (0, _1.default)({
|
|
10
|
+
name: 'test',
|
|
11
|
+
schema: {
|
|
12
|
+
name: { type: String },
|
|
13
|
+
age: { type: Number }
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
const schema = model.getSchema();
|
|
17
|
+
expect(schema.__model.name).toEqual('test');
|
|
18
|
+
});
|
|
19
|
+
it('can clean a schema with nested models', async () => {
|
|
20
|
+
const model = (0, _1.default)({
|
|
21
|
+
name: 'AModel',
|
|
22
|
+
schema: {
|
|
23
|
+
name: {
|
|
24
|
+
type: String
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
clean: () => ({ name: 'Model Schema' })
|
|
28
|
+
});
|
|
29
|
+
const finalModel = (0, _1.default)({
|
|
30
|
+
name: 'Test',
|
|
31
|
+
schema: {
|
|
32
|
+
subModel: {
|
|
33
|
+
type: model
|
|
34
|
+
},
|
|
35
|
+
subModelArray: {
|
|
36
|
+
type: [model]
|
|
37
|
+
},
|
|
38
|
+
primitive: {
|
|
39
|
+
type: String,
|
|
40
|
+
clean: () => 'Primitive'
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
const doc = { subModel: { name: 'Joaquin' }, subModelArray: [{ name: 'Roberto' }], primitive: 'hello' };
|
|
45
|
+
const schema = finalModel.getSchema();
|
|
46
|
+
expect(await (0, schema_1.clean)(schema, doc)).toEqual({
|
|
47
|
+
subModel: { name: 'Model Schema' },
|
|
48
|
+
subModelArray: [{ name: 'Model Schema' }],
|
|
49
|
+
primitive: 'Primitive'
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
it('Should return a schema with __clean if clean param is passed to model', async () => {
|
|
53
|
+
const clean = () => ({ name: 'hello' });
|
|
54
|
+
const model = (0, _1.default)({
|
|
55
|
+
name: 'test',
|
|
56
|
+
schema: {
|
|
57
|
+
name: { type: String },
|
|
58
|
+
age: { type: Number }
|
|
59
|
+
},
|
|
60
|
+
clean
|
|
61
|
+
});
|
|
62
|
+
const schema = model.getSchema();
|
|
63
|
+
expect(await schema.__clean()).toEqual(await clean());
|
|
64
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function (param: any): any;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function default_1(param) {
|
|
4
|
+
if (typeof param !== 'function')
|
|
5
|
+
return param;
|
|
6
|
+
const result = param() || {};
|
|
7
|
+
if (result.default)
|
|
8
|
+
return result.default;
|
|
9
|
+
return result || {};
|
|
10
|
+
}
|
|
11
|
+
exports.default = default_1;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
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 index_1 = __importDefault(require("./index"));
|
|
7
|
+
const helpers_1 = require("@orion-js/helpers");
|
|
8
|
+
const resolvers_1 = require("@orion-js/resolvers");
|
|
9
|
+
it('should call the resolver', async () => {
|
|
10
|
+
let index = 0;
|
|
11
|
+
const model = (0, index_1.default)({
|
|
12
|
+
name: 'AModel',
|
|
13
|
+
schema: {},
|
|
14
|
+
resolvers: {
|
|
15
|
+
res: (0, resolvers_1.modelResolver)({
|
|
16
|
+
private: true,
|
|
17
|
+
cache: 100,
|
|
18
|
+
async resolve() {
|
|
19
|
+
index++;
|
|
20
|
+
return index;
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
const item = model.initItem({ index: 0 });
|
|
26
|
+
expect(await item.res({ p: 1 })).toBe(1);
|
|
27
|
+
expect(await item.res({ p: 2 })).toBe(2);
|
|
28
|
+
expect(await item.res({ p: 1 })).toBe(1);
|
|
29
|
+
await (0, helpers_1.sleep)(100);
|
|
30
|
+
expect(await item.res({ p: 1 })).toBe(3);
|
|
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
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
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 index_1 = __importDefault(require("./index"));
|
|
7
|
+
const resolvers_1 = require("@orion-js/resolvers");
|
|
8
|
+
it('should validate a schema', async () => {
|
|
9
|
+
const model = (0, index_1.default)({
|
|
10
|
+
name: 'AModel',
|
|
11
|
+
schema: {
|
|
12
|
+
name: {
|
|
13
|
+
type: String
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
await model.validate({ name: 'String' });
|
|
18
|
+
});
|
|
19
|
+
it('should allow deep model validation', async () => {
|
|
20
|
+
const model2 = (0, index_1.default)({
|
|
21
|
+
name: 'AModel',
|
|
22
|
+
schema: {
|
|
23
|
+
firstName: {
|
|
24
|
+
type: String
|
|
25
|
+
},
|
|
26
|
+
lastName: {
|
|
27
|
+
type: String
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
const model1 = (0, index_1.default)({
|
|
32
|
+
name: 'AModel2',
|
|
33
|
+
schema: {
|
|
34
|
+
data: {
|
|
35
|
+
type: model2
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
await model1.validate({ data: { firstName: 'Nicolás', lastName: 'López' } });
|
|
40
|
+
expect.assertions(1);
|
|
41
|
+
try {
|
|
42
|
+
await model1.validate({});
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
expect(error.code).toBe('validationError');
|
|
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/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.modelToSchemaWithModel = exports.modelToSchema = exports.createModel = void 0;
|
|
17
|
+
const createModel_1 = __importDefault(require("./createModel"));
|
|
18
|
+
exports.createModel = createModel_1.default;
|
|
19
|
+
const modelToSchema_1 = require("./createModel/modelToSchema");
|
|
20
|
+
Object.defineProperty(exports, "modelToSchema", { enumerable: true, get: function () { return modelToSchema_1.modelToSchema; } });
|
|
21
|
+
Object.defineProperty(exports, "modelToSchemaWithModel", { enumerable: true, get: function () { return modelToSchema_1.modelToSchemaWithModel; } });
|
|
22
|
+
__exportStar(require("./types"), exports);
|