1import { defineRelations } from "drizzle-orm";
2import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
3
4export const Users = sqliteTable("users", {
5 id: integer("id").primaryKey({ autoIncrement: true }),
6 email: text("email").notNull().unique(),
7 name: text("name").notNull(),
8});
9export type User = typeof Users.$inferSelect;
10
11export const Posts = sqliteTable("posts", {
12 id: integer("id").primaryKey({ autoIncrement: true }),
13 userId: integer("user_id")
14 .notNull()
15 .references(() => Users.id, { onDelete: "cascade" }),
16 title: text("title").notNull(),
17});
18export type Post = typeof Posts.$inferSelect;
19
20export const relations = defineRelations({ Users, Posts }, (t) => ({
21 Users: {
22 posts: t.many.Posts(),
23 },
24 Posts: {
25 user: t.one.Users({
26 from: t.Posts.userId,
27 to: t.Users.id,
28 }),
29 },
30}));