@orion-js/models 3.0.16 → 3.0.26

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2018 Orionjs Team
3
+ Copyright (c) 2021 Orionjs Team
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -11,6 +11,7 @@ const modelToSchema_1 = __importDefault(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,9 +19,18 @@ 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);
22
+ resolvedSchema = (0, modelToSchema_1.default)(schema, model);
22
23
  return resolvedSchema;
23
24
  };
25
+ const getCleanSchema = () => {
26
+ if (!modelOptions.schema)
27
+ return {};
28
+ if (resolvedCleanSchema)
29
+ return resolvedCleanSchema;
30
+ const schema = (0, resolveParam_1.default)(modelOptions.schema);
31
+ resolvedCleanSchema = (0, modelToSchema_1.default)(schema);
32
+ return resolvedCleanSchema;
33
+ };
24
34
  const getResolvers = () => {
25
35
  if (!modelOptions.resolvers)
26
36
  return {};
@@ -38,15 +48,20 @@ const createModel = modelOptions => {
38
48
  __isModel: true,
39
49
  name,
40
50
  getSchema,
51
+ getCleanSchema,
41
52
  getResolvers,
42
53
  initItem: modelInitItem,
43
54
  validate: async (doc) => {
44
- const schema = getSchema();
55
+ const schema = getCleanSchema();
56
+ if (modelOptions.validate) {
57
+ await modelOptions.validate(doc);
58
+ }
45
59
  return await (0, schema_1.validate)(schema, doc);
46
60
  },
47
61
  clean: async (doc) => {
48
- const schema = getSchema();
49
- 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);
50
65
  },
51
66
  clone: (cloneOptions) => {
52
67
  return (0, clone_1.default)({
@@ -1 +1,2 @@
1
- export default function (schema: any): any;
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,13 @@ function default_1(schema) {
18
18
  schema[key].type = schema[key].type.getSchema();
19
19
  }
20
20
  }
21
- return schema;
21
+ if (!model)
22
+ return schema;
23
+ return {
24
+ ...schema,
25
+ __model: model,
26
+ __validate: model.validate,
27
+ __clean: model.clean
28
+ };
22
29
  }
23
30
  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
+ });
@@ -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
+ });
@@ -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
+ });
@@ -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>;
@@ -49,7 +60,13 @@ export interface Model {
49
60
  /**
50
61
  * Returns the schema of the model
51
62
  */
52
- getSchema: () => Schema;
63
+ getSchema: () => Schema & {
64
+ __model: Model;
65
+ };
66
+ /**
67
+ * Returns the schema without adding __model to the schema
68
+ */
69
+ getCleanSchema: () => Schema;
53
70
  /**
54
71
  * Returns the model resolvers
55
72
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/models",
3
- "version": "3.0.16",
3
+ "version": "3.0.26",
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": "yarn run clean && tsc -w"
16
+ "watch": "tsc -w",
17
+ "upgrade-interactive": "yarn upgrade-interactive"
17
18
  },
18
19
  "dependencies": {
19
- "@orion-js/helpers": "^3.0.0",
20
- "@orion-js/resolvers": "^3.0.16",
21
- "@orion-js/schema": "^3.0.7"
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.0-alpha.23",
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": "99af482fd83086d4588da728d7c5d4ee1688aff7"
39
+ "gitHead": "1a23db17e115fe86aefce8f496b063ecaa7d37e7"
39
40
  }