138 lines
3.6 KiB
TypeScript
138 lines
3.6 KiB
TypeScript
import {
|
|
sqliteTable,
|
|
text,
|
|
integer,
|
|
primaryKey,
|
|
unique,
|
|
index,
|
|
} from "drizzle-orm/sqlite-core";
|
|
import { relations, sql } from "drizzle-orm";
|
|
|
|
//----------------User
|
|
export const user = sqliteTable(
|
|
"users",
|
|
{
|
|
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
|
|
firstName: text("firstName").notNull(),
|
|
lastName: text("lastName").notNull(),
|
|
title: text("title").notNull(),
|
|
cid: text("cid", { length: 13 }).notNull().unique(),
|
|
age: integer("age").notNull(),
|
|
phone: text("phone").unique().notNull(),
|
|
public_phone: text("public_phone"),
|
|
facebook: text("facebook"),
|
|
twitter: text("twitter"),
|
|
tiktok: text("tiktok"),
|
|
otherSocial: text("other_social"),
|
|
email: text("email"),
|
|
job: text("job").notNull(),
|
|
education: text("education").notNull(),
|
|
vision: text("vision"),
|
|
reason: text("reason"),
|
|
group: integer("group_id")
|
|
.references(() => group.id)
|
|
.notNull(),
|
|
zone: integer("zone_id")
|
|
.notNull()
|
|
.references(() => zone.id),
|
|
},
|
|
(t) => ({
|
|
phone_idx: index("phone_idx").on(t.phone),
|
|
})
|
|
);
|
|
|
|
export const userRelation = relations(user, ({ many, one }) => ({
|
|
opinions: many(userOpinion),
|
|
group: one(group, {
|
|
fields: [user.group],
|
|
references: [group.id],
|
|
}),
|
|
zone: one(zone, {
|
|
fields: [user.zone],
|
|
references: [zone.id],
|
|
}),
|
|
}));
|
|
|
|
//----------------Group
|
|
export const group = sqliteTable("groups", {
|
|
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
|
|
name: text("name").unique().notNull(),
|
|
});
|
|
|
|
export const groupRelation = relations(group, ({ many }) => ({
|
|
users: many(user),
|
|
}));
|
|
|
|
//----------------Opinion
|
|
export const opinion = sqliteTable("opinions", {
|
|
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
|
|
name: text("name").unique().notNull(),
|
|
type: text("type", { enum: ["3Choice", "4Choice"] })
|
|
.default("3Choice")
|
|
.notNull(),
|
|
});
|
|
|
|
//----------------UserOpinion
|
|
export const userOpinion = sqliteTable(
|
|
"user_opinions",
|
|
{
|
|
userId: integer("user_id")
|
|
.notNull()
|
|
.references(() => user.id),
|
|
opinionId: integer("opinion_id")
|
|
.notNull()
|
|
.references(() => opinion.id),
|
|
choice: text("choice", {
|
|
enum: ["agree", "disagree", "deciding", "ignore"],
|
|
}).default("deciding"),
|
|
},
|
|
(t) => ({
|
|
pk: primaryKey({ columns: [t.userId, t.opinionId] }),
|
|
})
|
|
);
|
|
|
|
export const userOpinionRelation = relations(userOpinion, ({ one }) => ({
|
|
user: one(user, {
|
|
fields: [userOpinion.userId],
|
|
references: [user.id],
|
|
}),
|
|
}));
|
|
|
|
//----------------Zone
|
|
export const zone = sqliteTable(
|
|
"zones",
|
|
{
|
|
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
|
|
name: text("name").notNull(),
|
|
province: integer("province_id")
|
|
.notNull()
|
|
.references(() => province.id),
|
|
},
|
|
(t) => ({ unique_name_province: unique().on(t.name, t.province) })
|
|
);
|
|
export const zoneRelation = relations(zone, ({ one }) => ({
|
|
province: one(province, {
|
|
fields: [zone.province],
|
|
references: [province.id],
|
|
}),
|
|
}));
|
|
|
|
//----------------Province
|
|
export const province = sqliteTable("provinces", {
|
|
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
|
|
name: text("name").unique().notNull(),
|
|
});
|
|
|
|
export const provinceRelation = relations(province, ({ many }) => ({
|
|
zones: many(zone),
|
|
}));
|
|
|
|
//----------------PhoneToken
|
|
export const phoneToken = sqliteTable("phone_tokens", {
|
|
phone: text("phone").primaryKey(),
|
|
token: text("token").notNull(),
|
|
createdOn: integer("created_on", { mode: "timestamp" }).default(
|
|
sql`CURRENT_TIMESTAMP`
|
|
),
|
|
});
|