@simplysm/orm-common 13.0.96 → 13.0.98
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 +209 -54
- package/docs/core.md +225 -0
- package/docs/expression.md +296 -0
- package/docs/query-builder.md +196 -0
- package/docs/queryable.md +578 -0
- package/docs/schema-builders.md +415 -0
- package/docs/types.md +445 -0
- package/docs/utilities.md +122 -0
- package/package.json +2 -2
- package/docs/ddl.md +0 -300
- package/docs/query.md +0 -644
- package/docs/schema.md +0 -325
package/docs/ddl.md
DELETED
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
# DDL & 초기화
|
|
2
|
-
|
|
3
|
-
`createDbContext()`로 생성된 DbContext 인스턴스에서 사용하는 DDL 메서드.
|
|
4
|
-
|
|
5
|
-
**중요:** DDL 연산은 트랜잭션 내에서 실행할 수 없다. `connectWithoutTransaction()` 내에서 사용해야 한다.
|
|
6
|
-
|
|
7
|
-
## 스키마 초기화
|
|
8
|
-
|
|
9
|
-
```typescript
|
|
10
|
-
await db.connectWithoutTransaction(async () => {
|
|
11
|
-
// 정의된 모든 테이블/뷰/프로시저 자동 생성
|
|
12
|
-
await db.initialize();
|
|
13
|
-
|
|
14
|
-
// 기존 스키마 삭제 후 재생성
|
|
15
|
-
await db.initialize({ force: true });
|
|
16
|
-
|
|
17
|
-
// 특정 DB만 초기화
|
|
18
|
-
await db.initialize({ dbs: ["mydb"] });
|
|
19
|
-
});
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
### initialize API
|
|
23
|
-
|
|
24
|
-
```
|
|
25
|
-
db.initialize(options?: {
|
|
26
|
-
dbs?: string[]; // 초기화할 DB 목록 (생략 시 전체)
|
|
27
|
-
force?: boolean; // true이면 기존 스키마 삭제 후 재생성
|
|
28
|
-
}): Promise<void>
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
## 테이블 DDL
|
|
32
|
-
|
|
33
|
-
```typescript
|
|
34
|
-
// 테이블 생성 (TableBuilder 전달)
|
|
35
|
-
await db.createTable(User);
|
|
36
|
-
|
|
37
|
-
// 테이블 삭제 (QueryDefObjectName 전달)
|
|
38
|
-
await db.dropTable({ name: "user", database: "mydb" });
|
|
39
|
-
|
|
40
|
-
// 테이블 이름 변경
|
|
41
|
-
await db.renameTable({ name: "user" }, "users_v2");
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
## 컬럼 DDL
|
|
45
|
-
|
|
46
|
-
```typescript
|
|
47
|
-
import { createColumnFactory } from "@simplysm/orm-common";
|
|
48
|
-
const c = createColumnFactory();
|
|
49
|
-
|
|
50
|
-
// 컬럼 추가
|
|
51
|
-
await db.addColumn({ name: "user" }, "phone", c.varchar(20).nullable());
|
|
52
|
-
|
|
53
|
-
// 컬럼 수정
|
|
54
|
-
await db.modifyColumn({ name: "user" }, "phone", c.varchar(50).nullable());
|
|
55
|
-
|
|
56
|
-
// 컬럼 이름 변경
|
|
57
|
-
await db.renameColumn({ name: "user" }, "phone", "phoneNumber");
|
|
58
|
-
|
|
59
|
-
// 컬럼 삭제
|
|
60
|
-
await db.dropColumn({ name: "user" }, "phone");
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## 키/인덱스 DDL
|
|
64
|
-
|
|
65
|
-
```typescript
|
|
66
|
-
// PK
|
|
67
|
-
await db.addPrimaryKey({ name: "user" }, ["id"]);
|
|
68
|
-
await db.dropPrimaryKey({ name: "user" });
|
|
69
|
-
|
|
70
|
-
// FK
|
|
71
|
-
await db.addForeignKey({ name: "order" }, "user", userRelationDef);
|
|
72
|
-
await db.dropForeignKey({ name: "order" }, "user");
|
|
73
|
-
|
|
74
|
-
// 인덱스
|
|
75
|
-
await db.addIndex({ name: "user" }, indexBuilder);
|
|
76
|
-
await db.dropIndex({ name: "user" }, ["email"]);
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
## 뷰/프로시저 DDL
|
|
80
|
-
|
|
81
|
-
```typescript
|
|
82
|
-
await db.createView(UserSummary);
|
|
83
|
-
await db.dropView({ name: "user_summary" });
|
|
84
|
-
|
|
85
|
-
await db.createProc(GetUserOrders);
|
|
86
|
-
await db.dropProc({ name: "get_user_orders" });
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
## 스키마 관리
|
|
90
|
-
|
|
91
|
-
```typescript
|
|
92
|
-
// 스키마 존재 여부
|
|
93
|
-
const exists = await db.schemaExists("mydb", "dbo");
|
|
94
|
-
|
|
95
|
-
// 스키마 내 모든 테이블 삭제
|
|
96
|
-
await db.clearSchema({ database: "mydb", schema: "dbo" });
|
|
97
|
-
|
|
98
|
-
// 테이블 TRUNCATE
|
|
99
|
-
await db.truncate({ name: "user" });
|
|
100
|
-
|
|
101
|
-
// FK 제약조건 토글
|
|
102
|
-
await db.switchFk({ name: "user" }, false); // FK 비활성화
|
|
103
|
-
// ... 벌크 작업 ...
|
|
104
|
-
await db.switchFk({ name: "user" }, true); // FK 활성화
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
## DDL 메서드 API
|
|
108
|
-
|
|
109
|
-
### 테이블/뷰/프로시저
|
|
110
|
-
|
|
111
|
-
| 메서드 | 시그니처 | 설명 |
|
|
112
|
-
|--------|---------|------|
|
|
113
|
-
| `createTable` | `(table: TableBuilder) => Promise<void>` | 테이블 생성 |
|
|
114
|
-
| `dropTable` | `(table: QueryDefObjectName) => Promise<void>` | 테이블 삭제 |
|
|
115
|
-
| `renameTable` | `(table: QueryDefObjectName, newName: string) => Promise<void>` | 테이블 이름 변경 |
|
|
116
|
-
| `createView` | `(view: ViewBuilder) => Promise<void>` | 뷰 생성 |
|
|
117
|
-
| `dropView` | `(view: QueryDefObjectName) => Promise<void>` | 뷰 삭제 |
|
|
118
|
-
| `createProc` | `(proc: ProcedureBuilder) => Promise<void>` | 프로시저 생성 |
|
|
119
|
-
| `dropProc` | `(proc: QueryDefObjectName) => Promise<void>` | 프로시저 삭제 |
|
|
120
|
-
|
|
121
|
-
### 컬럼
|
|
122
|
-
|
|
123
|
-
| 메서드 | 시그니처 | 설명 |
|
|
124
|
-
|--------|---------|------|
|
|
125
|
-
| `addColumn` | `(table, columnName, column: ColumnBuilder) => Promise<void>` | 컬럼 추가 |
|
|
126
|
-
| `dropColumn` | `(table, column: string) => Promise<void>` | 컬럼 삭제 |
|
|
127
|
-
| `modifyColumn` | `(table, columnName, column: ColumnBuilder) => Promise<void>` | 컬럼 수정 |
|
|
128
|
-
| `renameColumn` | `(table, column, newName) => Promise<void>` | 컬럼 이름 변경 |
|
|
129
|
-
|
|
130
|
-
### 키/인덱스
|
|
131
|
-
|
|
132
|
-
| 메서드 | 시그니처 | 설명 |
|
|
133
|
-
|--------|---------|------|
|
|
134
|
-
| `addPrimaryKey` | `(table, columns: string[]) => Promise<void>` | PK 추가 |
|
|
135
|
-
| `dropPrimaryKey` | `(table) => Promise<void>` | PK 삭제 |
|
|
136
|
-
| `addForeignKey` | `(table, relationName, relationDef) => Promise<void>` | FK 추가 |
|
|
137
|
-
| `dropForeignKey` | `(table, relationName) => Promise<void>` | FK 삭제 |
|
|
138
|
-
| `addIndex` | `(table, indexBuilder) => Promise<void>` | 인덱스 추가 |
|
|
139
|
-
| `dropIndex` | `(table, columns: string[]) => Promise<void>` | 인덱스 삭제 |
|
|
140
|
-
|
|
141
|
-
### 스키마
|
|
142
|
-
|
|
143
|
-
| 메서드 | 시그니처 | 설명 |
|
|
144
|
-
|--------|---------|------|
|
|
145
|
-
| `schemaExists` | `(database, schema?) => Promise<boolean>` | 스키마 존재 여부 |
|
|
146
|
-
| `clearSchema` | `(params: { database, schema? }) => Promise<void>` | 스키마 내 모든 테이블 삭제 |
|
|
147
|
-
| `truncate` | `(table) => Promise<void>` | 테이블 TRUNCATE |
|
|
148
|
-
| `switchFk` | `(table, enabled: boolean) => Promise<void>` | FK 제약조건 on/off |
|
|
149
|
-
|
|
150
|
-
## 연결 & 트랜잭션
|
|
151
|
-
|
|
152
|
-
```typescript
|
|
153
|
-
// 자동 트랜잭션 (connect -> begin -> callback -> commit/rollback -> close)
|
|
154
|
-
const result = await db.connect(async () => {
|
|
155
|
-
await db.user().insert([{ name: "Alice", createdAt: new DateTime() }]);
|
|
156
|
-
return await db.user().execute();
|
|
157
|
-
}, "SERIALIZABLE"); // 격리 수준 선택
|
|
158
|
-
|
|
159
|
-
// 트랜잭션 없이 연결 (DDL 작업 등)
|
|
160
|
-
await db.connectWithoutTransaction(async () => {
|
|
161
|
-
await db.initialize();
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
// 수동 트랜잭션 (connectWithoutTransaction 내에서)
|
|
165
|
-
await db.connectWithoutTransaction(async () => {
|
|
166
|
-
// DDL 먼저 실행 (트랜잭션 밖)
|
|
167
|
-
await db.createTable(NewTable);
|
|
168
|
-
|
|
169
|
-
// 이후 트랜잭션 내에서 DML 실행
|
|
170
|
-
await db.transaction(async () => {
|
|
171
|
-
await db.user().insert([{ name: "Bob", createdAt: new DateTime() }]);
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
### 연결 API
|
|
177
|
-
|
|
178
|
-
| 메서드 | 시그니처 | 설명 |
|
|
179
|
-
|--------|---------|------|
|
|
180
|
-
| `connect` | `(fn, isolationLevel?) => Promise<T>` | 자동 트랜잭션 (connect -> begin -> fn -> commit/rollback -> close) |
|
|
181
|
-
| `connectWithoutTransaction` | `(fn) => Promise<T>` | 트랜잭션 없이 연결 (DDL용) |
|
|
182
|
-
| `transaction` | `(fn, isolationLevel?) => Promise<T>` | 수동 트랜잭션 (connectWithoutTransaction 내에서) |
|
|
183
|
-
|
|
184
|
-
격리 수준: `"READ_UNCOMMITTED"`, `"READ_COMMITTED"`, `"REPEATABLE_READ"`, `"SERIALIZABLE"`
|
|
185
|
-
|
|
186
|
-
## DbContextExecutor 인터페이스
|
|
187
|
-
|
|
188
|
-
Node.js(`@simplysm/orm-node`)나 서비스 클라이언트(`@simplysm/service-client`)에서 구현.
|
|
189
|
-
|
|
190
|
-
```typescript
|
|
191
|
-
interface DbContextExecutor {
|
|
192
|
-
connect(): Promise<void>;
|
|
193
|
-
close(): Promise<void>;
|
|
194
|
-
beginTransaction(isolationLevel?: IsolationLevel): Promise<void>;
|
|
195
|
-
commitTransaction(): Promise<void>;
|
|
196
|
-
rollbackTransaction(): Promise<void>;
|
|
197
|
-
executeDefs<T>(defs: QueryDef[], resultMetas?: (ResultMeta | undefined)[]): Promise<T[][]>;
|
|
198
|
-
}
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
## 마이그레이션
|
|
202
|
-
|
|
203
|
-
`defineDbContext`의 `migrations` 옵션으로 스키마 변경사항을 관리한다. `initialize()` 호출 시 아직 실행되지 않은 마이그레이션만 실행된다.
|
|
204
|
-
|
|
205
|
-
```typescript
|
|
206
|
-
const MyDb = defineDbContext({
|
|
207
|
-
tables: { user: User },
|
|
208
|
-
migrations: [
|
|
209
|
-
{
|
|
210
|
-
name: "20260105_001_create_user_table",
|
|
211
|
-
up: async (db) => {
|
|
212
|
-
await db.createTable(User);
|
|
213
|
-
},
|
|
214
|
-
},
|
|
215
|
-
{
|
|
216
|
-
name: "20260106_001_add_email_column",
|
|
217
|
-
up: async (db) => {
|
|
218
|
-
const c = createColumnFactory();
|
|
219
|
-
await db.addColumn({ name: "user" }, "email", c.varchar(200).nullable());
|
|
220
|
-
},
|
|
221
|
-
},
|
|
222
|
-
],
|
|
223
|
-
});
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
### Migration 인터페이스
|
|
227
|
-
|
|
228
|
-
```typescript
|
|
229
|
-
interface Migration {
|
|
230
|
-
name: string;
|
|
231
|
-
up: (db: DbContextBase & DbContextDdlMethods) => Promise<void>;
|
|
232
|
-
}
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
실행된 마이그레이션은 `_migration` 테이블에 기록된다.
|
|
236
|
-
|
|
237
|
-
```typescript
|
|
238
|
-
// 마이그레이션 코드 조회
|
|
239
|
-
const migrations = await db._migration().execute();
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
## DbTransactionError
|
|
243
|
-
|
|
244
|
-
트랜잭션 관련 에러를 DBMS 독립적으로 처리하기 위한 에러 클래스.
|
|
245
|
-
|
|
246
|
-
```typescript
|
|
247
|
-
import { DbTransactionError, DbErrorCode } from "@simplysm/orm-common";
|
|
248
|
-
|
|
249
|
-
// DbErrorCode:
|
|
250
|
-
// - NO_ACTIVE_TRANSACTION: 활성 트랜잭션 없음
|
|
251
|
-
// - TRANSACTION_ALREADY_STARTED: 트랜잭션 이미 시작됨
|
|
252
|
-
// - DEADLOCK: 데드락 발생
|
|
253
|
-
// - LOCK_TIMEOUT: 잠금 타임아웃
|
|
254
|
-
```
|
|
255
|
-
|
|
256
|
-
### DbTransactionError API
|
|
257
|
-
|
|
258
|
-
```typescript
|
|
259
|
-
class DbTransactionError extends Error {
|
|
260
|
-
readonly name: "DbTransactionError";
|
|
261
|
-
readonly code: DbErrorCode;
|
|
262
|
-
readonly originalError?: unknown;
|
|
263
|
-
constructor(code: DbErrorCode, message: string, originalError?: unknown);
|
|
264
|
-
}
|
|
265
|
-
```
|
|
266
|
-
|
|
267
|
-
## QueryDef 생성기
|
|
268
|
-
|
|
269
|
-
DDL 메서드 외에도 `get*QueryDef()` 메서드로 QueryDef만 생성할 수 있다 (직접 실행하지 않음).
|
|
270
|
-
|
|
271
|
-
```typescript
|
|
272
|
-
const def = db.getCreateTableQueryDef(User);
|
|
273
|
-
const def2 = db.getAddColumnQueryDef({ name: "user" }, "phone", c.varchar(20));
|
|
274
|
-
// ... 등. 모든 DDL 메서드에 대응하는 get*QueryDef() 메서드가 있다.
|
|
275
|
-
```
|
|
276
|
-
|
|
277
|
-
| 생성기 | 대응 DDL 메서드 |
|
|
278
|
-
|--------|---------------|
|
|
279
|
-
| `getCreateTableQueryDef` | `createTable` |
|
|
280
|
-
| `getCreateViewQueryDef` | `createView` |
|
|
281
|
-
| `getCreateProcQueryDef` | `createProc` |
|
|
282
|
-
| `getCreateObjectQueryDef` | Table/View/Procedure 자동 판별 |
|
|
283
|
-
| `getDropTableQueryDef` | `dropTable` |
|
|
284
|
-
| `getRenameTableQueryDef` | `renameTable` |
|
|
285
|
-
| `getDropViewQueryDef` | `dropView` |
|
|
286
|
-
| `getDropProcQueryDef` | `dropProc` |
|
|
287
|
-
| `getAddColumnQueryDef` | `addColumn` |
|
|
288
|
-
| `getDropColumnQueryDef` | `dropColumn` |
|
|
289
|
-
| `getModifyColumnQueryDef` | `modifyColumn` |
|
|
290
|
-
| `getRenameColumnQueryDef` | `renameColumn` |
|
|
291
|
-
| `getAddPrimaryKeyQueryDef` | `addPrimaryKey` |
|
|
292
|
-
| `getDropPrimaryKeyQueryDef` | `dropPrimaryKey` |
|
|
293
|
-
| `getAddForeignKeyQueryDef` | `addForeignKey` |
|
|
294
|
-
| `getAddIndexQueryDef` | `addIndex` |
|
|
295
|
-
| `getDropForeignKeyQueryDef` | `dropForeignKey` |
|
|
296
|
-
| `getDropIndexQueryDef` | `dropIndex` |
|
|
297
|
-
| `getClearSchemaQueryDef` | `clearSchema` |
|
|
298
|
-
| `getSchemaExistsQueryDef` | `schemaExists` |
|
|
299
|
-
| `getTruncateQueryDef` | `truncate` |
|
|
300
|
-
| `getSwitchFkQueryDef` | `switchFk` |
|