@petradb/engine 1.0.0 → 1.1.0
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 +24 -23
- package/index.d.ts +13 -6
- package/main.js +40489 -37999
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,11 +9,11 @@ npm install @petradb/engine
|
|
|
9
9
|
## Quick Start
|
|
10
10
|
|
|
11
11
|
```javascript
|
|
12
|
-
import {
|
|
12
|
+
import { Session } from '@petradb/engine';
|
|
13
13
|
|
|
14
|
-
const db = new
|
|
14
|
+
const db = new Session();
|
|
15
15
|
|
|
16
|
-
db.execute(`
|
|
16
|
+
await db.execute(`
|
|
17
17
|
CREATE TABLE users (
|
|
18
18
|
id SERIAL,
|
|
19
19
|
name TEXT NOT NULL,
|
|
@@ -22,10 +22,10 @@ db.execute(`
|
|
|
22
22
|
)
|
|
23
23
|
`);
|
|
24
24
|
|
|
25
|
-
db.execute("INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com')");
|
|
26
|
-
db.execute("INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com')");
|
|
25
|
+
await db.execute("INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com')");
|
|
26
|
+
await db.execute("INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com')");
|
|
27
27
|
|
|
28
|
-
const [{ rows, fields }] = db.execute('SELECT * FROM users');
|
|
28
|
+
const [{ rows, fields }] = await db.execute('SELECT * FROM users');
|
|
29
29
|
// rows: [{ id: 1, name: 'Alice', email: 'alice@example.com' }, ...]
|
|
30
30
|
// fields: [{ name: 'id', dataType: 'serial' }, { name: 'name', dataType: 'text' }, ...]
|
|
31
31
|
```
|
|
@@ -36,10 +36,10 @@ By default, SELECT rows are returned as objects keyed by column name. Use `rowMo
|
|
|
36
36
|
|
|
37
37
|
```javascript
|
|
38
38
|
// Set default for all queries
|
|
39
|
-
const db = new
|
|
39
|
+
const db = new Session({ rowMode: 'array' });
|
|
40
40
|
|
|
41
41
|
// Or override per call
|
|
42
|
-
const [{ rows }] = db.execute('SELECT id, name FROM users', { rowMode: 'array' });
|
|
42
|
+
const [{ rows }] = await db.execute('SELECT id, name FROM users', { rowMode: 'array' });
|
|
43
43
|
// rows: [[1, 'Alice'], [2, 'Bob']]
|
|
44
44
|
```
|
|
45
45
|
|
|
@@ -99,6 +99,7 @@ UPDATE ... SET ... WHERE
|
|
|
99
99
|
UPDATE ... SET ... FROM ... -- bulk update with join semantics
|
|
100
100
|
DELETE FROM ... WHERE
|
|
101
101
|
TRUNCATE TABLE -- fast table reset, resets serial sequences
|
|
102
|
+
INSERT ... ON CONFLICT (col) DO UPDATE SET ... -- upsert with EXCLUDED pseudo-table
|
|
102
103
|
```
|
|
103
104
|
|
|
104
105
|
### Queries
|
|
@@ -156,7 +157,7 @@ DEALLOCATE name
|
|
|
156
157
|
|
|
157
158
|
## API
|
|
158
159
|
|
|
159
|
-
### `new
|
|
160
|
+
### `new Session(options?)`
|
|
160
161
|
|
|
161
162
|
Creates a new database instance. Each instance is fully isolated.
|
|
162
163
|
|
|
@@ -166,7 +167,7 @@ Creates a new database instance. Each instance is fully isolated.
|
|
|
166
167
|
|
|
167
168
|
### `db.execute(sql, options?)`
|
|
168
169
|
|
|
169
|
-
Executes one or more SQL statements separated by `;`. Returns an array of results.
|
|
170
|
+
Executes one or more SQL statements separated by `;`. Returns a promise that resolves to an array of results.
|
|
170
171
|
|
|
171
172
|
| Option | Type | Default | Description |
|
|
172
173
|
|--------|------|---------|-------------|
|
|
@@ -178,10 +179,10 @@ Creates a prepared statement with `$1`, `$2`, ... parameter placeholders. Return
|
|
|
178
179
|
|
|
179
180
|
```javascript
|
|
180
181
|
const stmt = db.prepare('SELECT * FROM users WHERE id = $1');
|
|
181
|
-
const [{ rows }] = stmt.execute([42]);
|
|
182
|
+
const [{ rows }] = await stmt.execute([42]);
|
|
182
183
|
|
|
183
184
|
// With options
|
|
184
|
-
const [{ rows }] = stmt.execute([42], { rowMode: 'array' });
|
|
185
|
+
const [{ rows }] = await stmt.execute([42], { rowMode: 'array' });
|
|
185
186
|
```
|
|
186
187
|
|
|
187
188
|
### Result Types
|
|
@@ -200,10 +201,10 @@ Every result has a `command` field for easy discrimination:
|
|
|
200
201
|
{ command: 'alter table' }
|
|
201
202
|
|
|
202
203
|
// DML
|
|
203
|
-
{ command: 'insert', result: Record<string, any
|
|
204
|
+
{ command: 'insert', result: Record<string, any>, rows: T[], fields: FieldInfo[] }
|
|
204
205
|
{ command: 'select', rows: T[], fields: { name: string, dataType: string }[] }
|
|
205
|
-
{ command: 'update',
|
|
206
|
-
{ command: 'delete',
|
|
206
|
+
{ command: 'update', rowCount: number }
|
|
207
|
+
{ command: 'delete', rowCount: number }
|
|
207
208
|
|
|
208
209
|
// Transactions
|
|
209
210
|
{ command: 'begin' }
|
|
@@ -234,10 +235,10 @@ Every result has a `command` field for easy discrimination:
|
|
|
234
235
|
Full type definitions are included. Use discriminated unions to narrow result types:
|
|
235
236
|
|
|
236
237
|
```typescript
|
|
237
|
-
import {
|
|
238
|
+
import { Session, ExecuteResult } from '@petradb/engine';
|
|
238
239
|
|
|
239
|
-
const db = new
|
|
240
|
-
const results: ExecuteResult[] = db.execute('SELECT * FROM users');
|
|
240
|
+
const db = new Session();
|
|
241
|
+
const results: ExecuteResult[] = await db.execute('SELECT * FROM users');
|
|
241
242
|
|
|
242
243
|
for (const result of results) {
|
|
243
244
|
if (result.command === 'select') {
|
|
@@ -249,11 +250,11 @@ for (const result of results) {
|
|
|
249
250
|
## Example
|
|
250
251
|
|
|
251
252
|
```javascript
|
|
252
|
-
import {
|
|
253
|
+
import { Session } from '@petradb/engine';
|
|
253
254
|
|
|
254
|
-
const db = new
|
|
255
|
+
const db = new Session();
|
|
255
256
|
|
|
256
|
-
db.execute(`
|
|
257
|
+
await db.execute(`
|
|
257
258
|
CREATE TYPE status AS ENUM ('active', 'inactive');
|
|
258
259
|
CREATE TABLE products (
|
|
259
260
|
id SERIAL,
|
|
@@ -266,14 +267,14 @@ db.execute(`
|
|
|
266
267
|
)
|
|
267
268
|
`);
|
|
268
269
|
|
|
269
|
-
db.execute(`
|
|
270
|
+
await db.execute(`
|
|
270
271
|
INSERT INTO products (name, price, tags, created_at) VALUES
|
|
271
272
|
('Laptop', 999.99, '["electronics", "computers"]', '2025-01-15 10:30:00');
|
|
272
273
|
INSERT INTO products (name, price, tags, created_at) VALUES
|
|
273
274
|
('Coffee', 4.50, '["food", "organic"]', '2025-01-16 08:00:00')
|
|
274
275
|
`);
|
|
275
276
|
|
|
276
|
-
const [{ rows }] = db.execute(`
|
|
277
|
+
const [{ rows }] = await db.execute(`
|
|
277
278
|
SELECT name, price FROM products
|
|
278
279
|
WHERE price > 10
|
|
279
280
|
ORDER BY price DESC
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export interface
|
|
1
|
+
export interface SessionOptions {
|
|
2
2
|
rowMode?: 'object' | 'array';
|
|
3
3
|
}
|
|
4
4
|
|
|
@@ -75,6 +75,8 @@ export interface RollbackResult {
|
|
|
75
75
|
export interface InsertResult {
|
|
76
76
|
command: 'insert';
|
|
77
77
|
result: Record<string, any>;
|
|
78
|
+
rows: Record<string, any>[];
|
|
79
|
+
fields: FieldInfo[];
|
|
78
80
|
}
|
|
79
81
|
|
|
80
82
|
export interface SelectResult<T = Record<string, any>> {
|
|
@@ -85,12 +87,12 @@ export interface SelectResult<T = Record<string, any>> {
|
|
|
85
87
|
|
|
86
88
|
export interface UpdateResult {
|
|
87
89
|
command: 'update';
|
|
88
|
-
|
|
90
|
+
rowCount: number;
|
|
89
91
|
}
|
|
90
92
|
|
|
91
93
|
export interface DeleteResult {
|
|
92
94
|
command: 'delete';
|
|
93
|
-
|
|
95
|
+
rowCount: number;
|
|
94
96
|
}
|
|
95
97
|
|
|
96
98
|
export type ExecuteResult =
|
|
@@ -112,7 +114,12 @@ export type ExecuteResult =
|
|
|
112
114
|
| CommitResult
|
|
113
115
|
| RollbackResult;
|
|
114
116
|
|
|
115
|
-
export
|
|
116
|
-
|
|
117
|
-
|
|
117
|
+
export interface PreparedStatement {
|
|
118
|
+
execute(params?: any[], options?: ExecuteOptions): Promise<ExecuteResult[]>;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export class Session {
|
|
122
|
+
constructor(options?: SessionOptions);
|
|
123
|
+
execute(sql: string, options?: ExecuteOptions): Promise<ExecuteResult[]>;
|
|
124
|
+
prepare(sql: string): PreparedStatement;
|
|
118
125
|
}
|