Skip to content
Source
Sign inSign up
Open History to see the last commit

schema.ts

33 lines1005 Bytes

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