@orion-js/typed-model 3.3.10 → 3.3.11
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/lib/factories/cloneSchemaClass.d.ts +18 -0
- package/lib/factories/cloneSchemaClass.js +24 -0
- package/lib/factories/cloneSchemaClass.test.d.ts +1 -0
- package/lib/factories/cloneSchemaClass.test.js +38 -0
- package/lib/factories/getModelForClass.js +4 -0
- package/lib/factories/index.d.ts +1 -0
- package/lib/factories/index.js +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { CloneOptions } from '@orion-js/models';
|
|
2
|
+
import { Constructor } from '../utils/interfaces';
|
|
3
|
+
export interface CloneSchemaClassOptions<TClass, TFields extends keyof TClass> {
|
|
4
|
+
name: string;
|
|
5
|
+
pickFields: readonly TFields[];
|
|
6
|
+
mapFields?: CloneOptions['mapFields'];
|
|
7
|
+
extendSchema?: CloneOptions['extendSchema'];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* This function returns a cloned model but the type is a subset of the original Schema.
|
|
11
|
+
* It will work as a Model but the type will not be the Type of the Model.
|
|
12
|
+
*
|
|
13
|
+
* You can use the return variable as a Model and you `typeof returnType` will be the class
|
|
14
|
+
* for type checking.
|
|
15
|
+
*
|
|
16
|
+
* Remember that in runtime this will be a Model and not the class.
|
|
17
|
+
*/
|
|
18
|
+
export declare function cloneSchemaClass<TClass, TFields extends keyof TClass>(schema: Constructor<TClass>, options: CloneSchemaClassOptions<TClass, TFields>): Pick<TClass, TFields>;
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
* It will work as a Model but the type will not be the Type of the Model.
|
|
8
|
+
*
|
|
9
|
+
* You can use the return variable as a Model and you `typeof returnType` will be the class
|
|
10
|
+
* for type checking.
|
|
11
|
+
*
|
|
12
|
+
* Remember that in runtime this will be a Model and not the class.
|
|
13
|
+
*/
|
|
14
|
+
function cloneSchemaClass(schema, options) {
|
|
15
|
+
const model = (0, getModelForClass_1.getModelForClass)(schema);
|
|
16
|
+
const newModel = model.clone({
|
|
17
|
+
name: options.name,
|
|
18
|
+
pickFields: options.pickFields,
|
|
19
|
+
mapFields: options.mapFields,
|
|
20
|
+
extendSchema: options.extendSchema
|
|
21
|
+
});
|
|
22
|
+
return newModel;
|
|
23
|
+
}
|
|
24
|
+
exports.cloneSchemaClass = cloneSchemaClass;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
});
|
|
@@ -16,6 +16,10 @@ function processModelSchemaForProp(prop) {
|
|
|
16
16
|
return (0, processSchemaForProp_1.processSchemaForProp)(prop);
|
|
17
17
|
}
|
|
18
18
|
function getModelForClass(target) {
|
|
19
|
+
const targetAsModel = target;
|
|
20
|
+
if (targetAsModel.__isModel) {
|
|
21
|
+
return targetAsModel;
|
|
22
|
+
}
|
|
19
23
|
let modelResolvers = null;
|
|
20
24
|
if (target.prototype.typedModel) {
|
|
21
25
|
modelResolvers = target.prototype.resolvers || {};
|
package/lib/factories/index.d.ts
CHANGED
package/lib/factories/index.js
CHANGED
|
@@ -12,3 +12,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
__exportStar(require("./getSchemaForClass"), exports);
|
|
14
14
|
__exportStar(require("./getModelForClass"), exports);
|
|
15
|
+
__exportStar(require("./cloneSchemaClass"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/typed-model",
|
|
3
|
-
"version": "3.3.
|
|
3
|
+
"version": "3.3.11",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@orion-js/helpers": "^3.3.6",
|
|
21
|
-
"@orion-js/models": "^3.3.
|
|
21
|
+
"@orion-js/models": "^3.3.11",
|
|
22
22
|
"@orion-js/resolvers": "^3.3.9",
|
|
23
23
|
"@orion-js/schema": "^3.3.9",
|
|
24
24
|
"@orion-js/services": "^3.3.6",
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"publishConfig": {
|
|
42
42
|
"access": "public"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "67a10b0ec451c411c5a8e3e1ba60c707243fd0d5"
|
|
45
45
|
}
|