@carno.js/orm 0.2.3

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.
Files changed (98) hide show
  1. package/LICENSE +674 -0
  2. package/dist/SqlBuilder.d.ts +103 -0
  3. package/dist/SqlBuilder.js +618 -0
  4. package/dist/cache/cache-key-generator.d.ts +13 -0
  5. package/dist/cache/cache-key-generator.js +66 -0
  6. package/dist/cache/query-cache-manager.d.ts +14 -0
  7. package/dist/cache/query-cache-manager.js +44 -0
  8. package/dist/common/email.vo.d.ts +4 -0
  9. package/dist/common/email.vo.js +11 -0
  10. package/dist/common/uuid.d.ts +4 -0
  11. package/dist/common/uuid.js +10 -0
  12. package/dist/common/value-object.d.ts +95 -0
  13. package/dist/common/value-object.js +99 -0
  14. package/dist/constants.d.ts +6 -0
  15. package/dist/constants.js +9 -0
  16. package/dist/decorators/computed.decorator.d.ts +1 -0
  17. package/dist/decorators/computed.decorator.js +12 -0
  18. package/dist/decorators/entity.decorator.d.ts +3 -0
  19. package/dist/decorators/entity.decorator.js +12 -0
  20. package/dist/decorators/enum.decorator.d.ts +2 -0
  21. package/dist/decorators/enum.decorator.js +16 -0
  22. package/dist/decorators/event-hook.decorator.d.ts +4 -0
  23. package/dist/decorators/event-hook.decorator.js +31 -0
  24. package/dist/decorators/index.decorator.d.ts +17 -0
  25. package/dist/decorators/index.decorator.js +36 -0
  26. package/dist/decorators/one-many.decorator.d.ts +6 -0
  27. package/dist/decorators/one-many.decorator.js +42 -0
  28. package/dist/decorators/primary-key.decorator.d.ts +2 -0
  29. package/dist/decorators/primary-key.decorator.js +8 -0
  30. package/dist/decorators/property.decorator.d.ts +24 -0
  31. package/dist/decorators/property.decorator.js +44 -0
  32. package/dist/decorators/unique.decorator.d.ts +9 -0
  33. package/dist/decorators/unique.decorator.js +44 -0
  34. package/dist/domain/base-entity.d.ts +57 -0
  35. package/dist/domain/base-entity.js +198 -0
  36. package/dist/domain/collection.d.ts +6 -0
  37. package/dist/domain/collection.js +15 -0
  38. package/dist/domain/entities.d.ts +49 -0
  39. package/dist/domain/entities.js +259 -0
  40. package/dist/domain/reference.d.ts +86 -0
  41. package/dist/domain/reference.js +86 -0
  42. package/dist/driver/bun-driver.base.d.ts +72 -0
  43. package/dist/driver/bun-driver.base.js +270 -0
  44. package/dist/driver/bun-mysql.driver.d.ts +53 -0
  45. package/dist/driver/bun-mysql.driver.js +256 -0
  46. package/dist/driver/bun-pg.driver.d.ts +52 -0
  47. package/dist/driver/bun-pg.driver.js +263 -0
  48. package/dist/driver/driver.interface.d.ts +333 -0
  49. package/dist/driver/driver.interface.js +2 -0
  50. package/dist/entry.d.ts +2 -0
  51. package/dist/entry.js +13 -0
  52. package/dist/identity-map/entity-key-generator.d.ts +10 -0
  53. package/dist/identity-map/entity-key-generator.js +45 -0
  54. package/dist/identity-map/entity-registry.d.ts +11 -0
  55. package/dist/identity-map/entity-registry.js +41 -0
  56. package/dist/identity-map/identity-map-context.d.ts +9 -0
  57. package/dist/identity-map/identity-map-context.js +22 -0
  58. package/dist/identity-map/identity-map-integration.d.ts +5 -0
  59. package/dist/identity-map/identity-map-integration.js +37 -0
  60. package/dist/identity-map/identity-map.d.ts +11 -0
  61. package/dist/identity-map/identity-map.js +35 -0
  62. package/dist/identity-map/index.d.ts +5 -0
  63. package/dist/identity-map/index.js +14 -0
  64. package/dist/index.d.ts +28 -0
  65. package/dist/index.js +48 -0
  66. package/dist/middleware/identity-map.middleware.d.ts +4 -0
  67. package/dist/middleware/identity-map.middleware.js +22 -0
  68. package/dist/orm-session-context.d.ts +16 -0
  69. package/dist/orm-session-context.js +22 -0
  70. package/dist/orm.d.ts +20 -0
  71. package/dist/orm.js +69 -0
  72. package/dist/orm.service.d.ts +13 -0
  73. package/dist/orm.service.js +361 -0
  74. package/dist/query/index-condition-builder.d.ts +41 -0
  75. package/dist/query/index-condition-builder.js +235 -0
  76. package/dist/query/model-transformer.d.ts +27 -0
  77. package/dist/query/model-transformer.js +201 -0
  78. package/dist/query/sql-column-manager.d.ts +28 -0
  79. package/dist/query/sql-column-manager.js +157 -0
  80. package/dist/query/sql-condition-builder.d.ts +51 -0
  81. package/dist/query/sql-condition-builder.js +264 -0
  82. package/dist/query/sql-join-manager.d.ts +39 -0
  83. package/dist/query/sql-join-manager.js +242 -0
  84. package/dist/query/sql-subquery-builder.d.ts +20 -0
  85. package/dist/query/sql-subquery-builder.js +119 -0
  86. package/dist/repository/Repository.d.ts +121 -0
  87. package/dist/repository/Repository.js +174 -0
  88. package/dist/testing/index.d.ts +1 -0
  89. package/dist/testing/index.js +17 -0
  90. package/dist/testing/with-database.d.ts +20 -0
  91. package/dist/testing/with-database.js +311 -0
  92. package/dist/transaction/transaction-context.d.ts +10 -0
  93. package/dist/transaction/transaction-context.js +19 -0
  94. package/dist/utils/value-processor.d.ts +14 -0
  95. package/dist/utils/value-processor.js +94 -0
  96. package/dist/utils.d.ts +3 -0
  97. package/dist/utils.js +24 -0
  98. package/package.json +59 -0
@@ -0,0 +1,333 @@
1
+ import { PropertyOptions } from "../decorators/property.decorator";
2
+ import { Collection } from "../domain/collection";
3
+ import { Reference } from "../domain/reference";
4
+ import { ValueObject } from "../common/value-object";
5
+ export interface DriverInterface {
6
+ connectionString: string;
7
+ readonly dbType: "postgres" | "mysql";
8
+ executeStatement(statement: Statement<any>): Promise<{
9
+ query: any;
10
+ startTime: number;
11
+ sql: string;
12
+ }>;
13
+ connect(): Promise<void>;
14
+ disconnect(): Promise<void>;
15
+ executeSql(s: string): Promise<any>;
16
+ snapshot(tableName: string, options: any): Promise<SnapshotTable | undefined>;
17
+ getCreateTableInstruction(schema: string | undefined, tableName: string, creates: ColDiff[]): string;
18
+ getAlterTableFkInstruction(schema: string | undefined, tableName: string, colDiff: ColDiff, fk: ForeignKeyInfo): string;
19
+ getCreateIndex(index: IndexStatement, schema: string | undefined, tableName: string): string;
20
+ getAddColumn(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff, colDiffInstructions: string[]): void;
21
+ getDropColumn(colDiffInstructions: string[], schema: string | undefined, tableName: string, colName: string): void;
22
+ getDropIndex(index: IndexStatement, schema: string | undefined, tableName: string): string;
23
+ getCreateUniqueConstraint(unique: UniqueStatement, schema: string | undefined, tableName: string): string;
24
+ getDropUniqueConstraint(unique: UniqueStatement, schema: string | undefined, tableName: string): string;
25
+ getAlterTableType(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
26
+ getAlterTableDefaultInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
27
+ getAlterTablePrimaryKeyInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
28
+ getDropConstraint(param: {
29
+ name: string;
30
+ }, schema: string | undefined, tableName: string): string;
31
+ getAddUniqueConstraint(schema: string | undefined, tableName: string, colName: string): string;
32
+ getAlterTableDropNullInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
33
+ getAlterTableDropNotNullInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
34
+ getAlterTableEnumInstruction(schema: string, tableName: string, colName: string, colDiff: ColDiff): string;
35
+ getDropTypeEnumInstruction(param: {
36
+ name: string;
37
+ }, schema: string | undefined, tableName: string): string;
38
+ transaction<T>(callback: (tx: any) => Promise<T>): Promise<T>;
39
+ }
40
+ export type ValueOrInstance<T> = T extends ValueObject<any, any> ? // @ts-ignore
41
+ T | T["value"] : NonNullable<T>;
42
+ export type SnapshotConstraintInfo = {
43
+ indexName: string;
44
+ consDef: string;
45
+ type: string;
46
+ };
47
+ export interface ConnectionSettings<T extends DriverInterface = DriverInterface> {
48
+ host?: string;
49
+ port?: number;
50
+ username?: string;
51
+ password?: string;
52
+ database?: string;
53
+ connectionString?: string;
54
+ max?: number;
55
+ idleTimeout?: number;
56
+ maxLifetime?: number;
57
+ connectionTimeout?: number;
58
+ ssl?: boolean;
59
+ driver: new (options: ConnectionSettings<T>) => T;
60
+ entities?: Function[] | string;
61
+ migrationPath?: string;
62
+ }
63
+ export type ConditionOperators<T, C> = {
64
+ $ne?: T;
65
+ $in?: T[];
66
+ $nin?: T[];
67
+ $like?: string;
68
+ $gt?: T;
69
+ $gte?: T;
70
+ $lt?: T;
71
+ $lte?: T;
72
+ $and?: Condition<C>[];
73
+ $or?: Condition<C>[];
74
+ };
75
+ export type Condition<T> = {
76
+ [P in keyof T]?: T[P] | ConditionOperators<T[P], T>;
77
+ };
78
+ export type InstanceOf<T> = {
79
+ [key in keyof T]: T[key];
80
+ };
81
+ export type ClassType<T = any> = {
82
+ [k: string]: T;
83
+ };
84
+ export interface EnumOptions<T> extends PropertyOptions {
85
+ items?: (number | string)[] | (() => ClassType);
86
+ array?: boolean;
87
+ }
88
+ export type JoinStatement<T> = {
89
+ type: "INNER" | "LEFT" | "RIGHT";
90
+ originalEntity?: Function;
91
+ originTable: string;
92
+ originSchema: string;
93
+ originAlias: string;
94
+ joinTable: string;
95
+ joinSchema: string;
96
+ joinAlias: string;
97
+ joinEntity?: Function;
98
+ joinWhere?: string;
99
+ joinProperty: string;
100
+ on: string;
101
+ propertyKey: string | symbol;
102
+ hooks?: {
103
+ type: string;
104
+ propertyName: string;
105
+ }[];
106
+ };
107
+ export type Statement<T> = {
108
+ statement?: "select" | "insert" | "update" | "delete" | "count";
109
+ table?: string;
110
+ alias?: string;
111
+ customSchema?: string;
112
+ columns?: string[];
113
+ join?: JoinStatement<T>[];
114
+ selectJoin?: Statement<T>[];
115
+ strategy?: "select" | "joined";
116
+ where?: string;
117
+ values?: any;
118
+ groupBy?: string[];
119
+ orderBy?: string[];
120
+ limit?: number;
121
+ offset?: number;
122
+ hooks?: {
123
+ type: string;
124
+ propertyName: string;
125
+ }[];
126
+ instance?: InstanceOf<T>;
127
+ cache?: boolean | number | Date;
128
+ joinProperty?: string;
129
+ fkKey?: string;
130
+ primaryKey?: string;
131
+ originAlias?: string;
132
+ originProperty?: string;
133
+ joinEntity?: Function;
134
+ originEntity?: Function;
135
+ };
136
+ export type SnapshotTable = {
137
+ tableName: string;
138
+ schema?: string;
139
+ columns: ColumnsInfo[];
140
+ indexes: SnapshotIndexInfo[];
141
+ uniques?: SnapshotUniqueInfo[];
142
+ foreignKeys?: ForeignKeyInfo[];
143
+ };
144
+ export type SnapshotIndexInfo = {
145
+ table: string;
146
+ indexName: string;
147
+ columnName: string;
148
+ where?: string;
149
+ };
150
+ export type SnapshotUniqueInfo = {
151
+ table: string;
152
+ uniqueName: string;
153
+ columnName: string;
154
+ };
155
+ export type IndexStatement = {
156
+ name: string;
157
+ properties?: string[];
158
+ where?: string;
159
+ };
160
+ export type UniqueStatement = {
161
+ name: string;
162
+ properties?: string[];
163
+ };
164
+ export type ForeignKeyInfo = {
165
+ referencedTableName: string;
166
+ referencedColumnName: string;
167
+ };
168
+ export type ColumnsInfo = {
169
+ name: string;
170
+ type: string;
171
+ nullable?: boolean;
172
+ default?: string | null;
173
+ primary?: boolean;
174
+ unique?: boolean;
175
+ autoIncrement?: boolean;
176
+ length?: number;
177
+ isEnum?: boolean;
178
+ precision?: number;
179
+ scale?: number;
180
+ isDecimal?: boolean;
181
+ enumItems?: string[] | number[] | "__AUTO_DETECT__";
182
+ foreignKeys?: ForeignKeyInfo[];
183
+ };
184
+ export type SqlActionType = "CREATE" | "DELETE" | "ALTER" | "INDEX";
185
+ export type ColDiff = {
186
+ actionType: SqlActionType;
187
+ colName: string;
188
+ colType?: string;
189
+ colLength?: number;
190
+ indexTables?: {
191
+ name: string;
192
+ properties?: string[];
193
+ }[];
194
+ colChanges?: {
195
+ default?: string | null;
196
+ primary?: boolean;
197
+ unique?: boolean;
198
+ nullable?: boolean;
199
+ autoIncrement?: boolean;
200
+ enumItems?: string[] | number[];
201
+ enumModified?: boolean;
202
+ precision?: number;
203
+ scale?: number;
204
+ foreignKeys?: ForeignKeyInfo[];
205
+ };
206
+ };
207
+ export type TableDiff = {
208
+ tableName: string;
209
+ schema?: string;
210
+ newTable?: boolean;
211
+ colDiffs: ColDiff[];
212
+ };
213
+ export declare const PrimaryKeyType: unique symbol;
214
+ export declare const PrimaryKeyProp: unique symbol;
215
+ type ReadonlyPrimary<T> = T extends any[] ? Readonly<T> : T;
216
+ export type Primary<T> = T extends {
217
+ [PrimaryKeyType]?: infer PK;
218
+ } ? ReadonlyPrimary<PK> : T extends {
219
+ _id?: infer PK;
220
+ } ? ReadonlyPrimary<PK> | string : T extends {
221
+ uuid?: infer PK;
222
+ } ? ReadonlyPrimary<PK> : T extends {
223
+ id?: infer PK;
224
+ } ? ReadonlyPrimary<PK> : never;
225
+ export type PrimaryProperty<T> = T extends {
226
+ [PrimaryKeyProp]?: infer PK;
227
+ } ? PK : T extends {
228
+ _id?: any;
229
+ } ? "_id" | string : T extends {
230
+ uuid?: any;
231
+ } ? "uuid" : T extends {
232
+ id?: any;
233
+ } ? "id" : never;
234
+ export type IPrimaryKeyValue = number | string | bigint | Date | {
235
+ toHexString(): string;
236
+ };
237
+ export type IPrimaryKey<T extends IPrimaryKeyValue = IPrimaryKeyValue> = T;
238
+ export type OperatorMap<T> = {
239
+ $and?: Query<T>[];
240
+ $or?: Query<T>[];
241
+ $eq?: ExpandScalar<T> | ExpandScalar<T>[];
242
+ $ne?: ExpandScalar<T>;
243
+ $in?: ExpandScalar<T>[];
244
+ $nin?: ExpandScalar<T>[];
245
+ $not?: Query<T>;
246
+ $gt?: ExpandScalar<T>;
247
+ $gte?: ExpandScalar<T>;
248
+ $lt?: ExpandScalar<T>;
249
+ $lte?: ExpandScalar<T>;
250
+ $like?: string;
251
+ $exists?: FilterQuery<ExpandProperty<T>>;
252
+ $nexists?: FilterQuery<ExpandProperty<T>>;
253
+ };
254
+ export type ExcludeFunctions<T, K extends keyof T> = T[K] extends Function ? never : K extends symbol ? never : K;
255
+ export type Scalar = boolean | number | string | bigint | symbol | Date | RegExp | Uint8Array | {
256
+ toHexString(): string;
257
+ };
258
+ export type ExpandProperty<T> = T extends Reference<infer U> ? NonNullable<U> : T extends Collection<infer U, any> ? NonNullable<U> : T extends (infer U)[] ? NonNullable<U> : NonNullable<T>;
259
+ export type ExpandScalar<T> = null | (T extends string ? T | RegExp : T extends Date ? Date | string : T);
260
+ type ExpandObject<T> = T extends object ? T extends Scalar ? never : {
261
+ -readonly [K in keyof T as ExcludeFunctions<T, K>]?: Query<ExpandProperty<T[K]>> | FilterValue<ExpandProperty<T[K]>> | null;
262
+ } : never;
263
+ export type EntityProps<T> = {
264
+ -readonly [K in keyof T as ExcludeFunctions<T, K>]?: T[K];
265
+ };
266
+ export type Query<T> = T extends object ? T extends Scalar ? never : FilterQuery<T> : FilterValue<T>;
267
+ export type FilterValue2<T> = T | ExpandScalar<T> | Primary<T>;
268
+ export type FilterValue<T> = OperatorMap<FilterValue2<T>> | FilterValue2<T> | FilterValue2<T>[] | null;
269
+ export type EntityClass<T> = Function & {
270
+ prototype: T;
271
+ };
272
+ export type EntityName<T> = string | EntityClass<T> | {
273
+ name: string;
274
+ };
275
+ export type ObjectQuery<T> = ExpandObject<T> & OperatorMap<T>;
276
+ export type FilterQuery<T> = ObjectQuery<T> | NonNullable<ExpandScalar<Primary<T>>> | NonNullable<EntityProps<T> & OperatorMap<T>> | FilterQuery<T>[];
277
+ export type Relationship<T> = {
278
+ isRelation?: boolean;
279
+ relation: "one-to-many" | "many-to-one";
280
+ type: Function;
281
+ fkKey?: (string & keyof T) | ((e: T) => any);
282
+ entity: () => EntityName<T>;
283
+ originalEntity?: EntityName<T>;
284
+ propertyKey: string | symbol;
285
+ columnName: string;
286
+ } & Partial<PropertyOptions>;
287
+ export type Cast<T, R> = T extends R ? T : R;
288
+ export type IsUnknown<T> = T extends unknown ? unknown extends T ? true : never : never;
289
+ type Loadable<T extends object> = Collection<T, any> | Reference<T> | readonly T[];
290
+ type ExtractType<T> = T extends Loadable<infer U> ? U : T;
291
+ type StringKeys<T, E extends string = never> = T extends Collection<any, any> ? `${Exclude<keyof ExtractType<T> | E, symbol>}` : T extends Reference<any> ? `${Exclude<keyof ExtractType<T> | E, symbol>}` : T extends object ? `${Exclude<keyof ExtractType<T> | E, symbol>}` : never;
292
+ type Defined<T> = Exclude<T, null | undefined>;
293
+ type GetStringKey<T, K extends StringKeys<T, string>, E extends string> = K extends keyof T ? ExtractType<T[K]> : K extends E ? keyof T : never;
294
+ type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
295
+ export type AutoPath<O, P extends string, E extends string = never, D extends Prev[number] = 5> = [D] extends [never] ? string : P extends any ? (P & `${string}.` extends never ? P : P & `${string}.`) extends infer Q ? Q extends `${infer A}.${infer B}` ? A extends StringKeys<O, E> ? `${A}.${AutoPath<Defined<GetStringKey<O, A, E>>, B, E, Prev[D]>}` : never : Q extends StringKeys<O, E> ? (Defined<GetStringKey<O, Q, E>> extends unknown ? Exclude<P, `${string}.`> : never) | (StringKeys<Defined<GetStringKey<O, Q, E>>, E> extends never ? never : `${Q}.`) : keyof ExtractType<O> : never : never;
296
+ export declare enum QueryOrder {
297
+ ASC = "ASC",
298
+ ASC_NULLS_LAST = "ASC NULLS LAST",
299
+ ASC_NULLS_FIRST = "ASC NULLS FIRST",
300
+ DESC = "DESC",
301
+ DESC_NULLS_LAST = "DESC NULLS LAST",
302
+ DESC_NULLS_FIRST = "DESC NULLS FIRST",
303
+ asc = "asc",
304
+ asc_nulls_last = "asc nulls last",
305
+ asc_nulls_first = "asc nulls first",
306
+ desc = "desc",
307
+ desc_nulls_last = "desc nulls last",
308
+ desc_nulls_first = "desc nulls first"
309
+ }
310
+ export declare enum QueryOrderNumeric {
311
+ ASC = 1,
312
+ DESC = -1
313
+ }
314
+ export type QueryOrderKeysFlat = QueryOrder | QueryOrderNumeric | keyof typeof QueryOrder;
315
+ export type QueryOrderKeys<T> = QueryOrderKeysFlat | QueryOrderMap<T>;
316
+ export type QueryOrderMap<T> = {
317
+ [K in keyof T as ExcludeFunctions<T, K>]?: QueryOrderKeys<ExpandProperty<T[K]>>;
318
+ };
319
+ export type EntityField<T, P extends string = never> = AutoPath<T, P, "*">;
320
+ export interface FindOptions<T, P extends string = never> {
321
+ load?: readonly AutoPath<T, P>[];
322
+ orderBy?: (QueryOrderMap<T> & {
323
+ 0?: never;
324
+ }) | QueryOrderMap<T>[];
325
+ cache?: boolean | number | Date;
326
+ limit?: number;
327
+ offset?: number;
328
+ fields?: readonly EntityField<T, P>[];
329
+ schema?: string;
330
+ loadStrategy?: "select" | "joined";
331
+ }
332
+ export type FindOneOption<T, P extends string = never> = Omit<FindOptions<T, P>, "limit" | "offset">;
333
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ import { Carno } from '@carno.js/core';
2
+ export declare const CarnoOrm: Carno;
package/dist/entry.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CarnoOrm = void 0;
4
+ const core_1 = require("@carno.js/core");
5
+ const orm_1 = require("./orm");
6
+ const orm_service_1 = require("./orm.service");
7
+ const entities_1 = require("./domain/entities");
8
+ const identity_map_middleware_1 = require("./middleware/identity-map.middleware");
9
+ exports.CarnoOrm = new core_1.Carno({
10
+ exports: [orm_1.Orm, orm_service_1.OrmService, entities_1.EntityStorage, identity_map_middleware_1.IdentityMapMiddleware],
11
+ providers: [identity_map_middleware_1.IdentityMapMiddleware],
12
+ globalMiddlewares: [identity_map_middleware_1.IdentityMapMiddleware],
13
+ });
@@ -0,0 +1,10 @@
1
+ export declare class EntityKeyGenerator {
2
+ private entityStorage;
3
+ constructor();
4
+ generate(entityClass: Function, pk: any): string;
5
+ generateForEntity(entity: any): string;
6
+ extractPrimaryKey(entity: any): any;
7
+ private getPrimaryKeyName;
8
+ private serializePrimaryKey;
9
+ private getClassName;
10
+ }
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EntityKeyGenerator = void 0;
4
+ const entities_1 = require("../domain/entities");
5
+ class EntityKeyGenerator {
6
+ constructor() {
7
+ this.entityStorage = entities_1.EntityStorage.getInstance();
8
+ }
9
+ generate(entityClass, pk) {
10
+ const className = this.getClassName(entityClass);
11
+ const keyValue = this.serializePrimaryKey(pk);
12
+ return `${className}:${keyValue}`;
13
+ }
14
+ generateForEntity(entity) {
15
+ const pk = this.extractPrimaryKey(entity);
16
+ return this.generate(entity.constructor, pk);
17
+ }
18
+ extractPrimaryKey(entity) {
19
+ const pkName = this.getPrimaryKeyName(entity.constructor);
20
+ return entity[pkName];
21
+ }
22
+ getPrimaryKeyName(entityClass) {
23
+ const options = this.entityStorage.get(entityClass);
24
+ if (!options) {
25
+ return 'id';
26
+ }
27
+ for (const prop in options.properties) {
28
+ const property = options.properties[prop];
29
+ if (property.options.isPrimary) {
30
+ return prop;
31
+ }
32
+ }
33
+ return 'id';
34
+ }
35
+ serializePrimaryKey(pk) {
36
+ if (Array.isArray(pk)) {
37
+ return pk.join(':');
38
+ }
39
+ return String(pk);
40
+ }
41
+ getClassName(entityClass) {
42
+ return entityClass.name;
43
+ }
44
+ }
45
+ exports.EntityKeyGenerator = EntityKeyGenerator;
@@ -0,0 +1,11 @@
1
+ export declare class EntityRegistry {
2
+ private store;
3
+ private finalizationRegistry;
4
+ constructor();
5
+ private setupFinalizationRegistry;
6
+ get<T>(key: string): T | undefined;
7
+ set<T extends object>(key: string, entity: T): void;
8
+ has(key: string): boolean;
9
+ remove(key: string): void;
10
+ clear(): void;
11
+ }
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EntityRegistry = void 0;
4
+ class EntityRegistry {
5
+ constructor() {
6
+ this.store = new Map();
7
+ this.setupFinalizationRegistry();
8
+ }
9
+ setupFinalizationRegistry() {
10
+ this.finalizationRegistry = new FinalizationRegistry((key) => {
11
+ this.store.delete(key);
12
+ });
13
+ }
14
+ get(key) {
15
+ const weakRef = this.store.get(key);
16
+ if (!weakRef) {
17
+ return undefined;
18
+ }
19
+ const entity = weakRef.deref();
20
+ if (!entity) {
21
+ this.store.delete(key);
22
+ return undefined;
23
+ }
24
+ return entity;
25
+ }
26
+ set(key, entity) {
27
+ const weakRef = new WeakRef(entity);
28
+ this.store.set(key, weakRef);
29
+ this.finalizationRegistry.register(entity, key, entity);
30
+ }
31
+ has(key) {
32
+ return this.get(key) !== undefined;
33
+ }
34
+ remove(key) {
35
+ this.store.delete(key);
36
+ }
37
+ clear() {
38
+ this.store.clear();
39
+ }
40
+ }
41
+ exports.EntityRegistry = EntityRegistry;
@@ -0,0 +1,9 @@
1
+ import { IdentityMap } from './identity-map';
2
+ export declare class IdentityMapContext {
3
+ private storage;
4
+ constructor();
5
+ run<T>(callback: () => Promise<T>): Promise<T>;
6
+ getIdentityMap(): IdentityMap | undefined;
7
+ hasContext(): boolean;
8
+ }
9
+ export declare const identityMapContext: IdentityMapContext;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.identityMapContext = exports.IdentityMapContext = void 0;
4
+ const async_hooks_1 = require("async_hooks");
5
+ const identity_map_1 = require("./identity-map");
6
+ class IdentityMapContext {
7
+ constructor() {
8
+ this.storage = new async_hooks_1.AsyncLocalStorage();
9
+ }
10
+ run(callback) {
11
+ const identityMap = new identity_map_1.IdentityMap();
12
+ return this.storage.run(identityMap, callback);
13
+ }
14
+ getIdentityMap() {
15
+ return this.storage.getStore();
16
+ }
17
+ hasContext() {
18
+ return this.storage.getStore() !== undefined;
19
+ }
20
+ }
21
+ exports.IdentityMapContext = IdentityMapContext;
22
+ exports.identityMapContext = new IdentityMapContext();
@@ -0,0 +1,5 @@
1
+ export declare class IdentityMapIntegration {
2
+ static getOrCreateInstance<T>(model: new () => T, primaryKey: any, factory: () => T): T;
3
+ static registerEntity<T extends object>(entity: T): void;
4
+ static getEntity<T>(entityClass: new () => T, primaryKey: any): T | undefined;
5
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IdentityMapIntegration = void 0;
4
+ const identity_map_context_1 = require("./identity-map-context");
5
+ class IdentityMapIntegration {
6
+ static getOrCreateInstance(model, primaryKey, factory) {
7
+ const identityMap = identity_map_context_1.identityMapContext.getIdentityMap();
8
+ if (!identityMap) {
9
+ return factory();
10
+ }
11
+ if (primaryKey === undefined || primaryKey === null) {
12
+ return factory();
13
+ }
14
+ const cached = identityMap.get(model, primaryKey);
15
+ if (cached) {
16
+ return cached;
17
+ }
18
+ const instance = factory();
19
+ identityMap.setByKey(model, primaryKey, instance);
20
+ return instance;
21
+ }
22
+ static registerEntity(entity) {
23
+ const identityMap = identity_map_context_1.identityMapContext.getIdentityMap();
24
+ if (!identityMap) {
25
+ return;
26
+ }
27
+ identityMap.set(entity);
28
+ }
29
+ static getEntity(entityClass, primaryKey) {
30
+ const identityMap = identity_map_context_1.identityMapContext.getIdentityMap();
31
+ if (!identityMap) {
32
+ return undefined;
33
+ }
34
+ return identityMap.get(entityClass, primaryKey);
35
+ }
36
+ }
37
+ exports.IdentityMapIntegration = IdentityMapIntegration;
@@ -0,0 +1,11 @@
1
+ export declare class IdentityMap {
2
+ private registry;
3
+ private keyGenerator;
4
+ constructor();
5
+ get<T>(entityClass: Function, pk: any): T | undefined;
6
+ set<T extends object>(entity: T): void;
7
+ setByKey<T extends object>(entityClass: Function, pk: any, entity: T): void;
8
+ has(entityClass: Function, pk: any): boolean;
9
+ remove(entityClass: Function, pk: any): void;
10
+ clear(): void;
11
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IdentityMap = void 0;
4
+ const entity_key_generator_1 = require("./entity-key-generator");
5
+ const entity_registry_1 = require("./entity-registry");
6
+ class IdentityMap {
7
+ constructor() {
8
+ this.registry = new entity_registry_1.EntityRegistry();
9
+ this.keyGenerator = new entity_key_generator_1.EntityKeyGenerator();
10
+ }
11
+ get(entityClass, pk) {
12
+ const key = this.keyGenerator.generate(entityClass, pk);
13
+ return this.registry.get(key);
14
+ }
15
+ set(entity) {
16
+ const key = this.keyGenerator.generateForEntity(entity);
17
+ this.registry.set(key, entity);
18
+ }
19
+ setByKey(entityClass, pk, entity) {
20
+ const key = this.keyGenerator.generate(entityClass, pk);
21
+ this.registry.set(key, entity);
22
+ }
23
+ has(entityClass, pk) {
24
+ const key = this.keyGenerator.generate(entityClass, pk);
25
+ return this.registry.has(key);
26
+ }
27
+ remove(entityClass, pk) {
28
+ const key = this.keyGenerator.generate(entityClass, pk);
29
+ this.registry.remove(key);
30
+ }
31
+ clear() {
32
+ this.registry.clear();
33
+ }
34
+ }
35
+ exports.IdentityMap = IdentityMap;
@@ -0,0 +1,5 @@
1
+ export { IdentityMap } from './identity-map';
2
+ export { EntityKeyGenerator } from './entity-key-generator';
3
+ export { EntityRegistry } from './entity-registry';
4
+ export { IdentityMapContext, identityMapContext } from './identity-map-context';
5
+ export { IdentityMapIntegration } from './identity-map-integration';
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IdentityMapIntegration = exports.identityMapContext = exports.IdentityMapContext = exports.EntityRegistry = exports.EntityKeyGenerator = exports.IdentityMap = void 0;
4
+ var identity_map_1 = require("./identity-map");
5
+ Object.defineProperty(exports, "IdentityMap", { enumerable: true, get: function () { return identity_map_1.IdentityMap; } });
6
+ var entity_key_generator_1 = require("./entity-key-generator");
7
+ Object.defineProperty(exports, "EntityKeyGenerator", { enumerable: true, get: function () { return entity_key_generator_1.EntityKeyGenerator; } });
8
+ var entity_registry_1 = require("./entity-registry");
9
+ Object.defineProperty(exports, "EntityRegistry", { enumerable: true, get: function () { return entity_registry_1.EntityRegistry; } });
10
+ var identity_map_context_1 = require("./identity-map-context");
11
+ Object.defineProperty(exports, "IdentityMapContext", { enumerable: true, get: function () { return identity_map_context_1.IdentityMapContext; } });
12
+ Object.defineProperty(exports, "identityMapContext", { enumerable: true, get: function () { return identity_map_context_1.identityMapContext; } });
13
+ var identity_map_integration_1 = require("./identity-map-integration");
14
+ Object.defineProperty(exports, "IdentityMapIntegration", { enumerable: true, get: function () { return identity_map_integration_1.IdentityMapIntegration; } });
@@ -0,0 +1,28 @@
1
+ export * from './decorators/entity.decorator';
2
+ export * from './decorators/property.decorator';
3
+ export * from './decorators/primary-key.decorator';
4
+ export * from './decorators/one-many.decorator';
5
+ export * from './decorators/index.decorator';
6
+ export * from './decorators/unique.decorator';
7
+ export * from './decorators/event-hook.decorator';
8
+ export * from './decorators/enum.decorator';
9
+ export * from './decorators/computed.decorator';
10
+ export * from './orm';
11
+ export * from './orm.service';
12
+ export * from './domain/base-entity';
13
+ export * from './domain/reference';
14
+ export type { Ref } from './domain/reference';
15
+ export { EntityStorage } from './domain/entities';
16
+ export * from './driver/bun-pg.driver';
17
+ export * from './driver/bun-mysql.driver';
18
+ export * from './driver/bun-driver.base';
19
+ export * from './utils';
20
+ export * from './driver/driver.interface';
21
+ export * from './entry';
22
+ export * from './common/value-object';
23
+ export * from './common/email.vo';
24
+ export * from './common/uuid';
25
+ export * from './repository/Repository';
26
+ export { transactionContext } from './transaction/transaction-context';
27
+ export { IdentityMapMiddleware } from './middleware/identity-map.middleware';
28
+ export { identityMapContext } from './identity-map';