@orion-js/models 3.0.27 → 3.0.36

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.
@@ -7,7 +7,7 @@ const initItem_1 = __importDefault(require("./initItem"));
7
7
  const resolveParam_1 = __importDefault(require("./resolveParam"));
8
8
  const schema_1 = require("@orion-js/schema");
9
9
  const clone_1 = __importDefault(require("./clone"));
10
- const modelToSchema_1 = __importDefault(require("./modelToSchema"));
10
+ const modelToSchema_1 = require("./modelToSchema");
11
11
  const createModel = modelOptions => {
12
12
  const name = modelOptions.name;
13
13
  let resolvedSchema = null;
@@ -19,7 +19,7 @@ const createModel = modelOptions => {
19
19
  if (resolvedSchema)
20
20
  return resolvedSchema;
21
21
  const schema = (0, resolveParam_1.default)(modelOptions.schema);
22
- resolvedSchema = (0, modelToSchema_1.default)(schema, model);
22
+ resolvedSchema = (0, modelToSchema_1.modelToSchemaWithModel)(schema, model);
23
23
  return resolvedSchema;
24
24
  };
25
25
  const getCleanSchema = () => {
@@ -28,7 +28,7 @@ const createModel = modelOptions => {
28
28
  if (resolvedCleanSchema)
29
29
  return resolvedCleanSchema;
30
30
  const schema = (0, resolveParam_1.default)(modelOptions.schema);
31
- resolvedCleanSchema = (0, modelToSchema_1.default)(schema);
31
+ resolvedCleanSchema = (0, modelToSchema_1.modelToSchema)(schema);
32
32
  return resolvedCleanSchema;
33
33
  };
34
34
  const getResolvers = () => {
@@ -1,2 +1,9 @@
1
- import { Model } from '..';
2
- export default function (schema: any, model?: Model): any;
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;
8
+ __clean: (item: any) => Promise<any>;
9
+ };
@@ -3,21 +3,43 @@ 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
7
  const isArray_1 = __importDefault(require("lodash/isArray"));
7
- const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
8
- function default_1(schema, model) {
9
- schema = (0, cloneDeep_1.default)(schema);
10
- const keys = Object.keys(schema);
11
- for (const key of keys) {
12
- if ((0, isArray_1.default)(schema[key].type)) {
13
- if (schema[key].type[0].__isModel) {
14
- schema[key].type[0] = schema[key].type[0].getSchema();
15
- }
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
+ const fieldSchema = modelSchema[key];
18
+ let currNode;
19
+ if (isModelSchema(fieldSchema.type)) {
20
+ currNode = {
21
+ ...fieldSchema,
22
+ type: cleanSchema ? fieldSchema.type.getCleanSchema() : fieldSchema.type.getSchema()
23
+ };
16
24
  }
17
- if (schema[key].type && schema[key].type.__isModel) {
18
- schema[key].type = schema[key].type.getSchema();
25
+ else if (isModelArraySchema(fieldSchema.type)) {
26
+ currNode = {
27
+ ...fieldSchema,
28
+ type: cleanSchema
29
+ ? [fieldSchema.type[0].getCleanSchema()]
30
+ : [fieldSchema.type[0].getSchema()]
31
+ };
19
32
  }
33
+ else {
34
+ currNode = { ...fieldSchema, type: fieldSchema.type };
35
+ }
36
+ compiledSchema[key] = currNode;
20
37
  }
38
+ return compiledSchema;
39
+ }
40
+ exports.modelToSchema = modelToSchema;
41
+ function modelToSchemaWithModel(modelSchema, model) {
42
+ const schema = modelToSchema(modelSchema, { cleanSchema: !model });
21
43
  if (!model)
22
44
  return schema;
23
45
  return {
@@ -26,4 +48,4 @@ function default_1(schema, model) {
26
48
  __clean: model.clean
27
49
  };
28
50
  }
29
- exports.default = default_1;
51
+ exports.modelToSchemaWithModel = modelToSchemaWithModel;
@@ -3,7 +3,9 @@ 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
+ const __1 = require("..");
6
7
  const _1 = __importDefault(require("."));
8
+ const schema_1 = require("@orion-js/schema");
7
9
  it('should add the __model field when converting to schema', async () => {
8
10
  const model = (0, _1.default)({
9
11
  name: 'test',
@@ -15,3 +17,33 @@ it('should add the __model field when converting to schema', async () => {
15
17
  const schema = model.getSchema();
16
18
  expect(schema.__model.name).toEqual('test');
17
19
  });
20
+ it('can clean a schema with nested models', async () => {
21
+ const modelSchema = {
22
+ name: {
23
+ type: String,
24
+ clean: () => 'Model Schema'
25
+ }
26
+ };
27
+ const model = (0, _1.default)({
28
+ name: 'AModel',
29
+ schema: modelSchema
30
+ });
31
+ const schema = {
32
+ subModel: {
33
+ type: model
34
+ },
35
+ subModelArray: {
36
+ type: [model]
37
+ },
38
+ primitive: {
39
+ type: String,
40
+ clean: () => 'Primitive'
41
+ }
42
+ };
43
+ const doc = { subModel: { name: 'Joaquin' }, subModelArray: [{ name: 'Roberto' }], primitive: 'hello' };
44
+ expect(await (0, schema_1.clean)((0, __1.modelToSchema)(schema), doc)).toEqual({
45
+ subModel: { name: 'Model Schema' },
46
+ subModelArray: [{ name: 'Model Schema' }],
47
+ primitive: 'Primitive'
48
+ });
49
+ });
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import createModel from './createModel';
2
- import modelToSchema from './createModel/modelToSchema';
3
- export { createModel, modelToSchema };
2
+ import { modelToSchema, modelToSchemaWithModel } from './createModel/modelToSchema';
3
+ export { createModel, modelToSchema, modelToSchemaWithModel };
4
4
  export * from './types';
package/lib/index.js CHANGED
@@ -13,9 +13,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  return (mod && mod.__esModule) ? mod : { "default": mod };
14
14
  };
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.modelToSchema = exports.createModel = void 0;
16
+ exports.modelToSchemaWithModel = exports.modelToSchema = exports.createModel = void 0;
17
17
  const createModel_1 = __importDefault(require("./createModel"));
18
18
  exports.createModel = createModel_1.default;
19
- const modelToSchema_1 = __importDefault(require("./createModel/modelToSchema"));
20
- exports.modelToSchema = modelToSchema_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; } });
21
22
  __exportStar(require("./types"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/models",
3
- "version": "3.0.27",
3
+ "version": "3.0.36",
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.17",
21
- "@orion-js/resolvers": "^3.0.24",
22
- "@orion-js/schema": "^3.0.17"
20
+ "@orion-js/helpers": "^3.0.36",
21
+ "@orion-js/resolvers": "^3.0.36",
22
+ "@orion-js/schema": "^3.0.36"
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.17",
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": "084f56bda6733955cdc640b3c8adf1731ec95af4"
39
+ "gitHead": "7c921dbbf0ec679df743b9278abbf15cfcbac6ce"
40
40
  }