@cinnabun/db 0.0.7 → 0.0.9
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 +42 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @cinnabun/db
|
|
2
2
|
|
|
3
|
-
Database module for the Cinnabun framework with adapter pattern supporting Drizzle ORM and Prisma.
|
|
3
|
+
Database module for the Cinnabun framework with adapter pattern supporting Drizzle ORM and Prisma. Spring Data-style repositories with constructor injection.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -39,12 +39,13 @@ export const users = sqliteTable("users", {
|
|
|
39
39
|
|
|
40
40
|
```typescript
|
|
41
41
|
// src/main.ts
|
|
42
|
+
import "reflect-metadata";
|
|
42
43
|
import { CinnabunApp, CinnabunFactory } from "@cinnabun/core";
|
|
43
44
|
import { DatabaseModule, DatabasePlugin } from "@cinnabun/db";
|
|
44
45
|
|
|
45
46
|
@CinnabunApp({
|
|
46
47
|
port: 3000,
|
|
47
|
-
scanPaths: [],
|
|
48
|
+
scanPaths: ["./src"],
|
|
48
49
|
imports: [
|
|
49
50
|
DatabaseModule.forRoot({
|
|
50
51
|
adapter: "drizzle",
|
|
@@ -52,27 +53,33 @@ import { DatabaseModule, DatabasePlugin } from "@cinnabun/db";
|
|
|
52
53
|
autoMigrate: true,
|
|
53
54
|
}),
|
|
54
55
|
],
|
|
55
|
-
plugins: [
|
|
56
|
+
plugins: [new DatabasePlugin()],
|
|
56
57
|
})
|
|
57
58
|
class App {}
|
|
58
59
|
|
|
59
60
|
CinnabunFactory.run(App);
|
|
60
61
|
```
|
|
61
62
|
|
|
62
|
-
### 3. Use repositories
|
|
63
|
+
### 3. Use repositories
|
|
63
64
|
|
|
64
65
|
**Option A: Spring Data-style (constructor injection)**
|
|
65
66
|
|
|
67
|
+
Extend `CrudRepositoryBase` and use `@RepositoryEntity`. The framework provides the implementation at runtime.
|
|
68
|
+
|
|
66
69
|
```typescript
|
|
67
|
-
|
|
70
|
+
// src/repositories/user.repository.ts
|
|
68
71
|
import { RepositoryEntity, CrudRepositoryBase } from "@cinnabun/db";
|
|
69
|
-
import { users } from "
|
|
72
|
+
import { users } from "../schema";
|
|
70
73
|
|
|
71
74
|
type User = typeof users.$inferSelect;
|
|
72
75
|
|
|
73
76
|
@RepositoryEntity(users)
|
|
74
77
|
abstract class UserRepository extends CrudRepositoryBase<User, number> {}
|
|
75
78
|
|
|
79
|
+
// src/services/user.service.ts
|
|
80
|
+
import { Service } from "@cinnabun/core";
|
|
81
|
+
import { UserRepository } from "../repositories/user.repository.js";
|
|
82
|
+
|
|
76
83
|
@Service()
|
|
77
84
|
class UserService {
|
|
78
85
|
constructor(private readonly userRepo: UserRepository) {}
|
|
@@ -81,11 +88,19 @@ class UserService {
|
|
|
81
88
|
return this.userRepo.findAll();
|
|
82
89
|
}
|
|
83
90
|
|
|
91
|
+
async findById(id: number) {
|
|
92
|
+
return this.userRepo.findById(id);
|
|
93
|
+
}
|
|
94
|
+
|
|
84
95
|
async create(data: { name: string; email: string }) {
|
|
85
96
|
return this.userRepo.save(data);
|
|
86
97
|
}
|
|
87
98
|
}
|
|
88
99
|
|
|
100
|
+
// src/controllers/user.controller.ts
|
|
101
|
+
import { RestController, GetMapping, PostMapping, Body } from "@cinnabun/core";
|
|
102
|
+
import { UserService } from "../services/user.service.js";
|
|
103
|
+
|
|
89
104
|
@RestController("/api/users")
|
|
90
105
|
class UserController {
|
|
91
106
|
constructor(private readonly userService: UserService) {}
|
|
@@ -102,7 +117,14 @@ class UserController {
|
|
|
102
117
|
}
|
|
103
118
|
```
|
|
104
119
|
|
|
105
|
-
Register `UserRepository` and `UserService` in your module's `providers` array.
|
|
120
|
+
Register `UserRepository` and `UserService` in your module's `providers` array. With `scanPaths: ["./src"]`, scanned modules will discover them automatically.
|
|
121
|
+
|
|
122
|
+
**Prisma:** Use the model name string instead of a table schema:
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
@RepositoryEntity("Todo")
|
|
126
|
+
abstract class TodoCrudRepository extends CrudRepositoryBase<Todo, string> {}
|
|
127
|
+
```
|
|
106
128
|
|
|
107
129
|
**Option B: InjectRepository (property injection)**
|
|
108
130
|
|
|
@@ -134,11 +156,17 @@ class UserController {
|
|
|
134
156
|
|
|
135
157
|
## Transactions
|
|
136
158
|
|
|
137
|
-
Use `@Transactional()` to wrap methods in database transactions:
|
|
159
|
+
Use `@Transactional()` to wrap methods in database transactions. Inject `TransactionManager` and pass it to the decorator when using constructor-injected repositories:
|
|
138
160
|
|
|
139
161
|
```typescript
|
|
140
162
|
import { Service } from "@cinnabun/core";
|
|
141
|
-
import {
|
|
163
|
+
import {
|
|
164
|
+
Transactional,
|
|
165
|
+
InjectRepository,
|
|
166
|
+
Repository,
|
|
167
|
+
RepositoryFactory,
|
|
168
|
+
TransactionManager,
|
|
169
|
+
} from "@cinnabun/db";
|
|
142
170
|
import { users, profiles } from "./schema";
|
|
143
171
|
|
|
144
172
|
@Service()
|
|
@@ -192,23 +220,15 @@ interface DatabaseModuleOptions {
|
|
|
192
220
|
|
|
193
221
|
## Repository API
|
|
194
222
|
|
|
195
|
-
**
|
|
223
|
+
**CrudRepositoryBase** (Spring Data-style) — extend this with `@RepositoryEntity`. Do not implement `CrudRepository` directly; TypeScript requires all methods to be implemented.
|
|
196
224
|
|
|
197
225
|
```typescript
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
findAll(options?: QueryOptions): Promise<T[]>;
|
|
201
|
-
findOne(where: Partial<T>): Promise<T | null>;
|
|
202
|
-
findMany(where: Partial<T>, options?: QueryOptions): Promise<T[]>;
|
|
203
|
-
save(entity: Partial<T>): Promise<T>;
|
|
204
|
-
saveAll(entities: Partial<T>[]): Promise<T[]>;
|
|
205
|
-
deleteById(id: ID): Promise<void>;
|
|
206
|
-
delete(entity: T): Promise<void>;
|
|
207
|
-
existsById(id: ID): Promise<boolean>;
|
|
208
|
-
count(where?: Partial<T>): Promise<number>;
|
|
209
|
-
}
|
|
226
|
+
@RepositoryEntity(users)
|
|
227
|
+
abstract class UserRepository extends CrudRepositoryBase<User, number> {}
|
|
210
228
|
```
|
|
211
229
|
|
|
230
|
+
Methods: `findById`, `findAll`, `findOne`, `findMany`, `save`, `saveAll`, `deleteById`, `delete`, `existsById`, `count`.
|
|
231
|
+
|
|
212
232
|
**Repository** (lower-level, use with `@InjectRepository`):
|
|
213
233
|
|
|
214
234
|
```typescript
|