diff --git a/apps/event-queue/package.json b/apps/event-queue/package.json index e7dbef6c8..f95d02b3c 100644 --- a/apps/event-queue/package.json +++ b/apps/event-queue/package.json @@ -12,6 +12,7 @@ "with-env": "dotenv -e ../../.env --" }, "dependencies": { + "@ctrlplane/db": "workspace:*", "@ctrlplane/events": "workspace:*", "@ctrlplane/logger": "workspace:*", "@t3-oss/env-core": "catalog:", diff --git a/apps/event-queue/src/repository/db-resource-repository.ts b/apps/event-queue/src/repository/db-resource-repository.ts new file mode 100644 index 000000000..4b568a9ad --- /dev/null +++ b/apps/event-queue/src/repository/db-resource-repository.ts @@ -0,0 +1,65 @@ +import type { Tx } from "@ctrlplane/db"; + +import { and, eq, takeFirst, takeFirstOrNull } from "@ctrlplane/db"; +import { db } from "@ctrlplane/db/client"; +import * as schema from "@ctrlplane/db/schema"; + +import type { Repository } from "./repository.js"; + +export class DbResourceRepository implements Repository { + private readonly db: Tx; + private readonly workspaceId: string; + constructor(workspaceId: string, tx?: Tx) { + this.db = tx ?? db; + this.workspaceId = workspaceId; + } + + async get(id: string) { + return this.db + .select() + .from(schema.resource) + .where( + and( + eq(schema.resource.id, id), + eq(schema.resource.workspaceId, this.workspaceId), + ), + ) + .then(takeFirstOrNull); + } + async getAll() { + return this.db + .select() + .from(schema.resource) + .where(eq(schema.resource.workspaceId, this.workspaceId)); + } + async create(entity: schema.Resource) { + return this.db + .insert(schema.resource) + .values({ ...entity, workspaceId: this.workspaceId }) + .returning() + .then(takeFirst); + } + async update(entity: schema.Resource) { + return this.db + .update(schema.resource) + .set(entity) + .where(eq(schema.resource.id, entity.id)) + .returning() + .then(takeFirst); + } + async delete(id: string) { + return this.db + .delete(schema.resource) + .where(eq(schema.resource.id, id)) + .returning() + .then(takeFirstOrNull); + } + async exists(id: string) { + return this.db + .select() + .from(schema.resource) + .where(eq(schema.resource.id, id)) + .then(takeFirstOrNull) + .then((r) => r != null); + } +} diff --git a/apps/event-queue/src/repository/repository.ts b/apps/event-queue/src/repository/repository.ts new file mode 100644 index 000000000..6e4baa9c6 --- /dev/null +++ b/apps/event-queue/src/repository/repository.ts @@ -0,0 +1,10 @@ +type Entity = { id: string }; + +export interface Repository { + get(id: string): Promise | T | null; + getAll(): Promise | T[]; + create(entity: T): Promise | T; + update(entity: T): Promise | T; + delete(id: string): Promise | T | null; + exists(id: string): Promise | boolean; +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ba44b6517..183501838 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -136,6 +136,9 @@ importers: apps/event-queue: dependencies: + '@ctrlplane/db': + specifier: workspace:* + version: link:../../packages/db '@ctrlplane/events': specifier: workspace:* version: link:../../packages/events