-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.ts
80 lines (72 loc) · 2.41 KB
/
schema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { relations, sql } from "drizzle-orm";
import { pgTable, primaryKey } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { z } from "zod";
export const Post = pgTable("post", (t) => ({
id: t.uuid().notNull().primaryKey().defaultRandom(),
title: t.varchar({ length: 256 }).notNull(),
content: t.text().notNull(),
createdAt: t.timestamp().defaultNow().notNull(),
updatedAt: t
.timestamp({ mode: "date", withTimezone: true })
.$onUpdateFn(() => sql`now()`),
}));
export const CreatePostSchema = createInsertSchema(Post, {
title: z.string().max(256),
content: z.string().max(256),
}).omit({
id: true,
createdAt: true,
updatedAt: true,
});
export const User = pgTable("user", (t) => ({
id: t.uuid().notNull().primaryKey().defaultRandom(),
name: t.varchar({ length: 255 }),
email: t.varchar({ length: 255 }).notNull(),
emailVerified: t.timestamp({ mode: "date", withTimezone: true }),
image: t.varchar({ length: 255 }),
}));
export const UserRelations = relations(User, ({ many }) => ({
accounts: many(Account),
}));
export const Account = pgTable(
"account",
(t) => ({
userId: t
.uuid()
.notNull()
.references(() => User.id, { onDelete: "cascade" }),
type: t
.varchar({ length: 255 })
.$type<"email" | "oauth" | "oidc" | "webauthn">()
.notNull(),
provider: t.varchar({ length: 255 }).notNull(),
providerAccountId: t.varchar({ length: 255 }).notNull(),
refresh_token: t.varchar({ length: 255 }),
access_token: t.text(),
expires_at: t.integer(),
token_type: t.varchar({ length: 255 }),
scope: t.varchar({ length: 255 }),
id_token: t.text(),
session_state: t.varchar({ length: 255 }),
}),
(account) => ({
compoundKey: primaryKey({
columns: [account.provider, account.providerAccountId],
}),
}),
);
export const AccountRelations = relations(Account, ({ one }) => ({
user: one(User, { fields: [Account.userId], references: [User.id] }),
}));
export const Session = pgTable("session", (t) => ({
sessionToken: t.varchar({ length: 255 }).notNull().primaryKey(),
userId: t
.uuid()
.notNull()
.references(() => User.id, { onDelete: "cascade" }),
expires: t.timestamp({ mode: "date", withTimezone: true }).notNull(),
}));
export const SessionRelations = relations(Session, ({ one }) => ({
user: one(User, { fields: [Session.userId], references: [User.id] }),
}));