@simplysm/orm-node 14.0.23 → 14.0.25

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simplysm/orm-node",
3
- "version": "14.0.23",
3
+ "version": "14.0.25",
4
4
  "description": "심플리즘 패키지 - ORM (node)",
5
5
  "author": "심플리즘",
6
6
  "license": "Apache-2.0",
@@ -14,14 +14,13 @@
14
14
  "types": "./dist/index.d.ts",
15
15
  "files": [
16
16
  "dist",
17
- "src",
18
- "docs"
17
+ "src"
19
18
  ],
20
19
  "sideEffects": false,
21
20
  "dependencies": {
22
21
  "consola": "^3.4.2",
23
- "@simplysm/orm-common": "14.0.23",
24
- "@simplysm/core-common": "14.0.23"
22
+ "@simplysm/core-common": "14.0.25",
23
+ "@simplysm/orm-common": "14.0.25"
25
24
  },
26
25
  "devDependencies": {
27
26
  "@types/pg": "^8.20.0",
package/README.md DELETED
@@ -1,116 +0,0 @@
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 & Query Execution
21
-
22
- | Export | Type | Description |
23
- |--------|------|-------------|
24
- | [`createOrm`](./docs/orm-factory.md#createorm) | Function | Create an ORM instance from a DbContext subclass and connection config |
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
- | [`NodeDbContextExecutor`](./docs/orm-factory.md#nodedbcontextexecutor) | Class | Node.js `DbContextExecutor` implementation |
28
-
29
- ### Connection
30
-
31
- | Export | Type | Description |
32
- |--------|------|-------------|
33
- | [`createDbConn`](./docs/connection.md#createdbconn) | Function | Create a low-level DB connection |
34
- | [`DbConn`](./docs/connection.md#dbconn) | Interface | Low-level DB connection interface |
35
- | [`MysqlDbConn`](./docs/connection.md#mysqldbconn) | Class | MySQL connection implementation |
36
- | [`MssqlDbConn`](./docs/connection.md#mssqldbconn) | Class | MSSQL connection implementation |
37
- | [`PostgresqlDbConn`](./docs/connection.md#postgresqldbconn) | Class | PostgreSQL connection implementation |
38
-
39
- ### Config Types
40
-
41
- | Export | Type | Description |
42
- |--------|------|-------------|
43
- | [`DbConnConfig`](./docs/connection.md#dbconnconfig) | Type | Union of all dialect-specific configs |
44
- | [`MysqlDbConnConfig`](./docs/connection.md#mysqldbconnconfig) | Interface | MySQL connection config |
45
- | [`MssqlDbConnConfig`](./docs/connection.md#mssqldbconnconfig) | Interface | MSSQL connection config |
46
- | [`PostgresqlDbConnConfig`](./docs/connection.md#postgresqldbconnconfig) | Interface | PostgreSQL connection config |
47
-
48
- ### Constants & Utilities
49
-
50
- | Export | Type | Description |
51
- |--------|------|-------------|
52
- | [`DB_CONN_CONNECT_TIMEOUT`](./docs/connection.md#db_conn_connect_timeout) | Constant | Connection timeout: 10,000ms (10s) |
53
- | [`DB_CONN_DEFAULT_TIMEOUT`](./docs/connection.md#db_conn_default_timeout) | Constant | Query timeout: 600,000ms (10min) |
54
- | [`DB_CONN_ERRORS`](./docs/connection.md#db_conn_errors) | Constant | Error message constants |
55
- | [`getDialectFromConfig`](./docs/connection.md#getdialectfromconfig) | Function | Extract `Dialect` from config (`mssql-azure` -> `mssql`) |
56
-
57
- ## Usage Examples
58
-
59
- ### Basic ORM Usage
60
-
61
- ```typescript
62
- import { DbContext } from "@simplysm/orm-common";
63
- import { createOrm } from "@simplysm/orm-node";
64
-
65
- class MyDb extends DbContext {
66
- user = this.queryable(User);
67
- post = this.queryable(Post);
68
- }
69
-
70
- const orm = createOrm(MyDb, {
71
- dialect: "mysql",
72
- host: "localhost",
73
- port: 3306,
74
- username: "root",
75
- password: "password",
76
- database: "mydb",
77
- });
78
-
79
- // With transaction (auto commit/rollback)
80
- const users = await orm.connect(async (db) => {
81
- return db.user().execute();
82
- });
83
-
84
- // Without transaction (for DDL or read-only)
85
- await orm.connectWithoutTransaction(async (db) => {
86
- await db.initialize();
87
- });
88
- ```
89
-
90
- ### Low-Level Connection
91
-
92
- ```typescript
93
- import { createDbConn } from "@simplysm/orm-node";
94
-
95
- const conn = await createDbConn({
96
- dialect: "postgresql",
97
- host: "localhost",
98
- port: 5432,
99
- username: "postgres",
100
- password: "password",
101
- database: "mydb",
102
- });
103
-
104
- await conn.connect();
105
- const results = await conn.execute(["SELECT * FROM users"]);
106
- await conn.close();
107
- ```
108
-
109
- ### With OrmOptions Override
110
-
111
- ```typescript
112
- const orm = createOrm(MyDb, config, {
113
- database: "other_db", // Override config.database
114
- schema: "custom", // Override config.schema
115
- });
116
- ```
@@ -1,252 +0,0 @@
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"` |
@@ -1,122 +0,0 @@
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<T extends DbContext>(
9
- DbClass: new (executor: DbContextExecutor, opt: { database: string; schema?: string }) => T,
10
- config: DbConnConfig,
11
- options?: OrmOptions,
12
- ): Orm<T>
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
- The `database` parameter is required -- it is resolved from `options.database` first, then `config.database`. An error is thrown if neither is provided.
18
-
19
- | Parameter | Type | Description |
20
- |-----------|------|-------------|
21
- | `DbClass` | `new (executor, opt) => T` | DbContext subclass constructor |
22
- | `config` | `DbConnConfig` | Database connection configuration |
23
- | `options` | `OrmOptions?` | Optional overrides for database/schema |
24
-
25
- ```typescript
26
- import { DbContext } from "@simplysm/orm-common";
27
- import { createOrm } from "@simplysm/orm-node";
28
-
29
- class MyDb extends DbContext {
30
- user = this.queryable(User);
31
- post = this.queryable(Post);
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
- // Transaction mode
44
- await orm.connect(async (db) => {
45
- const users = await db.user().execute();
46
- return users;
47
- });
48
-
49
- // No-transaction mode (for DDL, read-only)
50
- await orm.connectWithoutTransaction(async (db) => {
51
- // ...
52
- });
53
- ```
54
-
55
- ## Orm
56
-
57
- ```typescript
58
- interface Orm<T extends DbContext> {
59
- readonly DbClass: new (
60
- executor: DbContextExecutor,
61
- opt: { database: string; schema?: string },
62
- ) => T;
63
- readonly config: DbConnConfig;
64
- readonly options?: OrmOptions;
65
- connect<R>(callback: (conn: T) => Promise<R>, isolationLevel?: IsolationLevel): Promise<R>;
66
- connectWithoutTransaction<R>(callback: (conn: T) => Promise<R>): Promise<R>;
67
- }
68
- ```
69
-
70
- ORM instance returned by `createOrm()`.
71
-
72
- | Field | Type | Description |
73
- |-------|------|-------------|
74
- | `DbClass` | `new (executor, opt) => T` | The DbContext subclass constructor |
75
- | `config` | `DbConnConfig` | The connection configuration |
76
- | `options` | `OrmOptions?` | The ORM options |
77
-
78
- | Method | Signature | Description |
79
- |--------|-----------|-------------|
80
- | `connect` | `<R>(callback: (conn: T) => Promise<R>, isolationLevel?) => Promise<R>` | Open connection, begin transaction, execute callback, commit (auto-rollback on error), close |
81
- | `connectWithoutTransaction` | `<R>(callback: (conn: T) => Promise<R>) => Promise<R>` | Open connection, execute callback, close (no transaction) |
82
-
83
- ## OrmOptions
84
-
85
- ```typescript
86
- interface OrmOptions {
87
- database?: string;
88
- schema?: string;
89
- }
90
- ```
91
-
92
- | Field | Type | Description |
93
- |-------|------|-------------|
94
- | `database` | `string?` | Override the database name from `DbConnConfig` |
95
- | `schema` | `string?` | Override the schema name (MSSQL: dbo, PostgreSQL: public) |
96
-
97
- ## NodeDbContextExecutor
98
-
99
- ```typescript
100
- class NodeDbContextExecutor implements DbContextExecutor {
101
- constructor(config: DbConnConfig);
102
- }
103
- ```
104
-
105
- Node.js implementation of `DbContextExecutor`. Manages a single database connection and provides query execution capabilities.
106
-
107
- | Method | Signature | Description |
108
- |--------|-----------|-------------|
109
- | `connect` | `() => Promise<void>` | Establish DB connection |
110
- | `close` | `() => Promise<void>` | Close DB connection |
111
- | `beginTransaction` | `(isolationLevel?: IsolationLevel) => Promise<void>` | Start transaction |
112
- | `commitTransaction` | `() => Promise<void>` | Commit transaction |
113
- | `rollbackTransaction` | `() => Promise<void>` | Rollback transaction |
114
- | `executeParametrized` | `(query: string, params?: unknown[]) => Promise<Record<string, unknown>[][]>` | Execute parameterized SQL query |
115
- | `bulkInsert` | `(tableName: string, columnMetas: Record<string, ColumnMeta>, records: DataRecord[]) => Promise<void>` | Bulk insert using native DB API |
116
- | `executeDefs` | `<T>(defs: QueryDef[], resultMetas?: (ResultMeta \| undefined)[]) => Promise<T[][]>` | Execute QueryDef array (builds SQL via `createQueryBuilder`, parses results via `parseQueryResult`) |
117
-
118
- The `executeDefs` method:
119
- 1. Builds SQL from each QueryDef using the dialect-specific QueryBuilder
120
- 2. If no resultMetas need data, combines all SQL into a single execution
121
- 3. Otherwise executes each QueryDef individually
122
- 4. Parses results using `parseQueryResult` when ResultMeta is provided