waibu-db 1.2.6 → 1.2.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.
@@ -1,15 +1,15 @@
1
- async function addHandler ({ req, reply, model, params = {}, template, addOnsHandler, templateDisabled = 'waibuDb.template:/disabled.html' } = {}) {
1
+ async function addHandler ({ req, reply, model, params = {}, template, addOnsHandler, templateDisabled = 'waibuDb.template:/disabled.html', options = {} } = {}) {
2
2
  const { pascalCase } = this.lib.aneka
3
3
  const { recordCreate, recordGet, getSchemaExt } = this.app.waibuDb
4
4
  const { buildUrl } = this.app.waibuMpa
5
5
  const { defaultsDeep } = this.lib.aneka
6
6
  const { pick, map, merge, omit, isEmpty } = this.lib._
7
- const options = {}
7
+ const opts = {}
8
8
  model = model ?? pascalCase(req.params.model)
9
- const { schema } = await getSchemaExt(model, 'add', { params })
9
+ const { schema } = await getSchemaExt(model, 'add', merge({}, { params }, options))
10
10
  if (schema.disabled.includes('create')) return await reply.view(templateDisabled, { action: 'add' })
11
11
  // req.query.attachment = true
12
- options.fields = schema.view.fields
12
+ opts.fields = schema.view.fields
13
13
  let def = {}
14
14
  if (req.method === 'GET' && req.query.mode === 'clone' && req.query.id) {
15
15
  const resp = await recordGet({ model, req, id: req.query.id, options: { fields: map(schema.properties, 'name') } })
@@ -22,7 +22,7 @@ async function addHandler ({ req, reply, model, params = {}, template, addOnsHan
22
22
  req.session[`wdb${model}AddMore`] = form._addmore
23
23
  req.session[`wdb${model}ClonePrev`] = form._cloneprev
24
24
  try {
25
- resp = await recordCreate({ model, req, reply, options })
25
+ resp = await recordCreate({ model, req, reply, options: opts })
26
26
  if (isEmpty(form._addmore)) return reply.redirectTo(buildUrl({ url: req.url, base: 'list', params: { page: 1 }, exclude: ['id', 'mode'] }))
27
27
  if (isEmpty(form._cloneprev)) form = pick(form, ['_addmore', '_cloneprev'])
28
28
  } catch (err) {
@@ -32,7 +32,7 @@ async function addHandler ({ req, reply, model, params = {}, template, addOnsHan
32
32
  form._addmore = req.session[`wdb${model}AddMore`]
33
33
  form._cloneprev = req.session[`wdb${model}ClonePrev`]
34
34
  }
35
- const addOns = addOnsHandler ? await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: resp, schema, error }) : undefined
35
+ const addOns = addOnsHandler ? await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: resp, schema, error, options }) : undefined
36
36
  merge(params, { form, schema, error, addOns })
37
37
  if (schema.template) template = schema.template
38
38
  if (schema.layout) params.page.layout = schema.layout
@@ -17,15 +17,15 @@ const handler = {
17
17
  list: listHandler
18
18
  }
19
19
 
20
- async function allHandler ({ model, action, req, reply, template, params = {} }) {
20
+ async function allHandler ({ model, action, req, reply, template, params = {}, options = {} }) {
21
21
  const { upperFirst, merge, keys } = this.lib._
22
22
  if (!keys(handler).includes(action)) throw this.error('_notFound')
23
23
  if (['delete', 'export'].includes(action)) {
24
24
  if (req.method === 'GET') throw this.error('_notFound')
25
- return await handler[action].call(this, { model, req, reply })
25
+ return await handler[action].call(this, { model, req, reply, options })
26
26
  }
27
- const allParams = merge(buildParams.call(this, { model, req, reply, action: upperFirst(action) }), params)
28
- return await handler[action].call(this, { model, req, reply, params: allParams, template, addOnsHandler })
27
+ const allParams = merge(buildParams.call(this, { model, req, reply, action: upperFirst(action), options }), params)
28
+ return await handler[action].call(this, { model, req, reply, params: allParams, template, addOnsHandler, options })
29
29
  }
30
30
 
31
31
  export default allHandler
@@ -1,20 +1,21 @@
1
- async function deleteHandler ({ req, reply, model, params = {}, templateDisabled = 'waibuDb.template:/disabled.html' } = {}) {
1
+ async function deleteHandler ({ req, reply, model, params = {}, templateDisabled = 'waibuDb.template:/disabled.html', options = {} } = {}) {
2
2
  const { pascalCase } = this.lib.aneka
3
3
  const { recordRemove, getSchemaExt } = this.app.waibuDb
4
4
  const { buildUrl } = this.app.waibuMpa
5
- const { reduce } = this.lib._
6
- const options = {}
5
+ const { reduce, merge } = this.lib._
6
+ const opts = {}
7
7
  model = model ?? pascalCase(req.params.model)
8
- const { schema } = await getSchemaExt(model, 'add', { params })
8
+ const { schema } = await getSchemaExt(model, 'add', merge({}, { params }, options))
9
9
  if (schema.disabled.includes('remove')) return await reply.view(templateDisabled, { action: 'delete' })
10
- options.fields = schema.view.fields
10
+ opts.fields = schema.view.fields
11
11
  const ids = (req.body.ids ?? '').split(',')
12
12
  if (ids.length > 0) {
13
13
  const result = []
14
- const options = { noResult: true, noFlash: true }
14
+ opts.noResult = true
15
+ opts.noFlash = true
15
16
  for (const id of ids) {
16
17
  try {
17
- await recordRemove({ model, id, req, reply, options })
18
+ await recordRemove({ model, id, req, reply, options: opts })
18
19
  result.push(true)
19
20
  } catch (err) {
20
21
  result.push(err.message)
@@ -1,20 +1,20 @@
1
1
  import attachmentHandler from './helper/attachment-handler.js'
2
2
 
3
- async function detailsHandler ({ req, reply, model, params = {}, id, template, addOnsHandler, templateDisabled = 'waibuDb.template:/disabled.html' } = {}) {
3
+ async function detailsHandler ({ req, reply, model, params = {}, id, template, addOnsHandler, templateDisabled = 'waibuDb.template:/disabled.html', options } = {}) {
4
4
  const { pascalCase } = this.lib.aneka
5
5
  const { recordGet, getSchemaExt } = this.app.waibuDb
6
6
  const { merge } = this.lib._
7
- const options = {}
7
+ const opts = {}
8
8
  model = model ?? pascalCase(req.params.model)
9
- const { schema } = await getSchemaExt(model, 'details', { params })
9
+ const { schema } = await getSchemaExt(model, 'details', merge({}, { params }, options))
10
10
  if (schema.disabled.includes('get')) return await reply.view(templateDisabled, { action: 'details' })
11
11
  // req.query.attachment = true
12
- options.fields = schema.view.fields
12
+ opts.fields = schema.view.fields
13
13
  id = id ?? req.params.id ?? req.query.id
14
- const resp = await recordGet({ model, req, id, options })
14
+ const resp = await recordGet({ model, req, id, options: opts })
15
15
  const form = resp.data
16
- const addOns = addOnsHandler ? await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: resp, schema }) : undefined
17
- const attachments = await attachmentHandler.call(this, { schema, id })
16
+ const addOns = addOnsHandler ? await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: resp, schema, options }) : undefined
17
+ const attachments = await attachmentHandler.call(this, { schema, id, options })
18
18
  merge(params, { form, schema, addOns, attachments })
19
19
  if (schema.template) template = schema.template
20
20
  if (schema.layout) params.page.layout = schema.layout
@@ -1,6 +1,6 @@
1
1
  import attachmentHandler from './helper/attachment-handler.js'
2
2
 
3
- async function editHandler ({ req, reply, model, id, params = {}, template, addOnsHandler, templateDisabled = 'waibuDb.template:/disabled.html' } = {}) {
3
+ async function editHandler ({ req, reply, model, id, params = {}, template, addOnsHandler, templateDisabled = 'waibuDb.template:/disabled.html', options = {} } = {}) {
4
4
  const { pascalCase } = this.lib.aneka
5
5
  const { getPluginDataDir } = this.app.bajo
6
6
  const { recordUpdate, recordGet, getSchemaExt } = this.app.waibuDb
@@ -8,18 +8,18 @@ async function editHandler ({ req, reply, model, id, params = {}, template, addO
8
8
  const { fs } = this.lib
9
9
  const { defaultsDeep } = this.lib.aneka
10
10
  const { merge, isEmpty, omit } = this.lib._
11
- const options = {}
11
+ const opts = {}
12
12
  let error
13
13
  let resp
14
14
  let form
15
15
  model = model ?? pascalCase(req.params.model)
16
- const { schema } = await getSchemaExt(model, 'edit', options, { params })
16
+ const { schema } = await getSchemaExt(model, 'edit', merge({}, { params }, options))
17
17
  if (schema.disabled.includes('update')) return await reply.view(templateDisabled, { action: 'edit' })
18
18
  // req.query.attachment = true
19
- options.fields = schema.view.fields
19
+ opts.fields = schema.view.fields
20
20
  id = id ?? req.params.id ?? req.query.id
21
21
  if (req.method === 'GET') {
22
- const old = await recordGet({ model, req, id, options })
22
+ const old = await recordGet({ model, req, id, options: opts })
23
23
  form = defaultsDeep(req.body, old.data)
24
24
  } else {
25
25
  form = omit(req.body, ['_action', '_value'])
@@ -34,7 +34,7 @@ async function editHandler ({ req, reply, model, id, params = {}, template, addO
34
34
  if (req && req.flash) req.flash('notify', req.t('attachmentRemoved'))
35
35
  } else {
36
36
  try {
37
- resp = await recordUpdate({ model, req, id, reply, options })
37
+ resp = await recordUpdate({ model, req, id, reply, options: opts })
38
38
  form = resp.data
39
39
  return reply.redirectTo(buildUrl({ url: req.url, base: req.params.base ?? req.query.base ?? 'list', params: { page: 1 }, exclude: ['id'] }))
40
40
  } catch (err) {
@@ -42,8 +42,8 @@ async function editHandler ({ req, reply, model, id, params = {}, template, addO
42
42
  }
43
43
  }
44
44
  }
45
- const addOns = addOnsHandler ? await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: resp, schema, error }) : undefined
46
- const attachments = await attachmentHandler.call(this, { schema, id })
45
+ const addOns = addOnsHandler ? await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: resp, schema, error, options }) : undefined
46
+ const attachments = await attachmentHandler.call(this, { schema, id, options })
47
47
  merge(params, { form, schema, error, addOns, attachments })
48
48
  if (schema.template) template = schema.template
49
49
  if (schema.layout) params.page.layout = schema.layout
@@ -1,15 +1,15 @@
1
1
  import prepCrud from '../prep-crud.js'
2
2
 
3
- async function exportHandler ({ req, reply, model, params = {}, templateDisabled = 'waibuDb.template:/disabled.html' } = {}) {
3
+ async function exportHandler ({ req, reply, model, params = {}, templateDisabled = 'waibuDb.template:/disabled.html', options = {} } = {}) {
4
4
  const { getPlugin } = this.app.bajo
5
5
  const { dayjs } = this.lib
6
- const { omit, kebabCase, get } = this.lib._
6
+ const { omit, kebabCase, get, merge } = this.lib._
7
7
  const { pascalCase } = this.lib.aneka
8
8
  const { getSchemaExt } = this.app.waibuDb
9
9
  const { buildUrl } = this.app.waibuMpa
10
10
  const { pushDownload } = getPlugin('sumba')
11
11
  model = model ?? pascalCase(req.params.model)
12
- const { schema } = await getSchemaExt(model, 'add', { params })
12
+ const { schema } = await getSchemaExt(model, 'add', merge({}, { params }, options))
13
13
  if (schema.disabled.includes('find')) return await reply.view(templateDisabled, { action: 'list' })
14
14
  const data = prepCrud.call(getPlugin('waibuDb'), { model, req, reply, args: ['model'] })
15
15
  data.opts = omit(data.opts, ['req', 'reply'])
@@ -1,4 +1,4 @@
1
- async function addOnsHandler ({ req, reply, data, schema }) {
1
+ async function addOnsHandler ({ req, reply, data, schema, options = {} }) {
2
2
  const { escape } = this.app.waibu
3
3
  const { base64JsonEncode } = this.app.waibuMpa
4
4
  const { statAggregate } = this.app.waibuDb
@@ -1,4 +1,4 @@
1
- async function attachmentHandler ({ schema, id }) {
1
+ async function attachmentHandler ({ schema, id, options = {} }) {
2
2
  const { listAttachments } = this.app.dobo
3
3
  if (!schema.view.attachment) return []
4
4
  return await listAttachments({ model: schema.name, id })
@@ -1,4 +1,4 @@
1
- function buildParams ({ model, req, reply, action }) {
1
+ function buildParams ({ model, req, reply, action, options = {} }) {
2
2
  const { camelCase, kebabCase, map, upperFirst, get } = this.lib._
3
3
  const { getSchema } = this.app.dobo
4
4
  const [, ...names] = map(kebabCase(model).split('-'), n => upperFirst(n))
@@ -1,11 +1,10 @@
1
- async function listHandler ({ req, reply, model, template, params = {}, addOnsHandler, templateDisabled = 'waibuDb.template:/disabled.html' } = {}) {
1
+ async function listHandler ({ req, reply, model, template, params = {}, addOnsHandler, templateDisabled = 'waibuDb.template:/disabled.html', options = {} } = {}) {
2
2
  const { pascalCase } = this.lib.aneka
3
3
  const { recordFind, getSchemaExt } = this.app.waibuDb
4
4
  const { get, merge, isArray, upperFirst } = this.lib._
5
5
  const qsKey = this.app.waibu.config.qsKey
6
- const options = { count: true, rels: '*' }
7
6
  model = model ?? pascalCase(req.params.model)
8
- const { schema } = await getSchemaExt(model, 'list', { params })
7
+ const { schema } = await getSchemaExt(model, 'list', merge({}, { params }, options))
9
8
  if (schema.disabled.includes('find')) return await reply.view(templateDisabled, { action: 'list' })
10
9
  for (const key of ['sort', 'limit', 'fields']) {
11
10
  const sessKey = `wdb${model}${upperFirst(key)}`
@@ -14,10 +13,10 @@ async function listHandler ({ req, reply, model, template, params = {}, addOnsHa
14
13
  }
15
14
  if (!req.query[qsKey.page]) req.query[qsKey.page] = 1
16
15
  // req.query.attachment = true
17
- const list = await recordFind({ model, req, options })
16
+ const list = await recordFind({ model, req, options: { count: true, rels: '*' } })
18
17
  let addOns = []
19
18
  if (addOnsHandler) {
20
- addOns = await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: list, schema })
19
+ addOns = await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: list, schema, options })
21
20
  if (!isArray(addOns)) addOns = [addOns]
22
21
  }
23
22
  merge(params, { list, schema, addOns })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu-db",
3
- "version": "1.2.6",
3
+ "version": "1.2.8",
4
4
  "description": "DB Helper",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -4,7 +4,7 @@ const defReadonly = ['id', 'createdAt', 'updatedAt']
4
4
 
5
5
  const defFormatter = {}
6
6
 
7
- function getCommons (action, schema, ext, opts = {}) {
7
+ function getCommons (action, schema, ext, options = {}) {
8
8
  const { defaultsDeep } = this.lib.aneka
9
9
  const { merge, map, get, set, without, uniq, pull } = this.lib._
10
10
  const calcFields = get(ext, `view.${action}.calcFields`, get(ext, 'common.calcFields', []))
@@ -21,7 +21,7 @@ function getCommons (action, schema, ext, opts = {}) {
21
21
  const aggregate = get(ext, `view.${action}.stat.aggregate`, get(ext, 'common.stat.aggregate', []))
22
22
  let attachment = get(ext, `view.${action}.attachment`, get(ext, 'common.attachment', false))
23
23
  if (!schema.attachment || action === 'list') attachment = false
24
- hidden.push('siteId', ...schema.hidden, ...(opts.hidden ?? []))
24
+ hidden.push('siteId', ...schema.hidden, ...(options.hidden ?? []))
25
25
  hidden = uniq(hidden)
26
26
  pull(hidden, ...forceVisible)
27
27
  const allFields = without(map(schema.properties, 'name'), ...hidden)
@@ -44,7 +44,8 @@ function getCommons (action, schema, ext, opts = {}) {
44
44
  if (calcFields.length > 0) fields.push(...map(calcFields, 'name'))
45
45
  fields = uniq(without(fields, ...hidden))
46
46
 
47
- if (action !== 'add' && !fields.includes('id')) fields.unshift('id')
47
+ options.forceShowId = options.forceShowId ?? true
48
+ if (options.forceShowId && action !== 'add' && !fields.includes('id')) fields.unshift('id')
48
49
  let noWrap = get(ext, `view.${action}.noWrap`, get(ext, 'common.noWrap', true))
49
50
  if (noWrap === true) noWrap = fields
50
51
  else if (noWrap === false) noWrap = []
@@ -131,9 +132,9 @@ function applyLayout (action, schema, ext) {
131
132
  }
132
133
 
133
134
  const handler = {
134
- list: async function (schema, ext, opts) {
135
+ list: async function (schema, ext, options) {
135
136
  const { get, set } = this.lib._
136
- const { fields } = getCommons.call(this, 'list', schema, ext, opts)
137
+ const { fields } = getCommons.call(this, 'list', schema, ext, options)
137
138
  const qsFields = []
138
139
  for (const f of get(schema, 'view.qs.fields', '').split(',')) {
139
140
  if (fields.includes(f)) qsFields.push(f)
@@ -148,18 +149,18 @@ const handler = {
148
149
  set(schema, 'view.fields', fields)
149
150
  set(schema, 'view.qs.fields', qsFields.join(','))
150
151
  },
151
- details: async function (schema, ext, opts) {
152
- applyLayout.call(this, 'details', schema, ext, opts)
152
+ details: async function (schema, ext, options) {
153
+ applyLayout.call(this, 'details', schema, ext, options)
153
154
  },
154
- add: async function (schema, ext, opts) {
155
- applyLayout.call(this, 'add', schema, ext, opts)
155
+ add: async function (schema, ext, options) {
156
+ applyLayout.call(this, 'add', schema, ext, options)
156
157
  },
157
- edit: async function (schema, ext, opts) {
158
- applyLayout.call(this, 'edit', schema, ext, opts)
158
+ edit: async function (schema, ext, options) {
159
+ applyLayout.call(this, 'edit', schema, ext, options)
159
160
  }
160
161
  }
161
162
 
162
- async function getSchemaExt (model, view, opts = {}) {
163
+ async function getSchemaExt (model, view, options = {}) {
163
164
  const { readConfig } = this.app.bajo
164
165
  const { defaultsDeep } = this.lib.aneka
165
166
  const { getSchema } = this.app.dobo
@@ -167,10 +168,10 @@ async function getSchemaExt (model, view, opts = {}) {
167
168
 
168
169
  let schema = getSchema(model)
169
170
  const base = path.basename(schema.file, path.extname(schema.file))
170
- let ext = await readConfig(`${schema.ns}:/waibuDb/schema/${base}.*`, { ignoreError: true, opts })
171
- const over = await readConfig(`main:/waibuDb/extend/${schema.ns}/schema/${base}.*`, { ignoreError: true, opts })
172
- ext = defaultsDeep(opts.schema ?? {}, over, ext)
173
- await handler[view].call(this, schema, ext, opts)
171
+ let ext = await readConfig(`${schema.ns}:/waibuDb/schema/${base}.*`, { ignoreError: true, options })
172
+ const over = await readConfig(`main:/waibuDb/extend/${schema.ns}/schema/${base}.*`, { ignoreError: true, options })
173
+ ext = defaultsDeep(options.schema ?? {}, over, ext)
174
+ await handler[view].call(this, schema, ext, options)
174
175
  schema = pick(schema, ['name', 'properties', 'indexes', 'disabled', 'attachment', 'sortables', 'view'])
175
176
  return { schema, ext }
176
177
  }