Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions packages/db/src/selectors/compute/compute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { PgTableWithColumns } from "drizzle-orm/pg-core";
import { resource } from "src/schema/index.js";

import type { Tx } from "../../common.js";

class InsertBuilder<T extends PgTableWithColumns<any>> {
constructor(
private readonly tx: Tx,
private readonly table: T,
private readonly values: () => Promise<any[]>,
) {}

async insert() {
const vals = await this.values();
if (vals.length === 0) return;
return this.tx.insert(this.table).values(vals);
}
}

class EnvironmentBuilder {
constructor(
private readonly tx: Tx,
private readonly ids: string[],
) {}

resourceSelector() {
return new InsertBuilder(this.tx, resource, async () => {
// return this.tx
// .select()
// .from(resource)
// .where(eq(resource.workspaceId, this.workspaceId));
});
}
}

class WorkspaceEnvironmentBuilder {
constructor(
private readonly tx: Tx,
private readonly workspaceId: string,
) {}

resourceSelectors() {
return new InsertBuilder(this.tx, resource, async () => {
// return this.tx
// .select()
// .from(resource)
// .where(eq(resource.workspaceId, this.workspaceId));
});
}
}

export class ComputeBuilder {
constructor(private readonly tx: Tx) {}

allEnvironments(workspaceId: string) {
return new WorkspaceEnvironmentBuilder(this.tx, workspaceId);
}

environments(ids: string[]) {
return new EnvironmentBuilder(this.tx, ids);
}
}
18 changes: 18 additions & 0 deletions packages/db/src/selectors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Tx } from "../common.js";
import { db } from "../client.js";
import { ComputeBuilder } from "./compute/compute.js";
import { QueryBuilder } from "./query/builder.js";

class InitialBuilder {
constructor(private readonly tx: Tx) {}

compute() {
return new ComputeBuilder(this.tx);
}

query() {
return new QueryBuilder(this.tx);
}
}

export const selector = (tx?: Tx) => new InitialBuilder(tx ?? db);
29 changes: 29 additions & 0 deletions packages/db/src/selectors/query/builder-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { VersionCondition } from "@ctrlplane/validators/conditions";
import type { DeploymentCondition } from "@ctrlplane/validators/deployments";
import type { EnvironmentCondition } from "@ctrlplane/validators/environments";
import type { ResourceCondition } from "@ctrlplane/validators/resources";
import type { SQL } from "drizzle-orm";

import type { Tx } from "../../common.js";

type SelectorCondition =
| ResourceCondition
| VersionCondition
| DeploymentCondition
| EnvironmentCondition;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface OutputBuilder<T extends object> {
sql(): SQL<unknown> | undefined;
}

export class WhereBuilder<T extends SelectorCondition, O extends object> {
constructor(
private readonly tx: Tx,
private Class: new (tx: Tx, selector?: T | null) => OutputBuilder<O>,
) {}

where(condition?: T | null): OutputBuilder<O> {
return new this.Class(this.tx, condition);
}
}
18 changes: 18 additions & 0 deletions packages/db/src/selectors/query/builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { EnvironmentCondition } from "@ctrlplane/validators/environments";

import type { Tx } from "../../common.js";
import type { Environment } from "../../schema/index.js";
import { WhereBuilder } from "./builder-types.js";
import { EnvironmentOutputBuilder } from "./environments-selector.js";

export class QueryBuilder {
constructor(private readonly tx: Tx) {}
deployments() {}

environments() {
return new WhereBuilder<EnvironmentCondition, Environment>(
this.tx,
EnvironmentOutputBuilder,
);
}
}
131 changes: 131 additions & 0 deletions packages/db/src/selectors/query/environments-selector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import type { MetadataCondition } from "@ctrlplane/validators/conditions";
import type { EnvironmentCondition } from "@ctrlplane/validators/environments";
import type { SQL } from "drizzle-orm";
import { and, eq, exists, ilike, not, notExists, or, sql } from "drizzle-orm";

import {
ComparisonOperator,
MetadataOperator,
} from "@ctrlplane/validators/conditions";

import type { Tx } from "../../common.js";
import type { Environment } from "../../schema/index.js";
import type { OutputBuilder } from "./builder-types.js";
import { ColumnOperatorFn } from "../../common.js";
import { environment, environmentMetadata } from "../../schema/index.js";

const buildMetadataCondition = (tx: Tx, cond: MetadataCondition): SQL => {
if (cond.operator === MetadataOperator.Null)
return notExists(
tx
.select({ value: sql<number>`1` })
.from(environmentMetadata)
.where(
and(
eq(environmentMetadata.environmentId, environment.id),
eq(environmentMetadata.key, cond.key),
),
),
);

if (cond.operator === MetadataOperator.StartsWith)
return exists(
tx
.select({ value: sql<number>`1` })
.from(environmentMetadata)
.where(
and(
eq(environmentMetadata.environmentId, environment.id),
eq(environmentMetadata.key, cond.key),
ilike(environmentMetadata.value, `${cond.value}%`),
),
),
);

if (cond.operator === MetadataOperator.EndsWith)
return exists(
tx
.select({ value: sql<number>`1` })
.from(environmentMetadata)
.where(
and(
eq(environmentMetadata.environmentId, environment.id),
eq(environmentMetadata.key, cond.key),
ilike(environmentMetadata.value, `%${cond.value}`),
),
),
);

if (cond.operator === MetadataOperator.Contains)
return exists(
tx
.select({ value: sql<number>`1` })
.from(environmentMetadata)
.where(
and(
eq(environmentMetadata.environmentId, environment.id),
eq(environmentMetadata.key, cond.key),
ilike(environmentMetadata.value, `%${cond.value}%`),
),
),
);

if ("value" in cond)
return exists(
tx
.select({ value: sql<number>`1` })
.from(environmentMetadata)
.where(
and(
eq(environmentMetadata.environmentId, environment.id),
eq(environmentMetadata.key, cond.key),
eq(environmentMetadata.value, cond.value),
),
),
);

throw Error("invalid metadata conditions");
};

const buildCondition = (
tx: Tx,
condition: EnvironmentCondition,
): SQL<unknown> => {
if (condition.type === "name")
return ColumnOperatorFn[condition.operator](
environment.name,
condition.value,
);
if (condition.type === "directory")
return ColumnOperatorFn[condition.operator](
environment.directory,
condition.value,
);
if (condition.type === "system")
return eq(environment.systemId, condition.value);
if (condition.type === "id") return eq(environment.id, condition.value);
if (condition.type === "metadata")
return buildMetadataCondition(tx, condition);

if (condition.conditions.length === 0) return sql`FALSE`;

const subCon = condition.conditions.map((c) => buildCondition(tx, c));
const con =
condition.operator === ComparisonOperator.And
? and(...subCon)!
: or(...subCon)!;
return condition.not ? not(con) : con;
};

export class EnvironmentOutputBuilder implements OutputBuilder<Environment> {
constructor(
private readonly tx: Tx,
readonly condition?: EnvironmentCondition | null,
) {}

sql(): SQL<unknown> | undefined {
return this.condition == null || Object.keys(this.condition).length === 0
? undefined
: buildCondition(this.tx, this.condition);
}
}
Loading