dobo 1.0.7 → 1.0.9

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.
@@ -1,3 +1,4 @@
1
+ import crypto from 'crypto'
1
2
  import resolveMethod from '../../../lib/resolve-method.js'
2
3
  import checkUnique from '../../../lib/check-unique.js'
3
4
  import handleAttachmentUpload from '../../../lib/handle-attachment-upload.js'
@@ -7,7 +8,7 @@ import execFeatureHook from '../../../lib/exec-feature-hook.js'
7
8
  async function create (name, input, opts = {}) {
8
9
  const { generateId, runHook, isSet } = this.app.bajo
9
10
  const { clearModel } = this.cache ?? {}
10
- const { find, forOwn, cloneDeep, camelCase, omit, get } = this.app.bajo.lib._
11
+ const { find, forOwn, cloneDeep, camelCase, omit, get, pick } = this.app.bajo.lib._
11
12
  const options = cloneDeep(omit(opts, ['req']))
12
13
  options.req = opts.req
13
14
  options.dataOnly = options.dataOnly ?? true
@@ -19,15 +20,21 @@ async function create (name, input, opts = {}) {
19
20
  const { handler, schema, driver } = await resolveMethod.call(this, name, 'record-create', options)
20
21
  const idField = find(schema.properties, { name: 'id' })
21
22
  const extFields = get(options, 'validation.extFields', [])
22
- if (!isSet(input.id)) {
23
- if (idField.type === 'string') input.id = generateId()
24
- else if (['integer', 'smallint'].includes(idField.type) && !idField.autoInc) input.id = generateId('int')
25
- }
26
- let body = noSanitize ? input : await this.sanitizeBody({ body: input, schema, extFields, strict: true })
23
+ let body = noSanitize ? cloneDeep(input) : await this.sanitizeBody({ body: input, schema, extFields, strict: true })
27
24
  if (!noHook) {
28
25
  await runHook(`${this.name}:beforeRecordCreate`, name, body, options)
29
26
  await runHook(`${this.name}.${camelCase(name)}:beforeRecordCreate`, body, options)
30
27
  }
28
+ if (!isSet(body.id)) {
29
+ if (idField.type === 'string') {
30
+ if (!options.checksumId) body.id = generateId()
31
+ else {
32
+ if (options.checksumId === true) options.checksumId = Object.keys(body)
33
+ const checksum = pick(body, options.checksumId)
34
+ body.id = crypto.createHash('md5').update(JSON.stringify(checksum)).digest('hex')
35
+ }
36
+ } else if (['integer', 'smallint'].includes(idField.type) && !idField.autoInc) input.id = generateId('int')
37
+ }
31
38
  if (!noFeatureHook) await execFeatureHook.call(this, 'beforeCreate', { schema, body })
32
39
  if (!noValidation) body = await execValidation.call(this, { name, body, options })
33
40
  if (isSet(body.id) && !noCheckUnique) await checkUnique.call(this, { schema, body })
@@ -18,9 +18,8 @@ async function addFixture (name, { spinner } = {}) {
18
18
  const overrides = await readConfig(`${getPluginDataDir(this.name)}/override/fixture/${schema.name}.*`, { ns: this.name, ignoreError: true })
19
19
  if (isArray(overrides) && !isEmpty(overrides)) items = overrides
20
20
  // extend
21
- const me = this
22
21
  await eachPlugins(async function ({ dir, ns }) {
23
- const extend = await readConfig(`${dir}/${me.name}/extend/fixture/${schema.name}.*`, { ns, ignoreError: true })
22
+ const extend = await readConfig(`${dir}/dobo/extend/${schema.ns}/fixture/${base}.*`, { ns, ignoreError: true })
24
23
  if (isArray(extend) && !isEmpty(extend)) items.push(...extend)
25
24
  })
26
25
  if (isEmpty(items)) return result
@@ -33,7 +33,7 @@ async function handler ({ file, alias, ns }) {
33
33
  if ((mod.properties ?? []).length === 0) this.fatal('Schema \'%s\' doesn\'t have properties at all', mod.name)
34
34
  // schema extender
35
35
  await eachPlugins(async function (opts) {
36
- const glob = `${opts.dir}/dobo/extend/schema/${mod.name}.*`
36
+ const glob = `${opts.dir}/dobo/extend/${mod.ns}/schema/${base}.*`
37
37
  const files = await fastGlob(glob)
38
38
  for (const file of files) {
39
39
  const extender = await readConfig(file, { ns: opts.ns, ignoreError: true })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dobo",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Database ORM/ODM for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {