@simplysm/orm-node 14.0.1 → 14.0.5

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 ADDED
@@ -0,0 +1,120 @@
1
+ # @simplysm/orm-node
2
+
3
+ Node.js ORM module for the Simplysm framework. Provides database connections, query execution, and a high-level ORM factory for MySQL, MSSQL, and PostgreSQL.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @simplysm/orm-node
9
+ # or
10
+ pnpm add @simplysm/orm-node
11
+ ```
12
+
13
+ Peer dependencies (install the driver for your DBMS):
14
+ - MySQL: `mysql2`
15
+ - MSSQL: `tedious`
16
+ - PostgreSQL: `pg`, `pg-copy-streams`
17
+
18
+ ## API Overview
19
+
20
+ ### ORM Factory
21
+
22
+ | Export | Type | Description |
23
+ |--------|------|-------------|
24
+ | [`createOrm`](./docs/orm-factory.md#createorm) | Function | Create an ORM instance with connection management |
25
+ | [`Orm`](./docs/orm-factory.md#orm) | Interface | ORM instance type with `connect` and `connectWithoutTransaction` |
26
+ | [`OrmOptions`](./docs/orm-factory.md#ormoptions) | Interface | ORM options (database/schema override) |
27
+
28
+ ### Query Execution
29
+
30
+ | Export | Type | Description |
31
+ |--------|------|-------------|
32
+ | [`NodeDbContextExecutor`](./docs/orm-factory.md#nodedbcontextexecutor) | Class | Node.js `DbContextExecutor` implementation |
33
+
34
+ ### Connection
35
+
36
+ | Export | Type | Description |
37
+ |--------|------|-------------|
38
+ | [`createDbConn`](./docs/connection.md#createdbconn) | Function | Create a low-level DB connection |
39
+ | [`DbConn`](./docs/connection.md#dbconn) | Interface | Low-level DB connection interface |
40
+ | [`MysqlDbConn`](./docs/connection.md#mysqldbconn) | Class | MySQL connection implementation |
41
+ | [`MssqlDbConn`](./docs/connection.md#mssqldbconn) | Class | MSSQL connection implementation |
42
+ | [`PostgresqlDbConn`](./docs/connection.md#postgresqldbconn) | Class | PostgreSQL connection implementation |
43
+
44
+ ### Config Types
45
+
46
+ | Export | Type | Description |
47
+ |--------|------|-------------|
48
+ | [`DbConnConfig`](./docs/connection.md#dbconnconfig) | Type | Union of all dialect-specific configs |
49
+ | [`MysqlDbConnConfig`](./docs/connection.md#mysqldbconnconfig) | Interface | MySQL connection config |
50
+ | [`MssqlDbConnConfig`](./docs/connection.md#mssqldbconnconfig) | Interface | MSSQL connection config |
51
+ | [`PostgresqlDbConnConfig`](./docs/connection.md#postgresqldbconnconfig) | Interface | PostgreSQL connection config |
52
+
53
+ ### Constants & Utilities
54
+
55
+ | Export | Type | Description |
56
+ |--------|------|-------------|
57
+ | [`DB_CONN_CONNECT_TIMEOUT`](./docs/connection.md#db_conn_connect_timeout) | Constant | Connection timeout: 10,000ms (10s) |
58
+ | [`DB_CONN_DEFAULT_TIMEOUT`](./docs/connection.md#db_conn_default_timeout) | Constant | Query timeout: 600,000ms (10min) |
59
+ | [`DB_CONN_ERRORS`](./docs/connection.md#db_conn_errors) | Constant | Error message constants |
60
+ | [`getDialectFromConfig`](./docs/connection.md#getdialectfromconfig) | Function | Extract `Dialect` from config (`mssql-azure` -> `mssql`) |
61
+
62
+ ## Usage Examples
63
+
64
+ ### Basic ORM Usage
65
+
66
+ ```typescript
67
+ import { defineDbContext } from "@simplysm/orm-common";
68
+ import { createOrm } from "@simplysm/orm-node";
69
+
70
+ const MyDb = defineDbContext({
71
+ tables: { user: User, post: Post },
72
+ });
73
+
74
+ const orm = createOrm(MyDb, {
75
+ dialect: "mysql",
76
+ host: "localhost",
77
+ port: 3306,
78
+ username: "root",
79
+ password: "password",
80
+ database: "mydb",
81
+ });
82
+
83
+ // With transaction (auto commit/rollback)
84
+ const users = await orm.connect(async (db) => {
85
+ return db.user().execute();
86
+ });
87
+
88
+ // Without transaction (for DDL or read-only)
89
+ await orm.connectWithoutTransaction(async (db) => {
90
+ await db.initialize();
91
+ });
92
+ ```
93
+
94
+ ### Low-Level Connection
95
+
96
+ ```typescript
97
+ import { createDbConn } from "@simplysm/orm-node";
98
+
99
+ const conn = await createDbConn({
100
+ dialect: "postgresql",
101
+ host: "localhost",
102
+ port: 5432,
103
+ username: "postgres",
104
+ password: "password",
105
+ database: "mydb",
106
+ });
107
+
108
+ await conn.connect();
109
+ const results = await conn.execute(["SELECT * FROM users"]);
110
+ await conn.close();
111
+ ```
112
+
113
+ ### With OrmOptions Override
114
+
115
+ ```typescript
116
+ const orm = createOrm(MyDb, config, {
117
+ database: "other_db", // Override config.database
118
+ schema: "custom", // Override config.schema
119
+ });
120
+ ```
@@ -0,0 +1,252 @@
1
+ # Connection
2
+
3
+ Low-level database connection management for MySQL, MSSQL, and PostgreSQL.
4
+
5
+ ## createDbConn
6
+
7
+ ```typescript
8
+ async function createDbConn(config: DbConnConfig): Promise<DbConn>
9
+ ```
10
+
11
+ Factory function that creates a dialect-specific database connection. The connection is **not yet established** -- call `connect()` on the returned object.
12
+
13
+ Driver modules are lazily loaded and cached:
14
+ - `mysql`: `mysql2/promise`
15
+ - `mssql`/`mssql-azure`: `tedious`
16
+ - `postgresql`: `pg` + `pg-copy-streams`
17
+
18
+ | Parameter | Type | Description |
19
+ |-----------|------|-------------|
20
+ | `config` | `DbConnConfig` | Database connection configuration |
21
+
22
+ Returns `MysqlDbConn`, `MssqlDbConn`, or `PostgresqlDbConn`.
23
+
24
+ ```typescript
25
+ const conn = await createDbConn({
26
+ dialect: "mysql",
27
+ host: "localhost",
28
+ port: 3306,
29
+ username: "root",
30
+ password: "password",
31
+ database: "mydb",
32
+ });
33
+
34
+ await conn.connect();
35
+ try {
36
+ const results = await conn.execute(["SELECT * FROM users"]);
37
+ } finally {
38
+ await conn.close();
39
+ }
40
+ ```
41
+
42
+ ## DbConn
43
+
44
+ ```typescript
45
+ interface DbConn extends EventEmitter<{ close: void }> {
46
+ config: DbConnConfig;
47
+ isConnected: boolean;
48
+ isInTransaction: boolean;
49
+ connect(): Promise<void>;
50
+ close(): Promise<void>;
51
+ beginTransaction(isolationLevel?: IsolationLevel): Promise<void>;
52
+ commitTransaction(): Promise<void>;
53
+ rollbackTransaction(): Promise<void>;
54
+ execute(queries: string[]): Promise<Record<string, unknown>[][]>;
55
+ executeParametrized(query: string, params?: unknown[]): Promise<Record<string, unknown>[][]>;
56
+ bulkInsert(tableName: string, columnMetas: Record<string, ColumnMeta>, records: Record<string, unknown>[]): Promise<void>;
57
+ }
58
+ ```
59
+
60
+ Low-level database connection interface. Extends `EventEmitter` from `@simplysm/core-common` and emits `close` events.
61
+
62
+ | Field | Type | Description |
63
+ |-------|------|-------------|
64
+ | `config` | `DbConnConfig` | Connection configuration |
65
+ | `isConnected` | `boolean` | Whether connection is established |
66
+ | `isInTransaction` | `boolean` | Whether a transaction is active |
67
+
68
+ | Method | Signature | Description |
69
+ |--------|-----------|-------------|
70
+ | `connect` | `() => Promise<void>` | Establish database connection |
71
+ | `close` | `() => Promise<void>` | Close database connection |
72
+ | `beginTransaction` | `(isolationLevel?) => Promise<void>` | Start transaction |
73
+ | `commitTransaction` | `() => Promise<void>` | Commit transaction |
74
+ | `rollbackTransaction` | `() => Promise<void>` | Rollback transaction |
75
+ | `execute` | `(queries: string[]) => Promise<Record<string, unknown>[][]>` | Execute SQL query strings |
76
+ | `executeParametrized` | `(query: string, params?: unknown[]) => Promise<Record<string, unknown>[][]>` | Execute parameterized query |
77
+ | `bulkInsert` | `(tableName, columnMetas, records) => Promise<void>` | Native bulk insert |
78
+
79
+ ### Bulk Insert Implementation by Dialect
80
+
81
+ | Dialect | Mechanism |
82
+ |---------|-----------|
83
+ | MySQL | `LOAD DATA LOCAL INFILE` (temporary CSV file) |
84
+ | MSSQL | tedious `BulkLoad` API |
85
+ | PostgreSQL | `COPY FROM STDIN` via pg-copy-streams |
86
+
87
+ ## MysqlDbConn
88
+
89
+ ```typescript
90
+ class MysqlDbConn extends EventEmitter<{ close: void }> implements DbConn
91
+ ```
92
+
93
+ MySQL connection implementation using the `mysql2/promise` library. Constructor:
94
+
95
+ ```typescript
96
+ constructor(mysql: typeof import("mysql2/promise"), config: MysqlDbConnConfig)
97
+ ```
98
+
99
+ ## MssqlDbConn
100
+
101
+ ```typescript
102
+ class MssqlDbConn extends EventEmitter<{ close: void }> implements DbConn
103
+ ```
104
+
105
+ MSSQL/Azure SQL connection implementation using the `tedious` library. Constructor:
106
+
107
+ ```typescript
108
+ constructor(tedious: typeof import("tedious"), config: MssqlDbConnConfig)
109
+ ```
110
+
111
+ ## PostgresqlDbConn
112
+
113
+ ```typescript
114
+ class PostgresqlDbConn extends EventEmitter<{ close: void }> implements DbConn
115
+ ```
116
+
117
+ PostgreSQL connection implementation using the `pg` and `pg-copy-streams` libraries. Constructor:
118
+
119
+ ```typescript
120
+ constructor(pg: typeof import("pg"), pgCopyStreams: typeof import("pg-copy-streams"), config: PostgresqlDbConnConfig)
121
+ ```
122
+
123
+ ## DbConnConfig
124
+
125
+ ```typescript
126
+ type DbConnConfig = MysqlDbConnConfig | MssqlDbConnConfig | PostgresqlDbConnConfig;
127
+ ```
128
+
129
+ Union type of all dialect-specific connection configurations.
130
+
131
+ ## MysqlDbConnConfig
132
+
133
+ ```typescript
134
+ interface MysqlDbConnConfig {
135
+ dialect: "mysql";
136
+ host: string;
137
+ port?: number;
138
+ username: string;
139
+ password: string;
140
+ database?: string;
141
+ defaultIsolationLevel?: IsolationLevel;
142
+ }
143
+ ```
144
+
145
+ | Field | Type | Default | Description |
146
+ |-------|------|---------|-------------|
147
+ | `dialect` | `"mysql"` | Required | Must be `"mysql"` |
148
+ | `host` | `string` | Required | Server hostname |
149
+ | `port` | `number?` | `3306` | Server port |
150
+ | `username` | `string` | Required | Login username |
151
+ | `password` | `string` | Required | Login password |
152
+ | `database` | `string?` | - | Default database name |
153
+ | `defaultIsolationLevel` | `IsolationLevel?` | - | Default transaction isolation level |
154
+
155
+ ## MssqlDbConnConfig
156
+
157
+ ```typescript
158
+ interface MssqlDbConnConfig {
159
+ dialect: "mssql" | "mssql-azure";
160
+ host: string;
161
+ port?: number;
162
+ username: string;
163
+ password: string;
164
+ database?: string;
165
+ schema?: string;
166
+ defaultIsolationLevel?: IsolationLevel;
167
+ }
168
+ ```
169
+
170
+ | Field | Type | Default | Description |
171
+ |-------|------|---------|-------------|
172
+ | `dialect` | `"mssql" \| "mssql-azure"` | Required | `"mssql"` for on-premises, `"mssql-azure"` for Azure SQL |
173
+ | `host` | `string` | Required | Server hostname |
174
+ | `port` | `number?` | `1433` | Server port |
175
+ | `username` | `string` | Required | Login username |
176
+ | `password` | `string` | Required | Login password |
177
+ | `database` | `string?` | - | Default database name |
178
+ | `schema` | `string?` | `"dbo"` | Default schema |
179
+ | `defaultIsolationLevel` | `IsolationLevel?` | - | Default transaction isolation level |
180
+
181
+ ## PostgresqlDbConnConfig
182
+
183
+ ```typescript
184
+ interface PostgresqlDbConnConfig {
185
+ dialect: "postgresql";
186
+ host: string;
187
+ port?: number;
188
+ username: string;
189
+ password: string;
190
+ database?: string;
191
+ schema?: string;
192
+ defaultIsolationLevel?: IsolationLevel;
193
+ }
194
+ ```
195
+
196
+ | Field | Type | Default | Description |
197
+ |-------|------|---------|-------------|
198
+ | `dialect` | `"postgresql"` | Required | Must be `"postgresql"` |
199
+ | `host` | `string` | Required | Server hostname |
200
+ | `port` | `number?` | `5432` | Server port |
201
+ | `username` | `string` | Required | Login username |
202
+ | `password` | `string` | Required | Login password |
203
+ | `database` | `string?` | - | Default database name |
204
+ | `schema` | `string?` | `"public"` | Default schema |
205
+ | `defaultIsolationLevel` | `IsolationLevel?` | - | Default transaction isolation level |
206
+
207
+ ## DB_CONN_CONNECT_TIMEOUT
208
+
209
+ ```typescript
210
+ const DB_CONN_CONNECT_TIMEOUT = 10 * 1000; // 10,000ms (10 seconds)
211
+ ```
212
+
213
+ Timeout for establishing a database connection.
214
+
215
+ ## DB_CONN_DEFAULT_TIMEOUT
216
+
217
+ ```typescript
218
+ const DB_CONN_DEFAULT_TIMEOUT = 10 * 60 * 1000; // 600,000ms (10 minutes)
219
+ ```
220
+
221
+ Default timeout for query execution.
222
+
223
+ ## DB_CONN_ERRORS
224
+
225
+ ```typescript
226
+ const DB_CONN_ERRORS = {
227
+ NOT_CONNECTED: "'Connection'이 연결되어 있지 않습니다.",
228
+ ALREADY_CONNECTED: "'Connection'이 이미 연결되어 있습니다.",
229
+ } as const;
230
+ ```
231
+
232
+ Error message constants for connection state validation.
233
+
234
+ | Key | Value | Description |
235
+ |-----|-------|-------------|
236
+ | `NOT_CONNECTED` | Connection is not established | Thrown when operating on a closed connection |
237
+ | `ALREADY_CONNECTED` | Connection is already established | Thrown when connecting an already-connected instance |
238
+
239
+ ## getDialectFromConfig
240
+
241
+ ```typescript
242
+ function getDialectFromConfig(config: DbConnConfig): Dialect
243
+ ```
244
+
245
+ Extracts the `Dialect` type from a connection config. Maps `"mssql-azure"` to `"mssql"`, passes through others unchanged.
246
+
247
+ | Input `config.dialect` | Output `Dialect` |
248
+ |------------------------|------------------|
249
+ | `"mysql"` | `"mysql"` |
250
+ | `"mssql"` | `"mssql"` |
251
+ | `"mssql-azure"` | `"mssql"` |
252
+ | `"postgresql"` | `"postgresql"` |
@@ -0,0 +1,117 @@
1
+ # ORM Factory & Query Execution
2
+
3
+ High-level ORM instance creation and Node.js query execution.
4
+
5
+ ## createOrm
6
+
7
+ ```typescript
8
+ function createOrm<TDef extends DbContextDef<any, any, any>>(
9
+ dbContextDef: TDef,
10
+ config: DbConnConfig,
11
+ options?: OrmOptions,
12
+ ): Orm<TDef>
13
+ ```
14
+
15
+ Creates an ORM instance that manages DbContext creation and database connections. Each `connect()` or `connectWithoutTransaction()` call creates a fresh DbContext and connection.
16
+
17
+ | Parameter | Type | Description |
18
+ |-----------|------|-------------|
19
+ | `dbContextDef` | `DbContextDef` | DbContext definition from `defineDbContext()` |
20
+ | `config` | `DbConnConfig` | Database connection configuration |
21
+ | `options` | `OrmOptions?` | Optional overrides for database/schema |
22
+
23
+ ```typescript
24
+ import { defineDbContext } from "@simplysm/orm-common";
25
+ import { createOrm } from "@simplysm/orm-node";
26
+
27
+ const MyDb = defineDbContext({
28
+ tables: { user: User, post: Post },
29
+ });
30
+
31
+ const orm = createOrm(MyDb, {
32
+ dialect: "mysql",
33
+ host: "localhost",
34
+ port: 3306,
35
+ username: "root",
36
+ password: "password",
37
+ database: "mydb",
38
+ });
39
+
40
+ // Transaction mode
41
+ await orm.connect(async (db) => {
42
+ const users = await db.user().execute();
43
+ await db.post().insert([{ title: "Hello", authorId: users[0].id }]);
44
+ return users;
45
+ });
46
+
47
+ // No-transaction mode (for DDL, read-only)
48
+ await orm.connectWithoutTransaction(async (db) => {
49
+ await db.initialize();
50
+ });
51
+ ```
52
+
53
+ ## Orm
54
+
55
+ ```typescript
56
+ interface Orm<TDef extends DbContextDef<any, any, any>> {
57
+ readonly dbContextDef: TDef;
58
+ readonly config: DbConnConfig;
59
+ readonly options?: OrmOptions;
60
+ connect<R>(callback: (conn: DbContextInstance<TDef>) => Promise<R>, isolationLevel?: IsolationLevel): Promise<R>;
61
+ connectWithoutTransaction<R>(callback: (conn: DbContextInstance<TDef>) => Promise<R>): Promise<R>;
62
+ }
63
+ ```
64
+
65
+ ORM instance returned by `createOrm()`.
66
+
67
+ | Field | Type | Description |
68
+ |-------|------|-------------|
69
+ | `dbContextDef` | `TDef` | The DbContext definition |
70
+ | `config` | `DbConnConfig` | The connection configuration |
71
+ | `options` | `OrmOptions?` | The ORM options |
72
+
73
+ | Method | Signature | Description |
74
+ |--------|-----------|-------------|
75
+ | `connect` | `<R>(callback: (conn) => Promise<R>, isolationLevel?) => Promise<R>` | Open connection, begin transaction, execute callback, commit (auto-rollback on error), close |
76
+ | `connectWithoutTransaction` | `<R>(callback: (conn) => Promise<R>) => Promise<R>` | Open connection, execute callback, close (no transaction) |
77
+
78
+ ## OrmOptions
79
+
80
+ ```typescript
81
+ interface OrmOptions {
82
+ database?: string;
83
+ schema?: string;
84
+ }
85
+ ```
86
+
87
+ | Field | Type | Description |
88
+ |-------|------|-------------|
89
+ | `database` | `string?` | Override the database name from `DbConnConfig` |
90
+ | `schema` | `string?` | Override the schema name (MSSQL: dbo, PostgreSQL: public) |
91
+
92
+ ## NodeDbContextExecutor
93
+
94
+ ```typescript
95
+ class NodeDbContextExecutor implements DbContextExecutor {
96
+ constructor(config: DbConnConfig);
97
+ }
98
+ ```
99
+
100
+ Node.js implementation of `DbContextExecutor`. Manages a single database connection and provides query execution capabilities.
101
+
102
+ | Method | Signature | Description |
103
+ |--------|-----------|-------------|
104
+ | `connect` | `() => Promise<void>` | Establish DB connection |
105
+ | `close` | `() => Promise<void>` | Close DB connection |
106
+ | `beginTransaction` | `(isolationLevel?: IsolationLevel) => Promise<void>` | Start transaction |
107
+ | `commitTransaction` | `() => Promise<void>` | Commit transaction |
108
+ | `rollbackTransaction` | `() => Promise<void>` | Rollback transaction |
109
+ | `executeParametrized` | `(query: string, params?: unknown[]) => Promise<Record<string, unknown>[][]>` | Execute parameterized SQL query |
110
+ | `bulkInsert` | `(tableName: string, columnMetas: Record<string, ColumnMeta>, records: DataRecord[]) => Promise<void>` | Bulk insert using native DB API |
111
+ | `executeDefs` | `<T>(defs: QueryDef[], resultMetas?: (ResultMeta \| undefined)[]) => Promise<T[][]>` | Execute QueryDef array (builds SQL via `createQueryBuilder`, parses results via `parseQueryResult`) |
112
+
113
+ The `executeDefs` method:
114
+ 1. Builds SQL from each QueryDef using the dialect-specific QueryBuilder
115
+ 2. If no resultMetas need data, combines all SQL into a single execution
116
+ 3. Otherwise executes each QueryDef individually
117
+ 4. Parses results using `parseQueryResult` when ResultMeta is provided
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simplysm/orm-node",
3
- "version": "14.0.1",
3
+ "version": "14.0.5",
4
4
  "description": "심플리즘 패키지 - ORM (node)",
5
5
  "author": "심플리즘",
6
6
  "license": "Apache-2.0",
@@ -14,13 +14,14 @@
14
14
  "types": "./dist/index.d.ts",
15
15
  "files": [
16
16
  "dist",
17
- "src"
17
+ "src",
18
+ "docs"
18
19
  ],
19
20
  "sideEffects": false,
20
21
  "dependencies": {
21
22
  "consola": "^3.4.2",
22
- "@simplysm/core-common": "14.0.1",
23
- "@simplysm/orm-common": "14.0.1"
23
+ "@simplysm/orm-common": "14.0.5",
24
+ "@simplysm/core-common": "14.0.5"
24
25
  },
25
26
  "devDependencies": {
26
27
  "@types/pg": "^8.20.0",