prisma-generator-express 1.49.0 → 1.49.1
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/dist/utils/copyFiles.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"copyFiles.js","sourceRoot":"","sources":["../../src/utils/copyFiles.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"copyFiles.js","sourceRoot":"","sources":["../../src/utils/copyFiles.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwGA,8BAmDC;AA1JD,uCAAwB;AACxB,2CAA4B;AAG5B,2CAAuC;AAEvC,MAAM,YAAY,GAAG;IACnB,qBAAqB;IACrB,sBAAsB;IACtB,yBAAyB;IACzB,SAAS;IACT,gBAAgB;IAChB,iBAAiB;IACjB,qBAAqB;CACtB,CAAA;AAED,MAAM,kBAAkB,GAAG;IACzB,uBAAuB;IACvB,uBAAuB;IACvB,uBAAuB;CACxB,CAAA;AAQD,SAAS,kBAAkB,CAAC,OAAe;IACzC,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,CAAA;IAChE,IAAI,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC;QAAE,OAAO,OAAO,CAAA;IAC1C,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;IACpD,IAAI,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC;QAAE,OAAO,QAAQ,CAAA;IAC5C,MAAM,IAAI,KAAK,CACb,sBAAsB,GAAG,OAAO,GAAG,iCAAiC;QACpE,OAAO,GAAG,QAAQ,GAAG,QAAQ,GAAG,iBAAiB,GAAG,SAAS,CAC9D,CAAA;AACH,CAAC;AAED,SAAS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AC,CAAA;IACjD,MAAM,SAAS,GAAG,YAAY,CAAC,YAAY,EAAE,SAAS,EAAE,sBAAsB,EAAE,WAAW,EAAE;QAC3F,QAAQ,EAAE,IAAI;QACd,cAAc,EAAE,CAAC,EAAE,IAAI,EAAE,cAAc,EAAE,EAAE,EAAE,SAAS,EAAE,CAAC;KAC1D,CAAC,CAAA;IACF,IAAI,SAAS;QAAE,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;IAErC,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CACb,iBAAiB,GAAG,MAAM,CAAC,MAAM,GAAG,sBAAsB;YAC1D,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;YACxC,gBAAgB,GAAG,QAAQ,GAAG,iBAAiB,GAAG,SAAS,CAC5D,CAAA;IACH,CAAC;IAED,OAAO,CAAC,GAAG,CAAC,uCAAuC,CAAC,CAAA;AACtD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prisma-generator-express",
|
|
3
3
|
"description": "Prisma generator for Express, Fastify, and Hono CRUD APIs with OpenAPI documentation",
|
|
4
|
-
"version": "1.49.
|
|
4
|
+
"version": "1.49.1",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"license": "MIT",
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import express from 'express'
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
NextFunction,
|
|
4
|
+
Request,
|
|
5
|
+
RequestHandler,
|
|
6
|
+
Response,
|
|
7
|
+
Router,
|
|
8
|
+
} from 'express'
|
|
3
9
|
import { HttpError, mapError, transformResult } from './operationRuntime'
|
|
4
10
|
|
|
5
11
|
type SortDirection = 'asc' | 'desc'
|
|
@@ -7,11 +13,7 @@ type NullsOrder = 'first' | 'last'
|
|
|
7
13
|
|
|
8
14
|
type OrderByDef =
|
|
9
15
|
| string
|
|
10
|
-
| {
|
|
11
|
-
field: string
|
|
12
|
-
direction?: SortDirection
|
|
13
|
-
nulls?: NullsOrder
|
|
14
|
-
}
|
|
16
|
+
| { field: string; direction?: SortDirection; nulls?: NullsOrder }
|
|
15
17
|
|
|
16
18
|
type ViewDef = {
|
|
17
19
|
relation: string
|
|
@@ -19,11 +21,18 @@ type ViewDef = {
|
|
|
19
21
|
defaultLimit?: number
|
|
20
22
|
maxLimit?: number
|
|
21
23
|
orderBy?: OrderByDef
|
|
22
|
-
authorize?: (
|
|
24
|
+
authorize?: (
|
|
25
|
+
req: Request,
|
|
26
|
+
viewName: string,
|
|
27
|
+
def: ViewDef,
|
|
28
|
+
) => void | Promise<void>
|
|
23
29
|
}
|
|
24
30
|
|
|
25
31
|
type PrismaRawClient = {
|
|
26
|
-
$queryRawUnsafe: <T = unknown>(
|
|
32
|
+
$queryRawUnsafe: <T = unknown>(
|
|
33
|
+
sql: string,
|
|
34
|
+
...values: unknown[]
|
|
35
|
+
) => Promise<T>
|
|
27
36
|
}
|
|
28
37
|
|
|
29
38
|
type MaterializedRouterOptions = {
|
|
@@ -39,7 +48,8 @@ type MaterializedRouterOptions = {
|
|
|
39
48
|
const IDENT_RE = /^[A-Za-z_][A-Za-z0-9_]*$/
|
|
40
49
|
|
|
41
50
|
const quoteIdent = (name: string): string => {
|
|
42
|
-
if (!IDENT_RE.test(name))
|
|
51
|
+
if (!IDENT_RE.test(name))
|
|
52
|
+
throw new HttpError(400, 'invalid identifier: ' + name)
|
|
43
53
|
return '"' + name.replace(/"/g, '""') + '"'
|
|
44
54
|
}
|
|
45
55
|
|
|
@@ -54,7 +64,12 @@ const buildFqn = (def: ViewDef): string =>
|
|
|
54
64
|
? quoteIdent(def.schema) + '.' + quoteIdent(def.relation)
|
|
55
65
|
: quoteIdent(def.relation)
|
|
56
66
|
|
|
57
|
-
const clampInt = (
|
|
67
|
+
const clampInt = (
|
|
68
|
+
v: unknown,
|
|
69
|
+
fallback: number,
|
|
70
|
+
min: number,
|
|
71
|
+
max: number,
|
|
72
|
+
): number => {
|
|
58
73
|
const n = Number(v ?? fallback)
|
|
59
74
|
if (!Number.isFinite(n)) return fallback
|
|
60
75
|
return Math.min(Math.max(Math.trunc(n), min), max)
|
|
@@ -63,23 +78,21 @@ const clampInt = (v: unknown, fallback: number, min: number, max: number): numbe
|
|
|
63
78
|
const normalizeDirection = (value: unknown): 'ASC' | 'DESC' => {
|
|
64
79
|
if (value === undefined || value === 'asc' || value === 'ASC') return 'ASC'
|
|
65
80
|
if (value === 'desc' || value === 'DESC') return 'DESC'
|
|
66
|
-
throw new
|
|
81
|
+
throw new HttpError(400, 'invalid sort direction')
|
|
67
82
|
}
|
|
68
83
|
|
|
69
|
-
const normalizeNulls = (
|
|
84
|
+
const normalizeNulls = (
|
|
85
|
+
value: unknown,
|
|
86
|
+
): '' | ' NULLS FIRST' | ' NULLS LAST' => {
|
|
70
87
|
if (value === undefined) return ''
|
|
71
88
|
if (value === 'first' || value === 'FIRST') return ' NULLS FIRST'
|
|
72
89
|
if (value === 'last' || value === 'LAST') return ' NULLS LAST'
|
|
73
|
-
throw new
|
|
90
|
+
throw new HttpError(400, 'invalid nulls order')
|
|
74
91
|
}
|
|
75
92
|
|
|
76
93
|
const buildOrderBy = (orderBy?: OrderByDef): string => {
|
|
77
94
|
if (!orderBy) return ''
|
|
78
|
-
|
|
79
|
-
if (typeof orderBy === 'string') {
|
|
80
|
-
return ' ORDER BY ' + quoteIdent(orderBy)
|
|
81
|
-
}
|
|
82
|
-
|
|
95
|
+
if (typeof orderBy === 'string') return ' ORDER BY ' + quoteIdent(orderBy)
|
|
83
96
|
return (
|
|
84
97
|
' ORDER BY ' +
|
|
85
98
|
quoteIdent(orderBy.field) +
|
|
@@ -89,7 +102,9 @@ const buildOrderBy = (orderBy?: OrderByDef): string => {
|
|
|
89
102
|
)
|
|
90
103
|
}
|
|
91
104
|
|
|
92
|
-
export const materializedViewsRouter = (
|
|
105
|
+
export const materializedViewsRouter = (
|
|
106
|
+
opts: MaterializedRouterOptions,
|
|
107
|
+
): Router => {
|
|
93
108
|
const router = express.Router()
|
|
94
109
|
const basePath = normalizeBasePath(opts.basePath)
|
|
95
110
|
const defaultLimit = opts.defaultLimit ?? 50
|
|
@@ -104,14 +119,8 @@ export const materializedViewsRouter = (opts: MaterializedRouterOptions): Router
|
|
|
104
119
|
try {
|
|
105
120
|
const viewName = req.params.viewName
|
|
106
121
|
const def = opts.views[viewName]
|
|
107
|
-
|
|
108
|
-
if (
|
|
109
|
-
throw new HttpError(404, 'unknown view')
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (def.authorize) {
|
|
113
|
-
await def.authorize(req, viewName, def)
|
|
114
|
-
}
|
|
122
|
+
if (!def) throw new HttpError(404, 'unknown view')
|
|
123
|
+
if (def.authorize) await def.authorize(req, viewName, def)
|
|
115
124
|
|
|
116
125
|
const take = clampInt(
|
|
117
126
|
req.query.take,
|
|
@@ -119,11 +128,13 @@ export const materializedViewsRouter = (opts: MaterializedRouterOptions): Router
|
|
|
119
128
|
1,
|
|
120
129
|
def.maxLimit ?? maxLimit,
|
|
121
130
|
)
|
|
122
|
-
|
|
123
131
|
const skip = clampInt(req.query.skip, 0, 0, Number.MAX_SAFE_INTEGER)
|
|
124
132
|
|
|
125
133
|
if (skip > 0 && !def.orderBy) {
|
|
126
|
-
throw new HttpError(
|
|
134
|
+
throw new HttpError(
|
|
135
|
+
400,
|
|
136
|
+
'skip requires orderBy for deterministic pagination',
|
|
137
|
+
)
|
|
127
138
|
}
|
|
128
139
|
|
|
129
140
|
const sql =
|
|
@@ -132,8 +143,11 @@ export const materializedViewsRouter = (opts: MaterializedRouterOptions): Router
|
|
|
132
143
|
buildOrderBy(def.orderBy) +
|
|
133
144
|
' LIMIT $1 OFFSET $2'
|
|
134
145
|
|
|
135
|
-
const rows = await opts.prisma.$queryRawUnsafe<unknown[]>(
|
|
136
|
-
|
|
146
|
+
const rows = await opts.prisma.$queryRawUnsafe<unknown[]>(
|
|
147
|
+
sql,
|
|
148
|
+
take,
|
|
149
|
+
skip,
|
|
150
|
+
)
|
|
137
151
|
res.locals.data = transformResult(rows)
|
|
138
152
|
next()
|
|
139
153
|
} catch (err) {
|
|
@@ -146,23 +160,28 @@ export const materializedViewsRouter = (opts: MaterializedRouterOptions): Router
|
|
|
146
160
|
},
|
|
147
161
|
)
|
|
148
162
|
|
|
149
|
-
router.use(
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
(err as {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
+
router.use(
|
|
164
|
+
(err: unknown, _req: Request, res: Response, next: NextFunction) => {
|
|
165
|
+
const httpError =
|
|
166
|
+
err instanceof HttpError
|
|
167
|
+
? err
|
|
168
|
+
: err &&
|
|
169
|
+
typeof err === 'object' &&
|
|
170
|
+
typeof (err as { status?: number }).status === 'number'
|
|
171
|
+
? new HttpError(
|
|
172
|
+
(err as { status: number }).status,
|
|
173
|
+
(err as { message?: string }).message ||
|
|
174
|
+
'Internal server error',
|
|
175
|
+
)
|
|
176
|
+
: mapError(err)
|
|
177
|
+
|
|
178
|
+
if (!res.headersSent) {
|
|
179
|
+
return res.status(httpError.status).json({ message: httpError.message })
|
|
180
|
+
}
|
|
163
181
|
|
|
164
|
-
|
|
165
|
-
|
|
182
|
+
next(err)
|
|
183
|
+
},
|
|
184
|
+
)
|
|
166
185
|
|
|
167
186
|
return router
|
|
168
|
-
}
|
|
187
|
+
}
|