116 lines
3.5 KiB
TypeScript
116 lines
3.5 KiB
TypeScript
import { eq, and } from 'drizzle-orm'
|
|
|
|
import { db } from './db-instance'
|
|
import { storageSpaces } from './schema/storageSpacesSchema'
|
|
import { parseStringArrJson, serializeStringArrJson } from './lib/json-utils'
|
|
|
|
export type StorageSpace = {
|
|
id: number
|
|
name: string
|
|
description: string | null
|
|
aliases: string[]
|
|
image_urls: string[]
|
|
}
|
|
|
|
export type CreateStorageSpaceInput = {
|
|
name: string
|
|
description?: StorageSpace['description']
|
|
aliases: StorageSpace['aliases']
|
|
image_urls: StorageSpace['image_urls']
|
|
}
|
|
|
|
export type UpdateStorageSpacePatch = Partial<CreateStorageSpaceInput>
|
|
|
|
export type StorageSpacesRepo = {
|
|
getStorageSpaces: (enterpriseId: number) => Promise<StorageSpace[]>
|
|
getStorageSpaceById: (id: number, enterpriseId: number) => Promise<StorageSpace | null>
|
|
createStorageSpace: (input: CreateStorageSpaceInput, enterpriseId: number) => Promise<StorageSpace>
|
|
updateStorageSpace: (id: number, patch: UpdateStorageSpacePatch, enterpriseId: number) => Promise<StorageSpace | null>
|
|
deleteStorageSpace: (id: number, enterpriseId: number) => Promise<boolean>
|
|
}
|
|
|
|
function toStorageSpace(row: {
|
|
id: number
|
|
name: string
|
|
description: string | null
|
|
aliases: string
|
|
imageUrls: string
|
|
}): StorageSpace {
|
|
return {
|
|
id: row.id,
|
|
name: row.name,
|
|
description: row.description,
|
|
aliases: parseStringArrJson(row.aliases),
|
|
image_urls: parseStringArrJson(row.imageUrls),
|
|
}
|
|
}
|
|
|
|
export function createStorageSpacesRepo(): {
|
|
repo: StorageSpacesRepo
|
|
} {
|
|
|
|
const repo: StorageSpacesRepo = {
|
|
getStorageSpaces(enterpriseId) {
|
|
const rows = db
|
|
.select()
|
|
.from(storageSpaces)
|
|
.where(eq(storageSpaces.enterpriseId, enterpriseId))
|
|
.all()
|
|
return Promise.resolve(rows.map(toStorageSpace))
|
|
},
|
|
|
|
getStorageSpaceById(id, enterpriseId) {
|
|
const row = db
|
|
.select()
|
|
.from(storageSpaces)
|
|
.where(and(eq(storageSpaces.id, id), eq(storageSpaces.enterpriseId, enterpriseId)))
|
|
.get()
|
|
return Promise.resolve(row ? toStorageSpace(row) : null)
|
|
},
|
|
|
|
createStorageSpace(input, enterpriseId) {
|
|
const created = db
|
|
.insert(storageSpaces)
|
|
.values({
|
|
name: input.name,
|
|
description: input.description ?? null,
|
|
aliases: serializeStringArrJson(input.aliases),
|
|
imageUrls: serializeStringArrJson(input.image_urls),
|
|
enterpriseId,
|
|
})
|
|
.returning()
|
|
.get()
|
|
return Promise.resolve(toStorageSpace(created))
|
|
},
|
|
|
|
updateStorageSpace(id, patch, enterpriseId) {
|
|
const updated = db
|
|
.update(storageSpaces)
|
|
.set({
|
|
...(patch.name !== undefined ? { name: patch.name } : {}),
|
|
...(patch.description !== undefined ? { description: patch.description } : {}),
|
|
...(patch.aliases !== undefined
|
|
? { aliases: serializeStringArrJson(patch.aliases) }
|
|
: {}),
|
|
...(patch.image_urls !== undefined
|
|
? { imageUrls: serializeStringArrJson(patch.image_urls) }
|
|
: {}),
|
|
})
|
|
.where(and(eq(storageSpaces.id, id), eq(storageSpaces.enterpriseId, enterpriseId)))
|
|
.returning()
|
|
.get()
|
|
return Promise.resolve(updated ? toStorageSpace(updated) : null)
|
|
},
|
|
|
|
deleteStorageSpace(id, enterpriseId) {
|
|
const deleted = db
|
|
.delete(storageSpaces)
|
|
.where(and(eq(storageSpaces.id, id), eq(storageSpaces.enterpriseId, enterpriseId)))
|
|
.returning({ id: storageSpaces.id })
|
|
.get()
|
|
return Promise.resolve(Boolean(deleted))
|
|
},
|
|
}
|
|
|
|
return { repo }
|
|
}
|