Skip to content

Commit f1deb53

Browse files
add db implemetation
1 parent c472ebc commit f1deb53

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

apps/event-queue/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"zod": "catalog:"
2121
},
2222
"devDependencies": {
23+
"@ctrlplane/db": "workspace:*",
2324
"@ctrlplane/eslint-config": "workspace:^",
2425
"@ctrlplane/prettier-config": "workspace:^",
2526
"@ctrlplane/tsconfig": "workspace:*",
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import type { Tx } from "@ctrlplane/db";
2+
3+
import { and, eq, takeFirst, takeFirstOrNull } from "@ctrlplane/db";
4+
import { db } from "@ctrlplane/db/client";
5+
import * as schema from "@ctrlplane/db/schema";
6+
7+
import type { Repository } from "./repository.js";
8+
9+
export class DbResourceRepository implements Repository<schema.Resource> {
10+
private readonly db: Tx;
11+
private readonly workspaceId: string;
12+
constructor(workspaceId: string, tx?: Tx) {
13+
this.db = tx ?? db;
14+
this.workspaceId = workspaceId;
15+
}
16+
17+
async get(id: string) {
18+
return this.db
19+
.select()
20+
.from(schema.resource)
21+
.where(
22+
and(
23+
eq(schema.resource.id, id),
24+
eq(schema.resource.workspaceId, this.workspaceId),
25+
),
26+
)
27+
.then(takeFirstOrNull);
28+
}
29+
async getAll() {
30+
return this.db
31+
.select()
32+
.from(schema.resource)
33+
.where(eq(schema.resource.workspaceId, this.workspaceId));
34+
}
35+
async create(entity: schema.Resource) {
36+
return this.db
37+
.insert(schema.resource)
38+
.values({ ...entity, workspaceId: this.workspaceId })
39+
.returning()
40+
.then(takeFirst);
41+
}
42+
async update(entity: schema.Resource) {
43+
return this.db
44+
.update(schema.resource)
45+
.set(entity)
46+
.where(eq(schema.resource.id, entity.id))
47+
.returning()
48+
.then(takeFirst);
49+
}
50+
async delete(id: string) {
51+
return this.db
52+
.delete(schema.resource)
53+
.where(eq(schema.resource.id, id))
54+
.returning()
55+
.then(takeFirstOrNull);
56+
}
57+
async exists(id: string) {
58+
return this.db
59+
.select()
60+
.from(schema.resource)
61+
.where(eq(schema.resource.id, id))
62+
.then(takeFirstOrNull)
63+
.then((r) => r != null);
64+
}
65+
}

apps/event-queue/src/repository.ts renamed to apps/event-queue/src/repository/repository.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
type Entity = { id: string };
22

3-
interface Repository<T extends Entity> {
4-
get(id: string): T | null;
5-
getAll(): T[];
6-
create(entity: T): T;
7-
update(entity: T): T;
8-
delete(id: string): T | null;
9-
exists(id: string): boolean;
3+
export interface Repository<T extends Entity> {
4+
get(id: string): Promise<T | null> | T | null;
5+
getAll(): Promise<T[]> | T[];
6+
create(entity: T): Promise<T> | T;
7+
update(entity: T): Promise<T> | T;
8+
delete(id: string): Promise<T | null> | T | null;
9+
exists(id: string): Promise<boolean> | boolean;
1010
}
1111

1212
export class RepositoryWithID<T extends Entity> implements Repository<T> {

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)