@simplysm/orm-node 13.0.98 → 13.0.100
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 +127 -149
- package/package.json +3 -4
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @simplysm/orm-node
|
|
2
2
|
|
|
3
|
-
ORM module (node)
|
|
3
|
+
Simplysm package - ORM module (node). Provides database connection implementations for MySQL, MSSQL, and PostgreSQL, plus a high-level ORM factory for managing DbContext instances with transaction support.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -8,194 +8,172 @@ ORM module (node) -- database connections and ORM integration for MySQL, Postgre
|
|
|
8
8
|
npm install @simplysm/orm-node
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
Database drivers are loaded lazily. Install only the driver(s) you need:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install mysql2 # for MySQL
|
|
15
|
+
npm install tedious # for MSSQL / Azure SQL
|
|
16
|
+
npm install pg pg-copy-streams # for PostgreSQL
|
|
17
|
+
```
|
|
18
|
+
|
|
11
19
|
## API Overview
|
|
12
20
|
|
|
13
21
|
### Types
|
|
14
22
|
|
|
15
23
|
| API | Type | Description |
|
|
16
24
|
|-----|------|-------------|
|
|
17
|
-
| `DB_CONN_CONNECT_TIMEOUT` | const | Connection establishment timeout (10 seconds) |
|
|
18
|
-
| `DB_CONN_DEFAULT_TIMEOUT` | const | Query default timeout (10 minutes) |
|
|
19
|
-
| `DB_CONN_ERRORS` | const | Error message constants (`NOT_CONNECTED`, `ALREADY_CONNECTED`) |
|
|
20
25
|
| `DbConn` | interface | Low-level DB connection interface |
|
|
21
|
-
| `DbConnConfig` | type | Union of all
|
|
26
|
+
| `DbConnConfig` | type | Union of all connection config types |
|
|
22
27
|
| `MysqlDbConnConfig` | interface | MySQL connection configuration |
|
|
23
|
-
| `MssqlDbConnConfig` | interface | MSSQL
|
|
28
|
+
| `MssqlDbConnConfig` | interface | MSSQL connection configuration |
|
|
24
29
|
| `PostgresqlDbConnConfig` | interface | PostgreSQL connection configuration |
|
|
25
|
-
| `
|
|
30
|
+
| `DB_CONN_CONNECT_TIMEOUT` | const | Connection timeout (10 seconds) |
|
|
31
|
+
| `DB_CONN_DEFAULT_TIMEOUT` | const | Query default timeout (10 minutes) |
|
|
32
|
+
| `DB_CONN_ERRORS` | const | Error message constants |
|
|
33
|
+
| `getDialectFromConfig` | function | Extract Dialect from DbConnConfig |
|
|
26
34
|
|
|
27
35
|
### Connections
|
|
28
36
|
|
|
29
37
|
| API | Type | Description |
|
|
30
38
|
|-----|------|-------------|
|
|
31
|
-
| `
|
|
32
|
-
| `
|
|
33
|
-
| `PostgresqlDbConn` | class | PostgreSQL connection
|
|
34
|
-
| `
|
|
39
|
+
| `MysqlDbConn` | class | MySQL connection (uses mysql2/promise) |
|
|
40
|
+
| `MssqlDbConn` | class | MSSQL/Azure SQL connection (uses tedious) |
|
|
41
|
+
| `PostgresqlDbConn` | class | PostgreSQL connection (uses pg) |
|
|
42
|
+
| `createDbConn` | function | DB connection factory function |
|
|
35
43
|
|
|
36
44
|
### Core
|
|
37
45
|
|
|
38
46
|
| API | Type | Description |
|
|
39
47
|
|-----|------|-------------|
|
|
40
|
-
| `
|
|
41
|
-
| `
|
|
42
|
-
| `
|
|
43
|
-
| `
|
|
48
|
+
| `NodeDbContextExecutor` | class | DbContextExecutor for Node.js |
|
|
49
|
+
| `createOrm` | function | ORM factory function |
|
|
50
|
+
| `Orm` | interface | ORM instance type |
|
|
51
|
+
| `OrmOptions` | interface | ORM options (database/schema override) |
|
|
44
52
|
|
|
45
|
-
|
|
53
|
+
---
|
|
46
54
|
|
|
47
|
-
|
|
48
|
-
type DbConnConfig = MysqlDbConnConfig | MssqlDbConnConfig | PostgresqlDbConnConfig;
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
## `MysqlDbConnConfig`
|
|
55
|
+
### `DbConnConfig`
|
|
52
56
|
|
|
53
57
|
```typescript
|
|
54
|
-
|
|
55
|
-
dialect: "mysql";
|
|
56
|
-
host: string;
|
|
57
|
-
port?: number;
|
|
58
|
-
username: string;
|
|
59
|
-
password: string;
|
|
60
|
-
database?: string;
|
|
61
|
-
defaultIsolationLevel?: IsolationLevel;
|
|
62
|
-
}
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
## `MssqlDbConnConfig`
|
|
66
|
-
|
|
67
|
-
```typescript
|
|
68
|
-
interface MssqlDbConnConfig {
|
|
69
|
-
dialect: "mssql" | "mssql-azure";
|
|
70
|
-
host: string;
|
|
71
|
-
port?: number;
|
|
72
|
-
username: string;
|
|
73
|
-
password: string;
|
|
74
|
-
database?: string;
|
|
75
|
-
schema?: string;
|
|
76
|
-
defaultIsolationLevel?: IsolationLevel;
|
|
77
|
-
}
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
## `PostgresqlDbConnConfig`
|
|
81
|
-
|
|
82
|
-
```typescript
|
|
83
|
-
interface PostgresqlDbConnConfig {
|
|
84
|
-
dialect: "postgresql";
|
|
85
|
-
host: string;
|
|
86
|
-
port?: number;
|
|
87
|
-
username: string;
|
|
88
|
-
password: string;
|
|
89
|
-
database?: string;
|
|
90
|
-
schema?: string;
|
|
91
|
-
defaultIsolationLevel?: IsolationLevel;
|
|
92
|
-
}
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
## `DbConn`
|
|
96
|
-
|
|
97
|
-
```typescript
|
|
98
|
-
interface DbConn extends EventEmitter<{ close: void }> {
|
|
99
|
-
config: DbConnConfig;
|
|
100
|
-
isConnected: boolean;
|
|
101
|
-
isInTransaction: boolean;
|
|
102
|
-
connect(): Promise<void>;
|
|
103
|
-
close(): Promise<void>;
|
|
104
|
-
beginTransaction(isolationLevel?: IsolationLevel): Promise<void>;
|
|
105
|
-
commitTransaction(): Promise<void>;
|
|
106
|
-
rollbackTransaction(): Promise<void>;
|
|
107
|
-
execute(queries: string[]): Promise<Record<string, unknown>[][]>;
|
|
108
|
-
executeParametrized(query: string, params?: unknown[]): Promise<Record<string, unknown>[][]>;
|
|
109
|
-
bulkInsert(
|
|
110
|
-
tableName: string,
|
|
111
|
-
columnMetas: Record<string, ColumnMeta>,
|
|
112
|
-
records: Record<string, unknown>[],
|
|
113
|
-
): Promise<void>;
|
|
114
|
-
}
|
|
58
|
+
type DbConnConfig = MysqlDbConnConfig | MssqlDbConnConfig | PostgresqlDbConnConfig;
|
|
115
59
|
```
|
|
116
60
|
|
|
117
|
-
|
|
61
|
+
### `MysqlDbConnConfig`
|
|
62
|
+
|
|
63
|
+
| Field | Type | Description |
|
|
64
|
+
|-------|------|-------------|
|
|
65
|
+
| `dialect` | `"mysql"` | Dialect identifier |
|
|
66
|
+
| `host` | `string` | Server hostname |
|
|
67
|
+
| `port` | `number?` | Server port |
|
|
68
|
+
| `username` | `string` | Username |
|
|
69
|
+
| `password` | `string` | Password |
|
|
70
|
+
| `database` | `string?` | Database name |
|
|
71
|
+
| `defaultIsolationLevel` | `IsolationLevel?` | Default transaction isolation level |
|
|
72
|
+
|
|
73
|
+
### `MssqlDbConnConfig`
|
|
74
|
+
|
|
75
|
+
| Field | Type | Description |
|
|
76
|
+
|-------|------|-------------|
|
|
77
|
+
| `dialect` | `"mssql" \| "mssql-azure"` | Dialect identifier |
|
|
78
|
+
| `host` | `string` | Server hostname |
|
|
79
|
+
| `port` | `number?` | Server port |
|
|
80
|
+
| `username` | `string` | Username |
|
|
81
|
+
| `password` | `string` | Password |
|
|
82
|
+
| `database` | `string?` | Database name |
|
|
83
|
+
| `schema` | `string?` | Schema name |
|
|
84
|
+
| `defaultIsolationLevel` | `IsolationLevel?` | Default transaction isolation level |
|
|
85
|
+
|
|
86
|
+
### `PostgresqlDbConnConfig`
|
|
87
|
+
|
|
88
|
+
| Field | Type | Description |
|
|
89
|
+
|-------|------|-------------|
|
|
90
|
+
| `dialect` | `"postgresql"` | Dialect identifier |
|
|
91
|
+
| `host` | `string` | Server hostname |
|
|
92
|
+
| `port` | `number?` | Server port |
|
|
93
|
+
| `username` | `string` | Username |
|
|
94
|
+
| `password` | `string` | Password |
|
|
95
|
+
| `database` | `string?` | Database name |
|
|
96
|
+
| `schema` | `string?` | Schema name |
|
|
97
|
+
| `defaultIsolationLevel` | `IsolationLevel?` | Default transaction isolation level |
|
|
98
|
+
|
|
99
|
+
### `DbConn`
|
|
100
|
+
|
|
101
|
+
Interface extending `EventEmitter<{ close: void }>`. Implemented by `MysqlDbConn`, `MssqlDbConn`, and `PostgresqlDbConn`.
|
|
102
|
+
|
|
103
|
+
| Property/Method | Signature | Description |
|
|
104
|
+
|-----------------|-----------|-------------|
|
|
105
|
+
| `config` | `DbConnConfig` | Connection configuration |
|
|
106
|
+
| `isConnected` | `boolean` | Whether connected |
|
|
107
|
+
| `isInTransaction` | `boolean` | Whether transaction is in progress |
|
|
108
|
+
| `connect` | `() => Promise<void>` | Establish DB connection |
|
|
109
|
+
| `close` | `() => Promise<void>` | Close DB connection |
|
|
110
|
+
| `beginTransaction` | `(isolationLevel?: IsolationLevel) => Promise<void>` | Begin transaction |
|
|
111
|
+
| `commitTransaction` | `() => Promise<void>` | Commit transaction |
|
|
112
|
+
| `rollbackTransaction` | `() => Promise<void>` | Rollback transaction |
|
|
113
|
+
| `execute` | `(queries: string[]) => Promise<Record<string, unknown>[][]>` | Execute SQL query array |
|
|
114
|
+
| `executeParametrized` | `(query: string, params?: unknown[]) => Promise<Record<string, unknown>[][]>` | Execute parameterized query |
|
|
115
|
+
| `bulkInsert` | `(tableName: string, columnMetas: Record<string, ColumnMeta>, records: Record<string, unknown>[]) => Promise<void>` | Bulk INSERT using native API |
|
|
116
|
+
|
|
117
|
+
### `createDbConn`
|
|
118
118
|
|
|
119
119
|
```typescript
|
|
120
|
-
function
|
|
120
|
+
function createDbConn(config: DbConnConfig): Promise<DbConn>
|
|
121
121
|
```
|
|
122
122
|
|
|
123
|
-
|
|
123
|
+
Factory function that creates a DB connection instance. The returned connection is **not yet connected** -- call `connect()` separately. Database drivers are lazily loaded.
|
|
124
124
|
|
|
125
|
-
|
|
125
|
+
### `getDialectFromConfig`
|
|
126
126
|
|
|
127
127
|
```typescript
|
|
128
|
-
|
|
128
|
+
function getDialectFromConfig(config: DbConnConfig): Dialect
|
|
129
129
|
```
|
|
130
130
|
|
|
131
|
-
|
|
131
|
+
Extracts the `Dialect` from a config. Maps `"mssql-azure"` to `"mssql"`.
|
|
132
132
|
|
|
133
|
-
|
|
133
|
+
### `OrmOptions`
|
|
134
134
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
```
|
|
135
|
+
| Field | Type | Description |
|
|
136
|
+
|-------|------|-------------|
|
|
137
|
+
| `database` | `string?` | Database name (overrides DbConnConfig's database) |
|
|
138
|
+
| `schema` | `string?` | Schema name (MSSQL: dbo, PostgreSQL: public) |
|
|
141
139
|
|
|
142
|
-
|
|
140
|
+
### `Orm<TDef>`
|
|
143
141
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
isolationLevel?: IsolationLevel,
|
|
152
|
-
): Promise<R>;
|
|
153
|
-
connectWithoutTransaction<R>(
|
|
154
|
-
callback: (conn: DbContextInstance<TDef>) => Promise<R>,
|
|
155
|
-
): Promise<R>;
|
|
156
|
-
}
|
|
157
|
-
```
|
|
142
|
+
| Property/Method | Signature | Description |
|
|
143
|
+
|-----------------|-----------|-------------|
|
|
144
|
+
| `dbContextDef` | `TDef` | DbContext definition |
|
|
145
|
+
| `config` | `DbConnConfig` | Connection configuration |
|
|
146
|
+
| `options` | `OrmOptions?` | ORM options |
|
|
147
|
+
| `connect` | `<R>(callback: (conn: DbContextInstance<TDef>) => Promise<R>, isolationLevel?: IsolationLevel) => Promise<R>` | Execute callback within a transaction |
|
|
148
|
+
| `connectWithoutTransaction` | `<R>(callback: (conn: DbContextInstance<TDef>) => Promise<R>) => Promise<R>` | Execute callback without a transaction |
|
|
158
149
|
|
|
159
|
-
|
|
150
|
+
### `createOrm`
|
|
160
151
|
|
|
161
152
|
```typescript
|
|
162
153
|
function createOrm<TDef extends DbContextDef<any, any, any>>(
|
|
163
154
|
dbContextDef: TDef,
|
|
164
155
|
config: DbConnConfig,
|
|
165
156
|
options?: OrmOptions,
|
|
166
|
-
): Orm<TDef
|
|
157
|
+
): Orm<TDef>
|
|
167
158
|
```
|
|
168
159
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
## `NodeDbContextExecutor`
|
|
160
|
+
### `NodeDbContextExecutor`
|
|
172
161
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
columnMetas: Record<string, ColumnMeta>,
|
|
185
|
-
records: DataRecord[],
|
|
186
|
-
): Promise<void>;
|
|
187
|
-
async executeDefs<T = DataRecord>(
|
|
188
|
-
defs: QueryDef[],
|
|
189
|
-
resultMetas?: (ResultMeta | undefined)[],
|
|
190
|
-
): Promise<T[][]>;
|
|
191
|
-
}
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
DbContextExecutor for Node.js. Handles actual DB connections and query execution including QueryDef-to-SQL conversion.
|
|
162
|
+
| Method | Signature | Description |
|
|
163
|
+
|--------|-----------|-------------|
|
|
164
|
+
| `constructor` | `(config: DbConnConfig)` | Create executor with connection config |
|
|
165
|
+
| `connect` | `() => Promise<void>` | Establish DB connection |
|
|
166
|
+
| `close` | `() => Promise<void>` | Close DB connection |
|
|
167
|
+
| `beginTransaction` | `(isolationLevel?: IsolationLevel) => Promise<void>` | Begin transaction |
|
|
168
|
+
| `commitTransaction` | `() => Promise<void>` | Commit transaction |
|
|
169
|
+
| `rollbackTransaction` | `() => Promise<void>` | Rollback transaction |
|
|
170
|
+
| `executeParametrized` | `(query: string, params?: unknown[]) => Promise<Record<string, unknown>[][]>` | Execute parameterized query |
|
|
171
|
+
| `bulkInsert` | `(tableName: string, columnMetas: Record<string, ColumnMeta>, records: DataRecord[]) => Promise<void>` | Bulk insert |
|
|
172
|
+
| `executeDefs` | `<T>(defs: QueryDef[], resultMetas?: (ResultMeta \| undefined)[]) => Promise<T[][]>` | Execute QueryDef array |
|
|
195
173
|
|
|
196
174
|
## Usage Examples
|
|
197
175
|
|
|
198
|
-
###
|
|
176
|
+
### Using createOrm (recommended)
|
|
199
177
|
|
|
200
178
|
```typescript
|
|
201
179
|
import { createOrm } from "@simplysm/orm-node";
|
|
@@ -214,12 +192,20 @@ const orm = createOrm(MyDb, {
|
|
|
214
192
|
database: "mydb",
|
|
215
193
|
});
|
|
216
194
|
|
|
217
|
-
|
|
218
|
-
|
|
195
|
+
// Execute within a transaction
|
|
196
|
+
await orm.connect(async (db) => {
|
|
197
|
+
const users = await db.user().execute();
|
|
198
|
+
return users;
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// Execute without a transaction
|
|
202
|
+
await orm.connectWithoutTransaction(async (db) => {
|
|
203
|
+
const users = await db.user().execute();
|
|
204
|
+
return users;
|
|
219
205
|
});
|
|
220
206
|
```
|
|
221
207
|
|
|
222
|
-
###
|
|
208
|
+
### Using low-level connection
|
|
223
209
|
|
|
224
210
|
```typescript
|
|
225
211
|
import { createDbConn } from "@simplysm/orm-node";
|
|
@@ -239,11 +225,3 @@ try {
|
|
|
239
225
|
await conn.close();
|
|
240
226
|
}
|
|
241
227
|
```
|
|
242
|
-
|
|
243
|
-
### Run without transaction
|
|
244
|
-
|
|
245
|
-
```typescript
|
|
246
|
-
const result = await orm.connectWithoutTransaction(async (db) => {
|
|
247
|
-
return await db.user().execute();
|
|
248
|
-
});
|
|
249
|
-
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simplysm/orm-node",
|
|
3
|
-
"version": "13.0.
|
|
3
|
+
"version": "13.0.100",
|
|
4
4
|
"description": "Simplysm package - ORM module (node)",
|
|
5
5
|
"author": "simplysm",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -14,14 +14,13 @@
|
|
|
14
14
|
"types": "./dist/index.d.ts",
|
|
15
15
|
"files": [
|
|
16
16
|
"dist",
|
|
17
|
-
"docs",
|
|
18
17
|
"src"
|
|
19
18
|
],
|
|
20
19
|
"sideEffects": false,
|
|
21
20
|
"dependencies": {
|
|
22
21
|
"consola": "^3.4.2",
|
|
23
|
-
"@simplysm/core-common": "13.0.
|
|
24
|
-
"@simplysm/orm-common": "13.0.
|
|
22
|
+
"@simplysm/core-common": "13.0.100",
|
|
23
|
+
"@simplysm/orm-common": "13.0.100"
|
|
25
24
|
},
|
|
26
25
|
"devDependencies": {
|
|
27
26
|
"@types/pg": "^8.20.0",
|