-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.ts
131 lines (118 loc) · 3.92 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { relations, sql } from "drizzle-orm";
import { primaryKey } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { z } from "zod";
import { pgTable } from "./_table";
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 }),
username: t.varchar({ length: 255 }).notNull(),
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),
services: many(CalendarService),
following: many(Follow),
}));
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] }),
}));
export const CalendarService = pgTable("calendar_service", (t) => ({
id: t.uuid().notNull().primaryKey().defaultRandom(),
userId: t
.uuid()
.notNull()
.references(() => User.id, { onDelete: "cascade" }),
type: t.varchar({ length: 255 }).notNull(),
credentials: t.varchar({ length: 255 }),
createdAt: t.timestamp().defaultNow().notNull(),
updatedAt: t
.timestamp({ mode: "date", withTimezone: true })
.$onUpdateFn(() => sql`now()`),
}));
export const ServiceRelations = relations(CalendarService, ({ one }) => ({
user: one(User, { fields: [CalendarService.userId], references: [User.id] }),
}));
export const Follow = pgTable(
"follow",
(t) => ({
userId: t
.uuid()
.notNull()
.references(() => User.id, { onDelete: "cascade" }),
followingId: t
.uuid()
.notNull()
.references(() => User.id, { onDelete: "cascade" }),
createdAt: t.timestamp().defaultNow().notNull(),
updatedAt: t
.timestamp({ mode: "date", withTimezone: true })
.$onUpdateFn(() => sql`now()`),
}),
(follow) => ({
compoundKey: primaryKey({
columns: [follow.userId, follow.followingId],
}),
}),
);
export const FollowRelations = relations(Follow, ({ one }) => ({
user: one(User, { fields: [Follow.userId], references: [User.id] }),
following: one(User, { fields: [Follow.followingId], references: [User.id] }),
}));