@simplysm/orm-common 14.0.16 → 14.0.18

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 (75) hide show
  1. package/README.md +25 -19
  2. package/dist/db-context.d.ts +133 -0
  3. package/dist/db-context.d.ts.map +1 -0
  4. package/dist/db-context.js +325 -0
  5. package/dist/db-context.js.map +1 -0
  6. package/dist/ddl/initialize.d.ts +5 -3
  7. package/dist/ddl/initialize.d.ts.map +1 -1
  8. package/dist/ddl/initialize.js +39 -32
  9. package/dist/ddl/initialize.js.map +1 -1
  10. package/dist/exec/executable.js +1 -1
  11. package/dist/exec/executable.js.map +1 -1
  12. package/dist/exec/queryable.js +2 -2
  13. package/dist/exec/queryable.js.map +1 -1
  14. package/dist/exec/search-parser.d.ts.map +1 -1
  15. package/dist/exec/search-parser.js +2 -1
  16. package/dist/exec/search-parser.js.map +1 -1
  17. package/dist/expr/expr.d.ts +2 -2
  18. package/dist/expr/expr.js +2 -2
  19. package/dist/index.d.ts +1 -2
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +2 -3
  22. package/dist/index.js.map +1 -1
  23. package/dist/query-builder/mssql/mssql-expr-renderer.js +1 -1
  24. package/dist/query-builder/mssql/mssql-expr-renderer.js.map +1 -1
  25. package/dist/query-builder/mysql/mysql-expr-renderer.js +1 -1
  26. package/dist/query-builder/mysql/mysql-query-builder.js +1 -1
  27. package/dist/query-builder/mysql/mysql-query-builder.js.map +1 -1
  28. package/dist/query-builder/postgresql/postgresql-query-builder.d.ts +7 -0
  29. package/dist/query-builder/postgresql/postgresql-query-builder.d.ts.map +1 -1
  30. package/dist/query-builder/postgresql/postgresql-query-builder.js +86 -16
  31. package/dist/query-builder/postgresql/postgresql-query-builder.js.map +1 -1
  32. package/dist/schema/factory/relation-builder.d.ts +44 -67
  33. package/dist/schema/factory/relation-builder.d.ts.map +1 -1
  34. package/dist/schema/factory/relation-builder.js +37 -78
  35. package/dist/schema/factory/relation-builder.js.map +1 -1
  36. package/dist/schema/view-builder.d.ts +1 -1
  37. package/dist/schema/view-builder.d.ts.map +1 -1
  38. package/dist/schema/view-builder.js +0 -2
  39. package/dist/schema/view-builder.js.map +1 -1
  40. package/dist/types/column.js +1 -1
  41. package/dist/types/column.js.map +1 -1
  42. package/dist/types/db-context-def.d.ts +2 -42
  43. package/dist/types/db-context-def.d.ts.map +1 -1
  44. package/dist/utils/result-parser.js +31 -23
  45. package/dist/utils/result-parser.js.map +1 -1
  46. package/docs/core.md +110 -50
  47. package/docs/schema-builders.md +13 -19
  48. package/docs/types.md +2 -41
  49. package/package.json +3 -3
  50. package/src/db-context.ts +455 -0
  51. package/src/ddl/initialize.ts +49 -37
  52. package/src/exec/executable.ts +1 -1
  53. package/src/exec/queryable.ts +2 -2
  54. package/src/exec/search-parser.ts +2 -1
  55. package/src/expr/expr.ts +2 -2
  56. package/src/index.ts +2 -3
  57. package/src/query-builder/mssql/mssql-expr-renderer.ts +1 -1
  58. package/src/query-builder/mysql/mysql-expr-renderer.ts +1 -1
  59. package/src/query-builder/mysql/mysql-query-builder.ts +1 -1
  60. package/src/query-builder/postgresql/postgresql-query-builder.ts +93 -14
  61. package/src/schema/factory/relation-builder.ts +56 -87
  62. package/src/schema/view-builder.ts +1 -3
  63. package/src/types/column.ts +1 -1
  64. package/src/types/db-context-def.ts +2 -60
  65. package/src/utils/result-parser.ts +29 -22
  66. package/dist/create-db-context.d.ts +0 -34
  67. package/dist/create-db-context.d.ts.map +0 -1
  68. package/dist/create-db-context.js +0 -329
  69. package/dist/create-db-context.js.map +0 -1
  70. package/dist/define-db-context.d.ts +0 -15
  71. package/dist/define-db-context.d.ts.map +0 -1
  72. package/dist/define-db-context.js +0 -12
  73. package/dist/define-db-context.js.map +0 -1
  74. package/src/create-db-context.ts +0 -409
  75. package/src/define-db-context.ts +0 -28
package/docs/core.md CHANGED
@@ -1,68 +1,62 @@
1
1
  # Core
2
2
 
3
- DbContext definition, creation, and lifecycle management.
3
+ DbContext abstract class, connection/transaction lifecycle, DDL execution, and initialization.
4
4
 
5
- ## defineDbContext
5
+ ## DbContext
6
6
 
7
7
  ```typescript
8
- function defineDbContext<
9
- TTables extends Record<string, TableBuilder<any, any>> = {},
10
- TViews extends Record<string, ViewBuilder<any, any, any>> = {},
11
- TProcedures extends Record<string, ProcedureBuilder<any, any>> = {},
12
- >(config: {
13
- tables?: TTables;
14
- views?: TViews;
15
- procedures?: TProcedures;
16
- migrations?: Migration[];
17
- }): DbContextDef<TTables & { _migration: typeof _Migration }, TViews, TProcedures>
18
- ```
8
+ abstract class DbContext implements DbContextBase {
9
+ status: DbContextStatus;
19
10
 
20
- Creates a DbContext definition (blueprint) containing schema metadata. Automatically adds the `_migration` system table. The definition itself has no runtime state.
11
+ constructor(
12
+ executor: DbContextExecutor,
13
+ opt: { database: string; schema?: string },
14
+ )
21
15
 
22
- | Parameter | Type | Description |
23
- |-----------|------|-------------|
24
- | `config.tables` | `Record<string, TableBuilder>` | Table definitions |
25
- | `config.views` | `Record<string, ViewBuilder>` | View definitions |
26
- | `config.procedures` | `Record<string, ProcedureBuilder>` | Procedure definitions |
27
- | `config.migrations` | `Migration[]` | Migration definitions |
16
+ // Registration
17
+ protected queryable<T extends TableBuilder | ViewBuilder>(builder: T): () => Queryable<...>
18
+ protected executable<T extends ProcedureBuilder>(builder: T): () => Executable<...>
28
19
 
29
- ```typescript
30
- const MyDb = defineDbContext({
31
- tables: { user: User, post: Post },
32
- views: { activeUsers: ActiveUsersView },
33
- procedures: { getUserById: GetUserById },
34
- migrations: [
35
- { name: "20260101_001_init", up: async (db) => { await db.createTable(User); } },
36
- ],
37
- });
38
- ```
20
+ // Connection
21
+ connect<R>(fn: () => Promise<R>, isolationLevel?: IsolationLevel): Promise<R>
22
+ connectWithoutTransaction<R>(fn: () => Promise<R>): Promise<R>
23
+ transaction<R>(fn: () => Promise<R>, isolationLevel?: IsolationLevel): Promise<R>
39
24
 
40
- ## createDbContext
25
+ // DDL execution (see DDL Execution Methods below)
26
+ // DDL QueryDef generators (get*QueryDef methods)
41
27
 
42
- ```typescript
43
- function createDbContext<TDef extends DbContextDef<any, any, any>>(
44
- def: TDef,
45
- executor: DbContextExecutor,
46
- opt: { database: string; schema?: string },
47
- ): DbContextInstance<TDef>
28
+ // Initialization
29
+ initialize(options?: { dbs?: string[]; force?: boolean }): Promise<void>
30
+
31
+ // Override in subclass
32
+ migrations: Migration[];
33
+ }
48
34
  ```
49
35
 
50
- Creates a full DbContext instance from a definition and executor. The returned object provides:
36
+ Base class for database contexts. Subclass to define tables, views, and procedures as class properties.
51
37
 
52
- - **Queryable accessors** for each table/view (e.g., `db.user()` returns a `Queryable`)
53
- - **Executable accessors** for each procedure (e.g., `db.getUserById()` returns an `Executable`)
54
- - **Connection management**: `connect()`, `connectWithoutTransaction()`, `transaction()`
55
- - **DDL methods**: `createTable()`, `dropTable()`, `addColumn()`, etc. (see `DbContextDdlMethods`)
56
- - **DDL QueryDef generators**: `getCreateTableQueryDef()`, etc.
57
- - **Initialize**: `initialize()` runs migrations
38
+ Each property registered via `queryable()` or `executable()` is independently serialized, avoiding TS7056 even with 40+ tables.
58
39
 
59
- | Parameter | Type | Description |
60
- |-----------|------|-------------|
61
- | `def` | `DbContextDef` | Definition from `defineDbContext()` |
40
+ | Constructor Parameter | Type | Description |
41
+ |---|---|---|
62
42
  | `executor` | `DbContextExecutor` | Query execution engine (e.g., `NodeDbContextExecutor`) |
63
43
  | `opt.database` | `string` | Database name |
64
44
  | `opt.schema` | `string?` | Schema name (MSSQL: dbo, PostgreSQL: public) |
65
45
 
46
+ ```typescript
47
+ class MainDb extends DbContext {
48
+ user = this.queryable(User);
49
+ post = this.queryable(Post);
50
+ getUserById = this.executable(GetUserById);
51
+
52
+ migrations = [
53
+ { name: "20260101_001_init", up: async (db) => { await db.createTable(User); } },
54
+ ];
55
+ }
56
+
57
+ const db = new MainDb(executor, { database: "mydb", schema: "dbo" });
58
+ ```
59
+
66
60
  ### Connection Methods
67
61
 
68
62
  | Method | Signature | Description |
@@ -99,7 +93,7 @@ Creates a full DbContext instance from a definition and executor. The returned o
99
93
 
100
94
  ### DDL QueryDef Generators
101
95
 
102
- Each DDL method has a corresponding `get*QueryDef` method that returns a `QueryDef` without executing it. For example: `getCreateTableQueryDef(table)`, `getDropTableQueryDef(table)`, etc.
96
+ Each DDL method has a corresponding `get*QueryDef` method that returns a `QueryDef` without executing it. For example: `getCreateTableQueryDef(table)`, `getDropTableQueryDef(table)`, `getCreateObjectQueryDef(builder)`, etc.
103
97
 
104
98
  ### Initialize
105
99
 
@@ -107,7 +101,20 @@ Each DDL method has a corresponding `get*QueryDef` method that returns a `QueryD
107
101
  async initialize(options?: { dbs?: string[]; force?: boolean }): Promise<void>
108
102
  ```
109
103
 
110
- Runs pending migrations. If `force` is true, drops and recreates the schema.
104
+ Code First database initialization. Creates all registered Table/View/Procedure objects and runs pending migrations.
105
+
106
+ - **force=true**: Clear schema, recreate all objects, register all migrations as applied
107
+ - **force=false** (default):
108
+ - No `_Migration` table: Full creation + register all migrations
109
+ - `_Migration` table exists: Run only pending migrations
110
+
111
+ ## SD_BUILDER
112
+
113
+ ```typescript
114
+ const SD_BUILDER: unique symbol
115
+ ```
116
+
117
+ Internal symbol used to tag queryable/executable factory functions with their underlying builder. Used by `initialize()` to discover registered builders from DbContext instances.
111
118
 
112
119
  ## DbTransactionError
113
120
 
@@ -148,10 +155,63 @@ enum DbErrorCode {
148
155
  | `DEADLOCK` | Deadlock detected |
149
156
  | `LOCK_TIMEOUT` | Lock timeout exceeded |
150
157
 
158
+ ## DbContextBase
159
+
160
+ ```typescript
161
+ interface DbContextBase {
162
+ status: DbContextStatus;
163
+ readonly database: string | undefined;
164
+ readonly schema: string | undefined;
165
+ getNextAlias(): string;
166
+ resetAliasCounter(): void;
167
+ executeDefs<T = DataRecord>(defs: QueryDef[], resultMetas?: (ResultMeta | undefined)[]): Promise<T[][]>;
168
+ getQueryDefObjectName(tableOrView: TableBuilder | ViewBuilder): QueryDefObjectName;
169
+ switchFk(table: QueryDefObjectName, enabled: boolean): Promise<void>;
170
+ }
171
+ ```
172
+
173
+ Core interface implemented by DbContext. Used internally by Queryable, Executable, and ViewBuilder.
174
+
175
+ | Field | Type | Description |
176
+ |-------|------|-------------|
177
+ | `status` | `DbContextStatus` | Current connection status |
178
+ | `database` | `string \| undefined` | Database name |
179
+ | `schema` | `string \| undefined` | Schema name |
180
+ | `getNextAlias()` | `string` | Generate next table alias (T1, T2, ...) |
181
+ | `resetAliasCounter()` | `void` | Reset alias counter |
182
+ | `executeDefs()` | `Promise<T[][]>` | Execute QueryDef array, returns result per def |
183
+ | `getQueryDefObjectName()` | `QueryDefObjectName` | Resolve table/view to qualified name |
184
+ | `switchFk()` | `Promise<void>` | Enable/disable FK constraints |
185
+
186
+ ## DbContextDdlMethods
187
+
188
+ ```typescript
189
+ interface DbContextDdlMethods {
190
+ createTable(table: TableBuilder): Promise<void>;
191
+ dropTable(table: QueryDefObjectName): Promise<void>;
192
+ // ... all DDL methods listed in DbContext DDL Execution Methods above
193
+ // ... plus all get*QueryDef generator methods
194
+ }
195
+ ```
196
+
197
+ Interface for DDL execution and QueryDef generation methods. DbContext implements this interface. Used by the `initialize()` function and `Migration.up()` callback parameter type.
198
+
199
+ ## DbContextStatus
200
+
201
+ ```typescript
202
+ type DbContextStatus = "ready" | "connect" | "transact";
203
+ ```
204
+
205
+ | Value | Description |
206
+ |-------|-------------|
207
+ | `"ready"` | Not connected |
208
+ | `"connect"` | Connected, no active transaction |
209
+ | `"transact"` | Connected with active transaction |
210
+
151
211
  ## _Migration
152
212
 
153
213
  ```typescript
154
214
  const _Migration: TableBuilder<{ code: ColumnBuilder<string, ...> }, {}>
155
215
  ```
156
216
 
157
- System migration tracking table. Automatically added to every DbContext via `defineDbContext()`. Has a single `code` column (VARCHAR(255)) as primary key, storing executed migration names.
217
+ System migration tracking table. Automatically registered in every DbContext as `_migration`. Has a single `code` column (VARCHAR(255)) as primary key, storing executed migration names.
@@ -215,9 +215,7 @@ class ForeignKeyBuilder<TOwner extends TableBuilder<any, any>, TTargetFn extends
215
215
 
216
216
  N:1 FK relation builder. Creates an actual DB foreign key constraint.
217
217
 
218
- | Method | Signature | Description |
219
- |--------|-----------|-------------|
220
- | `description` | `(desc: string) => ForeignKeyBuilder` | Set relation description |
218
+ `description` is configured via the factory function's `opts` parameter (3rd argument to `foreignKey()`), not via method chaining. This class has no methods — it is a data holder for relation metadata.
221
219
 
222
220
  ## ForeignKeyTargetBuilder
223
221
 
@@ -232,12 +230,11 @@ class ForeignKeyTargetBuilder<TTargetTableFn extends () => TableBuilder<any, any
232
230
  }
233
231
  ```
234
232
 
235
- 1:N FK reverse-reference builder. Loaded as array by default, single object with `.single()`.
233
+ 1:N FK reverse-reference builder. Loaded as array by default, single object with `{ single: true }` opts.
236
234
 
237
- | Method | Signature | Description |
238
- |--------|-----------|-------------|
239
- | `description` | `(desc: string) => ForeignKeyTargetBuilder` | Set relation description |
240
- | `single` | `() => ForeignKeyTargetBuilder<..., true>` | Change to 1:1 (single object) |
235
+ `description` and `single` are configured via the factory function's `opts` parameter (3rd argument to `foreignKeyTarget()`), not via method chaining. Method chaining was removed to prevent TS7022 in complex circular references.
236
+
237
+ This class has no methods it is a data holder for relation metadata.
241
238
 
242
239
  ## RelationKeyBuilder
243
240
 
@@ -250,9 +247,7 @@ class RelationKeyBuilder<
250
247
 
251
248
  Logical N:1 relation builder (no DB FK constraint). Works with both Tables and Views.
252
249
 
253
- | Method | Signature | Description |
254
- |--------|-----------|-------------|
255
- | `description` | `(desc: string) => RelationKeyBuilder` | Set relation description |
250
+ `description` is configured via the factory function's `opts` parameter (3rd argument to `relationKey()`), not via method chaining. This class has no methods — it is a data holder for relation metadata.
256
251
 
257
252
  ## RelationKeyTargetBuilder
258
253
 
@@ -265,10 +260,9 @@ class RelationKeyTargetBuilder<
265
260
 
266
261
  Logical 1:N reverse-reference builder (no DB FK constraint). Works with both Tables and Views.
267
262
 
268
- | Method | Signature | Description |
269
- |--------|-----------|-------------|
270
- | `description` | `(desc: string) => RelationKeyTargetBuilder` | Set relation description |
271
- | `single` | `() => RelationKeyTargetBuilder<..., true>` | Change to 1:1 (single object) |
263
+ `description` and `single` are configured via the factory function's `opts` parameter (3rd argument to `relationKeyTarget()`), not via method chaining.
264
+
265
+ This class has no methods it is a data holder for relation metadata.
272
266
 
273
267
  ## createRelationFactory
274
268
 
@@ -284,10 +278,10 @@ Creates a relation factory. For `TableBuilder`: provides `foreignKey`, `foreignK
284
278
 
285
279
  | Method | Signature | Description |
286
280
  |--------|-----------|-------------|
287
- | `foreignKey` | `(columns: TColumnKey[], targetFn: () => TableBuilder) => ForeignKeyBuilder` | N:1 FK (creates DB constraint) |
288
- | `foreignKeyTarget` | `(targetTableFn: () => TableBuilder, relationName: string) => ForeignKeyTargetBuilder` | 1:N FK reverse-reference |
289
- | `relationKey` | `(columns: TColumnKey[], targetFn: () => TableBuilder \| ViewBuilder) => RelationKeyBuilder` | N:1 logical relation (no DB FK) |
290
- | `relationKeyTarget` | `(targetTableFn: () => TableBuilder \| ViewBuilder, relationName: string) => RelationKeyTargetBuilder` | 1:N logical reverse-reference |
281
+ | `foreignKey` | `(columns, targetFn, opts?: { description?: string })` | N:1 FK (creates DB constraint) |
282
+ | `foreignKeyTarget` | `(targetTableFn, relationName, opts?: { single?: boolean; description?: string })` | 1:N FK reverse-reference. `{ single: true }` → 1:1 |
283
+ | `relationKey` | `(columns, targetFn, opts?: { description?: string })` | N:1 logical relation (no DB FK) |
284
+ | `relationKeyTarget` | `(targetTableFn, relationName, opts?: { single?: boolean; description?: string })` | 1:N logical reverse-reference. `{ single: true }` → 1:1 |
291
285
 
292
286
  ### Factory Methods (View)
293
287
 
package/docs/types.md CHANGED
@@ -100,20 +100,7 @@ interface QueryBuildResult {
100
100
 
101
101
  ## DbContext Types
102
102
 
103
- ### DbContextDef
104
-
105
- ```typescript
106
- interface DbContextDef<TTables, TViews, TProcedures> {
107
- readonly meta: {
108
- readonly tables: TTables;
109
- readonly views: TViews;
110
- readonly procedures: TProcedures;
111
- readonly migrations: Migration[];
112
- };
113
- }
114
- ```
115
-
116
- DbContext blueprint created by `defineDbContext()`. Contains only schema metadata, no runtime state.
103
+ See [Core documentation](./core.md) for detailed descriptions of these types.
117
104
 
118
105
  ### DbContextBase
119
106
 
@@ -130,7 +117,7 @@ interface DbContextBase {
130
117
  }
131
118
  ```
132
119
 
133
- Core interface used internally by Queryable, Executable, and ViewBuilder.
120
+ Core interface implemented by DbContext. Used internally by Queryable, Executable, and ViewBuilder.
134
121
 
135
122
  ### DbContextStatus
136
123
 
@@ -138,32 +125,6 @@ Core interface used internally by Queryable, Executable, and ViewBuilder.
138
125
  type DbContextStatus = "ready" | "connect" | "transact";
139
126
  ```
140
127
 
141
- ### DbContextInstance
142
-
143
- ```typescript
144
- type DbContextInstance<TDef extends DbContextDef<any, any, any>> =
145
- DbContextBase &
146
- DbContextConnectionMethods &
147
- DbContextDdlMethods &
148
- { [K in keyof TDef["meta"]["tables"]]: () => Queryable<...> } &
149
- { [K in keyof TDef["meta"]["views"]]: () => Queryable<...> } &
150
- { [K in keyof TDef["meta"]["procedures"]]: () => Executable<...> } &
151
- { _migration: () => Queryable<{ code: string }, any> } &
152
- { initialize(options?: { dbs?: string[]; force?: boolean }): Promise<void> };
153
- ```
154
-
155
- Full DbContext instance type returned by `createDbContext()`. Includes queryable accessors for all tables/views, executable accessors for all procedures, connection methods, DDL methods, and `initialize()`.
156
-
157
- ### DbContextConnectionMethods
158
-
159
- ```typescript
160
- interface DbContextConnectionMethods {
161
- connect<TResult>(fn: () => Promise<TResult>, isolationLevel?: IsolationLevel): Promise<TResult>;
162
- connectWithoutTransaction<TResult>(callback: () => Promise<TResult>): Promise<TResult>;
163
- transaction<TResult>(fn: () => Promise<TResult>, isolationLevel?: IsolationLevel): Promise<TResult>;
164
- }
165
- ```
166
-
167
128
  ### DbContextDdlMethods
168
129
 
169
130
  See [Core - DDL Execution Methods](./core.md#ddl-execution-methods) and [Core - DDL QueryDef Generators](./core.md#ddl-querydef-generators) for the full list of 18 DDL execution methods and 22 QueryDef generator methods.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simplysm/orm-common",
3
- "version": "14.0.16",
3
+ "version": "14.0.18",
4
4
  "description": "심플리즘 패키지 - ORM (common)",
5
5
  "author": "심플리즘",
6
6
  "license": "Apache-2.0",
@@ -19,9 +19,9 @@
19
19
  ],
20
20
  "sideEffects": false,
21
21
  "devDependencies": {
22
- "@types/node": "^20.19.37"
22
+ "@types/node": "^20.19.39"
23
23
  },
24
24
  "dependencies": {
25
- "@simplysm/core-common": "14.0.16"
25
+ "@simplysm/core-common": "14.0.18"
26
26
  }
27
27
  }