48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import { eq } from 'drizzle-orm'
|
|
|
|
import { db } from './db-instance'
|
|
import { enterpriseStaff } from './schema/enterpriseStaff'
|
|
|
|
export type EnterpriseStaff = {
|
|
id: number
|
|
staff_id: number
|
|
enterprise_id: number
|
|
}
|
|
|
|
export type EnterpriseStaffRepo = {
|
|
getByStaffId: (staffId: number) => EnterpriseStaff | null
|
|
listByEnterprise: (enterpriseId: number) => EnterpriseStaff[]
|
|
create: (input: { staff_id: number; enterprise_id: number }) => EnterpriseStaff
|
|
}
|
|
|
|
function toEnterpriseStaff(row: typeof enterpriseStaff.$inferSelect): EnterpriseStaff {
|
|
return {
|
|
id: row.id,
|
|
staff_id: row.staffId,
|
|
enterprise_id: row.enterpriseId,
|
|
}
|
|
}
|
|
|
|
export function createEnterpriseStaffRepo(): { repo: EnterpriseStaffRepo } {
|
|
const repo: EnterpriseStaffRepo = {
|
|
getByStaffId(staffId) {
|
|
const row = db.select().from(enterpriseStaff).where(eq(enterpriseStaff.staffId, staffId)).get()
|
|
return row ? toEnterpriseStaff(row) : null
|
|
},
|
|
|
|
listByEnterprise(enterpriseId) {
|
|
const rows = db.select().from(enterpriseStaff).where(eq(enterpriseStaff.enterpriseId, enterpriseId)).all()
|
|
return rows.map(toEnterpriseStaff)
|
|
},
|
|
|
|
create(input) {
|
|
const created = db.insert(enterpriseStaff).values({
|
|
staffId: input.staff_id,
|
|
enterpriseId: input.enterprise_id,
|
|
}).returning().get()
|
|
return toEnterpriseStaff(created)
|
|
},
|
|
}
|
|
|
|
return { repo }
|
|
}
|