@simplysm/orm-node 14.0.1 → 14.0.4
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 +254 -0
- package/package.json +3 -3
package/README.md
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# @simplysm/orm-node
|
|
2
|
+
|
|
3
|
+
Node.js ORM runtime providing database connections for MySQL, MSSQL, and PostgreSQL with typed query execution.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @simplysm/orm-node
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Peer dependencies (install the driver for your database):
|
|
12
|
+
- MySQL: `mysql2`
|
|
13
|
+
- MSSQL: `tedious`
|
|
14
|
+
- PostgreSQL: `pg`, `pg-copy-streams`
|
|
15
|
+
|
|
16
|
+
## Quick Start
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { defineDbContext, Table, expr } from "@simplysm/orm-common";
|
|
20
|
+
import { createOrm } from "@simplysm/orm-node";
|
|
21
|
+
|
|
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
|
+
const MyDb = defineDbContext({
|
|
31
|
+
tables: { user: User },
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const orm = createOrm(MyDb, {
|
|
35
|
+
dialect: "mysql",
|
|
36
|
+
host: "localhost",
|
|
37
|
+
port: 3306,
|
|
38
|
+
username: "root",
|
|
39
|
+
password: "password",
|
|
40
|
+
database: "mydb",
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// With transaction
|
|
44
|
+
await orm.connect(async (db) => {
|
|
45
|
+
await db.user().insert([{ name: "Gildong Hong" }]);
|
|
46
|
+
const users = await db.user().execute();
|
|
47
|
+
return users;
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Without transaction (for DDL or read-only)
|
|
51
|
+
await orm.connectWithoutTransaction(async (db) => {
|
|
52
|
+
await db.initialize();
|
|
53
|
+
const users = await db.user().execute();
|
|
54
|
+
return users;
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## API Reference
|
|
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.
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
class NodeDbContextExecutor implements DbContextExecutor {
|
|
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
|
+
```
|
|
204
|
+
|
|
205
|
+
#### createOrm
|
|
206
|
+
|
|
207
|
+
Main factory function. Creates an `Orm` instance that manages `DbContext` creation and connection lifecycle.
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
function createOrm<TDef extends DbContextDef<any, any, any>>(
|
|
211
|
+
dbContextDef: TDef,
|
|
212
|
+
config: DbConnConfig,
|
|
213
|
+
options?: OrmOptions,
|
|
214
|
+
): Orm<TDef>;
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
**Parameters:**
|
|
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)
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
interface Orm<TDef extends DbContextDef<any, any, any>> {
|
|
229
|
+
readonly dbContextDef: TDef;
|
|
230
|
+
readonly config: DbConnConfig;
|
|
231
|
+
readonly options?: OrmOptions;
|
|
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
|
+
}
|
|
242
|
+
```
|
|
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 |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simplysm/orm-node",
|
|
3
|
-
"version": "14.0.
|
|
3
|
+
"version": "14.0.4",
|
|
4
4
|
"description": "심플리즘 패키지 - ORM (node)",
|
|
5
5
|
"author": "심플리즘",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"sideEffects": false,
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"consola": "^3.4.2",
|
|
22
|
-
"@simplysm/core-common": "14.0.
|
|
23
|
-
"@simplysm/orm-common": "14.0.
|
|
22
|
+
"@simplysm/core-common": "14.0.4",
|
|
23
|
+
"@simplysm/orm-common": "14.0.4"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@types/pg": "^8.20.0",
|