@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.
- package/dist/index.cjs +6776 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +6748 -0
- package/package.json +24 -22
- package/LICENSE +0 -21
- package/lib/decorators/index.d.ts +0 -4
- package/lib/decorators/index.js +0 -16
- package/lib/decorators/prop.d.ts +0 -8
- package/lib/decorators/prop.js +0 -30
- package/lib/decorators/prop.test.d.ts +0 -1
- package/lib/decorators/prop.test.js +0 -26
- package/lib/decorators/resolver.d.ts +0 -5
- package/lib/decorators/resolver.js +0 -14
- package/lib/decorators/typedModel.d.ts +0 -5
- package/lib/decorators/typedModel.js +0 -16
- package/lib/decorators/typedSchema.d.ts +0 -2
- package/lib/decorators/typedSchema.js +0 -13
- package/lib/enum.test.d.ts +0 -1
- package/lib/enum.test.js +0 -29
- package/lib/errors/CannotDetermineType.d.ts +0 -3
- package/lib/errors/CannotDetermineType.js +0 -9
- package/lib/errors/CannotUseArray.d.ts +0 -3
- package/lib/errors/CannotUseArray.js +0 -9
- package/lib/errors/PropertyAlreadyExists.d.ts +0 -3
- package/lib/errors/PropertyAlreadyExists.js +0 -9
- package/lib/errors/SchemaAlreadyExists.d.ts +0 -3
- package/lib/errors/SchemaAlreadyExists.js +0 -9
- package/lib/errors/index.d.ts +0 -4
- package/lib/errors/index.js +0 -16
- package/lib/factories/cloneSchemaClass.d.ts +0 -23
- package/lib/factories/cloneSchemaClass.js +0 -28
- package/lib/factories/cloneSchemaClass.test.d.ts +0 -1
- package/lib/factories/cloneSchemaClass.test.js +0 -38
- package/lib/factories/getModelForClass.d.ts +0 -3
- package/lib/factories/getModelForClass.js +0 -62
- package/lib/factories/getModelForClass.test.d.ts +0 -1
- package/lib/factories/getModelForClass.test.js +0 -59
- package/lib/factories/getSchemaForClass.d.ts +0 -3
- package/lib/factories/getSchemaForClass.js +0 -8
- package/lib/factories/helpers/processSchemaForProp.d.ts +0 -4
- package/lib/factories/helpers/processSchemaForProp.js +0 -65
- package/lib/factories/index.d.ts +0 -3
- package/lib/factories/index.js +0 -15
- package/lib/index.d.ts +0 -2
- package/lib/index.js +0 -14
- package/lib/index.test.d.ts +0 -1
- package/lib/index.test.js +0 -682
- package/lib/schema.test.d.ts +0 -1
- package/lib/schema.test.js +0 -94
- package/lib/storage/metadataStorage.d.ts +0 -28
- package/lib/storage/metadataStorage.js +0 -55
- package/lib/utils/interfaces.d.ts +0 -3
- package/lib/utils/interfaces.js +0 -2
- package/lib/utils/isClass.d.ts +0 -1
- 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.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
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
|
-
"
|
|
14
|
+
"dist"
|
|
8
15
|
],
|
|
9
16
|
"author": "dmerrill6",
|
|
10
17
|
"license": "MIT",
|
|
11
18
|
"scripts": {
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
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": "
|
|
21
|
-
"@orion-js/models": "
|
|
22
|
-
"@orion-js/resolvers": "
|
|
23
|
-
"@orion-js/schema": "
|
|
24
|
-
"@orion-js/services": "
|
|
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
|
-
"
|
|
39
|
-
"typescript": "^4.4.4"
|
|
41
|
+
"typescript": "^5.4.5"
|
|
40
42
|
},
|
|
41
43
|
"publishConfig": {
|
|
42
44
|
"access": "public"
|
|
43
45
|
},
|
|
44
|
-
"gitHead": "
|
|
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.
|
package/lib/decorators/index.js
DELETED
|
@@ -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);
|
package/lib/decorators/prop.d.ts
DELETED
|
@@ -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;
|
package/lib/decorators/prop.js
DELETED
|
@@ -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,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,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,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;
|
package/lib/enum.test.d.ts
DELETED
|
@@ -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,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,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,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,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;
|
package/lib/errors/index.d.ts
DELETED
package/lib/errors/index.js
DELETED
|
@@ -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,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,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;
|