@simplysm/orm-node 13.0.97 → 13.0.99
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 +227 -0
- package/package.json +4 -5
package/README.md
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
# @simplysm/orm-node
|
|
2
|
+
|
|
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
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @simplysm/orm-node
|
|
9
|
+
```
|
|
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
|
+
|
|
19
|
+
## API Overview
|
|
20
|
+
|
|
21
|
+
### Types
|
|
22
|
+
|
|
23
|
+
| API | Type | Description |
|
|
24
|
+
|-----|------|-------------|
|
|
25
|
+
| `DbConn` | interface | Low-level DB connection interface |
|
|
26
|
+
| `DbConnConfig` | type | Union of all connection config types |
|
|
27
|
+
| `MysqlDbConnConfig` | interface | MySQL connection configuration |
|
|
28
|
+
| `MssqlDbConnConfig` | interface | MSSQL connection configuration |
|
|
29
|
+
| `PostgresqlDbConnConfig` | interface | PostgreSQL connection configuration |
|
|
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 |
|
|
34
|
+
|
|
35
|
+
### Connections
|
|
36
|
+
|
|
37
|
+
| API | Type | Description |
|
|
38
|
+
|-----|------|-------------|
|
|
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 |
|
|
43
|
+
|
|
44
|
+
### Core
|
|
45
|
+
|
|
46
|
+
| API | Type | Description |
|
|
47
|
+
|-----|------|-------------|
|
|
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) |
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
### `DbConnConfig`
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
type DbConnConfig = MysqlDbConnConfig | MssqlDbConnConfig | PostgresqlDbConnConfig;
|
|
59
|
+
```
|
|
60
|
+
|
|
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
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
function createDbConn(config: DbConnConfig): Promise<DbConn>
|
|
121
|
+
```
|
|
122
|
+
|
|
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
|
+
|
|
125
|
+
### `getDialectFromConfig`
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
function getDialectFromConfig(config: DbConnConfig): Dialect
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Extracts the `Dialect` from a config. Maps `"mssql-azure"` to `"mssql"`.
|
|
132
|
+
|
|
133
|
+
### `OrmOptions`
|
|
134
|
+
|
|
135
|
+
| Field | Type | Description |
|
|
136
|
+
|-------|------|-------------|
|
|
137
|
+
| `database` | `string?` | Database name (overrides DbConnConfig's database) |
|
|
138
|
+
| `schema` | `string?` | Schema name (MSSQL: dbo, PostgreSQL: public) |
|
|
139
|
+
|
|
140
|
+
### `Orm<TDef>`
|
|
141
|
+
|
|
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 |
|
|
149
|
+
|
|
150
|
+
### `createOrm`
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
function createOrm<TDef extends DbContextDef<any, any, any>>(
|
|
154
|
+
dbContextDef: TDef,
|
|
155
|
+
config: DbConnConfig,
|
|
156
|
+
options?: OrmOptions,
|
|
157
|
+
): Orm<TDef>
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### `NodeDbContextExecutor`
|
|
161
|
+
|
|
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 |
|
|
173
|
+
|
|
174
|
+
## Usage Examples
|
|
175
|
+
|
|
176
|
+
### Using createOrm (recommended)
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
import { createOrm } from "@simplysm/orm-node";
|
|
180
|
+
import { defineDbContext, queryable } from "@simplysm/orm-common";
|
|
181
|
+
|
|
182
|
+
const MyDb = defineDbContext({
|
|
183
|
+
user: (db) => queryable(db, User),
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const orm = createOrm(MyDb, {
|
|
187
|
+
dialect: "mysql",
|
|
188
|
+
host: "localhost",
|
|
189
|
+
port: 3306,
|
|
190
|
+
username: "root",
|
|
191
|
+
password: "password",
|
|
192
|
+
database: "mydb",
|
|
193
|
+
});
|
|
194
|
+
|
|
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;
|
|
205
|
+
});
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Using low-level connection
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
import { createDbConn } from "@simplysm/orm-node";
|
|
212
|
+
|
|
213
|
+
const conn = await createDbConn({
|
|
214
|
+
dialect: "postgresql",
|
|
215
|
+
host: "localhost",
|
|
216
|
+
username: "admin",
|
|
217
|
+
password: "secret",
|
|
218
|
+
database: "testdb",
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
await conn.connect();
|
|
222
|
+
try {
|
|
223
|
+
const results = await conn.execute(["SELECT * FROM users"]);
|
|
224
|
+
} finally {
|
|
225
|
+
await conn.close();
|
|
226
|
+
}
|
|
227
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simplysm/orm-node",
|
|
3
|
-
"version": "13.0.
|
|
3
|
+
"version": "13.0.99",
|
|
4
4
|
"description": "Simplysm package - ORM module (node)",
|
|
5
5
|
"author": "simplysm",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -14,17 +14,16 @@
|
|
|
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.99",
|
|
23
|
+
"@simplysm/orm-common": "13.0.99"
|
|
25
24
|
},
|
|
26
25
|
"devDependencies": {
|
|
27
|
-
"@types/pg": "^8.
|
|
26
|
+
"@types/pg": "^8.20.0",
|
|
28
27
|
"@types/pg-copy-streams": "^1.2.5",
|
|
29
28
|
"mysql2": "^3.20.0",
|
|
30
29
|
"pg": "^8.20.0",
|