|
| 1 | +import type { Tx } from "@ctrlplane/db"; |
| 2 | + |
| 3 | +import { eq } from "@ctrlplane/db"; |
| 4 | +import { db as dbClient } from "@ctrlplane/db/client"; |
| 5 | +import * as schema from "@ctrlplane/db/schema"; |
| 6 | + |
| 7 | +import type { Repository } from "../repository"; |
| 8 | +import { createSpanWrapper } from "../../traces.js"; |
| 9 | + |
| 10 | +type JobVariable = typeof schema.jobVariable.$inferSelect; |
| 11 | + |
| 12 | +const getReleaseJobVariables = createSpanWrapper( |
| 13 | + "job-variable-getReleaseJobVariables", |
| 14 | + async (_span, workspaceId: string) => |
| 15 | + dbClient |
| 16 | + .select() |
| 17 | + .from(schema.jobVariable) |
| 18 | + .innerJoin(schema.job, eq(schema.jobVariable.jobId, schema.job.id)) |
| 19 | + .innerJoin(schema.releaseJob, eq(schema.releaseJob.jobId, schema.job.id)) |
| 20 | + .innerJoin( |
| 21 | + schema.release, |
| 22 | + eq(schema.releaseJob.releaseId, schema.release.id), |
| 23 | + ) |
| 24 | + .innerJoin( |
| 25 | + schema.versionRelease, |
| 26 | + eq(schema.release.versionReleaseId, schema.versionRelease.id), |
| 27 | + ) |
| 28 | + .innerJoin( |
| 29 | + schema.releaseTarget, |
| 30 | + eq(schema.versionRelease.releaseTargetId, schema.releaseTarget.id), |
| 31 | + ) |
| 32 | + .innerJoin( |
| 33 | + schema.resource, |
| 34 | + eq(schema.releaseTarget.resourceId, schema.resource.id), |
| 35 | + ) |
| 36 | + .where(eq(schema.resource.workspaceId, workspaceId)) |
| 37 | + .then((rows) => rows.map((row) => row.job_variable)), |
| 38 | +); |
| 39 | + |
| 40 | +const getRunbookJobVariables = createSpanWrapper( |
| 41 | + "job-variable-getRunbookJobVariables", |
| 42 | + async (_span, workspaceId: string) => { |
| 43 | + return dbClient |
| 44 | + .select() |
| 45 | + .from(schema.jobVariable) |
| 46 | + .innerJoin(schema.job, eq(schema.jobVariable.jobId, schema.job.id)) |
| 47 | + .innerJoin( |
| 48 | + schema.runbookJobTrigger, |
| 49 | + eq(schema.runbookJobTrigger.jobId, schema.job.id), |
| 50 | + ) |
| 51 | + .innerJoin( |
| 52 | + schema.runbook, |
| 53 | + eq(schema.runbookJobTrigger.runbookId, schema.runbook.id), |
| 54 | + ) |
| 55 | + .innerJoin(schema.system, eq(schema.runbook.systemId, schema.system.id)) |
| 56 | + .where(eq(schema.system.workspaceId, workspaceId)) |
| 57 | + .then((rows) => rows.map((row) => row.job_variable)); |
| 58 | + }, |
| 59 | +); |
| 60 | + |
| 61 | +const getInitialEntities = createSpanWrapper( |
| 62 | + "job-variable-getInitialEntities", |
| 63 | + async (span, workspaceId: string) => { |
| 64 | + const [releaseJobVariables, runbookJobVariables] = await Promise.all([ |
| 65 | + getReleaseJobVariables(workspaceId), |
| 66 | + getRunbookJobVariables(workspaceId), |
| 67 | + ]); |
| 68 | + const initialEntities = [...releaseJobVariables, ...runbookJobVariables]; |
| 69 | + span.setAttributes({ "job-variable.count": initialEntities.length }); |
| 70 | + return initialEntities; |
| 71 | + }, |
| 72 | +); |
| 73 | + |
| 74 | +type InMemoryJobVariableRepositoryOptions = { |
| 75 | + initialEntities: JobVariable[]; |
| 76 | + tx?: Tx; |
| 77 | +}; |
| 78 | + |
| 79 | +export class InMemoryJobVariableRepository implements Repository<JobVariable> { |
| 80 | + private entities: Map<string, JobVariable>; |
| 81 | + private db: Tx; |
| 82 | + |
| 83 | + constructor(opts: InMemoryJobVariableRepositoryOptions) { |
| 84 | + this.entities = new Map(); |
| 85 | + for (const entity of opts.initialEntities) |
| 86 | + this.entities.set(entity.id, entity); |
| 87 | + this.db = opts.tx ?? dbClient; |
| 88 | + } |
| 89 | + |
| 90 | + static async create(workspaceId: string) { |
| 91 | + const initialEntities = await getInitialEntities(workspaceId); |
| 92 | + return new InMemoryJobVariableRepository({ |
| 93 | + initialEntities, |
| 94 | + tx: dbClient, |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + get(id: string) { |
| 99 | + return this.entities.get(id) ?? null; |
| 100 | + } |
| 101 | + |
| 102 | + getAll() { |
| 103 | + return Array.from(this.entities.values()); |
| 104 | + } |
| 105 | + |
| 106 | + async create(entity: JobVariable) { |
| 107 | + this.entities.set(entity.id, entity); |
| 108 | + await this.db |
| 109 | + .insert(schema.jobVariable) |
| 110 | + .values(entity) |
| 111 | + .onConflictDoNothing(); |
| 112 | + return entity; |
| 113 | + } |
| 114 | + |
| 115 | + async update(entity: JobVariable) { |
| 116 | + this.entities.set(entity.id, entity); |
| 117 | + await this.db |
| 118 | + .update(schema.jobVariable) |
| 119 | + .set(entity) |
| 120 | + .where(eq(schema.jobVariable.id, entity.id)); |
| 121 | + return entity; |
| 122 | + } |
| 123 | + |
| 124 | + async delete(id: string) { |
| 125 | + const entity = this.entities.get(id); |
| 126 | + if (entity == null) return null; |
| 127 | + this.entities.delete(id); |
| 128 | + await this.db |
| 129 | + .delete(schema.jobVariable) |
| 130 | + .where(eq(schema.jobVariable.id, id)); |
| 131 | + return entity; |
| 132 | + } |
| 133 | + |
| 134 | + exists(id: string) { |
| 135 | + return this.entities.has(id); |
| 136 | + } |
| 137 | +} |
0 commit comments