@petradb/engine 1.0.0 → 1.0.1
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 +23 -23
- package/index.d.ts +13 -6
- package/main.js +39427 -37266
- 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
|
|
|
@@ -156,7 +156,7 @@ DEALLOCATE name
|
|
|
156
156
|
|
|
157
157
|
## API
|
|
158
158
|
|
|
159
|
-
### `new
|
|
159
|
+
### `new Session(options?)`
|
|
160
160
|
|
|
161
161
|
Creates a new database instance. Each instance is fully isolated.
|
|
162
162
|
|
|
@@ -166,7 +166,7 @@ Creates a new database instance. Each instance is fully isolated.
|
|
|
166
166
|
|
|
167
167
|
### `db.execute(sql, options?)`
|
|
168
168
|
|
|
169
|
-
Executes one or more SQL statements separated by `;`. Returns an array of results.
|
|
169
|
+
Executes one or more SQL statements separated by `;`. Returns a promise that resolves to an array of results.
|
|
170
170
|
|
|
171
171
|
| Option | Type | Default | Description |
|
|
172
172
|
|--------|------|---------|-------------|
|
|
@@ -178,10 +178,10 @@ Creates a prepared statement with `$1`, `$2`, ... parameter placeholders. Return
|
|
|
178
178
|
|
|
179
179
|
```javascript
|
|
180
180
|
const stmt = db.prepare('SELECT * FROM users WHERE id = $1');
|
|
181
|
-
const [{ rows }] = stmt.execute([42]);
|
|
181
|
+
const [{ rows }] = await stmt.execute([42]);
|
|
182
182
|
|
|
183
183
|
// With options
|
|
184
|
-
const [{ rows }] = stmt.execute([42], { rowMode: 'array' });
|
|
184
|
+
const [{ rows }] = await stmt.execute([42], { rowMode: 'array' });
|
|
185
185
|
```
|
|
186
186
|
|
|
187
187
|
### Result Types
|
|
@@ -200,10 +200,10 @@ Every result has a `command` field for easy discrimination:
|
|
|
200
200
|
{ command: 'alter table' }
|
|
201
201
|
|
|
202
202
|
// DML
|
|
203
|
-
{ command: 'insert', result: Record<string, any
|
|
203
|
+
{ command: 'insert', result: Record<string, any>, rows: T[], fields: FieldInfo[] }
|
|
204
204
|
{ command: 'select', rows: T[], fields: { name: string, dataType: string }[] }
|
|
205
|
-
{ command: 'update',
|
|
206
|
-
{ command: 'delete',
|
|
205
|
+
{ command: 'update', rowCount: number }
|
|
206
|
+
{ command: 'delete', rowCount: number }
|
|
207
207
|
|
|
208
208
|
// Transactions
|
|
209
209
|
{ command: 'begin' }
|
|
@@ -234,10 +234,10 @@ Every result has a `command` field for easy discrimination:
|
|
|
234
234
|
Full type definitions are included. Use discriminated unions to narrow result types:
|
|
235
235
|
|
|
236
236
|
```typescript
|
|
237
|
-
import {
|
|
237
|
+
import { Session, ExecuteResult } from '@petradb/engine';
|
|
238
238
|
|
|
239
|
-
const db = new
|
|
240
|
-
const results: ExecuteResult[] = db.execute('SELECT * FROM users');
|
|
239
|
+
const db = new Session();
|
|
240
|
+
const results: ExecuteResult[] = await db.execute('SELECT * FROM users');
|
|
241
241
|
|
|
242
242
|
for (const result of results) {
|
|
243
243
|
if (result.command === 'select') {
|
|
@@ -249,11 +249,11 @@ for (const result of results) {
|
|
|
249
249
|
## Example
|
|
250
250
|
|
|
251
251
|
```javascript
|
|
252
|
-
import {
|
|
252
|
+
import { Session } from '@petradb/engine';
|
|
253
253
|
|
|
254
|
-
const db = new
|
|
254
|
+
const db = new Session();
|
|
255
255
|
|
|
256
|
-
db.execute(`
|
|
256
|
+
await db.execute(`
|
|
257
257
|
CREATE TYPE status AS ENUM ('active', 'inactive');
|
|
258
258
|
CREATE TABLE products (
|
|
259
259
|
id SERIAL,
|
|
@@ -266,14 +266,14 @@ db.execute(`
|
|
|
266
266
|
)
|
|
267
267
|
`);
|
|
268
268
|
|
|
269
|
-
db.execute(`
|
|
269
|
+
await db.execute(`
|
|
270
270
|
INSERT INTO products (name, price, tags, created_at) VALUES
|
|
271
271
|
('Laptop', 999.99, '["electronics", "computers"]', '2025-01-15 10:30:00');
|
|
272
272
|
INSERT INTO products (name, price, tags, created_at) VALUES
|
|
273
273
|
('Coffee', 4.50, '["food", "organic"]', '2025-01-16 08:00:00')
|
|
274
274
|
`);
|
|
275
275
|
|
|
276
|
-
const [{ rows }] = db.execute(`
|
|
276
|
+
const [{ rows }] = await db.execute(`
|
|
277
277
|
SELECT name, price FROM products
|
|
278
278
|
WHERE price > 10
|
|
279
279
|
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
|
}
|