2024-09-01 19:22:00 +00:00
|
|
|
import type { Repository } from '$lib/server/api/common/interfaces/repository.interface'
|
|
|
|
|
import { takeFirstOrThrow } from '$lib/server/api/common/utils/repository.utils'
|
|
|
|
|
import { DatabaseProvider } from '$lib/server/api/providers/database.provider'
|
|
|
|
|
import { type InferInsertModel, eq } from 'drizzle-orm'
|
|
|
|
|
import { inject, injectable } from 'tsyringe'
|
|
|
|
|
import { collections } from '../databases/tables'
|
2024-08-13 22:19:57 +00:00
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
export type CreateCollection = InferInsertModel<typeof collections>
|
|
|
|
|
export type UpdateCollection = Partial<CreateCollection>
|
2024-08-10 17:10:57 +00:00
|
|
|
|
|
|
|
|
@injectable()
|
2024-09-01 19:22:00 +00:00
|
|
|
export class CollectionsRepository implements Repository {
|
|
|
|
|
constructor(@inject(DatabaseProvider) private readonly db: DatabaseProvider) {}
|
2024-08-10 17:10:57 +00:00
|
|
|
|
|
|
|
|
async findAll() {
|
2024-09-01 19:22:00 +00:00
|
|
|
return this.db.query.collections.findMany()
|
2024-08-13 22:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOneById(id: string) {
|
|
|
|
|
return this.db.query.collections.findFirst({
|
2024-08-23 02:26:22 +00:00
|
|
|
where: eq(collections.id, id),
|
|
|
|
|
columns: {
|
|
|
|
|
cuid: true,
|
2024-09-01 19:22:00 +00:00
|
|
|
name: true,
|
|
|
|
|
},
|
2024-08-23 02:26:22 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOneByCuid(cuid: string) {
|
|
|
|
|
return this.db.query.collections.findFirst({
|
|
|
|
|
where: eq(collections.cuid, cuid),
|
|
|
|
|
columns: {
|
|
|
|
|
cuid: true,
|
2024-09-01 19:22:00 +00:00
|
|
|
name: true,
|
|
|
|
|
},
|
2024-08-13 22:19:57 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOneByUserId(userId: string) {
|
|
|
|
|
return this.db.query.collections.findFirst({
|
2024-08-23 02:26:22 +00:00
|
|
|
where: eq(collections.user_id, userId),
|
|
|
|
|
columns: {
|
|
|
|
|
cuid: true,
|
2024-09-01 19:22:00 +00:00
|
|
|
name: true,
|
|
|
|
|
},
|
2024-08-23 02:26:22 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findAllByUserId(userId: string) {
|
|
|
|
|
return this.db.query.collections.findMany({
|
2024-09-01 19:22:00 +00:00
|
|
|
where: eq(collections.user_id, userId),
|
2024-08-13 22:19:57 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(data: CreateCollection) {
|
2024-09-01 19:22:00 +00:00
|
|
|
return this.db.insert(collections).values(data).returning().then(takeFirstOrThrow)
|
2024-08-13 22:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async update(id: string, data: UpdateCollection) {
|
2024-09-01 19:22:00 +00:00
|
|
|
return this.db.update(collections).set(data).where(eq(collections.id, id)).returning().then(takeFirstOrThrow)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trxHost(trx: DatabaseProvider) {
|
|
|
|
|
return new CollectionsRepository(trx)
|
2024-08-10 17:10:57 +00:00
|
|
|
}
|
2024-09-01 19:22:00 +00:00
|
|
|
}
|