@med1802/repository-manager 3.1.0 → 3.1.1
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 +249 -212
- package/dist/core/index.d.ts +0 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +0 -1
- package/dist/core/middleware.d.ts +1 -1
- package/dist/core/middleware.d.ts.map +1 -1
- package/dist/core/repository.d.ts +2 -2
- package/dist/core/repository.d.ts.map +1 -1
- package/dist/core/repository.js +6 -6
- package/dist/infrastructure/logger.d.ts +2 -2
- package/dist/infrastructure/logger.d.ts.map +1 -1
- package/dist/manager.d.ts +2 -3
- package/dist/manager.d.ts.map +1 -1
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +3 -0
- package/dist/types/observer.types.d.ts.map +1 -0
- package/dist/types/repository.types.d.ts +22 -0
- package/dist/types/repository.types.d.ts.map +1 -0
- package/dist/types/workspace.types.d.ts +8 -0
- package/dist/types/workspace.types.d.ts.map +1 -0
- package/dist/workspace/client.d.ts +2 -3
- package/dist/workspace/client.d.ts.map +1 -1
- package/dist/workspace/client.js +1 -24
- package/dist/workspace/context.d.ts +6 -6
- package/dist/workspace/context.d.ts.map +1 -1
- package/dist/workspace/context.js +8 -11
- package/dist/workspace/index.d.ts +0 -1
- package/dist/workspace/index.d.ts.map +1 -1
- package/dist/workspace/index.js +0 -1
- package/dist/workspace/mount.d.ts +3 -0
- package/dist/workspace/mount.d.ts.map +1 -0
- package/dist/workspace/mount.js +30 -0
- package/package.json +1 -1
- package/src/core/index.ts +0 -1
- package/src/core/middleware.ts +1 -1
- package/src/core/repository.ts +9 -9
- package/src/infrastructure/logger.ts +4 -2
- package/src/manager.ts +3 -2
- package/src/types/index.ts +3 -0
- package/src/{core/types.ts → types/repository.types.ts} +12 -12
- package/src/types/workspace.types.ts +8 -0
- package/src/workspace/client.ts +4 -37
- package/src/workspace/context.ts +21 -21
- package/src/workspace/index.ts +0 -1
- package/src/workspace/mount.ts +35 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/core/types.d.ts +0 -22
- package/dist/core/types.d.ts.map +0 -1
- package/dist/workspace/types/configuration.types.d.ts +0 -6
- package/dist/workspace/types/configuration.types.d.ts.map +0 -1
- package/dist/workspace/types/index.d.ts +0 -3
- package/dist/workspace/types/index.d.ts.map +0 -1
- package/dist/workspace/types/index.js +0 -2
- package/dist/workspace/types/observer.types.d.ts.map +0 -1
- package/src/workspace/types/configuration.types.ts +0 -5
- package/src/workspace/types/index.ts +0 -2
- /package/dist/{workspace/types → types}/observer.types.d.ts +0 -0
- /package/dist/{workspace/types → types}/observer.types.js +0 -0
- /package/dist/{core/types.js → types/repository.types.js} +0 -0
- /package/dist/{workspace/types/configuration.types.js → types/workspace.types.js} +0 -0
- /package/src/{workspace/types → types}/observer.types.ts +0 -0
package/README.md
CHANGED
|
@@ -4,13 +4,14 @@ A lightweight, type-safe repository manager with dependency injection, event-dri
|
|
|
4
4
|
|
|
5
5
|
## ✨ Features
|
|
6
6
|
|
|
7
|
-
- ✅ **Dependency Injection** - Inject
|
|
7
|
+
- ✅ **Dependency Injection** - Inject dependencies into repositories
|
|
8
8
|
- ✅ **Event-Driven Architecture** - Built-in observer pattern for inter-repository communication
|
|
9
|
+
- ✅ **Plugin System** - Define repositories as plugins
|
|
9
10
|
- ✅ **Lifecycle Management** - Automatic connection/disconnection with reference counting
|
|
10
11
|
- ✅ **Multi-Workspace Support** - Manage multiple isolated workspaces with different dependencies
|
|
11
12
|
- ✅ **Type Safety** - Full TypeScript support with generics
|
|
12
13
|
- ✅ **Lazy Initialization** - Repository instances are created only when needed
|
|
13
|
-
- ✅ **
|
|
14
|
+
- ✅ **Clean API** - Simple `createWorkspace` pattern
|
|
14
15
|
- ✅ **Logging** - Built-in logging with colored console output
|
|
15
16
|
- ✅ **Memory Efficient** - Automatic cleanup when no connections remain
|
|
16
17
|
- ✅ **Middleware Support** - Intercept and modify repository method calls
|
|
@@ -36,8 +37,8 @@ interface IUserRepository {
|
|
|
36
37
|
// Create manager instance
|
|
37
38
|
const manager = repositoryManager();
|
|
38
39
|
|
|
39
|
-
// Define
|
|
40
|
-
const
|
|
40
|
+
// Define dependencies
|
|
41
|
+
const dependencies = {
|
|
41
42
|
httpClient: {
|
|
42
43
|
get: async (url: string) => fetch(url).then((r) => r.json()),
|
|
43
44
|
post: async (url: string, data: any) =>
|
|
@@ -45,40 +46,40 @@ const infrastructure = {
|
|
|
45
46
|
},
|
|
46
47
|
};
|
|
47
48
|
|
|
48
|
-
// Create workspace
|
|
49
|
-
const {
|
|
49
|
+
// Create workspace with plugins
|
|
50
|
+
const { queryRepository } = manager.createWorkspace({
|
|
50
51
|
id: "app",
|
|
51
52
|
logging: true,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
53
|
+
dependencies,
|
|
54
|
+
plugins: () => [
|
|
55
|
+
{
|
|
56
|
+
id: "user-repo",
|
|
57
|
+
install({ instance }): IUserRepository {
|
|
58
|
+
const { dependencies, observer } = instance;
|
|
59
|
+
return {
|
|
60
|
+
async getUsers() {
|
|
61
|
+
return dependencies.httpClient.get("/api/users");
|
|
62
|
+
},
|
|
63
|
+
async createUser(user) {
|
|
64
|
+
const result = await dependencies.httpClient.post("/api/users", user);
|
|
65
|
+
// Notify other repositories about new user
|
|
66
|
+
observer.dispatch({
|
|
67
|
+
type: "user.created",
|
|
68
|
+
repositoryId: "user-repo",
|
|
69
|
+
message: result,
|
|
70
|
+
});
|
|
71
|
+
return result;
|
|
72
|
+
},
|
|
73
|
+
};
|
|
63
74
|
},
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
// Notify other repositories about new user
|
|
67
|
-
observer.dispatch({
|
|
68
|
-
type: "user.created",
|
|
69
|
-
repositoryId: "user-repo",
|
|
70
|
-
message: result,
|
|
71
|
-
});
|
|
72
|
-
return result;
|
|
75
|
+
onConnect: () => {
|
|
76
|
+
console.log("User repository initialized");
|
|
73
77
|
},
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
onDisconnect: () => {
|
|
80
|
-
console.log("User repository cleaned up");
|
|
81
|
-
},
|
|
78
|
+
onDisconnect: () => {
|
|
79
|
+
console.log("User repository cleaned up");
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
],
|
|
82
83
|
});
|
|
83
84
|
|
|
84
85
|
// Query and use repository
|
|
@@ -111,19 +112,22 @@ workspace.queryRepository("repo-id"); // Query repositories
|
|
|
111
112
|
|
|
112
113
|
Workspace is the **entry point** for all operations. It encapsulates:
|
|
113
114
|
|
|
114
|
-
-
|
|
115
|
-
-
|
|
115
|
+
- Dependencies (shared services/infrastructure)
|
|
116
|
+
- Plugin definitions (repositories)
|
|
116
117
|
- Observer system for events
|
|
117
118
|
- Logging configuration
|
|
118
119
|
|
|
119
120
|
```typescript
|
|
120
|
-
const {
|
|
121
|
+
const { queryRepository } = manager.createWorkspace({
|
|
121
122
|
id: "app-workspace",
|
|
122
123
|
logging: true,
|
|
123
|
-
|
|
124
|
+
dependencies: {
|
|
124
125
|
httpClient: myHttpClient,
|
|
125
126
|
cache: myCache,
|
|
126
127
|
},
|
|
128
|
+
plugins: () => [
|
|
129
|
+
// Repository plugins defined here
|
|
130
|
+
],
|
|
127
131
|
});
|
|
128
132
|
```
|
|
129
133
|
|
|
@@ -132,44 +136,49 @@ const { defineRepository, queryRepository } = manager.createWorkspace({
|
|
|
132
136
|
The built-in observer enables **event-driven inter-repository communication**:
|
|
133
137
|
|
|
134
138
|
```typescript
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
139
|
+
const { queryRepository } = manager.createWorkspace({
|
|
140
|
+
id: "app",
|
|
141
|
+
dependencies,
|
|
142
|
+
plugins: () => [
|
|
143
|
+
// Repository that dispatches events
|
|
144
|
+
{
|
|
145
|
+
id: "user-repo",
|
|
146
|
+
install({ instance }) {
|
|
147
|
+
const { observer } = instance;
|
|
148
|
+
return {
|
|
149
|
+
async createUser(user) {
|
|
150
|
+
const result = await saveUser(user);
|
|
151
|
+
// Notify other repositories
|
|
152
|
+
observer.dispatch({
|
|
153
|
+
type: "user.created",
|
|
154
|
+
repositoryId: "user-repo",
|
|
155
|
+
message: result,
|
|
156
|
+
});
|
|
157
|
+
return result;
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
// Repository that subscribes to events
|
|
163
|
+
{
|
|
164
|
+
id: "notification-repo",
|
|
165
|
+
install({ instance }) {
|
|
166
|
+
const { observer } = instance;
|
|
167
|
+
|
|
168
|
+
// Subscribe to user events
|
|
169
|
+
observer.subscribe((payload) => {
|
|
170
|
+
if (payload.type === "user.created") {
|
|
171
|
+
console.log("New user:", payload.message);
|
|
172
|
+
// Send welcome email, etc.
|
|
173
|
+
}
|
|
148
174
|
});
|
|
149
|
-
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
// repository methods...
|
|
178
|
+
};
|
|
150
179
|
},
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
// Another repository can subscribe to these events
|
|
156
|
-
defineRepository({
|
|
157
|
-
id: "notification-repo",
|
|
158
|
-
install({ instance }) {
|
|
159
|
-
const { observer } = instance;
|
|
160
|
-
|
|
161
|
-
// Subscribe to user events
|
|
162
|
-
observer.subscribe((payload) => {
|
|
163
|
-
if (payload.type === "user.created") {
|
|
164
|
-
console.log("New user:", payload.message);
|
|
165
|
-
// Send welcome email, etc.
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
return {
|
|
170
|
-
// repository methods...
|
|
171
|
-
};
|
|
172
|
-
},
|
|
180
|
+
},
|
|
181
|
+
],
|
|
173
182
|
});
|
|
174
183
|
```
|
|
175
184
|
|
|
@@ -187,45 +196,53 @@ Creates a repository manager instance.
|
|
|
187
196
|
const manager = repositoryManager();
|
|
188
197
|
```
|
|
189
198
|
|
|
190
|
-
### `manager.createWorkspace<
|
|
199
|
+
### `manager.createWorkspace<D>(config)`
|
|
191
200
|
|
|
192
|
-
Creates a workspace with
|
|
201
|
+
Creates a workspace with dependencies and plugins.
|
|
193
202
|
|
|
194
203
|
**Parameters:**
|
|
195
204
|
|
|
196
|
-
- `config:
|
|
205
|
+
- `config: IWorkspaceConfig<D>`
|
|
197
206
|
- `id: string` - Unique identifier for the workspace
|
|
198
|
-
- `
|
|
207
|
+
- `dependencies: D` - Dependencies to inject into repositories
|
|
208
|
+
- `plugins: () => IPlugin[]` - Function that returns array of repository plugins
|
|
199
209
|
- `logging?: boolean` - Enable/disable logging (default: `false`)
|
|
200
210
|
|
|
201
211
|
**Returns:** Object with workspace methods:
|
|
202
212
|
|
|
203
|
-
- `defineRepository` - Define repositories
|
|
204
213
|
- `queryRepository` - Query repositories
|
|
205
214
|
|
|
206
215
|
**Example:**
|
|
207
216
|
|
|
208
217
|
```typescript
|
|
209
|
-
const {
|
|
218
|
+
const { queryRepository } = manager.createWorkspace({
|
|
210
219
|
id: "app",
|
|
211
|
-
|
|
220
|
+
dependencies: {
|
|
212
221
|
httpClient: myHttpClient,
|
|
213
222
|
database: myDb,
|
|
214
223
|
},
|
|
224
|
+
plugins: () => [
|
|
225
|
+
{
|
|
226
|
+
id: "user-repo",
|
|
227
|
+
install({ instance }) {
|
|
228
|
+
// repository implementation
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
],
|
|
215
232
|
logging: true,
|
|
216
233
|
});
|
|
217
234
|
```
|
|
218
235
|
|
|
219
|
-
###
|
|
236
|
+
### Plugin Definition
|
|
220
237
|
|
|
221
|
-
Defines a repository within the workspace.
|
|
238
|
+
Defines a repository plugin within the workspace.
|
|
222
239
|
|
|
223
|
-
**
|
|
240
|
+
**Plugin Structure:**
|
|
224
241
|
|
|
225
|
-
- `
|
|
242
|
+
- `IPlugin<D, R>`
|
|
226
243
|
- `id: string` - Unique identifier for the repository
|
|
227
244
|
- `install: ({ instance }) => R` - Factory function that returns repository instance
|
|
228
|
-
- `instance.
|
|
245
|
+
- `instance.dependencies` - Injected dependencies
|
|
229
246
|
- `instance.observer` - Observer for event-driven communication
|
|
230
247
|
- `onConnect?: () => void` - Called when repository is first connected
|
|
231
248
|
- `onDisconnect?: () => void` - Called when repository is last disconnected
|
|
@@ -234,32 +251,34 @@ Defines a repository within the workspace.
|
|
|
234
251
|
**Example:**
|
|
235
252
|
|
|
236
253
|
```typescript
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
254
|
+
plugins: () => [
|
|
255
|
+
{
|
|
256
|
+
id: "user-repo",
|
|
257
|
+
install({ instance }): IUserRepository {
|
|
258
|
+
const { dependencies, observer } = instance;
|
|
259
|
+
|
|
260
|
+
// Subscribe to events
|
|
261
|
+
observer.subscribe((payload) => {
|
|
262
|
+
console.log("Event received:", payload);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
return {
|
|
266
|
+
async createUser(user) {
|
|
267
|
+
const result = await dependencies.httpClient.post("/users", user);
|
|
268
|
+
// Dispatch event
|
|
269
|
+
observer.dispatch({
|
|
270
|
+
type: "user.created",
|
|
271
|
+
repositoryId: "user-repo",
|
|
272
|
+
message: result,
|
|
273
|
+
});
|
|
274
|
+
return result;
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
},
|
|
278
|
+
onConnect: () => console.log("Connected"),
|
|
279
|
+
onDisconnect: () => console.log("Disconnected"),
|
|
259
280
|
},
|
|
260
|
-
|
|
261
|
-
onDisconnect: () => console.log("Disconnected"),
|
|
262
|
-
});
|
|
281
|
+
]
|
|
263
282
|
```
|
|
264
283
|
|
|
265
284
|
### `queryRepository<R>(id)`
|
|
@@ -356,73 +375,80 @@ The built-in observer system enables decoupled communication between repositorie
|
|
|
356
375
|
Repositories can communicate through the observer system:
|
|
357
376
|
|
|
358
377
|
```typescript
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
378
|
+
const { queryRepository } = manager.createWorkspace({
|
|
379
|
+
id: "app",
|
|
380
|
+
dependencies: {
|
|
381
|
+
httpClient: myHttpClient,
|
|
382
|
+
emailService: myEmailService,
|
|
383
|
+
},
|
|
384
|
+
plugins: () => [
|
|
385
|
+
// User repository dispatches events
|
|
386
|
+
{
|
|
387
|
+
id: "user-repo",
|
|
388
|
+
install({ instance }) {
|
|
389
|
+
const { dependencies, observer } = instance;
|
|
390
|
+
return {
|
|
391
|
+
async createUser(user) {
|
|
392
|
+
const result = await dependencies.httpClient.post("/users", user);
|
|
393
|
+
observer.dispatch({
|
|
394
|
+
type: "user.created",
|
|
395
|
+
repositoryId: "user-repo",
|
|
396
|
+
message: result,
|
|
397
|
+
});
|
|
398
|
+
return result;
|
|
399
|
+
},
|
|
400
|
+
async deleteUser(userId) {
|
|
401
|
+
await dependencies.httpClient.delete(`/users/${userId}`);
|
|
402
|
+
observer.dispatch({
|
|
403
|
+
type: "user.deleted",
|
|
404
|
+
repositoryId: "user-repo",
|
|
405
|
+
message: { userId },
|
|
406
|
+
});
|
|
407
|
+
},
|
|
408
|
+
};
|
|
373
409
|
},
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
410
|
+
},
|
|
411
|
+
// Notification repository subscribes to user events
|
|
412
|
+
{
|
|
413
|
+
id: "notification-repo",
|
|
414
|
+
install({ instance }) {
|
|
415
|
+
const { observer, dependencies } = instance;
|
|
416
|
+
|
|
417
|
+
observer.subscribe((payload) => {
|
|
418
|
+
if (payload.type === "user.created") {
|
|
419
|
+
dependencies.emailService.send({
|
|
420
|
+
to: payload.message.email,
|
|
421
|
+
subject: "Welcome!",
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
if (payload.type === "user.deleted") {
|
|
425
|
+
console.log("User deleted:", payload.message.userId);
|
|
426
|
+
}
|
|
380
427
|
});
|
|
381
|
-
},
|
|
382
|
-
};
|
|
383
|
-
},
|
|
384
|
-
});
|
|
385
428
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
429
|
+
return {
|
|
430
|
+
// notification methods...
|
|
431
|
+
};
|
|
432
|
+
},
|
|
433
|
+
},
|
|
434
|
+
// Analytics repository also subscribes
|
|
435
|
+
{
|
|
436
|
+
id: "analytics-repo",
|
|
437
|
+
install({ instance }) {
|
|
438
|
+
const { observer } = instance;
|
|
439
|
+
|
|
440
|
+
observer.subscribe((payload) => {
|
|
441
|
+
if (payload.type === "user.created") {
|
|
442
|
+
console.log("Track new user:", payload.message);
|
|
443
|
+
}
|
|
397
444
|
});
|
|
398
|
-
}
|
|
399
|
-
if (payload.type === "user.deleted") {
|
|
400
|
-
console.log("User deleted:", payload.message.userId);
|
|
401
|
-
}
|
|
402
|
-
});
|
|
403
|
-
|
|
404
|
-
return {
|
|
405
|
-
// notification methods...
|
|
406
|
-
};
|
|
407
|
-
},
|
|
408
|
-
});
|
|
409
445
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
observer.subscribe((payload) => {
|
|
417
|
-
if (payload.type === "user.created") {
|
|
418
|
-
console.log("Track new user:", payload.message);
|
|
419
|
-
}
|
|
420
|
-
});
|
|
421
|
-
|
|
422
|
-
return {
|
|
423
|
-
// analytics methods...
|
|
424
|
-
};
|
|
425
|
-
},
|
|
446
|
+
return {
|
|
447
|
+
// analytics methods...
|
|
448
|
+
};
|
|
449
|
+
},
|
|
450
|
+
},
|
|
451
|
+
],
|
|
426
452
|
});
|
|
427
453
|
```
|
|
428
454
|
|
|
@@ -434,26 +460,30 @@ Create multiple isolated workspaces with different dependencies:
|
|
|
434
460
|
// API workspace
|
|
435
461
|
const apiWorkspace = manager.createWorkspace({
|
|
436
462
|
id: "api",
|
|
437
|
-
|
|
463
|
+
dependencies: {
|
|
438
464
|
httpClient: apiClient,
|
|
439
465
|
cache: redisCache,
|
|
440
466
|
},
|
|
467
|
+
plugins: () => [
|
|
468
|
+
// API repository plugins
|
|
469
|
+
],
|
|
441
470
|
logging: true,
|
|
442
471
|
});
|
|
443
472
|
|
|
444
473
|
// Database workspace
|
|
445
474
|
const dbWorkspace = manager.createWorkspace({
|
|
446
475
|
id: "database",
|
|
447
|
-
|
|
476
|
+
dependencies: {
|
|
448
477
|
db: postgresClient,
|
|
449
478
|
logger: winstonLogger,
|
|
450
479
|
},
|
|
480
|
+
plugins: () => [
|
|
481
|
+
// Database repository plugins
|
|
482
|
+
],
|
|
451
483
|
logging: false,
|
|
452
484
|
});
|
|
453
485
|
|
|
454
486
|
// Each workspace has isolated repositories and observers
|
|
455
|
-
apiWorkspace.defineRepository({...});
|
|
456
|
-
dbWorkspace.defineRepository({...});
|
|
457
487
|
```
|
|
458
488
|
|
|
459
489
|
### TypeScript Best Practices
|
|
@@ -461,8 +491,8 @@ dbWorkspace.defineRepository({...});
|
|
|
461
491
|
Define clear interfaces for type safety:
|
|
462
492
|
|
|
463
493
|
```typescript
|
|
464
|
-
//
|
|
465
|
-
interface
|
|
494
|
+
// Dependencies interface
|
|
495
|
+
interface IDependencies {
|
|
466
496
|
httpClient: IHttpClient;
|
|
467
497
|
cache: ICache;
|
|
468
498
|
logger: ILogger;
|
|
@@ -474,30 +504,29 @@ interface IUserRepository {
|
|
|
474
504
|
createUser(user: User): Promise<User>;
|
|
475
505
|
}
|
|
476
506
|
|
|
477
|
-
// Workspace with typed
|
|
478
|
-
const {
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
// TypeScript validates User type
|
|
497
|
-
return infrastructure.httpClient.post("/users", user);
|
|
507
|
+
// Workspace with typed dependencies
|
|
508
|
+
const { queryRepository } = manager.createWorkspace<IDependencies>({
|
|
509
|
+
id: "app",
|
|
510
|
+
dependencies,
|
|
511
|
+
plugins: () => [
|
|
512
|
+
{
|
|
513
|
+
id: "user-repo",
|
|
514
|
+
install({ instance }): IUserRepository {
|
|
515
|
+
const { dependencies } = instance;
|
|
516
|
+
return {
|
|
517
|
+
async getUsers() {
|
|
518
|
+
// TypeScript knows dependencies type
|
|
519
|
+
return dependencies.httpClient.get("/users");
|
|
520
|
+
},
|
|
521
|
+
async createUser(user) {
|
|
522
|
+
// TypeScript validates User type
|
|
523
|
+
return dependencies.httpClient.post("/users", user);
|
|
524
|
+
},
|
|
525
|
+
};
|
|
498
526
|
},
|
|
499
|
-
}
|
|
500
|
-
|
|
527
|
+
},
|
|
528
|
+
],
|
|
529
|
+
logging: true,
|
|
501
530
|
});
|
|
502
531
|
|
|
503
532
|
// Query with typed repository
|
|
@@ -546,14 +575,19 @@ const cacheMiddleware: Middleware = (method, args, next) => {
|
|
|
546
575
|
return result;
|
|
547
576
|
};
|
|
548
577
|
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
578
|
+
const { queryRepository } = manager.createWorkspace({
|
|
579
|
+
dependencies,
|
|
580
|
+
plugins: () => [
|
|
581
|
+
{
|
|
582
|
+
id: "user-repo",
|
|
583
|
+
install({ instance }) {
|
|
584
|
+
return {
|
|
585
|
+
getUsers: () => instance.dependencies.httpClient.get("/users"),
|
|
586
|
+
};
|
|
587
|
+
},
|
|
588
|
+
middlewares: [loggingMiddleware, cacheMiddleware],
|
|
589
|
+
},
|
|
590
|
+
],
|
|
557
591
|
});
|
|
558
592
|
```
|
|
559
593
|
|
|
@@ -571,9 +605,12 @@ defineRepository({
|
|
|
571
605
|
Enable logging to see connection lifecycle and events:
|
|
572
606
|
|
|
573
607
|
```typescript
|
|
574
|
-
const {
|
|
608
|
+
const { queryRepository } = manager.createWorkspace({
|
|
575
609
|
id: "app",
|
|
576
|
-
|
|
610
|
+
dependencies,
|
|
611
|
+
plugins: () => [
|
|
612
|
+
// plugins here
|
|
613
|
+
],
|
|
577
614
|
logging: true, // Enables colored console output
|
|
578
615
|
});
|
|
579
616
|
```
|
package/dist/core/index.d.ts
CHANGED
package/dist/core/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
|
package/dist/core/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/core/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/core/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE3C,wBAAgB,eAAe,CAAC,UAAU,EAAE,GAAG,EAAE,WAAW,EAAE,UAAU,EAAE,OAkCzE"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { scopedObserverType } from "../infrastructure";
|
|
2
|
-
import type {
|
|
3
|
-
declare function createRepository<
|
|
2
|
+
import type { IPlugin } from "../types";
|
|
3
|
+
declare function createRepository<D>(dependencies: D, plugin: IPlugin<D, any>, observer: scopedObserverType): {
|
|
4
4
|
readonly repository: unknown;
|
|
5
5
|
readonly connections: number;
|
|
6
6
|
connect(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"repository.d.ts","sourceRoot":"","sources":["../../src/core/repository.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"repository.d.ts","sourceRoot":"","sources":["../../src/core/repository.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAGxC,iBAAS,gBAAgB,CAAC,CAAC,EACzB,YAAY,EAAE,CAAC,EACf,MAAM,EAAE,OAAO,CAAC,CAAC,EAAE,GAAG,CAAC,EACvB,QAAQ,EAAE,kBAAkB;;;;;EAgF7B;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|