dobo 1.0.7 → 1.0.8
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/bajo/method/record/create.js +13 -6
- package/package.json +1 -1
|
@@ -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
|
-
|
|
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 })
|