topsyde-utils 1.0.199 → 1.0.201
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/utils/BaseDto.d.ts +11 -3
- package/dist/utils/BaseDto.js +87 -4
- package/dist/utils/BaseDto.js.map +1 -1
- package/dist/utils/BaseEntity.d.ts +8 -8
- package/dist/utils/BaseEntity.js +12 -12
- package/dist/utils/BaseEntity.js.map +1 -1
- package/package.json +1 -1
- package/src/utils/BaseDto.ts +34 -4
- package/src/utils/BaseEntity.ts +13 -13
package/dist/utils/BaseDto.d.ts
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
import type { ClassConstructor, ClassTransformOptions } from "class-transformer";
|
2
|
-
import { ValidationError } from "class-validator";
|
2
|
+
import { ValidationArguments, ValidationError, ValidatorConstraintInterface } from "class-validator";
|
3
|
+
/**
|
4
|
+
* Custom validator for number | number[] type
|
5
|
+
* Validates that value is either a single number or a 2-element number array [min, max]
|
6
|
+
*/
|
7
|
+
export declare class IsNumberOrRangeConstraint implements ValidatorConstraintInterface {
|
8
|
+
validate(value: any, args: ValidationArguments): boolean;
|
9
|
+
defaultMessage(args: ValidationArguments): string;
|
10
|
+
}
|
3
11
|
/**
|
4
12
|
* Base typesafe class for Data Transfer Objects (DTOs)
|
5
13
|
*/
|
@@ -27,7 +35,7 @@ export declare abstract class Dto {
|
|
27
35
|
* @returns New instance of the DTO
|
28
36
|
* @throws ValidationError[] if validation fails and validate is true
|
29
37
|
*/
|
30
|
-
static
|
38
|
+
static Create<T extends Dto>(cls: ClassConstructor<T>, data: Record<string, unknown>, options?: ClassTransformOptions): T;
|
31
39
|
/**
|
32
40
|
* Creates an array of DTOs from an array of plain objects
|
33
41
|
* @param cls - The class constructor to create instances from
|
@@ -35,5 +43,5 @@ export declare abstract class Dto {
|
|
35
43
|
* @param options - Class transformer options for controlling exposure and transformation
|
36
44
|
* @returns Array of DTO instances
|
37
45
|
*/
|
38
|
-
static
|
46
|
+
static CreateMany<T extends Dto>(cls: ClassConstructor<T>, dataArray: Record<string, unknown>[], options?: ClassTransformOptions): T[];
|
39
47
|
}
|
package/dist/utils/BaseDto.js
CHANGED
@@ -1,6 +1,89 @@
|
|
1
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
2
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
3
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
4
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
5
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
6
|
+
var _, done = false;
|
7
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
8
|
+
var context = {};
|
9
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
10
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
11
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
12
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
13
|
+
if (kind === "accessor") {
|
14
|
+
if (result === void 0) continue;
|
15
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
16
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
17
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
18
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
19
|
+
}
|
20
|
+
else if (_ = accept(result)) {
|
21
|
+
if (kind === "field") initializers.unshift(_);
|
22
|
+
else descriptor[key] = _;
|
23
|
+
}
|
24
|
+
}
|
25
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
26
|
+
done = true;
|
27
|
+
};
|
28
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
29
|
+
var useValue = arguments.length > 2;
|
30
|
+
for (var i = 0; i < initializers.length; i++) {
|
31
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
32
|
+
}
|
33
|
+
return useValue ? value : void 0;
|
34
|
+
};
|
35
|
+
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
|
36
|
+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
|
37
|
+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
|
38
|
+
};
|
1
39
|
import { instanceToPlain, plainToInstance } from "class-transformer";
|
2
40
|
import Guards from "./Guards.js";
|
3
|
-
import { validateSync } from "class-validator";
|
41
|
+
import { ValidatorConstraint, validateSync } from "class-validator";
|
42
|
+
/**
|
43
|
+
* Custom validator for number | number[] type
|
44
|
+
* Validates that value is either a single number or a 2-element number array [min, max]
|
45
|
+
*/
|
46
|
+
let IsNumberOrRangeConstraint = (() => {
|
47
|
+
let _classDecorators = [ValidatorConstraint({ name: "isNumberOrRange", async: false })];
|
48
|
+
let _classDescriptor;
|
49
|
+
let _classExtraInitializers = [];
|
50
|
+
let _classThis;
|
51
|
+
var IsNumberOrRangeConstraint = _classThis = class {
|
52
|
+
validate(value, args) {
|
53
|
+
// Allow single number
|
54
|
+
if (typeof value === "number" && !isNaN(value)) {
|
55
|
+
return true;
|
56
|
+
}
|
57
|
+
// Allow array of exactly 2 numbers (range)
|
58
|
+
if (Array.isArray(value)) {
|
59
|
+
if (value.length !== 2)
|
60
|
+
return false;
|
61
|
+
if (typeof value[0] !== "number" || isNaN(value[0]))
|
62
|
+
return false;
|
63
|
+
if (typeof value[1] !== "number" || isNaN(value[1]))
|
64
|
+
return false;
|
65
|
+
// Optional: Validate min <= max
|
66
|
+
if (value[0] > value[1])
|
67
|
+
return false;
|
68
|
+
return true;
|
69
|
+
}
|
70
|
+
return false;
|
71
|
+
}
|
72
|
+
defaultMessage(args) {
|
73
|
+
return "Value must be a number or a 2-element number array [min, max]";
|
74
|
+
}
|
75
|
+
};
|
76
|
+
__setFunctionName(_classThis, "IsNumberOrRangeConstraint");
|
77
|
+
(() => {
|
78
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
79
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
80
|
+
IsNumberOrRangeConstraint = _classThis = _classDescriptor.value;
|
81
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
82
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
83
|
+
})();
|
84
|
+
return IsNumberOrRangeConstraint = _classThis;
|
85
|
+
})();
|
86
|
+
export { IsNumberOrRangeConstraint };
|
4
87
|
/**
|
5
88
|
* Base typesafe class for Data Transfer Objects (DTOs)
|
6
89
|
*/
|
@@ -42,7 +125,7 @@ export class Dto {
|
|
42
125
|
* @returns New instance of the DTO
|
43
126
|
* @throws ValidationError[] if validation fails and validate is true
|
44
127
|
*/
|
45
|
-
static
|
128
|
+
static Create(cls, data, options = {}) {
|
46
129
|
const instance = plainToInstance(cls, data, {
|
47
130
|
...Dto.defaultTransformOptions,
|
48
131
|
...options,
|
@@ -56,8 +139,8 @@ export class Dto {
|
|
56
139
|
* @param options - Class transformer options for controlling exposure and transformation
|
57
140
|
* @returns Array of DTO instances
|
58
141
|
*/
|
59
|
-
static
|
60
|
-
return dataArray.map((data) => Dto.
|
142
|
+
static CreateMany(cls, dataArray, options = {}) {
|
143
|
+
return dataArray.map((data) => Dto.Create(cls, data, options));
|
61
144
|
}
|
62
145
|
}
|
63
146
|
/**
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"BaseDto.js","sourceRoot":"","sources":["../../src/utils/BaseDto.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM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AE,SAAoC,EAAE,UAAiC,EAAE;QAC1I,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;IAChE,CAAC;;AArED;;GAEG;AACuB,2BAAuB,GAA0B;IAC1E,uBAAuB,EAAE,IAAI;IAC7B,mBAAmB,EAAE,IAAI;IACzB,mBAAmB,EAAE,IAAI;IACzB,wBAAwB,EAAE,KAAK,EAAE,uDAAuD;CACxF,CAAC","sourcesContent":["import type { ClassConstructor, ClassTransformOptions } from \"class-transformer\";\nimport { instanceToPlain, plainToInstance } from \"class-transformer\";\nimport Guards from \"./Guards\";\nimport { ValidationArguments, ValidationError, ValidatorConstraint, ValidatorConstraintInterface, validateSync } from \"class-validator\";\n\n/**\n * Custom validator for number | number[] type\n * Validates that value is either a single number or a 2-element number array [min, max]\n */\n@ValidatorConstraint({ name: \"isNumberOrRange\", async: false })\nexport class IsNumberOrRangeConstraint implements ValidatorConstraintInterface {\n\tvalidate(value: any, args: ValidationArguments) {\n\t\t// Allow single number\n\t\tif (typeof value === \"number\" && !isNaN(value)) {\n\t\t\treturn true;\n\t\t}\n\n\t\t// Allow array of exactly 2 numbers (range)\n\t\tif (Array.isArray(value)) {\n\t\t\tif (value.length !== 2) return false;\n\t\t\tif (typeof value[0] !== \"number\" || isNaN(value[0])) return false;\n\t\t\tif (typeof value[1] !== \"number\" || isNaN(value[1])) return false;\n\t\t\t// Optional: Validate min <= max\n\t\t\tif (value[0] > value[1]) return false;\n\t\t\treturn true;\n\t\t}\n\n\t\treturn false;\n\t}\n\n\tdefaultMessage(args: ValidationArguments) {\n\t\treturn \"Value must be a number or a 2-element number array [min, max]\";\n\t}\n}\n\n/**\n * Base typesafe class for Data Transfer Objects (DTOs)\n */\nexport abstract class Dto {\n\t/**\n\t * Default options for class transformation\n\t */\n\tprotected static readonly defaultTransformOptions: ClassTransformOptions = {\n\t\texcludeExtraneousValues: true,\n\t\tenableCircularCheck: true,\n\t\texposeDefaultValues: true,\n\t\tenableImplicitConversion: false, // Safer default, especially when using class-validator\n\t};\n\n\t/**\n\t * Validates the DTO instance\n\t * @throws ValidationError[] if validation fails\n\t */\n\tpublic validate(): ValidationError[] {\n\t\tconst errors = validateSync(this, {\n\t\t\tvalidationError: { target: false },\n\t\t\tforbidUnknownValues: true,\n\t\t});\n\t\tif (errors.length > 0) {\n\t\t\tthrow errors;\n\t\t}\n\t\treturn errors;\n\t}\n\n\t/**\n\t * Converts the DTO to a plain object\n\t * @param options - Class transformer options for controlling exposure and transformation\n\t * @returns Plain object representation of the DTO\n\t */\n\tpublic toJSON<T = Record<string, unknown>>(include_undefined: boolean = true, options?: ClassTransformOptions): T {\n\t\tconst value = instanceToPlain(this, {\n\t\t\t...Dto.defaultTransformOptions,\n\t\t\t...options,\n\t\t}) as T;\n\n\t\tif (!include_undefined) {\n\t\t\treturn Object.fromEntries(Object.entries(value as Record<string, unknown>).filter(([_, v]) => !Guards.IsNil(v))) as T;\n\t\t}\n\n\t\treturn value;\n\t}\n\n\t/**\n\t * Creates a new instance of the DTO with validation\n\t * @param cls - The class constructor to create an instance from\n\t * @param data - Data to create the DTO from\n\t * @param options - Class transformer options for controlling exposure and transformation\n\t * @returns New instance of the DTO\n\t * @throws ValidationError[] if validation fails and validate is true\n\t */\n\tpublic static Create<T extends Dto>(cls: ClassConstructor<T>, data: Record<string, unknown>, options: ClassTransformOptions = {}): T {\n\t\tconst instance = plainToInstance(cls, data, {\n\t\t\t...Dto.defaultTransformOptions,\n\t\t\t...options,\n\t\t});\n\n\t\treturn instance;\n\t}\n\n\t/**\n\t * Creates an array of DTOs from an array of plain objects\n\t * @param cls - The class constructor to create instances from\n\t * @param dataArray - Array of data to create DTOs from\n\t * @param options - Class transformer options for controlling exposure and transformation\n\t * @returns Array of DTO instances\n\t */\n\tpublic static CreateMany<T extends Dto>(cls: ClassConstructor<T>, dataArray: Record<string, unknown>[], options: ClassTransformOptions = {}): T[] {\n\t\treturn dataArray.map((data) => Dto.Create(cls, data, options));\n\t}\n}\n"]}
|
@@ -9,21 +9,21 @@ export default abstract class BaseEntity {
|
|
9
9
|
* Abstract method - entities must define how to convert to DTO
|
10
10
|
*/
|
11
11
|
abstract toDto(): Dto;
|
12
|
+
/**
|
13
|
+
* Updates entity with partial data (immutable - returns new instance)
|
14
|
+
* @param data - Partial data to update
|
15
|
+
* @param validate - Whether to validate after update (default: true)
|
16
|
+
*/
|
17
|
+
update<T extends BaseEntity>(this: T, data: Partial<T>): T;
|
12
18
|
/**
|
13
19
|
* Creates a new entity instance from DTO with validation
|
14
20
|
* @param cls - Entity class constructor
|
15
21
|
* @param dto - DTO to create entity from
|
16
22
|
* @param validate - Whether to validate after creation (default: true)
|
17
23
|
*/
|
18
|
-
static
|
24
|
+
static FromDto<T extends BaseEntity>(cls: ClassConstructor<T>, dto: Dto): T;
|
19
25
|
/**
|
20
26
|
* Creates multiple entities from DTOs
|
21
27
|
*/
|
22
|
-
static
|
23
|
-
/**
|
24
|
-
* Updates entity with partial data (immutable - returns new instance)
|
25
|
-
* @param data - Partial data to update
|
26
|
-
* @param validate - Whether to validate after update (default: true)
|
27
|
-
*/
|
28
|
-
update<T extends BaseEntity>(this: T, data: Partial<T>): T;
|
28
|
+
static FromDtos<T extends BaseEntity>(cls: ClassConstructor<T>, dtos: Dto[]): T[];
|
29
29
|
}
|
package/dist/utils/BaseEntity.js
CHANGED
@@ -6,30 +6,30 @@ export default class BaseEntity {
|
|
6
6
|
toJSON() {
|
7
7
|
return instanceToPlain(this);
|
8
8
|
}
|
9
|
+
/**
|
10
|
+
* Updates entity with partial data (immutable - returns new instance)
|
11
|
+
* @param data - Partial data to update
|
12
|
+
* @param validate - Whether to validate after update (default: true)
|
13
|
+
*/
|
14
|
+
update(data) {
|
15
|
+
const updated = Object.assign(Object.create(Object.getPrototypeOf(this)), this, data);
|
16
|
+
return updated;
|
17
|
+
}
|
9
18
|
/**
|
10
19
|
* Creates a new entity instance from DTO with validation
|
11
20
|
* @param cls - Entity class constructor
|
12
21
|
* @param dto - DTO to create entity from
|
13
22
|
* @param validate - Whether to validate after creation (default: true)
|
14
23
|
*/
|
15
|
-
static
|
24
|
+
static FromDto(cls, dto) {
|
16
25
|
const instance = plainToInstance(cls, dto.toJSON());
|
17
26
|
return instance;
|
18
27
|
}
|
19
28
|
/**
|
20
29
|
* Creates multiple entities from DTOs
|
21
30
|
*/
|
22
|
-
static
|
23
|
-
return dtos.map((dto) => BaseEntity.
|
24
|
-
}
|
25
|
-
/**
|
26
|
-
* Updates entity with partial data (immutable - returns new instance)
|
27
|
-
* @param data - Partial data to update
|
28
|
-
* @param validate - Whether to validate after update (default: true)
|
29
|
-
*/
|
30
|
-
update(data) {
|
31
|
-
const updated = Object.assign(Object.create(Object.getPrototypeOf(this)), this, data);
|
32
|
-
return updated;
|
31
|
+
static FromDtos(cls, dtos) {
|
32
|
+
return dtos.map((dto) => BaseEntity.FromDto(cls, dto));
|
33
33
|
}
|
34
34
|
}
|
35
35
|
//# sourceMappingURL=BaseEntity.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"BaseEntity.js","sourceRoot":"","sources":["../../src/utils/BaseEntity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,eAAe,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAGvF,MAAM,CAAC,OAAO,OAAgB,UAAU;IACvC;;OAEG;IACI,MAAM;QACZ,OAAO,eAAe,CAAC,IAAI,CAAM,CAAC;IACnC,CAAC;IAOD
|
1
|
+
{"version":3,"file":"BaseEntity.js","sourceRoot":"","sources":["../../src/utils/BaseEntity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,eAAe,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAGvF,MAAM,CAAC,OAAO,OAAgB,UAAU;IACvC;;OAEG;IACI,MAAM;QACZ,OAAO,eAAe,CAAC,IAAI,CAAM,CAAC;IACnC,CAAC;IAOD;;;;OAIG;IACI,MAAM,CAAgC,IAAgB;QAC5D,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QACtF,OAAO,OAAO,CAAC;IAChB,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,OAAO,CAAuB,GAAwB,EAAE,GAAQ;QAC7E,MAAM,QAAQ,GAAG,eAAe,CAAC,GAAG,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;QACpD,OAAO,QAAQ,CAAC;IACjB,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,QAAQ,CAAuB,GAAwB,EAAE,IAAW;QACjF,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IACxD,CAAC;CACD","sourcesContent":["import { ClassConstructor, instanceToPlain, plainToInstance } from \"class-transformer\";\nimport { Dto } from \"./BaseDto\";\n\nexport default abstract class BaseEntity {\n\t/**\n\t * Converts entity to plain object\n\t */\n\tpublic toJSON<T = Record<string, unknown>>(): T {\n\t\treturn instanceToPlain(this) as T;\n\t}\n\n\t/**\n\t * Abstract method - entities must define how to convert to DTO\n\t */\n\tpublic abstract toDto(): Dto;\n\n\t/**\n\t * Updates entity with partial data (immutable - returns new instance)\n\t * @param data - Partial data to update\n\t * @param validate - Whether to validate after update (default: true)\n\t */\n\tpublic update<T extends BaseEntity>(this: T, data: Partial<T>): T {\n\t\tconst updated = Object.assign(Object.create(Object.getPrototypeOf(this)), this, data);\n\t\treturn updated;\n\t}\n\n\t/**\n\t * Creates a new entity instance from DTO with validation\n\t * @param cls - Entity class constructor\n\t * @param dto - DTO to create entity from\n\t * @param validate - Whether to validate after creation (default: true)\n\t */\n\tpublic static FromDto<T extends BaseEntity>(cls: ClassConstructor<T>, dto: Dto): T {\n\t\tconst instance = plainToInstance(cls, dto.toJSON());\n\t\treturn instance;\n\t}\n\n\t/**\n\t * Creates multiple entities from DTOs\n\t */\n\tpublic static FromDtos<T extends BaseEntity>(cls: ClassConstructor<T>, dtos: Dto[]): T[] {\n\t\treturn dtos.map((dto) => BaseEntity.FromDto(cls, dto));\n\t}\n}\n"]}
|
package/package.json
CHANGED
package/src/utils/BaseDto.ts
CHANGED
@@ -1,7 +1,37 @@
|
|
1
1
|
import type { ClassConstructor, ClassTransformOptions } from "class-transformer";
|
2
2
|
import { instanceToPlain, plainToInstance } from "class-transformer";
|
3
3
|
import Guards from "./Guards";
|
4
|
-
import { ValidationError, validateSync } from "class-validator";
|
4
|
+
import { ValidationArguments, ValidationError, ValidatorConstraint, ValidatorConstraintInterface, validateSync } from "class-validator";
|
5
|
+
|
6
|
+
/**
|
7
|
+
* Custom validator for number | number[] type
|
8
|
+
* Validates that value is either a single number or a 2-element number array [min, max]
|
9
|
+
*/
|
10
|
+
@ValidatorConstraint({ name: "isNumberOrRange", async: false })
|
11
|
+
export class IsNumberOrRangeConstraint implements ValidatorConstraintInterface {
|
12
|
+
validate(value: any, args: ValidationArguments) {
|
13
|
+
// Allow single number
|
14
|
+
if (typeof value === "number" && !isNaN(value)) {
|
15
|
+
return true;
|
16
|
+
}
|
17
|
+
|
18
|
+
// Allow array of exactly 2 numbers (range)
|
19
|
+
if (Array.isArray(value)) {
|
20
|
+
if (value.length !== 2) return false;
|
21
|
+
if (typeof value[0] !== "number" || isNaN(value[0])) return false;
|
22
|
+
if (typeof value[1] !== "number" || isNaN(value[1])) return false;
|
23
|
+
// Optional: Validate min <= max
|
24
|
+
if (value[0] > value[1]) return false;
|
25
|
+
return true;
|
26
|
+
}
|
27
|
+
|
28
|
+
return false;
|
29
|
+
}
|
30
|
+
|
31
|
+
defaultMessage(args: ValidationArguments) {
|
32
|
+
return "Value must be a number or a 2-element number array [min, max]";
|
33
|
+
}
|
34
|
+
}
|
5
35
|
|
6
36
|
/**
|
7
37
|
* Base typesafe class for Data Transfer Objects (DTOs)
|
@@ -58,7 +88,7 @@ export abstract class Dto {
|
|
58
88
|
* @returns New instance of the DTO
|
59
89
|
* @throws ValidationError[] if validation fails and validate is true
|
60
90
|
*/
|
61
|
-
static
|
91
|
+
public static Create<T extends Dto>(cls: ClassConstructor<T>, data: Record<string, unknown>, options: ClassTransformOptions = {}): T {
|
62
92
|
const instance = plainToInstance(cls, data, {
|
63
93
|
...Dto.defaultTransformOptions,
|
64
94
|
...options,
|
@@ -74,7 +104,7 @@ export abstract class Dto {
|
|
74
104
|
* @param options - Class transformer options for controlling exposure and transformation
|
75
105
|
* @returns Array of DTO instances
|
76
106
|
*/
|
77
|
-
static
|
78
|
-
return dataArray.map((data) => Dto.
|
107
|
+
public static CreateMany<T extends Dto>(cls: ClassConstructor<T>, dataArray: Record<string, unknown>[], options: ClassTransformOptions = {}): T[] {
|
108
|
+
return dataArray.map((data) => Dto.Create(cls, data, options));
|
79
109
|
}
|
80
110
|
}
|
package/src/utils/BaseEntity.ts
CHANGED
@@ -14,13 +14,23 @@ export default abstract class BaseEntity {
|
|
14
14
|
*/
|
15
15
|
public abstract toDto(): Dto;
|
16
16
|
|
17
|
+
/**
|
18
|
+
* Updates entity with partial data (immutable - returns new instance)
|
19
|
+
* @param data - Partial data to update
|
20
|
+
* @param validate - Whether to validate after update (default: true)
|
21
|
+
*/
|
22
|
+
public update<T extends BaseEntity>(this: T, data: Partial<T>): T {
|
23
|
+
const updated = Object.assign(Object.create(Object.getPrototypeOf(this)), this, data);
|
24
|
+
return updated;
|
25
|
+
}
|
26
|
+
|
17
27
|
/**
|
18
28
|
* Creates a new entity instance from DTO with validation
|
19
29
|
* @param cls - Entity class constructor
|
20
30
|
* @param dto - DTO to create entity from
|
21
31
|
* @param validate - Whether to validate after creation (default: true)
|
22
32
|
*/
|
23
|
-
static
|
33
|
+
public static FromDto<T extends BaseEntity>(cls: ClassConstructor<T>, dto: Dto): T {
|
24
34
|
const instance = plainToInstance(cls, dto.toJSON());
|
25
35
|
return instance;
|
26
36
|
}
|
@@ -28,17 +38,7 @@ export default abstract class BaseEntity {
|
|
28
38
|
/**
|
29
39
|
* Creates multiple entities from DTOs
|
30
40
|
*/
|
31
|
-
static
|
32
|
-
return dtos.map((dto) => BaseEntity.
|
33
|
-
}
|
34
|
-
|
35
|
-
/**
|
36
|
-
* Updates entity with partial data (immutable - returns new instance)
|
37
|
-
* @param data - Partial data to update
|
38
|
-
* @param validate - Whether to validate after update (default: true)
|
39
|
-
*/
|
40
|
-
public update<T extends BaseEntity>(this: T, data: Partial<T>): T {
|
41
|
-
const updated = Object.assign(Object.create(Object.getPrototypeOf(this)), this, data);
|
42
|
-
return updated;
|
41
|
+
public static FromDtos<T extends BaseEntity>(cls: ClassConstructor<T>, dtos: Dto[]): T[] {
|
42
|
+
return dtos.map((dto) => BaseEntity.FromDto(cls, dto));
|
43
43
|
}
|
44
44
|
}
|