dobo 1.1.8 → 1.1.10

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.
@@ -36,7 +36,7 @@ async function sanitizeFullText (item) {
36
36
 
37
37
  async function sanitizeSchema (items) {
38
38
  const { freeze, fatal, importModule, defaultsDeep, join, breakNsPath, runHook } = this.app.bajo
39
- const { orderBy, map, keys, findIndex, find, each, isString, get, isPlainObject, camelCase, uniq, filter } = this.lib._
39
+ const { isEmpty, orderBy, map, keys, findIndex, find, each, isString, get, isPlainObject, camelCase, uniq, filter } = this.lib._
40
40
  const propTypes = keys(this.propType)
41
41
  const schemas = []
42
42
  this.log.debug('loadingDbSchemas')
@@ -64,7 +64,7 @@ async function sanitizeSchema (items) {
64
64
  maxLength = maxLength ?? 255
65
65
  prop = { name, type }
66
66
  if (type === 'string') prop.maxLength = parseInt(maxLength) || undefined
67
- if (index) prop.index = { type: index === 'true' ? 'default' : index }
67
+ if (!isEmpty(index)) prop.index = { type: index === 'true' ? 'default' : index }
68
68
  prop.required = required === 'true'
69
69
  item.properties[idx] = prop
70
70
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dobo",
3
- "version": "1.1.8",
3
+ "version": "1.1.10",
4
4
  "description": "Database ORM/ODM for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/plugin/factory.js CHANGED
@@ -271,7 +271,37 @@ async function factory (pkgName) {
271
271
  if (trim(filter.query).startsWith('{')) query = JSON.parse(filter.query)
272
272
  else query = nql(filter.query).parse()
273
273
  } else if (isPlainObject(filter.query)) query = filter.query
274
- return query
274
+ return this.sanitizeQuery(query, schema)
275
+ }
276
+
277
+ sanitizeQuery = (query, schema, parent) => {
278
+ const { cloneDeep, isPlainObject, isArray, find } = this.lib._
279
+ const { isSet } = this.app.bajo
280
+ const { dayjs } = this.lib
281
+ const obj = cloneDeep(query)
282
+ const keys = Object.keys(obj)
283
+ const sanitize = (key, val, p) => {
284
+ if (!isSet(val)) return val
285
+ const prop = find(schema.properties, { name: key.startsWith('$') ? p : key })
286
+ if (!prop) return val
287
+ if (['datetime', 'date', 'time'].includes(prop.type)) {
288
+ const dt = dayjs(val)
289
+ return dt.isValid() ? dt.toDate() : val
290
+ } else if (['smallint', 'integer'].includes(prop.type)) return parseInt(val) || val
291
+ else if (['float', 'double'].includes(prop.type)) return parseFloat(val) || val
292
+ else if (['boolean'].includes(prop.type)) return !!val
293
+ return val
294
+ }
295
+ keys.forEach(k => {
296
+ const v = obj[k]
297
+ if (isPlainObject(v)) obj[k] = this.sanitizeQuery(v, schema, k)
298
+ else if (isArray(v)) {
299
+ v.forEach((i, idx) => {
300
+ if (isPlainObject(i)) obj[k][idx] = this.sanitizeQuery(i, schema, k)
301
+ })
302
+ } else obj[k] = sanitize(k, v, parent)
303
+ })
304
+ return obj
275
305
  }
276
306
 
277
307
  validationErrorMessage = (err) => {