@@ -2,15 +2,19 @@ import { PrismaClient } from '@fullstack-typescript-monorepo/prisma';
2
2
import { Request , Response } from 'express' ;
3
3
import auth from '../utils/auth' ;
4
4
import sendError from '../utils/sendError' ;
5
+ import TableUtils , { TableRequestBody } from '../utils/TableUtils' ;
5
6
6
- interface GenericPrisma extends PrismaClient {
7
+ export interface GenericPrisma extends PrismaClient {
7
8
[ key : string ] : unknown ;
8
9
}
9
10
10
- interface PrismaModel {
11
+ export interface MOCK_PrismaModel {
11
12
create : ( prop : { data : unknown } ) => Promise < unknown > ;
12
13
findUniqueOrThrow : ( prop : { where : { id : number } } ) => Promise < unknown > ;
13
- findMany : ( ) => Promise < unknown [ ] > ;
14
+ findMany : ( prop ?: Record < string , unknown > ) => Promise < unknown [ ] > ;
15
+ count : ( prop : Record < string , unknown > ) => Promise < number > ;
16
+ update : ( prop : { where : { id : number } ; data : unknown } ) => Promise < unknown > ;
17
+ delete : ( prop : { where : { id : number } } ) => Promise < unknown > ;
14
18
}
15
19
16
20
/**
@@ -25,7 +29,7 @@ const insert = (model: string) => (prisma: PrismaClient) => async (
25
29
await auth ( prisma , req ) ;
26
30
27
31
const { body } = req ;
28
- const prismaModel = ( prisma as GenericPrisma ) [ model ] as PrismaModel ;
32
+ const prismaModel = ( prisma as GenericPrisma ) [ model ] as MOCK_PrismaModel ;
29
33
30
34
const object = await prismaModel . create ( {
31
35
data : body ,
@@ -46,7 +50,7 @@ const get = (model: string) => (prisma: PrismaClient) => async (req: Request, re
46
50
await auth ( prisma , req ) ;
47
51
48
52
const { id } = req . params ;
49
- const prismaModel = ( prisma as GenericPrisma ) [ model ] as PrismaModel ;
53
+ const prismaModel = ( prisma as GenericPrisma ) [ model ] as MOCK_PrismaModel ;
50
54
51
55
const object = await prismaModel . findUniqueOrThrow ( {
52
56
where : { id : + id } ,
@@ -66,7 +70,7 @@ const getAll = (model: string) => (prisma: PrismaClient) => async (req: Request,
66
70
try {
67
71
await auth ( prisma , req ) ;
68
72
69
- const prismaModel = ( prisma as GenericPrisma ) [ model ] as PrismaModel ;
73
+ const prismaModel = ( prisma as GenericPrisma ) [ model ] as MOCK_PrismaModel ;
70
74
71
75
const objects = await prismaModel . findMany ( ) ;
72
76
@@ -87,7 +91,7 @@ const getAllAsCsv = (model: string) => (prisma: PrismaClient) => async (
87
91
try {
88
92
await auth ( prisma , req ) ;
89
93
90
- const prismaModel = ( prisma as GenericPrisma ) [ model ] as PrismaModel ;
94
+ const prismaModel = ( prisma as GenericPrisma ) [ model ] as MOCK_PrismaModel ;
91
95
92
96
const objects = await prismaModel . findMany ( ) as Record < string , unknown > [ ] ;
93
97
@@ -115,11 +119,79 @@ const getAllAsCsv = (model: string) => (prisma: PrismaClient) => async (
115
119
}
116
120
} ;
117
121
122
+ /**
123
+ * Get objects for a paginated table
124
+ * @param model
125
+ */
126
+ const table = ( model : string ) => ( prisma : PrismaClient ) => async (
127
+ req : Request < never , unknown , TableRequestBody > ,
128
+ res : Response ,
129
+ ) => {
130
+ try {
131
+ await auth ( prisma , req ) ;
132
+
133
+ const prismaModel = ( prisma as GenericPrisma ) [ model ] as MOCK_PrismaModel ;
134
+
135
+ res . json ( TableUtils . getData ( req , prismaModel ) ) ;
136
+ } catch ( error ) {
137
+ sendError ( res , error ) ;
138
+ }
139
+ } ;
140
+
141
+ /**
142
+ * Update an object in the database
143
+ * @param model
144
+ */
145
+ const update = ( model : string ) => ( prisma : PrismaClient ) => async (
146
+ req : Request < { id : string } , unknown , Record < string , unknown > > ,
147
+ res : Response ,
148
+ ) => {
149
+ try {
150
+ await auth ( prisma , req ) ;
151
+
152
+ const { id } = req . params ;
153
+ const { body } = req ;
154
+ const prismaModel = ( prisma as GenericPrisma ) [ model ] as MOCK_PrismaModel ;
155
+
156
+ const updatedObject = await prismaModel . update ( {
157
+ where : { id : + id } ,
158
+ data : { ...body } ,
159
+ } ) ;
160
+
161
+ res . json ( updatedObject ) ;
162
+ } catch ( error ) {
163
+ sendError ( res , error ) ;
164
+ }
165
+ } ;
166
+
167
+ const deleteObject = ( model : string ) => ( prisma : PrismaClient ) => async (
168
+ req : Request ,
169
+ res : Response ,
170
+ ) => {
171
+ try {
172
+ await auth ( prisma , req ) ;
173
+
174
+ const { id } = req . params ;
175
+ const prismaModel = ( prisma as GenericPrisma ) [ model ] as MOCK_PrismaModel ;
176
+
177
+ await prismaModel . delete ( {
178
+ where : { id : + id } ,
179
+ } ) ;
180
+
181
+ res . send ( ) ;
182
+ } catch ( error ) {
183
+ sendError ( res , error ) ;
184
+ }
185
+ } ;
186
+
118
187
const REST = ( model : string ) => ( {
119
188
insert : insert ( model ) ,
120
189
get : get ( model ) ,
121
190
getAll : getAll ( model ) ,
122
191
getAllAsCsv : getAllAsCsv ( model ) ,
192
+ table : table ( model ) ,
193
+ update : update ( model ) ,
194
+ delete : deleteObject ( model ) ,
123
195
} ) ;
124
196
125
197
export default REST ;
0 commit comments