@orion-js/typed-model 3.11.8 → 3.12.0

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.
Files changed (55) hide show
  1. package/dist/index.cjs +6776 -0
  2. package/dist/index.d.ts +48 -0
  3. package/dist/index.js +6748 -0
  4. package/package.json +24 -22
  5. package/LICENSE +0 -21
  6. package/lib/decorators/index.d.ts +0 -4
  7. package/lib/decorators/index.js +0 -16
  8. package/lib/decorators/prop.d.ts +0 -8
  9. package/lib/decorators/prop.js +0 -30
  10. package/lib/decorators/prop.test.d.ts +0 -1
  11. package/lib/decorators/prop.test.js +0 -26
  12. package/lib/decorators/resolver.d.ts +0 -5
  13. package/lib/decorators/resolver.js +0 -14
  14. package/lib/decorators/typedModel.d.ts +0 -5
  15. package/lib/decorators/typedModel.js +0 -16
  16. package/lib/decorators/typedSchema.d.ts +0 -2
  17. package/lib/decorators/typedSchema.js +0 -13
  18. package/lib/enum.test.d.ts +0 -1
  19. package/lib/enum.test.js +0 -29
  20. package/lib/errors/CannotDetermineType.d.ts +0 -3
  21. package/lib/errors/CannotDetermineType.js +0 -9
  22. package/lib/errors/CannotUseArray.d.ts +0 -3
  23. package/lib/errors/CannotUseArray.js +0 -9
  24. package/lib/errors/PropertyAlreadyExists.d.ts +0 -3
  25. package/lib/errors/PropertyAlreadyExists.js +0 -9
  26. package/lib/errors/SchemaAlreadyExists.d.ts +0 -3
  27. package/lib/errors/SchemaAlreadyExists.js +0 -9
  28. package/lib/errors/index.d.ts +0 -4
  29. package/lib/errors/index.js +0 -16
  30. package/lib/factories/cloneSchemaClass.d.ts +0 -23
  31. package/lib/factories/cloneSchemaClass.js +0 -28
  32. package/lib/factories/cloneSchemaClass.test.d.ts +0 -1
  33. package/lib/factories/cloneSchemaClass.test.js +0 -38
  34. package/lib/factories/getModelForClass.d.ts +0 -3
  35. package/lib/factories/getModelForClass.js +0 -62
  36. package/lib/factories/getModelForClass.test.d.ts +0 -1
  37. package/lib/factories/getModelForClass.test.js +0 -59
  38. package/lib/factories/getSchemaForClass.d.ts +0 -3
  39. package/lib/factories/getSchemaForClass.js +0 -8
  40. package/lib/factories/helpers/processSchemaForProp.d.ts +0 -4
  41. package/lib/factories/helpers/processSchemaForProp.js +0 -65
  42. package/lib/factories/index.d.ts +0 -3
  43. package/lib/factories/index.js +0 -15
  44. package/lib/index.d.ts +0 -2
  45. package/lib/index.js +0 -14
  46. package/lib/index.test.d.ts +0 -1
  47. package/lib/index.test.js +0 -682
  48. package/lib/schema.test.d.ts +0 -1
  49. package/lib/schema.test.js +0 -94
  50. package/lib/storage/metadataStorage.d.ts +0 -28
  51. package/lib/storage/metadataStorage.js +0 -55
  52. package/lib/utils/interfaces.d.ts +0 -3
  53. package/lib/utils/interfaces.js +0 -2
  54. package/lib/utils/isClass.d.ts +0 -1
  55. package/lib/utils/isClass.js +0 -6
package/package.json CHANGED
@@ -1,45 +1,47 @@
1
1
  {
2
2
  "name": "@orion-js/typed-model",
3
- "version": "3.11.8",
4
- "main": "lib/index.js",
5
- "types": "lib/index.d.ts",
3
+ "version": "3.12.0",
4
+ "type": "module",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "require": "./dist/index.cjs"
12
+ },
6
13
  "files": [
7
- "/lib"
14
+ "dist"
8
15
  ],
9
16
  "author": "dmerrill6",
10
17
  "license": "MIT",
11
18
  "scripts": {
12
- "prepare": "yarn run build",
13
- "clean": "rm -rf ./lib",
14
- "build": "yarn run clean && tsc",
15
- "watch": "tsc -w",
16
- "upgrade-interactive": "yarn upgrade-interactive",
17
- "test": "jest --config jest.config.js"
19
+ "test": "bun test",
20
+ "build": "bun run build.ts",
21
+ "dev": "bun --watch src/index.ts",
22
+ "clean": "rm -rf ./dist"
18
23
  },
19
24
  "dependencies": {
20
- "@orion-js/helpers": "^3.11.8",
21
- "@orion-js/models": "^3.11.8",
22
- "@orion-js/resolvers": "^3.11.8",
23
- "@orion-js/schema": "^3.11.8",
24
- "@orion-js/services": "^3.11.8",
25
+ "@orion-js/helpers": "3.12.0",
26
+ "@orion-js/models": "3.12.0",
27
+ "@orion-js/resolvers": "3.12.0",
28
+ "@orion-js/schema": "3.12.0",
29
+ "@orion-js/services": "3.12.0",
25
30
  "lodash": "^4.17.21",
26
31
  "reflect-metadata": "0.1.13"
27
32
  },
28
33
  "devDependencies": {
34
+ "@types/bun": "^1.2.4",
29
35
  "@types/eslint": "^7.29.0",
30
- "@types/jest": "27.0.2",
31
- "@types/node": "16.11.7",
32
36
  "@typescript-eslint/eslint-plugin": "5.3.1",
33
37
  "@typescript-eslint/parser": "5.3.1",
34
38
  "eslint": "8.2.0",
35
39
  "eslint-plugin-import": "2.25.3",
36
- "jest": "27.3.1",
37
40
  "kafkajs": "^1.16.0",
38
- "ts-jest": "27.0.7",
39
- "typescript": "^4.4.4"
41
+ "typescript": "^5.4.5"
40
42
  },
41
43
  "publishConfig": {
42
44
  "access": "public"
43
45
  },
44
- "gitHead": "78d70ac414964ec657cc000cedfad8a5efff5cc3"
45
- }
46
+ "gitHead": "9fd28b6f6b348cebc9f0dc207805647969277372"
47
+ }
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,4 +0,0 @@
1
- export * from './typedModel';
2
- export * from './typedSchema';
3
- export * from './prop';
4
- export * from './resolver';
@@ -1,16 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
- };
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./typedModel"), exports);
14
- __exportStar(require("./typedSchema"), exports);
15
- __exportStar(require("./prop"), exports);
16
- __exportStar(require("./resolver"), exports);
@@ -1,8 +0,0 @@
1
- import { Constructor, SchemaMetaFieldType, SchemaNode } from '@orion-js/schema';
2
- import 'reflect-metadata';
3
- import { Model } from '@orion-js/models';
4
- export interface SchemaNodeForClasses extends Omit<SchemaNode, 'type'> {
5
- type: SchemaMetaFieldType | Constructor<any> | Model | Model[];
6
- }
7
- export declare type PropOptions = Partial<SchemaNodeForClasses>;
8
- export declare function Prop(options?: PropOptions): PropertyDecorator;
@@ -1,30 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Prop = void 0;
4
- const metadataStorage_1 = require("../storage/metadataStorage");
5
- require("reflect-metadata");
6
- const errors_1 = require("../errors");
7
- const isClass_1 = require("../utils/isClass");
8
- function Prop(options = {}) {
9
- return (classDef, propertyKey) => {
10
- const schemaName = classDef.constructor?.name;
11
- if (!options.type) {
12
- const type = Reflect.getMetadata('design:type', classDef, propertyKey);
13
- if ((0, isClass_1.isClass)(type) || type === Object) {
14
- throw new errors_1.CannotDetermineTypeError(schemaName, propertyKey);
15
- }
16
- if (type === Array) {
17
- throw new errors_1.CannotUseArrayError(schemaName, propertyKey);
18
- }
19
- if (type) {
20
- options.type = type;
21
- }
22
- else {
23
- throw new errors_1.CannotDetermineTypeError(schemaName, propertyKey);
24
- }
25
- }
26
- metadataStorage_1.MetadataStorage.addPropMetadata({ target: classDef.constructor, propertyKey, options });
27
- classDef[propertyKey] = options;
28
- };
29
- }
30
- exports.Prop = Prop;
@@ -1 +0,0 @@
1
- export {};
@@ -1,26 +0,0 @@
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 __1 = require("..");
13
- describe('Test prop type generator', () => {
14
- it('Should detect strings automatically', async () => {
15
- let Schema = class Schema {
16
- };
17
- __decorate([
18
- (0, __1.Prop)(),
19
- __metadata("design:type", String)
20
- ], Schema.prototype, "key", void 0);
21
- Schema = __decorate([
22
- (0, __1.TypedSchema)()
23
- ], Schema);
24
- expect((0, __1.getModelForClass)(Schema).getCleanSchema().key.type).toBe(String);
25
- });
26
- });
@@ -1,5 +0,0 @@
1
- import { ModelResolver, ModelResolverResolve } from '@orion-js/resolvers';
2
- /**
3
- * @deprecated Please use a @TypedSchema and a @Model a @ModelResolver instead
4
- */
5
- export declare function ResolverProp(options: ModelResolver<ModelResolverResolve>): PropertyDecorator;
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ResolverProp = void 0;
4
- const metadataStorage_1 = require("../storage/metadataStorage");
5
- /**
6
- * @deprecated Please use a @TypedSchema and a @Model a @ModelResolver instead
7
- */
8
- function ResolverProp(options) {
9
- return (classDef, propertyKey) => {
10
- metadataStorage_1.MetadataStorage.addResolverMetadata({ target: classDef.constructor, propertyKey, options });
11
- classDef[propertyKey] = options;
12
- };
13
- }
14
- exports.ResolverProp = ResolverProp;
@@ -1,5 +0,0 @@
1
- import { TypedModelOptions } from '../storage/metadataStorage';
2
- /**
3
- * @deprecated Please use @TypedSchema instead
4
- */
5
- export declare function TypedModel(options?: TypedModelOptions): ClassDecorator;
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TypedModel = void 0;
4
- const __1 = require("..");
5
- const metadataStorage_1 = require("../storage/metadataStorage");
6
- /**
7
- * @deprecated Please use @TypedSchema instead
8
- */
9
- function TypedModel(options = {}) {
10
- return target => {
11
- metadataStorage_1.MetadataStorage.addSchemaMetadata({ target, options });
12
- // @ts-expect-error this is a trick to make it work in resolvers without having to call getModelForClass
13
- target.getModel = () => (0, __1.getModelForClass)(target);
14
- };
15
- }
16
- exports.TypedModel = TypedModel;
@@ -1,2 +0,0 @@
1
- import { TypedModelOptions } from '../storage/metadataStorage';
2
- export declare function TypedSchema(options?: TypedModelOptions): ClassDecorator;
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TypedSchema = void 0;
4
- const __1 = require("..");
5
- const metadataStorage_1 = require("../storage/metadataStorage");
6
- function TypedSchema(options = {}) {
7
- return target => {
8
- metadataStorage_1.MetadataStorage.addSchemaMetadata({ target, options });
9
- // @ts-expect-error this is a trick to make it work in resolvers without having to call getModelForClass
10
- target.getModel = () => (0, __1.getModelForClass)(target);
11
- };
12
- }
13
- exports.TypedSchema = TypedSchema;
@@ -1 +0,0 @@
1
- export {};
package/lib/enum.test.js DELETED
@@ -1,29 +0,0 @@
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 schema_1 = require("@orion-js/schema");
13
- const _1 = require(".");
14
- describe('Test custom fields', () => {
15
- it('Should convert custom fields correctly', async () => {
16
- const colorsEnum = (0, schema_1.createEnum)('ColorsEnum', ['red', 'blue', 'green']);
17
- let TestParams = class TestParams {
18
- };
19
- __decorate([
20
- (0, _1.Prop)({ type: colorsEnum }),
21
- __metadata("design:type", String)
22
- ], TestParams.prototype, "color", void 0);
23
- TestParams = __decorate([
24
- (0, _1.TypedSchema)()
25
- ], TestParams);
26
- const model = (0, _1.getModelForClass)(TestParams);
27
- expect(model.getCleanSchema().color.type._isFieldType).toBe(true);
28
- });
29
- });
@@ -1,3 +0,0 @@
1
- export declare class CannotDetermineTypeError extends Error {
2
- constructor(schemaName: string, propertyKey: string);
3
- }
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CannotDetermineTypeError = void 0;
4
- class CannotDetermineTypeError extends Error {
5
- constructor(schemaName, propertyKey) {
6
- super(`Cannot determine type at "${schemaName}.${propertyKey}" field (object/union/ambiguous type was used). Make sure your property decorator defines a "type" option. For example: "@Prop({ type: {name: String, age: Number} })"`);
7
- }
8
- }
9
- exports.CannotDetermineTypeError = CannotDetermineTypeError;
@@ -1,3 +0,0 @@
1
- export declare class CannotUseArrayError extends Error {
2
- constructor(schemaName: string, propertyKey: string);
3
- }
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CannotUseArrayError = void 0;
4
- class CannotUseArrayError extends Error {
5
- constructor(schemaName, propertyKey) {
6
- super(`Cannot infer type from an Array TypeScript type at "${schemaName}.${propertyKey}" field. Make sure your property decorator defines a "type" option. For example: "@Prop({ type: [String | Number | ...] })"`);
7
- }
8
- }
9
- exports.CannotUseArrayError = CannotUseArrayError;
@@ -1,3 +0,0 @@
1
- export declare class PropertyAlreadyExistsError extends Error {
2
- constructor(schemaName: string, propertyName: string);
3
- }
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PropertyAlreadyExistsError = void 0;
4
- class PropertyAlreadyExistsError extends Error {
5
- constructor(schemaName, propertyName) {
6
- super(`Schema with name "${schemaName}" already contains property "${propertyName}".`);
7
- }
8
- }
9
- exports.PropertyAlreadyExistsError = PropertyAlreadyExistsError;
@@ -1,3 +0,0 @@
1
- export declare class SchemaAlreadyExistsError extends Error {
2
- constructor(schemaName: string);
3
- }
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SchemaAlreadyExistsError = void 0;
4
- class SchemaAlreadyExistsError extends Error {
5
- constructor(schemaName) {
6
- super(`Schema with name "${schemaName}" has already been registered.`);
7
- }
8
- }
9
- exports.SchemaAlreadyExistsError = SchemaAlreadyExistsError;
@@ -1,4 +0,0 @@
1
- export * from './CannotDetermineType';
2
- export * from './CannotUseArray';
3
- export * from './SchemaAlreadyExists';
4
- export * from './PropertyAlreadyExists';
@@ -1,16 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
- };
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./CannotDetermineType"), exports);
14
- __exportStar(require("./CannotUseArray"), exports);
15
- __exportStar(require("./SchemaAlreadyExists"), exports);
16
- __exportStar(require("./PropertyAlreadyExists"), exports);
@@ -1,23 +0,0 @@
1
- import { Model } from '@orion-js/models';
2
- import { CloneOptions } from '@orion-js/models';
3
- import { Constructor } from '../utils/interfaces';
4
- export interface CloneSchemaClassOptions<TClass, TFields extends keyof TClass> {
5
- name: string;
6
- pickFields: readonly TFields[];
7
- mapFields?: CloneOptions['mapFields'];
8
- extendSchema?: CloneOptions['extendSchema'];
9
- }
10
- /**
11
- * This function returns a cloned model but the type is a subset of the original Schema.
12
- * To use the type of the cloned schema use `typeof ClonedModel.type`
13
- *
14
- * Example:
15
- * ```ts
16
- * const ClonedModel = cloneSchemaClass(Schema, {
17
- * name: 'ClonedSchema',
18
- * pickFields: ['name'] as const
19
- * })
20
- * type ClonedType = typeof ClonedModel.type
21
- * ```
22
- */
23
- export declare function cloneSchemaClass<TClass, TFields extends keyof TClass>(schema: Constructor<TClass>, options: CloneSchemaClassOptions<TClass, TFields>): Model<Pick<TClass, TFields>>;
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.cloneSchemaClass = void 0;
4
- const getModelForClass_1 = require("./getModelForClass");
5
- /**
6
- * This function returns a cloned model but the type is a subset of the original Schema.
7
- * To use the type of the cloned schema use `typeof ClonedModel.type`
8
- *
9
- * Example:
10
- * ```ts
11
- * const ClonedModel = cloneSchemaClass(Schema, {
12
- * name: 'ClonedSchema',
13
- * pickFields: ['name'] as const
14
- * })
15
- * type ClonedType = typeof ClonedModel.type
16
- * ```
17
- */
18
- function cloneSchemaClass(schema, options) {
19
- const model = (0, getModelForClass_1.getModelForClass)(schema);
20
- const newModel = model.clone({
21
- name: options.name,
22
- pickFields: options.pickFields,
23
- mapFields: options.mapFields,
24
- extendSchema: options.extendSchema
25
- });
26
- return newModel;
27
- }
28
- exports.cloneSchemaClass = cloneSchemaClass;
@@ -1 +0,0 @@
1
- export {};
@@ -1,38 +0,0 @@
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 __1 = require("..");
13
- const cloneSchemaClass_1 = require("./cloneSchemaClass");
14
- describe('cloneSchemaClass', () => {
15
- it('should clone a schema class', async () => {
16
- let SchemaName = class SchemaName {
17
- };
18
- __decorate([
19
- (0, __1.Prop)(),
20
- __metadata("design:type", String)
21
- ], SchemaName.prototype, "_id", void 0);
22
- __decorate([
23
- (0, __1.Prop)(),
24
- __metadata("design:type", String)
25
- ], SchemaName.prototype, "name", void 0);
26
- SchemaName = __decorate([
27
- (0, __1.TypedSchema)()
28
- ], SchemaName);
29
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
30
- const test2 = 'name';
31
- const ClonedSchema = (0, cloneSchemaClass_1.cloneSchemaClass)(SchemaName, {
32
- name: 'Test',
33
- pickFields: ['name']
34
- });
35
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
36
- const test = 'name';
37
- });
38
- });
@@ -1,3 +0,0 @@
1
- import { Model } from '@orion-js/models';
2
- import { Constructor } from '../utils/interfaces';
3
- export declare function getModelForClass<TClass>(target: Constructor<TClass>): Model;
@@ -1,62 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getModelForClass = void 0;
4
- /* eslint-disable @typescript-eslint/ban-types */
5
- const models_1 = require("@orion-js/models");
6
- const metadataStorage_1 = require("../storage/metadataStorage");
7
- const processSchemaForProp_1 = require("./helpers/processSchemaForProp");
8
- const modelCache = new Map();
9
- function processModelSchemaForProp(prop) {
10
- if (prop.type?.__isModel === true) {
11
- return prop;
12
- }
13
- if (prop.type?._isFieldType === true) {
14
- return prop;
15
- }
16
- return (0, processSchemaForProp_1.processSchemaForProp)(prop);
17
- }
18
- function getModelForClass(target) {
19
- const targetAsModel = target;
20
- if (targetAsModel.__isModel) {
21
- return targetAsModel;
22
- }
23
- let modelResolvers = null;
24
- if (target.prototype.typedModel) {
25
- modelResolvers = target.prototype.resolvers || {};
26
- target = target.prototype.typedModel;
27
- }
28
- const schemaId = target.__schemaId;
29
- if (modelCache.has(schemaId)) {
30
- return modelCache.get(schemaId);
31
- }
32
- const schema = {};
33
- const resolverMap = {};
34
- let parent = target;
35
- while (parent.prototype) {
36
- if (parent === Function.prototype) {
37
- break;
38
- }
39
- const props = metadataStorage_1.MetadataStorage.getSchemaProps(parent) ?? {};
40
- Object.keys(props).forEach(key => {
41
- schema[key] = processModelSchemaForProp(props[key]);
42
- });
43
- const resolvers = metadataStorage_1.MetadataStorage.getSchemaResolvers(parent) ?? {};
44
- Object.keys(resolvers).forEach(key => {
45
- resolverMap[key] = resolvers[key];
46
- });
47
- parent = Object.getPrototypeOf(parent);
48
- }
49
- const model = (0, models_1.createModel)({
50
- name: targetAsModel.name,
51
- schema,
52
- clean: targetAsModel.clean,
53
- validate: targetAsModel.validate,
54
- resolvers: {
55
- ...resolverMap,
56
- ...modelResolvers
57
- }
58
- });
59
- modelCache.set(schemaId, model);
60
- return model;
61
- }
62
- exports.getModelForClass = getModelForClass;
@@ -1 +0,0 @@
1
- export {};
@@ -1,59 +0,0 @@
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 decorators_1 = require("../decorators");
14
- const getModelForClass_1 = require("./getModelForClass");
15
- describe('getModelForClass', () => {
16
- it('should correctly pass the clean option to submodels', async () => {
17
- const clean = () => ({ name: 'hello' });
18
- const FileModel = (0, models_1.createModel)({
19
- name: 'File',
20
- schema: {
21
- name: {
22
- type: String
23
- }
24
- },
25
- clean
26
- });
27
- let Config = class Config {
28
- };
29
- __decorate([
30
- (0, decorators_1.Prop)({
31
- type: [FileModel]
32
- }),
33
- __metadata("design:type", Object)
34
- ], Config.prototype, "files", void 0);
35
- Config = __decorate([
36
- (0, decorators_1.TypedSchema)()
37
- ], Config);
38
- const ConfigAsModel = (0, models_1.createModel)({
39
- name: 'Config',
40
- schema: {
41
- files: {
42
- type: [FileModel]
43
- }
44
- }
45
- });
46
- const modelForClass = (0, getModelForClass_1.getModelForClass)(Config);
47
- const example = {
48
- files: [
49
- {
50
- _id: '1'
51
- }
52
- ]
53
- };
54
- const cleanedAsModel = await ConfigAsModel.cleanAndValidate(example);
55
- expect(cleanedAsModel.files[0].name).toEqual('hello');
56
- const cleanedAsClass = await modelForClass.cleanAndValidate(example);
57
- expect(cleanedAsClass).toEqual(cleanedAsModel);
58
- });
59
- });
@@ -1,3 +0,0 @@
1
- import { Schema } from '@orion-js/schema';
2
- import { Constructor } from '../utils/interfaces';
3
- export declare function getSchemaForClass<TClass>(target: Constructor<TClass>): Schema;
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSchemaForClass = void 0;
4
- const processSchemaForProp_1 = require("./helpers/processSchemaForProp");
5
- function getSchemaForClass(target) {
6
- return (0, processSchemaForProp_1.getSchemaForClassRecursive)(target);
7
- }
8
- exports.getSchemaForClass = getSchemaForClass;
@@ -1,4 +0,0 @@
1
- import { PropOptions } from '../../decorators/prop';
2
- import { Schema } from '@orion-js/schema';
3
- export declare function processSchemaForProp(prop: PropOptions): any;
4
- export declare function getSchemaForClassRecursive(target: any): Schema;