@jcbuisson/express-x 1.0.21 → 1.1.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.mjs +25 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.0.21",
3
+ "version": "1.1.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/index.mjs CHANGED
@@ -71,6 +71,7 @@ function expressX(app, options={}) {
71
71
  return prisma[entity].upsert(options)
72
72
  },
73
73
  })
74
+ service.prisma = prisma
74
75
  service.entity = entity
75
76
 
76
77
  if (options.debug) console.log(`created service '${name}' over table '${entity}'`)
@@ -169,8 +170,31 @@ function expressX(app, options={}) {
169
170
 
170
171
  app.get(path, async (req, res) => {
171
172
  context.http.req = req
173
+ const query = { ...req.query }
174
+ for (const fieldName in query) {
175
+ const fieldInfo = await service.prisma.$queryRawUnsafe(`
176
+ SELECT column_name, data_type
177
+ FROM information_schema.columns
178
+ WHERE table_name = '${service.entity}' AND column_name = '${fieldName}';
179
+ `)
180
+ const fieldType = fieldInfo[0].data_type
181
+ if (fieldType === 'integer') {
182
+ query[fieldName] = parseInt(query[fieldName])
183
+ } else if (fieldType === 'numeric') {
184
+ query[fieldName] = parseFloat(query[fieldName])
185
+ } else if (fieldType === 'boolean') {
186
+ query[fieldName] = (query[fieldName] === 't') ? true : false
187
+ } else if (fieldType === 'text' || fieldType === 'character varying') {
188
+ query[fieldName] = query[fieldName]
189
+ } else {
190
+ // ?
191
+ query[fieldName] = query[fieldName]
192
+ }
193
+ }
172
194
  try {
173
- const values = await service.__find(context, req.body)
195
+ const values = await service.__find(context, {
196
+ where: query,
197
+ })
174
198
  res.json(values)
175
199
  } catch(err) {
176
200
  console.log('callErr', err)