-
Notifications
You must be signed in to change notification settings - Fork 2
/
schema.ts
153 lines (146 loc) · 4.08 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {
pgTable,
pgEnum,
serial,
text,
uniqueIndex,
timestamp,
integer,
boolean
} from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';
export const user = pgTable('users', {
id: serial('id').primaryKey().notNull(),
supabase_uid: text('supabase_uid').notNull(),
email: text('email').notNull(),
display_name: text('display_name')
});
export const userRelations = relations(user, ({ many }) => ({
memberships: many(membership)
}));
export type User = typeof user.$inferSelect;
export enum ACCOUNT_ACCESS {
OWNER = 'OWNER',
ADMIN = 'ADMIN',
READ_WRITE = 'READ_WRITE',
READ_ONLY = 'READ_ONLY'
}
// I tried a few hacks to derive the pgEnum from the ts enum but in the end I gave up
export const accountAccessEnum = pgEnum('ACCOUNT_ACCESS', [
'OWNER',
'ADMIN',
'READ_WRITE',
'READ_ONLY'
]);
// Membership
export const membership = pgTable(
'membership',
{
id: serial('id').primaryKey().notNull(),
user_id: integer('user_id')
.notNull()
.references(() => user.id, { onDelete: 'restrict', onUpdate: 'cascade' }),
account_id: integer('account_id')
.notNull()
.references(() => account.id, {
onDelete: 'restrict',
onUpdate: 'cascade'
}),
access: accountAccessEnum('access')
.default(ACCOUNT_ACCESS.READ_ONLY)
.notNull(),
pending: boolean('pending').default(false).notNull()
},
table => {
return {
user_idAccountIdKey: uniqueIndex('membership_user_id_account_id_key').on(
table.user_id,
table.account_id
)
};
}
);
export const membershipRelations = relations(membership, ({ one }) => ({
user: one(user, {
fields: [membership.user_id],
references: [user.id]
}),
account: one(account, {
fields: [membership.account_id],
references: [account.id]
})
}));
export type Membership = typeof membership.$inferSelect;
// Account
export const account = pgTable(
'account',
{
id: serial('id').primaryKey().notNull(),
name: text('name').notNull(),
current_period_ends: timestamp('current_period_ends', {
precision: 3,
mode: 'date'
})
.defaultNow()
.notNull(),
features: text('features').array().notNull(),
plan_id: integer('plan_id')
.notNull()
.references(() => plan.id, { onDelete: 'restrict', onUpdate: 'cascade' }),
plan_name: text('plan_name').notNull(),
max_notes: integer('max_notes').default(100).notNull(),
stripe_subscription_id: text('stripe_subscription_id'),
stripe_customer_id: text('stripe_customer_id'),
max_members: integer('max_members').default(1).notNull(),
join_password: text('join_password').notNull(),
ai_gen_max_pm: integer('ai_gen_max_pm').default(7).notNull(),
ai_gen_count: integer('ai_gen_count').default(0).notNull()
},
table => {
return {
join_passwordKey: uniqueIndex('account_join_password_key').on(
table.join_password
)
};
}
);
export const accountRelations = relations(account, ({ many }) => ({
notes: many(note),
members: many(membership)
}));
export type Account = typeof account.$inferSelect;
// Plan
export const plan = pgTable(
'plan',
{
id: serial('id').primaryKey().notNull(),
name: text('name').notNull(),
features: text('features').array().notNull(),
max_notes: integer('max_notes').default(100).notNull(),
stripe_product_id: text('stripe_product_id'),
max_members: integer('max_members').default(1).notNull(),
ai_gen_max_pm: integer('ai_gen_max_pm').default(7).notNull()
},
table => {
return {
nameKey: uniqueIndex('plan_name_key').on(table.name)
};
}
);
export type Plan = typeof plan.$inferSelect;
// Note
export const note = pgTable('note', {
id: serial('id').primaryKey().notNull(),
account_id: integer('account_id').references(() => account.id, {
onDelete: 'set null',
onUpdate: 'cascade'
}),
note_text: text('note_text').notNull()
});
export const noteRelations = relations(note, ({ one }) => ({
account: one(account, {
fields: [note.account_id],
references: [account.id]
})
}));
export type Note = typeof note.$inferSelect;