prisma-generator-express 1.7.1 → 1.8.0
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/README.md +50 -21
- package/package.json +2 -2
- package/{dist → src}/bin.js +0 -1
- package/{dist → src}/constants.js +0 -1
- package/src/generator.js +195 -0
- package/src/generator.ts +7 -0
- package/{dist → src}/helpers/generateAggregate.js +14 -4
- package/src/helpers/generateAggregate.ts +7 -3
- package/{dist → src}/helpers/generateCount.js +14 -4
- package/src/helpers/generateCount.ts +7 -3
- package/{dist → src}/helpers/generateCreate.js +13 -4
- package/src/helpers/generateCreate.ts +7 -3
- package/{dist → src}/helpers/generateCreateMany.js +12 -3
- package/src/helpers/generateCreateMany.ts +6 -2
- package/{dist → src}/helpers/generateDelete.js +13 -3
- package/src/helpers/generateDelete.ts +6 -2
- package/{dist → src}/helpers/generateDeleteMany.js +13 -3
- package/src/helpers/generateDeleteMany.ts +6 -2
- package/{dist → src}/helpers/generateFindFirst.js +12 -3
- package/src/helpers/generateFindFirst.ts +6 -2
- package/{dist → src}/helpers/generateFindMany.js +12 -3
- package/src/helpers/generateFindMany.ts +6 -2
- package/{dist → src}/helpers/generateFindUnique.js +13 -4
- package/src/helpers/generateFindUnique.ts +7 -3
- package/{dist → src}/helpers/generateGroupBy.js +13 -3
- package/src/helpers/generateGroupBy.ts +6 -2
- package/{dist → src}/helpers/generateImportPrismaStatement.js +1 -1
- package/src/helpers/generateRouteConfigType.js +32 -0
- package/src/helpers/generateRouteConfigType.ts +28 -0
- package/src/helpers/generateRouteFile.js +155 -0
- package/src/helpers/generateRouteFile.ts +96 -121
- package/{dist → src}/helpers/generateUpdate.js +12 -3
- package/src/helpers/generateUpdate.ts +6 -2
- package/{dist → src}/helpers/generateUpdateMany.js +12 -3
- package/src/helpers/generateUpdateMany.ts +6 -2
- package/{dist → src}/helpers/generateUpsert.js +13 -3
- package/src/helpers/generateUpsert.ts +6 -2
- package/{dist → src}/utils/formatFile.js +2 -6
- package/{dist → src}/utils/strings.js +0 -1
- package/src/utils/writeFileSafely.js +29 -0
- package/src/utils/writeFileSafely.ts +4 -3
- package/dist/bin.js.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/generator.js +0 -161
- package/dist/generator.js.map +0 -1
- package/dist/helpers/generateAggregate.js.map +0 -1
- package/dist/helpers/generateCount.js.map +0 -1
- package/dist/helpers/generateCreate.js.map +0 -1
- package/dist/helpers/generateCreateMany.js.map +0 -1
- package/dist/helpers/generateDelete.js.map +0 -1
- package/dist/helpers/generateDeleteMany.js.map +0 -1
- package/dist/helpers/generateFindFirst.js.map +0 -1
- package/dist/helpers/generateFindMany.js.map +0 -1
- package/dist/helpers/generateFindUnique.js.map +0 -1
- package/dist/helpers/generateGroupBy.js.map +0 -1
- package/dist/helpers/generateImportPrismaStatement.js.map +0 -1
- package/dist/helpers/generateRouteFile.js +0 -181
- package/dist/helpers/generateRouteFile.js.map +0 -1
- package/dist/helpers/generateUpdate.js.map +0 -1
- package/dist/helpers/generateUpdateMany.js.map +0 -1
- package/dist/helpers/generateUpsert.js.map +0 -1
- package/dist/utils/formatFile.js.map +0 -1
- package/dist/utils/strings.js.map +0 -1
- package/dist/utils/writeFileSafely.js +0 -21
- package/dist/utils/writeFileSafely.js.map +0 -1
package/README.md
CHANGED
|
@@ -111,38 +111,67 @@ The `UserFindUnique` function will fetch the user details from the database, val
|
|
|
111
111
|
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.
|
|
112
112
|
|
|
113
113
|
```ts
|
|
114
|
-
import express from 'express'
|
|
115
|
-
import {
|
|
116
|
-
|
|
114
|
+
import express, { json } from 'express'
|
|
115
|
+
import type { Response, Request, NextFunction, RequestHandler } from 'express'
|
|
116
|
+
|
|
117
|
+
import { orderItemRouter } from '../prisma/generated/express/orderItem'
|
|
118
|
+
import RouteConfig from '../prisma/generated/express/RouteConfig'
|
|
119
|
+
import { PrismaClient } from '../prisma/generated/client'
|
|
117
120
|
|
|
118
121
|
const app = express()
|
|
119
122
|
|
|
120
|
-
|
|
123
|
+
const prisma = new PrismaClient()
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Middleware to attach Prisma client instance to the request object.
|
|
127
|
+
* This ensures that Prisma client is available in all subsequent middleware and route handlers.
|
|
128
|
+
*/
|
|
129
|
+
const addPrisma: RequestHandler = (
|
|
121
130
|
req: Request,
|
|
122
131
|
res: Response,
|
|
123
132
|
next: NextFunction,
|
|
124
133
|
) => {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
134
|
+
req.prisma = prisma
|
|
135
|
+
req.omitOutputValidation = true
|
|
136
|
+
next()
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Before middleware to set a custom property on the request object.
|
|
141
|
+
* Demonstrates how to add custom properties to the request object to be used in later middleware or route handlers.
|
|
142
|
+
*/
|
|
143
|
+
const beforeFindMany: RequestHandler = (
|
|
144
|
+
req: Request,
|
|
145
|
+
res: Response,
|
|
146
|
+
next: NextFunction,
|
|
147
|
+
) => {
|
|
148
|
+
;(req as any).passToNext = true
|
|
149
|
+
next()
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* After middleware placeholder for any post-processing after the main route handler.
|
|
154
|
+
* This example just calls next() but can be extended to perform actions like logging or response modification.
|
|
155
|
+
*/
|
|
156
|
+
const afterFindMany: RequestHandler = (
|
|
157
|
+
req: Request,
|
|
158
|
+
res: Response,
|
|
159
|
+
next: NextFunction,
|
|
160
|
+
) => {
|
|
161
|
+
console.log('req.locals?.data :>> ', req.locals?.data)
|
|
162
|
+
next()
|
|
137
163
|
}
|
|
138
164
|
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
165
|
+
const someRouterConfig: RouteConfig<RequestHandler> = {
|
|
166
|
+
findMany: {
|
|
167
|
+
before: [beforeFindMany],
|
|
168
|
+
after: [afterFindMany],
|
|
169
|
+
},
|
|
170
|
+
addModelPrefix: true,
|
|
171
|
+
enableAll: true,
|
|
142
172
|
}
|
|
143
173
|
|
|
144
|
-
|
|
145
|
-
app.use('/users', UserRouter(userRouterConfig))
|
|
174
|
+
app.use(addPrisma, orderItemRouter(someRouterConfig))
|
|
146
175
|
|
|
147
176
|
app.listen(3000, () => {
|
|
148
177
|
console.log('Server is running on http://localhost:3000')
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prisma-generator-express",
|
|
3
3
|
"description": "Prisma generator of Express CRUD API",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.8.0",
|
|
5
5
|
"main": "dist/generator.js",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"bin": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"scripts": {
|
|
14
14
|
"start": "node dist/bin.js",
|
|
15
15
|
"dev": "npx tsc -w",
|
|
16
|
-
"build": "npx tsc",
|
|
16
|
+
"build": "npx tsc --project ../../tsconfig.json",
|
|
17
17
|
"prepack": "yarn build",
|
|
18
18
|
"test": "jest",
|
|
19
19
|
"prepublishOnly": "node copy.js "
|
package/{dist → src}/bin.js
RENAMED
package/src/generator.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
12
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
13
|
+
var m = o[Symbol.asyncIterator], i;
|
|
14
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
15
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
16
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
17
|
+
};
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
const generator_helper_1 = require("@prisma/generator-helper");
|
|
20
|
+
const sdk_1 = require("@prisma/sdk");
|
|
21
|
+
const constants_1 = require("./constants");
|
|
22
|
+
const writeFileSafely_1 = require("./utils/writeFileSafely");
|
|
23
|
+
const generateFindUnique_1 = require("./helpers/generateFindUnique");
|
|
24
|
+
const generateImportPrismaStatement_1 = require("./helpers/generateImportPrismaStatement");
|
|
25
|
+
const generateFindMany_1 = require("./helpers/generateFindMany");
|
|
26
|
+
const generateFindFirst_1 = require("./helpers/generateFindFirst");
|
|
27
|
+
const generateCreate_1 = require("./helpers/generateCreate");
|
|
28
|
+
const generateRouteFile_1 = require("./helpers/generateRouteFile");
|
|
29
|
+
const generateCreateMany_1 = require("./helpers/generateCreateMany");
|
|
30
|
+
const generateUpdate_1 = require("./helpers/generateUpdate");
|
|
31
|
+
const generateUpdateMany_1 = require("./helpers/generateUpdateMany");
|
|
32
|
+
const generateUpsert_1 = require("./helpers/generateUpsert");
|
|
33
|
+
const generateDelete_1 = require("./helpers/generateDelete");
|
|
34
|
+
const generateDeleteMany_1 = require("./helpers/generateDeleteMany");
|
|
35
|
+
const generateAggregate_1 = require("./helpers/generateAggregate");
|
|
36
|
+
const generateCount_1 = require("./helpers/generateCount");
|
|
37
|
+
const generateGroupBy_1 = require("./helpers/generateGroupBy");
|
|
38
|
+
const generateRouteConfigType_1 = require("./helpers/generateRouteConfigType");
|
|
39
|
+
const { version } = require('../package.json');
|
|
40
|
+
(0, generator_helper_1.generatorHandler)({
|
|
41
|
+
onManifest() {
|
|
42
|
+
sdk_1.logger.info(`${constants_1.GENERATOR_NAME}:Registered`);
|
|
43
|
+
return {
|
|
44
|
+
version,
|
|
45
|
+
defaultOutput: '../generated',
|
|
46
|
+
prettyName: constants_1.GENERATOR_NAME,
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
onGenerate: (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
50
|
+
var _a, e_1, _b, _c;
|
|
51
|
+
const prismaImportStatement = (0, generateImportPrismaStatement_1.generateImportPrismaStatement)(options);
|
|
52
|
+
try {
|
|
53
|
+
for (var _d = true, _e = __asyncValues(options.dmmf.datamodel.models), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
|
|
54
|
+
_c = _f.value;
|
|
55
|
+
_d = false;
|
|
56
|
+
const model = _c;
|
|
57
|
+
yield (0, writeFileSafely_1.writeFileSafely)({
|
|
58
|
+
content: (0, generateFindUnique_1.generateFindUniqueFunction)({
|
|
59
|
+
model,
|
|
60
|
+
prismaImportStatement,
|
|
61
|
+
}),
|
|
62
|
+
options,
|
|
63
|
+
model,
|
|
64
|
+
operation: 'FindUnique',
|
|
65
|
+
});
|
|
66
|
+
yield (0, writeFileSafely_1.writeFileSafely)({
|
|
67
|
+
content: (0, generateFindFirst_1.generateFindFirstFunction)({
|
|
68
|
+
model,
|
|
69
|
+
prismaImportStatement,
|
|
70
|
+
}),
|
|
71
|
+
options,
|
|
72
|
+
model,
|
|
73
|
+
operation: 'FindFirst',
|
|
74
|
+
});
|
|
75
|
+
yield (0, writeFileSafely_1.writeFileSafely)({
|
|
76
|
+
content: (0, generateFindMany_1.generateFindManyFunction)({
|
|
77
|
+
model,
|
|
78
|
+
prismaImportStatement,
|
|
79
|
+
}),
|
|
80
|
+
options,
|
|
81
|
+
model,
|
|
82
|
+
operation: 'FindMany',
|
|
83
|
+
});
|
|
84
|
+
yield (0, writeFileSafely_1.writeFileSafely)({
|
|
85
|
+
content: (0, generateCreate_1.generateCreateFunction)({
|
|
86
|
+
model,
|
|
87
|
+
prismaImportStatement,
|
|
88
|
+
}),
|
|
89
|
+
options,
|
|
90
|
+
model,
|
|
91
|
+
operation: 'Create',
|
|
92
|
+
});
|
|
93
|
+
yield (0, writeFileSafely_1.writeFileSafely)({
|
|
94
|
+
content: (0, generateCreateMany_1.generateCreateManyFunction)({
|
|
95
|
+
model,
|
|
96
|
+
prismaImportStatement,
|
|
97
|
+
}),
|
|
98
|
+
options,
|
|
99
|
+
model,
|
|
100
|
+
operation: 'CreateMany',
|
|
101
|
+
});
|
|
102
|
+
yield (0, writeFileSafely_1.writeFileSafely)({
|
|
103
|
+
content: (0, generateUpdate_1.generateUpdateFunction)({
|
|
104
|
+
model,
|
|
105
|
+
prismaImportStatement,
|
|
106
|
+
}),
|
|
107
|
+
options,
|
|
108
|
+
model,
|
|
109
|
+
operation: 'Update',
|
|
110
|
+
});
|
|
111
|
+
yield (0, writeFileSafely_1.writeFileSafely)({
|
|
112
|
+
content: (0, generateUpdateMany_1.generateUpdateManyFunction)({
|
|
113
|
+
model,
|
|
114
|
+
prismaImportStatement,
|
|
115
|
+
}),
|
|
116
|
+
options,
|
|
117
|
+
model,
|
|
118
|
+
operation: 'UpdateMany',
|
|
119
|
+
});
|
|
120
|
+
yield (0, writeFileSafely_1.writeFileSafely)({
|
|
121
|
+
content: (0, generateUpsert_1.generateUpsertFunction)({
|
|
122
|
+
model,
|
|
123
|
+
prismaImportStatement,
|
|
124
|
+
}),
|
|
125
|
+
options,
|
|
126
|
+
model,
|
|
127
|
+
operation: 'Upsert',
|
|
128
|
+
});
|
|
129
|
+
yield (0, writeFileSafely_1.writeFileSafely)({
|
|
130
|
+
content: (0, generateDelete_1.generateDeleteFunction)({
|
|
131
|
+
model,
|
|
132
|
+
prismaImportStatement,
|
|
133
|
+
}),
|
|
134
|
+
options,
|
|
135
|
+
model,
|
|
136
|
+
operation: 'Delete',
|
|
137
|
+
});
|
|
138
|
+
yield (0, writeFileSafely_1.writeFileSafely)({
|
|
139
|
+
content: (0, generateDeleteMany_1.generateDeleteManyFunction)({
|
|
140
|
+
model,
|
|
141
|
+
prismaImportStatement,
|
|
142
|
+
}),
|
|
143
|
+
options,
|
|
144
|
+
model,
|
|
145
|
+
operation: 'DeleteMany',
|
|
146
|
+
});
|
|
147
|
+
yield (0, writeFileSafely_1.writeFileSafely)({
|
|
148
|
+
content: (0, generateAggregate_1.generateAggregateFunction)({
|
|
149
|
+
model,
|
|
150
|
+
prismaImportStatement,
|
|
151
|
+
}),
|
|
152
|
+
options,
|
|
153
|
+
model,
|
|
154
|
+
operation: 'Aggregate',
|
|
155
|
+
});
|
|
156
|
+
yield (0, writeFileSafely_1.writeFileSafely)({
|
|
157
|
+
content: (0, generateCount_1.generateCountFunction)({
|
|
158
|
+
model,
|
|
159
|
+
prismaImportStatement,
|
|
160
|
+
}),
|
|
161
|
+
options,
|
|
162
|
+
model,
|
|
163
|
+
operation: 'Count',
|
|
164
|
+
});
|
|
165
|
+
yield (0, writeFileSafely_1.writeFileSafely)({
|
|
166
|
+
content: (0, generateGroupBy_1.generateGroupByFunction)({
|
|
167
|
+
model,
|
|
168
|
+
prismaImportStatement,
|
|
169
|
+
}),
|
|
170
|
+
options,
|
|
171
|
+
model,
|
|
172
|
+
operation: 'GroupBy',
|
|
173
|
+
});
|
|
174
|
+
yield (0, writeFileSafely_1.writeFileSafely)({
|
|
175
|
+
content: (0, generateRouteFile_1.generateRouterFunction)({ model }),
|
|
176
|
+
options,
|
|
177
|
+
model,
|
|
178
|
+
operation: 'index',
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
183
|
+
finally {
|
|
184
|
+
try {
|
|
185
|
+
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
186
|
+
}
|
|
187
|
+
finally { if (e_1) throw e_1.error; }
|
|
188
|
+
}
|
|
189
|
+
yield (0, writeFileSafely_1.writeFileSafely)({
|
|
190
|
+
content: (0, generateRouteConfigType_1.generateRouteConfigType)(),
|
|
191
|
+
options,
|
|
192
|
+
operation: 'RouteConfig',
|
|
193
|
+
});
|
|
194
|
+
}),
|
|
195
|
+
});
|
package/src/generator.ts
CHANGED
|
@@ -17,6 +17,7 @@ import { generateDeleteManyFunction } from './helpers/generateDeleteMany'
|
|
|
17
17
|
import { generateAggregateFunction } from './helpers/generateAggregate'
|
|
18
18
|
import { generateCountFunction } from './helpers/generateCount'
|
|
19
19
|
import { generateGroupByFunction } from './helpers/generateGroupBy'
|
|
20
|
+
import { generateRouteConfigType } from './helpers/generateRouteConfigType'
|
|
20
21
|
|
|
21
22
|
const { version } = require('../package.json')
|
|
22
23
|
|
|
@@ -170,5 +171,11 @@ generatorHandler({
|
|
|
170
171
|
operation: 'index',
|
|
171
172
|
})
|
|
172
173
|
}
|
|
174
|
+
|
|
175
|
+
await writeFileSafely({
|
|
176
|
+
content: generateRouteConfigType(),
|
|
177
|
+
options,
|
|
178
|
+
operation: 'RouteConfig',
|
|
179
|
+
})
|
|
173
180
|
},
|
|
174
181
|
})
|
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateAggregateFunction = void 0;
|
|
4
4
|
const strings_1 = require("../utils/strings");
|
|
5
|
+
/**
|
|
6
|
+
* Generates an Express middleware function that handles aggregation queries
|
|
7
|
+
* and includes conditional output validation with Zod.
|
|
8
|
+
* This version dynamically includes the correct type for the arguments based on the Prisma model.
|
|
9
|
+
* @param options - The options containing the model name and the import statement for Prisma types.
|
|
10
|
+
* @returns {string} - The generated middleware function as a string.
|
|
11
|
+
*/
|
|
5
12
|
const generateAggregateFunction = (options) => {
|
|
6
13
|
const { model, prismaImportStatement } = options;
|
|
7
14
|
const modelName = model.name;
|
|
@@ -41,12 +48,15 @@ export async function ${functionName}(req: AggregateRequest, res: Response, next
|
|
|
41
48
|
} else {
|
|
42
49
|
res.status(200).json(result);
|
|
43
50
|
}
|
|
44
|
-
} catch (error) {
|
|
45
|
-
console.error(
|
|
46
|
-
|
|
51
|
+
} catch (error: unknown) {
|
|
52
|
+
console.error("Error in handling aggregation request:", error);
|
|
53
|
+
if (error instanceof Error) {
|
|
54
|
+
res.status(500).json({ error: error.message });
|
|
55
|
+
} else {
|
|
56
|
+
res.status(500).json({ error: "Unknown error occurred" });
|
|
57
|
+
}
|
|
47
58
|
next(error);
|
|
48
59
|
}
|
|
49
60
|
}`;
|
|
50
61
|
};
|
|
51
62
|
exports.generateAggregateFunction = generateAggregateFunction;
|
|
52
|
-
//# sourceMappingURL=generateAggregate.js.map
|
|
@@ -51,9 +51,13 @@ export async function ${functionName}(req: AggregateRequest, res: Response, next
|
|
|
51
51
|
} else {
|
|
52
52
|
res.status(200).json(result);
|
|
53
53
|
}
|
|
54
|
-
} catch (error) {
|
|
55
|
-
console.error(
|
|
56
|
-
|
|
54
|
+
} catch (error: unknown) {
|
|
55
|
+
console.error("Error in handling aggregation request:", error);
|
|
56
|
+
if (error instanceof Error) {
|
|
57
|
+
res.status(500).json({ error: error.message });
|
|
58
|
+
} else {
|
|
59
|
+
res.status(500).json({ error: "Unknown error occurred" });
|
|
60
|
+
}
|
|
57
61
|
next(error);
|
|
58
62
|
}
|
|
59
63
|
}`
|
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateCountFunction = void 0;
|
|
4
4
|
const strings_1 = require("../utils/strings");
|
|
5
|
+
/**
|
|
6
|
+
* Generates an Express middleware function that handles count queries
|
|
7
|
+
* and includes conditional output validation with Zod.
|
|
8
|
+
* This version dynamically includes the correct type for the arguments based on the Prisma model.
|
|
9
|
+
* @param options - The options containing the model name and the import statement for Prisma types.
|
|
10
|
+
* @returns {string} - The generated middleware function as a string.
|
|
11
|
+
*/
|
|
5
12
|
const generateCountFunction = (options) => {
|
|
6
13
|
const { model, prismaImportStatement } = options;
|
|
7
14
|
const modelName = model.name;
|
|
@@ -41,12 +48,15 @@ export async function ${functionName}(req: CountRequest, res: Response, next: Ne
|
|
|
41
48
|
} else {
|
|
42
49
|
res.status(200).json(result);
|
|
43
50
|
}
|
|
44
|
-
} catch (error) {
|
|
45
|
-
console.error(
|
|
46
|
-
|
|
51
|
+
} catch (error: unknown) {
|
|
52
|
+
console.error("Error in handling count request:", error);
|
|
53
|
+
if (error instanceof Error) {
|
|
54
|
+
res.status(500).json({ error: error.message });
|
|
55
|
+
} else {
|
|
56
|
+
res.status(500).json({ error: "Unknown error occurred" });
|
|
57
|
+
}
|
|
47
58
|
next(error);
|
|
48
59
|
}
|
|
49
60
|
}`;
|
|
50
61
|
};
|
|
51
62
|
exports.generateCountFunction = generateCountFunction;
|
|
52
|
-
//# sourceMappingURL=generateCount.js.map
|
|
@@ -51,9 +51,13 @@ export async function ${functionName}(req: CountRequest, res: Response, next: Ne
|
|
|
51
51
|
} else {
|
|
52
52
|
res.status(200).json(result);
|
|
53
53
|
}
|
|
54
|
-
} catch (error) {
|
|
55
|
-
console.error(
|
|
56
|
-
|
|
54
|
+
} catch (error: unknown) {
|
|
55
|
+
console.error("Error in handling count request:", error);
|
|
56
|
+
if (error instanceof Error) {
|
|
57
|
+
res.status(500).json({ error: error.message });
|
|
58
|
+
} else {
|
|
59
|
+
res.status(500).json({ error: "Unknown error occurred" });
|
|
60
|
+
}
|
|
57
61
|
next(error);
|
|
58
62
|
}
|
|
59
63
|
}`
|
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateCreateFunction = void 0;
|
|
4
4
|
const strings_1 = require("../utils/strings");
|
|
5
|
+
/**
|
|
6
|
+
* Generates an Express middleware function that handles creation of records and includes conditional output validation with Zod.
|
|
7
|
+
* This version dynamically includes the correct type for the arguments based on the Prisma model.
|
|
8
|
+
* @param options - The options containing the model name and the import statement for Prisma types.
|
|
9
|
+
* @returns {string} - The generated middleware function as a string.
|
|
10
|
+
*/
|
|
5
11
|
const generateCreateFunction = (options) => {
|
|
6
12
|
const { model, prismaImportStatement } = options;
|
|
7
13
|
const modelName = model.name;
|
|
@@ -41,12 +47,15 @@ export async function ${functionName}(req: CreateRequest, res: Response, next: N
|
|
|
41
47
|
} else {
|
|
42
48
|
res.status(201).json(data);
|
|
43
49
|
}
|
|
44
|
-
} catch (error) {
|
|
45
|
-
console.error(
|
|
46
|
-
|
|
50
|
+
} catch (error: unknown) {
|
|
51
|
+
console.error("Error in handling create request:", error);
|
|
52
|
+
if (error instanceof Error) {
|
|
53
|
+
res.status(500).json({ error: error.message });
|
|
54
|
+
} else {
|
|
55
|
+
res.status(500).json({ error: "Unknown error occurred" });
|
|
56
|
+
}
|
|
47
57
|
next(error);
|
|
48
58
|
}
|
|
49
59
|
}`;
|
|
50
60
|
};
|
|
51
61
|
exports.generateCreateFunction = generateCreateFunction;
|
|
52
|
-
//# sourceMappingURL=generateCreate.js.map
|
|
@@ -49,9 +49,13 @@ export async function ${functionName}(req: CreateRequest, res: Response, next: N
|
|
|
49
49
|
} else {
|
|
50
50
|
res.status(201).json(data);
|
|
51
51
|
}
|
|
52
|
-
} catch (error) {
|
|
53
|
-
console.error(
|
|
54
|
-
|
|
52
|
+
} catch (error: unknown) {
|
|
53
|
+
console.error("Error in handling create request:", error);
|
|
54
|
+
if (error instanceof Error) {
|
|
55
|
+
res.status(500).json({ error: error.message });
|
|
56
|
+
} else {
|
|
57
|
+
res.status(500).json({ error: "Unknown error occurred" });
|
|
58
|
+
}
|
|
55
59
|
next(error);
|
|
56
60
|
}
|
|
57
61
|
}`
|
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateCreateManyFunction = void 0;
|
|
4
4
|
const strings_1 = require("../utils/strings");
|
|
5
|
+
/**
|
|
6
|
+
* Generates an Express middleware function that handles the creation of multiple records and includes conditional output validation with Zod.
|
|
7
|
+
* This version dynamically includes the correct type for the arguments based on the Prisma model.
|
|
8
|
+
* @param options - The options containing the model name and the import statement for Prisma types.
|
|
9
|
+
* @returns {string} - The generated middleware function as a string.
|
|
10
|
+
*/
|
|
5
11
|
const generateCreateManyFunction = (options) => {
|
|
6
12
|
const { model, prismaImportStatement } = options;
|
|
7
13
|
const modelName = model.name;
|
|
@@ -41,12 +47,15 @@ export async function ${functionName}(req: CreateManyRequest, res: Response, nex
|
|
|
41
47
|
} else {
|
|
42
48
|
res.status(201).json(data);
|
|
43
49
|
}
|
|
44
|
-
} catch (error) {
|
|
50
|
+
} catch (error: unknown) {
|
|
45
51
|
console.error('Error in handling createMany request:', error);
|
|
46
|
-
|
|
52
|
+
if (error instanceof Error) {
|
|
53
|
+
res.status(500).json({ error: error.message });
|
|
54
|
+
} else {
|
|
55
|
+
res.status(500).json({ error: "Unknown error occurred" });
|
|
56
|
+
}
|
|
47
57
|
next(error);
|
|
48
58
|
}
|
|
49
59
|
}`;
|
|
50
60
|
};
|
|
51
61
|
exports.generateCreateManyFunction = generateCreateManyFunction;
|
|
52
|
-
//# sourceMappingURL=generateCreateMany.js.map
|
|
@@ -49,9 +49,13 @@ export async function ${functionName}(req: CreateManyRequest, res: Response, nex
|
|
|
49
49
|
} else {
|
|
50
50
|
res.status(201).json(data);
|
|
51
51
|
}
|
|
52
|
-
} catch (error) {
|
|
52
|
+
} catch (error: unknown) {
|
|
53
53
|
console.error('Error in handling createMany request:', error);
|
|
54
|
-
|
|
54
|
+
if (error instanceof Error) {
|
|
55
|
+
res.status(500).json({ error: error.message });
|
|
56
|
+
} else {
|
|
57
|
+
res.status(500).json({ error: "Unknown error occurred" });
|
|
58
|
+
}
|
|
55
59
|
next(error);
|
|
56
60
|
}
|
|
57
61
|
}`
|
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateDeleteFunction = void 0;
|
|
4
4
|
const strings_1 = require("../utils/strings");
|
|
5
|
+
/**
|
|
6
|
+
* Generates an Express middleware function that handles deleting records
|
|
7
|
+
* and includes conditional output validation with Zod.
|
|
8
|
+
* This version dynamically includes the correct type for the arguments based on the Prisma model.
|
|
9
|
+
* @param options - The options containing the model name and the import statement for Prisma types.
|
|
10
|
+
* @returns {string} - The generated middleware function as a string.
|
|
11
|
+
*/
|
|
5
12
|
const generateDeleteFunction = (options) => {
|
|
6
13
|
const { model, prismaImportStatement } = options;
|
|
7
14
|
const modelName = model.name;
|
|
@@ -42,12 +49,15 @@ export async function ${functionName}(req: DeleteRequest, res: Response, next: N
|
|
|
42
49
|
} else {
|
|
43
50
|
res.status(200).json(data);
|
|
44
51
|
}
|
|
45
|
-
} catch (error) {
|
|
52
|
+
} catch (error: unknown) {
|
|
46
53
|
console.error('Error in handling delete request:', error);
|
|
47
|
-
|
|
54
|
+
if (error instanceof Error) {
|
|
55
|
+
res.status(500).json({ error: error.message });
|
|
56
|
+
} else {
|
|
57
|
+
res.status(500).json({ error: "Unknown error occurred" });
|
|
58
|
+
}
|
|
48
59
|
next(error);
|
|
49
60
|
}
|
|
50
61
|
}`;
|
|
51
62
|
};
|
|
52
63
|
exports.generateDeleteFunction = generateDeleteFunction;
|
|
53
|
-
//# sourceMappingURL=generateDelete.js.map
|
|
@@ -51,9 +51,13 @@ export async function ${functionName}(req: DeleteRequest, res: Response, next: N
|
|
|
51
51
|
} else {
|
|
52
52
|
res.status(200).json(data);
|
|
53
53
|
}
|
|
54
|
-
} catch (error) {
|
|
54
|
+
} catch (error: unknown) {
|
|
55
55
|
console.error('Error in handling delete request:', error);
|
|
56
|
-
|
|
56
|
+
if (error instanceof Error) {
|
|
57
|
+
res.status(500).json({ error: error.message });
|
|
58
|
+
} else {
|
|
59
|
+
res.status(500).json({ error: "Unknown error occurred" });
|
|
60
|
+
}
|
|
57
61
|
next(error);
|
|
58
62
|
}
|
|
59
63
|
}`
|
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateDeleteManyFunction = void 0;
|
|
4
4
|
const strings_1 = require("../utils/strings");
|
|
5
|
+
/**
|
|
6
|
+
* Generates an Express middleware function that handles batch deleting records
|
|
7
|
+
* and includes conditional output validation with Zod.
|
|
8
|
+
* This version dynamically includes the correct type for the arguments based on the Prisma model.
|
|
9
|
+
* @param options - The options containing the model name and the import statement for Prisma types.
|
|
10
|
+
* @returns {string} - The generated middleware function as a string.
|
|
11
|
+
*/
|
|
5
12
|
const generateDeleteManyFunction = (options) => {
|
|
6
13
|
const { model, prismaImportStatement } = options;
|
|
7
14
|
const modelName = model.name;
|
|
@@ -40,12 +47,15 @@ export async function ${functionName}(req: DeleteManyRequest, res: Response, nex
|
|
|
40
47
|
} else {
|
|
41
48
|
res.status(200).json(result);
|
|
42
49
|
}
|
|
43
|
-
} catch (error) {
|
|
50
|
+
} catch (error: unknown) {
|
|
44
51
|
console.error('Error in handling batch delete request:', error);
|
|
45
|
-
|
|
52
|
+
if (error instanceof Error) {
|
|
53
|
+
res.status(500).json({ error: error.message });
|
|
54
|
+
} else {
|
|
55
|
+
res.status(500).json({ error: "Unknown error occurred" });
|
|
56
|
+
}
|
|
46
57
|
next(error);
|
|
47
58
|
}
|
|
48
59
|
}`;
|
|
49
60
|
};
|
|
50
61
|
exports.generateDeleteManyFunction = generateDeleteManyFunction;
|
|
51
|
-
//# sourceMappingURL=generateDeleteMany.js.map
|
|
@@ -49,9 +49,13 @@ export async function ${functionName}(req: DeleteManyRequest, res: Response, nex
|
|
|
49
49
|
} else {
|
|
50
50
|
res.status(200).json(result);
|
|
51
51
|
}
|
|
52
|
-
} catch (error) {
|
|
52
|
+
} catch (error: unknown) {
|
|
53
53
|
console.error('Error in handling batch delete request:', error);
|
|
54
|
-
|
|
54
|
+
if (error instanceof Error) {
|
|
55
|
+
res.status(500).json({ error: error.message });
|
|
56
|
+
} else {
|
|
57
|
+
res.status(500).json({ error: "Unknown error occurred" });
|
|
58
|
+
}
|
|
55
59
|
next(error);
|
|
56
60
|
}
|
|
57
61
|
}`
|