Files
sorvor-back/src/schema.ts
2024-05-16 17:12:19 +07:00

150 lines
3.9 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"),
image: text("image"),
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),
verified: integer("verified", { mode: "boolean" }).notNull().default(false),
},
(t) => ({
phone_idx: index("phone_idx").on(t.phone),
image_idx: index("image_idx").on(t.image),
}),
);
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", "5Choice"] })
.default("5Choice")
.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: [
"strongly agree",
"agree",
"disagree",
"strongly disagree",
"ignore",
"deciding",
],
}).default("ignore"),
},
(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),
}));
//----------------ImageToUser
export const imageToUser = sqliteTable("image_to_user", {
userId: integer("user_id")
.primaryKey()
.references(() => user.id),
imageName: text("image_name").notNull(),
createdOn: integer("created_on", { mode: "timestamp" }).default(
sql`CURRENT_TIMESTAMP`,
),
});