@noego/sqlego 0.0.0-stage → 0.0.1-dev.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 +81 -2
- package/bin/sqlego +4 -0
- package/dist/client.d.ts +75 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +751 -0
- package/dist/paths.d.ts +70 -0
- package/dist/protocol.d.ts +218 -0
- package/dist/version.d.ts +23 -0
- package/package.json +43 -4
- package/src/cli.ts +310 -0
- package/src/client.ts +497 -0
- package/src/index.ts +15 -0
- package/src/paths.ts +154 -0
- package/src/protocol.ts +370 -0
- package/src/server.ts +413 -0
- package/src/version.ts +78 -0
package/README.md
CHANGED
|
@@ -1,3 +1,82 @@
|
|
|
1
|
-
#
|
|
1
|
+
# sqlego
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
One owner process per SQLite file. Every client — the `sqlego` CLI, the Node
|
|
4
|
+
library, a Bun script — passes SQL over a local Unix socket to the one process
|
|
5
|
+
holding the file. The server boots on first connect and exits when idle.
|
|
6
|
+
|
|
7
|
+
Why: SQLite's write lock is per-process. Two processes (an app, a log host, a
|
|
8
|
+
retention job, a debugging CLI) writing the same file contend, time out, and
|
|
9
|
+
freeze each other. With a single owner there is nothing to contend: statements
|
|
10
|
+
from all clients run in arrival order on one connection, and a reader from any
|
|
11
|
+
process just asks the owner.
|
|
12
|
+
|
|
13
|
+
## Pieces
|
|
14
|
+
|
|
15
|
+
| Piece | Runtime | Role |
|
|
16
|
+
| --- | --- | --- |
|
|
17
|
+
| `bin/sqlego` | Bun, compiled single binary | CLI **and** server (`sqlego serve <file>` is what clients spawn) |
|
|
18
|
+
| `@noego/sqlego` (`src/client.ts`) | Node or Bun; `node:net` only | Protocol-only client — no SQLite dependency, no native addon |
|
|
19
|
+
| `src/server.ts` | Bun (`bun:sqlite`) | The owner: one `Database`, one Unix socket, idle exit |
|
|
20
|
+
|
|
21
|
+
## Lifecycle
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
client: connect(socket) ──ok──▶ hello ──▶ requests
|
|
25
|
+
│ refused/absent
|
|
26
|
+
▼
|
|
27
|
+
claim <key>.lock (O_EXCL; stale lock of a dead pid is reclaimed)
|
|
28
|
+
│ won │ lost
|
|
29
|
+
▼ ▼
|
|
30
|
+
spawn `sqlego serve <file>` wait for socket to appear
|
|
31
|
+
└──────────────▶ connect ◀──────────────┘
|
|
32
|
+
|
|
33
|
+
server: listen ▶ write <key>.json ▶ release lock ▶ serve
|
|
34
|
+
last client leaves ▶ idle timer (default 30 s) ▶ rm socket+meta ▶ close db ▶ exit 0
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Socket, lock, and meta live in `$SQLEGO_RUNTIME_DIR` (default `<tmpdir>/sqlego/`)
|
|
38
|
+
keyed by a hash of the canonical file path — never beside the database, because
|
|
39
|
+
Unix socket paths cap at 104 bytes.
|
|
40
|
+
|
|
41
|
+
A client whose version differs from the server's asks it to stop and spawns a
|
|
42
|
+
fresh one, so a rebuilt binary never keeps talking to an old owner.
|
|
43
|
+
|
|
44
|
+
## Protocol
|
|
45
|
+
|
|
46
|
+
Newline-delimited JSON, correlated by `id`. Ops: `hello`, `query` (rows),
|
|
47
|
+
`run` (changes), `batch` (all statements in one transaction, rolled back on
|
|
48
|
+
the first failure), `status`, `stop`. There is no `BEGIN` over the wire on
|
|
49
|
+
purpose: a client-held transaction would hold the only writer hostage to that
|
|
50
|
+
client's liveness. `batch` is the transactional unit.
|
|
51
|
+
|
|
52
|
+
## CLI
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
sqlego <file> "SELECT …" [--params '[1,"a"]'] [--json|--csv]
|
|
56
|
+
sqlego <file> "DELETE …"
|
|
57
|
+
sqlego <file> status | stop
|
|
58
|
+
sqlego <file> watch "SELECT count(*) FROM logs" --every 2s
|
|
59
|
+
sqlego ls
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Library
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { SqlegoClient } from '@noego/sqlego';
|
|
66
|
+
const db = await SqlegoClient.connect('/path/to/logs.db');
|
|
67
|
+
await db.run('INSERT INTO logs (…) VALUES (?, ?)', [a, b]);
|
|
68
|
+
const rows = await db.query('SELECT … WHERE occurred_at_ms < ?', [cutoff]);
|
|
69
|
+
await db.batch([{ sql: 'DELETE … LIMIT 5000' }, { sql: 'PRAGMA incremental_vacuum(256)' }]);
|
|
70
|
+
await db.close();
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Server location: `SQLEGO_BIN`, else the compiled `bin/sqlego` next to the
|
|
74
|
+
package, else `sqlego` on `PATH`; pass `serverCommand` to override.
|
|
75
|
+
|
|
76
|
+
## Develop
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
bun install
|
|
80
|
+
bun test
|
|
81
|
+
bun run build # → bin/sqlego (single executable)
|
|
82
|
+
```
|
package/bin/sqlego
ADDED
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { type BatchResult, type HelloResult, type RunResult, type SqlParams, type SqlRow, type Statement, type StatusResult } from './protocol.ts';
|
|
2
|
+
import { type FilePaths } from './paths.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Runs in Node and Bun alike (node:net, node:child_process only; no SQLite
|
|
5
|
+
* dependency). Connects to the file's owner, spawning one when none is
|
|
6
|
+
* listening. The sequence is always: connect → else claim lock → spawn → wait
|
|
7
|
+
* for the socket → connect. A claimer that loses the lock waits for the
|
|
8
|
+
* winner's socket instead.
|
|
9
|
+
*/
|
|
10
|
+
export type ClientOptions = Readonly<{
|
|
11
|
+
/** Spawn a server when none is listening. Default true. */
|
|
12
|
+
spawn?: boolean;
|
|
13
|
+
/** How the server is started when needed; see `serverCommand`. */
|
|
14
|
+
serverCommand?: readonly string[];
|
|
15
|
+
/** Idle timeout passed to a spawned server. Default 30 000. */
|
|
16
|
+
idleTimeoutMs?: number;
|
|
17
|
+
/** Pragmas a spawned server applies on open (`--pragma`), replacing its defaults. Ignored when a server is already running. */
|
|
18
|
+
serverPragmas?: readonly string[];
|
|
19
|
+
/** Total time to obtain a connection, including spawn. Default 10 000. */
|
|
20
|
+
connectTimeoutMs?: number;
|
|
21
|
+
/** Per-request timeout. Default 60 000. */
|
|
22
|
+
requestTimeoutMs?: number;
|
|
23
|
+
}>;
|
|
24
|
+
export declare class SqlegoClientError extends Error {
|
|
25
|
+
readonly code: string;
|
|
26
|
+
constructor(code: string, message: string);
|
|
27
|
+
}
|
|
28
|
+
export declare class SqlegoClient {
|
|
29
|
+
readonly paths: FilePaths;
|
|
30
|
+
private socket;
|
|
31
|
+
private buffer;
|
|
32
|
+
private nextId;
|
|
33
|
+
private readonly pending;
|
|
34
|
+
private hello;
|
|
35
|
+
/** Why the server this client spawned did not come up, if it did not. */
|
|
36
|
+
private spawnFailure;
|
|
37
|
+
private readonly options;
|
|
38
|
+
private constructor();
|
|
39
|
+
static connect(file: string, options?: ClientOptions): Promise<SqlegoClient>;
|
|
40
|
+
get server(): HelloResult;
|
|
41
|
+
query<Row extends SqlRow = SqlRow>(sql: string, params?: SqlParams): Promise<Row[]>;
|
|
42
|
+
run(sql: string, params?: SqlParams): Promise<RunResult>;
|
|
43
|
+
/** One transaction; the whole batch rolls back on the first failure. */
|
|
44
|
+
batch(statements: readonly Statement[]): Promise<BatchResult['results']>;
|
|
45
|
+
status(): Promise<StatusResult>;
|
|
46
|
+
/** Asks the server to shut down; the connection closes as a consequence. */
|
|
47
|
+
stop(): Promise<void>;
|
|
48
|
+
close(): Promise<void>;
|
|
49
|
+
private stopServer;
|
|
50
|
+
private open;
|
|
51
|
+
private connectOrSpawn;
|
|
52
|
+
/** O_EXCL create; a lock left by a dead process (or unreadable) is reclaimed. */
|
|
53
|
+
private claimLock;
|
|
54
|
+
private spawnServer;
|
|
55
|
+
/** The spawn never started: record why, and release the lock so another claimer can try. */
|
|
56
|
+
private onSpawnError;
|
|
57
|
+
private attach;
|
|
58
|
+
private onClose;
|
|
59
|
+
private onData;
|
|
60
|
+
/**
|
|
61
|
+
* One request whose reply `parse` turns from `unknown` into its typed
|
|
62
|
+
* result; a reply that does not parse is a `protocol` failure.
|
|
63
|
+
*/
|
|
64
|
+
private call;
|
|
65
|
+
/** Never rejects: every outcome, including transport failures, is a `Result`. The reply is `unknown` until parsed. */
|
|
66
|
+
private request;
|
|
67
|
+
private settleAll;
|
|
68
|
+
private waitForExit;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* How to start a server: SQLEGO_BIN if set; the compiled `sqlego` binary when
|
|
72
|
+
* running as one; the dev entry (`bun src/cli.ts`) when running from source
|
|
73
|
+
* under Bun; else `sqlego` on PATH.
|
|
74
|
+
*/
|
|
75
|
+
export declare function defaultServerCommand(): readonly string[];
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { SqlegoClient, SqlegoClientError, defaultServerCommand, type ClientOptions } from './client.ts';
|
|
2
|
+
export { pathsFor, runtimeDirectory, canonicalFile, type FilePaths, type ServerMeta } from './paths.ts';
|
|
3
|
+
export { PROTOCOL_VERSION, type SqlParams, type SqlRow, type SqlValue, type Statement, type RunResult, type QueryResult, type BatchResult, type StatusResult, type HelloResult, } from './protocol.ts';
|
|
4
|
+
export { VERSION } from './version.ts';
|