@playfast/reform-db-sqlite 1.0.1 → 1.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 CHANGED
@@ -44,9 +44,7 @@ const DataLayer = QueriesAndProcedures.pipe(
44
44
  )
45
45
 
46
46
  // Ephemeral in-memory — the default for tests / proofs:
47
- const TestLayer = QueriesAndProcedures.pipe(
48
- Layer.provideMerge(Sqlite.memory({ migrations })),
49
- )
47
+ const TestLayer = QueriesAndProcedures.pipe(Layer.provideMerge(Sqlite.memory({ migrations })))
50
48
  ```
51
49
 
52
50
  Both layers build the `Db` service **synchronously** (bun:sqlite opens on a background
package/package.json CHANGED
@@ -1,55 +1,55 @@
1
1
  {
2
2
  "name": "@playfast/reform-db-sqlite",
3
- "playbook": "./playbook",
4
- "version": "1.0.1",
5
- "type": "module",
3
+ "version": "1.1.0",
6
4
  "description": "bun:sqlite engine for @playfast/reform-db — a Db driver backed by Bun's built-in SQLite, with emulated reactivity and serialize()/deserialize() snapshots.",
7
5
  "keywords": [
8
- "reform",
9
- "effect",
10
- "sqlite",
11
6
  "bun",
12
- "sql",
7
+ "effect",
8
+ "local-first",
13
9
  "reactive",
14
- "local-first"
10
+ "reform",
11
+ "sql",
12
+ "sqlite"
15
13
  ],
14
+ "bugs": {
15
+ "url": "https://github.com/playfast/reform/issues"
16
+ },
16
17
  "license": "MIT",
17
18
  "repository": {
18
19
  "type": "git",
19
20
  "url": "https://github.com/playfast/reform.git",
20
21
  "directory": "packages/reform-db-sqlite"
21
22
  },
22
- "bugs": {
23
- "url": "https://github.com/playfast/reform/issues"
24
- },
23
+ "files": [
24
+ "src",
25
+ "README.md"
26
+ ],
27
+ "type": "module",
25
28
  "sideEffects": false,
26
29
  "exports": {
27
30
  "./package.json": "./package.json",
28
31
  ".": "./src/index.ts",
29
32
  "./*": "./src/*.ts"
30
33
  },
31
- "files": [
32
- "src",
33
- "README.md"
34
- ],
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
35
37
  "scripts": {
36
38
  "clean": "rm -rf dist .tsbuildinfo",
37
39
  "check": "tsc --noEmit",
38
40
  "build": "tsc -p tsconfig.build.json",
39
- "test": "bun test",
41
+ "test": "bun --bun run vitest run --config vitest.config.ts",
40
42
  "lint": "oxlint src",
41
43
  "lint:fix": "oxlint --fix src"
42
44
  },
43
- "peerDependencies": {
44
- "effect": "*",
45
- "kysely": "*",
46
- "@playfast/reform": "*",
47
- "@playfast/reform-db": "*"
48
- },
49
45
  "devDependencies": {
50
46
  "@types/bun": "^1.3.14"
51
47
  },
52
- "publishConfig": {
53
- "access": "public"
54
- }
48
+ "peerDependencies": {
49
+ "@playfast/reform": "*",
50
+ "@playfast/reform-db": "*",
51
+ "effect": "*",
52
+ "kysely": "*"
53
+ },
54
+ "playbook": "./playbook"
55
55
  }
package/src/engine.ts CHANGED
@@ -1,38 +1,15 @@
1
1
  import { type DbEngine, DbError, type EngineBackend, type Migration } from '@playfast/reform-db'
2
2
  import { Effect, Option } from 'effect'
3
3
  import { createQueryId, type RootOperationNode, SqliteQueryCompiler } from 'kysely'
4
- // Type-only: erased at build, so this package imports cleanly on Node (where the
5
- // `bun:sqlite` module does not exist); the real module is imported lazily at open time.
4
+ // Type-only: erased at build so package imports cleanly on Node (bun:sqlite absent).
6
5
  import type { Database } from 'bun:sqlite'
7
6
 
8
- // The bun:sqlite engine: Bun's built-in SQLite. It compiles operation nodes to SQLite
9
- // SQL, opens a `Database` lazily (so importing this package never touches the
10
- // Bun-only `bun:sqlite` module on Node), and runs migrations. SQLite has NO native
11
- // live queries, so `live` is `Option.none` — `makeDriver` emulates reactivity by
12
- // re-running reads after each write. `dump` uses `Database.serialize()` (a full
13
- // on-the-wire snapshot Buffer), restored via `Database.deserialize` on the next open.
14
-
15
- // A value SQLite can bind (bun:sqlite's `SQLQueryBindings`). Booleans ride as `0`/`1`,
16
- // JSON columns as text — matching the engine-tolerant column `Schema`s in
17
- // `@playfast/reform-db` that decode either shape. `ExternalApi`-named: it mirrors the
18
- // driver's own binding shape, which admits a raw `null`.
19
7
  type BindingExternalApi = string | number | bigint | boolean | Uint8Array | null
20
8
 
21
9
  export interface SqliteOptionsExternalApi {
22
- /** Database file path; omit for an ephemeral in-memory (`:memory:`) database. */
23
10
  readonly filename?: string
24
- /**
25
- * DDL/seed migrations to run on open. Use `Migration.fromSchema(AppDb, Migration.sqlite)`
26
- * so column types render for SQLite; `Sqlite.layer`/`Sqlite.memory` do NOT pick the
27
- * dialect for you.
28
- */
11
+ // Use Migration.fromSchema(AppDb, Migration.sqlite) — layer does not pick dialect.
29
12
  readonly migrations?: ReadonlyArray<Migration.Migration>
30
- /**
31
- * Seed the database from a previous `dump` snapshot (`Database.serialize()` bytes,
32
- * wrapped in a `Blob`). When present the database is opened via `Database.deserialize`
33
- * — an in-memory database restored from the snapshot — before migrations run
34
- * (`CREATE TABLE IF NOT EXISTS` then no-ops). `filename` is ignored in this case.
35
- */
36
13
  readonly loadDataDir?: Blob
37
14
  }
38
15
 
@@ -52,8 +29,7 @@ const bindable = (candidate: unknown): candidate is BindingExternalApi =>
52
29
  typeof candidate === 'bigint' ||
53
30
  typeof candidate === 'boolean'
54
31
 
55
- // SQLite binds strings/numbers/bigints/booleans/blobs/null directly and throws on any
56
- // other object — so a json-column value (an object/array) is serialized to JSON text.
32
+ // SQLite throws on object binds serialize json-column objects/arrays to text.
57
33
  const coerceParam = (param: unknown): BindingExternalApi => {
58
34
  if (param === undefined || param === null) {
59
35
  return null
@@ -62,18 +38,16 @@ const coerceParam = (param: unknown): BindingExternalApi => {
62
38
  return bindable(param) ? param : JSON.stringify(param)
63
39
  }
64
40
 
65
- // Open the database (loading the Bun-only module lazily) and run migrations. Though
66
- // bun:sqlite is synchronous, this is the ASYNC-shaped work `makeDriver` defers to a
67
- // background fiber so the `Db` service can be acquired SYNCHRONOUSLY.
41
+ // Lazy bun:sqlite import + migrations: async-shaped so makeDriver acquires Db sync.
68
42
  const openSqlite = Effect.fn('openSqlite')(function* (
69
43
  options: SqliteOptions,
70
44
  ): Effect.fn.Return<Database, DbError> {
71
45
  const { Database } = yield* Effect.tryPromise({
72
46
  try: () => import('bun:sqlite'),
73
- catch: (cause) => new DbError({ reason: 'bun:sqlite unavailable (Bun runtime required)', cause }),
47
+ catch: (cause) =>
48
+ new DbError({ reason: 'bun:sqlite unavailable (Bun runtime required)', cause }),
74
49
  })
75
- // `Database.deserialize` wants the snapshot bytes synchronously; the `Blob` is already
76
- // fully in memory (it came from a `dump`), so read it async here, then open sync below.
50
+ // deserialize wants bytes sync; Blob is already in memory — read async then open sync.
77
51
  const seed = yield* Option.match(Option.fromNullable(options.loadDataDir), {
78
52
  onNone: () => Effect.succeed(Option.none<Uint8Array>()),
79
53
  onSome: (blob) =>
@@ -86,7 +60,6 @@ const openSqlite = Effect.fn('openSqlite')(function* (
86
60
  try: () =>
87
61
  Option.match(seed, {
88
62
  onNone: () => new Database(options.filename ?? ':memory:'),
89
- // A snapshot restore is an in-memory database seeded from the serialized bytes.
90
63
  onSome: (bytes) => Database.deserialize(bytes),
91
64
  }),
92
65
  catch: (cause) => new DbError({ reason: 'open failed', cause }),
@@ -112,10 +85,8 @@ const backendOf = (db: Database): EngineBackend => ({
112
85
  try: () => db.query(sql).all(...parameters.map(coerceParam)),
113
86
  catch: (cause) => new DbError({ reason: 'query failed', sql, cause }),
114
87
  }),
115
- // No native reactivity: `makeDriver` re-runs live queries after each write.
88
+ // No native live makeDriver emulates by re-running after write.
116
89
  live: Option.none(),
117
- // `serialize()` returns the full database as a Buffer; wrap it as a portable Blob so
118
- // it round-trips through the same dump→save→reload contract as PGlite's tarball.
119
90
  dump: Effect.try({
120
91
  try: () => new Blob([new Uint8Array(db.serialize())]),
121
92
  catch: (cause) => new DbError({ reason: 'dump failed', cause }),
@@ -123,7 +94,6 @@ const backendOf = (db: Database): EngineBackend => ({
123
94
  close: Effect.sync(() => db.close()),
124
95
  })
125
96
 
126
- /** The bun:sqlite `DbEngine`. Feed to `makeDriver` (see `Sqlite.layer`/`Sqlite.memory`). */
127
97
  export const make = (options: SqliteOptions): DbEngine => ({
128
98
  compile,
129
99
  open: () => openSqlite(options).pipe(Effect.map(backendOf)),
package/src/index.ts CHANGED
@@ -1,21 +1,4 @@
1
- // @playfast/reform-db-sqlite — the bun:sqlite engine for @playfast/reform-db.
2
- //
3
- // Provides the `Db` driver layer backed by Bun's built-in SQLite (`bun:sqlite`):
4
- // synchronous, dependency-free, with emulated reactivity (live `DbQuery`s re-run
5
- // after each write) and `serialize()`/`deserialize()` snapshots. Pair it with the
6
- // engine-neutral primitives (`DbColumn`/`DbTable`/`DbSchema`/`DbQuery`/`Migration`)
7
- // from `@playfast/reform-db` — remember the SQLite dialect for migrations:
8
- //
9
- // import { Sqlite } from '@playfast/reform-db-sqlite'
10
- // import { Migration } from '@playfast/reform-db'
11
- // const DataLayer = QLive.pipe(
12
- // Layer.provideMerge(
13
- // Sqlite.layer({ filename: 'app.db', migrations: Migration.fromSchema(AppDb, Migration.sqlite) }),
14
- // ),
15
- // )
16
-
17
1
  export * as Sqlite from './sqlite'
18
2
  export type { MemoryOptions } from './sqlite'
19
3
  export type { SqliteOptions } from './engine'
20
- // The engine itself, for advanced callers wiring `makeDriver` directly.
21
4
  export { make as makeSqliteEngine } from './engine'
@@ -1,8 +1,4 @@
1
- // Bun-runtime integration test for the bun:sqlite engine. It uses Bun's own test
2
- // runner (`bun:test`) because `bun:sqlite` only exists under Bun — and vitest runs its
3
- // workers in Node, where that module is absent. Run it with `bun test` (the package's
4
- // `test` script); vitest's `*.test.ts` glob does not match `.spec.ts`, so the
5
- // workspace `vitest run` ignores it.
1
+ // Bun-only: bun:sqlite is absent under Node. Run with Vitest on Bun.
6
2
  import {
7
3
  Db,
8
4
  DbColumn,
@@ -12,15 +8,12 @@ import {
12
8
  Migration,
13
9
  type QueryData,
14
10
  } from '@playfast/reform-db'
15
- import { expect, test } from 'bun:test'
11
+ import { expect, it } from '@effect/vitest'
16
12
  import { Context, Effect, Exit, Layer, Schedule, Schema, Scope } from 'effect'
17
13
  import { Sqlite } from './index'
18
14
 
19
15
  const Meta = Schema.Struct({ note: Schema.String })
20
16
 
21
- // A table covering the type shapes SQLite stores differently from Postgres: a boolean
22
- // (stored 0/1) and a json column (stored as text). The engine-tolerant column Schemas
23
- // in @playfast/reform-db must still decode them back to `boolean` / object.
24
17
  const Items = DbTable.make('items', {
25
18
  columns: {
26
19
  id: DbColumn.text({ primaryKey: true }),
@@ -50,7 +43,10 @@ const rowsOf = (data: QueryData<Item>): ReadonlyArray<Item> =>
50
43
 
51
44
  const waitUntil = (predicate: () => boolean) =>
52
45
  Effect.sync(predicate).pipe(
53
- Effect.repeat({ until: (ready) => ready, schedule: Schedule.spaced('20 millis') }),
46
+ Effect.repeat({
47
+ until: (ready) => ready,
48
+ schedule: Schedule.spaced('20 millis'),
49
+ }),
54
50
  Effect.timeout('5 seconds'),
55
51
  )
56
52
 
@@ -61,11 +57,10 @@ const liveLayer = (dataLayer: Layer.Layer<Db>) =>
61
57
  build: (_inputs, db) => db.selectFrom('items').selectAll().orderBy('rank'),
62
58
  }).pipe(Layer.provideMerge(dataLayer))
63
59
 
64
- test('DbQuery reflects writes (emulated reactivity), decoding boolean + json columns', async () => {
60
+ it.live('DbQuery reflects writes (emulated reactivity), decoding boolean + json columns', () => {
65
61
  const program = Effect.gen(function* () {
66
62
  const store = yield* ItemsQuery.store
67
63
 
68
- // The table exists but is empty; the emulated live query resolves to empty Success.
69
64
  yield* waitUntil(() => store.getSnapshot()._tag === 'Success')
70
65
  expect(rowsOf(store.getSnapshot())).toEqual([])
71
66
 
@@ -75,36 +70,36 @@ test('DbQuery reflects writes (emulated reactivity), decoding boolean + json col
75
70
  yield* insert('b', 'second', 2, false, 'two')
76
71
  yield* insert('a', 'first', 1, true, 'one')
77
72
 
78
- // Each `exec` re-fires the emulated live query.
79
73
  yield* waitUntil(() => rowsOf(store.getSnapshot()).length === 2)
80
74
  const rows = rowsOf(store.getSnapshot())
81
75
  expect(rows.map((row) => row.label)).toEqual(['first', 'second'])
82
- // boolean decoded from SQLite's 0/1, json decoded from stored text.
83
76
  expect(rows.map((row) => row.done)).toEqual([true, false])
84
77
  expect(rows.map((row) => row.meta.note)).toEqual(['one', 'two'])
85
78
  }).pipe(Effect.provide(liveLayer(Sqlite.memory({ migrations }))))
86
79
 
87
- await Effect.runPromise(program)
80
+ return program
88
81
  })
89
82
 
90
- test('dump() → loadDataDir round-trips rows into a fresh in-memory db', async () => {
91
- const dump = await Effect.runPromise(
92
- Effect.scoped(
83
+ it.scopedLive('dump() → loadDataDir round-trips rows into a fresh in-memory db', () =>
84
+ Effect.gen(function* () {
85
+ const dump = yield* Effect.scoped(
93
86
  Effect.gen(function* () {
94
87
  const context = yield* Layer.build(Sqlite.memory({ migrations }))
95
88
  const driver = Context.get(context, Db)
96
89
  yield* Db.exec(
97
- AppDb.kysely
98
- .insertInto('items')
99
- .values({ id: 'a', label: 'first', rank: 1, done: true, meta: { note: 'one' } }),
90
+ AppDb.kysely.insertInto('items').values({
91
+ id: 'a',
92
+ label: 'first',
93
+ rank: 1,
94
+ done: true,
95
+ meta: { note: 'one' },
96
+ }),
100
97
  ).pipe(Effect.provideService(Db, driver))
101
98
  return yield* driver.dump()
102
99
  }),
103
- ),
104
- )
100
+ )
105
101
 
106
- await Effect.runPromise(
107
- Effect.scoped(
102
+ yield* Effect.scoped(
108
103
  Effect.gen(function* () {
109
104
  const store = yield* ItemsQuery.store
110
105
  yield* waitUntil(() => rowsOf(store.getSnapshot()).length === 1)
@@ -113,25 +108,23 @@ test('dump() → loadDataDir round-trips rows into a fresh in-memory db', async
113
108
  expect(row?.done).toBe(true)
114
109
  expect(row?.meta.note).toBe('one')
115
110
  }).pipe(Effect.provide(liveLayer(Sqlite.memory({ migrations, loadDataDir: dump })))),
116
- ),
117
- )
118
- })
119
-
120
- test('Sqlite.memory layer is sync-providable: store starts loading, then resolves to Success', async () => {
121
- const layer = liveLayer(Sqlite.memory({ migrations }))
122
-
123
- // Build the layer SYNCHRONOUSLY (bun:sqlite opens on a background fiber). This is the
124
- // path reform's `runSync` renderer/proof mount takes; it must not throw "Fiber cannot
125
- // be resolved synchronously".
126
- const scope = Effect.runSync(Scope.make())
127
- const context = Effect.runSync(Layer.buildWithScope(layer, scope))
128
- const store = Context.get(context, ItemsQuery.store)
129
-
130
- try {
131
- expect(store.getSnapshot()._tag).toBe('Loading')
132
- await Effect.runPromise(waitUntil(() => store.getSnapshot()._tag === 'Success'))
133
- expect(rowsOf(store.getSnapshot())).toEqual([])
134
- } finally {
135
- await Effect.runPromise(Scope.close(scope, Exit.void))
136
- }
137
- })
111
+ )
112
+ }),
113
+ )
114
+
115
+ it.scopedLive(
116
+ 'Sqlite.memory layer is sync-providable: store starts loading, then resolves to Success',
117
+ () =>
118
+ Effect.gen(function* () {
119
+ const layer = liveLayer(Sqlite.memory({ migrations }))
120
+
121
+ // Sync build (opens on background fiber) — must not throw "Fiber cannot be resolved synchronously".
122
+ const scope = Effect.runSync(Scope.make())
123
+ const context = Effect.runSync(Layer.buildWithScope(layer, scope))
124
+ yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void))
125
+ const store = Context.get(context, ItemsQuery.store)
126
+ expect(store.getSnapshot()._tag).toBe('Loading')
127
+ yield* waitUntil(() => store.getSnapshot()._tag === 'Success')
128
+ expect(rowsOf(store.getSnapshot())).toEqual([])
129
+ }),
130
+ )
package/src/sqlite.ts CHANGED
@@ -2,17 +2,6 @@ import { Db, makeDriver } from '@playfast/reform-db'
2
2
  import { Layer } from 'effect'
3
3
  import { make, type SqliteOptions } from './engine'
4
4
 
5
- // The bun:sqlite `Db` layers, built on `@playfast/reform-db`'s engine-neutral
6
- // `makeDriver` and the SQLite engine here. Both build the `Db` service synchronously
7
- // (bun:sqlite opens on a background fiber inside `makeDriver`), so they are providable
8
- // under reform's `runSync` scene/proof mount. Reactivity is emulated by the core:
9
- // SQLite has no native live queries, so live `DbQuery`s re-run after each write.
10
- //
11
- // NOTE: migrations must be rendered for SQLite — pass
12
- // `Migration.fromSchema(AppDb, Migration.sqlite)`.
13
-
14
- // Options for `Sqlite.memory` — an in-memory database can still be migrated and seeded
15
- // from a `dump` snapshot, so it accepts everything but `filename`.
16
5
  export interface MemoryOptionsExternalApi {
17
6
  readonly migrations?: SqliteOptions['migrations']
18
7
  readonly loadDataDir?: SqliteOptions['loadDataDir']
@@ -20,15 +9,13 @@ export interface MemoryOptionsExternalApi {
20
9
 
21
10
  export type MemoryOptions = MemoryOptionsExternalApi
22
11
 
23
- /** A persistent (or in-memory, if `filename` is omitted) bun:sqlite-backed `Db` driver. */
24
12
  export const layer = (options: SqliteOptions = {}): Layer.Layer<Db> =>
25
13
  Layer.scoped(Db, makeDriver(make(options)))
26
14
 
27
- /** An ephemeral in-memory bun:sqlite-backed `Db` driver — the default for tests/proofs. */
28
15
  export const memory = (options: MemoryOptions = {}): Layer.Layer<Db> =>
29
16
  Layer.scoped(
30
17
  Db,
31
- // Spread each option only when set so the bare `Sqlite.memory()` open stays unchanged.
18
+ // Spread only when set so bare `Sqlite.memory()` open stays unchanged.
32
19
  makeDriver(
33
20
  make({
34
21
  ...(options.migrations !== undefined ? { migrations: options.migrations } : {}),