@forklaunch/core 0.8.8 → 0.9.2

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.
@@ -130,16 +130,16 @@ declare abstract class RequestDtoMapper<Entity, SV extends AnySchemaValidator> e
130
130
  *
131
131
  * @abstract
132
132
  * @param {...unknown[]} additionalArgs - Additional arguments.
133
- * @returns {Entity} - The entity.
133
+ * @returns {Promise<Entity>} - The entity.
134
134
  */
135
- abstract toEntity(...additionalArgs: unknown[]): Entity;
135
+ abstract toEntity(...additionalArgs: unknown[]): Promise<Entity>;
136
136
  /**
137
137
  * Populates the DTO with data from a JSON object.
138
138
  *
139
139
  * @param {this['_dto']} json - The JSON object.
140
- * @returns {this} - The instance of the RequestDtoMapper.
140
+ * @returns {Promise<this>} - The instance of the RequestDtoMapper.
141
141
  */
142
- fromDto(json: this['_dto']): this;
142
+ fromDto(json: this['_dto']): Promise<this>;
143
143
  /**
144
144
  * Deserializes a JSON object to an entity.
145
145
  *
@@ -147,7 +147,7 @@ declare abstract class RequestDtoMapper<Entity, SV extends AnySchemaValidator> e
147
147
  * @param {...unknown[]} additionalArgs - Additional arguments.
148
148
  * @returns {Entity} - The entity.
149
149
  */
150
- deserializeDtoToEntity(json: this['_dto'], ...additionalArgs: Parameters<this['toEntity']>): Entity;
150
+ deserializeDtoToEntity(json: this['_dto'], ...additionalArgs: Parameters<this['toEntity']>): Promise<Entity>;
151
151
  /**
152
152
  * Creates an instance of a RequestDtoMapper from a JSON object.
153
153
  *
@@ -159,7 +159,7 @@ declare abstract class RequestDtoMapper<Entity, SV extends AnySchemaValidator> e
159
159
  * @param {JsonType} json - The JSON object.
160
160
  * @returns {T} - An instance of the T.
161
161
  */
162
- static fromDto<T extends RequestDtoMapper<unknown, SV>, SV extends AnySchemaValidator, JsonType extends T['_dto']>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, json: JsonType): T;
162
+ static fromDto<T extends RequestDtoMapper<unknown, SV>, SV extends AnySchemaValidator, JsonType extends T['_dto']>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, json: JsonType): Promise<T>;
163
163
  /**
164
164
  * Deserializes a JSON object to an entity.
165
165
  *
@@ -172,7 +172,7 @@ declare abstract class RequestDtoMapper<Entity, SV extends AnySchemaValidator> e
172
172
  * @param {...unknown[]} additionalArgs - Additional arguments.
173
173
  * @returns {T['_Entity']} - The entity.
174
174
  */
175
- static deserializeDtoToEntity<T extends RequestDtoMapper<unknown, SV>, SV extends AnySchemaValidator, JsonType extends T['_dto']>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, json: JsonType, ...additionalArgs: Parameters<T['toEntity']>): T['_Entity'];
175
+ static deserializeDtoToEntity<T extends RequestDtoMapper<unknown, SV>, SV extends AnySchemaValidator, JsonType extends T['_dto']>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, json: JsonType, ...additionalArgs: Parameters<T['toEntity']>): Promise<T['_Entity']>;
176
176
  }
177
177
 
178
178
  /**
@@ -197,14 +197,14 @@ declare abstract class ResponseDtoMapper<Entity, SV extends AnySchemaValidator>
197
197
  * @param {...unknown[]} additionalArgs - Additional arguments.
198
198
  * @returns {this} - The instance of the ResponseDtoMapper.
199
199
  */
200
- abstract fromEntity(entity: Entity, ...additionalArgs: unknown[]): this;
200
+ abstract fromEntity(entity: Entity, ...additionalArgs: unknown[]): Promise<this>;
201
201
  /**
202
202
  * Converts the underlying DTO to a JSON object.
203
203
  *
204
204
  * @returns {this['_dto']} - The JSON object.
205
205
  * @throws {Error} - Throws an error if the DTO is invalid.
206
206
  */
207
- toDto(): this['_dto'];
207
+ toDto(): Promise<this['_dto']>;
208
208
  /**
209
209
  * Serializes an entity to a JSON object.
210
210
  *
@@ -212,7 +212,7 @@ declare abstract class ResponseDtoMapper<Entity, SV extends AnySchemaValidator>
212
212
  * @returns {this['_dto']} - The JSON object.
213
213
  * @throws {Error} - Throws an error if the DTO is invalid.
214
214
  */
215
- serializeEntityToDto(...[entity, ...additionalArgs]: Parameters<this['fromEntity']>): this['_dto'];
215
+ serializeEntityToDto(...[entity, ...additionalArgs]: Parameters<this['fromEntity']>): Promise<this['_dto']>;
216
216
  /**
217
217
  * Populates entity mapper with DTO from an entity.
218
218
  *
@@ -223,7 +223,7 @@ declare abstract class ResponseDtoMapper<Entity, SV extends AnySchemaValidator>
223
223
  * @param {T['_Entity']} entity - The entity to convert.
224
224
  * @returns {T} - An instance of the T.
225
225
  */
226
- static fromEntity<T extends ResponseDtoMapper<unknown, SV>, SV extends AnySchemaValidator>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>): T;
226
+ static fromEntity<T extends ResponseDtoMapper<unknown, SV>, SV extends AnySchemaValidator>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>): Promise<T>;
227
227
  /**
228
228
  * Serializes an entity to a JSON object.
229
229
  *
@@ -235,7 +235,7 @@ declare abstract class ResponseDtoMapper<Entity, SV extends AnySchemaValidator>
235
235
  * @returns {T['_dto']} - The JSON object.
236
236
  * @throws {Error} - Throws an error if the DTO is invalid.
237
237
  */
238
- static serializeEntityToDto<T extends ResponseDtoMapper<unknown, SV>, SV extends AnySchemaValidator, DtoType extends T['_dto']>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>): DtoType;
238
+ static serializeEntityToDto<T extends ResponseDtoMapper<unknown, SV>, SV extends AnySchemaValidator, DtoType extends T['_dto']>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>): Promise<DtoType>;
239
239
  }
240
240
 
241
241
  type SchemasByValidator<T extends AnySchemaValidator, TypeBoxSchemas extends (...args: never[]) => unknown, ZodSchemas extends (...args: never[]) => unknown> = T extends TypeboxSchemaValidator ? ReturnType<TypeBoxSchemas> : T extends ZodSchemaValidator ? ReturnType<ZodSchemas> : never;
@@ -259,13 +259,13 @@ type InternalDtoMapper<DtoMapper extends Record<string, {
259
259
  _Entity: unknown;
260
260
  serializeEntityToDto: unknown;
261
261
  } ? {
262
- serializeEntityToDto: (entity: Entities[K], ...additionalArgs: AllAfterFirstParameters<DtoMapper[K]['serializeEntityToDto']>) => Dto[K];
262
+ serializeEntityToDto: (entity: Entities[K], ...additionalArgs: AllAfterFirstParameters<DtoMapper[K]['serializeEntityToDto']>) => Promise<Dto[K]>;
263
263
  } : DtoMapper[K] extends {
264
264
  dto: unknown;
265
265
  _Entity: unknown;
266
266
  deserializeDtoToEntity: unknown;
267
267
  } ? {
268
- deserializeDtoToEntity: (dto: Dto[K], ...additionalArgs: AllAfterFirstParameters<DtoMapper[K]['deserializeDtoToEntity']>) => Entities[K];
268
+ deserializeDtoToEntity: (dto: Dto[K], ...additionalArgs: AllAfterFirstParameters<DtoMapper[K]['deserializeDtoToEntity']>) => Promise<Entities[K]>;
269
269
  } : never;
270
270
  };
271
271
 
@@ -130,16 +130,16 @@ declare abstract class RequestDtoMapper<Entity, SV extends AnySchemaValidator> e
130
130
  *
131
131
  * @abstract
132
132
  * @param {...unknown[]} additionalArgs - Additional arguments.
133
- * @returns {Entity} - The entity.
133
+ * @returns {Promise<Entity>} - The entity.
134
134
  */
135
- abstract toEntity(...additionalArgs: unknown[]): Entity;
135
+ abstract toEntity(...additionalArgs: unknown[]): Promise<Entity>;
136
136
  /**
137
137
  * Populates the DTO with data from a JSON object.
138
138
  *
139
139
  * @param {this['_dto']} json - The JSON object.
140
- * @returns {this} - The instance of the RequestDtoMapper.
140
+ * @returns {Promise<this>} - The instance of the RequestDtoMapper.
141
141
  */
142
- fromDto(json: this['_dto']): this;
142
+ fromDto(json: this['_dto']): Promise<this>;
143
143
  /**
144
144
  * Deserializes a JSON object to an entity.
145
145
  *
@@ -147,7 +147,7 @@ declare abstract class RequestDtoMapper<Entity, SV extends AnySchemaValidator> e
147
147
  * @param {...unknown[]} additionalArgs - Additional arguments.
148
148
  * @returns {Entity} - The entity.
149
149
  */
150
- deserializeDtoToEntity(json: this['_dto'], ...additionalArgs: Parameters<this['toEntity']>): Entity;
150
+ deserializeDtoToEntity(json: this['_dto'], ...additionalArgs: Parameters<this['toEntity']>): Promise<Entity>;
151
151
  /**
152
152
  * Creates an instance of a RequestDtoMapper from a JSON object.
153
153
  *
@@ -159,7 +159,7 @@ declare abstract class RequestDtoMapper<Entity, SV extends AnySchemaValidator> e
159
159
  * @param {JsonType} json - The JSON object.
160
160
  * @returns {T} - An instance of the T.
161
161
  */
162
- static fromDto<T extends RequestDtoMapper<unknown, SV>, SV extends AnySchemaValidator, JsonType extends T['_dto']>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, json: JsonType): T;
162
+ static fromDto<T extends RequestDtoMapper<unknown, SV>, SV extends AnySchemaValidator, JsonType extends T['_dto']>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, json: JsonType): Promise<T>;
163
163
  /**
164
164
  * Deserializes a JSON object to an entity.
165
165
  *
@@ -172,7 +172,7 @@ declare abstract class RequestDtoMapper<Entity, SV extends AnySchemaValidator> e
172
172
  * @param {...unknown[]} additionalArgs - Additional arguments.
173
173
  * @returns {T['_Entity']} - The entity.
174
174
  */
175
- static deserializeDtoToEntity<T extends RequestDtoMapper<unknown, SV>, SV extends AnySchemaValidator, JsonType extends T['_dto']>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, json: JsonType, ...additionalArgs: Parameters<T['toEntity']>): T['_Entity'];
175
+ static deserializeDtoToEntity<T extends RequestDtoMapper<unknown, SV>, SV extends AnySchemaValidator, JsonType extends T['_dto']>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, json: JsonType, ...additionalArgs: Parameters<T['toEntity']>): Promise<T['_Entity']>;
176
176
  }
177
177
 
178
178
  /**
@@ -197,14 +197,14 @@ declare abstract class ResponseDtoMapper<Entity, SV extends AnySchemaValidator>
197
197
  * @param {...unknown[]} additionalArgs - Additional arguments.
198
198
  * @returns {this} - The instance of the ResponseDtoMapper.
199
199
  */
200
- abstract fromEntity(entity: Entity, ...additionalArgs: unknown[]): this;
200
+ abstract fromEntity(entity: Entity, ...additionalArgs: unknown[]): Promise<this>;
201
201
  /**
202
202
  * Converts the underlying DTO to a JSON object.
203
203
  *
204
204
  * @returns {this['_dto']} - The JSON object.
205
205
  * @throws {Error} - Throws an error if the DTO is invalid.
206
206
  */
207
- toDto(): this['_dto'];
207
+ toDto(): Promise<this['_dto']>;
208
208
  /**
209
209
  * Serializes an entity to a JSON object.
210
210
  *
@@ -212,7 +212,7 @@ declare abstract class ResponseDtoMapper<Entity, SV extends AnySchemaValidator>
212
212
  * @returns {this['_dto']} - The JSON object.
213
213
  * @throws {Error} - Throws an error if the DTO is invalid.
214
214
  */
215
- serializeEntityToDto(...[entity, ...additionalArgs]: Parameters<this['fromEntity']>): this['_dto'];
215
+ serializeEntityToDto(...[entity, ...additionalArgs]: Parameters<this['fromEntity']>): Promise<this['_dto']>;
216
216
  /**
217
217
  * Populates entity mapper with DTO from an entity.
218
218
  *
@@ -223,7 +223,7 @@ declare abstract class ResponseDtoMapper<Entity, SV extends AnySchemaValidator>
223
223
  * @param {T['_Entity']} entity - The entity to convert.
224
224
  * @returns {T} - An instance of the T.
225
225
  */
226
- static fromEntity<T extends ResponseDtoMapper<unknown, SV>, SV extends AnySchemaValidator>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>): T;
226
+ static fromEntity<T extends ResponseDtoMapper<unknown, SV>, SV extends AnySchemaValidator>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>): Promise<T>;
227
227
  /**
228
228
  * Serializes an entity to a JSON object.
229
229
  *
@@ -235,7 +235,7 @@ declare abstract class ResponseDtoMapper<Entity, SV extends AnySchemaValidator>
235
235
  * @returns {T['_dto']} - The JSON object.
236
236
  * @throws {Error} - Throws an error if the DTO is invalid.
237
237
  */
238
- static serializeEntityToDto<T extends ResponseDtoMapper<unknown, SV>, SV extends AnySchemaValidator, DtoType extends T['_dto']>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>): DtoType;
238
+ static serializeEntityToDto<T extends ResponseDtoMapper<unknown, SV>, SV extends AnySchemaValidator, DtoType extends T['_dto']>(this: DtoMapperConstructor<T, SV>, schemaValidator: SV, ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>): Promise<DtoType>;
239
239
  }
240
240
 
241
241
  type SchemasByValidator<T extends AnySchemaValidator, TypeBoxSchemas extends (...args: never[]) => unknown, ZodSchemas extends (...args: never[]) => unknown> = T extends TypeboxSchemaValidator ? ReturnType<TypeBoxSchemas> : T extends ZodSchemaValidator ? ReturnType<ZodSchemas> : never;
@@ -259,13 +259,13 @@ type InternalDtoMapper<DtoMapper extends Record<string, {
259
259
  _Entity: unknown;
260
260
  serializeEntityToDto: unknown;
261
261
  } ? {
262
- serializeEntityToDto: (entity: Entities[K], ...additionalArgs: AllAfterFirstParameters<DtoMapper[K]['serializeEntityToDto']>) => Dto[K];
262
+ serializeEntityToDto: (entity: Entities[K], ...additionalArgs: AllAfterFirstParameters<DtoMapper[K]['serializeEntityToDto']>) => Promise<Dto[K]>;
263
263
  } : DtoMapper[K] extends {
264
264
  dto: unknown;
265
265
  _Entity: unknown;
266
266
  deserializeDtoToEntity: unknown;
267
267
  } ? {
268
- deserializeDtoToEntity: (dto: Dto[K], ...additionalArgs: AllAfterFirstParameters<DtoMapper[K]['deserializeDtoToEntity']>) => Entities[K];
268
+ deserializeDtoToEntity: (dto: Dto[K], ...additionalArgs: AllAfterFirstParameters<DtoMapper[K]['deserializeDtoToEntity']>) => Promise<Entities[K]>;
269
269
  } : never;
270
270
  };
271
271
 
@@ -52,7 +52,7 @@ var BaseDtoMapper = class {
52
52
  * The Data Transfer Object (DTO).
53
53
  * @type {Schema<this['schema'], SV>}
54
54
  */
55
- _dto = {};
55
+ _dto;
56
56
  /**
57
57
  * Creates an instance of BaseDtoMapper.
58
58
  *
@@ -110,7 +110,7 @@ var RequestDtoMapper = class extends BaseDtoMapper {
110
110
  * Populates the DTO with data from a JSON object.
111
111
  *
112
112
  * @param {this['_dto']} json - The JSON object.
113
- * @returns {this} - The instance of the RequestDtoMapper.
113
+ * @returns {Promise<this>} - The instance of the RequestDtoMapper.
114
114
  */
115
115
  fromDto(json) {
116
116
  const parsedSchema = this.schemaValidator.parse(
@@ -121,7 +121,7 @@ var RequestDtoMapper = class extends BaseDtoMapper {
121
121
  throw new Error((0, import_validator2.prettyPrintParseErrors)(parsedSchema.errors, "DTO"));
122
122
  }
123
123
  this.dto = json;
124
- return this;
124
+ return Promise.resolve(this);
125
125
  }
126
126
  /**
127
127
  * Deserializes a JSON object to an entity.
@@ -131,7 +131,8 @@ var RequestDtoMapper = class extends BaseDtoMapper {
131
131
  * @returns {Entity} - The entity.
132
132
  */
133
133
  deserializeDtoToEntity(json, ...additionalArgs) {
134
- return this.fromDto(json).toEntity(...additionalArgs);
134
+ const result = this.fromDto(json);
135
+ return result.then((r) => r.toEntity(...additionalArgs));
135
136
  }
136
137
  /**
137
138
  * Creates an instance of a RequestDtoMapper from a JSON object.
@@ -160,7 +161,8 @@ var RequestDtoMapper = class extends BaseDtoMapper {
160
161
  * @returns {T['_Entity']} - The entity.
161
162
  */
162
163
  static deserializeDtoToEntity(schemaValidator, json, ...additionalArgs) {
163
- return construct(this, schemaValidator).fromDto(json).toEntity(...additionalArgs);
164
+ const result = construct(this, schemaValidator).fromDto(json);
165
+ return result.then((r) => r.toEntity(...additionalArgs));
164
166
  }
165
167
  };
166
168
 
@@ -187,7 +189,7 @@ var ResponseDtoMapper = class extends BaseDtoMapper {
187
189
  if (!parsedSchema.ok) {
188
190
  throw new Error((0, import_validator3.prettyPrintParseErrors)(parsedSchema.errors, "DTO"));
189
191
  }
190
- return this.dto;
192
+ return Promise.resolve(this.dto);
191
193
  }
192
194
  /**
193
195
  * Serializes an entity to a JSON object.
@@ -197,7 +199,8 @@ var ResponseDtoMapper = class extends BaseDtoMapper {
197
199
  * @throws {Error} - Throws an error if the DTO is invalid.
198
200
  */
199
201
  serializeEntityToDto(...[entity, ...additionalArgs]) {
200
- return this.fromEntity(entity, ...additionalArgs).toDto();
202
+ const result = this.fromEntity(entity, ...additionalArgs);
203
+ return result.then((r) => r.toDto());
201
204
  }
202
205
  /**
203
206
  * Populates entity mapper with DTO from an entity.
@@ -227,7 +230,11 @@ var ResponseDtoMapper = class extends BaseDtoMapper {
227
230
  * @throws {Error} - Throws an error if the DTO is invalid.
228
231
  */
229
232
  static serializeEntityToDto(schemaValidator, ...[entity, ...additionalArgs]) {
230
- return construct(this, schemaValidator).fromEntity(entity, ...additionalArgs).toDto();
233
+ const result = construct(this, schemaValidator).fromEntity(
234
+ entity,
235
+ ...additionalArgs
236
+ );
237
+ return result.then((r) => r.toDto());
231
238
  }
232
239
  };
233
240
 
@@ -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 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;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,QAAI,CAAC,aAAa,IAAI;AACpB,YAAM,IAAI,UAAM,yCAAuB,aAAa,QAAQ,KAAK,CAAC;AAAA,IACpE;AACA,SAAK,OAAO;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,MAAoB;AACtB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,OAAO,SAE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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 {Promise<Entity>} - The entity.\n */\n abstract toEntity(...additionalArgs: unknown[]): Promise<Entity>;\n\n /**\n * Populates the DTO with data from a JSON object.\n *\n * @param {this['_dto']} json - The JSON object.\n * @returns {Promise<this>} - The instance of the RequestDtoMapper.\n */\n fromDto(json: this['_dto']): Promise<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 Promise.resolve(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 ): Promise<Entity> {\n const result = this.fromDto(json);\n return result.then((r) => r.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 >(\n this: DtoMapperConstructor<T, SV>,\n schemaValidator: SV,\n json: JsonType\n ): Promise<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 ): Promise<T['_Entity']> {\n const result = construct(this, schemaValidator).fromDto(json);\n return result.then((r) => r.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>;\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(\n entity: Entity,\n ...additionalArgs: unknown[]\n ): Promise<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(): Promise<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 Promise.resolve(this.dto) as unknown as Promise<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 ): Promise<this['_dto']> {\n const result = this.fromEntity(entity, ...additionalArgs);\n return result.then((r) => r.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 ): Promise<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 ): Promise<DtoType> {\n const result = construct(this, schemaValidator).fromEntity(\n entity,\n ...additionalArgs\n );\n return result.then((r) => r.toDto()) as Promise<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,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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,OAAO,WAKL,oBACG,CAAC,QAAQ,GAAG,cAAc,GACjB;AACZ,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,GACX;AAClB,UAAM,SAAS,UAAU,MAAM,eAAe,EAAE;AAAA,MAC9C;AAAA,MACA,GAAG;AAAA,IACL;AACA,WAAO,OAAO,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC;AAAA,EACrC;AACF;;;ACvHA,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"]}
@@ -27,7 +27,7 @@ var BaseDtoMapper = class {
27
27
  * The Data Transfer Object (DTO).
28
28
  * @type {Schema<this['schema'], SV>}
29
29
  */
30
- _dto = {};
30
+ _dto;
31
31
  /**
32
32
  * Creates an instance of BaseDtoMapper.
33
33
  *
@@ -85,7 +85,7 @@ var RequestDtoMapper = class extends BaseDtoMapper {
85
85
  * Populates the DTO with data from a JSON object.
86
86
  *
87
87
  * @param {this['_dto']} json - The JSON object.
88
- * @returns {this} - The instance of the RequestDtoMapper.
88
+ * @returns {Promise<this>} - The instance of the RequestDtoMapper.
89
89
  */
90
90
  fromDto(json) {
91
91
  const parsedSchema = this.schemaValidator.parse(
@@ -96,7 +96,7 @@ var RequestDtoMapper = class extends BaseDtoMapper {
96
96
  throw new Error(prettyPrintParseErrors2(parsedSchema.errors, "DTO"));
97
97
  }
98
98
  this.dto = json;
99
- return this;
99
+ return Promise.resolve(this);
100
100
  }
101
101
  /**
102
102
  * Deserializes a JSON object to an entity.
@@ -106,7 +106,8 @@ var RequestDtoMapper = class extends BaseDtoMapper {
106
106
  * @returns {Entity} - The entity.
107
107
  */
108
108
  deserializeDtoToEntity(json, ...additionalArgs) {
109
- return this.fromDto(json).toEntity(...additionalArgs);
109
+ const result = this.fromDto(json);
110
+ return result.then((r) => r.toEntity(...additionalArgs));
110
111
  }
111
112
  /**
112
113
  * Creates an instance of a RequestDtoMapper from a JSON object.
@@ -135,7 +136,8 @@ var RequestDtoMapper = class extends BaseDtoMapper {
135
136
  * @returns {T['_Entity']} - The entity.
136
137
  */
137
138
  static deserializeDtoToEntity(schemaValidator, json, ...additionalArgs) {
138
- return construct(this, schemaValidator).fromDto(json).toEntity(...additionalArgs);
139
+ const result = construct(this, schemaValidator).fromDto(json);
140
+ return result.then((r) => r.toEntity(...additionalArgs));
139
141
  }
140
142
  };
141
143
 
@@ -164,7 +166,7 @@ var ResponseDtoMapper = class extends BaseDtoMapper {
164
166
  if (!parsedSchema.ok) {
165
167
  throw new Error(prettyPrintParseErrors3(parsedSchema.errors, "DTO"));
166
168
  }
167
- return this.dto;
169
+ return Promise.resolve(this.dto);
168
170
  }
169
171
  /**
170
172
  * Serializes an entity to a JSON object.
@@ -174,7 +176,8 @@ var ResponseDtoMapper = class extends BaseDtoMapper {
174
176
  * @throws {Error} - Throws an error if the DTO is invalid.
175
177
  */
176
178
  serializeEntityToDto(...[entity, ...additionalArgs]) {
177
- return this.fromEntity(entity, ...additionalArgs).toDto();
179
+ const result = this.fromEntity(entity, ...additionalArgs);
180
+ return result.then((r) => r.toDto());
178
181
  }
179
182
  /**
180
183
  * Populates entity mapper with DTO from an entity.
@@ -204,7 +207,11 @@ var ResponseDtoMapper = class extends BaseDtoMapper {
204
207
  * @throws {Error} - Throws an error if the DTO is invalid.
205
208
  */
206
209
  static serializeEntityToDto(schemaValidator, ...[entity, ...additionalArgs]) {
207
- return construct(this, schemaValidator).fromEntity(entity, ...additionalArgs).toDto();
210
+ const result = construct(this, schemaValidator).fromEntity(
211
+ entity,
212
+ ...additionalArgs
213
+ );
214
+ return result.then((r) => r.toDto());
208
215
  }
209
216
  };
210
217
 
@@ -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 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(schemaValid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A;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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,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"]}
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 {Promise<Entity>} - The entity.\n */\n abstract toEntity(...additionalArgs: unknown[]): Promise<Entity>;\n\n /**\n * Populates the DTO with data from a JSON object.\n *\n * @param {this['_dto']} json - The JSON object.\n * @returns {Promise<this>} - The instance of the RequestDtoMapper.\n */\n fromDto(json: this['_dto']): Promise<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 Promise.resolve(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 ): Promise<Entity> {\n const result = this.fromDto(json);\n return result.then((r) => r.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 >(\n this: DtoMapperConstructor<T, SV>,\n schemaValidator: SV,\n json: JsonType\n ): Promise<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 ): Promise<T['_Entity']> {\n const result = construct(this, schemaValidator).fromDto(json);\n return result.then((r) => r.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>;\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(\n entity: Entity,\n ...additionalArgs: unknown[]\n ): Promise<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(): Promise<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 Promise.resolve(this.dto) as unknown as Promise<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 ): Promise<this['_dto']> {\n const result = this.fromEntity(entity, ...additionalArgs);\n return result.then((r) => r.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 ): Promise<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 ): Promise<DtoType> {\n const result = construct(this, schemaValidator).fromEntity(\n entity,\n ...additionalArgs\n );\n return result.then((r) => r.toDto()) as Promise<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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,YAAY,iBAAqB;AAC/B,SAAK,kBAAkB;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAI,IAAI,MAAoB;AAC1B,UAAM,eAAe,KAAK,gBAA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AO,OAAO,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC;AAAA,EACrC;AACF;;;ACvHA,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"]}
@@ -57,7 +57,7 @@ declare abstract class BaseEntity extends BaseEntity$1 {
57
57
  * @param {...Parameters<Entity['create']>} args - Arguments for entity creation
58
58
  * @returns {Entity} A new entity instance
59
59
  */
60
- static create<Entity extends BaseEntityWithId>(this: Constructor<Entity>, ...args: Parameters<Entity['create']>): Entity;
60
+ static create<Entity extends BaseEntityWithId>(this: Constructor<Entity>, ...args: Parameters<Entity['create']>): Promise<Entity>;
61
61
  /**
62
62
  * Static method to update an entity instance.
63
63
  *
@@ -66,7 +66,7 @@ declare abstract class BaseEntity extends BaseEntity$1 {
66
66
  * @param {...Parameters<Entity['update']>} args - Arguments for entity update
67
67
  * @returns {Entity} The updated entity instance
68
68
  */
69
- static update<Entity extends BaseEntityWithId>(this: Constructor<Entity>, ...args: Parameters<Entity['update']>): Entity;
69
+ static update<Entity extends BaseEntityWithId>(this: Constructor<Entity>, ...args: Parameters<Entity['update']>): Promise<Entity>;
70
70
  /**
71
71
  * Static method to map data to an entity instance.
72
72
  *
@@ -75,34 +75,34 @@ declare abstract class BaseEntity extends BaseEntity$1 {
75
75
  * @param {Partial<EntityDTO<FromEntityType<Entity>>>} data - The data to map
76
76
  * @returns {Entity} A new entity instance with mapped data
77
77
  */
78
- static map<Entity extends BaseEntity>(this: Constructor<Entity>, data: Partial<EntityDTO<FromEntityType<Entity>>>): Entity;
78
+ static map<Entity extends BaseEntity>(this: Constructor<Entity>, data: Partial<EntityDTO<FromEntityType<Entity>>>): Promise<Entity>;
79
79
  /**
80
80
  * Creates a new entity instance with the provided data.
81
81
  *
82
82
  * @param {CreateShape<BaseEntityWithId, this>} data - The data to create the entity with
83
83
  * @returns {this} The created entity instance
84
84
  */
85
- create(data: CreateShape<BaseEntityWithId, this>): this;
85
+ create(data: CreateShape<BaseEntityWithId, this>): Promise<EntityDTO<this>>;
86
86
  /**
87
87
  * Updates the entity instance with the provided data.
88
88
  *
89
89
  * @param {UpdateShape<BaseEntityWithId, this>} data - The data to update the entity with
90
90
  * @returns {this} The updated entity instance
91
91
  */
92
- update(data: UpdateShape<BaseEntityWithId, this>): this;
92
+ update(data: UpdateShape<BaseEntityWithId, this>): Promise<EntityDTO<this>>;
93
93
  /**
94
94
  * Reads the entity data as a plain object.
95
95
  *
96
96
  * @returns {EntityDTO<this> | this} The entity data as a plain object
97
97
  */
98
- read(): EntityDTO<this> | this;
98
+ read(): Promise<EntityDTO<this>>;
99
99
  /**
100
100
  * Maps data to the entity instance.
101
101
  *
102
102
  * @param {Partial<EntityDTO<FromEntityType<this>>>} data - The data to map
103
103
  * @returns {this} The entity instance with mapped data
104
104
  */
105
- map(data: Partial<EntityDTO<FromEntityType<this>>>): this;
105
+ map(data: Partial<EntityDTO<FromEntityType<this>>>): Promise<this>;
106
106
  }
107
107
 
108
108
  /**
@@ -57,7 +57,7 @@ declare abstract class BaseEntity extends BaseEntity$1 {
57
57
  * @param {...Parameters<Entity['create']>} args - Arguments for entity creation
58
58
  * @returns {Entity} A new entity instance
59
59
  */
60
- static create<Entity extends BaseEntityWithId>(this: Constructor<Entity>, ...args: Parameters<Entity['create']>): Entity;
60
+ static create<Entity extends BaseEntityWithId>(this: Constructor<Entity>, ...args: Parameters<Entity['create']>): Promise<Entity>;
61
61
  /**
62
62
  * Static method to update an entity instance.
63
63
  *
@@ -66,7 +66,7 @@ declare abstract class BaseEntity extends BaseEntity$1 {
66
66
  * @param {...Parameters<Entity['update']>} args - Arguments for entity update
67
67
  * @returns {Entity} The updated entity instance
68
68
  */
69
- static update<Entity extends BaseEntityWithId>(this: Constructor<Entity>, ...args: Parameters<Entity['update']>): Entity;
69
+ static update<Entity extends BaseEntityWithId>(this: Constructor<Entity>, ...args: Parameters<Entity['update']>): Promise<Entity>;
70
70
  /**
71
71
  * Static method to map data to an entity instance.
72
72
  *
@@ -75,34 +75,34 @@ declare abstract class BaseEntity extends BaseEntity$1 {
75
75
  * @param {Partial<EntityDTO<FromEntityType<Entity>>>} data - The data to map
76
76
  * @returns {Entity} A new entity instance with mapped data
77
77
  */
78
- static map<Entity extends BaseEntity>(this: Constructor<Entity>, data: Partial<EntityDTO<FromEntityType<Entity>>>): Entity;
78
+ static map<Entity extends BaseEntity>(this: Constructor<Entity>, data: Partial<EntityDTO<FromEntityType<Entity>>>): Promise<Entity>;
79
79
  /**
80
80
  * Creates a new entity instance with the provided data.
81
81
  *
82
82
  * @param {CreateShape<BaseEntityWithId, this>} data - The data to create the entity with
83
83
  * @returns {this} The created entity instance
84
84
  */
85
- create(data: CreateShape<BaseEntityWithId, this>): this;
85
+ create(data: CreateShape<BaseEntityWithId, this>): Promise<EntityDTO<this>>;
86
86
  /**
87
87
  * Updates the entity instance with the provided data.
88
88
  *
89
89
  * @param {UpdateShape<BaseEntityWithId, this>} data - The data to update the entity with
90
90
  * @returns {this} The updated entity instance
91
91
  */
92
- update(data: UpdateShape<BaseEntityWithId, this>): this;
92
+ update(data: UpdateShape<BaseEntityWithId, this>): Promise<EntityDTO<this>>;
93
93
  /**
94
94
  * Reads the entity data as a plain object.
95
95
  *
96
96
  * @returns {EntityDTO<this> | this} The entity data as a plain object
97
97
  */
98
- read(): EntityDTO<this> | this;
98
+ read(): Promise<EntityDTO<this>>;
99
99
  /**
100
100
  * Maps data to the entity instance.
101
101
  *
102
102
  * @param {Partial<EntityDTO<FromEntityType<this>>>} data - The data to map
103
103
  * @returns {this} The entity instance with mapped data
104
104
  */
105
- map(data: Partial<EntityDTO<FromEntityType<this>>>): this;
105
+ map(data: Partial<EntityDTO<FromEntityType<this>>>): Promise<this>;
106
106
  }
107
107
 
108
108
  /**
@@ -60,12 +60,14 @@ var BaseEntity2 = class extends import_core2.BaseEntity {
60
60
  * @param {...Parameters<Entity['create']>} args - Arguments for entity creation
61
61
  * @returns {Entity} A new entity instance
62
62
  */
63
- static create(...args) {
63
+ static async create(...args) {
64
64
  const [data, ...additionalArgs] = args;
65
- return new this().create(
65
+ const entity = new this();
66
+ await entity.create(
66
67
  data,
67
68
  ...additionalArgs
68
69
  );
70
+ return entity;
69
71
  }
70
72
  /**
71
73
  * Static method to update an entity instance.
@@ -75,12 +77,14 @@ var BaseEntity2 = class extends import_core2.BaseEntity {
75
77
  * @param {...Parameters<Entity['update']>} args - Arguments for entity update
76
78
  * @returns {Entity} The updated entity instance
77
79
  */
78
- static update(...args) {
80
+ static async update(...args) {
79
81
  const [data, ...additionalArgs] = args;
80
- return new this().update(
82
+ const entity = new this();
83
+ await entity.update(
81
84
  data,
82
85
  ...additionalArgs
83
86
  );
87
+ return entity;
84
88
  }
85
89
  /**
86
90
  * Static method to map data to an entity instance.
@@ -90,8 +94,10 @@ var BaseEntity2 = class extends import_core2.BaseEntity {
90
94
  * @param {Partial<EntityDTO<FromEntityType<Entity>>>} data - The data to map
91
95
  * @returns {Entity} A new entity instance with mapped data
92
96
  */
93
- static map(data) {
94
- return new this().map(data);
97
+ static async map(data) {
98
+ const entity = new this();
99
+ await entity.map(data);
100
+ return entity;
95
101
  }
96
102
  /**
97
103
  * Creates a new entity instance with the provided data.
@@ -99,8 +105,13 @@ var BaseEntity2 = class extends import_core2.BaseEntity {
99
105
  * @param {CreateShape<BaseEntityWithId, this>} data - The data to create the entity with
100
106
  * @returns {this} The created entity instance
101
107
  */
102
- create(data) {
103
- return Object.assign(this, transformRawDto(data, this));
108
+ async create(data) {
109
+ Object.assign(this, transformRawDto(data, this));
110
+ const entity = await (0, import_core2.wrap)(this).init();
111
+ if (!entity) {
112
+ throw new Error("Entity not initialized");
113
+ }
114
+ return entity.toObject();
104
115
  }
105
116
  /**
106
117
  * Updates the entity instance with the provided data.
@@ -108,22 +119,24 @@ var BaseEntity2 = class extends import_core2.BaseEntity {
108
119
  * @param {UpdateShape<BaseEntityWithId, this>} data - The data to update the entity with
109
120
  * @returns {this} The updated entity instance
110
121
  */
111
- update(data) {
112
- (0, import_core2.wrap)(this).assign(
122
+ async update(data) {
123
+ const entity = (0, import_core2.wrap)(this);
124
+ entity.assign(
113
125
  (0, import_common.stripUndefinedProperties)(transformRawDto(data, this))
114
126
  );
115
- return this;
127
+ return entity.toObject();
116
128
  }
117
129
  /**
118
130
  * Reads the entity data as a plain object.
119
131
  *
120
132
  * @returns {EntityDTO<this> | this} The entity data as a plain object
121
133
  */
122
- read() {
123
- if (typeof (0, import_core2.wrap)(this).toPOJO === "function") {
124
- return (0, import_core2.wrap)(this).toPOJO();
134
+ async read() {
135
+ const entity = await (0, import_core2.wrap)(this).init();
136
+ if (!entity) {
137
+ throw new Error("Entity not initialized");
125
138
  }
126
- return this;
139
+ return entity.toObject();
127
140
  }
128
141
  /**
129
142
  * Maps data to the entity instance.
@@ -131,9 +144,13 @@ var BaseEntity2 = class extends import_core2.BaseEntity {
131
144
  * @param {Partial<EntityDTO<FromEntityType<this>>>} data - The data to map
132
145
  * @returns {this} The entity instance with mapped data
133
146
  */
134
- map(data) {
135
- (0, import_core2.wrap)(this).assign(data);
136
- return this;
147
+ async map(data) {
148
+ const entity = await (0, import_core2.wrap)(this).init();
149
+ if (!entity) {
150
+ throw new Error("Entity not initialized");
151
+ }
152
+ entity.assign(data);
153
+ return entity;
137
154
  }
138
155
  };
139
156
 
@@ -1 +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,kB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AyB,gBAAgB,MAAM,IAAI,CAAC;AAAA,IAGtD;AACA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAA+B;AAC7B,QAAI,WAAO,mBAAK,IAAI,EAAE,WAAW,YAAY;AAC3C,iBAAO,mBAAK,IAAI,EAAE,OAAO;AAAA,IAC3B;AACA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAI,MAAsD;AACxD,2BAAK,IAAI,EAAE,OAAO,IAAI;AACtB,WAAO;AAAA,EACT;AACF;;;AGnHO,SAAS,WAAc,OAAiC;AAC7D,SAAO;AAAA,IACL,aAAa;AAAA,IACb;AAAA,EACF;AACF;","names":["BaseEntity","import_core","BaseEntity","MikroOrmBaseEntity"]}
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 async create<Entity extends BaseEntityWithId>(\n this: Constructor<Entity>,\n ...args: Parameters<Entity['create']>\n ): Promise<Entity> {\n const [data, ...additionalArgs] = args;\n const entity = new this();\n await entity.create(\n data as CreateShape<BaseEntityWithId, Entity>,\n ...additionalArgs\n );\n return entity;\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 async update<Entity extends BaseEntityWithId>(\n this: Constructor<Entity>,\n ...args: Parameters<Entity['update']>\n ): Promise<Entity> {\n const [data, ...additionalArgs] = args;\n const entity = new this();\n await entity.update(\n data as UpdateShape<BaseEntity, Entity>,\n ...additionalArgs\n );\n return entity;\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 async map<Entity extends BaseEntity>(\n this: Constructor<Entity>,\n data: Partial<EntityDTO<FromEntityType<Entity>>>\n ): Promise<Entity> {\n const entity = new this();\n await entity.map(data);\n return entity;\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 async create(\n data: CreateShape<BaseEntityWithId, this>\n ): Promise<EntityDTO<this>> {\n Object.assign(this, transformRawDto(data, this));\n const entity = await wrap(this).init();\n if (!entity) {\n throw new Error('Entity not initialized');\n }\n return entity.toObject();\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 async update(\n data: UpdateShape<BaseEntityWithId, this>\n ): Promise<EntityDTO<this>> {\n const entity = wrap(this);\n entity.assign(\n stripUndefinedProperties(transformRawDto(data, this)) as Partial<\n EntityDTO<FromEntityType<this>>\n >\n );\n return entity.toObject();\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 async read(): Promise<EntityDTO<this>> {\n const entity = await wrap(this).init();\n if (!entity) {\n throw new Error('Entity not initialized');\n }\n return entity.toObject();\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 async map(data: Partial<EntityDTO<FromEntityType<this>>>): Promise<this> {\n const entity = await wrap(this).init();\n\n if (!entity) {\n throw new Error('Entity not initialized');\n }\n entity.assign(data);\n return entity;\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,OAAO;AACL,wBAAkB,GAAG,IAAI;AAAA,IAC3B;AAAA,EACF;AACA,SAAO;AACT;;;ADLO,IAAeC,cAAf,cAAkC,aAAAC,WAAmB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAS1D,aAAa,UAER,MACc;AACjB,UAAM,CAAC,MAAM,GAAG,cAAc,IAAI;AAClC,UAAM,SAAS,IAA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ACvE,UAAM,SAAS,UAAM,mBAAK,IAAI,EAAE,KAAK;AAErC,QAAI,CAAC,QAAQ;AACX,YAAM,IAAI,MAAM,wBAAwB;AAAA,IAC1C;AACA,WAAO,OAAO,IAAI;AAClB,WAAO;AAAA,EACT;AACF;;;AGzIO,SAAS,WAAc,OAAiC;AAC7D,SAAO;AAAA,IACL,aAAa;AAAA,IACb;AAAA,EACF;AACF;","names":["BaseEntity","import_core","BaseEntity","MikroOrmBaseEntity"]}
@@ -36,12 +36,14 @@ var BaseEntity2 = class extends MikroOrmBaseEntity {
36
36
  * @param {...Parameters<Entity['create']>} args - Arguments for entity creation
37
37
  * @returns {Entity} A new entity instance
38
38
  */
39
- static create(...args) {
39
+ static async create(...args) {
40
40
  const [data, ...additionalArgs] = args;
41
- return new this().create(
41
+ const entity = new this();
42
+ await entity.create(
42
43
  data,
43
44
  ...additionalArgs
44
45
  );
46
+ return entity;
45
47
  }
46
48
  /**
47
49
  * Static method to update an entity instance.
@@ -51,12 +53,14 @@ var BaseEntity2 = class extends MikroOrmBaseEntity {
51
53
  * @param {...Parameters<Entity['update']>} args - Arguments for entity update
52
54
  * @returns {Entity} The updated entity instance
53
55
  */
54
- static update(...args) {
56
+ static async update(...args) {
55
57
  const [data, ...additionalArgs] = args;
56
- return new this().update(
58
+ const entity = new this();
59
+ await entity.update(
57
60
  data,
58
61
  ...additionalArgs
59
62
  );
63
+ return entity;
60
64
  }
61
65
  /**
62
66
  * Static method to map data to an entity instance.
@@ -66,8 +70,10 @@ var BaseEntity2 = class extends MikroOrmBaseEntity {
66
70
  * @param {Partial<EntityDTO<FromEntityType<Entity>>>} data - The data to map
67
71
  * @returns {Entity} A new entity instance with mapped data
68
72
  */
69
- static map(data) {
70
- return new this().map(data);
73
+ static async map(data) {
74
+ const entity = new this();
75
+ await entity.map(data);
76
+ return entity;
71
77
  }
72
78
  /**
73
79
  * Creates a new entity instance with the provided data.
@@ -75,8 +81,13 @@ var BaseEntity2 = class extends MikroOrmBaseEntity {
75
81
  * @param {CreateShape<BaseEntityWithId, this>} data - The data to create the entity with
76
82
  * @returns {this} The created entity instance
77
83
  */
78
- create(data) {
79
- return Object.assign(this, transformRawDto(data, this));
84
+ async create(data) {
85
+ Object.assign(this, transformRawDto(data, this));
86
+ const entity = await wrap(this).init();
87
+ if (!entity) {
88
+ throw new Error("Entity not initialized");
89
+ }
90
+ return entity.toObject();
80
91
  }
81
92
  /**
82
93
  * Updates the entity instance with the provided data.
@@ -84,22 +95,24 @@ var BaseEntity2 = class extends MikroOrmBaseEntity {
84
95
  * @param {UpdateShape<BaseEntityWithId, this>} data - The data to update the entity with
85
96
  * @returns {this} The updated entity instance
86
97
  */
87
- update(data) {
88
- wrap(this).assign(
98
+ async update(data) {
99
+ const entity = wrap(this);
100
+ entity.assign(
89
101
  stripUndefinedProperties(transformRawDto(data, this))
90
102
  );
91
- return this;
103
+ return entity.toObject();
92
104
  }
93
105
  /**
94
106
  * Reads the entity data as a plain object.
95
107
  *
96
108
  * @returns {EntityDTO<this> | this} The entity data as a plain object
97
109
  */
98
- read() {
99
- if (typeof wrap(this).toPOJO === "function") {
100
- return wrap(this).toPOJO();
110
+ async read() {
111
+ const entity = await wrap(this).init();
112
+ if (!entity) {
113
+ throw new Error("Entity not initialized");
101
114
  }
102
- return this;
115
+ return entity.toObject();
103
116
  }
104
117
  /**
105
118
  * Maps data to the entity instance.
@@ -107,9 +120,13 @@ var BaseEntity2 = class extends MikroOrmBaseEntity {
107
120
  * @param {Partial<EntityDTO<FromEntityType<this>>>} data - The data to map
108
121
  * @returns {this} The entity instance with mapped data
109
122
  */
110
- map(data) {
111
- wrap(this).assign(data);
112
- return this;
123
+ async map(data) {
124
+ const entity = await wrap(this).init();
125
+ if (!entity) {
126
+ throw new Error("Entity not initialized");
127
+ }
128
+ entity.assign(data);
129
+ return entity;
113
130
  }
114
131
  };
115
132
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/persistence/base.entity.ts","../../../src/persistence/transformRawDto.ts","../../../src/persistence/guards/isMarkedCollection.ts","../../../src/persistence/collection.ts"],"sourcesContent":["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,SAAS,gCAAgC;AACzC;AAAA,EAIE,cAAc;AAAA,EACd;AAAA,OACK;;;ACPP,SAAqB,kBAAkB;;;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;AACr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D;AACxD,SAAK,IAAI,EAAE,OAAO,IAAI;AACtB,WAAO;AAAA,EACT;AACF;;;AGnHO,SAAS,WAAc,OAAiC;AAC7D,SAAO;AAAA,IACL,aAAa;AAAA,IACb;AAAA,EACF;AACF;","names":["BaseEntity"]}
1
+ {"version":3,"sources":["../../../src/persistence/base.entity.ts","../../../src/persistence/transformRawDto.ts","../../../src/persistence/guards/isMarkedCollection.ts","../../../src/persistence/collection.ts"],"sourcesContent":["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 async create<Entity extends BaseEntityWithId>(\n this: Constructor<Entity>,\n ...args: Parameters<Entity['create']>\n ): Promise<Entity> {\n const [data, ...additionalArgs] = args;\n const entity = new this();\n await entity.create(\n data as CreateShape<BaseEntityWithId, Entity>,\n ...additionalArgs\n );\n return entity;\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 async update<Entity extends BaseEntityWithId>(\n this: Constructor<Entity>,\n ...args: Parameters<Entity['update']>\n ): Promise<Entity> {\n const [data, ...additionalArgs] = args;\n const entity = new this();\n await entity.update(\n data as UpdateShape<BaseEntity, Entity>,\n ...additionalArgs\n );\n return entity;\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 async map<Entity extends BaseEntity>(\n this: Constructor<Entity>,\n data: Partial<EntityDTO<FromEntityType<Entity>>>\n ): Promise<Entity> {\n const entity = new this();\n await entity.map(data);\n return entity;\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 async create(\n data: CreateShape<BaseEntityWithId, this>\n ): Promise<EntityDTO<this>> {\n Object.assign(this, transformRawDto(data, this));\n const entity = await wrap(this).init();\n if (!entity) {\n throw new Error('Entity not initialized');\n }\n return entity.toObject();\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 async update(\n data: UpdateShape<BaseEntityWithId, this>\n ): Promise<EntityDTO<this>> {\n const entity = wrap(this);\n entity.assign(\n stripUndefinedProperties(transformRawDto(data, this)) as Partial<\n EntityDTO<FromEntityType<this>>\n >\n );\n return entity.toObject();\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 async read(): Promise<EntityDTO<this>> {\n const entity = await wrap(this).init();\n if (!entity) {\n throw new Error('Entity not initialized');\n }\n return entity.toObject();\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 async map(data: Partial<EntityDTO<FromEntityType<this>>>): Promise<this> {\n const entity = await wrap(this).init();\n\n if (!entity) {\n throw new Error('Entity not initialized');\n }\n entity.assign(data);\n return entity;\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,SAAS,gCAAgC;AACzC;AAAA,EAIE,cAAc;AAAA,EACd;AAAA,OACK;;;ACPP,SAAqB,kBAAkB;;;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,WAAW,QAAQ,MAAM,KAAK;AAAA,IAC7D,OAAO;AACL,wBAAkB,GAAG,IAAI;AAAA,IAC3B;AAAA,EACF;AACA,SAAO;AACT;;;ADLO,IAAeA,cAAf,cAAkC,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAS1D,aAAa,UAER,MACc;AACjB,UAAM,CAAC,MAAM,GAAG,cAAc,IAAI;AAClC,UAAM,SAAS,IAAI,KAAK;AACxB,UAAM,OAAO;AAAA,MACX;AAAA,MACA,GAAG;AAAA,IACL;AACA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,aAAa,UAER,MACc;AACjB,UAAM,CAAC,MAAM,GAAG,cAAc,IAAI;AAClC,UAAM,SAAS,IAAI,KAAK;AACxB,UAAM,OAAO;AAAA,MACX;AAAA,MACA,GAAG;AAAA,IACL;AACA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,aAAa,IAEX,MACiB;AACjB,UAAM,SAAS,IAAI,KAAK;AACxB,UAAM,OAAO,IAAI,IAAI;AACrB,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,OACJ,MAC0B;AAC1B,WAAO,OAAO,MAAM,gBAAgB,MAAM,IAAI,CAAC;AAC/C,UAAM,SAAS,MAAM,KAAK,IAAI,EAAE,KAAK;AACrC,QAAI,CAAC,QAAQ;AACX,YAAM,IAAI,MAAM,wBAAwB;AAAA,IAC1C;AACA,WAAO,OAAO,SAAS;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,OACJ,MAC0B;AAC1B,UAAM,SAAS,KAAK,IAAI;AACxB,WAAO;AAAA,MACL,yBAAyB,gBAAgB,MAAM,IAAI,CAAC;AAAA,IAGtD;AACA,WAAO,OAAO,SAAS;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,OAAiC;AACrC,UAAM,SAAS,MAAM,KAAK,IAAI,EAAE,KAAK;AACrC,QAAI,CAAC,QAAQ;AACX,YAAM,IAAI,MAAM,wBAAwB;AAAA,IAC1C;AACA,WAAO,OAAO,SAAS;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,IAAI,MAA+D;AACvE,UAAM,SAAS,MAAM,KAAK,IAAI,EAAE,KAAK;AAErC,QAAI,CAAC,QAAQ;AACX,YAAM,IAAI,MAAM,wBAAwB;AAAA,IAC1C;AACA,WAAO,OAAO,IAAI;AAClB,WAAO;AAAA,EACT;AACF;;;AGzIO,SAAS,WAAc,OAAiC;AAC7D,SAAO;AAAA,IACL,aAAa;AAAA,IACb;AAAA,EACF;AACF;","names":["BaseEntity"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forklaunch/core",
3
- "version": "0.8.8",
3
+ "version": "0.9.2",
4
4
  "description": "forklaunch-js core package. Contains useful building blocks.",
5
5
  "homepage": "https://github.com/forklaunch/forklaunch-js#readme",
6
6
  "bugs": {
@@ -96,12 +96,12 @@
96
96
  "pino-pretty": "^13.0.0",
97
97
  "redis": "^5.5.5",
98
98
  "uuid": "^11.1.0",
99
- "@forklaunch/common": "0.3.6",
100
- "@forklaunch/validator": "0.6.6"
99
+ "@forklaunch/common": "0.3.7",
100
+ "@forklaunch/validator": "0.6.7"
101
101
  },
102
102
  "devDependencies": {
103
103
  "@eslint/js": "^9.28.0",
104
- "@scalar/express-api-reference": "^0.8.2",
104
+ "@scalar/express-api-reference": "^0.8.3",
105
105
  "@types/cors": "^2.8.18",
106
106
  "@types/jest": "^29.5.14",
107
107
  "@types/qs": "^6.14.0",