@woltz/rich-domain-drizzle 0.1.0

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 (93) hide show
  1. package/dist/cjs/batch-executor.d.ts +30 -0
  2. package/dist/cjs/batch-executor.d.ts.map +1 -0
  3. package/dist/cjs/batch-executor.js +201 -0
  4. package/dist/cjs/batch-executor.js.map +1 -0
  5. package/dist/cjs/errors.d.ts +31 -0
  6. package/dist/cjs/errors.d.ts.map +1 -0
  7. package/dist/cjs/errors.js +84 -0
  8. package/dist/cjs/errors.js.map +1 -0
  9. package/dist/cjs/index.d.ts +8 -0
  10. package/dist/cjs/index.d.ts.map +1 -0
  11. package/dist/cjs/index.js +34 -0
  12. package/dist/cjs/index.js.map +1 -0
  13. package/dist/cjs/mappers/to-domain.d.ts +11 -0
  14. package/dist/cjs/mappers/to-domain.d.ts.map +1 -0
  15. package/dist/cjs/mappers/to-domain.js +15 -0
  16. package/dist/cjs/mappers/to-domain.js.map +1 -0
  17. package/dist/cjs/mappers/to-persistence.d.ts +47 -0
  18. package/dist/cjs/mappers/to-persistence.d.ts.map +1 -0
  19. package/dist/cjs/mappers/to-persistence.js +69 -0
  20. package/dist/cjs/mappers/to-persistence.js.map +1 -0
  21. package/dist/cjs/query-builder.d.ts +24 -0
  22. package/dist/cjs/query-builder.d.ts.map +1 -0
  23. package/dist/cjs/query-builder.js +146 -0
  24. package/dist/cjs/query-builder.js.map +1 -0
  25. package/dist/cjs/repository.d.ts +46 -0
  26. package/dist/cjs/repository.d.ts.map +1 -0
  27. package/dist/cjs/repository.js +192 -0
  28. package/dist/cjs/repository.js.map +1 -0
  29. package/dist/cjs/unit-of-work.d.ts +49 -0
  30. package/dist/cjs/unit-of-work.d.ts.map +1 -0
  31. package/dist/cjs/unit-of-work.js +94 -0
  32. package/dist/cjs/unit-of-work.js.map +1 -0
  33. package/dist/esm/batch-executor.d.ts +30 -0
  34. package/dist/esm/batch-executor.d.ts.map +1 -0
  35. package/dist/esm/batch-executor.js +196 -0
  36. package/dist/esm/batch-executor.js.map +1 -0
  37. package/dist/esm/errors.d.ts +31 -0
  38. package/dist/esm/errors.d.ts.map +1 -0
  39. package/dist/esm/errors.js +75 -0
  40. package/dist/esm/errors.js.map +1 -0
  41. package/dist/esm/index.d.ts +8 -0
  42. package/dist/esm/index.d.ts.map +1 -0
  43. package/dist/esm/index.js +14 -0
  44. package/dist/esm/index.js.map +1 -0
  45. package/dist/esm/mappers/to-domain.d.ts +11 -0
  46. package/dist/esm/mappers/to-domain.d.ts.map +1 -0
  47. package/dist/esm/mappers/to-domain.js +11 -0
  48. package/dist/esm/mappers/to-domain.js.map +1 -0
  49. package/dist/esm/mappers/to-persistence.d.ts +47 -0
  50. package/dist/esm/mappers/to-persistence.d.ts.map +1 -0
  51. package/dist/esm/mappers/to-persistence.js +65 -0
  52. package/dist/esm/mappers/to-persistence.js.map +1 -0
  53. package/dist/esm/package.json +3 -0
  54. package/dist/esm/query-builder.d.ts +24 -0
  55. package/dist/esm/query-builder.d.ts.map +1 -0
  56. package/dist/esm/query-builder.js +142 -0
  57. package/dist/esm/query-builder.js.map +1 -0
  58. package/dist/esm/repository.d.ts +46 -0
  59. package/dist/esm/repository.d.ts.map +1 -0
  60. package/dist/esm/repository.js +188 -0
  61. package/dist/esm/repository.js.map +1 -0
  62. package/dist/esm/unit-of-work.d.ts +49 -0
  63. package/dist/esm/unit-of-work.d.ts.map +1 -0
  64. package/dist/esm/unit-of-work.js +87 -0
  65. package/dist/esm/unit-of-work.js.map +1 -0
  66. package/dist/tsconfig.cjs.tsbuildinfo +1 -0
  67. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  68. package/dist/tsconfig.types.tsbuildinfo +1 -0
  69. package/dist/types/batch-executor.d.ts +30 -0
  70. package/dist/types/batch-executor.d.ts.map +1 -0
  71. package/dist/types/errors.d.ts +31 -0
  72. package/dist/types/errors.d.ts.map +1 -0
  73. package/dist/types/index.d.ts +8 -0
  74. package/dist/types/index.d.ts.map +1 -0
  75. package/dist/types/mappers/to-domain.d.ts +11 -0
  76. package/dist/types/mappers/to-domain.d.ts.map +1 -0
  77. package/dist/types/mappers/to-persistence.d.ts +47 -0
  78. package/dist/types/mappers/to-persistence.d.ts.map +1 -0
  79. package/dist/types/query-builder.d.ts +24 -0
  80. package/dist/types/query-builder.d.ts.map +1 -0
  81. package/dist/types/repository.d.ts +46 -0
  82. package/dist/types/repository.d.ts.map +1 -0
  83. package/dist/types/unit-of-work.d.ts +49 -0
  84. package/dist/types/unit-of-work.d.ts.map +1 -0
  85. package/package.json +69 -0
  86. package/src/batch-executor.ts +317 -0
  87. package/src/errors.ts +78 -0
  88. package/src/index.ts +37 -0
  89. package/src/mappers/to-domain.ts +13 -0
  90. package/src/mappers/to-persistence.ts +101 -0
  91. package/src/query-builder.ts +217 -0
  92. package/src/repository.ts +252 -0
  93. package/src/unit-of-work.ts +123 -0
@@ -0,0 +1,196 @@
1
+ import { eq, inArray, and } from "drizzle-orm";
2
+ import { TableNotFoundError, BatchOperationError, MissingJunctionConfigError, } from "./errors";
3
+ export class DrizzleBatchExecutor {
4
+ config;
5
+ constructor(config) {
6
+ this.config = config;
7
+ }
8
+ /**
9
+ * Execute all batch operations in order:
10
+ * 1. Deletes (leaf → root, sorted by depth DESC)
11
+ * 2. Creates (root → leaf, sorted by depth ASC)
12
+ * 3. Updates (any order)
13
+ */
14
+ async execute(changes) {
15
+ if (changes.isEmpty())
16
+ return;
17
+ const batch = changes.toBatchOperations();
18
+ await this.executeDeletes(batch.deletes);
19
+ await this.executeCreates(batch.creates);
20
+ await this.executeUpdates(batch.updates);
21
+ }
22
+ async executeDeletes(deletes) {
23
+ const sorted = [...deletes].sort((a, b) => b.depth - a.depth);
24
+ for (const del of sorted) {
25
+ const { entity, ids, relationField, parentEntity, parentId } = del;
26
+ if (relationField && parentEntity) {
27
+ this.config.registry.validateRelationField(parentEntity, relationField);
28
+ if (this.config.registry.isReferenceCollection(parentEntity, relationField)) {
29
+ await this.executeJunctionDelete(parentEntity, relationField, parentId, ids);
30
+ }
31
+ else {
32
+ await this.executeDeleteOwned(entity, ids);
33
+ }
34
+ }
35
+ else {
36
+ await this.executeDeleteOwned(entity, ids);
37
+ }
38
+ }
39
+ }
40
+ async executeDeleteOwned(entity, ids) {
41
+ if (ids.length === 0)
42
+ return;
43
+ const table = this.getTable(entity);
44
+ try {
45
+ await this.config.db.delete(table).where(inArray(table.id, ids));
46
+ }
47
+ catch (error) {
48
+ throw new BatchOperationError("delete", entity, error.message || "Unknown error during deletion", error);
49
+ }
50
+ }
51
+ async executeJunctionDelete(parentEntity, relationField, parentId, ids) {
52
+ if (ids.length === 0 || !parentId)
53
+ return;
54
+ const junction = this.config.registry.getJunctionConfig(parentEntity, relationField);
55
+ if (!junction) {
56
+ throw new MissingJunctionConfigError(parentEntity, relationField);
57
+ }
58
+ const junctionTable = this.getJunctionTable(junction.table);
59
+ try {
60
+ await this.config.db
61
+ .delete(junctionTable)
62
+ .where(and(eq(junctionTable[junction.sourceKey], parentId), inArray(junctionTable[junction.targetKey], ids)));
63
+ }
64
+ catch (error) {
65
+ throw new BatchOperationError("disconnect", junction.table, error.message || "Failed to delete from junction table", error);
66
+ }
67
+ }
68
+ async executeCreates(creates) {
69
+ const sorted = [...creates].sort((a, b) => a.depth - b.depth);
70
+ for (const create of sorted) {
71
+ const { entity, items, relationField, parentEntity } = create;
72
+ if (relationField && parentEntity) {
73
+ this.config.registry.validateRelationField(parentEntity, relationField);
74
+ if (this.config.registry.isReferenceCollection(parentEntity, relationField)) {
75
+ await this.executeJunctionCreate(parentEntity, relationField, items);
76
+ }
77
+ else {
78
+ await this.executeCreateOwned(entity, items);
79
+ }
80
+ }
81
+ else {
82
+ await this.executeCreateOwned(entity, items);
83
+ }
84
+ }
85
+ }
86
+ async executeCreateOwned(entity, items) {
87
+ if (items.length === 0)
88
+ return;
89
+ const table = this.getTable(entity);
90
+ const records = items.map((item) => {
91
+ const entityData = this.config.registry.mapEntity(entity, item.data);
92
+ const fk = item.parentId
93
+ ? this.config.registry.getParentFk(entity, item.parentId)
94
+ : null;
95
+ return { ...entityData, ...fk };
96
+ });
97
+ try {
98
+ await this.config.db.insert(table).values(records);
99
+ }
100
+ catch (error) {
101
+ throw new BatchOperationError("create", entity, error.message || "Unknown error during creation", error);
102
+ }
103
+ }
104
+ async executeJunctionCreate(parentEntity, relationField, items) {
105
+ if (items.length === 0)
106
+ return;
107
+ const junction = this.config.registry.getJunctionConfig(parentEntity, relationField);
108
+ if (!junction) {
109
+ throw new MissingJunctionConfigError(parentEntity, relationField);
110
+ }
111
+ const junctionTable = this.getJunctionTable(junction.table);
112
+ const grouped = new Map();
113
+ for (const item of items) {
114
+ const parentId = item.parentId;
115
+ if (!parentId)
116
+ continue;
117
+ const entityId = this.extractId(item.data);
118
+ if (!entityId)
119
+ continue;
120
+ if (!grouped.has(parentId)) {
121
+ grouped.set(parentId, []);
122
+ }
123
+ grouped.get(parentId).push(entityId);
124
+ }
125
+ const records = [];
126
+ for (const [parentId, targetIds] of grouped) {
127
+ for (const targetId of targetIds) {
128
+ records.push({
129
+ [junction.sourceKey]: parentId,
130
+ [junction.targetKey]: targetId,
131
+ });
132
+ }
133
+ }
134
+ if (records.length === 0)
135
+ return;
136
+ try {
137
+ await this.config.db
138
+ .insert(junctionTable)
139
+ .values(records)
140
+ .onConflictDoNothing();
141
+ }
142
+ catch (error) {
143
+ throw new BatchOperationError("connect", junction.table, error.message || "Failed to insert into junction table", error);
144
+ }
145
+ }
146
+ async executeUpdates(updates) {
147
+ for (const upd of updates) {
148
+ const table = this.getTable(upd.entity);
149
+ for (const item of upd.items) {
150
+ const mappedFields = this.config.registry.mapFields(upd.entity, item.changedFields);
151
+ if (Object.keys(mappedFields).length > 0) {
152
+ try {
153
+ await this.config.db
154
+ .update(table)
155
+ .set(mappedFields)
156
+ .where(eq(table.id, item.id));
157
+ }
158
+ catch (error) {
159
+ throw new BatchOperationError("update", upd.entity, error.message || "Failed to update entity", error);
160
+ }
161
+ }
162
+ }
163
+ }
164
+ }
165
+ getTable(entityName) {
166
+ const table = this.config.tableMap.get(entityName);
167
+ if (!table) {
168
+ throw new TableNotFoundError(entityName, Array.from(this.config.tableMap.keys()));
169
+ }
170
+ return table;
171
+ }
172
+ getJunctionTable(tableName) {
173
+ const table = this.config.tableMap.get(tableName);
174
+ if (!table) {
175
+ throw new TableNotFoundError(tableName, Array.from(this.config.tableMap.keys()));
176
+ }
177
+ return table;
178
+ }
179
+ extractId(data) {
180
+ if (!data)
181
+ return undefined;
182
+ if (data.id?.value !== undefined && data.id?.value !== null) {
183
+ return String(data.id.value);
184
+ }
185
+ if (typeof data.id === "string")
186
+ return data.id;
187
+ if (typeof data.id === "number")
188
+ return String(data.id);
189
+ return undefined;
190
+ }
191
+ }
192
+ export async function executeBatch(db, changes, config) {
193
+ const executor = new DrizzleBatchExecutor({ ...config, db });
194
+ await executor.execute(changes);
195
+ }
196
+ //# sourceMappingURL=batch-executor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"batch-executor.js","sourceRoot":"","sources":["../../src/batch-executor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAU/C,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,0BAA0B,GAC3B,MAAM,UAAU,CAAC;AAQlB,MAAM,OAAO,oBAAoB;IACF;IAA7B,YAA6B,MAAkC;QAAlC,WAAM,GAAN,MAAM,CAA4B;IAAG,CAAC;IAEnE;;;;;OAKG;IACH,KAAK,CAAC,OAAO,CAAC,OAAyB;QACrC,IAAI,OAAO,CAAC,OAAO,EAAE;YAAE,OAAO;QAE9B,MAAM,KAAK,GAAG,OAAO,CAAC,iBAAiB,EAAE,CAAC;QAE1C,MAAM,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACzC,MAAM,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,OA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AC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CACxC,CAAC;QACJ,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAEO,SAAS,CAAC,IAAS;QACzB,IAAI,CAAC,IAAI;YAAE,OAAO,SAAS,CAAC;QAC5B,IAAI,IAAI,CAAC,EAAE,EAAE,KAAK,KAAK,SAAS,IAAI,IAAI,CAAC,EAAE,EAAE,KAAK,KAAK,IAAI,EAAE,CAAC;YAC5D,OAAO,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;QAC/B,CAAC;QACD,IAAI,OAAO,IAAI,CAAC,EAAE,KAAK,QAAQ;YAAE,OAAO,IAAI,CAAC,EAAE,CAAC;QAChD,IAAI,OAAO,IAAI,CAAC,EAAE,KAAK,QAAQ;YAAE,OAAO,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACxD,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AAED,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,EAAiB,EACjB,OAAyB,EACzB,MAA8C;IAE9C,MAAM,QAAQ,GAAG,IAAI,oBAAoB,CAAC,EAAE,GAAG,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC;IAC7D,MAAM,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;AAClC,CAAC"}
@@ -0,0 +1,31 @@
1
+ export declare class DrizzleAdapterError extends Error {
2
+ constructor(message: string);
3
+ }
4
+ export declare class TableNotFoundError extends DrizzleAdapterError {
5
+ readonly entityName: string;
6
+ readonly availableTables: string[];
7
+ constructor(entityName: string, availableTables: string[]);
8
+ }
9
+ export declare class NoRecordsAffectedError extends DrizzleAdapterError {
10
+ readonly operation: string;
11
+ readonly entity: string;
12
+ readonly id: string;
13
+ readonly cause?: Error | undefined;
14
+ constructor(operation: string, entity: string, id: string, cause?: Error | undefined);
15
+ }
16
+ export declare class BatchOperationError extends DrizzleAdapterError {
17
+ readonly operation: string;
18
+ readonly entity: string;
19
+ readonly cause?: Error | undefined;
20
+ constructor(operation: string, entity: string, message: string, cause?: Error | undefined);
21
+ }
22
+ export declare class DrizzleRepositoryError extends DrizzleAdapterError {
23
+ readonly cause?: Error | undefined;
24
+ constructor(message: string, cause?: Error | undefined);
25
+ }
26
+ export declare class MissingJunctionConfigError extends DrizzleAdapterError {
27
+ readonly parentEntity: string;
28
+ readonly collectionField: string;
29
+ constructor(parentEntity: string, collectionField: string);
30
+ }
31
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,mBAAoB,SAAQ,KAAK;gBAChC,OAAO,EAAE,MAAM;CAI5B;AAED,qBAAa,kBAAmB,SAAQ,mBAAmB;aAEvC,UAAU,EAAE,MAAM;aAClB,eAAe,EAAE,MAAM,EAAE;gBADzB,UAAU,EAAE,MAAM,EAClB,eAAe,EAAE,MAAM,EAAE;CAQ5C;AAED,qBAAa,sBAAuB,SAAQ,mBAAmB;aAE3C,SAAS,EAAE,MAAM;aACjB,MAAM,EAAE,MAAM;aACd,EAAE,EAAE,MAAM;aACV,KAAK,CAAC,EAAE,KAAK;gBAHb,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,EAAE,EAAE,MAAM,EACV,KAAK,CAAC,EAAE,KAAK,YAAA;CAKhC;AAED,qBAAa,mBAAoB,SAAQ,mBAAmB;aAExC,SAAS,EAAE,MAAM;aACjB,MAAM,EAAE,MAAM;aAEd,KAAK,CAAC,EAAE,KAAK;gBAHb,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EAC9B,OAAO,EAAE,MAAM,EACC,KAAK,CAAC,EAAE,KAAK,YAAA;CAKhC;AAED,qBAAa,sBAAuB,SAAQ,mBAAmB;aAG3C,KAAK,CAAC,EAAE,KAAK;gBAD7B,OAAO,EAAE,MAAM,EACC,KAAK,CAAC,EAAE,KAAK,YAAA;CAKhC;AAED,qBAAa,0BAA2B,SAAQ,mBAAmB;aAE/C,YAAY,EAAE,MAAM;aACpB,eAAe,EAAE,MAAM;gBADvB,YAAY,EAAE,MAAM,EACpB,eAAe,EAAE,MAAM;CAoB1C"}
@@ -0,0 +1,75 @@
1
+ export class DrizzleAdapterError extends Error {
2
+ constructor(message) {
3
+ super(`[DrizzleAdapter] ${message}`);
4
+ this.name = "DrizzleAdapterError";
5
+ }
6
+ }
7
+ export class TableNotFoundError extends DrizzleAdapterError {
8
+ entityName;
9
+ availableTables;
10
+ constructor(entityName, availableTables) {
11
+ super(`Table for entity "${entityName}" not found in tableMap. ` +
12
+ `Available: ${availableTables.join(", ")}`);
13
+ this.entityName = entityName;
14
+ this.availableTables = availableTables;
15
+ this.name = "TableNotFoundError";
16
+ }
17
+ }
18
+ export class NoRecordsAffectedError extends DrizzleAdapterError {
19
+ operation;
20
+ entity;
21
+ id;
22
+ cause;
23
+ constructor(operation, entity, id, cause) {
24
+ super(`${operation} on ${entity} (id: ${id}) affected 0 records`);
25
+ this.operation = operation;
26
+ this.entity = entity;
27
+ this.id = id;
28
+ this.cause = cause;
29
+ this.name = "NoRecordsAffectedError";
30
+ }
31
+ }
32
+ export class BatchOperationError extends DrizzleAdapterError {
33
+ operation;
34
+ entity;
35
+ cause;
36
+ constructor(operation, entity, message, cause) {
37
+ super(`Batch ${operation} on ${entity} failed: ${message}`);
38
+ this.operation = operation;
39
+ this.entity = entity;
40
+ this.cause = cause;
41
+ this.name = "BatchOperationError";
42
+ }
43
+ }
44
+ export class DrizzleRepositoryError extends DrizzleAdapterError {
45
+ cause;
46
+ constructor(message, cause) {
47
+ super(message);
48
+ this.cause = cause;
49
+ this.name = "DrizzleRepositoryError";
50
+ }
51
+ }
52
+ export class MissingJunctionConfigError extends DrizzleAdapterError {
53
+ parentEntity;
54
+ collectionField;
55
+ constructor(parentEntity, collectionField) {
56
+ super(`Collection "${collectionField}" on entity "${parentEntity}" is of type "reference" but has no junction configured. ` +
57
+ `Drizzle does not manage junction tables automatically — you must provide the junction config. ` +
58
+ `Example:\n` +
59
+ ` collections: {\n` +
60
+ ` ${collectionField}: {\n` +
61
+ ` type: "reference",\n` +
62
+ ` entity: "TargetEntity",\n` +
63
+ ` junction: {\n` +
64
+ ` table: "${parentEntity.toLowerCase()}_${collectionField}",\n` +
65
+ ` sourceKey: "${parentEntity.toLowerCase()}Id",\n` +
66
+ ` targetKey: "targetEntityId",\n` +
67
+ ` },\n` +
68
+ ` },\n` +
69
+ ` }`);
70
+ this.parentEntity = parentEntity;
71
+ this.collectionField = collectionField;
72
+ this.name = "MissingJunctionConfigError";
73
+ }
74
+ }
75
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAC5C,YAAY,OAAe;QACzB,KAAK,CAAC,oBAAoB,OAAO,EAAE,CAAC,CAAC;QACrC,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF;AAED,MAAM,OAAO,kBAAmB,SAAQ,mBAAmB;IAEvC;IACA;IAFlB,YACkB,UAAkB,EAClB,eAAyB;QAEzC,KAAK,CACH,qBAAqB,UAAU,2BAA2B;YACxD,cAAc,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAC7C,CAAC;QANc,eAAU,GAAV,UAAU,CAAQ;QAClB,oBAAe,GAAf,eAAe,CAAU;QAMzC,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF;AAED,MAAM,OAAO,sBAAuB,SAAQ,mBAAmB;IAE3C;IACA;IACA;IACA;IAJlB,YACkB,SAAiB,EACjB,MAAc,EACd,EAAU,EACV,KAAa;QAE7B,KAAK,CAAC,GAAG,SAAS,OAAO,MAAM,SAAS,EAAE,sBAAsB,CAAC,CAAC;QALlD,cAAS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I,GAAG,4BAA4B,CAAC;IAC3C,CAAC;CACF"}
@@ -0,0 +1,8 @@
1
+ export { DrizzleUnitOfWork, DrizzleTransactionContext, UOWStorage, Transactional, getCurrentDrizzleContext, type DrizzleClient, type DrizzleTransactionClient, } from "./unit-of-work";
2
+ export { DrizzleRepository, type DrizzleRepositoryConfig } from "./repository";
3
+ export { DrizzleToPersistence } from "./mappers/to-persistence";
4
+ export { DrizzleToDomain } from "./mappers/to-domain";
5
+ export { DrizzleBatchExecutor, executeBatch, type DrizzleBatchExecutorConfig, } from "./batch-executor";
6
+ export { DrizzleQueryBuilder, type SearchableField } from "./query-builder";
7
+ export { DrizzleAdapterError, TableNotFoundError, NoRecordsAffectedError, BatchOperationError, DrizzleRepositoryError, MissingJunctionConfigError, } from "./errors";
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,iBAAiB,EACjB,yBAAyB,EACzB,UAAU,EACV,aAAa,EACb,wBAAwB,EACxB,KAAK,aAAa,EAClB,KAAK,wBAAwB,GAC9B,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EAAE,iBAAiB,EAAE,KAAK,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAG/E,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAGtD,OAAO,EACL,oBAAoB,EACpB,YAAY,EACZ,KAAK,0BAA0B,GAChC,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EAAE,mBAAmB,EAAE,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAG5E,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,sBAAsB,EACtB,mBAAmB,EACnB,sBAAsB,EACtB,0BAA0B,GAC3B,MAAM,UAAU,CAAC"}
@@ -0,0 +1,14 @@
1
+ // Unit of Work
2
+ export { DrizzleUnitOfWork, DrizzleTransactionContext, UOWStorage, Transactional, getCurrentDrizzleContext, } from "./unit-of-work";
3
+ // Repository
4
+ export { DrizzleRepository } from "./repository";
5
+ // Mappers
6
+ export { DrizzleToPersistence } from "./mappers/to-persistence";
7
+ export { DrizzleToDomain } from "./mappers/to-domain";
8
+ // Batch Executor
9
+ export { DrizzleBatchExecutor, executeBatch, } from "./batch-executor";
10
+ // Query Builder
11
+ export { DrizzleQueryBuilder } from "./query-builder";
12
+ // Errors
13
+ export { DrizzleAdapterError, TableNotFoundError, NoRecordsAffectedError, BatchOperationError, DrizzleRepositoryError, MissingJunctionConfigError, } from "./errors";
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,eAAe;AACf,OAAO,EACL,iBAAiB,EACjB,yBAAyB,EACzB,UAAU,EACV,aAAa,EACb,wBAAwB,GAGzB,MAAM,gBAAgB,CAAC;AAExB,aAAa;AACb,OAAO,EAAE,iBAAiB,EAAgC,MAAM,cAAc,CAAC;AAE/E,UAAU;AACV,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEtD,iBAAiB;AACjB,OAAO,EACL,oBAAoB,EACpB,YAAY,GAEb,MAAM,kBAAkB,CAAC;AAE1B,gBAAgB;AAChB,OAAO,EAAE,mBAAmB,EAAwB,MAAM,iBAAiB,CAAC;AAE5E,SAAS;AACT,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,sBAAsB,EACtB,mBAAmB,EACnB,sBAAsB,EACtB,0BAA0B,GAC3B,MAAM,UAAU,CAAC"}
@@ -0,0 +1,11 @@
1
+ import { Mapper } from "@woltz/rich-domain";
2
+ /**
3
+ * Base class for mapping Drizzle query results to domain entities.
4
+ * Subclass and implement build() to transform DB records to domain aggregates.
5
+ *
6
+ * This is identical to a plain Mapper<TPersistence, TDomain>.
7
+ * Provided for naming consistency with the adapter pattern.
8
+ */
9
+ export declare abstract class DrizzleToDomain<TPersistence, TDomain> extends Mapper<TPersistence, TDomain> {
10
+ }
11
+ //# sourceMappingURL=to-domain.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to-domain.d.ts","sourceRoot":"","sources":["../../../src/mappers/to-domain.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;;;;;GAMG;AACH,8BAAsB,eAAe,CAAC,YAAY,EAAE,OAAO,CAAE,SAAQ,MAAM,CACzE,YAAY,EACZ,OAAO,CACR;CAAG"}
@@ -0,0 +1,11 @@
1
+ import { Mapper } from "@woltz/rich-domain";
2
+ /**
3
+ * Base class for mapping Drizzle query results to domain entities.
4
+ * Subclass and implement build() to transform DB records to domain aggregates.
5
+ *
6
+ * This is identical to a plain Mapper<TPersistence, TDomain>.
7
+ * Provided for naming consistency with the adapter pattern.
8
+ */
9
+ export class DrizzleToDomain extends Mapper {
10
+ }
11
+ //# sourceMappingURL=to-domain.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to-domain.js","sourceRoot":"","sources":["../../../src/mappers/to-domain.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;;;;;GAMG;AACH,MAAM,OAAgB,eAAuC,SAAQ,MAGpE;CAAG"}
@@ -0,0 +1,47 @@
1
+ import { Mapper, AggregateChanges, EntitySchemaRegistry } from "@woltz/rich-domain";
2
+ import { DrizzleClient, DrizzleUnitOfWork } from "../unit-of-work";
3
+ export declare abstract class DrizzleToPersistence<TDomain> extends Mapper<TDomain, void> {
4
+ protected readonly uow: DrizzleUnitOfWork;
5
+ constructor(uow: DrizzleUnitOfWork);
6
+ /**
7
+ * Schema registry for field mapping (entity → table, field → column).
8
+ */
9
+ protected abstract readonly registry: EntitySchemaRegistry;
10
+ /**
11
+ * Map of entity names to Drizzle table objects.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * protected readonly tableMap = new Map([
16
+ * ["User", usersTable],
17
+ * ["Post", postsTable],
18
+ * ]);
19
+ * ```
20
+ */
21
+ protected abstract readonly tableMap: Map<string, any>;
22
+ /**
23
+ * Get the raw db instance.
24
+ */
25
+ protected abstract getDb(): DrizzleClient;
26
+ /**
27
+ * Get current context (transaction client or raw db).
28
+ */
29
+ protected get context(): DrizzleClient;
30
+ /**
31
+ * Build persistence operations.
32
+ */
33
+ build(entity: TDomain): Promise<void>;
34
+ /**
35
+ * Handle new aggregate creation.
36
+ * Must be implemented by subclass.
37
+ */
38
+ protected abstract onCreate(aggregate: TDomain): Promise<void>;
39
+ /**
40
+ * Handle aggregate update with changes.
41
+ * Default implementation uses DrizzleBatchExecutor.
42
+ * Subclass can override for custom logic.
43
+ */
44
+ protected onUpdate(changes: AggregateChanges, _aggregate: TDomain): Promise<void>;
45
+ private handleUpdate;
46
+ }
47
+ //# sourceMappingURL=to-persistence.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to-persistence.d.ts","sourceRoot":"","sources":["../../../src/mappers/to-persistence.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,gBAAgB,EAChB,oBAAoB,EACrB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,aAAa,EACb,iBAAiB,EAGlB,MAAM,iBAAiB,CAAC;AAGzB,8BAAsB,oBAAoB,CAAC,OAAO,CAAE,SAAQ,MAAM,CAChE,OAAO,EACP,IAAI,CACL;IACa,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,iBAAiB;gBAAtB,GAAG,EAAE,iBAAiB;IAIrD;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,QAAQ,EAAE,oBAAoB,CAAC;IAE3D;;;;;;;;;;OAUG;IACH,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAEvD;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,KAAK,IAAI,aAAa;IAEzC;;OAEG;IACH,SAAS,KAAK,OAAO,IAAI,aAAa,CAGrC;IAED;;OAEG;IACG,KAAK,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAU3C;;;OAGG;IACH,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,SAAS,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAE9D;;;;OAIG;cACa,QAAQ,CACtB,OAAO,EAAE,gBAAgB,EACzB,UAAU,EAAE,OAAO,GAClB,OAAO,CAAC,IAAI,CAAC;YAUF,YAAY;CAW3B"}
@@ -0,0 +1,65 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { Mapper, } from "@woltz/rich-domain";
11
+ import { UOWStorage, Transactional, } from "../unit-of-work";
12
+ import { DrizzleBatchExecutor } from "../batch-executor";
13
+ export class DrizzleToPersistence extends Mapper {
14
+ uow;
15
+ constructor(uow) {
16
+ super();
17
+ this.uow = uow;
18
+ }
19
+ /**
20
+ * Get current context (transaction client or raw db).
21
+ */
22
+ get context() {
23
+ const ctx = UOWStorage.getStore()?.ctx;
24
+ return ctx?.client ?? this.getDb();
25
+ }
26
+ /**
27
+ * Build persistence operations.
28
+ */
29
+ async build(entity) {
30
+ const isNew = entity.isNew?.() ?? false;
31
+ if (isNew) {
32
+ await this.onCreate(entity);
33
+ }
34
+ else {
35
+ await this.handleUpdate(entity);
36
+ }
37
+ }
38
+ /**
39
+ * Handle aggregate update with changes.
40
+ * Default implementation uses DrizzleBatchExecutor.
41
+ * Subclass can override for custom logic.
42
+ */
43
+ async onUpdate(changes, _aggregate) {
44
+ const executor = new DrizzleBatchExecutor({
45
+ registry: this.registry,
46
+ db: this.context,
47
+ tableMap: this.tableMap,
48
+ });
49
+ await executor.execute(changes);
50
+ }
51
+ async handleUpdate(entity) {
52
+ const changes = entity.getChanges?.();
53
+ if (!changes || changes.isEmpty()) {
54
+ return;
55
+ }
56
+ await this.onUpdate(changes, entity);
57
+ }
58
+ }
59
+ __decorate([
60
+ Transactional(),
61
+ __metadata("design:type", Function),
62
+ __metadata("design:paramtypes", [Object]),
63
+ __metadata("design:returntype", Promise)
64
+ ], DrizzleToPersistence.prototype, "handleUpdate", null);
65
+ //# sourceMappingURL=to-persistence.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to-persistence.js","sourceRoot":"","sources":["../../../src/mappers/to-persistence.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EACL,MAAM,GAGP,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAGL,UAAU,EACV,aAAa,GACd,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAEzD,MAAM,OAAgB,oBAA8B,SAAQ,MAG3D;IACgC;IAA/B,YAA+B,GAAsB;QACnD,KAAK,EAAE,CAAC;QADqB,QAAG,GAAH,GAAG,CAAmB;IAErD,CAAC;IAyBD;;OAEG;IACH,IAAc,OAAO;QACnB,MAAM,GAAG,GAAG,UAAU,CAAC,QAAQ,EAAE,EAAE,GAAG,CAAC;QACvC,OAAO,GAAG,EAAE,MAAM,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;IACrC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,KAAK,CAAC,MAAe;QACzB,MAAM,KAAK,GAAI,MAAc,CAAC,KAAK,EAAE,EAAE,IAAI,KAAK,CAAC;QAEjD,IAAI,KAAK,EAAE,CAAC;YACV,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;QAC9B,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;QAClC,CAAC;IACH,CAAC;IAQD;;;;OAIG;IACO,KAAK,CAAC,QAAQ,CACtB,OAAyB,EACzB,UAAmB;QAEnB,MAAM,QAAQ,GAAG,IAAI,oBAAoB,CAAC;YACxC,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,EAAE,EAAE,IAAI,CAAC,OAAO;YAChB,QAAQ,EAAE,IAAI,CAAC,QAAQ;SACxB,CAAC,CAAC;QACH,MAAM,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAClC,CAAC;IAGa,AAAN,KAAK,CAAC,YAAY,CAAC,MAAe;QACxC,MAAM,OAAO,GAAI,MAAc,CAAC,UAAU,EAAE,EAE/B,CAAC;QAEd,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;YAClC,OAAO;QACT,CAAC;QAED,MAAM,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACvC,CAAC;CACF;AAXe;IADb,aAAa,EAAE;;;;wDAWf"}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,24 @@
1
+ import { SQL } from "drizzle-orm";
2
+ import { Criteria } from "@woltz/rich-domain";
3
+ export type SearchableField<T> = keyof T | {
4
+ field: string;
5
+ caseSensitive?: boolean;
6
+ };
7
+ export declare class DrizzleQueryBuilder {
8
+ /**
9
+ * Apply Criteria to a Drizzle select query.
10
+ *
11
+ * @param criteria - The Criteria instance with filters, orders, pagination, search
12
+ * @param table - The Drizzle table object (e.g., usersTable)
13
+ * @param searchableFields - Fields to search when criteria.hasSearch()
14
+ * @returns Object with { where, orderBy, limit, offset } ready for Drizzle query
15
+ */
16
+ static apply<T>(criteria: Criteria<T>, table: any, searchableFields?: SearchableField<any>[]): {
17
+ where?: SQL;
18
+ orderBy?: SQL[];
19
+ limit?: number;
20
+ offset?: number;
21
+ };
22
+ static resolveColumn(table: any, fieldPath: string): any;
23
+ }
24
+ //# sourceMappingURL=query-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query-builder.d.ts","sourceRoot":"","sources":["../../src/query-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAkBL,GAAG,EACJ,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAG9C,MAAM,MAAM,eAAe,CAAC,CAAC,IACzB,MAAM,CAAC,GACP;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,aAAa,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAqB/C,qBAAa,mBAAmB;IAC9B;;;;;;;OAOG;IACH,MAAM,CAAC,KAAK,CAAC,CAAC,EACZ,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,KAAK,EAAE,GAAG,EACV,gBAAgB,CAAC,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,GACxC;QACD,KAAK,CAAC,EAAE,GAAG,CAAC;QACZ,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC;QAChB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB;IAqJD,MAAM,CAAC,aAAa,CAAC,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,GAAG,GAAG;CAGzD"}