2024-09-04 23:04:41 +00:00
|
|
|
import { takeFirstOrThrow } from '$lib/server/api/common/utils/repository'
|
|
|
|
|
import { DrizzleService } from '$lib/server/api/services/drizzle.service'
|
2024-09-01 19:22:00 +00:00
|
|
|
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-04 23:04:41 +00:00
|
|
|
export class CollectionsRepository {
|
|
|
|
|
constructor(@inject(DrizzleService) private readonly drizzle: DrizzleService) {}
|
2024-08-10 17:10:57 +00:00
|
|
|
|
2024-09-04 23:04:41 +00:00
|
|
|
async findAll(db = this.drizzle.db) {
|
|
|
|
|
return db.query.collections.findMany()
|
2024-08-13 22:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-04 23:04:41 +00:00
|
|
|
async findOneById(id: string, db = this.drizzle.db) {
|
|
|
|
|
return 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
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 23:04:41 +00:00
|
|
|
async findOneByCuid(cuid: string, db = this.drizzle.db) {
|
|
|
|
|
return db.query.collections.findFirst({
|
2024-08-23 02:26:22 +00:00
|
|
|
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
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 23:04:41 +00:00
|
|
|
async findOneByUserId(userId: string, db = this.drizzle.db) {
|
|
|
|
|
return 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
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 23:04:41 +00:00
|
|
|
async findAllByUserId(userId: string, db = this.drizzle.db) {
|
|
|
|
|
return 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
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 23:04:41 +00:00
|
|
|
async create(data: CreateCollection, db = this.drizzle.db) {
|
|
|
|
|
return db.insert(collections).values(data).returning().then(takeFirstOrThrow)
|
2024-08-13 22:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-04 23:04:41 +00:00
|
|
|
async update(id: string, data: UpdateCollection, db = this.drizzle.db) {
|
|
|
|
|
return db.update(collections).set(data).where(eq(collections.id, id)).returning().then(takeFirstOrThrow)
|
2024-08-10 17:10:57 +00:00
|
|
|
}
|
2024-09-01 19:22:00 +00:00
|
|
|
}
|