@simplysm/orm-node 14.0.4 → 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 +74 -208
- package/docs/connection.md +252 -0
- package/docs/orm-factory.md +117 -0
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -1,34 +1,74 @@
|
|
|
1
1
|
# @simplysm/orm-node
|
|
2
2
|
|
|
3
|
-
Node.js ORM
|
|
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
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
npm install @simplysm/orm-node
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @simplysm/orm-node
|
|
9
11
|
```
|
|
10
12
|
|
|
11
|
-
Peer dependencies (install the driver for your
|
|
13
|
+
Peer dependencies (install the driver for your DBMS):
|
|
12
14
|
- MySQL: `mysql2`
|
|
13
15
|
- MSSQL: `tedious`
|
|
14
16
|
- PostgreSQL: `pg`, `pg-copy-streams`
|
|
15
17
|
|
|
16
|
-
##
|
|
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
|
|
17
65
|
|
|
18
66
|
```typescript
|
|
19
|
-
import { defineDbContext
|
|
67
|
+
import { defineDbContext } from "@simplysm/orm-common";
|
|
20
68
|
import { createOrm } from "@simplysm/orm-node";
|
|
21
69
|
|
|
22
|
-
const User = Table("User")
|
|
23
|
-
.columns((c) => ({
|
|
24
|
-
id: c.bigint().autoIncrement(),
|
|
25
|
-
name: c.varchar(100),
|
|
26
|
-
email: c.varchar(200).nullable(),
|
|
27
|
-
}))
|
|
28
|
-
.primaryKey("id");
|
|
29
|
-
|
|
30
70
|
const MyDb = defineDbContext({
|
|
31
|
-
tables: { user: User },
|
|
71
|
+
tables: { user: User, post: Post },
|
|
32
72
|
});
|
|
33
73
|
|
|
34
74
|
const orm = createOrm(MyDb, {
|
|
@@ -40,215 +80,41 @@ const orm = createOrm(MyDb, {
|
|
|
40
80
|
database: "mydb",
|
|
41
81
|
});
|
|
42
82
|
|
|
43
|
-
// With transaction
|
|
44
|
-
await orm.connect(async (db) => {
|
|
45
|
-
|
|
46
|
-
const users = await db.user().execute();
|
|
47
|
-
return users;
|
|
83
|
+
// With transaction (auto commit/rollback)
|
|
84
|
+
const users = await orm.connect(async (db) => {
|
|
85
|
+
return db.user().execute();
|
|
48
86
|
});
|
|
49
87
|
|
|
50
88
|
// Without transaction (for DDL or read-only)
|
|
51
89
|
await orm.connectWithoutTransaction(async (db) => {
|
|
52
90
|
await db.initialize();
|
|
53
|
-
const users = await db.user().execute();
|
|
54
|
-
return users;
|
|
55
91
|
});
|
|
56
92
|
```
|
|
57
93
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
### Types
|
|
61
|
-
|
|
62
|
-
#### DbConn (interface)
|
|
63
|
-
|
|
64
|
-
Low-level database connection interface. Extends `EventEmitter<{ close: void }>`.
|
|
65
|
-
|
|
66
|
-
| Field / Method | Type | Description |
|
|
67
|
-
|---|---|---|
|
|
68
|
-
| `config` | `DbConnConfig` | Connection configuration |
|
|
69
|
-
| `isConnected` | `boolean` | Whether the connection is active |
|
|
70
|
-
| `isInTransaction` | `boolean` | Whether a transaction is in progress |
|
|
71
|
-
| `connect()` | `Promise<void>` | Establish connection |
|
|
72
|
-
| `close()` | `Promise<void>` | Close connection |
|
|
73
|
-
| `beginTransaction(isolationLevel?)` | `Promise<void>` | Start transaction |
|
|
74
|
-
| `commitTransaction()` | `Promise<void>` | Commit transaction |
|
|
75
|
-
| `rollbackTransaction()` | `Promise<void>` | Rollback transaction |
|
|
76
|
-
| `execute(queries: string[])` | `Promise<Record<string, unknown>[][]>` | Execute SQL query array |
|
|
77
|
-
| `executeParametrized(query, params?)` | `Promise<Record<string, unknown>[][]>` | Execute parameterized query |
|
|
78
|
-
| `bulkInsert(tableName, columnMetas, records)` | `Promise<void>` | Native bulk insert (MSSQL: BulkLoad, MySQL: LOAD DATA, PostgreSQL: COPY) |
|
|
79
|
-
|
|
80
|
-
#### DbConnConfig (type)
|
|
81
|
-
|
|
82
|
-
```typescript
|
|
83
|
-
type DbConnConfig = MysqlDbConnConfig | MssqlDbConnConfig | PostgresqlDbConnConfig;
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
#### MysqlDbConnConfig (interface)
|
|
87
|
-
|
|
88
|
-
| Field | Type | Required | Description |
|
|
89
|
-
|---|---|---|---|
|
|
90
|
-
| `dialect` | `"mysql"` | Yes | Dialect identifier |
|
|
91
|
-
| `host` | `string` | Yes | Server hostname |
|
|
92
|
-
| `port` | `number` | No | Server port |
|
|
93
|
-
| `username` | `string` | Yes | Login username |
|
|
94
|
-
| `password` | `string` | Yes | Login password |
|
|
95
|
-
| `database` | `string` | No | Default database |
|
|
96
|
-
| `defaultIsolationLevel` | `IsolationLevel` | No | Default transaction isolation level |
|
|
97
|
-
|
|
98
|
-
#### MssqlDbConnConfig (interface)
|
|
99
|
-
|
|
100
|
-
| Field | Type | Required | Description |
|
|
101
|
-
|---|---|---|---|
|
|
102
|
-
| `dialect` | `"mssql" \| "mssql-azure"` | Yes | Dialect identifier |
|
|
103
|
-
| `host` | `string` | Yes | Server hostname |
|
|
104
|
-
| `port` | `number` | No | Server port |
|
|
105
|
-
| `username` | `string` | Yes | Login username |
|
|
106
|
-
| `password` | `string` | Yes | Login password |
|
|
107
|
-
| `database` | `string` | No | Default database |
|
|
108
|
-
| `schema` | `string` | No | Default schema (default: dbo) |
|
|
109
|
-
| `defaultIsolationLevel` | `IsolationLevel` | No | Default transaction isolation level |
|
|
110
|
-
|
|
111
|
-
#### PostgresqlDbConnConfig (interface)
|
|
112
|
-
|
|
113
|
-
| Field | Type | Required | Description |
|
|
114
|
-
|---|---|---|---|
|
|
115
|
-
| `dialect` | `"postgresql"` | Yes | Dialect identifier |
|
|
116
|
-
| `host` | `string` | Yes | Server hostname |
|
|
117
|
-
| `port` | `number` | No | Server port |
|
|
118
|
-
| `username` | `string` | Yes | Login username |
|
|
119
|
-
| `password` | `string` | Yes | Login password |
|
|
120
|
-
| `database` | `string` | No | Default database |
|
|
121
|
-
| `schema` | `string` | No | Default schema (default: public) |
|
|
122
|
-
| `defaultIsolationLevel` | `IsolationLevel` | No | Default transaction isolation level |
|
|
123
|
-
|
|
124
|
-
#### getDialectFromConfig
|
|
125
|
-
|
|
126
|
-
Extracts the `Dialect` from a `DbConnConfig`. Maps `"mssql-azure"` to `"mssql"`.
|
|
127
|
-
|
|
128
|
-
```typescript
|
|
129
|
-
function getDialectFromConfig(config: DbConnConfig): Dialect;
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
#### Constants
|
|
133
|
-
|
|
134
|
-
| Constant | Value | Description |
|
|
135
|
-
|---|---|---|
|
|
136
|
-
| `DB_CONN_CONNECT_TIMEOUT` | `10000` | Connection establishment timeout (10 seconds) |
|
|
137
|
-
| `DB_CONN_DEFAULT_TIMEOUT` | `600000` | Default query timeout (10 minutes) |
|
|
138
|
-
| `DB_CONN_ERRORS` | `{ NOT_CONNECTED, ALREADY_CONNECTED }` | Standard error messages |
|
|
139
|
-
|
|
140
|
-
### Connections
|
|
141
|
-
|
|
142
|
-
#### MssqlDbConn (class)
|
|
143
|
-
|
|
144
|
-
Implements `DbConn` using the `tedious` driver for Microsoft SQL Server.
|
|
145
|
-
|
|
146
|
-
```typescript
|
|
147
|
-
class MssqlDbConn implements DbConn {
|
|
148
|
-
constructor(tedious: typeof import("tedious"), config: MssqlDbConnConfig);
|
|
149
|
-
}
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
#### MysqlDbConn (class)
|
|
153
|
-
|
|
154
|
-
Implements `DbConn` using the `mysql2/promise` driver for MySQL.
|
|
155
|
-
|
|
156
|
-
```typescript
|
|
157
|
-
class MysqlDbConn implements DbConn {
|
|
158
|
-
constructor(mysql: typeof import("mysql2/promise"), config: MysqlDbConnConfig);
|
|
159
|
-
}
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
#### PostgresqlDbConn (class)
|
|
163
|
-
|
|
164
|
-
Implements `DbConn` using `pg` and `pg-copy-streams` drivers for PostgreSQL.
|
|
165
|
-
|
|
166
|
-
```typescript
|
|
167
|
-
class PostgresqlDbConn implements DbConn {
|
|
168
|
-
constructor(
|
|
169
|
-
pg: typeof import("pg"),
|
|
170
|
-
pgCopyStreams: typeof import("pg-copy-streams"),
|
|
171
|
-
config: PostgresqlDbConnConfig,
|
|
172
|
-
);
|
|
173
|
-
}
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
### Core
|
|
177
|
-
|
|
178
|
-
#### createDbConn
|
|
179
|
-
|
|
180
|
-
Factory function that creates a `DbConn` instance with lazy-loaded drivers. The connection is NOT established -- call `connect()` separately.
|
|
181
|
-
|
|
182
|
-
```typescript
|
|
183
|
-
async function createDbConn(config: DbConnConfig): Promise<DbConn>;
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
#### NodeDbContextExecutor (class)
|
|
187
|
-
|
|
188
|
-
Implements `DbContextExecutor` for Node.js. Bridges `DbConn` with the ORM's `QueryDef` system.
|
|
94
|
+
### Low-Level Connection
|
|
189
95
|
|
|
190
96
|
```typescript
|
|
191
|
-
|
|
192
|
-
constructor(config: DbConnConfig);
|
|
193
|
-
|
|
194
|
-
connect(): Promise<void>;
|
|
195
|
-
close(): Promise<void>;
|
|
196
|
-
beginTransaction(isolationLevel?: IsolationLevel): Promise<void>;
|
|
197
|
-
commitTransaction(): Promise<void>;
|
|
198
|
-
rollbackTransaction(): Promise<void>;
|
|
199
|
-
executeParametrized(query: string, params?: unknown[]): Promise<Record<string, unknown>[][]>;
|
|
200
|
-
bulkInsert(tableName: string, columnMetas: Record<string, ColumnMeta>, records: DataRecord[]): Promise<void>;
|
|
201
|
-
executeDefs<T = DataRecord>(defs: QueryDef[], resultMetas?: (ResultMeta | undefined)[]): Promise<T[][]>;
|
|
202
|
-
}
|
|
203
|
-
```
|
|
97
|
+
import { createDbConn } from "@simplysm/orm-node";
|
|
204
98
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
99
|
+
const conn = await createDbConn({
|
|
100
|
+
dialect: "postgresql",
|
|
101
|
+
host: "localhost",
|
|
102
|
+
port: 5432,
|
|
103
|
+
username: "postgres",
|
|
104
|
+
password: "password",
|
|
105
|
+
database: "mydb",
|
|
106
|
+
});
|
|
208
107
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
config: DbConnConfig,
|
|
213
|
-
options?: OrmOptions,
|
|
214
|
-
): Orm<TDef>;
|
|
108
|
+
await conn.connect();
|
|
109
|
+
const results = await conn.execute(["SELECT * FROM users"]);
|
|
110
|
+
await conn.close();
|
|
215
111
|
```
|
|
216
112
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
| Parameter | Type | Description |
|
|
220
|
-
|---|---|---|
|
|
221
|
-
| `dbContextDef` | `DbContextDef` | Definition from `defineDbContext()` |
|
|
222
|
-
| `config` | `DbConnConfig` | Database connection configuration |
|
|
223
|
-
| `options` | `OrmOptions` | Optional overrides for database/schema |
|
|
224
|
-
|
|
225
|
-
#### Orm (interface)
|
|
113
|
+
### With OrmOptions Override
|
|
226
114
|
|
|
227
115
|
```typescript
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
connect<R>(
|
|
234
|
-
callback: (conn: DbContextInstance<TDef>) => Promise<R>,
|
|
235
|
-
isolationLevel?: IsolationLevel,
|
|
236
|
-
): Promise<R>;
|
|
237
|
-
|
|
238
|
-
connectWithoutTransaction<R>(
|
|
239
|
-
callback: (conn: DbContextInstance<TDef>) => Promise<R>,
|
|
240
|
-
): Promise<R>;
|
|
241
|
-
}
|
|
116
|
+
const orm = createOrm(MyDb, config, {
|
|
117
|
+
database: "other_db", // Override config.database
|
|
118
|
+
schema: "custom", // Override config.schema
|
|
119
|
+
});
|
|
242
120
|
```
|
|
243
|
-
|
|
244
|
-
| Method | Description |
|
|
245
|
-
|---|---|
|
|
246
|
-
| `connect(callback, isolationLevel?)` | Create a new DbContext, connect with transaction, execute callback, auto-commit/rollback, close. |
|
|
247
|
-
| `connectWithoutTransaction(callback)` | Create a new DbContext, connect without transaction, execute callback, close. |
|
|
248
|
-
|
|
249
|
-
#### OrmOptions (interface)
|
|
250
|
-
|
|
251
|
-
| Field | Type | Description |
|
|
252
|
-
|---|---|---|
|
|
253
|
-
| `database` | `string` | Override database name from config |
|
|
254
|
-
| `schema` | `string` | Override schema name from config |
|
|
@@ -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.
|
|
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/
|
|
23
|
-
"@simplysm/
|
|
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",
|