@effectstream/db 0.100.14 → 0.100.15
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 +103 -0
- package/package.json +6 -6
- package/test/examples.test.ts +27 -0
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# @effectstream/db
|
|
2
|
+
|
|
3
|
+
The PostgreSQL (and PgLite) layer for EffectStream. Wraps `pg` with a
|
|
4
|
+
connection pool, ships every pgtyped-generated SQL query the runtime
|
|
5
|
+
needs, owns the snapshot loop, and provides a mutex needed for safe
|
|
6
|
+
single-threaded PgLite access.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
bun add @effectstream/db
|
|
12
|
+
# or
|
|
13
|
+
npm install @effectstream/db
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Standalone usage
|
|
17
|
+
|
|
18
|
+
You can drop `@effectstream/db` into any Node service that needs a
|
|
19
|
+
pooled Postgres client plus EffectStream's pgtyped queries. The most
|
|
20
|
+
common things you'd reach for:
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import {
|
|
24
|
+
getConnection,
|
|
25
|
+
getBlockHeights,
|
|
26
|
+
getLatestProcessedBlockHeight,
|
|
27
|
+
} from "@effectstream/db";
|
|
28
|
+
|
|
29
|
+
const pool = getConnection({
|
|
30
|
+
host: process.env.PGHOST,
|
|
31
|
+
user: process.env.PGUSER,
|
|
32
|
+
password: process.env.PGPASSWORD,
|
|
33
|
+
database: process.env.PGDATABASE,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const client = await pool.connect();
|
|
37
|
+
try {
|
|
38
|
+
const heights = await getBlockHeights.run({ limit: 5 }, client);
|
|
39
|
+
console.log(heights);
|
|
40
|
+
} finally {
|
|
41
|
+
client.release();
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
For PgLite (in-memory) the package ships `./start-pglite`,
|
|
46
|
+
`./apply-migrations`, `./db-wait`, `./pgtyped-update`, `./version` as
|
|
47
|
+
small executable subpaths — used directly by the orchestrator.
|
|
48
|
+
|
|
49
|
+
> **PgLite caveat:** PgLite is single-writer. Wrap PgLite-bound code in
|
|
50
|
+
> `acquireDBMutex(name)` / `releaseDBMutex(name)` so concurrent generators
|
|
51
|
+
> don't trample each other.
|
|
52
|
+
|
|
53
|
+
## Inside EffectStream
|
|
54
|
+
|
|
55
|
+
`@effectstream/db` is the canonical access path. The runtime gets a
|
|
56
|
+
pooled client through `getConnection()`, every state-transition function
|
|
57
|
+
yields against pgtyped queries exported here, and the snapshot loop
|
|
58
|
+
(`runSnapshotLoop`) emits versioned `pg_dump` artifacts so a fresh node
|
|
59
|
+
can rejoin without re-syncing from genesis.
|
|
60
|
+
|
|
61
|
+
## Key exports
|
|
62
|
+
|
|
63
|
+
Connection management (heavily used):
|
|
64
|
+
|
|
65
|
+
- `getConnection(creds?)` — pooled `Pool` singleton. The dominant entry point (~31 cross-package call sites).
|
|
66
|
+
- `acquireDBMutex(name, priority?)` / `releaseDBMutex(name)` — coordination for PgLite (~7 cross-package call sites each).
|
|
67
|
+
- `waitUntilFree()` — companion to the mutex.
|
|
68
|
+
- `getPersistentConnection(creds)` — a non-pooled `Client` for long-lived listeners.
|
|
69
|
+
|
|
70
|
+
Queries (pgtyped-generated, shipped under one umbrella):
|
|
71
|
+
|
|
72
|
+
- `getBlockHeights`, `getBlockByHash`, `blockHeightDone`, `saveLastBlock`, `getLatestProcessedBlockHeight` — block bookkeeping.
|
|
73
|
+
- `getAchievementProgress`, `setAchievementProgress` — achievements.
|
|
74
|
+
- Re-exports of `*.queries.ts` for statistics, nonces, rollup inputs, accounts, events, sync protocol pages, primitives, system, tables.
|
|
75
|
+
|
|
76
|
+
Snapshots:
|
|
77
|
+
|
|
78
|
+
- `runSnapshotLoop` — periodic `pg_dump` of synced state.
|
|
79
|
+
- `createSnapshot` — single-shot snapshot helper.
|
|
80
|
+
- `SnapshotConfig`, `SnapshotRetentionConfig` — config types.
|
|
81
|
+
|
|
82
|
+
Dynamic table / event helpers:
|
|
83
|
+
|
|
84
|
+
- `createDynamicTables` — register tables a primitive wants to own.
|
|
85
|
+
- `createIndexesForEvents`, `registerEventHandlers` — register pgtyped indexes for app events.
|
|
86
|
+
|
|
87
|
+
Subpath entry points (executable scripts):
|
|
88
|
+
|
|
89
|
+
- `@effectstream/db/start-pglite` — boot a PgLite instance.
|
|
90
|
+
- `@effectstream/db/apply-migrations` — apply SQL migrations against the active DB.
|
|
91
|
+
- `@effectstream/db/db-wait` — block until the DB accepts a connection.
|
|
92
|
+
- `@effectstream/db/pgtyped-update` — regenerate pgtyped types.
|
|
93
|
+
- `@effectstream/db/version` — print schema version.
|
|
94
|
+
|
|
95
|
+
## Examples
|
|
96
|
+
|
|
97
|
+
- Real connection round-trip: [`src/pg-connection.test.ts`](./src/pg-connection.test.ts).
|
|
98
|
+
- Runnable: [`test/examples.test.ts`](./test/examples.test.ts).
|
|
99
|
+
|
|
100
|
+
## Links
|
|
101
|
+
|
|
102
|
+
- Docs: https://effectstream.github.io/docs/packages/node/db
|
|
103
|
+
- Source: https://github.com/PaimaStudios/paima-engine/tree/main/packages/node-sdk/db
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effectstream/db",
|
|
3
|
-
"version": "0.100.
|
|
3
|
+
"version": "0.100.15",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"homepage": "https://effectstream.github.io/docs/",
|
|
6
6
|
"repository": {
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
"./version": "./migrations/system-version.ts"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@effectstream/coroutine": "0.100.
|
|
25
|
-
"@effectstream/log": "0.100.
|
|
26
|
-
"@effectstream/runtime": "0.100.
|
|
27
|
-
"@effectstream/sync": "0.100.
|
|
28
|
-
"@effectstream/utils": "0.100.
|
|
24
|
+
"@effectstream/coroutine": "0.100.15",
|
|
25
|
+
"@effectstream/log": "0.100.15",
|
|
26
|
+
"@effectstream/runtime": "0.100.15",
|
|
27
|
+
"@effectstream/sync": "0.100.15",
|
|
28
|
+
"@effectstream/utils": "0.100.15",
|
|
29
29
|
"@electric-sql/pglite": "^0.3.14",
|
|
30
30
|
"@pgtyped/runtime": "2.4.2",
|
|
31
31
|
"@pgtyped/parser": "2.4.2",
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Examples for the README — verify the public surface resolves without
|
|
2
|
+
// touching an actual Postgres or PgLite instance.
|
|
3
|
+
|
|
4
|
+
import { test, expect } from "bun:test";
|
|
5
|
+
import * as db from "../src/mod.ts";
|
|
6
|
+
|
|
7
|
+
test("README: connection helpers are exported", () => {
|
|
8
|
+
expect(typeof db.getConnection).toBe("function");
|
|
9
|
+
expect(typeof db.getPersistentConnection).toBe("function");
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("README: PgLite mutex helpers are exported", () => {
|
|
13
|
+
expect(typeof db.acquireDBMutex).toBe("function");
|
|
14
|
+
expect(typeof db.releaseDBMutex).toBe("function");
|
|
15
|
+
expect(typeof db.waitUntilFree).toBe("function");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("README: snapshot helpers are exported", () => {
|
|
19
|
+
expect(typeof db.createSnapshot).toBe("function");
|
|
20
|
+
expect(typeof db.runSnapshotLoop).toBe("function");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("README: pgtyped query objects are exported", () => {
|
|
24
|
+
expect(db.getBlockHeights).toBeDefined();
|
|
25
|
+
expect(db.getBlockByHash).toBeDefined();
|
|
26
|
+
expect(db.getLatestProcessedBlockHeight).toBeDefined();
|
|
27
|
+
});
|