@forklaunch/core 0.6.6 → 0.7.1
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/{openTelemetryCollector-CWrfzmmW.d.mts → openTelemetryCollector-CmZ3T_2T.d.mts} +9 -1
- package/lib/{openTelemetryCollector-CWrfzmmW.d.ts → openTelemetryCollector-CmZ3T_2T.d.ts} +9 -1
- package/lib/src/cache/index.d.mts +210 -24
- package/lib/src/cache/index.d.ts +210 -24
- package/lib/src/cache/index.js +364 -31
- package/lib/src/cache/index.js.map +1 -1
- package/lib/src/cache/index.mjs +374 -31
- package/lib/src/cache/index.mjs.map +1 -1
- package/lib/src/controllers/index.d.mts +10 -0
- package/lib/src/controllers/index.d.ts +10 -0
- package/lib/src/http/index.d.mts +51 -4
- package/lib/src/http/index.d.ts +51 -4
- package/lib/src/http/index.js +24 -1
- package/lib/src/http/index.js.map +1 -1
- package/lib/src/http/index.mjs +23 -1
- package/lib/src/http/index.mjs.map +1 -1
- package/lib/src/mappers/index.d.mts +2 -2
- package/lib/src/mappers/index.d.ts +2 -2
- package/lib/src/mappers/index.js.map +1 -1
- package/lib/src/mappers/index.mjs.map +1 -1
- package/lib/src/persistence/index.d.mts +120 -0
- package/lib/src/persistence/index.d.ts +120 -0
- package/lib/src/persistence/index.js +152 -0
- package/lib/src/persistence/index.js.map +1 -0
- package/lib/src/persistence/index.mjs +127 -0
- package/lib/src/persistence/index.mjs.map +1 -0
- package/lib/src/services/index.d.mts +3 -2
- package/lib/src/services/index.d.ts +3 -2
- package/lib/src/services/index.js.map +1 -1
- package/lib/src/services/index.mjs.map +1 -1
- package/package.json +19 -13
@@ -114,7 +114,7 @@ declare abstract class BaseDtoMapper<SV extends AnySchemaValidator> {
|
|
114
114
|
/**
|
115
115
|
* Abstract class representing a request entity mapper.
|
116
116
|
*
|
117
|
-
* @template Entity - A type that extends
|
117
|
+
* @template Entity - A type that extends SqlBaseEntity.
|
118
118
|
* @template SV - A type that extends AnySchemaValidator.
|
119
119
|
* @extends {BaseDtoMapper<SV>}
|
120
120
|
*/
|
@@ -178,7 +178,7 @@ declare abstract class RequestDtoMapper<Entity, SV extends AnySchemaValidator> e
|
|
178
178
|
/**
|
179
179
|
* Abstract class representing a response entity mapper.
|
180
180
|
*
|
181
|
-
* @template Entity - A type that extends
|
181
|
+
* @template Entity - A type that extends SqlBaseEntity.
|
182
182
|
* @template SV - A type that extends AnySchemaValidator.
|
183
183
|
* @extends {BaseDtoMapper<SV>}
|
184
184
|
*/
|
@@ -114,7 +114,7 @@ declare abstract class BaseDtoMapper<SV extends AnySchemaValidator> {
|
|
114
114
|
/**
|
115
115
|
* Abstract class representing a request entity mapper.
|
116
116
|
*
|
117
|
-
* @template Entity - A type that extends
|
117
|
+
* @template Entity - A type that extends SqlBaseEntity.
|
118
118
|
* @template SV - A type that extends AnySchemaValidator.
|
119
119
|
* @extends {BaseDtoMapper<SV>}
|
120
120
|
*/
|
@@ -178,7 +178,7 @@ declare abstract class RequestDtoMapper<Entity, SV extends AnySchemaValidator> e
|
|
178
178
|
/**
|
179
179
|
* Abstract class representing a response entity mapper.
|
180
180
|
*
|
181
|
-
* @template Entity - A type that extends
|
181
|
+
* @template Entity - A type that extends SqlBaseEntity.
|
182
182
|
* @template SV - A type that extends AnySchemaValidator.
|
183
183
|
* @extends {BaseDtoMapper<SV>}
|
184
184
|
*/
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../src/mappers/index.ts","../../../src/mappers/models/requestDtoMapper.model.ts","../../../src/mappers/models/baseDtoMapper.model.ts","../../../src/mappers/models/responseDtoMapper.model.ts","../../../src/mappers/serviceSchemaResolver.ts","../../../src/mappers/transformIntoInternalDtoMapper.ts"],"sourcesContent":["export * from './models/requestDtoMapper.model';\nexport * from './models/responseDtoMapper.model';\nexport * from './serviceSchemaResolver';\nexport * from './transformIntoInternalDtoMapper';\nexport * from './types/mappers.types';\nexport * from './types/internalDtoMapper.types';\nexport * from './types/serviceSchemaResolver.types';\n","import {\n AnySchemaValidator,\n prettyPrintParseErrors\n} from '@forklaunch/validator';\nimport { DtoMapperConstructor } from '../interfaces/mappers.interface';\nimport { BaseDtoMapper, construct } from './baseDtoMapper.model';\n\n/**\n * Abstract class representing a request entity mapper.\n *\n * @template Entity - A type that extends BaseEntity.\n * @template SV - A type that extends AnySchemaValidator.\n * @extends {BaseDtoMapper<SV>}\n */\nexport abstract class RequestDtoMapper<\n Entity,\n SV extends AnySchemaValidator\n> extends BaseDtoMapper<SV> {\n /**\n * The entity type.\n * @type {Entity}\n * @protected\n */\n _Entity!: Entity;\n\n /**\n * Converts the underlying DTO to an entity.\n *\n * @abstract\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {Entity} - The entity.\n */\n abstract toEntity(...additionalArgs: unknown[]): Entity;\n\n /**\n * Populates the DTO with data from a JSON object.\n *\n * @param {this['_dto']} json - The JSON object.\n * @returns {this} - The instance of the RequestDtoMapper.\n */\n fromDto(json: this['_dto']): this {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n json\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n this.dto = json;\n return this;\n }\n\n /**\n * Deserializes a JSON object to an entity.\n *\n * @param {this['_dto']} json - The JSON object.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {Entity} - The entity.\n */\n deserializeDtoToEntity(\n json: this['_dto'],\n ...additionalArgs: Parameters<this['toEntity']>\n ): Entity {\n return this.fromDto(json).toEntity(...additionalArgs);\n }\n\n /**\n * Creates an instance of a RequestDtoMapper from a JSON object.\n *\n * @template T - A type that extends RequestDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @template JsonType - The type of the JSON object.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {JsonType} json - The JSON object.\n * @returns {T} - An instance of the T.\n */\n static fromDto<\n T extends RequestDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n JsonType extends T['_dto']\n >(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, json: JsonType): T {\n return construct(this, schemaValidator).fromDto(json);\n }\n\n /**\n * Deserializes a JSON object to an entity.\n *\n * @template T - A type that extends RequestDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @template JsonType - The type of the JSON object.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {JsonType} json - The JSON object.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {T['_Entity']} - The entity.\n */\n static deserializeDtoToEntity<\n T extends RequestDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n JsonType extends T['_dto']\n >(\n this: DtoMapperConstructor<T, SV>,\n schemaValidator: SV,\n json: JsonType,\n ...additionalArgs: Parameters<T['toEntity']>\n ): T['_Entity'] {\n return construct(this, schemaValidator)\n .fromDto(json)\n .toEntity(...additionalArgs);\n }\n}\n","import {\n AnySchemaValidator,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { DtoMapperConstructor } from '../interfaces/mappers.interface';\nimport { DtoMapperSchemaValidatorObject } from '../types/mappers.types';\n\n/**\n * Constructs an instance of a T.\n *\n * @template T - A type that extends BaseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} self - The constructor of the T.\n * @param {SV} [schemaValidator] - The optional schema validator.\n * @returns {T} - An instance of the T.\n */\nexport function construct<T, SV extends AnySchemaValidator>(\n self: DtoMapperConstructor<T, SV>,\n schemaValidator?: SV\n): T {\n return new self(schemaValidator || ({} as SV));\n}\n\n/**\n * Abstract class representing a base entity mapper.\n *\n * @template SV - A type that extends AnySchemaValidator.\n */\nexport abstract class BaseDtoMapper<SV extends AnySchemaValidator> {\n /**\n * The schema validator exact type.\n * @type {SV}\n * @protected\n */\n _SV!: SV;\n\n /**\n * The schema validator as a general type.\n * @type {SchemaValidator}\n * @protected\n */\n protected schemaValidator: SchemaValidator;\n\n /**\n * The schema definition.\n * @type {DtoMapperSchemaValidatorObject<SV>}\n * @abstract\n */\n abstract schema: DtoMapperSchemaValidatorObject<SV>;\n\n /**\n * The Data Transfer Object (DTO).\n * @type {Schema<this['schema'], SV>}\n */\n _dto: Schema<this['schema'], SV> = {} as unknown as Schema<\n this['schema'],\n SV\n >;\n\n /**\n * Creates an instance of BaseDtoMapper.\n *\n * @param {SV} schemaValidator - The schema provider.\n */\n constructor(schemaValidator: SV) {\n this.schemaValidator = schemaValidator as unknown as SchemaValidator;\n }\n\n /**\n * Validates and sets the Data Transfer Object (DTO).\n *\n * @param {this['_dto']} dto - The Data Transfer Object (DTO).\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n set dto(_dto: this['_dto']) {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n _dto\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n this._dto = _dto as unknown as Schema<this['schema'], SV>;\n }\n\n /**\n * Validates and gets the Data Transfer Object (DTO).\n *\n * @returns {this['_dto']} - The Data Transfer Object (DTO).\n */\n get dto(): this['_dto'] {\n return this._dto as unknown as this['_dto'];\n }\n\n /**\n * Gets the schema of a T.\n *\n * @template T - A type that extends BaseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @returns {T['schema']} - The schema of the T.\n */\n static schema<T extends BaseDtoMapper<SV>, SV extends AnySchemaValidator>(\n this: DtoMapperConstructor<T, SV>\n ): T['schema'] {\n return construct(this).schema;\n }\n}\n","import {\n AnySchemaValidator,\n prettyPrintParseErrors\n} from '@forklaunch/validator';\nimport { DtoMapperConstructor } from '../interfaces/mappers.interface';\nimport { BaseDtoMapper, construct } from './baseDtoMapper.model';\n\n/**\n * Abstract class representing a response entity mapper.\n *\n * @template Entity - A type that extends BaseEntity.\n * @template SV - A type that extends AnySchemaValidator.\n * @extends {BaseDtoMapper<SV>}\n */\nexport abstract class ResponseDtoMapper<\n Entity,\n SV extends AnySchemaValidator\n> extends BaseDtoMapper<SV> {\n /**\n * The entity type.\n * @type {Entity}\n * @protected\n */\n _Entity!: Entity;\n\n /**\n * Populates entity mapper with DTO from an entity.\n *\n * @abstract\n * @param {Entity} entity - The entity to convert.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {this} - The instance of the ResponseDtoMapper.\n */\n abstract fromEntity(entity: Entity, ...additionalArgs: unknown[]): this;\n\n /**\n * Converts the underlying DTO to a JSON object.\n *\n * @returns {this['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n toDto(): this['_dto'] {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n this.dto\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return this.dto;\n }\n\n /**\n * Serializes an entity to a JSON object.\n *\n * @param {Entity} entity - The entity to serialize.\n * @returns {this['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n serializeEntityToDto(\n ...[entity, ...additionalArgs]: Parameters<this['fromEntity']>\n ): this['_dto'] {\n return this.fromEntity(entity, ...additionalArgs).toDto();\n }\n\n /**\n * Populates entity mapper with DTO from an entity.\n *\n * @template T - A type that extends ResponseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {T['_Entity']} entity - The entity to convert.\n * @returns {T} - An instance of the T.\n */\n static fromEntity<\n T extends ResponseDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator\n >(\n this: DtoMapperConstructor<T, SV>,\n schemaValidator: SV,\n ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>\n ): T {\n return construct(this, schemaValidator).fromEntity(\n entity,\n ...additionalArgs\n );\n }\n\n /**\n * Serializes an entity to a JSON object.\n *\n * @template T - A type that extends ResponseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {T['_Entity']} entity - The entity to serialize.\n * @returns {T['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n static serializeEntityToDto<\n T extends ResponseDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n DtoType extends T['_dto']\n >(\n this: DtoMapperConstructor<T, SV>,\n schemaValidator: SV,\n ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>\n ): DtoType {\n return construct(this, schemaValidator)\n .fromEntity(entity, ...additionalArgs)\n .toDto() as DtoType;\n }\n}\n","import { isNever } from '@forklaunch/common';\nimport { AnySchemaValidator } from '@forklaunch/validator';\nimport { SchemasByValidator } from './types/serviceSchemaResolver.types';\n\nexport function serviceSchemaResolver<\n Options extends Record<string, unknown>,\n TypeBoxSchemas,\n ZodSchemas\n>(\n TypeBoxSchemas: (options: Options) => TypeBoxSchemas,\n ZodSchemas: (options: Options) => ZodSchemas\n) {\n return <SchemaValidator extends AnySchemaValidator>(\n options: Options & { validator: SchemaValidator }\n ): SchemasByValidator<\n SchemaValidator,\n (options: Options) => TypeBoxSchemas,\n (options: Options) => ZodSchemas\n > => {\n switch (options.validator._Type) {\n case 'TypeBox':\n return TypeBoxSchemas(options) as SchemasByValidator<\n SchemaValidator,\n (options: Options) => TypeBoxSchemas,\n (options: Options) => ZodSchemas\n >;\n case 'Zod':\n return ZodSchemas(options) as SchemasByValidator<\n SchemaValidator,\n (options: Options) => TypeBoxSchemas,\n (options: Options) => ZodSchemas\n >;\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n case 'Mock':\n throw new Error('Mock schema validator not supported');\n default:\n isNever(options.validator._Type);\n throw new Error('Invalid schema validator');\n }\n };\n}\n","import { InstanceTypeRecord } from '@forklaunch/common';\nimport { AnySchemaValidator } from '@forklaunch/validator';\nimport { InternalDtoMapper } from './types/internalDtoMapper.types';\n\nexport function transformIntoInternalDtoMapper<\n SchemaValidator extends AnySchemaValidator,\n DtoMapper extends Record<\n string,\n new (schemaValidator: SchemaValidator) =>\n | {\n dto: unknown;\n _Entity: unknown;\n serializeEntityToDto: unknown;\n }\n | {\n dto: unknown;\n _Entity: unknown;\n deserializeDtoToEntity: unknown;\n }\n >,\n Entities extends Record<keyof DtoMapper, unknown>,\n Dto extends Record<keyof DtoMapper, unknown>\n>(\n mapperss: DtoMapper,\n schemaValidator: SchemaValidator\n): InternalDtoMapper<InstanceTypeRecord<DtoMapper>, Entities, Dto> {\n return Object.fromEntries(\n Object.entries(mapperss).map(([key, value]) => [\n key,\n new value(schemaValidator)\n ])\n ) as unknown as InternalDtoMapper<\n InstanceTypeRecord<DtoMapper>,\n Entities,\n Dto\n >;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,oBAGO;;;ACHP,uBAKO;AAaA,SAAS,UACd,MACA,iBACG;AACH,SAAO,IAAI,KAAK,mBAAoB,CAAC,CAAQ;AAC/C;AAOO,IAAe,gBAAf,MAA4D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMjE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOU;AAAA;AAAA;AAAA;AAAA;AAAA,EAaV,OAAmC,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUpC,YAAY,iBAAqB;AAC/B,SAAK,kBAAkB;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAI,IAAI,MAAoB;AAC1B,UAAM,eAAe,KAAK,gBAAgB;AAAA,MACxC,KAAK,gBAAgB,SAAS,KAAK,MAAM;AAAA,MACzC;AAAA,IACF;AACA,QAA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U,KAAK;AAC/B,cAAM,IAAI,MAAM,0BAA0B;AAAA,IAC9C;AAAA,EACF;AACF;;;ACrCO,SAAS,+BAmBd,UACA,iBACiE;AACjE,SAAO,OAAO;AAAA,IACZ,OAAO,QAAQ,QAAQ,EAAE,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM;AAAA,MAC7C;AAAA,MACA,IAAI,MAAM,eAAe;AAAA,IAC3B,CAAC;AAAA,EACH;AAKF;","names":["import_validator","import_validator"]}
|
1
|
+
{"version":3,"sources":["../../../src/mappers/index.ts","../../../src/mappers/models/requestDtoMapper.model.ts","../../../src/mappers/models/baseDtoMapper.model.ts","../../../src/mappers/models/responseDtoMapper.model.ts","../../../src/mappers/serviceSchemaResolver.ts","../../../src/mappers/transformIntoInternalDtoMapper.ts"],"sourcesContent":["export * from './models/requestDtoMapper.model';\nexport * from './models/responseDtoMapper.model';\nexport * from './serviceSchemaResolver';\nexport * from './transformIntoInternalDtoMapper';\nexport * from './types/mappers.types';\nexport * from './types/internalDtoMapper.types';\nexport * from './types/serviceSchemaResolver.types';\n","import {\n AnySchemaValidator,\n prettyPrintParseErrors\n} from '@forklaunch/validator';\nimport { DtoMapperConstructor } from '../interfaces/mappers.interface';\nimport { BaseDtoMapper, construct } from './baseDtoMapper.model';\n\n/**\n * Abstract class representing a request entity mapper.\n *\n * @template Entity - A type that extends SqlBaseEntity.\n * @template SV - A type that extends AnySchemaValidator.\n * @extends {BaseDtoMapper<SV>}\n */\nexport abstract class RequestDtoMapper<\n Entity,\n SV extends AnySchemaValidator\n> extends BaseDtoMapper<SV> {\n /**\n * The entity type.\n * @type {Entity}\n * @protected\n */\n _Entity!: Entity;\n\n /**\n * Converts the underlying DTO to an entity.\n *\n * @abstract\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {Entity} - The entity.\n */\n abstract toEntity(...additionalArgs: unknown[]): Entity;\n\n /**\n * Populates the DTO with data from a JSON object.\n *\n * @param {this['_dto']} json - The JSON object.\n * @returns {this} - The instance of the RequestDtoMapper.\n */\n fromDto(json: this['_dto']): this {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n json\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n this.dto = json;\n return this;\n }\n\n /**\n * Deserializes a JSON object to an entity.\n *\n * @param {this['_dto']} json - The JSON object.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {Entity} - The entity.\n */\n deserializeDtoToEntity(\n json: this['_dto'],\n ...additionalArgs: Parameters<this['toEntity']>\n ): Entity {\n return this.fromDto(json).toEntity(...additionalArgs);\n }\n\n /**\n * Creates an instance of a RequestDtoMapper from a JSON object.\n *\n * @template T - A type that extends RequestDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @template JsonType - The type of the JSON object.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {JsonType} json - The JSON object.\n * @returns {T} - An instance of the T.\n */\n static fromDto<\n T extends RequestDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n JsonType extends T['_dto']\n >(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, json: JsonType): T {\n return construct(this, schemaValidator).fromDto(json);\n }\n\n /**\n * Deserializes a JSON object to an entity.\n *\n * @template T - A type that extends RequestDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @template JsonType - The type of the JSON object.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {JsonType} json - The JSON object.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {T['_Entity']} - The entity.\n */\n static deserializeDtoToEntity<\n T extends RequestDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n JsonType extends T['_dto']\n >(\n this: DtoMapperConstructor<T, SV>,\n schemaValidator: SV,\n json: JsonType,\n ...additionalArgs: Parameters<T['toEntity']>\n ): T['_Entity'] {\n return construct(this, schemaValidator)\n .fromDto(json)\n .toEntity(...additionalArgs);\n }\n}\n","import {\n AnySchemaValidator,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { DtoMapperConstructor } from '../interfaces/mappers.interface';\nimport { DtoMapperSchemaValidatorObject } from '../types/mappers.types';\n\n/**\n * Constructs an instance of a T.\n *\n * @template T - A type that extends BaseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} self - The constructor of the T.\n * @param {SV} [schemaValidator] - The optional schema validator.\n * @returns {T} - An instance of the T.\n */\nexport function construct<T, SV extends AnySchemaValidator>(\n self: DtoMapperConstructor<T, SV>,\n schemaValidator?: SV\n): T {\n return new self(schemaValidator || ({} as SV));\n}\n\n/**\n * Abstract class representing a base entity mapper.\n *\n * @template SV - A type that extends AnySchemaValidator.\n */\nexport abstract class BaseDtoMapper<SV extends AnySchemaValidator> {\n /**\n * The schema validator exact type.\n * @type {SV}\n * @protected\n */\n _SV!: SV;\n\n /**\n * The schema validator as a general type.\n * @type {SchemaValidator}\n * @protected\n */\n protected schemaValidator: SchemaValidator;\n\n /**\n * The schema definition.\n * @type {DtoMapperSchemaValidatorObject<SV>}\n * @abstract\n */\n abstract schema: DtoMapperSchemaValidatorObject<SV>;\n\n /**\n * The Data Transfer Object (DTO).\n * @type {Schema<this['schema'], SV>}\n */\n _dto: Schema<this['schema'], SV> = {} as unknown as Schema<\n this['schema'],\n SV\n >;\n\n /**\n * Creates an instance of BaseDtoMapper.\n *\n * @param {SV} schemaValidator - The schema provider.\n */\n constructor(schemaValidator: SV) {\n this.schemaValidator = schemaValidator as unknown as SchemaValidator;\n }\n\n /**\n * Validates and sets the Data Transfer Object (DTO).\n *\n * @param {this['_dto']} dto - The Data Transfer Object (DTO).\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n set dto(_dto: this['_dto']) {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n _dto\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n this._dto = _dto as unknown as Schema<this['schema'], SV>;\n }\n\n /**\n * Validates and gets the Data Transfer Object (DTO).\n *\n * @returns {this['_dto']} - The Data Transfer Object (DTO).\n */\n get dto(): this['_dto'] {\n return this._dto as unknown as this['_dto'];\n }\n\n /**\n * Gets the schema of a T.\n *\n * @template T - A type that extends BaseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @returns {T['schema']} - The schema of the T.\n */\n static schema<T extends BaseDtoMapper<SV>, SV extends AnySchemaValidator>(\n this: DtoMapperConstructor<T, SV>\n ): T['schema'] {\n return construct(this).schema;\n }\n}\n","import {\n AnySchemaValidator,\n prettyPrintParseErrors\n} from '@forklaunch/validator';\nimport { DtoMapperConstructor } from '../interfaces/mappers.interface';\nimport { BaseDtoMapper, construct } from './baseDtoMapper.model';\n\n/**\n * Abstract class representing a response entity mapper.\n *\n * @template Entity - A type that extends SqlBaseEntity.\n * @template SV - A type that extends AnySchemaValidator.\n * @extends {BaseDtoMapper<SV>}\n */\nexport abstract class ResponseDtoMapper<\n Entity,\n SV extends AnySchemaValidator\n> extends BaseDtoMapper<SV> {\n /**\n * The entity type.\n * @type {Entity}\n * @protected\n */\n _Entity!: Entity;\n\n /**\n * Populates entity mapper with DTO from an entity.\n *\n * @abstract\n * @param {Entity} entity - The entity to convert.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {this} - The instance of the ResponseDtoMapper.\n */\n abstract fromEntity(entity: Entity, ...additionalArgs: unknown[]): this;\n\n /**\n * Converts the underlying DTO to a JSON object.\n *\n * @returns {this['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n toDto(): this['_dto'] {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n this.dto\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return this.dto;\n }\n\n /**\n * Serializes an entity to a JSON object.\n *\n * @param {Entity} entity - The entity to serialize.\n * @returns {this['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n serializeEntityToDto(\n ...[entity, ...additionalArgs]: Parameters<this['fromEntity']>\n ): this['_dto'] {\n return this.fromEntity(entity, ...additionalArgs).toDto();\n }\n\n /**\n * Populates entity mapper with DTO from an entity.\n *\n * @template T - A type that extends ResponseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {T['_Entity']} entity - The entity to convert.\n * @returns {T} - An instance of the T.\n */\n static fromEntity<\n T extends ResponseDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator\n >(\n this: DtoMapperConstructor<T, SV>,\n schemaValidator: SV,\n ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>\n ): T {\n return construct(this, schemaValidator).fromEntity(\n entity,\n ...additionalArgs\n );\n }\n\n /**\n * Serializes an entity to a JSON object.\n *\n * @template T - A type that extends ResponseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {T['_Entity']} entity - The entity to serialize.\n * @returns {T['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n static serializeEntityToDto<\n T extends ResponseDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n DtoType extends T['_dto']\n >(\n this: DtoMapperConstructor<T, SV>,\n schemaValidator: SV,\n ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>\n ): DtoType {\n return construct(this, schemaValidator)\n .fromEntity(entity, ...additionalArgs)\n .toDto() as DtoType;\n }\n}\n","import { isNever } from '@forklaunch/common';\nimport { AnySchemaValidator } from '@forklaunch/validator';\nimport { SchemasByValidator } from './types/serviceSchemaResolver.types';\n\nexport function serviceSchemaResolver<\n Options extends Record<string, unknown>,\n TypeBoxSchemas,\n ZodSchemas\n>(\n TypeBoxSchemas: (options: Options) => TypeBoxSchemas,\n ZodSchemas: (options: Options) => ZodSchemas\n) {\n return <SchemaValidator extends AnySchemaValidator>(\n options: Options & { validator: SchemaValidator }\n ): SchemasByValidator<\n SchemaValidator,\n (options: Options) => TypeBoxSchemas,\n (options: Options) => ZodSchemas\n > => {\n switch (options.validator._Type) {\n case 'TypeBox':\n return TypeBoxSchemas(options) as SchemasByValidator<\n SchemaValidator,\n (options: Options) => TypeBoxSchemas,\n (options: Options) => ZodSchemas\n >;\n case 'Zod':\n return ZodSchemas(options) as SchemasByValidator<\n SchemaValidator,\n (options: Options) => TypeBoxSchemas,\n (options: Options) => ZodSchemas\n >;\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n case 'Mock':\n throw new Error('Mock schema validator not supported');\n default:\n isNever(options.validator._Type);\n throw new Error('Invalid schema validator');\n }\n };\n}\n","import { InstanceTypeRecord } from '@forklaunch/common';\nimport { AnySchemaValidator } from '@forklaunch/validator';\nimport { InternalDtoMapper } from './types/internalDtoMapper.types';\n\nexport function transformIntoInternalDtoMapper<\n SchemaValidator extends AnySchemaValidator,\n DtoMapper extends Record<\n string,\n new (schemaValidator: SchemaValidator) =>\n | {\n dto: unknown;\n _Entity: unknown;\n serializeEntityToDto: unknown;\n }\n | {\n dto: unknown;\n _Entity: unknown;\n deserializeDtoToEntity: unknown;\n }\n >,\n Entities extends Record<keyof DtoMapper, unknown>,\n Dto extends Record<keyof DtoMapper, unknown>\n>(\n mapperss: DtoMapper,\n schemaValidator: SchemaValidator\n): InternalDtoMapper<InstanceTypeRecord<DtoMapper>, Entities, Dto> {\n return Object.fromEntries(\n Object.entries(mapperss).map(([key, value]) => [\n key,\n new value(schemaValidator)\n ])\n ) as unknown as InternalDtoMapper<\n InstanceTypeRecord<DtoMapper>,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AA;AAAA,EAYA,OAAO,WAKL,oBACG,CAAC,QAAQ,GAAG,cAAc,GAC1B;AACH,WAAO,UAAU,MAAM,eAAe,EAAE;AAAA,MACtC;AAAA,MACA,GAAG;AAAA,IACL;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaA,OAAO,qBAML,oBACG,CAAC,QAAQ,GAAG,cAAc,GACpB;AACT,WAAO,UAAU,MAAM,eAAe,EACnC,WAAW,QAAQ,GAAG,cAAc,EACpC,MAAM;AAAA,EACX;AACF;;;ACjHA,oBAAwB;AAIjB,SAAS,sBAKd,gBACA,YACA;AACA,SAAO,CACL,YAKG;AACH,YAAQ,QAAQ,UAAU,OAAO;AAAA,MAC/B,KAAK;AACH,eAAO,eAAe,OAAO;AAAA,MAK/B,KAAK;AACH,eAAO,WAAW,OAAO;AAAA;AAAA;AAAA,MAO3B,KAAK;AACH,cAAM,IAAI,MAAM,qCAAqC;AAAA,MACvD;AACE,mCAAQ,QAAQ,UAAU,KAAK;AAC/B,cAAM,IAAI,MAAM,0BAA0B;AAAA,IAC9C;AAAA,EACF;AACF;;;ACrCO,SAAS,+BAmBd,UACA,iBACiE;AACjE,SAAO,OAAO;AAAA,IACZ,OAAO,QAAQ,QAAQ,EAAE,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM;AAAA,MAC7C;AAAA,MACA,IAAI,MAAM,eAAe;AAAA,IAC3B,CAAC;AAAA,EACH;AAKF;","names":["import_validator","import_validator"]}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../src/mappers/models/requestDtoMapper.model.ts","../../../src/mappers/models/baseDtoMapper.model.ts","../../../src/mappers/models/responseDtoMapper.model.ts","../../../src/mappers/serviceSchemaResolver.ts","../../../src/mappers/transformIntoInternalDtoMapper.ts"],"sourcesContent":["import {\n AnySchemaValidator,\n prettyPrintParseErrors\n} from '@forklaunch/validator';\nimport { DtoMapperConstructor } from '../interfaces/mappers.interface';\nimport { BaseDtoMapper, construct } from './baseDtoMapper.model';\n\n/**\n * Abstract class representing a request entity mapper.\n *\n * @template Entity - A type that extends BaseEntity.\n * @template SV - A type that extends AnySchemaValidator.\n * @extends {BaseDtoMapper<SV>}\n */\nexport abstract class RequestDtoMapper<\n Entity,\n SV extends AnySchemaValidator\n> extends BaseDtoMapper<SV> {\n /**\n * The entity type.\n * @type {Entity}\n * @protected\n */\n _Entity!: Entity;\n\n /**\n * Converts the underlying DTO to an entity.\n *\n * @abstract\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {Entity} - The entity.\n */\n abstract toEntity(...additionalArgs: unknown[]): Entity;\n\n /**\n * Populates the DTO with data from a JSON object.\n *\n * @param {this['_dto']} json - The JSON object.\n * @returns {this} - The instance of the RequestDtoMapper.\n */\n fromDto(json: this['_dto']): this {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n json\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n this.dto = json;\n return this;\n }\n\n /**\n * Deserializes a JSON object to an entity.\n *\n * @param {this['_dto']} json - The JSON object.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {Entity} - The entity.\n */\n deserializeDtoToEntity(\n json: this['_dto'],\n ...additionalArgs: Parameters<this['toEntity']>\n ): Entity {\n return this.fromDto(json).toEntity(...additionalArgs);\n }\n\n /**\n * Creates an instance of a RequestDtoMapper from a JSON object.\n *\n * @template T - A type that extends RequestDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @template JsonType - The type of the JSON object.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {JsonType} json - The JSON object.\n * @returns {T} - An instance of the T.\n */\n static fromDto<\n T extends RequestDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n JsonType extends T['_dto']\n >(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, json: JsonType): T {\n return construct(this, schemaValidator).fromDto(json);\n }\n\n /**\n * Deserializes a JSON object to an entity.\n *\n * @template T - A type that extends RequestDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @template JsonType - The type of the JSON object.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {JsonType} json - The JSON object.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {T['_Entity']} - The entity.\n */\n static deserializeDtoToEntity<\n T extends RequestDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n JsonType extends T['_dto']\n >(\n this: DtoMapperConstructor<T, SV>,\n schemaValidator: SV,\n json: JsonType,\n ...additionalArgs: Parameters<T['toEntity']>\n ): T['_Entity'] {\n return construct(this, schemaValidator)\n .fromDto(json)\n .toEntity(...additionalArgs);\n }\n}\n","import {\n AnySchemaValidator,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { DtoMapperConstructor } from '../interfaces/mappers.interface';\nimport { DtoMapperSchemaValidatorObject } from '../types/mappers.types';\n\n/**\n * Constructs an instance of a T.\n *\n * @template T - A type that extends BaseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} self - The constructor of the T.\n * @param {SV} [schemaValidator] - The optional schema validator.\n * @returns {T} - An instance of the T.\n */\nexport function construct<T, SV extends AnySchemaValidator>(\n self: DtoMapperConstructor<T, SV>,\n schemaValidator?: SV\n): T {\n return new self(schemaValidator || ({} as SV));\n}\n\n/**\n * Abstract class representing a base entity mapper.\n *\n * @template SV - A type that extends AnySchemaValidator.\n */\nexport abstract class BaseDtoMapper<SV extends AnySchemaValidator> {\n /**\n * The schema validator exact type.\n * @type {SV}\n * @protected\n */\n _SV!: SV;\n\n /**\n * The schema validator as a general type.\n * @type {SchemaValidator}\n * @protected\n */\n protected schemaValidator: SchemaValidator;\n\n /**\n * The schema definition.\n * @type {DtoMapperSchemaValidatorObject<SV>}\n * @abstract\n */\n abstract schema: DtoMapperSchemaValidatorObject<SV>;\n\n /**\n * The Data Transfer Object (DTO).\n * @type {Schema<this['schema'], SV>}\n */\n _dto: Schema<this['schema'], SV> = {} as unknown as Schema<\n this['schema'],\n SV\n >;\n\n /**\n * Creates an instance of BaseDtoMapper.\n *\n * @param {SV} schemaValidator - The schema provider.\n */\n constructor(schemaValidator: SV) {\n this.schemaValidator = schemaValidator as unknown as SchemaValidator;\n }\n\n /**\n * Validates and sets the Data Transfer Object (DTO).\n *\n * @param {this['_dto']} dto - The Data Transfer Object (DTO).\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n set dto(_dto: this['_dto']) {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n _dto\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n this._dto = _dto as unknown as Schema<this['schema'], SV>;\n }\n\n /**\n * Validates and gets the Data Transfer Object (DTO).\n *\n * @returns {this['_dto']} - The Data Transfer Object (DTO).\n */\n get dto(): this['_dto'] {\n return this._dto as unknown as this['_dto'];\n }\n\n /**\n * Gets the schema of a T.\n *\n * @template T - A type that extends BaseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @returns {T['schema']} - The schema of the T.\n */\n static schema<T extends BaseDtoMapper<SV>, SV extends AnySchemaValidator>(\n this: DtoMapperConstructor<T, SV>\n ): T['schema'] {\n return construct(this).schema;\n }\n}\n","import {\n AnySchemaValidator,\n prettyPrintParseErrors\n} from '@forklaunch/validator';\nimport { DtoMapperConstructor } from '../interfaces/mappers.interface';\nimport { BaseDtoMapper, construct } from './baseDtoMapper.model';\n\n/**\n * Abstract class representing a response entity mapper.\n *\n * @template Entity - A type that extends BaseEntity.\n * @template SV - A type that extends AnySchemaValidator.\n * @extends {BaseDtoMapper<SV>}\n */\nexport abstract class ResponseDtoMapper<\n Entity,\n SV extends AnySchemaValidator\n> extends BaseDtoMapper<SV> {\n /**\n * The entity type.\n * @type {Entity}\n * @protected\n */\n _Entity!: Entity;\n\n /**\n * Populates entity mapper with DTO from an entity.\n *\n * @abstract\n * @param {Entity} entity - The entity to convert.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {this} - The instance of the ResponseDtoMapper.\n */\n abstract fromEntity(entity: Entity, ...additionalArgs: unknown[]): this;\n\n /**\n * Converts the underlying DTO to a JSON object.\n *\n * @returns {this['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n toDto(): this['_dto'] {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n this.dto\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return this.dto;\n }\n\n /**\n * Serializes an entity to a JSON object.\n *\n * @param {Entity} entity - The entity to serialize.\n * @returns {this['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n serializeEntityToDto(\n ...[entity, ...additionalArgs]: Parameters<this['fromEntity']>\n ): this['_dto'] {\n return this.fromEntity(entity, ...additionalArgs).toDto();\n }\n\n /**\n * Populates entity mapper with DTO from an entity.\n *\n * @template T - A type that extends ResponseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {T['_Entity']} entity - The entity to convert.\n * @returns {T} - An instance of the T.\n */\n static fromEntity<\n T extends ResponseDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator\n >(\n this: DtoMapperConstructor<T, SV>,\n schemaValidator: SV,\n ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>\n ): T {\n return construct(this, schemaValidator).fromEntity(\n entity,\n ...additionalArgs\n );\n }\n\n /**\n * Serializes an entity to a JSON object.\n *\n * @template T - A type that extends ResponseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {T['_Entity']} entity - The entity to serialize.\n * @returns {T['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n static serializeEntityToDto<\n T extends ResponseDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n DtoType extends T['_dto']\n >(\n this: DtoMapperConstructor<T, SV>,\n schemaValidator: SV,\n ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>\n ): DtoType {\n return construct(this, schemaValidator)\n .fromEntity(entity, ...additionalArgs)\n .toDto() as DtoType;\n }\n}\n","import { isNever } from '@forklaunch/common';\nimport { AnySchemaValidator } from '@forklaunch/validator';\nimport { SchemasByValidator } from './types/serviceSchemaResolver.types';\n\nexport function serviceSchemaResolver<\n Options extends Record<string, unknown>,\n TypeBoxSchemas,\n ZodSchemas\n>(\n TypeBoxSchemas: (options: Options) => TypeBoxSchemas,\n ZodSchemas: (options: Options) => ZodSchemas\n) {\n return <SchemaValidator extends AnySchemaValidator>(\n options: Options & { validator: SchemaValidator }\n ): SchemasByValidator<\n SchemaValidator,\n (options: Options) => TypeBoxSchemas,\n (options: Options) => ZodSchemas\n > => {\n switch (options.validator._Type) {\n case 'TypeBox':\n return TypeBoxSchemas(options) as SchemasByValidator<\n SchemaValidator,\n (options: Options) => TypeBoxSchemas,\n (options: Options) => ZodSchemas\n >;\n case 'Zod':\n return ZodSchemas(options) as SchemasByValidator<\n SchemaValidator,\n (options: Options) => TypeBoxSchemas,\n (options: Options) => ZodSchemas\n >;\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n case 'Mock':\n throw new Error('Mock schema validator not supported');\n default:\n isNever(options.validator._Type);\n throw new Error('Invalid schema validator');\n }\n };\n}\n","import { InstanceTypeRecord } from '@forklaunch/common';\nimport { AnySchemaValidator } from '@forklaunch/validator';\nimport { InternalDtoMapper } from './types/internalDtoMapper.types';\n\nexport function transformIntoInternalDtoMapper<\n SchemaValidator extends AnySchemaValidator,\n DtoMapper extends Record<\n string,\n new (schemaValidator: SchemaValidator) =>\n | {\n dto: unknown;\n _Entity: unknown;\n serializeEntityToDto: unknown;\n }\n | {\n dto: unknown;\n _Entity: unknown;\n deserializeDtoToEntity: unknown;\n }\n >,\n Entities extends Record<keyof DtoMapper, unknown>,\n Dto extends Record<keyof DtoMapper, unknown>\n>(\n mapperss: DtoMapper,\n schemaValidator: SchemaValidator\n): InternalDtoMapper<InstanceTypeRecord<DtoMapper>, Entities, Dto> {\n return Object.fromEntries(\n Object.entries(mapperss).map(([key, value]) => [\n key,\n new value(schemaValidator)\n ])\n ) as unknown as InternalDtoMapper<\n InstanceTypeRecord<DtoMapper>,\n Entities,\n Dto\n >;\n}\n"],"mappings":";AAAA;AAAA,EAEE,0BAAAA;AAAA,OACK;;;ACHP;AAAA,EAEE;AAAA,OAGK;AAaA,SAAS,UACd,MACA,iBACG;AACH,SAAO,IAAI,KAAK,mBAAoB,CAAC,CAAQ;AAC/C;AAOO,IAAe,gBAAf,MAA4D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMjE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOU;AAAA;AAAA;AAAA;AAAA;AAAA,EAaV,OAAmC,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUpC,YAAY,iBAAqB;AAC/B,SAAK,kBAAkB;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAI,IAAI,MAAoB;AAC1B,UAAM,eAAe,KAAK,gBAAgB;AAAA,MACxC,KAAK,gBAAgB,SAAS,KAAK,MAAM;AAAA,MACzC;AAAA,IACF;AACA,QAAI,CAAC,aAAa,IAAI;AACpB,YAAM,IAAI,MAAM,uBAAuB,aAAa,QAAQ,KAAK,CAAC;AAAA,IACpE;AACA,SAAK,OAAO;AAAA,EACd;AAAA;AAAA;AAAA;A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AAO,OAAO;AAAA,IACZ,OAAO,QAAQ,QAAQ,EAAE,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM;AAAA,MAC7C;AAAA,MACA,IAAI,MAAM,eAAe;AAAA,IAC3B,CAAC;AAAA,EACH;AAKF;","names":["prettyPrintParseErrors","prettyPrintParseErrors","prettyPrintParseErrors","prettyPrintParseErrors"]}
|
1
|
+
{"version":3,"sources":["../../../src/mappers/models/requestDtoMapper.model.ts","../../../src/mappers/models/baseDtoMapper.model.ts","../../../src/mappers/models/responseDtoMapper.model.ts","../../../src/mappers/serviceSchemaResolver.ts","../../../src/mappers/transformIntoInternalDtoMapper.ts"],"sourcesContent":["import {\n AnySchemaValidator,\n prettyPrintParseErrors\n} from '@forklaunch/validator';\nimport { DtoMapperConstructor } from '../interfaces/mappers.interface';\nimport { BaseDtoMapper, construct } from './baseDtoMapper.model';\n\n/**\n * Abstract class representing a request entity mapper.\n *\n * @template Entity - A type that extends SqlBaseEntity.\n * @template SV - A type that extends AnySchemaValidator.\n * @extends {BaseDtoMapper<SV>}\n */\nexport abstract class RequestDtoMapper<\n Entity,\n SV extends AnySchemaValidator\n> extends BaseDtoMapper<SV> {\n /**\n * The entity type.\n * @type {Entity}\n * @protected\n */\n _Entity!: Entity;\n\n /**\n * Converts the underlying DTO to an entity.\n *\n * @abstract\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {Entity} - The entity.\n */\n abstract toEntity(...additionalArgs: unknown[]): Entity;\n\n /**\n * Populates the DTO with data from a JSON object.\n *\n * @param {this['_dto']} json - The JSON object.\n * @returns {this} - The instance of the RequestDtoMapper.\n */\n fromDto(json: this['_dto']): this {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n json\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n this.dto = json;\n return this;\n }\n\n /**\n * Deserializes a JSON object to an entity.\n *\n * @param {this['_dto']} json - The JSON object.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {Entity} - The entity.\n */\n deserializeDtoToEntity(\n json: this['_dto'],\n ...additionalArgs: Parameters<this['toEntity']>\n ): Entity {\n return this.fromDto(json).toEntity(...additionalArgs);\n }\n\n /**\n * Creates an instance of a RequestDtoMapper from a JSON object.\n *\n * @template T - A type that extends RequestDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @template JsonType - The type of the JSON object.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {JsonType} json - The JSON object.\n * @returns {T} - An instance of the T.\n */\n static fromDto<\n T extends RequestDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n JsonType extends T['_dto']\n >(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, json: JsonType): T {\n return construct(this, schemaValidator).fromDto(json);\n }\n\n /**\n * Deserializes a JSON object to an entity.\n *\n * @template T - A type that extends RequestDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @template JsonType - The type of the JSON object.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {JsonType} json - The JSON object.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {T['_Entity']} - The entity.\n */\n static deserializeDtoToEntity<\n T extends RequestDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n JsonType extends T['_dto']\n >(\n this: DtoMapperConstructor<T, SV>,\n schemaValidator: SV,\n json: JsonType,\n ...additionalArgs: Parameters<T['toEntity']>\n ): T['_Entity'] {\n return construct(this, schemaValidator)\n .fromDto(json)\n .toEntity(...additionalArgs);\n }\n}\n","import {\n AnySchemaValidator,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { DtoMapperConstructor } from '../interfaces/mappers.interface';\nimport { DtoMapperSchemaValidatorObject } from '../types/mappers.types';\n\n/**\n * Constructs an instance of a T.\n *\n * @template T - A type that extends BaseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} self - The constructor of the T.\n * @param {SV} [schemaValidator] - The optional schema validator.\n * @returns {T} - An instance of the T.\n */\nexport function construct<T, SV extends AnySchemaValidator>(\n self: DtoMapperConstructor<T, SV>,\n schemaValidator?: SV\n): T {\n return new self(schemaValidator || ({} as SV));\n}\n\n/**\n * Abstract class representing a base entity mapper.\n *\n * @template SV - A type that extends AnySchemaValidator.\n */\nexport abstract class BaseDtoMapper<SV extends AnySchemaValidator> {\n /**\n * The schema validator exact type.\n * @type {SV}\n * @protected\n */\n _SV!: SV;\n\n /**\n * The schema validator as a general type.\n * @type {SchemaValidator}\n * @protected\n */\n protected schemaValidator: SchemaValidator;\n\n /**\n * The schema definition.\n * @type {DtoMapperSchemaValidatorObject<SV>}\n * @abstract\n */\n abstract schema: DtoMapperSchemaValidatorObject<SV>;\n\n /**\n * The Data Transfer Object (DTO).\n * @type {Schema<this['schema'], SV>}\n */\n _dto: Schema<this['schema'], SV> = {} as unknown as Schema<\n this['schema'],\n SV\n >;\n\n /**\n * Creates an instance of BaseDtoMapper.\n *\n * @param {SV} schemaValidator - The schema provider.\n */\n constructor(schemaValidator: SV) {\n this.schemaValidator = schemaValidator as unknown as SchemaValidator;\n }\n\n /**\n * Validates and sets the Data Transfer Object (DTO).\n *\n * @param {this['_dto']} dto - The Data Transfer Object (DTO).\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n set dto(_dto: this['_dto']) {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n _dto\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n this._dto = _dto as unknown as Schema<this['schema'], SV>;\n }\n\n /**\n * Validates and gets the Data Transfer Object (DTO).\n *\n * @returns {this['_dto']} - The Data Transfer Object (DTO).\n */\n get dto(): this['_dto'] {\n return this._dto as unknown as this['_dto'];\n }\n\n /**\n * Gets the schema of a T.\n *\n * @template T - A type that extends BaseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @returns {T['schema']} - The schema of the T.\n */\n static schema<T extends BaseDtoMapper<SV>, SV extends AnySchemaValidator>(\n this: DtoMapperConstructor<T, SV>\n ): T['schema'] {\n return construct(this).schema;\n }\n}\n","import {\n AnySchemaValidator,\n prettyPrintParseErrors\n} from '@forklaunch/validator';\nimport { DtoMapperConstructor } from '../interfaces/mappers.interface';\nimport { BaseDtoMapper, construct } from './baseDtoMapper.model';\n\n/**\n * Abstract class representing a response entity mapper.\n *\n * @template Entity - A type that extends SqlBaseEntity.\n * @template SV - A type that extends AnySchemaValidator.\n * @extends {BaseDtoMapper<SV>}\n */\nexport abstract class ResponseDtoMapper<\n Entity,\n SV extends AnySchemaValidator\n> extends BaseDtoMapper<SV> {\n /**\n * The entity type.\n * @type {Entity}\n * @protected\n */\n _Entity!: Entity;\n\n /**\n * Populates entity mapper with DTO from an entity.\n *\n * @abstract\n * @param {Entity} entity - The entity to convert.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {this} - The instance of the ResponseDtoMapper.\n */\n abstract fromEntity(entity: Entity, ...additionalArgs: unknown[]): this;\n\n /**\n * Converts the underlying DTO to a JSON object.\n *\n * @returns {this['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n toDto(): this['_dto'] {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n this.dto\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return this.dto;\n }\n\n /**\n * Serializes an entity to a JSON object.\n *\n * @param {Entity} entity - The entity to serialize.\n * @returns {this['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n serializeEntityToDto(\n ...[entity, ...additionalArgs]: Parameters<this['fromEntity']>\n ): this['_dto'] {\n return this.fromEntity(entity, ...additionalArgs).toDto();\n }\n\n /**\n * Populates entity mapper with DTO from an entity.\n *\n * @template T - A type that extends ResponseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {T['_Entity']} entity - The entity to convert.\n * @returns {T} - An instance of the T.\n */\n static fromEntity<\n T extends ResponseDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator\n >(\n this: DtoMapperConstructor<T, SV>,\n schemaValidator: SV,\n ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>\n ): T {\n return construct(this, schemaValidator).fromEntity(\n entity,\n ...additionalArgs\n );\n }\n\n /**\n * Serializes an entity to a JSON object.\n *\n * @template T - A type that extends ResponseDtoMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {DtoMapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {T['_Entity']} entity - The entity to serialize.\n * @returns {T['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n static serializeEntityToDto<\n T extends ResponseDtoMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n DtoType extends T['_dto']\n >(\n this: DtoMapperConstructor<T, SV>,\n schemaValidator: SV,\n ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>\n ): DtoType {\n return construct(this, schemaValidator)\n .fromEntity(entity, ...additionalArgs)\n .toDto() as DtoType;\n }\n}\n","import { isNever } from '@forklaunch/common';\nimport { AnySchemaValidator } from '@forklaunch/validator';\nimport { SchemasByValidator } from './types/serviceSchemaResolver.types';\n\nexport function serviceSchemaResolver<\n Options extends Record<string, unknown>,\n TypeBoxSchemas,\n ZodSchemas\n>(\n TypeBoxSchemas: (options: Options) => TypeBoxSchemas,\n ZodSchemas: (options: Options) => ZodSchemas\n) {\n return <SchemaValidator extends AnySchemaValidator>(\n options: Options & { validator: SchemaValidator }\n ): SchemasByValidator<\n SchemaValidator,\n (options: Options) => TypeBoxSchemas,\n (options: Options) => ZodSchemas\n > => {\n switch (options.validator._Type) {\n case 'TypeBox':\n return TypeBoxSchemas(options) as SchemasByValidator<\n SchemaValidator,\n (options: Options) => TypeBoxSchemas,\n (options: Options) => ZodSchemas\n >;\n case 'Zod':\n return ZodSchemas(options) as SchemasByValidator<\n SchemaValidator,\n (options: Options) => TypeBoxSchemas,\n (options: Options) => ZodSchemas\n >;\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n case 'Mock':\n throw new Error('Mock schema validator not supported');\n default:\n isNever(options.validator._Type);\n throw new Error('Invalid schema validator');\n }\n };\n}\n","import { InstanceTypeRecord } from '@forklaunch/common';\nimport { AnySchemaValidator } from '@forklaunch/validator';\nimport { InternalDtoMapper } from './types/internalDtoMapper.types';\n\nexport function transformIntoInternalDtoMapper<\n SchemaValidator extends AnySchemaValidator,\n DtoMapper extends Record<\n string,\n new (schemaValidator: SchemaValidator) =>\n | {\n dto: unknown;\n _Entity: unknown;\n serializeEntityToDto: unknown;\n }\n | {\n dto: unknown;\n _Entity: unknown;\n deserializeDtoToEntity: unknown;\n }\n >,\n Entities extends Record<keyof DtoMapper, unknown>,\n Dto extends Record<keyof DtoMapper, unknown>\n>(\n mapperss: DtoMapper,\n schemaValidator: SchemaValidator\n): InternalDtoMapper<InstanceTypeRecord<DtoMapper>, Entities, Dto> {\n return Object.fromEntries(\n Object.entries(mapperss).map(([key, value]) => [\n key,\n new value(schemaValidator)\n ])\n ) as unknown as InternalDtoMapper<\n InstanceTypeRecord<DtoMapper>,\n Entities,\n Dto\n >;\n}\n"],"mappings":";AAAA;AAAA,EAEE,0BAAAA;AAAA,OACK;;;ACHP;AAAA,EAEE;AAAA,OAGK;AAaA,SAAS,UACd,MACA,iBACG;AACH,SAAO,IAAI,KAAK,mBAAoB,CAAC,CAAQ;AAC/C;AAOO,IAAe,gBAAf,MAA4D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMj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c,GACpB;AACT,WAAO,UAAU,MAAM,eAAe,EACnC,WAAW,QAAQ,GAAG,cAAc,EACpC,MAAM;AAAA,EACX;AACF;;;ACjHA,SAAS,eAAe;AAIjB,SAAS,sBAKd,gBACA,YACA;AACA,SAAO,CACL,YAKG;AACH,YAAQ,QAAQ,UAAU,OAAO;AAAA,MAC/B,KAAK;AACH,eAAO,eAAe,OAAO;AAAA,MAK/B,KAAK;AACH,eAAO,WAAW,OAAO;AAAA;AAAA;AAAA,MAO3B,KAAK;AACH,cAAM,IAAI,MAAM,qCAAqC;AAAA,MACvD;AACE,gBAAQ,QAAQ,UAAU,KAAK;AAC/B,cAAM,IAAI,MAAM,0BAA0B;AAAA,IAC9C;AAAA,EACF;AACF;;;ACrCO,SAAS,+BAmBd,UACA,iBACiE;AACjE,SAAO,OAAO;AAAA,IACZ,OAAO,QAAQ,QAAQ,EAAE,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM;AAAA,MAC7C;AAAA,MACA,IAAI,MAAM,eAAe;AAAA,IAC3B,CAAC;AAAA,EACH;AAKF;","names":["prettyPrintParseErrors","prettyPrintParseErrors","prettyPrintParseErrors","prettyPrintParseErrors"]}
|
@@ -0,0 +1,120 @@
|
|
1
|
+
import { Collection, BaseEntity as BaseEntity$1, Constructor, EntityDTO, FromEntityType } from '@mikro-orm/core';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Type representing a marked collection of items.
|
5
|
+
* A marked collection is a wrapper around an array that indicates it should be treated as a collection.
|
6
|
+
*
|
7
|
+
* @template T - The type of items in the collection
|
8
|
+
* @property {true} _collection - Marker indicating this is a collection
|
9
|
+
* @property {T[]} items - The array of items in the collection
|
10
|
+
*/
|
11
|
+
type MarkedCollection<T> = {
|
12
|
+
_collection: true;
|
13
|
+
items: T[];
|
14
|
+
};
|
15
|
+
|
16
|
+
/**
|
17
|
+
* Type representing the shape of data used to create an entity.
|
18
|
+
* Converts MikroORM collections to marked collections and omits entity properties.
|
19
|
+
*
|
20
|
+
* @template Entity - The base entity type
|
21
|
+
* @template T - The type being created
|
22
|
+
*/
|
23
|
+
type CreateShape<Entity, T> = {
|
24
|
+
[K in keyof Omit<T, keyof Entity>]: T[K] extends Collection<infer U> ? MarkedCollection<U> : T[K];
|
25
|
+
};
|
26
|
+
/**
|
27
|
+
* Type representing the shape of data used to update an entity.
|
28
|
+
* Similar to CreateShape but makes all properties optional and includes an id field.
|
29
|
+
*
|
30
|
+
* @template Entity - The base entity type
|
31
|
+
* @template T - The type being updated
|
32
|
+
*/
|
33
|
+
type UpdateShape<Entity, T> = Partial<CreateShape<Entity, T> & {
|
34
|
+
id: string;
|
35
|
+
}>;
|
36
|
+
|
37
|
+
/**
|
38
|
+
* Type representing a base entity with common fields.
|
39
|
+
* Extends BaseEntity with optional id, _id, createdAt, and updatedAt fields.
|
40
|
+
*/
|
41
|
+
type BaseEntityWithId = BaseEntity & {
|
42
|
+
id?: unknown;
|
43
|
+
_id?: unknown;
|
44
|
+
createdAt?: unknown;
|
45
|
+
updatedAt?: unknown;
|
46
|
+
};
|
47
|
+
/**
|
48
|
+
* Abstract base class for all entities in the system.
|
49
|
+
* Extends MikroORM's BaseEntity and provides common CRUD operations.
|
50
|
+
*/
|
51
|
+
declare abstract class BaseEntity extends BaseEntity$1 {
|
52
|
+
/**
|
53
|
+
* Static factory method to create a new entity instance.
|
54
|
+
*
|
55
|
+
* @template Entity - The type of entity being created
|
56
|
+
* @param {Constructor<Entity>} this - The entity constructor
|
57
|
+
* @param {...Parameters<Entity['create']>} args - Arguments for entity creation
|
58
|
+
* @returns {Entity} A new entity instance
|
59
|
+
*/
|
60
|
+
static create<Entity extends BaseEntityWithId>(this: Constructor<Entity>, ...args: Parameters<Entity['create']>): Entity;
|
61
|
+
/**
|
62
|
+
* Static method to update an entity instance.
|
63
|
+
*
|
64
|
+
* @template Entity - The type of entity being updated
|
65
|
+
* @param {Constructor<Entity>} this - The entity constructor
|
66
|
+
* @param {...Parameters<Entity['update']>} args - Arguments for entity update
|
67
|
+
* @returns {Entity} The updated entity instance
|
68
|
+
*/
|
69
|
+
static update<Entity extends BaseEntityWithId>(this: Constructor<Entity>, ...args: Parameters<Entity['update']>): Entity;
|
70
|
+
/**
|
71
|
+
* Static method to map data to an entity instance.
|
72
|
+
*
|
73
|
+
* @template Entity - The type of entity being mapped
|
74
|
+
* @param {Constructor<Entity>} this - The entity constructor
|
75
|
+
* @param {Partial<EntityDTO<FromEntityType<Entity>>>} data - The data to map
|
76
|
+
* @returns {Entity} A new entity instance with mapped data
|
77
|
+
*/
|
78
|
+
static map<Entity extends BaseEntity>(this: Constructor<Entity>, data: Partial<EntityDTO<FromEntityType<Entity>>>): Entity;
|
79
|
+
/**
|
80
|
+
* Creates a new entity instance with the provided data.
|
81
|
+
*
|
82
|
+
* @param {CreateShape<BaseEntityWithId, this>} data - The data to create the entity with
|
83
|
+
* @returns {this} The created entity instance
|
84
|
+
*/
|
85
|
+
create(data: CreateShape<BaseEntityWithId, this>): this;
|
86
|
+
/**
|
87
|
+
* Updates the entity instance with the provided data.
|
88
|
+
*
|
89
|
+
* @param {UpdateShape<BaseEntityWithId, this>} data - The data to update the entity with
|
90
|
+
* @returns {this} The updated entity instance
|
91
|
+
*/
|
92
|
+
update(data: UpdateShape<BaseEntityWithId, this>): this;
|
93
|
+
/**
|
94
|
+
* Reads the entity data as a plain object.
|
95
|
+
*
|
96
|
+
* @returns {EntityDTO<this> | this} The entity data as a plain object
|
97
|
+
*/
|
98
|
+
read(): EntityDTO<this> | this;
|
99
|
+
/**
|
100
|
+
* Maps data to the entity instance.
|
101
|
+
*
|
102
|
+
* @param {Partial<EntityDTO<FromEntityType<this>>>} data - The data to map
|
103
|
+
* @returns {this} The entity instance with mapped data
|
104
|
+
*/
|
105
|
+
map(data: Partial<EntityDTO<FromEntityType<this>>>): this;
|
106
|
+
}
|
107
|
+
|
108
|
+
/**
|
109
|
+
* Creates a marked collection from an array of items.
|
110
|
+
* A marked collection is a wrapper around an array that indicates it should be treated as a collection.
|
111
|
+
*
|
112
|
+
* @template T - The type of items in the collection
|
113
|
+
* @param {T[]} items - The array of items to wrap in a collection
|
114
|
+
* @returns {MarkedCollection<T>} A marked collection containing the provided items
|
115
|
+
* @example
|
116
|
+
* const users = collection([{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]);
|
117
|
+
*/
|
118
|
+
declare function collection<T>(items: T[]): MarkedCollection<T>;
|
119
|
+
|
120
|
+
export { BaseEntity, collection };
|
@@ -0,0 +1,120 @@
|
|
1
|
+
import { Collection, BaseEntity as BaseEntity$1, Constructor, EntityDTO, FromEntityType } from '@mikro-orm/core';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Type representing a marked collection of items.
|
5
|
+
* A marked collection is a wrapper around an array that indicates it should be treated as a collection.
|
6
|
+
*
|
7
|
+
* @template T - The type of items in the collection
|
8
|
+
* @property {true} _collection - Marker indicating this is a collection
|
9
|
+
* @property {T[]} items - The array of items in the collection
|
10
|
+
*/
|
11
|
+
type MarkedCollection<T> = {
|
12
|
+
_collection: true;
|
13
|
+
items: T[];
|
14
|
+
};
|
15
|
+
|
16
|
+
/**
|
17
|
+
* Type representing the shape of data used to create an entity.
|
18
|
+
* Converts MikroORM collections to marked collections and omits entity properties.
|
19
|
+
*
|
20
|
+
* @template Entity - The base entity type
|
21
|
+
* @template T - The type being created
|
22
|
+
*/
|
23
|
+
type CreateShape<Entity, T> = {
|
24
|
+
[K in keyof Omit<T, keyof Entity>]: T[K] extends Collection<infer U> ? MarkedCollection<U> : T[K];
|
25
|
+
};
|
26
|
+
/**
|
27
|
+
* Type representing the shape of data used to update an entity.
|
28
|
+
* Similar to CreateShape but makes all properties optional and includes an id field.
|
29
|
+
*
|
30
|
+
* @template Entity - The base entity type
|
31
|
+
* @template T - The type being updated
|
32
|
+
*/
|
33
|
+
type UpdateShape<Entity, T> = Partial<CreateShape<Entity, T> & {
|
34
|
+
id: string;
|
35
|
+
}>;
|
36
|
+
|
37
|
+
/**
|
38
|
+
* Type representing a base entity with common fields.
|
39
|
+
* Extends BaseEntity with optional id, _id, createdAt, and updatedAt fields.
|
40
|
+
*/
|
41
|
+
type BaseEntityWithId = BaseEntity & {
|
42
|
+
id?: unknown;
|
43
|
+
_id?: unknown;
|
44
|
+
createdAt?: unknown;
|
45
|
+
updatedAt?: unknown;
|
46
|
+
};
|
47
|
+
/**
|
48
|
+
* Abstract base class for all entities in the system.
|
49
|
+
* Extends MikroORM's BaseEntity and provides common CRUD operations.
|
50
|
+
*/
|
51
|
+
declare abstract class BaseEntity extends BaseEntity$1 {
|
52
|
+
/**
|
53
|
+
* Static factory method to create a new entity instance.
|
54
|
+
*
|
55
|
+
* @template Entity - The type of entity being created
|
56
|
+
* @param {Constructor<Entity>} this - The entity constructor
|
57
|
+
* @param {...Parameters<Entity['create']>} args - Arguments for entity creation
|
58
|
+
* @returns {Entity} A new entity instance
|
59
|
+
*/
|
60
|
+
static create<Entity extends BaseEntityWithId>(this: Constructor<Entity>, ...args: Parameters<Entity['create']>): Entity;
|
61
|
+
/**
|
62
|
+
* Static method to update an entity instance.
|
63
|
+
*
|
64
|
+
* @template Entity - The type of entity being updated
|
65
|
+
* @param {Constructor<Entity>} this - The entity constructor
|
66
|
+
* @param {...Parameters<Entity['update']>} args - Arguments for entity update
|
67
|
+
* @returns {Entity} The updated entity instance
|
68
|
+
*/
|
69
|
+
static update<Entity extends BaseEntityWithId>(this: Constructor<Entity>, ...args: Parameters<Entity['update']>): Entity;
|
70
|
+
/**
|
71
|
+
* Static method to map data to an entity instance.
|
72
|
+
*
|
73
|
+
* @template Entity - The type of entity being mapped
|
74
|
+
* @param {Constructor<Entity>} this - The entity constructor
|
75
|
+
* @param {Partial<EntityDTO<FromEntityType<Entity>>>} data - The data to map
|
76
|
+
* @returns {Entity} A new entity instance with mapped data
|
77
|
+
*/
|
78
|
+
static map<Entity extends BaseEntity>(this: Constructor<Entity>, data: Partial<EntityDTO<FromEntityType<Entity>>>): Entity;
|
79
|
+
/**
|
80
|
+
* Creates a new entity instance with the provided data.
|
81
|
+
*
|
82
|
+
* @param {CreateShape<BaseEntityWithId, this>} data - The data to create the entity with
|
83
|
+
* @returns {this} The created entity instance
|
84
|
+
*/
|
85
|
+
create(data: CreateShape<BaseEntityWithId, this>): this;
|
86
|
+
/**
|
87
|
+
* Updates the entity instance with the provided data.
|
88
|
+
*
|
89
|
+
* @param {UpdateShape<BaseEntityWithId, this>} data - The data to update the entity with
|
90
|
+
* @returns {this} The updated entity instance
|
91
|
+
*/
|
92
|
+
update(data: UpdateShape<BaseEntityWithId, this>): this;
|
93
|
+
/**
|
94
|
+
* Reads the entity data as a plain object.
|
95
|
+
*
|
96
|
+
* @returns {EntityDTO<this> | this} The entity data as a plain object
|
97
|
+
*/
|
98
|
+
read(): EntityDTO<this> | this;
|
99
|
+
/**
|
100
|
+
* Maps data to the entity instance.
|
101
|
+
*
|
102
|
+
* @param {Partial<EntityDTO<FromEntityType<this>>>} data - The data to map
|
103
|
+
* @returns {this} The entity instance with mapped data
|
104
|
+
*/
|
105
|
+
map(data: Partial<EntityDTO<FromEntityType<this>>>): this;
|
106
|
+
}
|
107
|
+
|
108
|
+
/**
|
109
|
+
* Creates a marked collection from an array of items.
|
110
|
+
* A marked collection is a wrapper around an array that indicates it should be treated as a collection.
|
111
|
+
*
|
112
|
+
* @template T - The type of items in the collection
|
113
|
+
* @param {T[]} items - The array of items to wrap in a collection
|
114
|
+
* @returns {MarkedCollection<T>} A marked collection containing the provided items
|
115
|
+
* @example
|
116
|
+
* const users = collection([{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]);
|
117
|
+
*/
|
118
|
+
declare function collection<T>(items: T[]): MarkedCollection<T>;
|
119
|
+
|
120
|
+
export { BaseEntity, collection };
|
@@ -0,0 +1,152 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
|
20
|
+
// src/persistence/index.ts
|
21
|
+
var persistence_exports = {};
|
22
|
+
__export(persistence_exports, {
|
23
|
+
BaseEntity: () => BaseEntity2,
|
24
|
+
collection: () => collection
|
25
|
+
});
|
26
|
+
module.exports = __toCommonJS(persistence_exports);
|
27
|
+
|
28
|
+
// src/persistence/base.entity.ts
|
29
|
+
var import_common = require("@forklaunch/common");
|
30
|
+
var import_core2 = require("@mikro-orm/core");
|
31
|
+
|
32
|
+
// src/persistence/transformRawDto.ts
|
33
|
+
var import_core = require("@mikro-orm/core");
|
34
|
+
|
35
|
+
// src/persistence/guards/isMarkedCollection.ts
|
36
|
+
function isMarkedCollection(value) {
|
37
|
+
return typeof value === "object" && value !== null && "_collection" in value && typeof value._collection === "boolean" && value._collection;
|
38
|
+
}
|
39
|
+
|
40
|
+
// src/persistence/transformRawDto.ts
|
41
|
+
function transformRawDto(data, entity) {
|
42
|
+
const transformedObject = {};
|
43
|
+
for (const [key, value] of Object.entries(data)) {
|
44
|
+
if (isMarkedCollection(value)) {
|
45
|
+
transformedObject[key] = new import_core.Collection(entity, value.items);
|
46
|
+
} else {
|
47
|
+
transformedObject[key] = value;
|
48
|
+
}
|
49
|
+
}
|
50
|
+
return transformedObject;
|
51
|
+
}
|
52
|
+
|
53
|
+
// src/persistence/base.entity.ts
|
54
|
+
var BaseEntity2 = class extends import_core2.BaseEntity {
|
55
|
+
/**
|
56
|
+
* Static factory method to create a new entity instance.
|
57
|
+
*
|
58
|
+
* @template Entity - The type of entity being created
|
59
|
+
* @param {Constructor<Entity>} this - The entity constructor
|
60
|
+
* @param {...Parameters<Entity['create']>} args - Arguments for entity creation
|
61
|
+
* @returns {Entity} A new entity instance
|
62
|
+
*/
|
63
|
+
static create(...args) {
|
64
|
+
const [data, ...additionalArgs] = args;
|
65
|
+
return new this().create(
|
66
|
+
data,
|
67
|
+
...additionalArgs
|
68
|
+
);
|
69
|
+
}
|
70
|
+
/**
|
71
|
+
* Static method to update an entity instance.
|
72
|
+
*
|
73
|
+
* @template Entity - The type of entity being updated
|
74
|
+
* @param {Constructor<Entity>} this - The entity constructor
|
75
|
+
* @param {...Parameters<Entity['update']>} args - Arguments for entity update
|
76
|
+
* @returns {Entity} The updated entity instance
|
77
|
+
*/
|
78
|
+
static update(...args) {
|
79
|
+
const [data, ...additionalArgs] = args;
|
80
|
+
return new this().update(
|
81
|
+
data,
|
82
|
+
...additionalArgs
|
83
|
+
);
|
84
|
+
}
|
85
|
+
/**
|
86
|
+
* Static method to map data to an entity instance.
|
87
|
+
*
|
88
|
+
* @template Entity - The type of entity being mapped
|
89
|
+
* @param {Constructor<Entity>} this - The entity constructor
|
90
|
+
* @param {Partial<EntityDTO<FromEntityType<Entity>>>} data - The data to map
|
91
|
+
* @returns {Entity} A new entity instance with mapped data
|
92
|
+
*/
|
93
|
+
static map(data) {
|
94
|
+
return new this().map(data);
|
95
|
+
}
|
96
|
+
/**
|
97
|
+
* Creates a new entity instance with the provided data.
|
98
|
+
*
|
99
|
+
* @param {CreateShape<BaseEntityWithId, this>} data - The data to create the entity with
|
100
|
+
* @returns {this} The created entity instance
|
101
|
+
*/
|
102
|
+
create(data) {
|
103
|
+
return Object.assign(this, transformRawDto(data, this));
|
104
|
+
}
|
105
|
+
/**
|
106
|
+
* Updates the entity instance with the provided data.
|
107
|
+
*
|
108
|
+
* @param {UpdateShape<BaseEntityWithId, this>} data - The data to update the entity with
|
109
|
+
* @returns {this} The updated entity instance
|
110
|
+
*/
|
111
|
+
update(data) {
|
112
|
+
(0, import_core2.wrap)(this).assign(
|
113
|
+
(0, import_common.stripUndefinedProperties)(transformRawDto(data, this))
|
114
|
+
);
|
115
|
+
return this;
|
116
|
+
}
|
117
|
+
/**
|
118
|
+
* Reads the entity data as a plain object.
|
119
|
+
*
|
120
|
+
* @returns {EntityDTO<this> | this} The entity data as a plain object
|
121
|
+
*/
|
122
|
+
read() {
|
123
|
+
if (typeof (0, import_core2.wrap)(this).toPOJO === "function") {
|
124
|
+
return (0, import_core2.wrap)(this).toPOJO();
|
125
|
+
}
|
126
|
+
return this;
|
127
|
+
}
|
128
|
+
/**
|
129
|
+
* Maps data to the entity instance.
|
130
|
+
*
|
131
|
+
* @param {Partial<EntityDTO<FromEntityType<this>>>} data - The data to map
|
132
|
+
* @returns {this} The entity instance with mapped data
|
133
|
+
*/
|
134
|
+
map(data) {
|
135
|
+
(0, import_core2.wrap)(this).assign(data);
|
136
|
+
return this;
|
137
|
+
}
|
138
|
+
};
|
139
|
+
|
140
|
+
// src/persistence/collection.ts
|
141
|
+
function collection(items) {
|
142
|
+
return {
|
143
|
+
_collection: true,
|
144
|
+
items
|
145
|
+
};
|
146
|
+
}
|
147
|
+
// Annotate the CommonJS export names for ESM import in node:
|
148
|
+
0 && (module.exports = {
|
149
|
+
BaseEntity,
|
150
|
+
collection
|
151
|
+
});
|
152
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../../src/persistence/index.ts","../../../src/persistence/base.entity.ts","../../../src/persistence/transformRawDto.ts","../../../src/persistence/guards/isMarkedCollection.ts","../../../src/persistence/collection.ts"],"sourcesContent":["export * from './base.entity';\nexport * from './collection';\n","import { stripUndefinedProperties } from '@forklaunch/common';\nimport {\n Constructor,\n EntityDTO,\n FromEntityType,\n BaseEntity as MikroOrmBaseEntity,\n wrap\n} from '@mikro-orm/core';\nimport { transformRawDto } from './transformRawDto';\nimport { CreateShape, UpdateShape } from './types/shapes.types';\n\n/**\n * Type representing a base entity with common fields.\n * Extends BaseEntity with optional id, _id, createdAt, and updatedAt fields.\n */\ntype BaseEntityWithId = BaseEntity & {\n id?: unknown;\n _id?: unknown;\n createdAt?: unknown;\n updatedAt?: unknown;\n};\n\n/**\n * Abstract base class for all entities in the system.\n * Extends MikroORM's BaseEntity and provides common CRUD operations.\n */\nexport abstract class BaseEntity extends MikroOrmBaseEntity {\n /**\n * Static factory method to create a new entity instance.\n *\n * @template Entity - The type of entity being created\n * @param {Constructor<Entity>} this - The entity constructor\n * @param {...Parameters<Entity['create']>} args - Arguments for entity creation\n * @returns {Entity} A new entity instance\n */\n static create<Entity extends BaseEntityWithId>(\n this: Constructor<Entity>,\n ...args: Parameters<Entity['create']>\n ): Entity {\n const [data, ...additionalArgs] = args;\n return new this().create(\n data as CreateShape<BaseEntityWithId, Entity>,\n ...additionalArgs\n );\n }\n\n /**\n * Static method to update an entity instance.\n *\n * @template Entity - The type of entity being updated\n * @param {Constructor<Entity>} this - The entity constructor\n * @param {...Parameters<Entity['update']>} args - Arguments for entity update\n * @returns {Entity} The updated entity instance\n */\n static update<Entity extends BaseEntityWithId>(\n this: Constructor<Entity>,\n ...args: Parameters<Entity['update']>\n ): Entity {\n const [data, ...additionalArgs] = args;\n return new this().update(\n data as UpdateShape<BaseEntity, Entity>,\n ...additionalArgs\n );\n }\n\n /**\n * Static method to map data to an entity instance.\n *\n * @template Entity - The type of entity being mapped\n * @param {Constructor<Entity>} this - The entity constructor\n * @param {Partial<EntityDTO<FromEntityType<Entity>>>} data - The data to map\n * @returns {Entity} A new entity instance with mapped data\n */\n static map<Entity extends BaseEntity>(\n this: Constructor<Entity>,\n data: Partial<EntityDTO<FromEntityType<Entity>>>\n ): Entity {\n return new this().map(data);\n }\n\n /**\n * Creates a new entity instance with the provided data.\n *\n * @param {CreateShape<BaseEntityWithId, this>} data - The data to create the entity with\n * @returns {this} The created entity instance\n */\n create(data: CreateShape<BaseEntityWithId, this>): this {\n return Object.assign(this, transformRawDto(data, this));\n }\n\n /**\n * Updates the entity instance with the provided data.\n *\n * @param {UpdateShape<BaseEntityWithId, this>} data - The data to update the entity with\n * @returns {this} The updated entity instance\n */\n update(data: UpdateShape<BaseEntityWithId, this>): this {\n wrap(this).assign(\n stripUndefinedProperties(transformRawDto(data, this)) as Partial<\n EntityDTO<FromEntityType<this>>\n >\n );\n return this;\n }\n\n /**\n * Reads the entity data as a plain object.\n *\n * @returns {EntityDTO<this> | this} The entity data as a plain object\n */\n read(): EntityDTO<this> | this {\n if (typeof wrap(this).toPOJO === 'function') {\n return wrap(this).toPOJO();\n }\n return this;\n }\n\n /**\n * Maps data to the entity instance.\n *\n * @param {Partial<EntityDTO<FromEntityType<this>>>} data - The data to map\n * @returns {this} The entity instance with mapped data\n */\n map(data: Partial<EntityDTO<FromEntityType<this>>>): this {\n wrap(this).assign(data);\n return this;\n }\n}\n","import { BaseEntity, Collection } from '@mikro-orm/core';\nimport { isMarkedCollection } from './guards/isMarkedCollection';\n\n/**\n * Transforms a raw DTO (Data Transfer Object) by converting marked collections into MikroORM collections.\n * This function is used to properly handle collections when converting between DTOs and entities.\n *\n * @template T - The type of the DTO being transformed\n * @template U - The type of the entity being transformed into\n * @param {T} data - The raw DTO data to transform\n * @param {U} entity - The entity instance to associate collections with\n * @returns {T} The transformed DTO with collections properly initialized\n * @example\n * const dto = { users: { _collection: true, items: [{ id: 1 }] } };\n * const entity = new UserEntity();\n * const transformed = transformRawDto(dto, entity);\n * // transformed.users is now a MikroORM Collection\n */\nexport function transformRawDto<\n T extends Record<string, unknown>,\n U extends BaseEntity\n>(data: T, entity: U): T {\n const transformedObject: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(data)) {\n if (isMarkedCollection<object>(value)) {\n transformedObject[key] = new Collection(entity, value.items);\n } else {\n transformedObject[key] = value;\n }\n }\n return transformedObject as T;\n}\n","import { MarkedCollection } from '../types/markedCollection.types';\n\n/**\n * Type guard function that checks if a value is a marked collection.\n *\n * @template T - The type of items in the collection\n * @param {unknown} value - The value to check\n * @returns {value is MarkedCollection<T>} True if the value is a marked collection, false otherwise\n * @example\n * const value = { _collection: true, items: [1, 2, 3] };\n * if (isMarkedCollection<number>(value)) {\n * // value is now typed as MarkedCollection<number>\n * console.log(value.items); // [1, 2, 3]\n * }\n */\nexport function isMarkedCollection<T>(\n value: unknown\n): value is MarkedCollection<T> {\n return (\n typeof value === 'object' &&\n value !== null &&\n '_collection' in value &&\n typeof value._collection === 'boolean' &&\n value._collection\n );\n}\n","import { MarkedCollection } from './types/markedCollection.types';\n\n/**\n * Creates a marked collection from an array of items.\n * A marked collection is a wrapper around an array that indicates it should be treated as a collection.\n *\n * @template T - The type of items in the collection\n * @param {T[]} items - The array of items to wrap in a collection\n * @returns {MarkedCollection<T>} A marked collection containing the provided items\n * @example\n * const users = collection([{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]);\n */\nexport function collection<T>(items: T[]): MarkedCollection<T> {\n return {\n _collection: true,\n items\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,oBAAAA;AAAA,EAAA;AAAA;AAAA;;;ACAA,oBAAyC;AACzC,IAAAC,eAMO;;;ACPP,kBAAuC;;;ACehC,SAAS,mBACd,OAC8B;AAC9B,SACE,OAAO,UAAU,YACjB,UAAU,QACV,iBAAiB,SACjB,OAAO,MAAM,gBAAgB,aAC7B,MAAM;AAEV;;;ADPO,SAAS,gBAGd,MAAS,QAAc;AACvB,QAAM,oBAA6C,CAAC;AACpD,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,IAAI,GAAG;AAC/C,QAAI,mBAA2B,KAAK,GAAG;AACrC,wBAAkB,GAAG,IAAI,IAAI,uBAAW,QAAQ,MAAM,KAAK;AAAA,IAC7D,OAA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