File tree Expand file tree Collapse file tree 6 files changed +168
-0
lines changed
lib/javascript/fullstack_demo Expand file tree Collapse file tree 6 files changed +168
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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" ,
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" ,
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11export type { TodoRepository } from './todo-repository' ;
22
33import { JsonTodoRespository } from './json-todo-repository' ;
4+ import { PostgresTodoRepository } from './postgres-todo-repository' ;
45import { RedisTodoRepository } from './redis-todo-repository' ;
56import { TodoRepository } from './todo-repository' ;
67
78export 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments