@forklaunch/core 0.8.7 → 0.8.9
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.d.mts +13 -1
- package/lib/src/http/index.d.ts +13 -1
- package/lib/src/http/index.js +55 -37
- package/lib/src/http/index.js.map +1 -1
- package/lib/src/http/index.mjs +41 -25
- package/lib/src/http/index.mjs.map +1 -1
- package/lib/src/services/index.d.mts +2 -2
- package/lib/src/services/index.d.ts +2 -2
- package/lib/src/services/index.js +6 -6
- package/lib/src/services/index.js.map +1 -1
- package/lib/src/services/index.mjs +6 -6
- package/lib/src/services/index.mjs.map +1 -1
- package/package.json +19 -19
- 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 -152
- package/lib/src/persistence/index.js.map +0 -1
- package/lib/src/persistence/index.mjs +0 -127
- package/lib/src/persistence/index.mjs.map +0 -1
@@ -1,120 +0,0 @@
|
|
1
|
-
import { Collection, BaseEntity as BaseEntity$1, Constructor, EntityDTO, FromEntityType } from '@mikro-orm/core';
|
2
|
-
|
3
|
-
/**
|
4
|
-
* Type representing a marked collection of items.
|
5
|
-
* A marked collection is a wrapper around an array that indicates it should be treated as a collection.
|
6
|
-
*
|
7
|
-
* @template T - The type of items in the collection
|
8
|
-
* @property {true} _collection - Marker indicating this is a collection
|
9
|
-
* @property {T[]} items - The array of items in the collection
|
10
|
-
*/
|
11
|
-
type MarkedCollection<T> = {
|
12
|
-
_collection: true;
|
13
|
-
items: T[];
|
14
|
-
};
|
15
|
-
|
16
|
-
/**
|
17
|
-
* Type representing the shape of data used to create an entity.
|
18
|
-
* Converts MikroORM collections to marked collections and omits entity properties.
|
19
|
-
*
|
20
|
-
* @template Entity - The base entity type
|
21
|
-
* @template T - The type being created
|
22
|
-
*/
|
23
|
-
type CreateShape<Entity, T> = {
|
24
|
-
[K in keyof Omit<T, keyof Entity>]: T[K] extends Collection<infer U> ? MarkedCollection<U> : T[K];
|
25
|
-
};
|
26
|
-
/**
|
27
|
-
* Type representing the shape of data used to update an entity.
|
28
|
-
* Similar to CreateShape but makes all properties optional and includes an id field.
|
29
|
-
*
|
30
|
-
* @template Entity - The base entity type
|
31
|
-
* @template T - The type being updated
|
32
|
-
*/
|
33
|
-
type UpdateShape<Entity, T> = Partial<CreateShape<Entity, T> & {
|
34
|
-
id: string;
|
35
|
-
}>;
|
36
|
-
|
37
|
-
/**
|
38
|
-
* Type representing a base entity with common fields.
|
39
|
-
* Extends BaseEntity with optional id, _id, createdAt, and updatedAt fields.
|
40
|
-
*/
|
41
|
-
type BaseEntityWithId = BaseEntity & {
|
42
|
-
id?: unknown;
|
43
|
-
_id?: unknown;
|
44
|
-
createdAt?: unknown;
|
45
|
-
updatedAt?: unknown;
|
46
|
-
};
|
47
|
-
/**
|
48
|
-
* Abstract base class for all entities in the system.
|
49
|
-
* Extends MikroORM's BaseEntity and provides common CRUD operations.
|
50
|
-
*/
|
51
|
-
declare abstract class BaseEntity extends BaseEntity$1 {
|
52
|
-
/**
|
53
|
-
* Static factory method to create a new entity instance.
|
54
|
-
*
|
55
|
-
* @template Entity - The type of entity being created
|
56
|
-
* @param {Constructor<Entity>} this - The entity constructor
|
57
|
-
* @param {...Parameters<Entity['create']>} args - Arguments for entity creation
|
58
|
-
* @returns {Entity} A new entity instance
|
59
|
-
*/
|
60
|
-
static create<Entity extends BaseEntityWithId>(this: Constructor<Entity>, ...args: Parameters<Entity['create']>): Entity;
|
61
|
-
/**
|
62
|
-
* Static method to update an entity instance.
|
63
|
-
*
|
64
|
-
* @template Entity - The type of entity being updated
|
65
|
-
* @param {Constructor<Entity>} this - The entity constructor
|
66
|
-
* @param {...Parameters<Entity['update']>} args - Arguments for entity update
|
67
|
-
* @returns {Entity} The updated entity instance
|
68
|
-
*/
|
69
|
-
static update<Entity extends BaseEntityWithId>(this: Constructor<Entity>, ...args: Parameters<Entity['update']>): Entity;
|
70
|
-
/**
|
71
|
-
* Static method to map data to an entity instance.
|
72
|
-
*
|
73
|
-
* @template Entity - The type of entity being mapped
|
74
|
-
* @param {Constructor<Entity>} this - The entity constructor
|
75
|
-
* @param {Partial<EntityDTO<FromEntityType<Entity>>>} data - The data to map
|
76
|
-
* @returns {Entity} A new entity instance with mapped data
|
77
|
-
*/
|
78
|
-
static map<Entity extends BaseEntity>(this: Constructor<Entity>, data: Partial<EntityDTO<FromEntityType<Entity>>>): Entity;
|
79
|
-
/**
|
80
|
-
* Creates a new entity instance with the provided data.
|
81
|
-
*
|
82
|
-
* @param {CreateShape<BaseEntityWithId, this>} data - The data to create the entity with
|
83
|
-
* @returns {this} The created entity instance
|
84
|
-
*/
|
85
|
-
create(data: CreateShape<BaseEntityWithId, this>): this;
|
86
|
-
/**
|
87
|
-
* Updates the entity instance with the provided data.
|
88
|
-
*
|
89
|
-
* @param {UpdateShape<BaseEntityWithId, this>} data - The data to update the entity with
|
90
|
-
* @returns {this} The updated entity instance
|
91
|
-
*/
|
92
|
-
update(data: UpdateShape<BaseEntityWithId, this>): this;
|
93
|
-
/**
|
94
|
-
* Reads the entity data as a plain object.
|
95
|
-
*
|
96
|
-
* @returns {EntityDTO<this> | this} The entity data as a plain object
|
97
|
-
*/
|
98
|
-
read(): EntityDTO<this> | this;
|
99
|
-
/**
|
100
|
-
* Maps data to the entity instance.
|
101
|
-
*
|
102
|
-
* @param {Partial<EntityDTO<FromEntityType<this>>>} data - The data to map
|
103
|
-
* @returns {this} The entity instance with mapped data
|
104
|
-
*/
|
105
|
-
map(data: Partial<EntityDTO<FromEntityType<this>>>): this;
|
106
|
-
}
|
107
|
-
|
108
|
-
/**
|
109
|
-
* Creates a marked collection from an array of items.
|
110
|
-
* A marked collection is a wrapper around an array that indicates it should be treated as a collection.
|
111
|
-
*
|
112
|
-
* @template T - The type of items in the collection
|
113
|
-
* @param {T[]} items - The array of items to wrap in a collection
|
114
|
-
* @returns {MarkedCollection<T>} A marked collection containing the provided items
|
115
|
-
* @example
|
116
|
-
* const users = collection([{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]);
|
117
|
-
*/
|
118
|
-
declare function collection<T>(items: T[]): MarkedCollection<T>;
|
119
|
-
|
120
|
-
export { BaseEntity, collection };
|
@@ -1,152 +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 create(...args) {
|
64
|
-
const [data, ...additionalArgs] = args;
|
65
|
-
return new this().create(
|
66
|
-
data,
|
67
|
-
...additionalArgs
|
68
|
-
);
|
69
|
-
}
|
70
|
-
/**
|
71
|
-
* Static method to update an entity instance.
|
72
|
-
*
|
73
|
-
* @template Entity - The type of entity being updated
|
74
|
-
* @param {Constructor<Entity>} this - The entity constructor
|
75
|
-
* @param {...Parameters<Entity['update']>} args - Arguments for entity update
|
76
|
-
* @returns {Entity} The updated entity instance
|
77
|
-
*/
|
78
|
-
static update(...args) {
|
79
|
-
const [data, ...additionalArgs] = args;
|
80
|
-
return new this().update(
|
81
|
-
data,
|
82
|
-
...additionalArgs
|
83
|
-
);
|
84
|
-
}
|
85
|
-
/**
|
86
|
-
* Static method to map data to an entity instance.
|
87
|
-
*
|
88
|
-
* @template Entity - The type of entity being mapped
|
89
|
-
* @param {Constructor<Entity>} this - The entity constructor
|
90
|
-
* @param {Partial<EntityDTO<FromEntityType<Entity>>>} data - The data to map
|
91
|
-
* @returns {Entity} A new entity instance with mapped data
|
92
|
-
*/
|
93
|
-
static map(data) {
|
94
|
-
return new this().map(data);
|
95
|
-
}
|
96
|
-
/**
|
97
|
-
* Creates a new entity instance with the provided data.
|
98
|
-
*
|
99
|
-
* @param {CreateShape<BaseEntityWithId, this>} data - The data to create the entity with
|
100
|
-
* @returns {this} The created entity instance
|
101
|
-
*/
|
102
|
-
create(data) {
|
103
|
-
return Object.assign(this, transformRawDto(data, this));
|
104
|
-
}
|
105
|
-
/**
|
106
|
-
* Updates the entity instance with the provided data.
|
107
|
-
*
|
108
|
-
* @param {UpdateShape<BaseEntityWithId, this>} data - The data to update the entity with
|
109
|
-
* @returns {this} The updated entity instance
|
110
|
-
*/
|
111
|
-
update(data) {
|
112
|
-
(0, import_core2.wrap)(this).assign(
|
113
|
-
(0, import_common.stripUndefinedProperties)(transformRawDto(data, this))
|
114
|
-
);
|
115
|
-
return this;
|
116
|
-
}
|
117
|
-
/**
|
118
|
-
* Reads the entity data as a plain object.
|
119
|
-
*
|
120
|
-
* @returns {EntityDTO<this> | this} The entity data as a plain object
|
121
|
-
*/
|
122
|
-
read() {
|
123
|
-
if (typeof (0, import_core2.wrap)(this).toPOJO === "function") {
|
124
|
-
return (0, import_core2.wrap)(this).toPOJO();
|
125
|
-
}
|
126
|
-
return this;
|
127
|
-
}
|
128
|
-
/**
|
129
|
-
* Maps data to the entity instance.
|
130
|
-
*
|
131
|
-
* @param {Partial<EntityDTO<FromEntityType<this>>>} data - The data to map
|
132
|
-
* @returns {this} The entity instance with mapped data
|
133
|
-
*/
|
134
|
-
map(data) {
|
135
|
-
(0, import_core2.wrap)(this).assign(data);
|
136
|
-
return this;
|
137
|
-
}
|
138
|
-
};
|
139
|
-
|
140
|
-
// src/persistence/collection.ts
|
141
|
-
function collection(items) {
|
142
|
-
return {
|
143
|
-
_collection: true,
|
144
|
-
items
|
145
|
-
};
|
146
|
-
}
|
147
|
-
// Annotate the CommonJS export names for ESM import in node:
|
148
|
-
0 && (module.exports = {
|
149
|
-
BaseEntity,
|
150
|
-
collection
|
151
|
-
});
|
152
|
-
//# sourceMappingURL=index.js.map
|
@@ -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 create<Entity extends BaseEntityWithId>(\n this: Constructor<Entity>,\n ...args: Parameters<Entity['create']>\n ): Entity {\n const [data, ...additionalArgs] = args;\n return new this().create(\n data as CreateShape<BaseEntityWithId, Entity>,\n ...additionalArgs\n );\n }\n\n /**\n * Static method to update an entity instance.\n *\n * @template Entity - The type of entity being updated\n * @param {Constructor<Entity>} this - The entity constructor\n * @param {...Parameters<Entity['update']>} args - Arguments for entity update\n * @returns {Entity} The updated entity instance\n */\n static update<Entity extends BaseEntityWithId>(\n this: Constructor<Entity>,\n ...args: Parameters<Entity['update']>\n ): Entity {\n const [data, ...additionalArgs] = args;\n return new this().update(\n data as UpdateShape<BaseEntity, Entity>,\n ...additionalArgs\n );\n }\n\n /**\n * Static method to map data to an entity instance.\n *\n * @template Entity - The type of entity being mapped\n * @param {Constructor<Entity>} this - The entity constructor\n * @param {Partial<EntityDTO<FromEntityType<Entity>>>} data - The data to map\n * @returns {Entity} A new entity instance with mapped data\n */\n static map<Entity extends BaseEntity>(\n this: Constructor<Entity>,\n data: Partial<EntityDTO<FromEntityType<Entity>>>\n ): Entity {\n return new this().map(data);\n }\n\n /**\n * Creates a new entity instance with the provided data.\n *\n * @param {CreateShape<BaseEntityWithId, this>} data - The data to create the entity with\n * @returns {this} The created entity instance\n */\n create(data: CreateShape<BaseEntityWithId, this>): this {\n return Object.assign(this, transformRawDto(data, this));\n }\n\n /**\n * Updates the entity instance with the provided data.\n *\n * @param {UpdateShape<BaseEntityWithId, this>} data - The data to update the entity with\n * @returns {this} The updated entity instance\n */\n update(data: UpdateShape<BaseEntityWithId, this>): this {\n wrap(this).assign(\n stripUndefinedProperties(transformRawDto(data, this)) as Partial<\n EntityDTO<FromEntityType<this>>\n >\n );\n return this;\n }\n\n /**\n * Reads the entity data as a plain object.\n *\n * @returns {EntityDTO<this> | this} The entity data as a plain object\n */\n read(): EntityDTO<this> | this {\n if (typeof wrap(this).toPOJO === 'function') {\n return wrap(this).toPOJO();\n }\n return this;\n }\n\n /**\n * Maps data to the entity instance.\n *\n * @param {Partial<EntityDTO<FromEntityType<this>>>} data - The data to map\n * @returns {this} The entity instance with mapped data\n */\n map(data: Partial<EntityDTO<FromEntityType<this>>>): this {\n wrap(this).assign(data);\n return this;\n }\n}\n","import { BaseEntity, Collection } from '@mikro-orm/core';\nimport { isMarkedCollection } from './guards/isMarkedCollection';\n\n/**\n * Transforms a raw DTO (Data Transfer Object) by converting marked collections into MikroORM collections.\n * This function is used to properly handle collections when converting between DTOs and entities.\n *\n * @template T - The type of the DTO being transformed\n * @template U - The type of the entity being transformed into\n * @param {T} data - The raw DTO data to transform\n * @param {U} entity - The entity instance to associate collections with\n * @returns {T} The transformed DTO with collections properly initialized\n * @example\n * const dto = { users: { _collection: true, items: [{ id: 1 }] } };\n * const entity = new UserEntity();\n * const transformed = transformRawDto(dto, entity);\n * // transformed.users is now a MikroORM Collection\n */\nexport function transformRawDto<\n T extends Record<string, unknown>,\n U extends BaseEntity\n>(data: T, entity: U): T {\n const transformedObject: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(data)) {\n if (isMarkedCollection<object>(value)) {\n transformedObject[key] = new Collection(entity, value.items);\n } else {\n transformedObject[key] = value;\n }\n }\n return transformedObject as T;\n}\n","import { MarkedCollection } from '../types/markedCollection.types';\n\n/**\n * Type guard function that checks if a value is a marked collection.\n *\n * @template T - The type of items in the collection\n * @param {unknown} value - The value to check\n * @returns {value is MarkedCollection<T>} True if the value is a marked collection, false otherwise\n * @example\n * const value = { _collection: true, items: [1, 2, 3] };\n * if (isMarkedCollection<number>(value)) {\n * // value is now typed as MarkedCollection<number>\n * console.log(value.items); // [1, 2, 3]\n * }\n */\nexport function isMarkedCollection<T>(\n value: unknown\n): value is MarkedCollection<T> {\n return (\n typeof value === 'object' &&\n value !== null &&\n '_collection' in value &&\n typeof value._collection === 'boolean' &&\n value._collection\n );\n}\n","import { MarkedCollection } from './types/markedCollection.types';\n\n/**\n * Creates a marked collection from an array of items.\n * A marked collection is a wrapper around an array that indicates it should be treated as a collection.\n *\n * @template T - The type of items in the collection\n * @param {T[]} items - The array of items to wrap in a collection\n * @returns {MarkedCollection<T>} A marked collection containing the provided items\n * @example\n * const users = collection([{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]);\n */\nexport function collection<T>(items: T[]): MarkedCollection<T> {\n return {\n _collection: true,\n items\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,oBAAAA;AAAA,EAAA;AAAA;AAAA;;;ACAA,oBAAyC;AACzC,IAAAC,eAMO;;;ACPP,kBAAuC;;;ACehC,SAAS,mBACd,OAC8B;AAC9B,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tD;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,127 +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 create(...args) {
|
40
|
-
const [data, ...additionalArgs] = args;
|
41
|
-
return new this().create(
|
42
|
-
data,
|
43
|
-
...additionalArgs
|
44
|
-
);
|
45
|
-
}
|
46
|
-
/**
|
47
|
-
* Static method to update an entity instance.
|
48
|
-
*
|
49
|
-
* @template Entity - The type of entity being updated
|
50
|
-
* @param {Constructor<Entity>} this - The entity constructor
|
51
|
-
* @param {...Parameters<Entity['update']>} args - Arguments for entity update
|
52
|
-
* @returns {Entity} The updated entity instance
|
53
|
-
*/
|
54
|
-
static update(...args) {
|
55
|
-
const [data, ...additionalArgs] = args;
|
56
|
-
return new this().update(
|
57
|
-
data,
|
58
|
-
...additionalArgs
|
59
|
-
);
|
60
|
-
}
|
61
|
-
/**
|
62
|
-
* Static method to map data to an entity instance.
|
63
|
-
*
|
64
|
-
* @template Entity - The type of entity being mapped
|
65
|
-
* @param {Constructor<Entity>} this - The entity constructor
|
66
|
-
* @param {Partial<EntityDTO<FromEntityType<Entity>>>} data - The data to map
|
67
|
-
* @returns {Entity} A new entity instance with mapped data
|
68
|
-
*/
|
69
|
-
static map(data) {
|
70
|
-
return new this().map(data);
|
71
|
-
}
|
72
|
-
/**
|
73
|
-
* Creates a new entity instance with the provided data.
|
74
|
-
*
|
75
|
-
* @param {CreateShape<BaseEntityWithId, this>} data - The data to create the entity with
|
76
|
-
* @returns {this} The created entity instance
|
77
|
-
*/
|
78
|
-
create(data) {
|
79
|
-
return Object.assign(this, transformRawDto(data, this));
|
80
|
-
}
|
81
|
-
/**
|
82
|
-
* Updates the entity instance with the provided data.
|
83
|
-
*
|
84
|
-
* @param {UpdateShape<BaseEntityWithId, this>} data - The data to update the entity with
|
85
|
-
* @returns {this} The updated entity instance
|
86
|
-
*/
|
87
|
-
update(data) {
|
88
|
-
wrap(this).assign(
|
89
|
-
stripUndefinedProperties(transformRawDto(data, this))
|
90
|
-
);
|
91
|
-
return this;
|
92
|
-
}
|
93
|
-
/**
|
94
|
-
* Reads the entity data as a plain object.
|
95
|
-
*
|
96
|
-
* @returns {EntityDTO<this> | this} The entity data as a plain object
|
97
|
-
*/
|
98
|
-
read() {
|
99
|
-
if (typeof wrap(this).toPOJO === "function") {
|
100
|
-
return wrap(this).toPOJO();
|
101
|
-
}
|
102
|
-
return this;
|
103
|
-
}
|
104
|
-
/**
|
105
|
-
* Maps data to the entity instance.
|
106
|
-
*
|
107
|
-
* @param {Partial<EntityDTO<FromEntityType<this>>>} data - The data to map
|
108
|
-
* @returns {this} The entity instance with mapped data
|
109
|
-
*/
|
110
|
-
map(data) {
|
111
|
-
wrap(this).assign(data);
|
112
|
-
return this;
|
113
|
-
}
|
114
|
-
};
|
115
|
-
|
116
|
-
// src/persistence/collection.ts
|
117
|
-
function collection(items) {
|
118
|
-
return {
|
119
|
-
_collection: true,
|
120
|
-
items
|
121
|
-
};
|
122
|
-
}
|
123
|
-
export {
|
124
|
-
BaseEntity2 as BaseEntity,
|
125
|
-
collection
|
126
|
-
};
|
127
|
-
//# 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 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;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,OAAO,UAEF,MACK;AACR,UAAM,CAAC,MAAM,GAAG,cAAc,IAAI;AAClC,WAAO,IAAI,KAAK,EA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