@orion-js/models 3.11.15 → 4.0.0-alpha.3

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/package.json CHANGED
@@ -1,40 +1,45 @@
1
1
  {
2
2
  "name": "@orion-js/models",
3
- "version": "3.11.15",
4
- "main": "lib/index.js",
5
- "types": "lib/index.d.ts",
3
+ "version": "4.0.0-alpha.3",
4
+ "main": "./dist/index.cjs",
5
+ "types": "./dist/index.d.ts",
6
6
  "files": [
7
- "/lib"
7
+ "dist"
8
8
  ],
9
9
  "author": "nicolaslopezj",
10
10
  "license": "MIT",
11
11
  "scripts": {
12
- "test": "jest",
12
+ "test": "bun test",
13
13
  "prepare": "yarn run build",
14
- "clean": "rm -rf ./lib",
15
- "build": "yarn run clean && tsc",
14
+ "clean": "rm -rf ./dist",
15
+ "build": "bun run build.ts",
16
16
  "watch": "tsc -w",
17
- "upgrade-interactive": "yarn upgrade-interactive"
17
+ "upgrade-interactive": "yarn upgrade-interactive",
18
+ "dev": "bun --watch src/index.ts"
18
19
  },
19
20
  "dependencies": {
20
- "@orion-js/helpers": "^3.11.15",
21
- "@orion-js/resolvers": "^3.11.15",
22
- "@orion-js/schema": "^3.11.8"
21
+ "@orion-js/helpers": "^4.0.0-alpha.2",
22
+ "@orion-js/resolvers": "^4.0.0-alpha.2",
23
+ "@orion-js/schema": "^4.0.0-alpha.2"
23
24
  },
24
25
  "peerDependencies": {
25
26
  "@orion-js/cache": "^3.0.0-alpha.10"
26
27
  },
27
28
  "devDependencies": {
28
- "@orion-js/cache": "^3.11.15",
29
- "@shelf/jest-mongodb": "^2.1.0",
30
- "@types/jest": "^27.0.2",
29
+ "@orion-js/cache": "^4.0.0-alpha.2",
31
30
  "@types/lodash": "4.14.176",
32
- "jest": "27.3.1",
33
- "ts-jest": "27.0.7",
34
- "typescript": "^4.4.4"
31
+ "typescript": "^5.4.5",
32
+ "@types/bun": "^1.2.4"
35
33
  },
36
34
  "publishConfig": {
37
35
  "access": "public"
38
36
  },
39
- "gitHead": "a5c95d9fd18f779c834644eb757928cdb840643c"
40
- }
37
+ "gitHead": "9fd28b6f6b348cebc9f0dc207805647969277372",
38
+ "type": "module",
39
+ "module": "./dist/index.js",
40
+ "exports": {
41
+ "types": "./dist/index.d.ts",
42
+ "import": "./dist/index.js",
43
+ "require": "./dist/index.cjs"
44
+ }
45
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
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.
@@ -1,9 +0,0 @@
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;
@@ -1,57 +0,0 @@
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;
@@ -1 +0,0 @@
1
- export {};
@@ -1,49 +0,0 @@
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
- });
@@ -1,2 +0,0 @@
1
- import { Model, CreateModelOptions } from '../types';
2
- export default function createModel<TSchema = any>(modelOptions: CreateModelOptions): Model<TSchema>;
@@ -1,92 +0,0 @@
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;
@@ -1,8 +0,0 @@
1
- import { ModelResolversMap } from '../types';
2
- interface InitItemOptions {
3
- name: string;
4
- schema: any;
5
- resolvers: ModelResolversMap;
6
- }
7
- export default function ({ schema, resolvers, name }: InitItemOptions, item: any): any;
8
- export {};
@@ -1,52 +0,0 @@
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;
@@ -1 +0,0 @@
1
- export {};
@@ -1,28 +0,0 @@
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
- });
@@ -1,9 +0,0 @@
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;
@@ -1,57 +0,0 @@
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;
@@ -1 +0,0 @@
1
- export {};
@@ -1,64 +0,0 @@
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
- });
@@ -1 +0,0 @@
1
- export default function (param: any): any;
@@ -1,11 +0,0 @@
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;
@@ -1 +0,0 @@
1
- export {};
@@ -1,55 +0,0 @@
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
- });
@@ -1 +0,0 @@
1
- export {};