@petradb/client 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 ADDED
@@ -0,0 +1,81 @@
1
+ Network client for [petradb-server](https://github.com/edadma/petradb) with the same `Session` API as `@petradb/engine`.
2
+
3
+ ## Installation
4
+
5
+ ```bash
6
+ npm install @petradb/client
7
+ ```
8
+
9
+ ## Quick Start
10
+
11
+ ```javascript
12
+ import { Session } from '@petradb/client';
13
+
14
+ const db = new Session({ host: 'localhost', port: 5432 });
15
+
16
+ await db.execute(`
17
+ CREATE TABLE users (
18
+ id SERIAL,
19
+ name TEXT NOT NULL,
20
+ email TEXT,
21
+ PRIMARY KEY (id)
22
+ )
23
+ `);
24
+
25
+ await db.execute("INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com')");
26
+
27
+ const [{ rows }] = await db.execute('SELECT * FROM users');
28
+ console.log(rows);
29
+ ```
30
+
31
+ ## Swapping Embedded and Network
32
+
33
+ Both `@petradb/engine` and `@petradb/client` export `Session` with the same `async execute()` API. Switch between them with a one-line import change:
34
+
35
+ ```javascript
36
+ // Embedded (in-process, no server needed)
37
+ import { Session } from '@petradb/engine';
38
+ const db = new Session();
39
+
40
+ // Network (talks to petradb-server over HTTP)
41
+ import { Session } from '@petradb/client';
42
+ const db = new Session({ host: 'localhost', port: 5432 });
43
+
44
+ // Everything below works identically
45
+ await db.execute('CREATE TABLE ...');
46
+ const [{ rows }] = await db.execute('SELECT * FROM ...');
47
+ ```
48
+
49
+ ## API
50
+
51
+ ### `new Session(options?)`
52
+
53
+ | Option | Type | Default | Description |
54
+ |--------|------|---------|-------------|
55
+ | `host` | `string` | `'localhost'` | Server hostname |
56
+ | `port` | `number` | `5432` | Server port |
57
+ | `rowMode` | `'object' \| 'array'` | `'object'` | Default row format for SELECT results |
58
+
59
+ ### `db.execute(sql, options?)`
60
+
61
+ Sends SQL to the server. Returns a promise that resolves to an array of results.
62
+
63
+ | Option | Type | Default | Description |
64
+ |--------|------|---------|-------------|
65
+ | `rowMode` | `'object' \| 'array'` | constructor default | Row format for this call |
66
+
67
+ ### `db.connect()`
68
+
69
+ Creates a server-side session. The session ID is sent automatically with subsequent `execute()` calls via the `X-Session-Id` header.
70
+
71
+ ### `db.close()`
72
+
73
+ Closes the server-side session.
74
+
75
+ ## No Dependencies
76
+
77
+ Uses the native `fetch` API (Node.js 18+, all modern browsers).
78
+
79
+ ## License
80
+
81
+ [ISC](https://opensource.org/licenses/ISC)
@@ -0,0 +1,25 @@
1
+ export interface SessionOptions {
2
+ host?: string;
3
+ port?: number;
4
+ rowMode?: "object" | "array";
5
+ }
6
+ export interface ExecuteOptions {
7
+ rowMode?: "object" | "array";
8
+ }
9
+ export interface FieldInfo {
10
+ name: string;
11
+ dataType: string;
12
+ }
13
+ export interface ExecuteResult {
14
+ command: string;
15
+ [key: string]: any;
16
+ }
17
+ export declare class Session {
18
+ private baseUrl;
19
+ private defaultRowMode;
20
+ private sessionId;
21
+ constructor(options?: SessionOptions);
22
+ execute(sql: string, options?: ExecuteOptions): Promise<ExecuteResult[]>;
23
+ connect(): Promise<void>;
24
+ close(): Promise<void>;
25
+ }
package/dist/index.js ADDED
@@ -0,0 +1,51 @@
1
+ export class Session {
2
+ constructor(options = {}) {
3
+ this.sessionId = null;
4
+ const host = options.host ?? "localhost";
5
+ const port = options.port ?? 5432;
6
+ this.baseUrl = `http://${host}:${port}`;
7
+ this.defaultRowMode = options.rowMode ?? "object";
8
+ }
9
+ async execute(sql, options) {
10
+ const rowMode = options?.rowMode ?? this.defaultRowMode;
11
+ const headers = {
12
+ "Content-Type": "application/json",
13
+ };
14
+ if (this.sessionId) {
15
+ headers["X-Session-Id"] = this.sessionId;
16
+ }
17
+ const res = await fetch(`${this.baseUrl}/sql`, {
18
+ method: "POST",
19
+ headers,
20
+ body: JSON.stringify({ sql, rowMode }),
21
+ });
22
+ if (!res.ok) {
23
+ const body = await res.text();
24
+ throw new Error(body || `HTTP ${res.status}`);
25
+ }
26
+ return (await res.json());
27
+ }
28
+ async connect() {
29
+ const res = await fetch(`${this.baseUrl}/session`, {
30
+ method: "POST",
31
+ });
32
+ if (!res.ok) {
33
+ const body = await res.text();
34
+ throw new Error(body || `HTTP ${res.status}`);
35
+ }
36
+ const data = (await res.json());
37
+ this.sessionId = data.sessionId;
38
+ }
39
+ async close() {
40
+ if (!this.sessionId)
41
+ return;
42
+ const res = await fetch(`${this.baseUrl}/session/${this.sessionId}`, {
43
+ method: "DELETE",
44
+ });
45
+ if (!res.ok) {
46
+ const body = await res.text();
47
+ throw new Error(body || `HTTP ${res.status}`);
48
+ }
49
+ this.sessionId = null;
50
+ }
51
+ }
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@petradb/client",
3
+ "version": "1.0.1",
4
+ "description": "Network client for petradb-server with the same Session API as @petradb/engine",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "default": "./dist/index.js"
10
+ }
11
+ },
12
+ "main": "dist/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "files": [
15
+ "dist",
16
+ "README.md"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc"
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/edadma/petradb.git"
24
+ },
25
+ "keywords": [
26
+ "SQL",
27
+ "database",
28
+ "client",
29
+ "petradb"
30
+ ],
31
+ "author": "Edward A. Maxedon, Sr.",
32
+ "license": "ISC",
33
+ "bugs": {
34
+ "url": "https://github.com/edadma/petradb/issues"
35
+ },
36
+ "homepage": "https://github.com/edadma/petradb#readme",
37
+ "devDependencies": {
38
+ "typescript": "^5.9.3"
39
+ }
40
+ }