@forklaunch/core 0.9.2 → 0.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/src/http/index.js +19 -0
- package/lib/src/http/index.js.map +1 -1
- package/lib/src/http/index.mjs +19 -0
- package/lib/src/http/index.mjs.map +1 -1
- package/lib/src/mappers/index.d.mts +3 -2
- package/lib/src/mappers/index.d.ts +3 -2
- package/lib/src/mappers/index.js +2 -2
- package/lib/src/mappers/index.js.map +1 -1
- package/lib/src/mappers/index.mjs +2 -2
- package/lib/src/mappers/index.mjs.map +1 -1
- package/package.json +8 -14
- package/lib/src/persistence/index.d.mts +0 -120
- package/lib/src/persistence/index.d.ts +0 -120
- package/lib/src/persistence/index.js +0 -169
- package/lib/src/persistence/index.js.map +0 -1
- package/lib/src/persistence/index.mjs +0 -144
- package/lib/src/persistence/index.mjs.map +0 -1
@@ -1,169 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __defProp = Object.defineProperty;
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
-
var __export = (target, all) => {
|
7
|
-
for (var name in all)
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
-
};
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
-
for (let key of __getOwnPropNames(from))
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
-
}
|
16
|
-
return to;
|
17
|
-
};
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
-
|
20
|
-
// src/persistence/index.ts
|
21
|
-
var persistence_exports = {};
|
22
|
-
__export(persistence_exports, {
|
23
|
-
BaseEntity: () => BaseEntity2,
|
24
|
-
collection: () => collection
|
25
|
-
});
|
26
|
-
module.exports = __toCommonJS(persistence_exports);
|
27
|
-
|
28
|
-
// src/persistence/base.entity.ts
|
29
|
-
var import_common = require("@forklaunch/common");
|
30
|
-
var import_core2 = require("@mikro-orm/core");
|
31
|
-
|
32
|
-
// src/persistence/transformRawDto.ts
|
33
|
-
var import_core = require("@mikro-orm/core");
|
34
|
-
|
35
|
-
// src/persistence/guards/isMarkedCollection.ts
|
36
|
-
function isMarkedCollection(value) {
|
37
|
-
return typeof value === "object" && value !== null && "_collection" in value && typeof value._collection === "boolean" && value._collection;
|
38
|
-
}
|
39
|
-
|
40
|
-
// src/persistence/transformRawDto.ts
|
41
|
-
function transformRawDto(data, entity) {
|
42
|
-
const transformedObject = {};
|
43
|
-
for (const [key, value] of Object.entries(data)) {
|
44
|
-
if (isMarkedCollection(value)) {
|
45
|
-
transformedObject[key] = new import_core.Collection(entity, value.items);
|
46
|
-
} else {
|
47
|
-
transformedObject[key] = value;
|
48
|
-
}
|
49
|
-
}
|
50
|
-
return transformedObject;
|
51
|
-
}
|
52
|
-
|
53
|
-
// src/persistence/base.entity.ts
|
54
|
-
var BaseEntity2 = class extends import_core2.BaseEntity {
|
55
|
-
/**
|
56
|
-
* Static factory method to create a new entity instance.
|
57
|
-
*
|
58
|
-
* @template Entity - The type of entity being created
|
59
|
-
* @param {Constructor<Entity>} this - The entity constructor
|
60
|
-
* @param {...Parameters<Entity['create']>} args - Arguments for entity creation
|
61
|
-
* @returns {Entity} A new entity instance
|
62
|
-
*/
|
63
|
-
static async create(...args) {
|
64
|
-
const [data, ...additionalArgs] = args;
|
65
|
-
const entity = new this();
|
66
|
-
await entity.create(
|
67
|
-
data,
|
68
|
-
...additionalArgs
|
69
|
-
);
|
70
|
-
return entity;
|
71
|
-
}
|
72
|
-
/**
|
73
|
-
* Static method to update an entity instance.
|
74
|
-
*
|
75
|
-
* @template Entity - The type of entity being updated
|
76
|
-
* @param {Constructor<Entity>} this - The entity constructor
|
77
|
-
* @param {...Parameters<Entity['update']>} args - Arguments for entity update
|
78
|
-
* @returns {Entity} The updated entity instance
|
79
|
-
*/
|
80
|
-
static async update(...args) {
|
81
|
-
const [data, ...additionalArgs] = args;
|
82
|
-
const entity = new this();
|
83
|
-
await entity.update(
|
84
|
-
data,
|
85
|
-
...additionalArgs
|
86
|
-
);
|
87
|
-
return entity;
|
88
|
-
}
|
89
|
-
/**
|
90
|
-
* Static method to map data to an entity instance.
|
91
|
-
*
|
92
|
-
* @template Entity - The type of entity being mapped
|
93
|
-
* @param {Constructor<Entity>} this - The entity constructor
|
94
|
-
* @param {Partial<EntityDTO<FromEntityType<Entity>>>} data - The data to map
|
95
|
-
* @returns {Entity} A new entity instance with mapped data
|
96
|
-
*/
|
97
|
-
static async map(data) {
|
98
|
-
const entity = new this();
|
99
|
-
await entity.map(data);
|
100
|
-
return entity;
|
101
|
-
}
|
102
|
-
/**
|
103
|
-
* Creates a new entity instance with the provided data.
|
104
|
-
*
|
105
|
-
* @param {CreateShape<BaseEntityWithId, this>} data - The data to create the entity with
|
106
|
-
* @returns {this} The created entity instance
|
107
|
-
*/
|
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();
|
115
|
-
}
|
116
|
-
/**
|
117
|
-
* Updates the entity instance with the provided data.
|
118
|
-
*
|
119
|
-
* @param {UpdateShape<BaseEntityWithId, this>} data - The data to update the entity with
|
120
|
-
* @returns {this} The updated entity instance
|
121
|
-
*/
|
122
|
-
async update(data) {
|
123
|
-
const entity = (0, import_core2.wrap)(this);
|
124
|
-
entity.assign(
|
125
|
-
(0, import_common.stripUndefinedProperties)(transformRawDto(data, this))
|
126
|
-
);
|
127
|
-
return entity.toObject();
|
128
|
-
}
|
129
|
-
/**
|
130
|
-
* Reads the entity data as a plain object.
|
131
|
-
*
|
132
|
-
* @returns {EntityDTO<this> | this} The entity data as a plain object
|
133
|
-
*/
|
134
|
-
async read() {
|
135
|
-
const entity = await (0, import_core2.wrap)(this).init();
|
136
|
-
if (!entity) {
|
137
|
-
throw new Error("Entity not initialized");
|
138
|
-
}
|
139
|
-
return entity.toObject();
|
140
|
-
}
|
141
|
-
/**
|
142
|
-
* Maps data to the entity instance.
|
143
|
-
*
|
144
|
-
* @param {Partial<EntityDTO<FromEntityType<this>>>} data - The data to map
|
145
|
-
* @returns {this} The entity instance with mapped data
|
146
|
-
*/
|
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;
|
154
|
-
}
|
155
|
-
};
|
156
|
-
|
157
|
-
// src/persistence/collection.ts
|
158
|
-
function collection(items) {
|
159
|
-
return {
|
160
|
-
_collection: true,
|
161
|
-
items
|
162
|
-
};
|
163
|
-
}
|
164
|
-
// Annotate the CommonJS export names for ESM import in node:
|
165
|
-
0 && (module.exports = {
|
166
|
-
BaseEntity,
|
167
|
-
collection
|
168
|
-
});
|
169
|
-
//# sourceMappingURL=index.js.map
|
@@ -1 +0,0 @@
|
|
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,GAA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L,wCAAyB,gBAAgB,MAAM,IAAI,CAAC;AAAA,IAGtD;AACA,WAAO,OAAO,SAAS;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,OAAiC;AACrC,UAAM,SAAS,UAAM,mBAAK,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,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"]}
|
@@ -1,144 +0,0 @@
|
|
1
|
-
// src/persistence/base.entity.ts
|
2
|
-
import { stripUndefinedProperties } from "@forklaunch/common";
|
3
|
-
import {
|
4
|
-
BaseEntity as MikroOrmBaseEntity,
|
5
|
-
wrap
|
6
|
-
} from "@mikro-orm/core";
|
7
|
-
|
8
|
-
// src/persistence/transformRawDto.ts
|
9
|
-
import { Collection } from "@mikro-orm/core";
|
10
|
-
|
11
|
-
// src/persistence/guards/isMarkedCollection.ts
|
12
|
-
function isMarkedCollection(value) {
|
13
|
-
return typeof value === "object" && value !== null && "_collection" in value && typeof value._collection === "boolean" && value._collection;
|
14
|
-
}
|
15
|
-
|
16
|
-
// src/persistence/transformRawDto.ts
|
17
|
-
function transformRawDto(data, entity) {
|
18
|
-
const transformedObject = {};
|
19
|
-
for (const [key, value] of Object.entries(data)) {
|
20
|
-
if (isMarkedCollection(value)) {
|
21
|
-
transformedObject[key] = new Collection(entity, value.items);
|
22
|
-
} else {
|
23
|
-
transformedObject[key] = value;
|
24
|
-
}
|
25
|
-
}
|
26
|
-
return transformedObject;
|
27
|
-
}
|
28
|
-
|
29
|
-
// src/persistence/base.entity.ts
|
30
|
-
var BaseEntity2 = class extends MikroOrmBaseEntity {
|
31
|
-
/**
|
32
|
-
* Static factory method to create a new entity instance.
|
33
|
-
*
|
34
|
-
* @template Entity - The type of entity being created
|
35
|
-
* @param {Constructor<Entity>} this - The entity constructor
|
36
|
-
* @param {...Parameters<Entity['create']>} args - Arguments for entity creation
|
37
|
-
* @returns {Entity} A new entity instance
|
38
|
-
*/
|
39
|
-
static async create(...args) {
|
40
|
-
const [data, ...additionalArgs] = args;
|
41
|
-
const entity = new this();
|
42
|
-
await entity.create(
|
43
|
-
data,
|
44
|
-
...additionalArgs
|
45
|
-
);
|
46
|
-
return entity;
|
47
|
-
}
|
48
|
-
/**
|
49
|
-
* Static method to update an entity instance.
|
50
|
-
*
|
51
|
-
* @template Entity - The type of entity being updated
|
52
|
-
* @param {Constructor<Entity>} this - The entity constructor
|
53
|
-
* @param {...Parameters<Entity['update']>} args - Arguments for entity update
|
54
|
-
* @returns {Entity} The updated entity instance
|
55
|
-
*/
|
56
|
-
static async update(...args) {
|
57
|
-
const [data, ...additionalArgs] = args;
|
58
|
-
const entity = new this();
|
59
|
-
await entity.update(
|
60
|
-
data,
|
61
|
-
...additionalArgs
|
62
|
-
);
|
63
|
-
return entity;
|
64
|
-
}
|
65
|
-
/**
|
66
|
-
* Static method to map data to an entity instance.
|
67
|
-
*
|
68
|
-
* @template Entity - The type of entity being mapped
|
69
|
-
* @param {Constructor<Entity>} this - The entity constructor
|
70
|
-
* @param {Partial<EntityDTO<FromEntityType<Entity>>>} data - The data to map
|
71
|
-
* @returns {Entity} A new entity instance with mapped data
|
72
|
-
*/
|
73
|
-
static async map(data) {
|
74
|
-
const entity = new this();
|
75
|
-
await entity.map(data);
|
76
|
-
return entity;
|
77
|
-
}
|
78
|
-
/**
|
79
|
-
* Creates a new entity instance with the provided data.
|
80
|
-
*
|
81
|
-
* @param {CreateShape<BaseEntityWithId, this>} data - The data to create the entity with
|
82
|
-
* @returns {this} The created entity instance
|
83
|
-
*/
|
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();
|
91
|
-
}
|
92
|
-
/**
|
93
|
-
* Updates the entity instance with the provided data.
|
94
|
-
*
|
95
|
-
* @param {UpdateShape<BaseEntityWithId, this>} data - The data to update the entity with
|
96
|
-
* @returns {this} The updated entity instance
|
97
|
-
*/
|
98
|
-
async update(data) {
|
99
|
-
const entity = wrap(this);
|
100
|
-
entity.assign(
|
101
|
-
stripUndefinedProperties(transformRawDto(data, this))
|
102
|
-
);
|
103
|
-
return entity.toObject();
|
104
|
-
}
|
105
|
-
/**
|
106
|
-
* Reads the entity data as a plain object.
|
107
|
-
*
|
108
|
-
* @returns {EntityDTO<this> | this} The entity data as a plain object
|
109
|
-
*/
|
110
|
-
async read() {
|
111
|
-
const entity = await wrap(this).init();
|
112
|
-
if (!entity) {
|
113
|
-
throw new Error("Entity not initialized");
|
114
|
-
}
|
115
|
-
return entity.toObject();
|
116
|
-
}
|
117
|
-
/**
|
118
|
-
* Maps data to the entity instance.
|
119
|
-
*
|
120
|
-
* @param {Partial<EntityDTO<FromEntityType<this>>>} data - The data to map
|
121
|
-
* @returns {this} The entity instance with mapped data
|
122
|
-
*/
|
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;
|
130
|
-
}
|
131
|
-
};
|
132
|
-
|
133
|
-
// src/persistence/collection.ts
|
134
|
-
function collection(items) {
|
135
|
-
return {
|
136
|
-
_collection: true,
|
137
|
-
items
|
138
|
-
};
|
139
|
-
}
|
140
|
-
export {
|
141
|
-
BaseEntity2 as BaseEntity,
|
142
|
-
collection
|
143
|
-
};
|
144
|
-
//# sourceMappingURL=index.mjs.map
|
@@ -1 +0,0 @@
|
|
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,K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