-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.ts
59 lines (52 loc) · 1.71 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
import { relations } from 'drizzle-orm'
import { integer, pgTable, text, timestamp } from 'drizzle-orm/pg-core'
import { createInsertSchema } from 'drizzle-zod'
import { z } from 'zod'
export const accounts = pgTable('accounts', {
id: text('id').primaryKey(),
plaidId: text('plaid_id'),
name: text('name').notNull(),
userId: text('user_id').notNull()
})
export const accountsRelations = relations(accounts, ({ many }) => ({
transactions: many(transactions)
}))
export const insertAccountsSchema = createInsertSchema(accounts)
export const categories = pgTable('categories', {
id: text('id').primaryKey(),
plaidId: text('plaid_id'),
name: text('name').notNull(),
userId: text('user_id').notNull()
})
export const categoriesRelations = relations(categories, ({ many }) => ({
transactions: many(transactions)
}))
export const insertCategorySchema = createInsertSchema(categories)
export const transactions = pgTable('transactions', {
id: text('id').primaryKey(),
amount: integer('amount').notNull(),
payee: text('payee').notNull(),
notes: text('notes'),
date: timestamp('date', { mode: 'date' }).notNull(),
accountId: text('account_id')
.references(() => accounts.id, {
onDelete: 'cascade'
})
.notNull(),
categoryId: text('category_id').references(() => categories.id, {
onDelete: 'set null'
})
})
export const transactionsRelations = relations(transactions, ({ one }) => ({
accounts: one(accounts, {
fields: [transactions.accountId],
references: [accounts.id]
}),
categories: one(categories, {
fields: [transactions.categoryId],
references: [categories.id]
})
}))
export const insertTransactionSchema = createInsertSchema(transactions, {
date: z.coerce.date()
})