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 12
12
},
13
13
"dependencies" : {
14
14
"@clerk/nextjs" : " ^6.9.2" ,
15
+ "@prisma/client" : " ^6.1.0" ,
15
16
"@upstash/redis" : " ^1.34.3" ,
16
17
"lowdb" : " ^7.0.1" ,
17
18
"lucide-react" : " ^0.468.0" ,
32
33
"jsdom" : " ^25.0.1" ,
33
34
"postcss" : " ^8" ,
34
35
"prettier" : " ^3.4.2" ,
36
+ "prisma" : " ^6.1.0" ,
35
37
"tailwindcss" : " ^3.4.16" ,
36
38
"typescript" : " ^5" ,
37
39
"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 1
1
export type { TodoRepository } from './todo-repository' ;
2
2
3
3
import { JsonTodoRespository } from './json-todo-repository' ;
4
+ import { PostgresTodoRepository } from './postgres-todo-repository' ;
4
5
import { RedisTodoRepository } from './redis-todo-repository' ;
5
6
import { TodoRepository } from './todo-repository' ;
6
7
7
8
export function createTodoRepository ( ) : TodoRepository {
8
9
if ( process . env . DB_TYPE ?. toUpperCase ( ) === 'REDIS' ) {
9
10
return new RedisTodoRepository ( ) ;
10
11
}
12
+ if ( process . env . DB_TYPE ?. toUpperCase ( ) === 'POSTGRES' ) {
13
+ return new PostgresTodoRepository ( ) ;
14
+ }
11
15
return new JsonTodoRespository ( ) ;
12
16
}
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