schema.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {
  2. pgTable,
  3. varchar,
  4. text,
  5. integer,
  6. decimal,
  7. timestamp,
  8. pgEnum,
  9. jsonb,
  10. } from "drizzle-orm/pg-core";
  11. // Enums
  12. export const customerStatusEnum = pgEnum("customer_status", [
  13. "active",
  14. "inactive",
  15. ]);
  16. export const invoiceStatusEnum = pgEnum("invoice_status", [
  17. "draft",
  18. "sent",
  19. "paid",
  20. "overdue",
  21. ]);
  22. export const expenseStatusEnum = pgEnum("expense_status", [
  23. "pending",
  24. "approved",
  25. "rejected",
  26. ]);
  27. export const accountTypeEnum = pgEnum("account_type", [
  28. "bank",
  29. "credit_card",
  30. "cash",
  31. ]);
  32. export const transactionTypeEnum = pgEnum("transaction_type", [
  33. "income",
  34. "expense",
  35. "transfer",
  36. ]);
  37. // Tables
  38. export const customers = pgTable("customers", {
  39. id: varchar("id", { length: 50 }).primaryKey(),
  40. name: varchar("name", { length: 255 }).notNull(),
  41. email: varchar("email", { length: 255 }).notNull(),
  42. phone: varchar("phone", { length: 50 }),
  43. balance: decimal("balance", { precision: 12, scale: 2 })
  44. .notNull()
  45. .default("0"),
  46. status: customerStatusEnum("status").notNull().default("active"),
  47. createdAt: timestamp("created_at").notNull().defaultNow(),
  48. });
  49. export const invoices = pgTable("invoices", {
  50. id: varchar("id", { length: 50 }).primaryKey(),
  51. customerId: varchar("customer_id", { length: 50 })
  52. .notNull()
  53. .references(() => customers.id),
  54. customerName: varchar("customer_name", { length: 255 }).notNull(),
  55. amount: decimal("amount", { precision: 12, scale: 2 }).notNull(),
  56. status: invoiceStatusEnum("status").notNull().default("draft"),
  57. dueDate: timestamp("due_date").notNull(),
  58. createdAt: timestamp("created_at").notNull().defaultNow(),
  59. items: jsonb("items").$type<InvoiceItem[]>().notNull().default([]),
  60. });
  61. export const expenses = pgTable("expenses", {
  62. id: varchar("id", { length: 50 }).primaryKey(),
  63. vendor: varchar("vendor", { length: 255 }).notNull(),
  64. category: varchar("category", { length: 100 }).notNull(),
  65. amount: decimal("amount", { precision: 12, scale: 2 }).notNull(),
  66. date: timestamp("date").notNull(),
  67. status: expenseStatusEnum("status").notNull().default("pending"),
  68. description: text("description"),
  69. });
  70. export const accounts = pgTable("accounts", {
  71. id: varchar("id", { length: 50 }).primaryKey(),
  72. name: varchar("name", { length: 255 }).notNull(),
  73. type: accountTypeEnum("type").notNull(),
  74. balance: decimal("balance", { precision: 12, scale: 2 }).notNull(),
  75. lastSync: timestamp("last_sync").notNull().defaultNow(),
  76. });
  77. export const transactions = pgTable("transactions", {
  78. id: varchar("id", { length: 50 }).primaryKey(),
  79. accountId: varchar("account_id", { length: 50 })
  80. .notNull()
  81. .references(() => accounts.id),
  82. type: transactionTypeEnum("type").notNull(),
  83. amount: decimal("amount", { precision: 12, scale: 2 }).notNull(),
  84. description: text("description").notNull(),
  85. category: varchar("category", { length: 100 }).notNull(),
  86. date: timestamp("date").notNull(),
  87. });
  88. export const widgets = pgTable("widgets", {
  89. id: varchar("id", { length: 50 }).primaryKey(),
  90. prompt: text("prompt").notNull(),
  91. spec: jsonb("spec").$type<WidgetSpec>().notNull(),
  92. order: integer("order").notNull().default(0),
  93. createdAt: timestamp("created_at").notNull().defaultNow(),
  94. updatedAt: timestamp("updated_at").notNull().defaultNow(),
  95. });
  96. // Types
  97. export interface WidgetSpec {
  98. root: string;
  99. elements: Record<string, unknown>;
  100. }
  101. export interface InvoiceItem {
  102. description: string;
  103. quantity: number;
  104. rate: number;
  105. amount: number;
  106. }
  107. export type Customer = typeof customers.$inferSelect;
  108. export type NewCustomer = typeof customers.$inferInsert;
  109. export type Invoice = typeof invoices.$inferSelect;
  110. export type NewInvoice = typeof invoices.$inferInsert;
  111. export type Expense = typeof expenses.$inferSelect;
  112. export type NewExpense = typeof expenses.$inferInsert;
  113. export type Account = typeof accounts.$inferSelect;
  114. export type Transaction = typeof transactions.$inferSelect;
  115. export type Widget = typeof widgets.$inferSelect;
  116. export type NewWidget = typeof widgets.$inferInsert;