@woltz/rich-domain-drizzle 0.1.0 → 0.1.2
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/README.md +391 -0
- package/dist/cjs/mappers/to-persistence.d.ts +4 -7
- package/dist/cjs/mappers/to-persistence.d.ts.map +1 -1
- package/dist/cjs/mappers/to-persistence.js +4 -2
- package/dist/cjs/mappers/to-persistence.js.map +1 -1
- package/dist/cjs/repository.d.ts +8 -8
- package/dist/cjs/repository.d.ts.map +1 -1
- package/dist/cjs/repository.js +1 -1
- package/dist/cjs/repository.js.map +1 -1
- package/dist/esm/mappers/to-persistence.d.ts +4 -7
- package/dist/esm/mappers/to-persistence.d.ts.map +1 -1
- package/dist/esm/mappers/to-persistence.js +4 -2
- package/dist/esm/mappers/to-persistence.js.map +1 -1
- package/dist/esm/repository.d.ts +8 -8
- package/dist/esm/repository.d.ts.map +1 -1
- package/dist/esm/repository.js +1 -1
- package/dist/esm/repository.js.map +1 -1
- package/dist/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.types.tsbuildinfo +1 -1
- package/dist/types/mappers/to-persistence.d.ts +4 -7
- package/dist/types/mappers/to-persistence.d.ts.map +1 -1
- package/dist/types/repository.d.ts +8 -8
- package/dist/types/repository.d.ts.map +1 -1
- package/package.json +69 -69
- package/src/batch-executor.ts +317 -317
- package/src/errors.ts +78 -78
- package/src/index.ts +37 -37
- package/src/mappers/to-domain.ts +13 -13
- package/src/mappers/to-persistence.ts +99 -101
- package/src/query-builder.ts +217 -217
- package/src/repository.ts +257 -252
- package/src/unit-of-work.ts +123 -123
package/README.md
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
# @woltz/rich-domain-drizzle
|
|
2
|
+
|
|
3
|
+
Drizzle ORM adapter for [@woltz/rich-domain](https://github.com/tarcisioandrade/rich-domain). Provides plug and play integration between rich-domain and Drizzle ORM.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @woltz/rich-domain @woltz/rich-domain-drizzle drizzle-orm
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- ✅ **Unit of Work** with AsyncLocalStorage (request isolation)
|
|
14
|
+
- ✅ **DrizzleRepository** base class with Criteria support
|
|
15
|
+
- ✅ **DrizzleToPersistence** base class with change tracking
|
|
16
|
+
- ✅ **@Transactional** decorator for automatic transactions
|
|
17
|
+
- ✅ **DrizzleBatchExecutor** for batch change operations
|
|
18
|
+
- ✅ **EntitySchemaRegistry** for owned (1:N) and reference (N:N) collections
|
|
19
|
+
- ✅ **Junction table support** for explicit N:N mappings
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
### 1. Setup
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { drizzle } from "drizzle-orm/node-postgres";
|
|
27
|
+
import * as schema from "./schema";
|
|
28
|
+
import { DrizzleUnitOfWork } from "@woltz/rich-domain-drizzle";
|
|
29
|
+
|
|
30
|
+
type DB = ReturnType<typeof drizzle<typeof schema>>;
|
|
31
|
+
|
|
32
|
+
const db: DB = drizzle(pool, { schema });
|
|
33
|
+
const uow = new DrizzleUnitOfWork(db);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### 2. Create Repository
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { DrizzleRepository, SearchableField } from "@woltz/rich-domain-drizzle";
|
|
40
|
+
import { users } from "./schema";
|
|
41
|
+
|
|
42
|
+
type DB = ReturnType<typeof drizzle<typeof schema>>;
|
|
43
|
+
|
|
44
|
+
class UserRepository extends DrizzleRepository<User, UserRecord, DB> {
|
|
45
|
+
constructor(db: DB, uow: DrizzleUnitOfWork) {
|
|
46
|
+
super({
|
|
47
|
+
db,
|
|
48
|
+
table: users,
|
|
49
|
+
toDomainMapper: new UserToDomainMapper(),
|
|
50
|
+
toPersistenceMapper: new UserToPersistenceMapper(db, uow),
|
|
51
|
+
uow,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
protected get model() {
|
|
56
|
+
return "users"; // key in db.query — matches Drizzle schema export name
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
protected getSearchableFields(): SearchableField<UserRecord>[] {
|
|
60
|
+
return ["name", "email"];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
protected getDefaultRelations() {
|
|
64
|
+
return { posts: { with: { tags: { with: { tag: true } } } } };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 3. Use It
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
const userRepo = new UserRepository(uow);
|
|
73
|
+
|
|
74
|
+
// Create
|
|
75
|
+
const user = User.create({
|
|
76
|
+
name: "John",
|
|
77
|
+
email: "john@example.com",
|
|
78
|
+
posts: [],
|
|
79
|
+
});
|
|
80
|
+
await userRepo.save(user);
|
|
81
|
+
|
|
82
|
+
// Find by ID
|
|
83
|
+
const found = await userRepo.findById(user.id.value);
|
|
84
|
+
|
|
85
|
+
// Find with Criteria
|
|
86
|
+
const criteria = Criteria.create<User>()
|
|
87
|
+
.whereEquals("name", "John")
|
|
88
|
+
.orderByAsc("createdAt")
|
|
89
|
+
.paginate(1, 10);
|
|
90
|
+
|
|
91
|
+
const result = await userRepo.find(criteria);
|
|
92
|
+
// result.data → User[]
|
|
93
|
+
// result.toJSON().meta.total → total count
|
|
94
|
+
|
|
95
|
+
// Update (automatic change tracking)
|
|
96
|
+
found.updateName("John Updated");
|
|
97
|
+
await userRepo.save(found); // Detects and applies only what changed
|
|
98
|
+
|
|
99
|
+
// Delete
|
|
100
|
+
await userRepo.delete(found);
|
|
101
|
+
await userRepo.deleteById(userId);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## API Reference
|
|
107
|
+
|
|
108
|
+
### DrizzleUnitOfWork
|
|
109
|
+
|
|
110
|
+
Manages transactions with per-request isolation using AsyncLocalStorage.
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
const uow = new DrizzleUnitOfWork(db);
|
|
114
|
+
|
|
115
|
+
// Execute in transaction
|
|
116
|
+
await uow.transaction(async () => {
|
|
117
|
+
await userRepo.save(user);
|
|
118
|
+
await postRepo.save(post);
|
|
119
|
+
// All or nothing — rolls back on failure
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// Check if in transaction
|
|
123
|
+
uow.isInTransaction(); // boolean
|
|
124
|
+
|
|
125
|
+
// Get current context
|
|
126
|
+
uow.getCurrentContext(); // DrizzleTransactionContext | null
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
### DrizzleRepository
|
|
132
|
+
|
|
133
|
+
Base class for repositories with full Criteria support.
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
abstract class DrizzleRepository<
|
|
137
|
+
TDomain,
|
|
138
|
+
TRecord,
|
|
139
|
+
TDb extends DrizzleClient = DrizzleClient,
|
|
140
|
+
> {
|
|
141
|
+
constructor(config: { db: TDb; table: any; toDomainMapper; toPersistenceMapper; uow }) { ... }
|
|
142
|
+
|
|
143
|
+
// Required: key in db.query matching the Drizzle schema export name
|
|
144
|
+
protected abstract get model(): string;
|
|
145
|
+
|
|
146
|
+
// Optional: searchable columns for Criteria "search" operator
|
|
147
|
+
protected getSearchableFields(): SearchableField<TRecord>[];
|
|
148
|
+
|
|
149
|
+
// Optional: default relations to include in queries
|
|
150
|
+
protected getDefaultRelations(): Record<string, any>;
|
|
151
|
+
|
|
152
|
+
// Available: current context (transaction or plain db) — typed as TDb
|
|
153
|
+
protected get context(): TDb;
|
|
154
|
+
|
|
155
|
+
// Available methods
|
|
156
|
+
async find(criteria: Criteria<TDomain>): Promise<PaginatedResult<TDomain>>;
|
|
157
|
+
async findById(id: string): Promise<TDomain | null>;
|
|
158
|
+
async findOne(criteria: Criteria<TDomain>): Promise<TDomain | null>;
|
|
159
|
+
async count(criteria?: Criteria<TDomain>): Promise<number>;
|
|
160
|
+
async exists(id: string): Promise<boolean>;
|
|
161
|
+
async save(entity: TDomain): Promise<void>;
|
|
162
|
+
async delete(entity: TDomain): Promise<void>;
|
|
163
|
+
async deleteById(id: string): Promise<void>;
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
> **Note:** Criteria filters and ordering only support top-level columns of the primary table. Use custom methods with explicit JOINs for cross-table queries.
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
### DrizzleToPersistence
|
|
172
|
+
|
|
173
|
+
Base class for persistence mappers with change tracking support.
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
abstract class DrizzleToPersistence<
|
|
177
|
+
TDomain,
|
|
178
|
+
TDb extends DrizzleClient = DrizzleClient,
|
|
179
|
+
> extends Mapper<TDomain, void> {
|
|
180
|
+
constructor(db: TDb, uow: DrizzleUnitOfWork) { ... }
|
|
181
|
+
|
|
182
|
+
// Required: registry for entity/table/collection mapping
|
|
183
|
+
protected abstract readonly registry: EntitySchemaRegistry;
|
|
184
|
+
|
|
185
|
+
// Required: map entity names to Drizzle table objects (and junction tables)
|
|
186
|
+
protected abstract readonly tableMap: Map<string, any>;
|
|
187
|
+
|
|
188
|
+
// Required: implement creation
|
|
189
|
+
protected abstract onCreate(entity: TDomain): Promise<void>;
|
|
190
|
+
|
|
191
|
+
// Available: current context (transaction or plain db) — typed as TDb
|
|
192
|
+
protected get context(): TDb;
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
#### Example
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
import {
|
|
200
|
+
DrizzleToPersistence,
|
|
201
|
+
DrizzleUnitOfWork,
|
|
202
|
+
Transactional,
|
|
203
|
+
} from "@woltz/rich-domain-drizzle";
|
|
204
|
+
import { EntitySchemaRegistry } from "@woltz/rich-domain";
|
|
205
|
+
import { users, posts, tags, postsToTags } from "./schema";
|
|
206
|
+
|
|
207
|
+
const userRegistry = new EntitySchemaRegistry()
|
|
208
|
+
.register({
|
|
209
|
+
entity: "User",
|
|
210
|
+
table: "users",
|
|
211
|
+
collections: {
|
|
212
|
+
posts: { type: "owned", entity: "Post" },
|
|
213
|
+
},
|
|
214
|
+
})
|
|
215
|
+
.register({
|
|
216
|
+
entity: "Post",
|
|
217
|
+
table: "posts",
|
|
218
|
+
parentFk: { field: "authorId", parentEntity: "User" },
|
|
219
|
+
collections: {
|
|
220
|
+
tags: {
|
|
221
|
+
type: "reference",
|
|
222
|
+
entity: "Tag",
|
|
223
|
+
junction: {
|
|
224
|
+
table: "posts_to_tags", // must match tableMap key
|
|
225
|
+
sourceKey: "postId",
|
|
226
|
+
targetKey: "tagId",
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
})
|
|
231
|
+
.register({ entity: "Tag", table: "tags" });
|
|
232
|
+
|
|
233
|
+
type DB = ReturnType<typeof drizzle<typeof schema>>;
|
|
234
|
+
|
|
235
|
+
class UserToPersistenceMapper extends DrizzleToPersistence<User, DB> {
|
|
236
|
+
protected readonly registry = userRegistry;
|
|
237
|
+
|
|
238
|
+
protected readonly tableMap = new Map<string, any>([
|
|
239
|
+
["User", users],
|
|
240
|
+
["Post", posts],
|
|
241
|
+
["Tag", tags],
|
|
242
|
+
["posts_to_tags", postsToTags], // junction table key must match registry
|
|
243
|
+
]);
|
|
244
|
+
|
|
245
|
+
constructor(db: DB, uow: DrizzleUnitOfWork) {
|
|
246
|
+
super(db, uow);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
@Transactional()
|
|
250
|
+
protected async onCreate(user: User): Promise<void> {
|
|
251
|
+
await this.context.insert(users).values({
|
|
252
|
+
id: user.id.value,
|
|
253
|
+
email: user.email,
|
|
254
|
+
name: user.name,
|
|
255
|
+
createdAt: user.createdAt,
|
|
256
|
+
updatedAt: user.updatedAt,
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
if (user.posts.length > 0) {
|
|
260
|
+
await this.context.insert(posts).values(
|
|
261
|
+
user.posts.map((p) => ({
|
|
262
|
+
id: p.id.value,
|
|
263
|
+
title: p.title,
|
|
264
|
+
content: p.content,
|
|
265
|
+
published: p.published,
|
|
266
|
+
authorId: user.id.value,
|
|
267
|
+
createdAt: p.createdAt,
|
|
268
|
+
updatedAt: p.updatedAt,
|
|
269
|
+
}))
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
// onUpdate is handled automatically by DrizzleBatchExecutor
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
---
|
|
278
|
+
|
|
279
|
+
### EntitySchemaRegistry — Collection Types
|
|
280
|
+
|
|
281
|
+
| Type | Behavior | Junction required? |
|
|
282
|
+
| ----------- | ------------------------------------------------------ | ------------------ |
|
|
283
|
+
| `owned` | 1:N — child rows belong to the parent; deletes cascade | No |
|
|
284
|
+
| `reference` | N:N — rows in a junction table | **Yes — always** |
|
|
285
|
+
|
|
286
|
+
Unlike Prisma, Drizzle does not manage implicit junction tables. Every `reference` collection must provide a `junction` config. Omitting it throws `MissingJunctionConfigError`.
|
|
287
|
+
|
|
288
|
+
---
|
|
289
|
+
|
|
290
|
+
### @Transactional Decorator
|
|
291
|
+
|
|
292
|
+
Wraps a method in a transaction automatically.
|
|
293
|
+
|
|
294
|
+
```typescript
|
|
295
|
+
import { Transactional } from "@woltz/rich-domain-drizzle";
|
|
296
|
+
|
|
297
|
+
class CreateUserUseCase {
|
|
298
|
+
constructor(
|
|
299
|
+
private readonly userRepo: UserRepository,
|
|
300
|
+
private readonly uow: DrizzleUnitOfWork // Required!
|
|
301
|
+
) {}
|
|
302
|
+
|
|
303
|
+
@Transactional()
|
|
304
|
+
async execute(input: CreateUserInput): Promise<User> {
|
|
305
|
+
const user = User.create({ ...input, posts: [] });
|
|
306
|
+
await this.userRepo.save(user);
|
|
307
|
+
return user;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
| Scenario | Behavior |
|
|
313
|
+
| ---------------------- | ----------------------- |
|
|
314
|
+
| Direct call | Creates new transaction |
|
|
315
|
+
| Already in transaction | Reuses existing one |
|
|
316
|
+
| Error thrown | Automatic rollback |
|
|
317
|
+
|
|
318
|
+
The class must expose a `uow` property of type `DrizzleUnitOfWork` for the decorator to find it.
|
|
319
|
+
|
|
320
|
+
---
|
|
321
|
+
|
|
322
|
+
### DrizzleBatchExecutor
|
|
323
|
+
|
|
324
|
+
Executes batch operations from `AggregateChanges` in the correct order for referential integrity:
|
|
325
|
+
|
|
326
|
+
1. **Deletes** — leaf → root (depth DESC)
|
|
327
|
+
2. **Creates** — root → leaf (depth ASC)
|
|
328
|
+
3. **Updates** — any order
|
|
329
|
+
|
|
330
|
+
`onUpdate` in `DrizzleToPersistence` is called automatically; you do not need to instantiate the executor directly.
|
|
331
|
+
|
|
332
|
+
---
|
|
333
|
+
|
|
334
|
+
## Connect / Disconnect (N:N)
|
|
335
|
+
|
|
336
|
+
```typescript
|
|
337
|
+
// Connect — adds a row to the junction table
|
|
338
|
+
const post = await postRepo.findById(postId);
|
|
339
|
+
post.addTag(new Tag({ id: new Id(tagId) }));
|
|
340
|
+
await postRepo.save(post);
|
|
341
|
+
|
|
342
|
+
// Disconnect — removes the row from the junction table
|
|
343
|
+
post.removeTag(new Tag({ id: new Id(tagId) }));
|
|
344
|
+
await postRepo.save(post);
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
---
|
|
348
|
+
|
|
349
|
+
## Transactions
|
|
350
|
+
|
|
351
|
+
```typescript
|
|
352
|
+
await uow.transaction(async () => {
|
|
353
|
+
const user = User.create({ name: "Alice", email: "alice@example.com", posts: [] });
|
|
354
|
+
await userRepo.save(user);
|
|
355
|
+
|
|
356
|
+
const post = Post.restore({ ... });
|
|
357
|
+
await postRepo.save(post);
|
|
358
|
+
// Both committed atomically, or both rolled back on error
|
|
359
|
+
});
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
---
|
|
363
|
+
|
|
364
|
+
## Limitations
|
|
365
|
+
|
|
366
|
+
- **Criteria dot-field paths are not supported.** Filters and ordering must reference top-level columns of the primary table. `"posts.title"` or `"profile.name"` throws `DrizzleAdapterError`. Add custom repository methods with explicit JOINs instead.
|
|
367
|
+
- **`contains` / `startsWith` / `endsWith`** use `ILIKE` — PostgreSQL only.
|
|
368
|
+
|
|
369
|
+
---
|
|
370
|
+
|
|
371
|
+
## Error Reference
|
|
372
|
+
|
|
373
|
+
| Error | When thrown |
|
|
374
|
+
| ---------------------------- | ------------------------------------------------------------------ |
|
|
375
|
+
| `TableNotFoundError` | `tableMap` key not found for an entity or junction name |
|
|
376
|
+
| `MissingJunctionConfigError` | `reference` collection has no `junction` configured |
|
|
377
|
+
| `BatchOperationError` | DB error during a batch create, update, or delete |
|
|
378
|
+
| `NoRecordsAffectedError` | `delete()` / `deleteById()` matched 0 rows |
|
|
379
|
+
| `DrizzleAdapterError` | Unsupported Criteria operator, dot-field path, or column not found |
|
|
380
|
+
|
|
381
|
+
---
|
|
382
|
+
|
|
383
|
+
## Full Example
|
|
384
|
+
|
|
385
|
+
See the [fastify-with-drizzle example](https://github.com/tarcisioandrade/rich-domain/tree/main/examples/backend/fastify-with-drizzle) for a complete working application.
|
|
386
|
+
|
|
387
|
+
---
|
|
388
|
+
|
|
389
|
+
## License
|
|
390
|
+
|
|
391
|
+
MIT
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Mapper, AggregateChanges, EntitySchemaRegistry } from "@woltz/rich-domain";
|
|
2
2
|
import { DrizzleClient, DrizzleUnitOfWork } from "../unit-of-work";
|
|
3
|
-
export declare abstract class DrizzleToPersistence<TDomain> extends Mapper<TDomain, void> {
|
|
3
|
+
export declare abstract class DrizzleToPersistence<TDomain, TDb extends DrizzleClient = DrizzleClient> extends Mapper<TDomain, void> {
|
|
4
|
+
protected readonly db: TDb;
|
|
4
5
|
protected readonly uow: DrizzleUnitOfWork;
|
|
5
|
-
constructor(uow: DrizzleUnitOfWork);
|
|
6
|
+
constructor(db: TDb, uow: DrizzleUnitOfWork);
|
|
6
7
|
/**
|
|
7
8
|
* Schema registry for field mapping (entity → table, field → column).
|
|
8
9
|
*/
|
|
@@ -19,14 +20,10 @@ export declare abstract class DrizzleToPersistence<TDomain> extends Mapper<TDoma
|
|
|
19
20
|
* ```
|
|
20
21
|
*/
|
|
21
22
|
protected abstract readonly tableMap: Map<string, any>;
|
|
22
|
-
/**
|
|
23
|
-
* Get the raw db instance.
|
|
24
|
-
*/
|
|
25
|
-
protected abstract getDb(): DrizzleClient;
|
|
26
23
|
/**
|
|
27
24
|
* Get current context (transaction client or raw db).
|
|
28
25
|
*/
|
|
29
|
-
protected get context():
|
|
26
|
+
protected get context(): TDb;
|
|
30
27
|
/**
|
|
31
28
|
* Build persistence operations.
|
|
32
29
|
*/
|
|
@@ -1 +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,
|
|
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,CACxC,OAAO,EACP,GAAG,SAAS,aAAa,GAAG,aAAa,CACzC,SAAQ,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC;IAE3B,SAAS,CAAC,QAAQ,CAAC,EAAE,EAAE,GAAG;IAC1B,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,iBAAiB;gBADtB,EAAE,EAAE,GAAG,EACP,GAAG,EAAE,iBAAiB;IAK3C;;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,KAAK,OAAO,IAAI,GAAG,CAG3B;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"}
|
|
@@ -14,9 +14,11 @@ const rich_domain_1 = require("@woltz/rich-domain");
|
|
|
14
14
|
const unit_of_work_1 = require("../unit-of-work");
|
|
15
15
|
const batch_executor_1 = require("../batch-executor");
|
|
16
16
|
class DrizzleToPersistence extends rich_domain_1.Mapper {
|
|
17
|
+
db;
|
|
17
18
|
uow;
|
|
18
|
-
constructor(uow) {
|
|
19
|
+
constructor(db, uow) {
|
|
19
20
|
super();
|
|
21
|
+
this.db = db;
|
|
20
22
|
this.uow = uow;
|
|
21
23
|
}
|
|
22
24
|
/**
|
|
@@ -24,7 +26,7 @@ class DrizzleToPersistence extends rich_domain_1.Mapper {
|
|
|
24
26
|
*/
|
|
25
27
|
get context() {
|
|
26
28
|
const ctx = unit_of_work_1.UOWStorage.getStore()?.ctx;
|
|
27
|
-
return ctx?.client ?? this.
|
|
29
|
+
return (ctx?.client ?? this.db);
|
|
28
30
|
}
|
|
29
31
|
/**
|
|
30
32
|
* Build persistence operations.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to-persistence.js","sourceRoot":"","sources":["../../../src/mappers/to-persistence.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,oDAI4B;AAC5B,kDAKyB;AACzB,sDAAyD;AAEzD,MAAsB,
|
|
1
|
+
{"version":3,"file":"to-persistence.js","sourceRoot":"","sources":["../../../src/mappers/to-persistence.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,oDAI4B;AAC5B,kDAKyB;AACzB,sDAAyD;AAEzD,MAAsB,oBAGpB,SAAQ,oBAAqB;IAER;IACA;IAFrB,YACqB,EAAO,EACP,GAAsB;QAEzC,KAAK,EAAE,CAAC;QAHW,OAAE,GAAF,EAAE,CAAK;QACP,QAAG,GAAH,GAAG,CAAmB;IAG3C,CAAC;IAoBD;;OAEG;IACH,IAAc,OAAO;QACnB,MAAM,GAAG,GAAG,yBAAU,CAAC,QAAQ,EAAE,EAAE,GAAG,CAAC;QACvC,OAAO,CAAC,GAAG,EAAE,MAAM,IAAI,IAAI,CAAC,EAAE,CAAQ,CAAC;IACzC,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,qCAAoB,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;AArFD,oDAqFC;AAXe;IADb,IAAA,4BAAa,GAAE;;;;wDAWf"}
|
package/dist/cjs/repository.d.ts
CHANGED
|
@@ -2,24 +2,24 @@ import { Aggregate, Repository, Mapper, Criteria, PaginatedResult } from "@woltz
|
|
|
2
2
|
import { DrizzleClient, DrizzleUnitOfWork } from "./unit-of-work";
|
|
3
3
|
import { DrizzleToPersistence } from "./mappers/to-persistence";
|
|
4
4
|
import { SearchableField } from "./query-builder";
|
|
5
|
-
export interface DrizzleRepositoryConfig<TDomain, TPersistence> {
|
|
6
|
-
db:
|
|
5
|
+
export interface DrizzleRepositoryConfig<TDomain, TPersistence, TDb extends DrizzleClient = DrizzleClient> {
|
|
6
|
+
db: TDb;
|
|
7
7
|
table: any;
|
|
8
8
|
toDomainMapper: Mapper<TPersistence, TDomain>;
|
|
9
|
-
toPersistenceMapper: DrizzleToPersistence<TDomain>;
|
|
9
|
+
toPersistenceMapper: DrizzleToPersistence<TDomain, TDb>;
|
|
10
10
|
uow: DrizzleUnitOfWork;
|
|
11
11
|
}
|
|
12
|
-
export declare abstract class DrizzleRepository<TDomain extends Aggregate<any>, TPersistence> extends Repository<TDomain> {
|
|
13
|
-
protected readonly db:
|
|
12
|
+
export declare abstract class DrizzleRepository<TDomain extends Aggregate<any>, TPersistence, TDb extends DrizzleClient = DrizzleClient> extends Repository<TDomain> {
|
|
13
|
+
protected readonly db: TDb;
|
|
14
14
|
protected readonly table: any;
|
|
15
15
|
protected readonly toDomainMapper: Mapper<TPersistence, TDomain>;
|
|
16
|
-
protected readonly toPersistenceMapper: DrizzleToPersistence<TDomain>;
|
|
16
|
+
protected readonly toPersistenceMapper: DrizzleToPersistence<TDomain, TDb>;
|
|
17
17
|
protected readonly uow: DrizzleUnitOfWork;
|
|
18
|
-
constructor(config: DrizzleRepositoryConfig<TDomain, TPersistence>);
|
|
18
|
+
constructor(config: DrizzleRepositoryConfig<TDomain, TPersistence, TDb>);
|
|
19
19
|
/**
|
|
20
20
|
* Returns tx from UOWStorage if inside a transaction, otherwise the raw db.
|
|
21
21
|
*/
|
|
22
|
-
protected get context():
|
|
22
|
+
protected get context(): TDb;
|
|
23
23
|
/**
|
|
24
24
|
* The table name string used by EntitySchemaRegistry and db.query accessor.
|
|
25
25
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"repository.d.ts","sourceRoot":"","sources":["../../src/repository.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,UAAU,EACV,MAAM,EACN,QAAQ,EACR,eAAe,EAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAc,MAAM,gBAAgB,CAAC;AAE9E,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAuB,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEvE,MAAM,WAAW,uBAAuB,
|
|
1
|
+
{"version":3,"file":"repository.d.ts","sourceRoot":"","sources":["../../src/repository.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,UAAU,EACV,MAAM,EACN,QAAQ,EACR,eAAe,EAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAA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Y,CAAC,EAAE;IAEzE;;OAEG;IACH,SAAS,CAAC,mBAAmB,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAI9C,IAAI,CAAC,QAAQ,EAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;IAqDpE,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IA8B7C,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IA4BhD,KAAK,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;IAkBpD,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAQpC,IAAI,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAKpC,MAAM,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAiBtC,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBrC,WAAW,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAIxD,OAAO,CAAC,6BAA6B;CAOtC"}
|
package/dist/cjs/repository.js
CHANGED
|
@@ -25,7 +25,7 @@ class DrizzleRepository extends rich_domain_1.Repository {
|
|
|
25
25
|
*/
|
|
26
26
|
get context() {
|
|
27
27
|
const ctx = unit_of_work_1.UOWStorage.getStore()?.ctx;
|
|
28
|
-
return ctx?.client ?? this.db;
|
|
28
|
+
return (ctx?.client ?? this.db);
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
31
|
* Relations to include when fetching (for Drizzle relational query API).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"repository.js","sourceRoot":"","sources":["../../src/repository.ts"],"names":[],"mappings":";;;AAAA,6CAAsD;AACtD,oDAM4B;AAC5B,iDAA8E;AAC9E,qCAAkD;AAElD,mDAAuE;
|
|
1
|
+
{"version":3,"file":"repository.js","sourceRoot":"","sources":["../../src/repository.ts"],"names":[],"mappings":";;;AAAA,6CAAsD;AACtD,oDAM4B;AAC5B,iDAA8E;AAC9E,qCAAkD;AAElD,mDAAuE;AAcvE,MAAsB,iBAIpB,SAAQ,wBAAmB;IACR,EAAE,CAAM;IACR,KAAK,CAAM;IACX,cAAc,CAAgC;IAC9C,mBAAmB,CAAqC;IACxD,GAAG,CAAoB;IAE1C,YAAY,MAA2D;QACrE,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC;QACpB,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QAC1B,IAAI,CAAC,cAAc,GAAG,MAAM,CAAC,cAAc,CAAC;QAC5C,IAAI,CAAC,mBAAmB,GAAG,MAAM,CAAC,mBAAmB,CAAC;QACtD,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC;IACxB,CAAC;IAED;;OAEG;IACH,IAAc,OAAO;QACnB,MAAM,GAAG,GAAG,yBAAU,CAAC,QAAQ,EAAE,EAAE,GAAG,CAAC;QACvC,OAAO,CAAC,GAAG,EAAE,MAAM,IAAI,IAAI,CAAC,EAAE,CAAQ,CAAC;IACzC,CAAC;IAYD;;OAEG;IACO,mBAAmB;QAC3B,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,QAA2B;QACpC,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,mCAAmB,CAAC,KAAK,CACjE,QAAQ,EACR,IAAI,CAAC,KAAK,EACV,IAAI,CAAC,mBAAmB,EAAE,CAC3B,CAAC;QAEF,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK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YAC7D,CAAC;QACH,CAAC;QAAC,OAAO,KAAU,EAAE,CAAC;YACpB,IAAI,KAAK,YAAY,+BAAsB;gBAAE,MAAM,KAAK,CAAC;YACzD,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;IAED,KAAK,CAAC,WAAW,CAAI,IAAsB;QACzC,OAAO,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC;IAEO,6BAA6B,CAAC,QAAmB;QACvD,KAAK,MAAM,MAAM,IAAI,QAAQ,EAAE,CAAC;YAC9B,IAAI,MAAM,YAAY,uBAAS,EAAE,CAAC;gBAChC,MAAM,CAAC,WAAW,EAAE,CAAC;YACvB,CAAC;QACH,CAAC;IACH,CAAC;CACF;AAvOD,8CAuOC"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Mapper, AggregateChanges, EntitySchemaRegistry } from "@woltz/rich-domain";
|
|
2
2
|
import { DrizzleClient, DrizzleUnitOfWork } from "../unit-of-work";
|
|
3
|
-
export declare abstract class DrizzleToPersistence<TDomain> extends Mapper<TDomain, void> {
|
|
3
|
+
export declare abstract class DrizzleToPersistence<TDomain, TDb extends DrizzleClient = DrizzleClient> extends Mapper<TDomain, void> {
|
|
4
|
+
protected readonly db: TDb;
|
|
4
5
|
protected readonly uow: DrizzleUnitOfWork;
|
|
5
|
-
constructor(uow: DrizzleUnitOfWork);
|
|
6
|
+
constructor(db: TDb, uow: DrizzleUnitOfWork);
|
|
6
7
|
/**
|
|
7
8
|
* Schema registry for field mapping (entity → table, field → column).
|
|
8
9
|
*/
|
|
@@ -19,14 +20,10 @@ export declare abstract class DrizzleToPersistence<TDomain> extends Mapper<TDoma
|
|
|
19
20
|
* ```
|
|
20
21
|
*/
|
|
21
22
|
protected abstract readonly tableMap: Map<string, any>;
|
|
22
|
-
/**
|
|
23
|
-
* Get the raw db instance.
|
|
24
|
-
*/
|
|
25
|
-
protected abstract getDb(): DrizzleClient;
|
|
26
23
|
/**
|
|
27
24
|
* Get current context (transaction client or raw db).
|
|
28
25
|
*/
|
|
29
|
-
protected get context():
|
|
26
|
+
protected get context(): TDb;
|
|
30
27
|
/**
|
|
31
28
|
* Build persistence operations.
|
|
32
29
|
*/
|
|
@@ -1 +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,
|
|
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,CACxC,OAAO,EACP,GAAG,SAAS,aAAa,GAAG,aAAa,CACzC,SAAQ,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC;IAE3B,SAAS,CAAC,QAAQ,CAAC,EAAE,EAAE,GAAG;IAC1B,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,iBAAiB;gBADtB,EAAE,EAAE,GAAG,EACP,GAAG,EAAE,iBAAiB;IAK3C;;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,KAAK,OAAO,IAAI,GAAG,CAG3B;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"}
|
|
@@ -11,9 +11,11 @@ import { Mapper, } from "@woltz/rich-domain";
|
|
|
11
11
|
import { UOWStorage, Transactional, } from "../unit-of-work";
|
|
12
12
|
import { DrizzleBatchExecutor } from "../batch-executor";
|
|
13
13
|
export class DrizzleToPersistence extends Mapper {
|
|
14
|
+
db;
|
|
14
15
|
uow;
|
|
15
|
-
constructor(uow) {
|
|
16
|
+
constructor(db, uow) {
|
|
16
17
|
super();
|
|
18
|
+
this.db = db;
|
|
17
19
|
this.uow = uow;
|
|
18
20
|
}
|
|
19
21
|
/**
|
|
@@ -21,7 +23,7 @@ export class DrizzleToPersistence extends Mapper {
|
|
|
21
23
|
*/
|
|
22
24
|
get context() {
|
|
23
25
|
const ctx = UOWStorage.getStore()?.ctx;
|
|
24
|
-
return ctx?.client ?? this.
|
|
26
|
+
return (ctx?.client ?? this.db);
|
|
25
27
|
}
|
|
26
28
|
/**
|
|
27
29
|
* Build persistence operations.
|
|
@@ -1 +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,
|
|
1
|
+
{"version":3,"file":"to-persistence.js","sourceRoot":"","sources":["../../../src/mappers/to-persistence.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,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"}
|
package/dist/esm/repository.d.ts
CHANGED
|
@@ -2,24 +2,24 @@ import { Aggregate, Repository, Mapper, Criteria, PaginatedResult } from "@woltz
|
|
|
2
2
|
import { DrizzleClient, DrizzleUnitOfWork } from "./unit-of-work";
|
|
3
3
|
import { DrizzleToPersistence } from "./mappers/to-persistence";
|
|
4
4
|
import { SearchableField } from "./query-builder";
|
|
5
|
-
export interface DrizzleRepositoryConfig<TDomain, TPersistence> {
|
|
6
|
-
db:
|
|
5
|
+
export interface DrizzleRepositoryConfig<TDomain, TPersistence, TDb extends DrizzleClient = DrizzleClient> {
|
|
6
|
+
db: TDb;
|
|
7
7
|
table: any;
|
|
8
8
|
toDomainMapper: Mapper<TPersistence, TDomain>;
|
|
9
|
-
toPersistenceMapper: DrizzleToPersistence<TDomain>;
|
|
9
|
+
toPersistenceMapper: DrizzleToPersistence<TDomain, TDb>;
|
|
10
10
|
uow: DrizzleUnitOfWork;
|
|
11
11
|
}
|
|
12
|
-
export declare abstract class DrizzleRepository<TDomain extends Aggregate<any>, TPersistence> extends Repository<TDomain> {
|
|
13
|
-
protected readonly db:
|
|
12
|
+
export declare abstract class DrizzleRepository<TDomain extends Aggregate<any>, TPersistence, TDb extends DrizzleClient = DrizzleClient> extends Repository<TDomain> {
|
|
13
|
+
protected readonly db: TDb;
|
|
14
14
|
protected readonly table: any;
|
|
15
15
|
protected readonly toDomainMapper: Mapper<TPersistence, TDomain>;
|
|
16
|
-
protected readonly toPersistenceMapper: DrizzleToPersistence<TDomain>;
|
|
16
|
+
protected readonly toPersistenceMapper: DrizzleToPersistence<TDomain, TDb>;
|
|
17
17
|
protected readonly uow: DrizzleUnitOfWork;
|
|
18
|
-
constructor(config: DrizzleRepositoryConfig<TDomain, TPersistence>);
|
|
18
|
+
constructor(config: DrizzleRepositoryConfig<TDomain, TPersistence, TDb>);
|
|
19
19
|
/**
|
|
20
20
|
* Returns tx from UOWStorage if inside a transaction, otherwise the raw db.
|
|
21
21
|
*/
|
|
22
|
-
protected get context():
|
|
22
|
+
protected get context(): TDb;
|
|
23
23
|
/**
|
|
24
24
|
* The table name string used by EntitySchemaRegistry and db.query accessor.
|
|
25
25
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"repository.d.ts","sourceRoot":"","sources":["../../src/repository.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,UAAU,EACV,MAAM,EACN,QAAQ,EACR,eAAe,EAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAc,MAAM,gBAAgB,CAAC;AAE9E,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAuB,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEvE,MAAM,WAAW,uBAAuB,
|
|
1
|
+
{"version":3,"file":"repository.d.ts","sourceRoot":"","sources":["../../src/repository.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,UAAU,EACV,MAAM,EACN,QAAQ,EACR,eAAe,EAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAc,MAAM,gBAAgB,CAAC;AAE9E,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAuB,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEvE,MAAM,WAAW,uBAAuB,CACtC,OAAO,EACP,YAAY,EACZ,GAAG,SAAS,aAAa,GAAG,aAAa;IAEzC,EAAE,EAAE,GAAG,CAAC;IACR,KAAK,EAAE,GAAG,CA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