peta-orm 0.2.2 → 0.2.3

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.
Files changed (2) hide show
  1. package/README.md +49 -6
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -23,6 +23,7 @@ bun add -d kysely-bun-sqlite
23
23
  // db.ts
24
24
  import { Database } from "bun:sqlite"
25
25
  import { BunSqliteDialect } from "kysely-bun-sqlite"
26
+ import type { ColumnShape } from "peta-orm"
26
27
  import { Peta, $t, ArkTypeSchemaConfig, Model, HasMany } from "peta-orm"
27
28
 
28
29
  const t = $t({ schema: new ArkTypeSchemaConfig() })
@@ -32,8 +33,8 @@ class User extends Model {
32
33
  static override columns = {
33
34
  id: t.integer().primaryKey(),
34
35
  name: t.string(255).min(2),
35
- email: t.text().email(),
36
- }
36
+ email: t.text().email().unique(),
37
+ } satisfies ColumnShape
37
38
  static override relations = {
38
39
  posts: new HasMany(() => Post),
39
40
  }
@@ -43,9 +44,9 @@ class Post extends Model {
43
44
  static override table = "posts"
44
45
  static override columns = {
45
46
  id: t.integer().primaryKey(),
46
- userId: t.integer(),
47
+ userId: t.integer().references(() => User, ["id"]),
47
48
  title: t.string(255),
48
- }
49
+ } satisfies ColumnShape
49
50
  }
50
51
 
51
52
  const database = new Database("my-app.db")
@@ -88,18 +89,30 @@ export { peta, User, Post }
88
89
  ### Column Types & Validation
89
90
 
90
91
  ```ts
92
+ import type { ColumnShape } from "peta-orm"
93
+
91
94
  const t = $t({ schema: new ArkTypeSchemaConfig() })
92
95
 
93
96
  class User extends Model {
94
97
  static override columns = {
95
98
  id: t.integer().primaryKey(),
96
99
  name: t.string(255).min(2), // min length
97
- email: t.text().email(), // email format
100
+ email: t.text().email().unique(), // email format + unique constraint
98
101
  age: t.integer().nullable().min(0).max(150).default(0),
99
102
  role: t.enum("admin", "user").default("user"),
100
103
  score: t.double().nullable(),
101
104
  ...t.timestamps(), // createdAt, updatedAt
102
- }
105
+ } satisfies ColumnShape
106
+ }
107
+
108
+ class Post extends Model {
109
+ static override columns = {
110
+ id: t.integer().primaryKey(),
111
+ userId: t.integer().references(() => User, ["id"]), // foreign key
112
+ title: t.string(255),
113
+ slug: t.string().unique(),
114
+ published: t.boolean().default(false),
115
+ } satisfies ColumnShape
103
116
  }
104
117
  ```
105
118
 
@@ -132,6 +145,36 @@ const authors = await User.query().has("posts").execute()
132
145
  const active = await User.query().whereHas("posts", (q) => q.where("published", true)).execute()
133
146
  ```
134
147
 
148
+ ### ManyToMany
149
+
150
+ ```ts
151
+ class Post extends Model {
152
+ static override columns = { id: t.integer().primaryKey(), title: t.string(255) } satisfies ColumnShape
153
+ static override relations = {
154
+ tags: new ManyToMany(() => Tag, {
155
+ through: "post_tags",
156
+ foreignPivotKey: "postId",
157
+ relatedPivotKey: "tagId",
158
+ }),
159
+ }
160
+ }
161
+
162
+ class Tag extends Model {
163
+ static override columns = { id: t.integer().primaryKey(), name: t.string(255) } satisfies ColumnShape
164
+ }
165
+
166
+ // Pivot tables are regular Models — register them so the migration
167
+ // generator includes the pivot table automatically.
168
+ class PostTag extends Model {
169
+ static override table = "post_tags"
170
+ static override columns = {
171
+ id: t.integer().primaryKey(),
172
+ postId: t.integer().references(() => Post, ["id"]),
173
+ tagId: t.integer().references(() => Tag, ["id"]),
174
+ } satisfies ColumnShape
175
+ }
176
+ ```
177
+
135
178
  ### CRUD & Pagination
136
179
 
137
180
  ```ts
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "peta-orm",
3
3
  "type": "module",
4
4
  "private": false,
5
- "version": "0.2.2",
5
+ "version": "0.2.3",
6
6
  "description": "ORM for Bun, built on Kysely",
7
7
  "license": "MIT",
8
8
  "module": "src/index.ts",