@orion-js/typed-model 3.0.32 → 3.0.38

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.
@@ -5,6 +5,7 @@ exports.getModelForClass = void 0;
5
5
  const models_1 = require("@orion-js/models");
6
6
  const metadataStorage_1 = require("../storage/metadataStorage");
7
7
  const processSchemaForProp_1 = require("./helpers/processSchemaForProp");
8
+ const modelCache = new Map();
8
9
  function processModelSchemaForProp(prop) {
9
10
  if (prop.type?.__isModel === true) {
10
11
  return prop;
@@ -12,6 +13,10 @@ function processModelSchemaForProp(prop) {
12
13
  return (0, processSchemaForProp_1.processSchemaForProp)(prop);
13
14
  }
14
15
  function getModelForClass(target) {
16
+ const schemaId = target.__schemaId;
17
+ if (modelCache.has(schemaId)) {
18
+ return modelCache.get(schemaId);
19
+ }
15
20
  const schema = {};
16
21
  const resolverMap = {};
17
22
  let parent = target;
@@ -29,10 +34,12 @@ function getModelForClass(target) {
29
34
  });
30
35
  parent = Object.getPrototypeOf(parent);
31
36
  }
32
- return (0, models_1.createModel)({
37
+ const model = (0, models_1.createModel)({
33
38
  name: target.name,
34
39
  schema,
35
40
  resolvers: resolverMap
36
41
  });
42
+ modelCache.set(schemaId, model);
43
+ return model;
37
44
  }
38
45
  exports.getModelForClass = getModelForClass;
package/lib/index.test.js CHANGED
@@ -552,6 +552,19 @@ describe('typed-schema e2e tests', () => {
552
552
  expect((0, index_1.getModelForClass)(Spec).name).toEqual(expected.name);
553
553
  expect((0, index_1.getModelForClass)(Spec).getCleanSchema()).toEqual(expected.getCleanSchema());
554
554
  });
555
+ it('Should return the same object when calling getModelForClass multiple times', () => {
556
+ let Spec = class Spec {
557
+ };
558
+ __decorate([
559
+ (0, index_1.Prop)(),
560
+ __metadata("design:type", String)
561
+ ], Spec.prototype, "name", void 0);
562
+ Spec = __decorate([
563
+ (0, index_1.TypedModel)()
564
+ ], Spec);
565
+ const model = (0, index_1.getModelForClass)(Spec);
566
+ expect((0, index_1.getModelForClass)(Spec)).toBe(model);
567
+ });
555
568
  });
556
569
  describe('using resolvers', () => {
557
570
  it('allows passing resolvers to the model', () => {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const models_1 = require("@orion-js/models");
13
+ const schema_1 = require("@orion-js/schema");
14
+ const _1 = require(".");
15
+ describe('Test typed model with Schema', () => {
16
+ it('Should allow passing a typed model to schema', async () => {
17
+ let Point = class Point {
18
+ };
19
+ __decorate([
20
+ (0, _1.Prop)(),
21
+ __metadata("design:type", Number)
22
+ ], Point.prototype, "latitude", void 0);
23
+ __decorate([
24
+ (0, _1.Prop)(),
25
+ __metadata("design:type", Number)
26
+ ], Point.prototype, "longitude", void 0);
27
+ Point = __decorate([
28
+ (0, _1.TypedModel)()
29
+ ], Point);
30
+ const schema = {
31
+ points: {
32
+ type: [Point]
33
+ }
34
+ };
35
+ const result = await (0, schema_1.clean)(schema, { points: [{ latitude: '1', longitude: 2 }] });
36
+ expect(result).toEqual({ points: [{ latitude: 1, longitude: 2 }] });
37
+ await (0, schema_1.validate)(schema, { points: [{ latitude: 1, longitude: 2 }] });
38
+ });
39
+ it('Should allow passing a typed model to a model', async () => {
40
+ let Point = class Point {
41
+ };
42
+ __decorate([
43
+ (0, _1.Prop)(),
44
+ __metadata("design:type", Number)
45
+ ], Point.prototype, "latitude", void 0);
46
+ __decorate([
47
+ (0, _1.Prop)(),
48
+ __metadata("design:type", Number)
49
+ ], Point.prototype, "longitude", void 0);
50
+ Point = __decorate([
51
+ (0, _1.TypedModel)()
52
+ ], Point);
53
+ const Item = (0, models_1.createModel)({
54
+ name: 'Item',
55
+ schema: {
56
+ points: {
57
+ type: [Point]
58
+ }
59
+ }
60
+ });
61
+ const result = await Item.clean({ points: [{ latitude: '1', longitude: 2 }] });
62
+ expect.assertions(2);
63
+ expect(result).toEqual({ points: [{ latitude: 1, longitude: 2 }] });
64
+ try {
65
+ await Item.validate({ points: [{ latitude: '1', longitude: 2 }] });
66
+ }
67
+ catch (error) {
68
+ expect(error.message).toBe('Validation Error: {points.0.latitude: notANumber}');
69
+ }
70
+ });
71
+ it('Should allow cleaning and validating just the typed model', async () => {
72
+ let Point = class Point {
73
+ };
74
+ __decorate([
75
+ (0, _1.Prop)(),
76
+ __metadata("design:type", Number)
77
+ ], Point.prototype, "latitude", void 0);
78
+ __decorate([
79
+ (0, _1.Prop)(),
80
+ __metadata("design:type", Number)
81
+ ], Point.prototype, "longitude", void 0);
82
+ Point = __decorate([
83
+ (0, _1.TypedModel)()
84
+ ], Point);
85
+ const result = await (0, schema_1.clean)(Point, { latitude: '1', longitude: 2 });
86
+ expect(result).toEqual({ latitude: 1, longitude: 2 });
87
+ try {
88
+ await (0, schema_1.validate)(Point, { latitude: '1', longitude: 2 });
89
+ }
90
+ catch (error) {
91
+ expect(error.message).toBe('Validation Error: {latitude: notANumber}');
92
+ }
93
+ });
94
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/typed-model",
3
- "version": "3.0.32",
3
+ "version": "3.0.38",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "files": [
@@ -17,10 +17,10 @@
17
17
  "test": "jest --config jest.config.js"
18
18
  },
19
19
  "dependencies": {
20
- "@orion-js/helpers": "^3.0.31",
21
- "@orion-js/models": "^3.0.32",
22
- "@orion-js/resolvers": "^3.0.32",
23
- "@orion-js/schema": "^3.0.32",
20
+ "@orion-js/helpers": "^3.0.36",
21
+ "@orion-js/models": "^3.0.38",
22
+ "@orion-js/resolvers": "^3.0.37",
23
+ "@orion-js/schema": "^3.0.37",
24
24
  "lodash": "^4.17.21",
25
25
  "reflect-metadata": "0.1.13"
26
26
  },
@@ -39,5 +39,5 @@
39
39
  "publishConfig": {
40
40
  "access": "public"
41
41
  },
42
- "gitHead": "8d6dfbbd6fbf5aed082e9d67df81d09db580b015"
42
+ "gitHead": "00903f1488b72e13b7d5031a916eb897d0d80e0c"
43
43
  }