@orion-js/models 3.0.24 → 3.0.31

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,10 +7,11 @@ 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;
14
+ let resolvedCleanSchema = null;
14
15
  let resolvedResolvers = null;
15
16
  const getSchema = () => {
16
17
  if (!modelOptions.schema)
@@ -18,17 +19,17 @@ const createModel = modelOptions => {
18
19
  if (resolvedSchema)
19
20
  return resolvedSchema;
20
21
  const schema = (0, resolveParam_1.default)(modelOptions.schema);
21
- resolvedSchema = (0, modelToSchema_1.default)(schema, model);
22
+ resolvedSchema = (0, modelToSchema_1.modelToSchemaWithModel)(schema, model);
22
23
  return resolvedSchema;
23
24
  };
24
25
  const getCleanSchema = () => {
25
26
  if (!modelOptions.schema)
26
27
  return {};
27
- if (resolvedSchema)
28
- return resolvedSchema;
28
+ if (resolvedCleanSchema)
29
+ return resolvedCleanSchema;
29
30
  const schema = (0, resolveParam_1.default)(modelOptions.schema);
30
- resolvedSchema = (0, modelToSchema_1.default)(schema);
31
- return resolvedSchema;
31
+ resolvedCleanSchema = (0, modelToSchema_1.modelToSchema)(schema);
32
+ return resolvedCleanSchema;
32
33
  };
33
34
  const getResolvers = () => {
34
35
  if (!modelOptions.resolvers)
@@ -51,12 +52,16 @@ const createModel = modelOptions => {
51
52
  getResolvers,
52
53
  initItem: modelInitItem,
53
54
  validate: async (doc) => {
54
- const schema = getSchema();
55
+ const schema = getCleanSchema();
56
+ if (modelOptions.validate) {
57
+ await modelOptions.validate(doc);
58
+ }
55
59
  return await (0, schema_1.validate)(schema, doc);
56
60
  },
57
61
  clean: async (doc) => {
58
- const schema = getSchema();
59
- return await (0, schema_1.clean)(schema, doc);
62
+ const schema = getCleanSchema();
63
+ const cleanedDoc = modelOptions.clean ? await modelOptions.clean(doc) : doc;
64
+ return await (0, schema_1.clean)(schema, cleanedDoc);
60
65
  },
61
66
  clone: (cloneOptions) => {
62
67
  return (0, clone_1.default)({
@@ -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,26 +3,49 @@ 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 {
24
46
  ...schema,
25
- __model: model
47
+ __model: model,
48
+ __clean: model.clean
26
49
  };
27
50
  }
28
- 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
+ });
@@ -29,3 +29,27 @@ it('should call the resolver', async () => {
29
29
  await (0, helpers_1.sleep)(100);
30
30
  expect(await item.res({ p: 1 })).toBe(3);
31
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
+ });
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);
@@ -25,6 +25,17 @@ export interface CreateModelOptions {
25
25
  resolvers?: ModelResolversMap | (() => {
26
26
  default: ModelResolversMap;
27
27
  });
28
+ /**
29
+ * Optional function that will process the document before being returned.
30
+ * @param doc The current document
31
+ * @return The processed document promise
32
+ */
33
+ clean?: (doc: any) => Promise<any> | any;
34
+ /**
35
+ * Optional function that will validate the document before being returned.
36
+ * @param doc The current document
37
+ */
38
+ validate?: (doc: any) => Promise<void> | void;
28
39
  }
29
40
  export interface ModelResolversMap {
30
41
  [key: string]: Resolver<ModelResolverResolve, true>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/models",
3
- "version": "3.0.24",
3
+ "version": "3.0.31",
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.31",
21
+ "@orion-js/resolvers": "^3.0.31",
22
+ "@orion-js/schema": "^3.0.31"
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.31",
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": "5ac1c68e72af3dbeaf8eb5c2a04d5325da2a7e3f"
39
+ "gitHead": "8d1f8b92ea815c14ffeaecfa8879ca775bc1d991"
40
40
  }