Skip to content

Commit fe92f7f

Browse files
committed
feat: adds postgres repo
1 parent 1e83a61 commit fe92f7f

File tree

6 files changed

+168
-0
lines changed

6 files changed

+168
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=Your Next Public Clerk Publishable Key here
2+
CLERK_SECRET_KEY=Your Clerk Secret Key here
3+
DB_TYPE=postgres
4+
NEXT_PUBLIC_API_URL=http://localhost:3000
5+
DATABASE_URL=Your Postgres database URL

lib/javascript/fullstack_demo/package-lock.json

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

lib/javascript/fullstack_demo/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
},
1313
"dependencies": {
1414
"@clerk/nextjs": "^6.9.2",
15+
"@prisma/client": "^6.1.0",
1516
"@upstash/redis": "^1.34.3",
1617
"lowdb": "^7.0.1",
1718
"lucide-react": "^0.468.0",
@@ -32,6 +33,7 @@
3233
"jsdom": "^25.0.1",
3334
"postcss": "^8",
3435
"prettier": "^3.4.2",
36+
"prisma": "^6.1.0",
3537
"tailwindcss": "^3.4.16",
3638
"typescript": "^5",
3739
"vite-tsconfig-paths": "^5.1.4",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
generator client {
2+
provider = "prisma-client-js"
3+
}
4+
5+
datasource db {
6+
provider = "postgresql"
7+
url = env("DATABASE_URL")
8+
}
9+
10+
/// This table has subclasses and requires additional setup for migrations. Visit https://pris.ly/d/table-inheritance for more info.
11+
/// This model contains row level security and requires additional setup for migrations. Visit https://pris.ly/d/row-level-security for more info.
12+
model Todos {
13+
id BigInt @id @default(autoincrement())
14+
user_id String @db.VarChar
15+
text String @default("") @db.VarChar
16+
completed Boolean @default(false)
17+
created_at DateTime @default(now()) @db.Timestamptz(6)
18+
updated_at DateTime @default(now()) @db.Timestamp(6)
19+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
export type { TodoRepository } from './todo-repository';
22

33
import { JsonTodoRespository } from './json-todo-repository';
4+
import { PostgresTodoRepository } from './postgres-todo-repository';
45
import { RedisTodoRepository } from './redis-todo-repository';
56
import { TodoRepository } from './todo-repository';
67

78
export function createTodoRepository(): TodoRepository {
89
if (process.env.DB_TYPE?.toUpperCase() === 'REDIS') {
910
return new RedisTodoRepository();
1011
}
12+
if (process.env.DB_TYPE?.toUpperCase() === 'POSTGRES') {
13+
return new PostgresTodoRepository();
14+
}
1115
return new JsonTodoRespository();
1216
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Todo } from '@/models';
2+
import { PrismaClient } from '@prisma/client';
3+
import { TodoRepository } from './todo-repository';
4+
5+
const client = new PrismaClient();
6+
7+
export class PostgresTodoRepository implements TodoRepository {
8+
async getAll(userId: string): Promise<Todo[]> {
9+
const dbTodos = await client.todos.findMany({ where: { user_id: userId } });
10+
return dbTodos.map((todo) => ({
11+
id: Number(todo.id),
12+
text: todo.text,
13+
completed: todo.completed,
14+
}));
15+
}
16+
17+
async create(todo: Todo, userId: string): Promise<number> {
18+
const dbTodo = await client.todos.create({
19+
data: {
20+
text: todo.text,
21+
completed: todo.completed,
22+
user_id: userId,
23+
},
24+
});
25+
return Number(dbTodo.id);
26+
}
27+
28+
async patch(todo: Partial<Todo>, userId: string): Promise<Todo | undefined> {
29+
const dbTodo = await client.todos.update({
30+
where: { id: todo.id },
31+
data: {
32+
text: todo.text,
33+
completed: todo.completed,
34+
},
35+
});
36+
return dbTodo
37+
? {
38+
id: Number(dbTodo.id),
39+
text: dbTodo.text,
40+
completed: dbTodo.completed,
41+
}
42+
: undefined;
43+
}
44+
45+
async delete(id: number, userId: string): Promise<void> {
46+
await client.todos.delete({ where: { id } });
47+
}
48+
}

0 commit comments

Comments
 (0)