41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
// db/schema/location.ts
|
|
import {
|
|
pgTable,
|
|
uuid,
|
|
varchar,
|
|
text,
|
|
timestamp,
|
|
index,
|
|
} from "drizzle-orm/pg-core";
|
|
|
|
export const locations = pgTable(
|
|
"ms_locations",
|
|
{
|
|
id: uuid("id").defaultRandom().primaryKey(),
|
|
branchId: text("branch_id").notNull(), // Multi-tenant support
|
|
code: varchar("code", { length: 255 }).notNull(),
|
|
nameTh: varchar("name_th", { length: 255 }).notNull(),
|
|
nameEn: varchar("name_en", { length: 255 }),
|
|
|
|
type: varchar("type", { length: 50 }).notNull(),
|
|
// 'country' | 'province' | 'district' | 'subdistrict'
|
|
|
|
parentId: uuid("parent_id"), // self reference (tree)
|
|
|
|
createdAt: timestamp("created_at").defaultNow().notNull(),
|
|
updatedAt: timestamp("updated_at").defaultNow().notNull(),
|
|
},
|
|
(table) => ({
|
|
branchIdx: index("ms_locations_branch_id_idx").on(table.branchId),
|
|
typeIndex: index("ms_locations_type_idx").on(table.type),
|
|
parentIdIndex: index("ms_locations_parent_id_idx").on(table.parentId),
|
|
branchTypeIdx: index("ms_locations_branch_type_idx").on(
|
|
table.branchId,
|
|
table.type,
|
|
),
|
|
}),
|
|
);
|
|
|
|
export type Location = typeof locations.$inferSelect;
|
|
export type NewLocation = typeof locations.$inferInsert;
|