@drej/sqlite 0.1.2 → 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/dist/index.d.mts +23 -0
- package/dist/index.mjs +155 -0
- package/package.json +23 -5
- package/CHANGELOG.md +0 -52
- package/src/adapter.ts +0 -97
- package/src/index.ts +0 -1
- package/src/migrations.ts +0 -16
- package/tsconfig.json +0 -13
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { CheckpointInfo, EnvironmentRecord, IStorageAdapter, LedgerEntry, ListSandboxOptions, SandboxDetails } from "@drej/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,155 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import { SandboxStatus } from "@drej/core";
|
|
3
|
+
//#region src/migrations.ts
|
|
4
|
+
const MIGRATION_SQL = `
|
|
5
|
+
CREATE TABLE IF NOT EXISTS drej_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 drej_events_sandbox_id ON drej_events(sandbox_id);
|
|
18
|
+
CREATE INDEX IF NOT EXISTS drej_events_name ON drej_events(name);
|
|
19
|
+
|
|
20
|
+
CREATE TABLE IF NOT EXISTS drej_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
|
+
FROM drej_events
|
|
39
|
+
${whereClause}
|
|
40
|
+
GROUP BY name, sandbox_id
|
|
41
|
+
)
|
|
42
|
+
SELECT * FROM agg WHERE started_at IS NOT NULL ORDER BY started_at DESC
|
|
43
|
+
`;
|
|
44
|
+
function aggRowToDetails(row) {
|
|
45
|
+
return {
|
|
46
|
+
name: row.name,
|
|
47
|
+
sandboxId: row.sandbox_id,
|
|
48
|
+
status: row.is_closed ? SandboxStatus.Completed : SandboxStatus.Running,
|
|
49
|
+
startedAt: row.started_at,
|
|
50
|
+
completedAt: row.completed_at ?? void 0,
|
|
51
|
+
execCount: row.exec_count
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function applyOpts(details, opts) {
|
|
55
|
+
let result = details;
|
|
56
|
+
if (opts?.before != null) result = result.filter((d) => d.startedAt < opts.before);
|
|
57
|
+
if (opts?.status != null) result = result.filter((d) => d.status === opts.status);
|
|
58
|
+
if (opts?.limit != null) result = result.slice(0, opts.limit);
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
function rowToEntry(row) {
|
|
62
|
+
return {
|
|
63
|
+
sandboxId: row.sandbox_id,
|
|
64
|
+
name: row.name,
|
|
65
|
+
stepIndex: row.step_idx,
|
|
66
|
+
branch: row.branch ?? void 0,
|
|
67
|
+
event: row.event,
|
|
68
|
+
payload: row.payload !== null ? JSON.parse(row.payload) : void 0,
|
|
69
|
+
error: row.error ?? void 0,
|
|
70
|
+
ts: row.ts
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
var SQLiteAdapter = class {
|
|
74
|
+
db;
|
|
75
|
+
constructor(path) {
|
|
76
|
+
this.db = new Database(path, { create: true });
|
|
77
|
+
}
|
|
78
|
+
async connect() {
|
|
79
|
+
this.db.exec(MIGRATION_SQL);
|
|
80
|
+
this.db.exec("PRAGMA journal_mode = WAL;");
|
|
81
|
+
}
|
|
82
|
+
async close() {
|
|
83
|
+
this.db.close();
|
|
84
|
+
}
|
|
85
|
+
async append(entry) {
|
|
86
|
+
this.db.prepare(`INSERT INTO drej_events (sandbox_id, name, step_idx, branch, event, payload, error, ts)
|
|
87
|
+
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);
|
|
88
|
+
}
|
|
89
|
+
async readAll(name, sandboxId) {
|
|
90
|
+
return this.db.prepare(`SELECT sandbox_id, name, step_idx, branch, event, payload, error, ts
|
|
91
|
+
FROM drej_events
|
|
92
|
+
WHERE name = ? AND sandbox_id = ?
|
|
93
|
+
ORDER BY ts ASC`).all(name, sandboxId).map(rowToEntry);
|
|
94
|
+
}
|
|
95
|
+
async lastCheckpoint(name, sandboxId) {
|
|
96
|
+
const row = this.db.prepare(`SELECT sandbox_id, name, step_idx, branch, event, payload, error, ts
|
|
97
|
+
FROM drej_events
|
|
98
|
+
WHERE name = ? AND sandbox_id = ? AND event = 'checkpoint_created'
|
|
99
|
+
ORDER BY ts DESC
|
|
100
|
+
LIMIT 1`).get(name, sandboxId);
|
|
101
|
+
return row ? rowToEntry(row) : null;
|
|
102
|
+
}
|
|
103
|
+
async listSandboxDetails(name, opts) {
|
|
104
|
+
return applyOpts(this.db.prepare(AGG_SQL("WHERE name = ?")).all(name).map(aggRowToDetails), opts);
|
|
105
|
+
}
|
|
106
|
+
async listAllSandboxDetails(opts) {
|
|
107
|
+
return applyOpts(this.db.prepare(AGG_SQL("")).all().map(aggRowToDetails), opts);
|
|
108
|
+
}
|
|
109
|
+
async getSandboxDetails(name, sandboxId) {
|
|
110
|
+
const row = this.db.prepare(AGG_SQL("WHERE name = ? AND sandbox_id = ?")).get(name, sandboxId);
|
|
111
|
+
return row ? aggRowToDetails(row) : null;
|
|
112
|
+
}
|
|
113
|
+
async deleteSandbox(name, sandboxId) {
|
|
114
|
+
this.db.prepare(`DELETE FROM drej_events WHERE name = ? AND sandbox_id = ?`).run(name, sandboxId);
|
|
115
|
+
}
|
|
116
|
+
async listCheckpoints(name, sandboxId) {
|
|
117
|
+
return this.db.prepare(`SELECT sandbox_id, name, step_idx, branch, event, payload, error, ts
|
|
118
|
+
FROM drej_events
|
|
119
|
+
WHERE name = ? AND sandbox_id = ? AND event = 'checkpoint_created'
|
|
120
|
+
ORDER BY ts ASC`).all(name, sandboxId).map((r) => {
|
|
121
|
+
const p = r.payload !== null ? JSON.parse(r.payload) : { snapshotId: "" };
|
|
122
|
+
return {
|
|
123
|
+
snapshotId: p.snapshotId,
|
|
124
|
+
tag: p.name,
|
|
125
|
+
createdAt: r.ts
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
async getEnvironment(name) {
|
|
130
|
+
const row = this.db.prepare("SELECT name, snapshot_id, image, built_at FROM drej_environments WHERE name = ?").get(name);
|
|
131
|
+
return row ? {
|
|
132
|
+
name: row.name,
|
|
133
|
+
snapshotId: row.snapshot_id,
|
|
134
|
+
image: row.image,
|
|
135
|
+
builtAt: row.built_at
|
|
136
|
+
} : null;
|
|
137
|
+
}
|
|
138
|
+
async saveEnvironment(record) {
|
|
139
|
+
this.db.prepare(`INSERT INTO drej_environments (name, snapshot_id, image, built_at) VALUES (?, ?, ?, ?)
|
|
140
|
+
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);
|
|
141
|
+
}
|
|
142
|
+
async deleteEnvironment(name) {
|
|
143
|
+
this.db.prepare("DELETE FROM drej_environments WHERE name = ?").run(name);
|
|
144
|
+
}
|
|
145
|
+
async listEnvironments() {
|
|
146
|
+
return this.db.prepare("SELECT name, snapshot_id, image, built_at FROM drej_environments ORDER BY built_at DESC").all().map((r) => ({
|
|
147
|
+
name: r.name,
|
|
148
|
+
snapshotId: r.snapshot_id,
|
|
149
|
+
image: r.image,
|
|
150
|
+
builtAt: r.built_at
|
|
151
|
+
}));
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
//#endregion
|
|
155
|
+
export { SQLiteAdapter };
|
package/package.json
CHANGED
|
@@ -1,13 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drej/sqlite",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist"
|
|
6
|
+
],
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.mjs",
|
|
9
|
+
"types": "./dist/index.d.mts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"types": "./dist/index.d.mts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsdown",
|
|
21
|
+
"test": "bun test"
|
|
22
|
+
},
|
|
6
23
|
"dependencies": {
|
|
7
24
|
"@drej/core": "workspace:*"
|
|
8
25
|
},
|
|
9
26
|
"devDependencies": {
|
|
10
|
-
"bun-types": "
|
|
11
|
-
"
|
|
27
|
+
"bun-types": "1.3.14",
|
|
28
|
+
"tsdown": "0.22.3",
|
|
29
|
+
"typescript": "6.0.3"
|
|
12
30
|
}
|
|
13
31
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
# @drej/sqlite
|
|
2
|
-
|
|
3
|
-
## 0.1.2
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- Updated dependencies [4c0ad93]
|
|
8
|
-
- Updated dependencies [b04f8eb]
|
|
9
|
-
- Updated dependencies [a971b7b]
|
|
10
|
-
- Updated dependencies [ce173be]
|
|
11
|
-
- Updated dependencies [86c2dde]
|
|
12
|
-
- Updated dependencies [82094ae]
|
|
13
|
-
- @drej/core@0.2.0
|
|
14
|
-
|
|
15
|
-
## 0.1.1
|
|
16
|
-
|
|
17
|
-
### Patch Changes
|
|
18
|
-
|
|
19
|
-
- 0ea4c33: Rename npm scope from `@drej/*` to `@drej/*` and add TSDoc to all public API surfaces.
|
|
20
|
-
|
|
21
|
-
- All workspace packages now published under `@drej/*` (e.g. `@drej/sqlite`, `@drej/postgres`)
|
|
22
|
-
- `DrejClient`, `WorkflowBuilder`, `SandboxStepBuilder`, `IStorageAdapter`, `LedgerEvent`, `SandboxOpts` and all their members now have hover documentation visible in VS Code
|
|
23
|
-
|
|
24
|
-
- Updated dependencies [0ea4c33]
|
|
25
|
-
- @drej/core@0.1.1
|
|
26
|
-
|
|
27
|
-
## 0.1.0
|
|
28
|
-
|
|
29
|
-
### Minor Changes
|
|
30
|
-
|
|
31
|
-
- 5d77498: Introduce `@drej/sqlite` storage adapter.
|
|
32
|
-
|
|
33
|
-
`SQLiteAdapter` implements `IStorageAdapter` via Bun's built-in `bun:sqlite` — zero extra dependencies and no infrastructure required. Data persists across restarts, making it suitable for local development and single-process production workloads.
|
|
34
|
-
|
|
35
|
-
```ts
|
|
36
|
-
import { DrejClient } from "drej";
|
|
37
|
-
import { SQLiteAdapter } from "@drej/sqlite";
|
|
38
|
-
|
|
39
|
-
const client = new DrejClient({
|
|
40
|
-
baseUrl: "http://localhost:8080",
|
|
41
|
-
adapter: new SQLiteAdapter("./drej.db"),
|
|
42
|
-
});
|
|
43
|
-
await client.connect();
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
WAL mode is enabled on `connect()` for safe concurrent reads alongside ongoing writes.
|
|
47
|
-
|
|
48
|
-
### Patch Changes
|
|
49
|
-
|
|
50
|
-
- Updated dependencies [82e77fd]
|
|
51
|
-
- Updated dependencies [5d77498]
|
|
52
|
-
- @drej/core@0.1.0
|
package/src/adapter.ts
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { Database } from "bun:sqlite";
|
|
2
|
-
import type { IStorageAdapter, LedgerEntry, LedgerEvent } from "@drej/core";
|
|
3
|
-
import { MIGRATION_SQL } from "./migrations";
|
|
4
|
-
|
|
5
|
-
type Row = {
|
|
6
|
-
run_id: string;
|
|
7
|
-
wf_name: string;
|
|
8
|
-
step_idx: number;
|
|
9
|
-
branch: number | null;
|
|
10
|
-
event: string;
|
|
11
|
-
payload: string | null;
|
|
12
|
-
error: string | null;
|
|
13
|
-
ts: number;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
function rowToEntry(row: Row): LedgerEntry {
|
|
17
|
-
return {
|
|
18
|
-
runId: row.run_id,
|
|
19
|
-
workflowName: row.wf_name,
|
|
20
|
-
stepIndex: row.step_idx,
|
|
21
|
-
branch: row.branch ?? undefined,
|
|
22
|
-
event: row.event as LedgerEvent,
|
|
23
|
-
payload: row.payload !== null ? (JSON.parse(row.payload) as unknown) : undefined,
|
|
24
|
-
error: row.error ?? undefined,
|
|
25
|
-
ts: row.ts,
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export class SQLiteAdapter implements IStorageAdapter {
|
|
30
|
-
private readonly db: Database;
|
|
31
|
-
|
|
32
|
-
constructor(path: string) {
|
|
33
|
-
this.db = new Database(path, { create: true });
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async connect(): Promise<void> {
|
|
37
|
-
this.db.exec(MIGRATION_SQL);
|
|
38
|
-
// WAL mode prevents writer from blocking readers on concurrent access
|
|
39
|
-
this.db.exec("PRAGMA journal_mode = WAL;");
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async close(): Promise<void> {
|
|
43
|
-
this.db.close();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async append(entry: LedgerEntry): Promise<void> {
|
|
47
|
-
this.db
|
|
48
|
-
.prepare(
|
|
49
|
-
`INSERT INTO drej_events (run_id, wf_name, step_idx, branch, event, payload, error, ts)
|
|
50
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
51
|
-
)
|
|
52
|
-
.run(
|
|
53
|
-
entry.runId,
|
|
54
|
-
entry.workflowName,
|
|
55
|
-
entry.stepIndex,
|
|
56
|
-
entry.branch ?? null,
|
|
57
|
-
entry.event,
|
|
58
|
-
entry.payload !== undefined ? JSON.stringify(entry.payload) : null,
|
|
59
|
-
entry.error ?? null,
|
|
60
|
-
entry.ts,
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async readAll(workflowName: string, runId: string): Promise<LedgerEntry[]> {
|
|
65
|
-
const rows = this.db
|
|
66
|
-
.prepare<Row, [string, string]>(
|
|
67
|
-
`SELECT run_id, wf_name, step_idx, branch, event, payload, error, ts
|
|
68
|
-
FROM drej_events
|
|
69
|
-
WHERE wf_name = ? AND run_id = ?
|
|
70
|
-
ORDER BY ts ASC`,
|
|
71
|
-
)
|
|
72
|
-
.all(workflowName, runId);
|
|
73
|
-
return rows.map(rowToEntry);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async lastCheckpoint(workflowName: string, runId: string): Promise<LedgerEntry | null> {
|
|
77
|
-
const row = this.db
|
|
78
|
-
.prepare<Row, [string, string]>(
|
|
79
|
-
`SELECT run_id, wf_name, step_idx, branch, event, payload, error, ts
|
|
80
|
-
FROM drej_events
|
|
81
|
-
WHERE wf_name = ? AND run_id = ? AND event = 'checkpoint'
|
|
82
|
-
ORDER BY ts DESC
|
|
83
|
-
LIMIT 1`,
|
|
84
|
-
)
|
|
85
|
-
.get(workflowName, runId);
|
|
86
|
-
return row ? rowToEntry(row) : null;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
async listRuns(workflowName: string): Promise<string[]> {
|
|
90
|
-
const rows = this.db
|
|
91
|
-
.prepare<{ run_id: string }, [string]>(
|
|
92
|
-
`SELECT DISTINCT run_id FROM drej_events WHERE wf_name = ?`,
|
|
93
|
-
)
|
|
94
|
-
.all(workflowName);
|
|
95
|
-
return rows.map((r) => r.run_id);
|
|
96
|
-
}
|
|
97
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { SQLiteAdapter } from "./adapter";
|
package/src/migrations.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export const MIGRATION_SQL = `
|
|
2
|
-
CREATE TABLE IF NOT EXISTS drej_events (
|
|
3
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
4
|
-
run_id TEXT NOT NULL,
|
|
5
|
-
wf_name TEXT NOT NULL,
|
|
6
|
-
step_idx INTEGER NOT NULL,
|
|
7
|
-
branch INTEGER,
|
|
8
|
-
event TEXT NOT NULL,
|
|
9
|
-
payload TEXT,
|
|
10
|
-
error TEXT,
|
|
11
|
-
ts INTEGER NOT NULL
|
|
12
|
-
);
|
|
13
|
-
|
|
14
|
-
CREATE INDEX IF NOT EXISTS drej_events_run_id ON drej_events(run_id);
|
|
15
|
-
CREATE INDEX IF NOT EXISTS drej_events_wf_name ON drej_events(wf_name);
|
|
16
|
-
`;
|
package/tsconfig.json
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ESNext",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"moduleResolution": "bundler",
|
|
6
|
-
"strict": true,
|
|
7
|
-
"noEmit": true,
|
|
8
|
-
"skipLibCheck": true,
|
|
9
|
-
"allowImportingTsExtensions": true,
|
|
10
|
-
"types": ["bun-types"]
|
|
11
|
-
},
|
|
12
|
-
"include": ["src"]
|
|
13
|
-
}
|