sqlite-hub-client 0.2.0 → 0.3.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 +21 -156
- package/dist/adapters/http.d.ts +7 -3
- package/dist/adapters/http.js +2 -2
- package/dist/database.d.ts +1 -1
- package/dist/database.js +2 -2
- package/dist/index.d.ts +15 -6
- package/dist/index.js +13 -4
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,25 +1,29 @@
|
|
|
1
|
-
# sqlite-
|
|
1
|
+
# sqlite-hub-client
|
|
2
2
|
|
|
3
|
-
High-level TypeScript/JavaScript client for [sqlite-
|
|
3
|
+
High-level TypeScript/JavaScript client for [sqlite-hub](https://github.com/0xdps/sqlite-hub).
|
|
4
4
|
Comes with a full set of APIs for schema management, reads, writes — all using an **adapter** abstraction so the same code works over HTTP today and can talk to SQLite directly later.
|
|
5
5
|
|
|
6
6
|
## Install
|
|
7
7
|
|
|
8
8
|
```bash
|
|
9
|
-
npm install sqlite-
|
|
9
|
+
npm install sqlite-hub-client
|
|
10
10
|
```
|
|
11
11
|
|
|
12
12
|
## Quick start
|
|
13
13
|
|
|
14
14
|
```ts
|
|
15
|
-
import { connect } from "sqlite-
|
|
15
|
+
import { connect } from "sqlite-hub-client";
|
|
16
16
|
|
|
17
|
+
// Using a per-DB service secret (recommended for services)
|
|
17
18
|
const db = connect({
|
|
18
|
-
url: process.env.
|
|
19
|
-
token: process.env.
|
|
20
|
-
db: "my-service",
|
|
19
|
+
url: process.env.SQLITE_HUB_URL, // e.g. https://my-app.up.railway.app
|
|
20
|
+
token: process.env.SQLITE_HUB_SERVICE_SECRET, // service_secret generated at DB creation
|
|
21
|
+
db: "my-service", // database name
|
|
21
22
|
});
|
|
22
23
|
|
|
24
|
+
// Or use the global admin token (dev / admin tooling)
|
|
25
|
+
// token: process.env.SQLITE_HUB_ADMIN_TOKEN
|
|
26
|
+
|
|
23
27
|
// Create table
|
|
24
28
|
await db.createTable("users", [
|
|
25
29
|
{ name: "id", type: "INTEGER", primaryKey: true, autoIncrement: true },
|
|
@@ -90,14 +94,12 @@ const result = await db.exec("PRAGMA table_info(users)");
|
|
|
90
94
|
|
|
91
95
|
### `connect(options)` — create a database client
|
|
92
96
|
|
|
93
|
-
| Option | Type | Required | Description
|
|
94
|
-
| --------- | -------- | -------- |
|
|
95
|
-
| `url` | `string` | ✅ | Base URL of your sqlite-
|
|
96
|
-
| `token` | `string` | ✅ | `ADMIN_TOKEN`
|
|
97
|
-
| `db` | `string` | ✅ | Name of the database to operate on
|
|
98
|
-
| `timeout` | `number` | ❌ | Request timeout in ms (default: `10000`)
|
|
99
|
-
|
|
100
|
-
Returns a `Database` instance.
|
|
97
|
+
| Option | Type | Required | Description |
|
|
98
|
+
| --------- | -------- | -------- | ----------------------------------------------------------------------------------------------- |
|
|
99
|
+
| `url` | `string` | ✅ | Base URL of your sqlite-hub deployment |
|
|
100
|
+
| `token` | `string` | ✅ | Per-DB `service_secret` (recommended) or global `ADMIN_TOKEN` — sent as `Authorization: Bearer` |
|
|
101
|
+
| `db` | `string` | ✅ | Name of the database to operate on |
|
|
102
|
+
| `timeout` | `number` | ❌ | Request timeout in ms (default: `10000`) |
|
|
101
103
|
|
|
102
104
|
---
|
|
103
105
|
|
|
@@ -251,12 +253,12 @@ await db.exec("CREATE INDEX IF NOT EXISTS idx_title ON posts (title)");
|
|
|
251
253
|
## Architecture
|
|
252
254
|
|
|
253
255
|
```
|
|
254
|
-
sqlite-
|
|
256
|
+
sqlite-hub-client
|
|
255
257
|
├── index.ts ← connect() factory + all public exports
|
|
256
258
|
├── database.ts ← Database class — all high-level APIs
|
|
257
259
|
└── adapters/
|
|
258
260
|
├── types.ts ← IAdapter interface (exec only)
|
|
259
|
-
├── http.ts ← HttpAdapter (sqlite-
|
|
261
|
+
├── http.ts ← HttpAdapter (sqlite-hub over HTTP)
|
|
260
262
|
└── index.ts ← re-exports
|
|
261
263
|
```
|
|
262
264
|
|
|
@@ -264,8 +266,8 @@ Adding a direct SQLite adapter in the future is a one-liner:
|
|
|
264
266
|
|
|
265
267
|
```ts
|
|
266
268
|
// future
|
|
267
|
-
import { Database } from "sqlite-
|
|
268
|
-
import { DirectAdapter } from "sqlite-
|
|
269
|
+
import { Database } from "sqlite-hub-client";
|
|
270
|
+
import { DirectAdapter } from "sqlite-hub-client/adapters/direct"; // coming soon
|
|
269
271
|
|
|
270
272
|
const db = new Database(new DirectAdapter({ path: "./local.db" }));
|
|
271
273
|
// same API — createTable, find, insert, update, delete…
|
|
@@ -273,140 +275,3 @@ const db = new Database(new DirectAdapter({ path: "./local.db" }));
|
|
|
273
275
|
|
|
274
276
|
## License
|
|
275
277
|
|
|
276
|
-
MIT
|
|
277
|
-
|
|
278
|
-
## Install
|
|
279
|
-
|
|
280
|
-
```bash
|
|
281
|
-
npm install sqlite-db-hub-client
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
Or directly from GitHub (before the npm package is published):
|
|
285
|
-
|
|
286
|
-
```bash
|
|
287
|
-
npm install github:0xdps/sqlite-db-hub-client
|
|
288
|
-
```
|
|
289
|
-
|
|
290
|
-
## Quick start
|
|
291
|
-
|
|
292
|
-
```ts
|
|
293
|
-
import { createClient } from "sqlite-db-hub-client";
|
|
294
|
-
|
|
295
|
-
const db = createClient({
|
|
296
|
-
url: process.env.SQLITE_DB_HUB_URL, // e.g. https://my-app.up.railway.app
|
|
297
|
-
token: process.env.SQLITE_DB_HUB_TOKEN, // ADMIN_TOKEN set on the service
|
|
298
|
-
db: "my-service", // name of the database to use
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
// Create a table
|
|
302
|
-
await db.run(`
|
|
303
|
-
CREATE TABLE IF NOT EXISTS users (
|
|
304
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
305
|
-
email TEXT NOT NULL UNIQUE,
|
|
306
|
-
name TEXT
|
|
307
|
-
)
|
|
308
|
-
`);
|
|
309
|
-
|
|
310
|
-
// Insert a row
|
|
311
|
-
await db.run("INSERT INTO users (email, name) VALUES (?, ?)", [
|
|
312
|
-
"alice@example.com",
|
|
313
|
-
"Alice",
|
|
314
|
-
]);
|
|
315
|
-
|
|
316
|
-
// Query rows (typed)
|
|
317
|
-
const users = await db.query<{ id: number; email: string; name: string }>(
|
|
318
|
-
"SELECT * FROM users"
|
|
319
|
-
);
|
|
320
|
-
|
|
321
|
-
// Query a single row (or null)
|
|
322
|
-
const user = await db.queryOne<{ id: number; email: string }>(
|
|
323
|
-
"SELECT * FROM users WHERE email = ?",
|
|
324
|
-
["alice@example.com"]
|
|
325
|
-
);
|
|
326
|
-
```
|
|
327
|
-
|
|
328
|
-
## API
|
|
329
|
-
|
|
330
|
-
### `createClient(options)` / `new FileDbClient(options)`
|
|
331
|
-
|
|
332
|
-
| Option | Type | Required | Description |
|
|
333
|
-
| --------- | -------- | -------- | ----------------------------------------------------- |
|
|
334
|
-
| `url` | `string` | ✅ | Base URL of your sqlite-db-hub deployment |
|
|
335
|
-
| `token` | `string` | ✅ | `ADMIN_TOKEN` configured on the sqlite-db-hub service |
|
|
336
|
-
| `db` | `string` | ✅ | Name of the database to operate on |
|
|
337
|
-
| `timeout` | `number` | ❌ | Request timeout in ms (default: `10000`) |
|
|
338
|
-
|
|
339
|
-
---
|
|
340
|
-
|
|
341
|
-
### `db.exec(sql, bindings?)`
|
|
342
|
-
|
|
343
|
-
Run any SQL statement. Returns a `QueryResult` for SELECT, or an `ExecResult` for writes.
|
|
344
|
-
|
|
345
|
-
```ts
|
|
346
|
-
const result = await db.exec("SELECT count(*) as n FROM users");
|
|
347
|
-
// { headers: [...], rows: [{ n: 1 }], rowsRead: 1 }
|
|
348
|
-
```
|
|
349
|
-
|
|
350
|
-
---
|
|
351
|
-
|
|
352
|
-
### `db.query<T>(sql, bindings?)`
|
|
353
|
-
|
|
354
|
-
Run a SELECT and return typed rows.
|
|
355
|
-
|
|
356
|
-
```ts
|
|
357
|
-
const rows = await db.query<{ id: number; name: string }>(
|
|
358
|
-
"SELECT id, name FROM users WHERE id > ?",
|
|
359
|
-
[5]
|
|
360
|
-
);
|
|
361
|
-
```
|
|
362
|
-
|
|
363
|
-
---
|
|
364
|
-
|
|
365
|
-
### `db.queryOne<T>(sql, bindings?)`
|
|
366
|
-
|
|
367
|
-
Run a SELECT and return the first row, or `null` if no results.
|
|
368
|
-
|
|
369
|
-
```ts
|
|
370
|
-
const row = await db.queryOne<{ name: string }>(
|
|
371
|
-
"SELECT name FROM users WHERE id = ?",
|
|
372
|
-
[1]
|
|
373
|
-
);
|
|
374
|
-
```
|
|
375
|
-
|
|
376
|
-
---
|
|
377
|
-
|
|
378
|
-
### `db.run(sql, bindings?)`
|
|
379
|
-
|
|
380
|
-
Run a write statement (INSERT, UPDATE, DELETE, CREATE, ALTER, DROP). Returns `{ rowsAffected, lastInsertRowid }`.
|
|
381
|
-
|
|
382
|
-
```ts
|
|
383
|
-
const { rowsAffected, lastInsertRowid } = await db.run(
|
|
384
|
-
"INSERT INTO jobs (payload) VALUES (?)",
|
|
385
|
-
[JSON.stringify({ task: "send-email" })]
|
|
386
|
-
);
|
|
387
|
-
```
|
|
388
|
-
|
|
389
|
-
## Types
|
|
390
|
-
|
|
391
|
-
```ts
|
|
392
|
-
interface QueryResult<T> {
|
|
393
|
-
headers: ColumnHeader[];
|
|
394
|
-
rows: T[];
|
|
395
|
-
rowsRead: number;
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
interface ExecResult {
|
|
399
|
-
rowsAffected: number;
|
|
400
|
-
lastInsertRowid: number | null;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
interface ColumnHeader {
|
|
404
|
-
name: string;
|
|
405
|
-
displayName: string;
|
|
406
|
-
originalType: string | null;
|
|
407
|
-
}
|
|
408
|
-
```
|
|
409
|
-
|
|
410
|
-
## License
|
|
411
|
-
|
|
412
|
-
MIT
|
package/dist/adapters/http.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import type { IAdapter, RawResult } from "./types.js";
|
|
2
2
|
export interface HttpAdapterOptions {
|
|
3
|
-
/** Base URL of the sqlite-
|
|
3
|
+
/** Base URL of the sqlite-hub deployment, e.g. https://my-app.up.railway.app */
|
|
4
4
|
url: string;
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* Bearer token for this database.
|
|
7
|
+
* Use the per-DB `service_secret` (recommended for services) or the
|
|
8
|
+
* global `ADMIN_TOKEN` (for admin-level access or dev environments).
|
|
9
|
+
*/
|
|
6
10
|
token: string;
|
|
7
11
|
/** Name of the database to operate on */
|
|
8
12
|
db: string;
|
|
@@ -10,7 +14,7 @@ export interface HttpAdapterOptions {
|
|
|
10
14
|
timeout?: number;
|
|
11
15
|
}
|
|
12
16
|
/**
|
|
13
|
-
* Adapter that executes SQL via the sqlite-
|
|
17
|
+
* Adapter that executes SQL via the sqlite-hub HTTP API
|
|
14
18
|
* (POST /api/db/:name/exec).
|
|
15
19
|
*/
|
|
16
20
|
export declare class HttpAdapter implements IAdapter {
|
package/dist/adapters/http.js
CHANGED
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.HttpAdapter = void 0;
|
|
7
7
|
const fetch_1 = __importDefault(require("@pingpong-js/fetch"));
|
|
8
8
|
/**
|
|
9
|
-
* Adapter that executes SQL via the sqlite-
|
|
9
|
+
* Adapter that executes SQL via the sqlite-hub HTTP API
|
|
10
10
|
* (POST /api/db/:name/exec).
|
|
11
11
|
*/
|
|
12
12
|
class HttpAdapter {
|
|
@@ -30,7 +30,7 @@ class HttpAdapter {
|
|
|
30
30
|
if (res.isError()) {
|
|
31
31
|
const body = res.data;
|
|
32
32
|
throw new Error(body?.error ??
|
|
33
|
-
`sqlite-
|
|
33
|
+
`sqlite-hub: HTTP ${res.status} for db "${this.options.db}"`);
|
|
34
34
|
}
|
|
35
35
|
return res.data;
|
|
36
36
|
}
|
package/dist/database.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ExecResult, IAdapter, RawResult } from "./adapters/types.js";
|
|
2
2
|
export type WhereClause = Record<string, unknown>;
|
|
3
3
|
export type OrderDirection = "ASC" | "DESC";
|
|
4
4
|
export interface FindOptions {
|
package/dist/database.js
CHANGED
|
@@ -217,14 +217,14 @@ class Database {
|
|
|
217
217
|
async _read(sql, bindings) {
|
|
218
218
|
const result = await this.adapter.exec(sql, bindings);
|
|
219
219
|
if (!isQueryResult(result)) {
|
|
220
|
-
throw new Error(`sqlite-
|
|
220
|
+
throw new Error(`sqlite-hub: expected SELECT, got write result for: ${sql}`);
|
|
221
221
|
}
|
|
222
222
|
return result.rows;
|
|
223
223
|
}
|
|
224
224
|
async _write(sql, bindings) {
|
|
225
225
|
const result = await this.adapter.exec(sql, bindings);
|
|
226
226
|
if (!isExecResult(result)) {
|
|
227
|
-
throw new Error(`sqlite-
|
|
227
|
+
throw new Error(`sqlite-hub: expected write result, got SELECT for: ${sql}`);
|
|
228
228
|
}
|
|
229
229
|
return result;
|
|
230
230
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
export { Database } from "./database.js";
|
|
2
|
-
export type {
|
|
2
|
+
export type { ColumnDef, CreateIndexOptions, CreateTableOptions, FindOptions, OrderDirection, WhereClause } from "./database.js";
|
|
3
3
|
export { HttpAdapter } from "./adapters/http.js";
|
|
4
4
|
export type { HttpAdapterOptions } from "./adapters/http.js";
|
|
5
|
-
export type {
|
|
6
|
-
import { Database } from "./database.js";
|
|
5
|
+
export type { ColumnHeader, ExecResult, IAdapter, QueryResult, RawResult } from "./adapters/types.js";
|
|
7
6
|
import { type HttpAdapterOptions } from "./adapters/http.js";
|
|
7
|
+
import { Database } from "./database.js";
|
|
8
8
|
/**
|
|
9
|
-
* Create a Database connected to a sqlite-
|
|
9
|
+
* Create a Database connected to a sqlite-hub service over HTTP.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // Using a per-DB service secret (recommended for services)
|
|
13
|
+
* const db = connect({
|
|
14
|
+
* url: process.env.SQLITE_HUB_URL,
|
|
15
|
+
* token: process.env.SQLITE_HUB_SERVICE_SECRET,
|
|
16
|
+
* db: "my-service",
|
|
17
|
+
* });
|
|
10
18
|
*
|
|
11
19
|
* @example
|
|
20
|
+
* // Using the global admin token (dev / admin tooling)
|
|
12
21
|
* const db = connect({
|
|
13
|
-
* url: process.env.
|
|
14
|
-
* token: process.env.
|
|
22
|
+
* url: process.env.SQLITE_HUB_URL,
|
|
23
|
+
* token: process.env.SQLITE_HUB_ADMIN_TOKEN,
|
|
15
24
|
* db: "my-service",
|
|
16
25
|
* });
|
|
17
26
|
*/
|
package/dist/index.js
CHANGED
|
@@ -7,15 +7,24 @@ Object.defineProperty(exports, "Database", { enumerable: true, get: function ()
|
|
|
7
7
|
var http_js_1 = require("./adapters/http.js");
|
|
8
8
|
Object.defineProperty(exports, "HttpAdapter", { enumerable: true, get: function () { return http_js_1.HttpAdapter; } });
|
|
9
9
|
// ── Convenience factory ──────────────────────────────────────────────────────
|
|
10
|
-
const database_js_2 = require("./database.js");
|
|
11
10
|
const http_js_2 = require("./adapters/http.js");
|
|
11
|
+
const database_js_2 = require("./database.js");
|
|
12
12
|
/**
|
|
13
|
-
* Create a Database connected to a sqlite-
|
|
13
|
+
* Create a Database connected to a sqlite-hub service over HTTP.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Using a per-DB service secret (recommended for services)
|
|
17
|
+
* const db = connect({
|
|
18
|
+
* url: process.env.SQLITE_HUB_URL,
|
|
19
|
+
* token: process.env.SQLITE_HUB_SERVICE_SECRET,
|
|
20
|
+
* db: "my-service",
|
|
21
|
+
* });
|
|
14
22
|
*
|
|
15
23
|
* @example
|
|
24
|
+
* // Using the global admin token (dev / admin tooling)
|
|
16
25
|
* const db = connect({
|
|
17
|
-
* url: process.env.
|
|
18
|
-
* token: process.env.
|
|
26
|
+
* url: process.env.SQLITE_HUB_URL,
|
|
27
|
+
* token: process.env.SQLITE_HUB_ADMIN_TOKEN,
|
|
19
28
|
* db: "my-service",
|
|
20
29
|
* });
|
|
21
30
|
*/
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sqlite-hub-client",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "High-level SQLite client for sqlite-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "High-level SQLite client for sqlite-hub — HTTP adapter included, direct SQLite coming soon",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"files": ["dist", "README.md"],
|
|
8
8
|
"scripts": {
|
|
9
9
|
"build": "tsc"
|
|
10
10
|
},
|
|
11
|
-
"keywords": ["sqlite", "sqlite-
|
|
11
|
+
"keywords": ["sqlite", "sqlite-hub", "railway", "orm", "database"],
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@pingpong-js/fetch": "^1.0.2"
|