peta-orm 0.4.0 → 0.5.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
@@ -17,28 +17,70 @@ const page = await Post.query().with("author").orderBy("id", "asc").paginate(1,
17
17
  ## Quick Start
18
18
 
19
19
  ```bash
20
- bun add peta-orm arktype kysely
21
- bun add -d kysely-bun-sqlite
20
+ bun add peta-orm arktype kysely @libsql/kysely-libsql @libsql/client
22
21
  ```
23
22
 
23
+ ### Simple setup (examples, scripts)
24
+
24
25
  ```ts
25
- import { Database } from "bun:sqlite"
26
- import { BunSqliteDialect } from "kysely-bun-sqlite"
26
+ import { createClient } from "@libsql/client"
27
+ import { LibsqlDialect } from "@libsql/kysely-libsql"
27
28
  import { createORM, defineModel, t } from "peta-orm"
28
29
 
29
- const orm = createORM({
30
- dialect: new BunSqliteDialect({ database: new Database("my-app.db") }),
30
+ const User = defineModel("users", {
31
+ columns: { id: t.integer().primaryKey(), name: t.string(255), email: t.text().unique() },
31
32
  })
32
33
 
34
+ // Eager init — fine for scripts, one-off tasks
35
+ const client = createClient({ url: "file::memory:?cache=shared" })
36
+ await client.execute(
37
+ "CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE)",
38
+ )
39
+
40
+ const orm = createORM({ dialect: new LibsqlDialect({ client }), models: { User } })
41
+
42
+ const user = await User.insert({ name: "Alice", email: "alice@test.com" })
43
+ ```
44
+
45
+ ### Production setup (apps, servers) — no module-level side effects
46
+
47
+ Module-level side effects (database connections, schema init, ORM setup at import time) cause problems with testing, HMR, and error recovery. Use `createDb()` for lazy, safe initialization:
48
+
49
+ ```ts
50
+ import { createClient } from "@libsql/client"
51
+ import { LibsqlDialect } from "@libsql/kysely-libsql"
52
+ import { createDb, createORM, defineModel, t } from "peta-orm"
53
+
33
54
  const User = defineModel("users", {
34
55
  columns: { id: t.integer().primaryKey(), name: t.string(255), email: t.text().unique() },
35
56
  })
36
57
 
37
- orm.registerAll(User)
58
+ async function setup() {
59
+ const client = createClient({ url: "file:my-app.db" })
60
+ await client.execute(
61
+ "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE)",
62
+ )
63
+ const orm = createORM({ dialect: new LibsqlDialect({ client }) })
64
+ orm.registerAll(User)
65
+ return orm
66
+ }
38
67
 
39
- const user = await User.insert({ name: "Alice", email: "alice@test.com" })
68
+ /** Lazy singleton first call creates the connection, subsequent calls reuse it. */
69
+ export const db = createDb(setup)
70
+
71
+ // In route handlers:
72
+ // const orm = await db()
73
+ // const users = await User.query().execute()
40
74
  ```
41
75
 
76
+ The factory function runs **once** on the first `await db()` call. Importing models has zero side effects — no connection, no schema init, no unhandled promises.
77
+
78
+ > [!TIP]
79
+ > For an existing Kysely instance (e.g. from a migration runner), pass it via the `kysely` config option:
80
+ > ```ts
81
+ > const orm = createORM({ kysely: existingKysely })
82
+ > ```
83
+
42
84
  > [!TIP]
43
85
  > See the 32 [runnable examples](./examples) for every feature. Run them with `bun run examples/XX-*.ts`.
44
86
 
@@ -71,7 +113,6 @@ const user = await User.insert({ name: "Alice", email: "alice@test.com" })
71
113
  Column definitions double as validation schemas — no separate validation step needed.
72
114
 
73
115
  ```ts
74
- const t = columnTypes({ schema: createArkTypeSchemaConfig() })
75
116
 
76
117
  const User = defineModel("users", {
77
118
  columns: {
@@ -343,7 +384,7 @@ bun run examples/07-soft-deletes.ts
343
384
 
344
385
  | Database | Dialect package | Status |
345
386
  |----------|----------------|--------|
346
- | SQLite | `kysely-bun-sqlite` | ✅ Tested |
387
+ | SQLite | `@libsql/kysely-libsql` + `@libsql/client` | ✅ Tested |
347
388
  | PostgreSQL | `pg` | ✅ Tested via Docker |
348
389
  | MySQL | `mysql2` | ✅ Tested via Docker |
349
390