peta-orm 0.2.1 → 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.
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(),
|
|
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
|
|
@@ -2159,6 +2159,15 @@ ${downTables.join(`
|
|
|
2159
2159
|
if (col.isUnique && !col.isPrimaryKey) {
|
|
2160
2160
|
calls.push("unique()");
|
|
2161
2161
|
}
|
|
2162
|
+
const refConstraint = col.constraints.find((c) => c.type === "references");
|
|
2163
|
+
if (refConstraint?.args[0]) {
|
|
2164
|
+
const targetClass = typeof refConstraint.args[0] === "function" ? refConstraint.args[0]() : refConstraint.args[0];
|
|
2165
|
+
const targetTable = targetClass?.table;
|
|
2166
|
+
const targetColumns = refConstraint.args[1];
|
|
2167
|
+
if (typeof targetTable === "string" && targetTable && targetColumns?.length) {
|
|
2168
|
+
calls.push(`references("${targetTable}.${targetColumns[0]}")`);
|
|
2169
|
+
}
|
|
2170
|
+
}
|
|
2162
2171
|
if (calls.length === 0)
|
|
2163
2172
|
return "";
|
|
2164
2173
|
const str = calls.join(".");
|
package/dist/migrations/cli.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../src/migrations/generator.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAEhD,MAAM,WAAW,gBAAgB;IAC/B,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED,qBAAa,kBAAkB;;IAC7B,wBAAwB,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,EAAE,OAAO,GAAE,gBAAqB,GAAG,MAAM;
|
|
1
|
+
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../src/migrations/generator.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAEhD,MAAM,WAAW,gBAAgB;IAC/B,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED,qBAAa,kBAAkB;;IAC7B,wBAAwB,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,EAAE,OAAO,GAAE,gBAAqB,GAAG,MAAM;CAqIlG"}
|
package/dist/migrations/index.js
CHANGED