@alineo-labs/sqlite 0.1.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 ADDED
@@ -0,0 +1,33 @@
1
+ # @alineo-labs/sqlite
2
+
3
+ SQLite storage adapter for [alineo](https://alineo.tech). Stores the sandbox ledger in a local `.db` file — zero infrastructure, works out of the box.
4
+
5
+ ```bash
6
+ bun add @alineo-labs/sqlite
7
+ ```
8
+
9
+ For production workloads that need durability across multiple processes or machines, use [`@alineo-labs/postgres`](https://github.com/DrejT/alineo/tree/main/packages/adapters/postgres) instead.
10
+
11
+ ---
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { Alineo } from "alineo";
17
+ import { SQLiteAdapter } from "@alineo-labs/sqlite";
18
+
19
+ const client = new Alineo({
20
+ baseUrl: "http://localhost:8080",
21
+ adapter: new SQLiteAdapter("./ledger.db"),
22
+ });
23
+ ```
24
+
25
+ The adapter initialises lazily on first use — no `connect()` call needed. On process exit, `beforeExit` closes the connection automatically.
26
+
27
+ WAL mode is enabled by default for better concurrent read performance.
28
+
29
+ ---
30
+
31
+ ## License
32
+
33
+ Apache 2.0
@@ -0,0 +1,23 @@
1
+ import { CheckpointInfo, EnvironmentRecord, IStorageAdapter, LedgerEntry, ListSandboxOptions, SandboxDetails } from "@alineo-labs/core";
2
+
3
+ //#region src/adapter.d.ts
4
+ declare class SQLiteAdapter implements IStorageAdapter {
5
+ private readonly db;
6
+ constructor(path: string);
7
+ connect(): Promise<void>;
8
+ close(): Promise<void>;
9
+ append(entry: LedgerEntry): Promise<void>;
10
+ readAll(name: string, sandboxId: string): Promise<LedgerEntry[]>;
11
+ lastCheckpoint(name: string, sandboxId: string): Promise<LedgerEntry | null>;
12
+ listSandboxDetails(name: string, opts?: ListSandboxOptions): Promise<SandboxDetails[]>;
13
+ listAllSandboxDetails(opts?: ListSandboxOptions): Promise<SandboxDetails[]>;
14
+ getSandboxDetails(name: string, sandboxId: string): Promise<SandboxDetails | null>;
15
+ deleteSandbox(name: string, sandboxId: string): Promise<void>;
16
+ listCheckpoints(name: string, sandboxId: string): Promise<CheckpointInfo[]>;
17
+ getEnvironment(name: string): Promise<EnvironmentRecord | null>;
18
+ saveEnvironment(record: EnvironmentRecord): Promise<void>;
19
+ deleteEnvironment(name: string): Promise<void>;
20
+ listEnvironments(): Promise<EnvironmentRecord[]>;
21
+ }
22
+ //#endregion
23
+ export { SQLiteAdapter };
package/dist/index.mjs ADDED
@@ -0,0 +1,158 @@
1
+ import { Database } from "bun:sqlite";
2
+ import { SandboxStatus } from "@alineo-labs/core";
3
+ //#region src/migrations.ts
4
+ const MIGRATION_SQL = `
5
+ CREATE TABLE IF NOT EXISTS alineo_events (
6
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
7
+ sandbox_id TEXT NOT NULL,
8
+ name TEXT NOT NULL,
9
+ step_idx INTEGER NOT NULL,
10
+ branch INTEGER,
11
+ event TEXT NOT NULL,
12
+ payload TEXT,
13
+ error TEXT,
14
+ ts INTEGER NOT NULL
15
+ );
16
+
17
+ CREATE INDEX IF NOT EXISTS alineo_events_sandbox_id ON alineo_events(sandbox_id);
18
+ CREATE INDEX IF NOT EXISTS alineo_events_name ON alineo_events(name);
19
+
20
+ CREATE TABLE IF NOT EXISTS alineo_environments (
21
+ name TEXT PRIMARY KEY,
22
+ snapshot_id TEXT NOT NULL,
23
+ image TEXT NOT NULL,
24
+ built_at INTEGER NOT NULL
25
+ );
26
+ `;
27
+ //#endregion
28
+ //#region src/adapter.ts
29
+ const AGG_SQL = (whereClause) => `
30
+ WITH agg AS (
31
+ SELECT
32
+ name,
33
+ sandbox_id,
34
+ MIN(CASE WHEN event = 'sandbox_created' THEN ts END) AS started_at,
35
+ MAX(CASE WHEN event = 'sandbox_closed' THEN ts END) AS completed_at,
36
+ MAX(CASE WHEN event = 'sandbox_closed' THEN 1 ELSE 0 END) AS is_closed,
37
+ CAST(COUNT(CASE WHEN event = 'exec_complete' THEN 1 END) AS INTEGER) AS exec_count,
38
+ MAX(CASE WHEN event = 'sandbox_created' THEN json_extract(payload, '$.runId') END) AS run_id
39
+ FROM alineo_events
40
+ ${whereClause}
41
+ GROUP BY name, sandbox_id
42
+ )
43
+ SELECT * FROM agg WHERE started_at IS NOT NULL ORDER BY started_at DESC
44
+ `;
45
+ function aggRowToDetails(row) {
46
+ return {
47
+ name: row.name,
48
+ sandboxId: row.sandbox_id,
49
+ status: row.is_closed ? SandboxStatus.Completed : SandboxStatus.Running,
50
+ startedAt: row.started_at,
51
+ completedAt: row.completed_at ?? void 0,
52
+ execCount: row.exec_count,
53
+ runId: row.run_id ?? row.sandbox_id
54
+ };
55
+ }
56
+ function applyOpts(details, opts) {
57
+ let result = details;
58
+ if (opts?.before != null) result = result.filter((d) => d.startedAt < opts.before);
59
+ if (opts?.status != null) result = result.filter((d) => d.status === opts.status);
60
+ if (opts?.runId != null) result = result.filter((d) => d.runId === opts.runId);
61
+ if (opts?.limit != null) result = result.slice(0, opts.limit);
62
+ return result;
63
+ }
64
+ function rowToEntry(row) {
65
+ return {
66
+ sandboxId: row.sandbox_id,
67
+ name: row.name,
68
+ stepIndex: row.step_idx,
69
+ branch: row.branch ?? void 0,
70
+ event: row.event,
71
+ payload: row.payload !== null ? JSON.parse(row.payload) : void 0,
72
+ error: row.error ?? void 0,
73
+ ts: row.ts
74
+ };
75
+ }
76
+ var SQLiteAdapter = class {
77
+ db;
78
+ constructor(path) {
79
+ this.db = new Database(path, { create: true });
80
+ }
81
+ async connect() {
82
+ this.db.exec(MIGRATION_SQL);
83
+ this.db.exec("PRAGMA journal_mode = WAL;");
84
+ }
85
+ async close() {
86
+ this.db.close();
87
+ }
88
+ async append(entry) {
89
+ this.db.prepare(`INSERT INTO alineo_events (sandbox_id, name, step_idx, branch, event, payload, error, ts)
90
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)`).run(entry.sandboxId, entry.name, entry.stepIndex, entry.branch ?? null, entry.event, entry.payload !== void 0 ? JSON.stringify(entry.payload) : null, entry.error ?? null, entry.ts);
91
+ }
92
+ async readAll(name, sandboxId) {
93
+ return this.db.prepare(`SELECT sandbox_id, name, step_idx, branch, event, payload, error, ts
94
+ FROM alineo_events
95
+ WHERE name = ? AND sandbox_id = ?
96
+ ORDER BY ts ASC`).all(name, sandboxId).map(rowToEntry);
97
+ }
98
+ async lastCheckpoint(name, sandboxId) {
99
+ const row = this.db.prepare(`SELECT sandbox_id, name, step_idx, branch, event, payload, error, ts
100
+ FROM alineo_events
101
+ WHERE name = ? AND sandbox_id = ? AND event = 'checkpoint_created'
102
+ ORDER BY ts DESC
103
+ LIMIT 1`).get(name, sandboxId);
104
+ return row ? rowToEntry(row) : null;
105
+ }
106
+ async listSandboxDetails(name, opts) {
107
+ return applyOpts(this.db.prepare(AGG_SQL("WHERE name = ?")).all(name).map(aggRowToDetails), opts);
108
+ }
109
+ async listAllSandboxDetails(opts) {
110
+ return applyOpts(this.db.prepare(AGG_SQL("")).all().map(aggRowToDetails), opts);
111
+ }
112
+ async getSandboxDetails(name, sandboxId) {
113
+ const row = this.db.prepare(AGG_SQL("WHERE name = ? AND sandbox_id = ?")).get(name, sandboxId);
114
+ return row ? aggRowToDetails(row) : null;
115
+ }
116
+ async deleteSandbox(name, sandboxId) {
117
+ this.db.prepare(`DELETE FROM alineo_events WHERE name = ? AND sandbox_id = ?`).run(name, sandboxId);
118
+ }
119
+ async listCheckpoints(name, sandboxId) {
120
+ return this.db.prepare(`SELECT sandbox_id, name, step_idx, branch, event, payload, error, ts
121
+ FROM alineo_events
122
+ WHERE name = ? AND sandbox_id = ? AND event = 'checkpoint_created'
123
+ ORDER BY ts ASC`).all(name, sandboxId).map((r) => {
124
+ const p = r.payload !== null ? JSON.parse(r.payload) : { snapshotId: "" };
125
+ return {
126
+ snapshotId: p.snapshotId,
127
+ tag: p.name,
128
+ createdAt: r.ts
129
+ };
130
+ });
131
+ }
132
+ async getEnvironment(name) {
133
+ const row = this.db.prepare("SELECT name, snapshot_id, image, built_at FROM alineo_environments WHERE name = ?").get(name);
134
+ return row ? {
135
+ name: row.name,
136
+ snapshotId: row.snapshot_id,
137
+ image: row.image,
138
+ builtAt: row.built_at
139
+ } : null;
140
+ }
141
+ async saveEnvironment(record) {
142
+ this.db.prepare(`INSERT INTO alineo_environments (name, snapshot_id, image, built_at) VALUES (?, ?, ?, ?)
143
+ ON CONFLICT(name) DO UPDATE SET snapshot_id = excluded.snapshot_id, image = excluded.image, built_at = excluded.built_at`).run(record.name, record.snapshotId, record.image, record.builtAt);
144
+ }
145
+ async deleteEnvironment(name) {
146
+ this.db.prepare("DELETE FROM alineo_environments WHERE name = ?").run(name);
147
+ }
148
+ async listEnvironments() {
149
+ return this.db.prepare("SELECT name, snapshot_id, image, built_at FROM alineo_environments ORDER BY built_at DESC").all().map((r) => ({
150
+ name: r.name,
151
+ snapshotId: r.snapshot_id,
152
+ image: r.image,
153
+ builtAt: r.built_at
154
+ }));
155
+ }
156
+ };
157
+ //#endregion
158
+ export { SQLiteAdapter };
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@alineo-labs/sqlite",
3
+ "version": "0.1.0",
4
+ "license": "Apache-2.0",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/DrejT/alineo.git",
8
+ "directory": "packages/adapters/sqlite"
9
+ },
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "type": "module",
14
+ "main": "./dist/index.mjs",
15
+ "types": "./dist/index.d.mts",
16
+ "exports": {
17
+ ".": {
18
+ "import": "./dist/index.mjs",
19
+ "types": "./dist/index.d.mts"
20
+ }
21
+ },
22
+ "publishConfig": {
23
+ "access": "public",
24
+ "provenance": true
25
+ },
26
+ "scripts": {
27
+ "build": "tsdown",
28
+ "test": "bun test"
29
+ },
30
+ "dependencies": {
31
+ "@alineo-labs/core": "0.1.0"
32
+ },
33
+ "devDependencies": {
34
+ "bun-types": "1.3.14",
35
+ "tsdown": "0.22.3",
36
+ "typescript": "6.0.3"
37
+ }
38
+ }