30 lines
714 B
TypeScript
30 lines
714 B
TypeScript
import { eq } from 'drizzle-orm'
|
|
|
|
import { db } from './db-instance'
|
|
import { units } from './schema/units'
|
|
|
|
export type Unit = {
|
|
id: number
|
|
name: string
|
|
}
|
|
|
|
export type UnitsRepo = {
|
|
listUnits: () => Promise<Unit[]>
|
|
createUnit: (input: { name: string }) => Promise<Unit>
|
|
}
|
|
|
|
export function createUnitsRepo(): { repo: UnitsRepo } {
|
|
const repo: UnitsRepo = {
|
|
listUnits() {
|
|
const rows = db.select().from(units).all()
|
|
return Promise.resolve(rows.map((r) => ({ id: r.id, name: r.name })))
|
|
},
|
|
|
|
createUnit(input) {
|
|
const created = db.insert(units).values(input).returning().get()
|
|
return Promise.resolve({ id: created.id, name: created.name })
|
|
},
|
|
}
|
|
|
|
return { repo }
|
|
}
|