@alineo-labs/postgres 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 +31 -0
- package/dist/index.d.mts +24 -0
- package/dist/index.mjs +186 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# @alineo-labs/postgres
|
|
2
|
+
|
|
3
|
+
Postgres storage adapter for [alineo](https://alineo.tech). Stores the sandbox ledger in a Postgres database — suitable for production deployments where multiple processes or machines share the same ledger.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
bun add @alineo-labs/postgres
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
For local development, [`@alineo-labs/sqlite`](https://github.com/DrejT/alineo/tree/main/packages/adapters/sqlite) is simpler and requires no infrastructure.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { Alineo } from "alineo";
|
|
17
|
+
import { PostgresAdapter } from "@alineo-labs/postgres";
|
|
18
|
+
|
|
19
|
+
const client = new Alineo({
|
|
20
|
+
baseUrl: "http://localhost:8080",
|
|
21
|
+
adapter: new PostgresAdapter("postgresql://user:pass@localhost:5432/alineo"),
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The adapter initialises lazily on first use and runs idempotent migrations (`CREATE TABLE IF NOT EXISTS`) on startup — no separate migration step needed.
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## License
|
|
30
|
+
|
|
31
|
+
Apache 2.0
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { CheckpointInfo, EnvironmentRecord, IStorageAdapter, LedgerEntry, ListSandboxOptions, SandboxDetails } from "@alineo-labs/core";
|
|
2
|
+
|
|
3
|
+
//#region src/adapter.d.ts
|
|
4
|
+
declare class PostgresAdapter implements IStorageAdapter {
|
|
5
|
+
private readonly sql;
|
|
6
|
+
constructor(connectionString: 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
|
+
private _aggQuery;
|
|
13
|
+
listSandboxDetails(name: string, opts?: ListSandboxOptions): Promise<SandboxDetails[]>;
|
|
14
|
+
listAllSandboxDetails(opts?: ListSandboxOptions): Promise<SandboxDetails[]>;
|
|
15
|
+
getSandboxDetails(name: string, sandboxId: string): Promise<SandboxDetails | null>;
|
|
16
|
+
deleteSandbox(name: string, sandboxId: string): Promise<void>;
|
|
17
|
+
listCheckpoints(name: string, sandboxId: string): Promise<CheckpointInfo[]>;
|
|
18
|
+
getEnvironment(name: string): Promise<EnvironmentRecord | null>;
|
|
19
|
+
saveEnvironment(record: EnvironmentRecord): Promise<void>;
|
|
20
|
+
deleteEnvironment(name: string): Promise<void>;
|
|
21
|
+
listEnvironments(): Promise<EnvironmentRecord[]>;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { PostgresAdapter };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import postgres from "postgres";
|
|
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 BIGSERIAL PRIMARY KEY,
|
|
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 JSONB,
|
|
13
|
+
error TEXT,
|
|
14
|
+
ts BIGINT 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 BIGINT NOT NULL
|
|
25
|
+
);
|
|
26
|
+
`;
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/adapter.ts
|
|
29
|
+
function aggRowToDetails(row) {
|
|
30
|
+
return {
|
|
31
|
+
name: row.name,
|
|
32
|
+
sandboxId: row.sandbox_id,
|
|
33
|
+
status: Number(row.is_closed) ? SandboxStatus.Completed : SandboxStatus.Running,
|
|
34
|
+
startedAt: Number(row.started_at),
|
|
35
|
+
completedAt: row.completed_at != null ? Number(row.completed_at) : void 0,
|
|
36
|
+
execCount: Number(row.exec_count),
|
|
37
|
+
runId: row.run_id ?? row.sandbox_id
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function applyOpts(details, opts) {
|
|
41
|
+
let result = details;
|
|
42
|
+
if (opts?.before != null) result = result.filter((d) => d.startedAt < opts.before);
|
|
43
|
+
if (opts?.status != null) result = result.filter((d) => d.status === opts.status);
|
|
44
|
+
if (opts?.runId != null) result = result.filter((d) => d.runId === opts.runId);
|
|
45
|
+
if (opts?.limit != null) result = result.slice(0, opts.limit);
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
function rowToEntry(row) {
|
|
49
|
+
return {
|
|
50
|
+
sandboxId: row.sandbox_id,
|
|
51
|
+
name: row.name,
|
|
52
|
+
stepIndex: row.step_idx,
|
|
53
|
+
branch: row.branch ?? void 0,
|
|
54
|
+
event: row.event,
|
|
55
|
+
payload: row.payload ?? void 0,
|
|
56
|
+
error: row.error ?? void 0,
|
|
57
|
+
ts: Number(row.ts)
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
var PostgresAdapter = class {
|
|
61
|
+
sql;
|
|
62
|
+
constructor(connectionString) {
|
|
63
|
+
this.sql = postgres(connectionString);
|
|
64
|
+
}
|
|
65
|
+
async connect() {
|
|
66
|
+
await this.sql.unsafe(MIGRATION_SQL);
|
|
67
|
+
}
|
|
68
|
+
async close() {
|
|
69
|
+
await this.sql.end();
|
|
70
|
+
}
|
|
71
|
+
async append(entry) {
|
|
72
|
+
await this.sql`
|
|
73
|
+
INSERT INTO alineo_events (sandbox_id, name, step_idx, branch, event, payload, error, ts)
|
|
74
|
+
VALUES (
|
|
75
|
+
${entry.sandboxId},
|
|
76
|
+
${entry.name},
|
|
77
|
+
${entry.stepIndex},
|
|
78
|
+
${entry.branch ?? null},
|
|
79
|
+
${entry.event},
|
|
80
|
+
${entry.payload !== void 0 ? JSON.stringify(entry.payload) : null},
|
|
81
|
+
${entry.error ?? null},
|
|
82
|
+
${entry.ts}
|
|
83
|
+
)
|
|
84
|
+
`;
|
|
85
|
+
}
|
|
86
|
+
async readAll(name, sandboxId) {
|
|
87
|
+
return (await this.sql`
|
|
88
|
+
SELECT sandbox_id, name, step_idx, branch, event, payload, error, ts
|
|
89
|
+
FROM alineo_events
|
|
90
|
+
WHERE name = ${name} AND sandbox_id = ${sandboxId}
|
|
91
|
+
ORDER BY ts ASC
|
|
92
|
+
`).map(rowToEntry);
|
|
93
|
+
}
|
|
94
|
+
async lastCheckpoint(name, sandboxId) {
|
|
95
|
+
const rows = await this.sql`
|
|
96
|
+
SELECT sandbox_id, name, step_idx, branch, event, payload, error, ts
|
|
97
|
+
FROM alineo_events
|
|
98
|
+
WHERE name = ${name} AND sandbox_id = ${sandboxId} AND event = 'checkpoint_created'
|
|
99
|
+
ORDER BY ts DESC
|
|
100
|
+
LIMIT 1
|
|
101
|
+
`;
|
|
102
|
+
return rows.length ? rowToEntry(rows[0]) : null;
|
|
103
|
+
}
|
|
104
|
+
async _aggQuery(whereClause, params) {
|
|
105
|
+
return this.sql.unsafe(`WITH agg AS (
|
|
106
|
+
SELECT
|
|
107
|
+
name,
|
|
108
|
+
sandbox_id,
|
|
109
|
+
MIN(CASE WHEN event = 'sandbox_created' THEN ts END) AS started_at,
|
|
110
|
+
MAX(CASE WHEN event = 'sandbox_closed' THEN ts END) AS completed_at,
|
|
111
|
+
MAX(CASE WHEN event = 'sandbox_closed' THEN 1 ELSE 0 END)::int AS is_closed,
|
|
112
|
+
COUNT(CASE WHEN event = 'exec_complete' THEN 1 END)::int AS exec_count,
|
|
113
|
+
MAX(CASE WHEN event = 'sandbox_created' THEN payload->>'runId' END) AS run_id
|
|
114
|
+
FROM alineo_events
|
|
115
|
+
${whereClause}
|
|
116
|
+
GROUP BY name, sandbox_id
|
|
117
|
+
)
|
|
118
|
+
SELECT * FROM agg WHERE started_at IS NOT NULL ORDER BY started_at DESC`, params);
|
|
119
|
+
}
|
|
120
|
+
async listSandboxDetails(name, opts) {
|
|
121
|
+
return applyOpts((await this._aggQuery("WHERE name = $1", [name])).map(aggRowToDetails), opts);
|
|
122
|
+
}
|
|
123
|
+
async listAllSandboxDetails(opts) {
|
|
124
|
+
return applyOpts((await this._aggQuery("", [])).map(aggRowToDetails), opts);
|
|
125
|
+
}
|
|
126
|
+
async getSandboxDetails(name, sandboxId) {
|
|
127
|
+
const rows = await this._aggQuery("WHERE name = $1 AND sandbox_id = $2", [name, sandboxId]);
|
|
128
|
+
return rows.length ? aggRowToDetails(rows[0]) : null;
|
|
129
|
+
}
|
|
130
|
+
async deleteSandbox(name, sandboxId) {
|
|
131
|
+
await this.sql`DELETE FROM alineo_events WHERE name = ${name} AND sandbox_id = ${sandboxId}`;
|
|
132
|
+
}
|
|
133
|
+
async listCheckpoints(name, sandboxId) {
|
|
134
|
+
return (await this.sql`
|
|
135
|
+
SELECT sandbox_id, name, step_idx, branch, event, payload, error, ts
|
|
136
|
+
FROM alineo_events
|
|
137
|
+
WHERE name = ${name} AND sandbox_id = ${sandboxId} AND event = 'checkpoint_created'
|
|
138
|
+
ORDER BY ts ASC
|
|
139
|
+
`).map((r) => {
|
|
140
|
+
const p = r.payload ?? {};
|
|
141
|
+
return {
|
|
142
|
+
snapshotId: p.snapshotId,
|
|
143
|
+
tag: p.name,
|
|
144
|
+
createdAt: Number(r.ts)
|
|
145
|
+
};
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
async getEnvironment(name) {
|
|
149
|
+
const rows = await this.sql`
|
|
150
|
+
SELECT name, snapshot_id, image, built_at FROM alineo_environments WHERE name = ${name}
|
|
151
|
+
`;
|
|
152
|
+
if (!rows.length) return null;
|
|
153
|
+
const r = rows[0];
|
|
154
|
+
return {
|
|
155
|
+
name: r.name,
|
|
156
|
+
snapshotId: r.snapshot_id,
|
|
157
|
+
image: r.image,
|
|
158
|
+
builtAt: Number(r.built_at)
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
async saveEnvironment(record) {
|
|
162
|
+
await this.sql`
|
|
163
|
+
INSERT INTO alineo_environments (name, snapshot_id, image, built_at)
|
|
164
|
+
VALUES (${record.name}, ${record.snapshotId}, ${record.image}, ${record.builtAt})
|
|
165
|
+
ON CONFLICT (name) DO UPDATE
|
|
166
|
+
SET snapshot_id = excluded.snapshot_id,
|
|
167
|
+
image = excluded.image,
|
|
168
|
+
built_at = excluded.built_at
|
|
169
|
+
`;
|
|
170
|
+
}
|
|
171
|
+
async deleteEnvironment(name) {
|
|
172
|
+
await this.sql`DELETE FROM alineo_environments WHERE name = ${name}`;
|
|
173
|
+
}
|
|
174
|
+
async listEnvironments() {
|
|
175
|
+
return (await this.sql`
|
|
176
|
+
SELECT name, snapshot_id, image, built_at FROM alineo_environments ORDER BY built_at DESC
|
|
177
|
+
`).map((r) => ({
|
|
178
|
+
name: r.name,
|
|
179
|
+
snapshotId: r.snapshot_id,
|
|
180
|
+
image: r.image,
|
|
181
|
+
builtAt: Number(r.built_at)
|
|
182
|
+
}));
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
//#endregion
|
|
186
|
+
export { PostgresAdapter };
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alineo-labs/postgres",
|
|
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/postgres"
|
|
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
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@alineo-labs/core": "0.1.0",
|
|
31
|
+
"postgres": "3.4.9"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"bun-types": "1.3.14",
|
|
35
|
+
"tsdown": "0.22.3",
|
|
36
|
+
"typescript": "6.0.3"
|
|
37
|
+
}
|
|
38
|
+
}
|