@alineo-labs/sqlite 0.2.3 → 0.3.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/dist/index.d.mts +90 -2
- package/dist/index.mjs +185 -21
- package/package.json +3 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import { CheckpointInfo, EngineLedgerEntry, EngineLedgerRow, EnvironmentRecord, IEngineLedger, IStorageAdapter, LedgerEntry, ListSandboxOptions, SandboxDetails } from "@alineo-labs/core";
|
|
2
3
|
//#region src/adapter.d.ts
|
|
3
4
|
export declare class SQLiteAdapter implements IStorageAdapter {
|
|
4
5
|
private readonly db;
|
|
@@ -18,4 +19,91 @@ export declare class SQLiteAdapter implements IStorageAdapter {
|
|
|
18
19
|
deleteEnvironment(name: string): Promise<void>;
|
|
19
20
|
listEnvironments(): Promise<EnvironmentRecord[]>;
|
|
20
21
|
}
|
|
21
|
-
//#endregion
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/engine-ledger.d.ts
|
|
24
|
+
/**
|
|
25
|
+
* Column names to bind this ledger onto. Defaults to its own `engine_ledger` table — but a
|
|
26
|
+
* consumer with a pre-existing, differently-named ledger table (alineod's `ledger`, with
|
|
27
|
+
* `run_id`/`agent_id` instead of `scope`/`sub_scope`) can point this at its *own* table and
|
|
28
|
+
* columns instead, so the physical schema never has to change. `seq`/`ts`/`event`/`payload`
|
|
29
|
+
* are not configurable — every known consumer's schema already spells them the same way.
|
|
30
|
+
*/
|
|
31
|
+
interface SQLiteEngineLedgerSchema {
|
|
32
|
+
table: string;
|
|
33
|
+
scopeColumn: string;
|
|
34
|
+
subScopeColumn: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A row with `payload` left as the raw stored string, never `JSON.parse`d. Exists because
|
|
38
|
+
* `rowToEntry`'s eager parse turns one historically-malformed row (bad JSON written before a
|
|
39
|
+
* validation bug was fixed, or written directly for a test) into a hard failure for every
|
|
40
|
+
* other row in the same read — the caller never gets a chance to catch and skip just that one.
|
|
41
|
+
* alineod's `db.ts` depends on exactly this: a damaged `ledger` row must not break reading the
|
|
42
|
+
* rest of a run's events (`apps/alineod/test/transcript.test.ts`'s "skips a damaged row").
|
|
43
|
+
*/
|
|
44
|
+
interface RawEngineLedgerRow {
|
|
45
|
+
seq: number;
|
|
46
|
+
scope: string;
|
|
47
|
+
subScope: string | null;
|
|
48
|
+
ts: number;
|
|
49
|
+
event: string;
|
|
50
|
+
payload: string | null;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* `IEngineLedger` on `bun:sqlite` — durability-roadmap.md M3 (3.1b). alineod's own
|
|
54
|
+
* `apps/alineod/src/state/db.ts` is this interface's first non-sandbox-SDK consumer: it binds
|
|
55
|
+
* this onto its *existing* `ledger` table (`run_id`/`agent_id`, via `schema`) rather than
|
|
56
|
+
* migrating onto a renamed one, since 11 other files across `apps/alineod` (migrations, ops
|
|
57
|
+
* checks, several tests) already address that table and its columns directly in raw SQL —
|
|
58
|
+
* renaming it would ripple into all of them for no behavioral gain. What alineod actually
|
|
59
|
+
* gets from adopting this: the same query/index shape as the sandbox SDK's own ledger, from
|
|
60
|
+
* one shared, tested implementation, instead of two independently hand-rolled ones that can
|
|
61
|
+
* silently drift (e.g. the composite-index fix in `adapter.ts` otherwise has no reason to ever
|
|
62
|
+
* get applied to alineod's copy).
|
|
63
|
+
*/
|
|
64
|
+
export declare class SQLiteEngineLedger implements IEngineLedger {
|
|
65
|
+
private readonly db;
|
|
66
|
+
private readonly ownsConnection;
|
|
67
|
+
private readonly insert;
|
|
68
|
+
private readonly selectByScope;
|
|
69
|
+
private readonly selectByScopeAfter;
|
|
70
|
+
private readonly selectBySubScope;
|
|
71
|
+
private readonly selectBySubScopeAndEvent;
|
|
72
|
+
private readonly selectAll;
|
|
73
|
+
/**
|
|
74
|
+
* `pathOrDb` accepts either a file path (opens its own connection — a standalone consumer
|
|
75
|
+
* like `apps/sandbox/server/registry.ts`) or an already-open `Database` (a consumer that,
|
|
76
|
+
* like alineod, already manages one shared connection across several tables and must not
|
|
77
|
+
* open a second one to the same file). Pragmas only run for a path, not a passed-in
|
|
78
|
+
* connection — the caller already owns those for their connection.
|
|
79
|
+
*
|
|
80
|
+
* `schema` lets a consumer with a pre-existing, differently-shaped ledger table bind onto
|
|
81
|
+
* its own table/columns instead of this class's default `engine_ledger` one — see
|
|
82
|
+
* `SQLiteEngineLedgerSchema`. When a custom `schema` is given, this class never runs
|
|
83
|
+
* `ENGINE_LEDGER_MIGRATION_SQL`: the caller already owns that table's DDL (its indexes,
|
|
84
|
+
* its migrations), and creating/indexing it here too would duplicate or conflict with that.
|
|
85
|
+
*/
|
|
86
|
+
constructor(pathOrDb: string | Database, schema?: SQLiteEngineLedgerSchema);
|
|
87
|
+
connect(): Promise<void>;
|
|
88
|
+
/** A no-op when constructed from a shared connection — the owner closes it, not this. */
|
|
89
|
+
close(): Promise<void>;
|
|
90
|
+
append(entry: EngineLedgerEntry): Promise<EngineLedgerRow>;
|
|
91
|
+
readByScope(scope: string, opts?: {
|
|
92
|
+
afterSeq?: number;
|
|
93
|
+
}): Promise<EngineLedgerRow[]>;
|
|
94
|
+
readBySubScope(subScope: string, event?: string): Promise<EngineLedgerRow[]>;
|
|
95
|
+
readAll(): Promise<EngineLedgerRow[]>;
|
|
96
|
+
appendSync(entry: EngineLedgerEntry): EngineLedgerRow;
|
|
97
|
+
readByScopeSync(scope: string, opts?: {
|
|
98
|
+
afterSeq?: number;
|
|
99
|
+
}): EngineLedgerRow[];
|
|
100
|
+
readBySubScopeSync(subScope: string, event?: string): EngineLedgerRow[];
|
|
101
|
+
readAllSync(): EngineLedgerRow[];
|
|
102
|
+
readByScopeRawSync(scope: string, opts?: {
|
|
103
|
+
afterSeq?: number;
|
|
104
|
+
}): RawEngineLedgerRow[];
|
|
105
|
+
readBySubScopeRawSync(subScope: string, event?: string): RawEngineLedgerRow[];
|
|
106
|
+
readAllRawSync(): RawEngineLedgerRow[];
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
109
|
+
export type { RawEngineLedgerRow, SQLiteEngineLedgerSchema };
|
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import { Database } from "bun:sqlite";
|
|
2
2
|
import { mkdirSync } from "node:fs";
|
|
3
3
|
import { dirname } from "node:path";
|
|
4
|
-
import { SandboxStatus } from "@alineo-labs/core";
|
|
4
|
+
import { DEFAULT_PROVIDER_ID, SandboxStatus } from "@alineo-labs/core";
|
|
5
|
+
import { renameEventsStatement } from "@alineo-labs/ledger";
|
|
5
6
|
//#region src/migrations.ts
|
|
6
7
|
const MIGRATION_SQL = `
|
|
7
8
|
CREATE TABLE IF NOT EXISTS alineo_events (
|
|
8
|
-
|
|
9
|
+
-- No AUTOINCREMENT: this is an append-only log, so rowid reuse after a delete is not a
|
|
10
|
+
-- concern worth paying a sqlite_sequence write on every insert for (foundation-packages.md
|
|
11
|
+
-- Phase 3). Only affects freshly-created databases -- SQLite can't retroactively strip
|
|
12
|
+
-- AUTOINCREMENT from an existing table's schema, and that's a one-time insert-cost
|
|
13
|
+
-- difference, not a correctness issue, so an existing database is left as-is.
|
|
14
|
+
id INTEGER PRIMARY KEY,
|
|
9
15
|
sandbox_id TEXT NOT NULL,
|
|
10
16
|
name TEXT NOT NULL,
|
|
11
17
|
step_idx INTEGER NOT NULL,
|
|
@@ -16,8 +22,17 @@ CREATE TABLE IF NOT EXISTS alineo_events (
|
|
|
16
22
|
ts INTEGER NOT NULL
|
|
17
23
|
);
|
|
18
24
|
|
|
19
|
-
|
|
20
|
-
|
|
25
|
+
-- Replaces the two single-column indexes below (dropped in adapter.ts's connect(), for
|
|
26
|
+
-- existing databases) -- every hot read filters on (name, sandbox_id) and orders by ts, which
|
|
27
|
+
-- this one composite index now covers end to end instead of SQLite picking one single-column
|
|
28
|
+
-- index, filtering the rest, then sorting ts separately. 'id' is a tie-breaker only (two
|
|
29
|
+
-- entries can share a ts) -- the primary sort key stays 'ts', not 'id': callers can append
|
|
30
|
+
-- entries with an out-of-order ts (see adapter.test.ts's "returns entries in ascending
|
|
31
|
+
-- timestamp order", which deliberately appends ts 2000 then ts 1000 and expects ts order back),
|
|
32
|
+
-- so 'id' is not a safe substitute for 'ts' the way foundation-packages.md's original proposal
|
|
33
|
+
-- assumed -- insertion order and timestamp order are not guaranteed to agree.
|
|
34
|
+
CREATE INDEX IF NOT EXISTS alineo_events_name_sandbox_ts
|
|
35
|
+
ON alineo_events(name, sandbox_id, ts, id);
|
|
21
36
|
|
|
22
37
|
CREATE TABLE IF NOT EXISTS alineo_environments (
|
|
23
38
|
name TEXT PRIMARY KEY,
|
|
@@ -33,14 +48,15 @@ const AGG_SQL = (whereClause) => `
|
|
|
33
48
|
SELECT
|
|
34
49
|
name,
|
|
35
50
|
sandbox_id,
|
|
36
|
-
MIN(CASE WHEN event = '
|
|
37
|
-
MAX(CASE WHEN event = '
|
|
38
|
-
MAX(CASE WHEN event = '
|
|
39
|
-
CAST(COUNT(CASE WHEN event = '
|
|
40
|
-
MAX(CASE WHEN event = '
|
|
41
|
-
MAX(CASE WHEN event = '
|
|
42
|
-
MAX(CASE WHEN event = '
|
|
43
|
-
MAX(CASE WHEN event = '
|
|
51
|
+
MIN(CASE WHEN event = 'sandbox.created' THEN ts END) AS started_at,
|
|
52
|
+
MAX(CASE WHEN event = 'sandbox.closed' THEN ts END) AS completed_at,
|
|
53
|
+
MAX(CASE WHEN event = 'sandbox.closed' THEN 1 ELSE 0 END) AS is_closed,
|
|
54
|
+
CAST(COUNT(CASE WHEN event = 'exec.completed' THEN 1 END) AS INTEGER) AS exec_count,
|
|
55
|
+
MAX(CASE WHEN event = 'sandbox.created' THEN json_extract(payload, '$.runId') END) AS run_id,
|
|
56
|
+
MAX(CASE WHEN event = 'sandbox.created' THEN json_extract(payload, '$.resourceId') END) AS resource_id,
|
|
57
|
+
MAX(CASE WHEN event = 'sandbox.created' THEN json_extract(payload, '$.teamId') END) AS team_id,
|
|
58
|
+
MAX(CASE WHEN event = 'sandbox.created' THEN json_extract(payload, '$.parentSandboxId') END) AS parent_sandbox_id,
|
|
59
|
+
MAX(CASE WHEN event = 'sandbox.created' THEN json_extract(payload, '$.provider') END) AS provider
|
|
44
60
|
FROM alineo_events
|
|
45
61
|
${whereClause}
|
|
46
62
|
GROUP BY name, sandbox_id
|
|
@@ -59,7 +75,8 @@ function aggRowToDetails(row) {
|
|
|
59
75
|
runId: row.run_id ?? row.sandbox_id,
|
|
60
76
|
resourceId: row.resource_id ?? void 0,
|
|
61
77
|
teamId: row.team_id ?? void 0,
|
|
62
|
-
parentSandboxId: row.parent_sandbox_id ?? void 0
|
|
78
|
+
parentSandboxId: row.parent_sandbox_id ?? void 0,
|
|
79
|
+
provider: row.provider ?? DEFAULT_PROVIDER_ID
|
|
63
80
|
};
|
|
64
81
|
}
|
|
65
82
|
function applyOpts(details, opts) {
|
|
@@ -72,10 +89,11 @@ function applyOpts(details, opts) {
|
|
|
72
89
|
if (opts?.runId != null) result = result.filter((d) => d.runId === opts.runId);
|
|
73
90
|
if (opts?.resourceId != null) result = result.filter((d) => d.resourceId === opts.resourceId);
|
|
74
91
|
if (opts?.teamId != null) result = result.filter((d) => d.teamId === opts.teamId);
|
|
92
|
+
if (opts?.provider != null) result = result.filter((d) => d.provider === opts.provider);
|
|
75
93
|
if (opts?.limit != null) result = result.slice(0, opts.limit);
|
|
76
94
|
return result;
|
|
77
95
|
}
|
|
78
|
-
function rowToEntry(row) {
|
|
96
|
+
function rowToEntry$1(row) {
|
|
79
97
|
return {
|
|
80
98
|
sandboxId: row.sandbox_id,
|
|
81
99
|
name: row.name,
|
|
@@ -98,8 +116,13 @@ var SQLiteAdapter = class {
|
|
|
98
116
|
this.db = new Database(path, { create: true });
|
|
99
117
|
}
|
|
100
118
|
async connect() {
|
|
119
|
+
this.db.run("DROP INDEX IF EXISTS alineo_events_sandbox_id;");
|
|
120
|
+
this.db.run("DROP INDEX IF EXISTS alineo_events_name;");
|
|
101
121
|
this.db.run(MIGRATION_SQL);
|
|
122
|
+
const rename = renameEventsStatement();
|
|
123
|
+
this.db.run(rename.sql, rename.params);
|
|
102
124
|
this.db.run("PRAGMA journal_mode = WAL;");
|
|
125
|
+
this.db.run("PRAGMA synchronous = FULL;");
|
|
103
126
|
}
|
|
104
127
|
async close() {
|
|
105
128
|
this.db.close();
|
|
@@ -112,15 +135,15 @@ var SQLiteAdapter = class {
|
|
|
112
135
|
return this.db.prepare(`SELECT sandbox_id, name, step_idx, branch, event, payload, error, ts
|
|
113
136
|
FROM alineo_events
|
|
114
137
|
WHERE name = ? AND sandbox_id = ?
|
|
115
|
-
ORDER BY ts ASC`).all(name, sandboxId).map(rowToEntry);
|
|
138
|
+
ORDER BY ts ASC, id ASC`).all(name, sandboxId).map(rowToEntry$1);
|
|
116
139
|
}
|
|
117
140
|
async lastCheckpoint(name, sandboxId) {
|
|
118
141
|
const row = this.db.prepare(`SELECT sandbox_id, name, step_idx, branch, event, payload, error, ts
|
|
119
142
|
FROM alineo_events
|
|
120
|
-
WHERE name = ? AND sandbox_id = ? AND event = 'checkpoint_created'
|
|
121
|
-
ORDER BY ts DESC
|
|
143
|
+
WHERE name = ? AND sandbox_id = ? AND event = 'sandbox.checkpoint_created'
|
|
144
|
+
ORDER BY ts DESC, id DESC
|
|
122
145
|
LIMIT 1`).get(name, sandboxId);
|
|
123
|
-
return row ? rowToEntry(row) : null;
|
|
146
|
+
return row ? rowToEntry$1(row) : null;
|
|
124
147
|
}
|
|
125
148
|
async listSandboxDetails(name, opts) {
|
|
126
149
|
return applyOpts(this.db.prepare(AGG_SQL("WHERE name = ?")).all(name).map(aggRowToDetails), opts);
|
|
@@ -138,8 +161,8 @@ var SQLiteAdapter = class {
|
|
|
138
161
|
async listCheckpoints(name, sandboxId) {
|
|
139
162
|
return this.db.prepare(`SELECT sandbox_id, name, step_idx, branch, event, payload, error, ts
|
|
140
163
|
FROM alineo_events
|
|
141
|
-
WHERE name = ? AND sandbox_id = ? AND event = 'checkpoint_created'
|
|
142
|
-
ORDER BY ts ASC`).all(name, sandboxId).map((r) => {
|
|
164
|
+
WHERE name = ? AND sandbox_id = ? AND event = 'sandbox.checkpoint_created'
|
|
165
|
+
ORDER BY ts ASC, id ASC`).all(name, sandboxId).map((r) => {
|
|
143
166
|
const p = r.payload !== null ? JSON.parse(r.payload) : { snapshotId: "" };
|
|
144
167
|
return {
|
|
145
168
|
snapshotId: p.snapshotId,
|
|
@@ -174,4 +197,145 @@ var SQLiteAdapter = class {
|
|
|
174
197
|
}
|
|
175
198
|
};
|
|
176
199
|
//#endregion
|
|
177
|
-
|
|
200
|
+
//#region src/engine-ledger.ts
|
|
201
|
+
const ENGINE_LEDGER_MIGRATION_SQL = `
|
|
202
|
+
CREATE TABLE IF NOT EXISTS engine_ledger (
|
|
203
|
+
seq INTEGER PRIMARY KEY,
|
|
204
|
+
scope TEXT NOT NULL,
|
|
205
|
+
sub_scope TEXT,
|
|
206
|
+
ts INTEGER NOT NULL,
|
|
207
|
+
event TEXT NOT NULL,
|
|
208
|
+
payload TEXT
|
|
209
|
+
);
|
|
210
|
+
CREATE INDEX IF NOT EXISTS engine_ledger_scope ON engine_ledger (scope, seq);
|
|
211
|
+
CREATE INDEX IF NOT EXISTS engine_ledger_sub_scope_event ON engine_ledger (sub_scope, event, seq);
|
|
212
|
+
`;
|
|
213
|
+
const DEFAULT_SCHEMA = {
|
|
214
|
+
table: "engine_ledger",
|
|
215
|
+
scopeColumn: "scope",
|
|
216
|
+
subScopeColumn: "sub_scope"
|
|
217
|
+
};
|
|
218
|
+
function rowToEntry(row) {
|
|
219
|
+
return {
|
|
220
|
+
seq: row.seq,
|
|
221
|
+
scope: row.scope,
|
|
222
|
+
subScope: row.sub_scope ?? void 0,
|
|
223
|
+
ts: row.ts,
|
|
224
|
+
event: row.event,
|
|
225
|
+
payload: row.payload !== null ? JSON.parse(row.payload) : void 0
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function rowToRawEntry(row) {
|
|
229
|
+
return {
|
|
230
|
+
seq: row.seq,
|
|
231
|
+
scope: row.scope,
|
|
232
|
+
subScope: row.sub_scope,
|
|
233
|
+
ts: row.ts,
|
|
234
|
+
event: row.event,
|
|
235
|
+
payload: row.payload
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* `IEngineLedger` on `bun:sqlite` — durability-roadmap.md M3 (3.1b). alineod's own
|
|
240
|
+
* `apps/alineod/src/state/db.ts` is this interface's first non-sandbox-SDK consumer: it binds
|
|
241
|
+
* this onto its *existing* `ledger` table (`run_id`/`agent_id`, via `schema`) rather than
|
|
242
|
+
* migrating onto a renamed one, since 11 other files across `apps/alineod` (migrations, ops
|
|
243
|
+
* checks, several tests) already address that table and its columns directly in raw SQL —
|
|
244
|
+
* renaming it would ripple into all of them for no behavioral gain. What alineod actually
|
|
245
|
+
* gets from adopting this: the same query/index shape as the sandbox SDK's own ledger, from
|
|
246
|
+
* one shared, tested implementation, instead of two independently hand-rolled ones that can
|
|
247
|
+
* silently drift (e.g. the composite-index fix in `adapter.ts` otherwise has no reason to ever
|
|
248
|
+
* get applied to alineod's copy).
|
|
249
|
+
*/
|
|
250
|
+
var SQLiteEngineLedger = class {
|
|
251
|
+
db;
|
|
252
|
+
ownsConnection;
|
|
253
|
+
insert;
|
|
254
|
+
selectByScope;
|
|
255
|
+
selectByScopeAfter;
|
|
256
|
+
selectBySubScope;
|
|
257
|
+
selectBySubScopeAndEvent;
|
|
258
|
+
selectAll;
|
|
259
|
+
/**
|
|
260
|
+
* `pathOrDb` accepts either a file path (opens its own connection — a standalone consumer
|
|
261
|
+
* like `apps/sandbox/server/registry.ts`) or an already-open `Database` (a consumer that,
|
|
262
|
+
* like alineod, already manages one shared connection across several tables and must not
|
|
263
|
+
* open a second one to the same file). Pragmas only run for a path, not a passed-in
|
|
264
|
+
* connection — the caller already owns those for their connection.
|
|
265
|
+
*
|
|
266
|
+
* `schema` lets a consumer with a pre-existing, differently-shaped ledger table bind onto
|
|
267
|
+
* its own table/columns instead of this class's default `engine_ledger` one — see
|
|
268
|
+
* `SQLiteEngineLedgerSchema`. When a custom `schema` is given, this class never runs
|
|
269
|
+
* `ENGINE_LEDGER_MIGRATION_SQL`: the caller already owns that table's DDL (its indexes,
|
|
270
|
+
* its migrations), and creating/indexing it here too would duplicate or conflict with that.
|
|
271
|
+
*/
|
|
272
|
+
constructor(pathOrDb, schema = DEFAULT_SCHEMA) {
|
|
273
|
+
this.ownsConnection = typeof pathOrDb === "string";
|
|
274
|
+
if (typeof pathOrDb === "string") {
|
|
275
|
+
try {
|
|
276
|
+
mkdirSync(dirname(pathOrDb), { recursive: true });
|
|
277
|
+
} catch (e) {
|
|
278
|
+
if (e.code !== "EEXIST") throw e;
|
|
279
|
+
}
|
|
280
|
+
this.db = new Database(pathOrDb, { create: true });
|
|
281
|
+
this.db.run("PRAGMA journal_mode = WAL;");
|
|
282
|
+
this.db.run("PRAGMA synchronous = FULL;");
|
|
283
|
+
} else this.db = pathOrDb;
|
|
284
|
+
if (schema === DEFAULT_SCHEMA) this.db.run(ENGINE_LEDGER_MIGRATION_SQL);
|
|
285
|
+
const { table, scopeColumn: sc, subScopeColumn: ssc } = schema;
|
|
286
|
+
const cols = `seq, ${sc} AS scope, ${ssc} AS sub_scope, ts, event, payload`;
|
|
287
|
+
this.insert = this.db.query(`INSERT INTO ${table} (${sc}, ${ssc}, ts, event, payload)
|
|
288
|
+
VALUES (?, ?, ?, ?, ?) RETURNING seq`);
|
|
289
|
+
this.selectByScope = this.db.query(`SELECT ${cols} FROM ${table} WHERE ${sc} = ? ORDER BY seq ASC`);
|
|
290
|
+
this.selectByScopeAfter = this.db.query(`SELECT ${cols} FROM ${table} WHERE ${sc} = ? AND seq > ? ORDER BY seq ASC`);
|
|
291
|
+
this.selectBySubScope = this.db.query(`SELECT ${cols} FROM ${table} WHERE ${ssc} = ? ORDER BY seq ASC`);
|
|
292
|
+
this.selectBySubScopeAndEvent = this.db.query(`SELECT ${cols} FROM ${table} WHERE ${ssc} = ? AND event = ? ORDER BY seq ASC`);
|
|
293
|
+
this.selectAll = this.db.query(`SELECT ${cols} FROM ${table} ORDER BY seq ASC`);
|
|
294
|
+
}
|
|
295
|
+
async connect() {}
|
|
296
|
+
/** A no-op when constructed from a shared connection — the owner closes it, not this. */
|
|
297
|
+
close() {
|
|
298
|
+
if (this.ownsConnection) this.db.close();
|
|
299
|
+
return Promise.resolve();
|
|
300
|
+
}
|
|
301
|
+
append(entry) {
|
|
302
|
+
return Promise.resolve(this.appendSync(entry));
|
|
303
|
+
}
|
|
304
|
+
readByScope(scope, opts) {
|
|
305
|
+
return Promise.resolve(this.readByScopeSync(scope, opts));
|
|
306
|
+
}
|
|
307
|
+
readBySubScope(subScope, event) {
|
|
308
|
+
return Promise.resolve(this.readBySubScopeSync(subScope, event));
|
|
309
|
+
}
|
|
310
|
+
readAll() {
|
|
311
|
+
return Promise.resolve(this.readAllSync());
|
|
312
|
+
}
|
|
313
|
+
appendSync(entry) {
|
|
314
|
+
const payloadJson = entry.payload === void 0 ? null : JSON.stringify(entry.payload);
|
|
315
|
+
const { seq } = this.insert.get(entry.scope, entry.subScope ?? null, entry.ts, entry.event, payloadJson);
|
|
316
|
+
return {
|
|
317
|
+
...entry,
|
|
318
|
+
seq
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
readByScopeSync(scope, opts) {
|
|
322
|
+
return (opts?.afterSeq !== void 0 ? this.selectByScopeAfter.all(scope, opts.afterSeq) : this.selectByScope.all(scope)).map(rowToEntry);
|
|
323
|
+
}
|
|
324
|
+
readBySubScopeSync(subScope, event) {
|
|
325
|
+
return (event !== void 0 ? this.selectBySubScopeAndEvent.all(subScope, event) : this.selectBySubScope.all(subScope)).map(rowToEntry);
|
|
326
|
+
}
|
|
327
|
+
readAllSync() {
|
|
328
|
+
return this.selectAll.all().map(rowToEntry);
|
|
329
|
+
}
|
|
330
|
+
readByScopeRawSync(scope, opts) {
|
|
331
|
+
return (opts?.afterSeq !== void 0 ? this.selectByScopeAfter.all(scope, opts.afterSeq) : this.selectByScope.all(scope)).map(rowToRawEntry);
|
|
332
|
+
}
|
|
333
|
+
readBySubScopeRawSync(subScope, event) {
|
|
334
|
+
return (event !== void 0 ? this.selectBySubScopeAndEvent.all(subScope, event) : this.selectBySubScope.all(subScope)).map(rowToRawEntry);
|
|
335
|
+
}
|
|
336
|
+
readAllRawSync() {
|
|
337
|
+
return this.selectAll.all().map(rowToRawEntry);
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
//#endregion
|
|
341
|
+
export { SQLiteAdapter, SQLiteEngineLedger };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alineo-labs/sqlite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
"test": "bun test"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@alineo-labs/core": "0.
|
|
31
|
+
"@alineo-labs/core": "0.6.0",
|
|
32
|
+
"@alineo-labs/ledger": "0.2.1"
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
|
34
35
|
"bun-types": "1.4.2",
|