metal-orm 1.0.15 → 1.0.16

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 (43) hide show
  1. package/README.md +34 -27
  2. package/dist/decorators/index.cjs +339 -153
  3. package/dist/decorators/index.cjs.map +1 -1
  4. package/dist/decorators/index.d.cts +1 -5
  5. package/dist/decorators/index.d.ts +1 -5
  6. package/dist/decorators/index.js +339 -153
  7. package/dist/decorators/index.js.map +1 -1
  8. package/dist/index.cjs +838 -484
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.cts +17 -14
  11. package/dist/index.d.ts +17 -14
  12. package/dist/index.js +833 -483
  13. package/dist/index.js.map +1 -1
  14. package/dist/{select-Bkv8g8u_.d.cts → select-BKZrMRCQ.d.cts} +363 -28
  15. package/dist/{select-Bkv8g8u_.d.ts → select-BKZrMRCQ.d.ts} +363 -28
  16. package/package.json +1 -1
  17. package/src/codegen/naming-strategy.ts +64 -0
  18. package/src/codegen/typescript.ts +48 -53
  19. package/src/core/ddl/schema-generator.ts +3 -2
  20. package/src/core/ddl/schema-introspect.ts +1 -1
  21. package/src/core/dialect/abstract.ts +28 -0
  22. package/src/decorators/column.ts +13 -4
  23. package/src/index.ts +8 -1
  24. package/src/orm/entity-context.ts +30 -0
  25. package/src/orm/entity-meta.ts +2 -2
  26. package/src/orm/entity-metadata.ts +1 -6
  27. package/src/orm/entity.ts +88 -88
  28. package/src/orm/execute.ts +42 -25
  29. package/src/orm/execution-context.ts +12 -0
  30. package/src/orm/hydration-context.ts +14 -0
  31. package/src/orm/identity-map.ts +4 -0
  32. package/src/orm/interceptor-pipeline.ts +29 -0
  33. package/src/orm/lazy-batch.ts +6 -6
  34. package/src/orm/orm-session.ts +234 -0
  35. package/src/orm/orm.ts +58 -0
  36. package/src/orm/relation-change-processor.ts +5 -1
  37. package/src/orm/relations/belongs-to.ts +45 -44
  38. package/src/orm/relations/has-many.ts +44 -43
  39. package/src/orm/relations/has-one.ts +140 -139
  40. package/src/orm/relations/many-to-many.ts +46 -45
  41. package/src/orm/unit-of-work.ts +6 -1
  42. package/src/query-builder/select.ts +509 -3
  43. package/src/orm/orm-context.ts +0 -159
@@ -1,159 +0,0 @@
1
- import type { Dialect } from '../core/dialect/abstract.js';
2
- import type { RelationDef } from '../schema/relation.js';
3
- import type { TableDef } from '../schema/table.js';
4
- import type { DbExecutor, QueryResult } from '../core/execution/db-executor.js';
5
- import { DomainEventBus, DomainEventHandler as DomainEventHandlerFn, addDomainEvent } from './domain-event-bus.js';
6
- import { IdentityMap } from './identity-map.js';
7
- import { RelationChangeProcessor } from './relation-change-processor.js';
8
- import { runInTransaction } from './transaction-runner.js';
9
- import { UnitOfWork } from './unit-of-work.js';
10
- import {
11
- EntityStatus,
12
- HasDomainEvents,
13
- RelationChange,
14
- RelationChangeEntry,
15
- RelationKey,
16
- TrackedEntity
17
- } from './runtime-types.js';
18
- import { createQueryLoggingExecutor, QueryLogger } from './query-logger.js';
19
-
20
- export interface OrmInterceptor {
21
- beforeFlush?(ctx: OrmContext): Promise<void> | void;
22
- afterFlush?(ctx: OrmContext): Promise<void> | void;
23
- }
24
-
25
- export type DomainEventHandler = DomainEventHandlerFn<OrmContext>;
26
-
27
- export interface OrmContextOptions {
28
- dialect: Dialect;
29
- executor: DbExecutor;
30
- interceptors?: OrmInterceptor[];
31
- domainEventHandlers?: Record<string, DomainEventHandler[]>;
32
- queryLogger?: QueryLogger;
33
- }
34
-
35
- export class OrmContext {
36
- private readonly identityMap = new IdentityMap();
37
- private readonly executorWithLogging: DbExecutor;
38
- private readonly unitOfWork: UnitOfWork;
39
- private readonly relationChanges: RelationChangeProcessor;
40
- private readonly interceptors: OrmInterceptor[];
41
- private readonly domainEvents: DomainEventBus<OrmContext>;
42
-
43
- constructor(private readonly options: OrmContextOptions) {
44
- this.interceptors = [...(options.interceptors ?? [])];
45
- this.executorWithLogging = createQueryLoggingExecutor(options.executor, options.queryLogger);
46
- this.unitOfWork = new UnitOfWork(
47
- options.dialect,
48
- this.executorWithLogging,
49
- this.identityMap,
50
- () => this
51
- );
52
- this.relationChanges = new RelationChangeProcessor(
53
- this.unitOfWork,
54
- options.dialect,
55
- this.executorWithLogging
56
- );
57
- this.domainEvents = new DomainEventBus<OrmContext>(options.domainEventHandlers);
58
- }
59
-
60
- get dialect(): Dialect {
61
- return this.options.dialect;
62
- }
63
-
64
- get executor(): DbExecutor {
65
- return this.executorWithLogging;
66
- }
67
-
68
- get identityBuckets(): Map<string, Map<string, TrackedEntity>> {
69
- return this.unitOfWork.identityBuckets;
70
- }
71
-
72
- get tracked(): TrackedEntity[] {
73
- return this.unitOfWork.getTracked();
74
- }
75
-
76
- getEntity(table: TableDef, pk: string | number): any | undefined {
77
- return this.unitOfWork.getEntity(table, pk);
78
- }
79
-
80
- setEntity(table: TableDef, pk: string | number, entity: any): void {
81
- this.unitOfWork.setEntity(table, pk, entity);
82
- }
83
-
84
- trackNew(table: TableDef, entity: any, pk?: string | number): void {
85
- this.unitOfWork.trackNew(table, entity, pk);
86
- }
87
-
88
- trackManaged(table: TableDef, pk: string | number, entity: any): void {
89
- this.unitOfWork.trackManaged(table, pk, entity);
90
- }
91
-
92
- markDirty(entity: any): void {
93
- this.unitOfWork.markDirty(entity);
94
- }
95
-
96
- markRemoved(entity: any): void {
97
- this.unitOfWork.markRemoved(entity);
98
- }
99
-
100
- registerRelationChange(
101
- root: any,
102
- relationKey: RelationKey,
103
- rootTable: TableDef,
104
- relationName: string,
105
- relation: RelationDef,
106
- change: RelationChange<any>
107
- ): void {
108
- const entry: RelationChangeEntry = {
109
- root,
110
- relationKey,
111
- rootTable,
112
- relationName,
113
- relation,
114
- change
115
- };
116
- this.relationChanges.registerChange(entry);
117
- }
118
-
119
- registerInterceptor(interceptor: OrmInterceptor): void {
120
- this.interceptors.push(interceptor);
121
- }
122
-
123
- registerDomainEventHandler(name: string, handler: DomainEventHandler): void {
124
- this.domainEvents.register(name, handler);
125
- }
126
-
127
- async saveChanges(): Promise<void> {
128
- await runInTransaction(this.executor, async () => {
129
- for (const interceptor of this.interceptors) {
130
- await interceptor.beforeFlush?.(this);
131
- }
132
-
133
- await this.unitOfWork.flush();
134
- await this.relationChanges.process();
135
- await this.unitOfWork.flush();
136
-
137
- for (const interceptor of this.interceptors) {
138
- await interceptor.afterFlush?.(this);
139
- }
140
- });
141
-
142
- await this.domainEvents.dispatch(this.unitOfWork.getTracked(), this);
143
- }
144
-
145
- getEntitiesForTable(table: TableDef): TrackedEntity[] {
146
- return this.unitOfWork.getEntitiesForTable(table);
147
- }
148
- }
149
-
150
- export { addDomainEvent };
151
- export { EntityStatus };
152
- export type {
153
- QueryResult,
154
- DbExecutor,
155
- RelationKey,
156
- RelationChange,
157
- HasDomainEvents
158
- };
159
- export type { QueryLogEntry, QueryLogger } from './query-logger.js';