Skip to content

Commit

Permalink
add created at to resources
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Nov 17, 2024
1 parent d5002db commit 5314e67
Show file tree
Hide file tree
Showing 6 changed files with 4,704 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { environmentRouter } from "./router/environment";
import { githubRouter } from "./router/github";
import { jobRouter } from "./router/job";
import { releaseRouter } from "./router/release";
import { resourceRouter } from "./router/resources";
import { runbookRouter } from "./router/runbook";
import { runtimeRouter } from "./router/runtime";
import { systemRouter } from "./router/system";
import { resourceRouter } from "./router/target";
import { profileRouter, userRouter } from "./router/user";
import { variableSetRouter } from "./router/variable-set";
import { workspaceRouter } from "./router/workspace";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
and,
asc,
count,
desc,
eq,
inArray,
not,
Expand Down Expand Up @@ -300,8 +301,7 @@ const resourceQuery = (db: Tx, checks: Array<SQL<unknown>>) =>
schema.resource.id,
schema.resourceProvider.id,
schema.workspace.id,
)
.orderBy(asc(schema.resource.kind), asc(schema.resource.name));
);

export const resourceRouter = createTRPCRouter({
metadataGroup: resourceMetadataGroupRouter,
Expand Down Expand Up @@ -346,6 +346,14 @@ export const resourceRouter = createTRPCRouter({
filter: resourceCondition.optional(),
limit: z.number().int().nonnegative().max(1000).default(200),
offset: z.number().int().nonnegative().default(0),
orderBy: z
.array(
z.object({
property: z.enum(["kind", "name", "createdAt"]),
direction: z.enum(["asc", "desc"]).optional().default("asc"),
}),
)
.optional(),
}),
)
.query(({ ctx, input }) => {
Expand All @@ -359,9 +367,28 @@ export const resourceRouter = createTRPCRouter({
);
const checks = [workspaceIdCheck, resourceConditions].filter(isPresent);

const properties = {
kind: schema.resource.kind,
name: schema.resource.name,
createdAt: schema.resource.createdAt,
};

const orderBy: SQL[] = input.orderBy
? []
: [asc(schema.resource.kind), asc(schema.resource.name)];

if (input.orderBy)
for (const order of input.orderBy) {
const column = properties[order.property];
orderBy.push(
order.direction === "asc" ? asc(column) : desc(column),
);
}

const items = resourceQuery(ctx.db, checks)
.limit(input.limit)
.offset(input.offset)
.orderBy(...orderBy)
.then((t) =>
t.map((a) => ({
...a.resource,
Expand All @@ -371,9 +398,7 @@ export const resourceRouter = createTRPCRouter({
);

const total = ctx.db
.select({
count: sql`COUNT(*)`.mapWith(Number),
})
.select({ count: sql`COUNT(*)`.mapWith(Number) })
.from(schema.resource)
.where(and(...checks))
.then(takeFirst)
Expand Down
1 change: 1 addition & 0 deletions packages/db/drizzle/0038_fearless_stark_industries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "resource" ADD COLUMN "created_at" timestamp with time zone DEFAULT now() NOT NULL;
Loading

0 comments on commit 5314e67

Please sign in to comment.