pipedrive / src /lib /db /schema.ts
ppEmiliano's picture
Add full CRM application with HF Spaces Docker deployment
ea8dde3
import { sqliteTable, text, integer, real } from "drizzle-orm/sqlite-core";
export const users = sqliteTable("users", {
id: integer("id").primaryKey({ autoIncrement: true }),
email: text("email").notNull().unique(),
name: text("name").notNull(),
passwordHash: text("password_hash").notNull(),
createdAt: text("created_at").notNull().$defaultFn(() => new Date().toISOString()),
});
export const companies = sqliteTable("companies", {
id: integer("id").primaryKey({ autoIncrement: true }),
name: text("name").notNull(),
domain: text("domain"),
phone: text("phone"),
address: text("address"),
notes: text("notes"),
userId: integer("user_id").notNull().references(() => users.id),
createdAt: text("created_at").notNull().$defaultFn(() => new Date().toISOString()),
updatedAt: text("updated_at").notNull().$defaultFn(() => new Date().toISOString()),
});
export const contacts = sqliteTable("contacts", {
id: integer("id").primaryKey({ autoIncrement: true }),
firstName: text("first_name").notNull(),
lastName: text("last_name").notNull(),
email: text("email"),
phone: text("phone"),
jobTitle: text("job_title"),
companyId: integer("company_id").references(() => companies.id),
userId: integer("user_id").notNull().references(() => users.id),
createdAt: text("created_at").notNull().$defaultFn(() => new Date().toISOString()),
updatedAt: text("updated_at").notNull().$defaultFn(() => new Date().toISOString()),
});
export const pipelines = sqliteTable("pipelines", {
id: integer("id").primaryKey({ autoIncrement: true }),
name: text("name").notNull(),
userId: integer("user_id").notNull().references(() => users.id),
createdAt: text("created_at").notNull().$defaultFn(() => new Date().toISOString()),
});
export const pipelineStages = sqliteTable("pipeline_stages", {
id: integer("id").primaryKey({ autoIncrement: true }),
name: text("name").notNull(),
position: integer("position").notNull(),
probability: integer("probability").notNull().default(0),
pipelineId: integer("pipeline_id").notNull().references(() => pipelines.id),
createdAt: text("created_at").notNull().$defaultFn(() => new Date().toISOString()),
});
export const deals = sqliteTable("deals", {
id: integer("id").primaryKey({ autoIncrement: true }),
title: text("title").notNull(),
value: real("value").notNull().default(0),
status: text("status", { enum: ["open", "won", "lost"] }).notNull().default("open"),
position: integer("position").notNull().default(0),
expectedCloseDate: text("expected_close_date"),
stageId: integer("stage_id").notNull().references(() => pipelineStages.id),
contactId: integer("contact_id").references(() => contacts.id),
companyId: integer("company_id").references(() => companies.id),
pipelineId: integer("pipeline_id").notNull().references(() => pipelines.id),
userId: integer("user_id").notNull().references(() => users.id),
createdAt: text("created_at").notNull().$defaultFn(() => new Date().toISOString()),
updatedAt: text("updated_at").notNull().$defaultFn(() => new Date().toISOString()),
});
export const activities = sqliteTable("activities", {
id: integer("id").primaryKey({ autoIncrement: true }),
type: text("type", { enum: ["call", "email", "meeting", "task", "note"] }).notNull(),
subject: text("subject").notNull(),
description: text("description"),
dueDate: text("due_date"),
isDone: integer("is_done", { mode: "boolean" }).notNull().default(false),
dealId: integer("deal_id").references(() => deals.id),
contactId: integer("contact_id").references(() => contacts.id),
companyId: integer("company_id").references(() => companies.id),
userId: integer("user_id").notNull().references(() => users.id),
createdAt: text("created_at").notNull().$defaultFn(() => new Date().toISOString()),
updatedAt: text("updated_at").notNull().$defaultFn(() => new Date().toISOString()),
});