prisma-generator-express 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +180 -0
- package/dist/bin.js.map +1 -0
- package/dist/constants.js +5 -0
- package/dist/constants.js.map +1 -0
- package/dist/generator.js.map +1 -0
- package/dist/helpers/genEnum.js +9 -0
- package/dist/helpers/genEnum.js.map +1 -0
- package/dist/helpers/generateAggregate.js +50 -0
- package/dist/helpers/generateAggregate.js.map +1 -0
- package/dist/helpers/generateCount.js +50 -0
- package/dist/helpers/generateCount.js.map +1 -0
- package/dist/helpers/generateCreate.js +51 -0
- package/dist/helpers/generateCreate.js.map +1 -0
- package/dist/helpers/generateCreateMany.js +51 -0
- package/dist/helpers/generateCreateMany.js.map +1 -0
- package/dist/helpers/generateDelete.js +52 -0
- package/dist/helpers/generateDelete.js.map +1 -0
- package/dist/helpers/generateDeleteMany.js +50 -0
- package/dist/helpers/generateDeleteMany.js.map +1 -0
- package/dist/helpers/generateFindFirst.js +61 -0
- package/dist/helpers/generateFindFirst.js.map +1 -0
- package/dist/helpers/generateFindMany.js +61 -0
- package/dist/helpers/generateFindMany.js.map +1 -0
- package/dist/helpers/generateFindUnique copy.js +49 -0
- package/dist/helpers/generateFindUnique copy.js.map +1 -0
- package/dist/helpers/generateFindUnique.js +61 -0
- package/dist/helpers/generateFindUnique.js.map +1 -0
- package/dist/helpers/generateGroupBy.js +50 -0
- package/dist/helpers/generateGroupBy.js.map +1 -0
- package/dist/helpers/generateImportPrismaStatement.js +26 -0
- package/dist/helpers/generateImportPrismaStatement.js.map +1 -0
- package/dist/helpers/generatePrismaImport.js +2 -0
- package/dist/helpers/generatePrismaImport.js.map +1 -0
- package/dist/helpers/generateRouteFile.js +181 -0
- package/dist/helpers/generateRouteFile.js.map +1 -0
- package/dist/helpers/generateUpdate.js +55 -0
- package/dist/helpers/generateUpdate.js.map +1 -0
- package/dist/helpers/generateUpdateMany.js +52 -0
- package/dist/helpers/generateUpdateMany.js.map +1 -0
- package/dist/helpers/generateUpsert.js +52 -0
- package/dist/helpers/generateUpsert.js.map +1 -0
- package/dist/utils/formatFile.js +26 -0
- package/dist/utils/formatFile.js.map +1 -0
- package/dist/utils/getGeneratorOptions.js +14 -0
- package/dist/utils/getGeneratorOptions.js.map +1 -0
- package/dist/utils/writeFileSafely.js +21 -0
- package/dist/utils/writeFileSafely.js.map +1 -0
- package/package.json +7 -3
- package/src/bin.ts +2 -0
- package/src/constants.ts +1 -0
- package/src/generator.ts +174 -0
- package/src/helpers/generateAggregate.ts +58 -0
- package/src/helpers/generateCount.ts +58 -0
- package/src/helpers/generateCreate.ts +58 -0
- package/src/helpers/generateCreateMany.ts +58 -0
- package/src/helpers/generateDelete.ts +60 -0
- package/src/helpers/generateDeleteMany.ts +58 -0
- package/src/helpers/generateFindFirst.ts +68 -0
- package/src/helpers/generateFindMany.ts +68 -0
- package/src/helpers/generateFindUnique.ts +68 -0
- package/src/helpers/generateGroupBy.ts +58 -0
- package/src/helpers/generateImportPrismaStatement.ts +38 -0
- package/src/helpers/generateRouteFile.ts +183 -0
- package/src/helpers/generateUpdate.ts +62 -0
- package/src/helpers/generateUpdateMany.ts +59 -0
- package/src/helpers/generateUpsert.ts +60 -0
- package/src/utils/formatFile.ts +22 -0
- package/src/utils/writeFileSafely.ts +28 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 multipliedtwice
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# Prisma Generator Express
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/prisma-generator-express)
|
|
4
|
+
[](https://www.npmjs.com/package/prisma-generator-express)
|
|
5
|
+
[](http://hits.dwyl.com/multipliedtwice/prisma-generator-express)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
This tool helps you quickly create API endpoints in your Express app using your Prisma models.
|
|
9
|
+
|
|
10
|
+
When you run `npx prisma generate`, it automatically creates two things:
|
|
11
|
+
|
|
12
|
+
- Service functions that you can import into your Express routes. By default these functions handle CRUD operations and output validation. This behavior can be controlled.
|
|
13
|
+
- Router generator function that lets you select which routes to add to the application and which middlewares to apply.
|
|
14
|
+
|
|
15
|
+
## Table of Contents
|
|
16
|
+
|
|
17
|
+
- [Installation](#installation)
|
|
18
|
+
- [Basic Usage](#basic-usage)
|
|
19
|
+
- [Router Generator Usage](#router-generator-usage)
|
|
20
|
+
- [Request Object Properties](#request-object-properties)
|
|
21
|
+
- [Roadmap](#roadmap)
|
|
22
|
+
|
|
23
|
+
# Installation
|
|
24
|
+
|
|
25
|
+
Using npm:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install prisma-generator-express
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Using yarn:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
yarn add prisma-generator-express
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
# Basic usage
|
|
38
|
+
|
|
39
|
+
- Include this generator in your schema.prisma file:
|
|
40
|
+
|
|
41
|
+
```prisma
|
|
42
|
+
generator express {
|
|
43
|
+
provider = "prisma-generator-express"
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
- Generate your middleware functions by running:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npx prisma generate
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
- Attach Prisma instance
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { PrismaClient } from '@prisma/client'
|
|
57
|
+
import express from 'express'
|
|
58
|
+
|
|
59
|
+
const prisma = new PrismaClient()
|
|
60
|
+
const app = express()
|
|
61
|
+
|
|
62
|
+
app.use(express.json()) // for parsing application/json
|
|
63
|
+
|
|
64
|
+
// Attach Prisma to every request
|
|
65
|
+
app.use((req, res, next) => {
|
|
66
|
+
req.prisma = prisma
|
|
67
|
+
next()
|
|
68
|
+
})
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
- Here’s how you can use a generated function in your Express app:
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { UserFindUnique } from './generated/UserFindUnique' // Adjust the path as necessary
|
|
75
|
+
import { FindUniqueUserSchema } from './prisma-zod-generator/schemas/FindUniqueUser.schema' // Adjust the path as necessary
|
|
76
|
+
import { FindUniqueUserSchemaOutput } from './prisma-zod-generator/schemas/FindUniqueUserOutput.schema' // Adjust the path as necessary
|
|
77
|
+
|
|
78
|
+
// move this to /helpers
|
|
79
|
+
export function validateQuery(schema: ZodSchema) {
|
|
80
|
+
return async (req: Request, res: Response, next: NextFunction) => {
|
|
81
|
+
try {
|
|
82
|
+
req.query = schema.parse(req.query)
|
|
83
|
+
next()
|
|
84
|
+
} catch (error) {
|
|
85
|
+
res.status(400).json({
|
|
86
|
+
error: 'Input Validation failed',
|
|
87
|
+
details: error.errors,
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
app.get(
|
|
94
|
+
'/user/:id',
|
|
95
|
+
validateQuery(FindUniqueUserSchema),
|
|
96
|
+
async (req, res, next) => {
|
|
97
|
+
// Attach generated Zod schema for output validation
|
|
98
|
+
req.outputValidation = FindUniqueUserOutput
|
|
99
|
+
|
|
100
|
+
// Use the generated middleware to handle the request
|
|
101
|
+
await UserFindUnique(req, res, next)
|
|
102
|
+
},
|
|
103
|
+
)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
The `UserFindUnique` function will fetch the user details from the database, validate the output with Zod, and handle the API response.
|
|
107
|
+
|
|
108
|
+
# Router generator usage
|
|
109
|
+
|
|
110
|
+
The library will create functions to generate routers per each model in schema. Each route can accept middleware that will be injected right before generated handler is invoked. You can use it to modify/remove/validate request payload. The output validation can be also attached to `req` object on this step.
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
import express from 'express'
|
|
114
|
+
import { UserRouter } from './path/to/generated/UserRouter' // Adjust the path as necessary
|
|
115
|
+
import { UserFindManyArgs } from './prisma-zod-generator/schemas/UserFindManyArgs.schema' // Adjust the path as necessary
|
|
116
|
+
|
|
117
|
+
const app = express()
|
|
118
|
+
|
|
119
|
+
export const validateUserQuery = (
|
|
120
|
+
req: Request,
|
|
121
|
+
res: Response,
|
|
122
|
+
next: NextFunction,
|
|
123
|
+
) => {
|
|
124
|
+
try {
|
|
125
|
+
UserFindManyArgs.parse(req.query)
|
|
126
|
+
next()
|
|
127
|
+
} catch (error) {
|
|
128
|
+
if (error instanceof ZodError) {
|
|
129
|
+
return res.status(400).json({
|
|
130
|
+
message: 'Validation failed',
|
|
131
|
+
errors: error.errors,
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
next(error)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const userRouterConfig = {
|
|
139
|
+
findManyMiddleware: [validateUserQuery], // Add other middleware as needed
|
|
140
|
+
findUniqueMiddleware: undefined, // This can be omitted, route won't be generated if middleware is not provided
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Use the router in your application
|
|
144
|
+
app.use('/users', UserRouter(userRouterConfig))
|
|
145
|
+
|
|
146
|
+
app.listen(3000, () => {
|
|
147
|
+
console.log('Server is running on http://localhost:3000')
|
|
148
|
+
})
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Request Object Properties
|
|
152
|
+
|
|
153
|
+
The following properties can be attached to the `req` object to control the behavior of generated middleware:
|
|
154
|
+
|
|
155
|
+
| Property | Type | Description |
|
|
156
|
+
| ---------------------- | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
157
|
+
| `prisma` | PrismaClient | An instance of PrismaClient that allows the middleware to interact with your database. |
|
|
158
|
+
| `passToNext` | boolean | Optional, if `true` - the result of a Prisma request will be passed to a next middleware as `req.locals.data` |
|
|
159
|
+
| `query` | Object | A structured object that conforms to Prisma's API for the selected method. |
|
|
160
|
+
| `outputValidation` | ZodTypeAny | (Optional) A Zod schema used to validate the data returned from the Prisma query before sending it to the client. This helps ensure the response adheres to expected data formats. |
|
|
161
|
+
| `omitOutputValidation` | Boolean | (Optional) A flag that, if set to `true`, disables output validation even if a Zod schema is provided. |
|
|
162
|
+
|
|
163
|
+
## Roadmap
|
|
164
|
+
|
|
165
|
+
| Function | Supported | Notes |
|
|
166
|
+
| -------------- | ------------------ | ---------- |
|
|
167
|
+
| `Model router` | :white_check_mark: | Available. |
|
|
168
|
+
| `findUnique` | :white_check_mark: | Available. |
|
|
169
|
+
| `findFirst` | :white_check_mark: | Available. |
|
|
170
|
+
| `findMany` | :white_check_mark: | Available. |
|
|
171
|
+
| `create` | :white_check_mark: | Available. |
|
|
172
|
+
| `createMany` | :white_check_mark: | Available. |
|
|
173
|
+
| `update` | :white_check_mark: | Available. |
|
|
174
|
+
| `updateMany` | :white_check_mark: | Available. |
|
|
175
|
+
| `upsert` | :white_check_mark: | Available. |
|
|
176
|
+
| `delete` | :white_check_mark: | Available. |
|
|
177
|
+
| `deleteMany` | :white_check_mark: | Available. |
|
|
178
|
+
| `aggregate` | :white_check_mark: | Available. |
|
|
179
|
+
| `count` | :white_check_mark: | Available. |
|
|
180
|
+
| `groupBy` | :white_check_mark: | Available. |
|
package/dist/bin.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.js","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";;;AACA,uBAAoB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,cAAc,GAAG,0BAA0B,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generator.js","sourceRoot":"","sources":["../src/generator.ts"],"names":[],"mappings":";;AAAA,+DAA6E;AAC7E,qCAAoC;AACpC,2CAA4C;AAC5C,6DAAyD;AACzD,qEAAyE;AACzE,2FAAuF;AACvF,iEAAqE;AACrE,mEAAuE;AACvE,6DAAiE;AACjE,mEAAoE;AACpE,qEAAyE;AACzE,6DAAiE;AACjE,qEAAyE;AACzE,6DAAiE;AACjE,6DAAiE;AACjE,qEAAyE;AACzE,mEAAuE;AACvE,2DAA+D;AAC/D,+DAAmE;AAEnE,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAA;AAE9C,IAAA,mCAAgB,EAAC;IACf,UAAU;QACR,YAAM,CAAC,IAAI,CAAC,GAAG,0BAAc,aAAa,CAAC,CAAA;QAC3C,OAAO;YACL,OAAO;YACP,aAAa,EAAE,cAAc;YAC7B,UAAU,EAAE,0BAAc;SAC3B,CAAA;IACH,CAAC;IACD,UAAU,EAAE,KAAK,EAAE,OAAyB,EAAE,EAAE;QAC9C,MAAM,qBAAqB,GAAG,IAAA,6DAA6B,EAAC,OAAO,CAAC,CAAA;QAEpE,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;YACpD,MAAM,IAAA,iCAAe,EAAC;gBACpB,OAAO,EAAE,IAAA,+CAA0B,EAAC;oBAClC,KAAK;oBACL,qBAAqB;iBACtB,CAAC;gBACF,OAAO;gBACP,KAAK;gBACL,SAAS,EAAE,YAAY;aACxB,CAAC,CAAA;YAEF,MAAM,IAAA,iCAAe,EAAC;gBACpB,OAAO,EAAE,IAAA,6CAAyB,EAAC;oBACjC,KAAK;oBACL,qBAAqB;iBACtB,CAAC;gBACF,OAAO;gBACP,KAAK;gBACL,SAAS,EAAE,WAAW;aACvB,CAAC,CAAA;YAEF,MAAM,IAAA,iCAAe,EAAC;gBACpB,OAAO,EAAE,IAAA,2CAAwB,EAAC;oBAChC,KAAK;oBACL,qBAAqB;iBACtB,CAAC;gBACF,OAAO;gBACP,KAAK;gBACL,SAAS,EAAE,UAAU;aACtB,CAAC,CAAA;YAEF,MAAM,IAAA,iCAAe,EAAC;gBACpB,OAAO,EAAE,IAAA,uCAAsB,EAAC;oBAC9B,KAAK;oBACL,qBAAqB;iBACtB,CAAC;gBACF,OAAO;gBACP,KAAK;gBACL,SAAS,EAAE,QAAQ;aACpB,CAAC,CAAA;YAEF,MAAM,IAAA,iCAAe,EAAC;gBACpB,OAAO,EAAE,IAAA,+CAA0B,EAAC;oBAClC,KAAK;oBACL,qBAAqB;iBACtB,CAAC;gBACF,OAAO;gBACP,KAAK;gBACL,SAAS,EAAE,YAAY;aACxB,CAAC,CAAA;YAEF,MAAM,IAAA,iCAAe,EAAC;gBACpB,OAAO,EAAE,IAAA,uCAAsB,EAAC;oBAC9B,KAAK;oBACL,qBAAqB;iBACtB,CAAC;gBACF,OAAO;gBACP,KAAK;gBACL,SAAS,EAAE,QAAQ;aACpB,CAAC,CAAA;YAEF,MAAM,IAAA,iCAAe,EAAC;gBACpB,OAAO,EAAE,IAAA,+CAA0B,EAAC;oBAClC,KAAK;oBACL,qBAAqB;iBACtB,CAAC;gBACF,OAAO;gBACP,KAAK;gBACL,SAAS,EAAE,YAAY;aACxB,CAAC,CAAA;YAEF,MAAM,IAAA,iCAAe,EAAC;gBACpB,OAAO,EAAE,IAAA,uCAAsB,EAAC;oBAC9B,KAAK;oBACL,qBAAqB;iBACtB,CAAC;gBACF,OAAO;gBACP,KAAK;gBACL,SAAS,EAAE,QAAQ;aACpB,CAAC,CAAA;YAEF,MAAM,IAAA,iCAAe,EAAC;gBACpB,OAAO,EAAE,IAAA,uCAAsB,EAAC;oBAC9B,KAAK;oBACL,qBAAqB;iBACtB,CAAC;gBACF,OAAO;gBACP,KAAK;gBACL,SAAS,EAAE,QAAQ;aACpB,CAAC,CAAA;YAEF,MAAM,IAAA,iCAAe,EAAC;gBACpB,OAAO,EAAE,IAAA,+CAA0B,EAAC;oBAClC,KAAK;oBACL,qBAAqB;iBACtB,CAAC;gBACF,OAAO;gBACP,KAAK;gBACL,SAAS,EAAE,YAAY;aACxB,CAAC,CAAA;YAEF,MAAM,IAAA,iCAAe,EAAC;gBACpB,OAAO,EAAE,IAAA,6CAAyB,EAAC;oBACjC,KAAK;oBACL,qBAAqB;iBACtB,CAAC;gBACF,OAAO;gBACP,KAAK;gBACL,SAAS,EAAE,WAAW;aACvB,CAAC,CAAA;YAEF,MAAM,IAAA,iCAAe,EAAC;gBACpB,OAAO,EAAE,IAAA,qCAAqB,EAAC;oBAC7B,KAAK;oBACL,qBAAqB;iBACtB,CAAC;gBACF,OAAO;gBACP,KAAK;gBACL,SAAS,EAAE,OAAO;aACnB,CAAC,CAAA;YAEF,MAAM,IAAA,iCAAe,EAAC;gBACpB,OAAO,EAAE,IAAA,yCAAuB,EAAC;oBAC/B,KAAK;oBACL,qBAAqB;iBACtB,CAAC;gBACF,OAAO;gBACP,KAAK;gBACL,SAAS,EAAE,SAAS;aACrB,CAAC,CAAA;YAEF,MAAM,IAAA,iCAAe,EAAC;gBACpB,OAAO,EAAE,IAAA,0CAAsB,EAAC,EAAE,KAAK,EAAE,CAAC;gBAC1C,OAAO;gBACP,KAAK;gBACL,SAAS,EAAE,OAAO;aACnB,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC;CACF,CAAC,CAAA"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.genEnum = void 0;
|
|
4
|
+
const genEnum = ({ name, values }) => {
|
|
5
|
+
const enumValues = values.map(({ name }) => `${name}="${name}"`).join(',\n');
|
|
6
|
+
return `enum ${name} { \n${enumValues}\n }`;
|
|
7
|
+
};
|
|
8
|
+
exports.genEnum = genEnum;
|
|
9
|
+
//# sourceMappingURL=genEnum.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"genEnum.js","sourceRoot":"","sources":["../../src/helpers/genEnum.ts"],"names":[],"mappings":";;;AAEO,MAAM,OAAO,GAAG,CAAC,EAAE,IAAI,EAAE,MAAM,EAAsB,EAAE,EAAE;IAC9D,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,GAAG,IAAI,KAAK,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IAE5E,OAAO,QAAQ,IAAI,QAAQ,UAAU,MAAM,CAAA;AAC7C,CAAC,CAAA;AAJY,QAAA,OAAO,WAInB"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateAggregateFunction = void 0;
|
|
4
|
+
const generateAggregateFunction = (options) => {
|
|
5
|
+
const { model, prismaImportStatement } = options;
|
|
6
|
+
const modelName = model.name;
|
|
7
|
+
const functionName = `${modelName}Aggregate`;
|
|
8
|
+
const argsTypeName = `Prisma.${modelName}AggregateArgs`;
|
|
9
|
+
return `
|
|
10
|
+
${prismaImportStatement}
|
|
11
|
+
import { Request, Response, NextFunction } from 'express';
|
|
12
|
+
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core'
|
|
13
|
+
import { ZodTypeAny } from 'zod';
|
|
14
|
+
|
|
15
|
+
interface AggregateRequest extends Request {
|
|
16
|
+
prisma: PrismaClient;
|
|
17
|
+
body: ${argsTypeName};
|
|
18
|
+
outputValidation?: ZodTypeAny;
|
|
19
|
+
omitOutputValidation?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type AggregateMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>;
|
|
23
|
+
|
|
24
|
+
export async function ${functionName}(req: AggregateRequest, res: Response, next: NextFunction) {
|
|
25
|
+
try {
|
|
26
|
+
if (!req.outputValidation && !req.omitOutputValidation) {
|
|
27
|
+
throw new Error('Output validation schema or omission flag must be provided.');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const result = await req.prisma.${modelName.toLowerCase()}.aggregate(req.body);
|
|
31
|
+
|
|
32
|
+
if (!req.omitOutputValidation && req.outputValidation) {
|
|
33
|
+
const validationResult = req.outputValidation.safeParse(result);
|
|
34
|
+
if (validationResult.success) {
|
|
35
|
+
res.status(200).json(validationResult.data);
|
|
36
|
+
} else {
|
|
37
|
+
res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
res.status(200).json(result);
|
|
41
|
+
}
|
|
42
|
+
} catch (error) {
|
|
43
|
+
console.error('Error in handling aggregation request:', error);
|
|
44
|
+
res.status(500).json({ error: error.message });
|
|
45
|
+
next(error);
|
|
46
|
+
}
|
|
47
|
+
}`;
|
|
48
|
+
};
|
|
49
|
+
exports.generateAggregateFunction = generateAggregateFunction;
|
|
50
|
+
//# sourceMappingURL=generateAggregate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateAggregate.js","sourceRoot":"","sources":["../../src/helpers/generateAggregate.ts"],"names":[],"mappings":";;;AASO,MAAM,yBAAyB,GAAG,CAAC,OAGzC,EAAU,EAAE;IACX,MAAM,EAAE,KAAK,EAAE,qBAAqB,EAAE,GAAG,OAAO,CAAA;IAChD,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,YAAY,GAAG,GAAG,SAAS,WAAW,CAAA;IAC5C,MAAM,YAAY,GAAG,UAAU,SAAS,eAAe,CAAA;IAEvD,OAAO;EACP,qBAAqB;;;;;;;UAOb,YAAY;;;;;0EAKoD,YAAY;;wBAE9D,YAAY;;;;;;sCAME,SAAS,CAAC,WAAW,EAAE;;;;;;;;;;;;;;;;;EAiB3D,CAAA;AACF,CAAC,CAAA;AAhDY,QAAA,yBAAyB,6BAgDrC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateCountFunction = void 0;
|
|
4
|
+
const generateCountFunction = (options) => {
|
|
5
|
+
const { model, prismaImportStatement } = options;
|
|
6
|
+
const modelName = model.name;
|
|
7
|
+
const functionName = `${modelName}Count`;
|
|
8
|
+
const argsTypeName = `Prisma.${modelName}CountArgs`;
|
|
9
|
+
return `
|
|
10
|
+
${prismaImportStatement}
|
|
11
|
+
import { Request, Response, NextFunction } from 'express';
|
|
12
|
+
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core'
|
|
13
|
+
import { ZodTypeAny } from 'zod';
|
|
14
|
+
|
|
15
|
+
interface CountRequest extends Request {
|
|
16
|
+
prisma: PrismaClient;
|
|
17
|
+
body: ${argsTypeName};
|
|
18
|
+
outputValidation?: ZodTypeAny;
|
|
19
|
+
omitOutputValidation?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type CountMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>;
|
|
23
|
+
|
|
24
|
+
export async function ${functionName}(req: CountRequest, res: Response, next: NextFunction) {
|
|
25
|
+
try {
|
|
26
|
+
if (!req.outputValidation && !req.omitOutputValidation) {
|
|
27
|
+
throw new Error('Output validation schema or omission flag must be provided.');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const result = await req.prisma.${modelName.toLowerCase()}.count(req.body);
|
|
31
|
+
|
|
32
|
+
if (!req.omitOutputValidation && req.outputValidation) {
|
|
33
|
+
const validationResult = req.outputValidation.safeParse(result);
|
|
34
|
+
if (validationResult.success) {
|
|
35
|
+
res.status(200).json(validationResult.data);
|
|
36
|
+
} else {
|
|
37
|
+
res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
res.status(200).json(result);
|
|
41
|
+
}
|
|
42
|
+
} catch (error) {
|
|
43
|
+
console.error('Error in handling count request:', error);
|
|
44
|
+
res.status(500).json({ error: error.message });
|
|
45
|
+
next(error);
|
|
46
|
+
}
|
|
47
|
+
}`;
|
|
48
|
+
};
|
|
49
|
+
exports.generateCountFunction = generateCountFunction;
|
|
50
|
+
//# sourceMappingURL=generateCount.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateCount.js","sourceRoot":"","sources":["../../src/helpers/generateCount.ts"],"names":[],"mappings":";;;AASO,MAAM,qBAAqB,GAAG,CAAC,OAGrC,EAAU,EAAE;IACX,MAAM,EAAE,KAAK,EAAE,qBAAqB,EAAE,GAAG,OAAO,CAAA;IAChD,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,YAAY,GAAG,GAAG,SAAS,OAAO,CAAA;IACxC,MAAM,YAAY,GAAG,UAAU,SAAS,WAAW,CAAA;IAEnD,OAAO;EACP,qBAAqB;;;;;;;UAOb,YAAY;;;;;sEAKgD,YAAY;;wBAE1D,YAAY;;;;;;sCAME,SAAS,CAAC,WAAW,EAAE;;;;;;;;;;;;;;;;;EAiB3D,CAAA;AACF,CAAC,CAAA;AAhDY,QAAA,qBAAqB,yBAgDjC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateCreateFunction = void 0;
|
|
4
|
+
const generateCreateFunction = (options) => {
|
|
5
|
+
const { model, prismaImportStatement } = options;
|
|
6
|
+
const modelName = model.name;
|
|
7
|
+
const functionName = `${modelName}Create`;
|
|
8
|
+
const argsTypeName = `Prisma.${modelName}CreateArgs`;
|
|
9
|
+
return `
|
|
10
|
+
${prismaImportStatement}
|
|
11
|
+
import { Request, Response, NextFunction } from 'express';
|
|
12
|
+
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core'
|
|
13
|
+
import { ZodTypeAny } from 'zod';
|
|
14
|
+
|
|
15
|
+
interface CreateRequest extends Request {
|
|
16
|
+
prisma: PrismaClient;
|
|
17
|
+
body: ${argsTypeName};
|
|
18
|
+
outputValidation?: ZodTypeAny;
|
|
19
|
+
omitOutputValidation?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type CreateMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>
|
|
23
|
+
|
|
24
|
+
export async function ${functionName}(req: CreateRequest, res: Response, next: NextFunction) {
|
|
25
|
+
try {
|
|
26
|
+
if (!req.outputValidation && !req.omitOutputValidation) {
|
|
27
|
+
throw new Error('Output validation schema or omission flag must be provided.');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const data = await req.prisma.${modelName.toLowerCase()}.create(req.body);
|
|
31
|
+
if (!req.omitOutputValidation && req.outputValidation) {
|
|
32
|
+
const validationResult = req.outputValidation.safeParse(data);
|
|
33
|
+
if (validationResult.success) {
|
|
34
|
+
res.status(201).json(validationResult.data);
|
|
35
|
+
} else {
|
|
36
|
+
res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
37
|
+
}
|
|
38
|
+
} else if (!req.omitOutputValidation) {
|
|
39
|
+
throw new Error('Output validation schema must be provided unless explicitly omitted.');
|
|
40
|
+
} else {
|
|
41
|
+
res.status(201).json(data);
|
|
42
|
+
}
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error('Error in handling create request:', error);
|
|
45
|
+
res.status(500).json({ error: error.message });
|
|
46
|
+
next(error);
|
|
47
|
+
}
|
|
48
|
+
}`;
|
|
49
|
+
};
|
|
50
|
+
exports.generateCreateFunction = generateCreateFunction;
|
|
51
|
+
//# sourceMappingURL=generateCreate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateCreate.js","sourceRoot":"","sources":["../../src/helpers/generateCreate.ts"],"names":[],"mappings":";;;AAQO,MAAM,sBAAsB,GAAG,CAAC,OAGtC,EAAU,EAAE;IACX,MAAM,EAAE,KAAK,EAAE,qBAAqB,EAAE,GAAG,OAAO,CAAA;IAChD,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,YAAY,GAAG,GAAG,SAAS,QAAQ,CAAA;IACzC,MAAM,YAAY,GAAG,UAAU,SAAS,YAAY,CAAA;IAEpD,OAAO;EACP,qBAAqB;;;;;;;UAOb,YAAY;;;;;uEAKiD,YAAY;;wBAE3D,YAAY;;;;;;oCAMA,SAAS,CAAC,WAAW,EAAE;;;;;;;;;;;;;;;;;;EAkBzD,CAAA;AACF,CAAC,CAAA;AAjDY,QAAA,sBAAsB,0BAiDlC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateCreateManyFunction = void 0;
|
|
4
|
+
const generateCreateManyFunction = (options) => {
|
|
5
|
+
const { model, prismaImportStatement } = options;
|
|
6
|
+
const modelName = model.name;
|
|
7
|
+
const functionName = `${modelName}CreateMany`;
|
|
8
|
+
const argsTypeName = `Prisma.${modelName}CreateManyArgs`;
|
|
9
|
+
return `
|
|
10
|
+
${prismaImportStatement}
|
|
11
|
+
import { Request, Response, NextFunction } from 'express';
|
|
12
|
+
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core';
|
|
13
|
+
import { ZodTypeAny } from 'zod';
|
|
14
|
+
|
|
15
|
+
interface CreateManyRequest extends Request {
|
|
16
|
+
prisma: PrismaClient;
|
|
17
|
+
body: ${argsTypeName};
|
|
18
|
+
outputValidation?: ZodTypeAny;
|
|
19
|
+
omitOutputValidation?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type CreateManyMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>
|
|
23
|
+
|
|
24
|
+
export async function ${functionName}(req: CreateManyRequest, res: Response, next: NextFunction) {
|
|
25
|
+
try {
|
|
26
|
+
if (!req.outputValidation && !req.omitOutputValidation) {
|
|
27
|
+
throw new Error('Output validation schema or omission flag must be provided.');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const data = await req.prisma.${modelName.toLowerCase()}.createMany(req.body);
|
|
31
|
+
if (!req.omitOutputValidation && req.outputValidation) {
|
|
32
|
+
const validationResult = req.outputValidation.safeParse(data);
|
|
33
|
+
if (validationResult.success) {
|
|
34
|
+
res.status(201).json(validationResult.data);
|
|
35
|
+
} else {
|
|
36
|
+
res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
37
|
+
}
|
|
38
|
+
} else if (!req.omitOutputValidation) {
|
|
39
|
+
throw new Error('Output validation schema must be provided unless explicitly omitted.');
|
|
40
|
+
} else {
|
|
41
|
+
res.status(201).json(data);
|
|
42
|
+
}
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error('Error in handling createMany request:', error);
|
|
45
|
+
res.status(500).json({ error: error.message });
|
|
46
|
+
next(error);
|
|
47
|
+
}
|
|
48
|
+
}`;
|
|
49
|
+
};
|
|
50
|
+
exports.generateCreateManyFunction = generateCreateManyFunction;
|
|
51
|
+
//# sourceMappingURL=generateCreateMany.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateCreateMany.js","sourceRoot":"","sources":["../../src/helpers/generateCreateMany.ts"],"names":[],"mappings":";;;AAQO,MAAM,0BAA0B,GAAG,CAAC,OAG1C,EAAU,EAAE;IACX,MAAM,EAAE,KAAK,EAAE,qBAAqB,EAAE,GAAG,OAAO,CAAA;IAChD,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,YAAY,GAAG,GAAG,SAAS,YAAY,CAAA;IAC7C,MAAM,YAAY,GAAG,UAAU,SAAS,gBAAgB,CAAA;IAExD,OAAO;EACP,qBAAqB;;;;;;;UAOb,YAAY;;;;;2EAKqD,YAAY;;wBAE/D,YAAY;;;;;;oCAMA,SAAS,CAAC,WAAW,EAAE;;;;;;;;;;;;;;;;;;EAkBzD,CAAA;AACF,CAAC,CAAA;AAjDY,QAAA,0BAA0B,8BAiDtC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateDeleteFunction = void 0;
|
|
4
|
+
const generateDeleteFunction = (options) => {
|
|
5
|
+
const { model, prismaImportStatement } = options;
|
|
6
|
+
const modelName = model.name;
|
|
7
|
+
const functionName = `${modelName}Delete`;
|
|
8
|
+
const argsTypeName = `Prisma.${modelName}DeleteArgs`;
|
|
9
|
+
return `
|
|
10
|
+
${prismaImportStatement}
|
|
11
|
+
import { Request, Response, NextFunction } from 'express';
|
|
12
|
+
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core';
|
|
13
|
+
import { ZodTypeAny } from 'zod';
|
|
14
|
+
|
|
15
|
+
interface DeleteRequest extends Request {
|
|
16
|
+
prisma: PrismaClient;
|
|
17
|
+
body: ${argsTypeName};
|
|
18
|
+
outputValidation?: ZodTypeAny;
|
|
19
|
+
omitOutputValidation?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type DeleteMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>
|
|
23
|
+
|
|
24
|
+
export async function ${functionName}(req: DeleteRequest, res: Response, next: NextFunction) {
|
|
25
|
+
try {
|
|
26
|
+
if (!req.outputValidation && !req.omitOutputValidation) {
|
|
27
|
+
throw new Error('Output validation schema or omission flag must be provided.');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const data = await req.prisma.${modelName.toLowerCase()}.delete(req.body);
|
|
31
|
+
|
|
32
|
+
if (!req.omitOutputValidation && req.outputValidation) {
|
|
33
|
+
const validationResult = req.outputValidation.safeParse(data);
|
|
34
|
+
if (validationResult.success) {
|
|
35
|
+
res.status(200).json(validationResult.data);
|
|
36
|
+
} else {
|
|
37
|
+
res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
38
|
+
}
|
|
39
|
+
} else if (!req.omitOutputValidation) {
|
|
40
|
+
throw new Error('Output validation schema must be provided unless explicitly omitted.');
|
|
41
|
+
} else {
|
|
42
|
+
res.status(200).json(data);
|
|
43
|
+
}
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.error('Error in handling delete request:', error);
|
|
46
|
+
res.status(500).json({ error: error.message });
|
|
47
|
+
next(error);
|
|
48
|
+
}
|
|
49
|
+
}`;
|
|
50
|
+
};
|
|
51
|
+
exports.generateDeleteFunction = generateDeleteFunction;
|
|
52
|
+
//# sourceMappingURL=generateDelete.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateDelete.js","sourceRoot":"","sources":["../../src/helpers/generateDelete.ts"],"names":[],"mappings":";;;AASO,MAAM,sBAAsB,GAAG,CAAC,OAGtC,EAAU,EAAE;IACX,MAAM,EAAE,KAAK,EAAE,qBAAqB,EAAE,GAAG,OAAO,CAAA;IAChD,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,YAAY,GAAG,GAAG,SAAS,QAAQ,CAAA;IACzC,MAAM,YAAY,GAAG,UAAU,SAAS,YAAY,CAAA;IAEpD,OAAO;EACP,qBAAqB;;;;;;;UAOb,YAAY;;;;;uEAKiD,YAAY;;wBAE3D,YAAY;;;;;;oCAMA,SAAS,CAAC,WAAW,EAAE;;;;;;;;;;;;;;;;;;;EAmBzD,CAAA;AACF,CAAC,CAAA;AAlDY,QAAA,sBAAsB,0BAkDlC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateDeleteManyFunction = void 0;
|
|
4
|
+
const generateDeleteManyFunction = (options) => {
|
|
5
|
+
const { model, prismaImportStatement } = options;
|
|
6
|
+
const modelName = model.name;
|
|
7
|
+
const functionName = `${modelName}DeleteMany`;
|
|
8
|
+
const argsTypeName = `Prisma.${modelName}DeleteManyArgs`;
|
|
9
|
+
return `
|
|
10
|
+
${prismaImportStatement}
|
|
11
|
+
import { Request, Response, NextFunction } from 'express';
|
|
12
|
+
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core';
|
|
13
|
+
import { ZodTypeAny } from 'zod';
|
|
14
|
+
|
|
15
|
+
interface DeleteManyRequest extends Request {
|
|
16
|
+
prisma: PrismaClient;
|
|
17
|
+
body: ${argsTypeName};
|
|
18
|
+
outputValidation?: ZodTypeAny;
|
|
19
|
+
omitOutputValidation?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type DeleteManyMiddleware = RequestHandler<ParamsDictionary, any, Prisma.${modelName}DeleteManyArgs, Record<string, any>>;
|
|
23
|
+
|
|
24
|
+
export async function ${functionName}(req: DeleteManyRequest, res: Response, next: NextFunction) {
|
|
25
|
+
try {
|
|
26
|
+
if (!req.outputValidation && !req.omitOutputValidation) {
|
|
27
|
+
throw new Error('Output validation schema or omission flag must be provided.');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const result = await req.prisma.${modelName.toLowerCase()}.deleteMany(req.body);
|
|
31
|
+
|
|
32
|
+
if (!req.omitOutputValidation && req.outputValidation) {
|
|
33
|
+
const validationResult = req.outputValidation.safeParse(result);
|
|
34
|
+
if (validationResult.success) {
|
|
35
|
+
res.status(200).json(validationResult.data);
|
|
36
|
+
} else {
|
|
37
|
+
res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
res.status(200).json(result);
|
|
41
|
+
}
|
|
42
|
+
} catch (error) {
|
|
43
|
+
console.error('Error in handling batch delete request:', error);
|
|
44
|
+
res.status(500).json({ error: error.message });
|
|
45
|
+
next(error);
|
|
46
|
+
}
|
|
47
|
+
}`;
|
|
48
|
+
};
|
|
49
|
+
exports.generateDeleteManyFunction = generateDeleteManyFunction;
|
|
50
|
+
//# sourceMappingURL=generateDeleteMany.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateDeleteMany.js","sourceRoot":"","sources":["../../src/helpers/generateDeleteMany.ts"],"names":[],"mappings":";;;AASO,MAAM,0BAA0B,GAAG,CAAC,OAG1C,EAAU,EAAE;IACX,MAAM,EAAE,KAAK,EAAE,qBAAqB,EAAE,GAAG,OAAO,CAAA;IAChD,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,YAAY,GAAG,GAAG,SAAS,YAAY,CAAA;IAC7C,MAAM,YAAY,GAAG,UAAU,SAAS,gBAAgB,CAAA;IAExD,OAAO;EACP,qBAAqB;;;;;;;UAOb,YAAY;;;;;kFAK4D,SAAS;;wBAEnE,YAAY;;;;;;sCAME,SAAS,CAAC,WAAW,EAAE;;;;;;;;;;;;;;;;;EAiB3D,CAAA;AACF,CAAC,CAAA;AAhDY,QAAA,0BAA0B,8BAgDtC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateFindFirstFunction = void 0;
|
|
4
|
+
const generateFindFirstFunction = (options) => {
|
|
5
|
+
const { model, prismaImportStatement } = options;
|
|
6
|
+
const modelName = model.name;
|
|
7
|
+
const functionName = `${modelName}FindFirst`;
|
|
8
|
+
const queryTypeName = `Prisma.${modelName}FindFirstArgs`;
|
|
9
|
+
return `
|
|
10
|
+
${prismaImportStatement.replace('{ Prisma }', `{ Prisma, ${modelName} }`)}
|
|
11
|
+
import { Request, Response, NextFunction } from 'express'
|
|
12
|
+
import {
|
|
13
|
+
RequestHandler,
|
|
14
|
+
ParamsDictionary,
|
|
15
|
+
} from 'express-serve-static-core'
|
|
16
|
+
import { ParsedQs } from 'qs';
|
|
17
|
+
import { ZodTypeAny } from 'zod';
|
|
18
|
+
|
|
19
|
+
export interface FindFirstRequest extends Request {
|
|
20
|
+
prisma: PrismaClient;
|
|
21
|
+
query: ${queryTypeName} & ParsedQs;
|
|
22
|
+
outputValidation?: ZodTypeAny;
|
|
23
|
+
omitOutputValidation?: boolean;
|
|
24
|
+
passToNext?: boolean;
|
|
25
|
+
locals: {
|
|
26
|
+
data?: ${modelName} | null
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export type FindFirstMiddleware = RequestHandler<ParamsDictionary, any, any, ${queryTypeName} & ParsedQs, Record<string, any>>
|
|
30
|
+
|
|
31
|
+
export async function ${functionName}(req: FindFirstRequest, res: Response, next: NextFunction) {
|
|
32
|
+
try {
|
|
33
|
+
if (!req.outputValidation && !req.omitOutputValidation) {
|
|
34
|
+
throw new Error('Output validation schema or omission flag must be provided.');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const data = await req.prisma.${modelName.toLowerCase()}.findFirst(req.query as ${queryTypeName});
|
|
38
|
+
if (req.passToNext) {
|
|
39
|
+
req.locals.data = data;
|
|
40
|
+
next();
|
|
41
|
+
} else if (!req.omitOutputValidation && req.outputValidation) {
|
|
42
|
+
const validationResult = req.outputValidation.safeParse(data);
|
|
43
|
+
if (validationResult.success) {
|
|
44
|
+
res.status(200).json(validationResult.data);
|
|
45
|
+
} else {
|
|
46
|
+
res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
47
|
+
}
|
|
48
|
+
} else if (!req.omitOutputValidation) {
|
|
49
|
+
throw new Error('Output validation schema must be provided unless explicitly omitted. Attach omitOutputValidation = true to request to suppress this error.');
|
|
50
|
+
} else {
|
|
51
|
+
res.status(200).json(data);
|
|
52
|
+
}
|
|
53
|
+
} catch (error) {
|
|
54
|
+
console.error('Error in handling request:', error);
|
|
55
|
+
res.status(500).json({ error: error.message });
|
|
56
|
+
next(error);
|
|
57
|
+
}
|
|
58
|
+
}`;
|
|
59
|
+
};
|
|
60
|
+
exports.generateFindFirstFunction = generateFindFirstFunction;
|
|
61
|
+
//# sourceMappingURL=generateFindFirst.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateFindFirst.js","sourceRoot":"","sources":["../../src/helpers/generateFindFirst.ts"],"names":[],"mappings":";;;AAQO,MAAM,yBAAyB,GAAG,CAAC,OAGzC,EAAU,EAAE;IACX,MAAM,EAAE,KAAK,EAAE,qBAAqB,EAAE,GAAG,OAAO,CAAA;IAChD,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,YAAY,GAAG,GAAG,SAAS,WAAW,CAAA;IAC5C,MAAM,aAAa,GAAG,UAAU,SAAS,eAAe,CAAA;IAExD,OAAO;EACP,qBAAqB,CAAC,OAAO,CAAC,YAAY,EAAE,aAAa,SAAS,IAAI,CAAC;;;;;;;;;;;WAW9D,aAAa;;;;;aAKX,SAAS;;;+EAGyD,aAAa;;wBAEpE,YAAY;;;;;;oCAMA,SAAS,CAAC,WAAW,EAAE,2BAA2B,aAAa;;;;;;;;;;;;;;;;;;;;;EAqBjG,CAAA;AACF,CAAC,CAAA;AA3DY,QAAA,yBAAyB,6BA2DrC"}
|