waibu-db 1.2.1 → 1.2.3

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/README.md CHANGED
@@ -18,6 +18,10 @@ $ npm install waibu-db
18
18
 
19
19
  Now open your ```<bajo-data-dir>/config/.plugins``` and put ```waibu-db``` in it
20
20
 
21
+ ## Configuration
22
+
23
+ - ```modelRestApi```: set to ```true``` to enable the auto creation of model's rest API. default: ```false```
24
+
21
25
  ## License
22
26
 
23
27
  [MIT](LICENSE)
@@ -0,0 +1,10 @@
1
+ async function afterInit () {
2
+ if (!this.config.modelRestApi) {
3
+ this.app.waibuRestApi.config.disabled.push(
4
+ 'waibuDb:/:model',
5
+ 'waibuDb:/:model/:id'
6
+ )
7
+ }
8
+ }
9
+
10
+ export default afterInit
package/index.js CHANGED
@@ -21,7 +21,8 @@ async function factory (pkgName) {
21
21
  dbModel: {
22
22
  count: false,
23
23
  patchEnabled: false
24
- }
24
+ },
25
+ modelRestApi: false
25
26
  }
26
27
  }
27
28
  }
@@ -7,7 +7,7 @@ async function addHandler ({ req, reply, model, params = {}, template, addOnsHan
7
7
  const options = {}
8
8
  model = model ?? pascalCase(req.params.model)
9
9
  const { schema } = await getSchemaExt(model, 'add', { params })
10
- if (schema.disabled.includes('create')) return reply.view(templateDisabled, { action: 'add' })
10
+ if (schema.disabled.includes('create')) return await reply.view(templateDisabled, { action: 'add' })
11
11
  // req.query.attachment = true
12
12
  options.fields = schema.view.fields
13
13
  let def = {}
@@ -34,7 +34,7 @@ async function addHandler ({ req, reply, model, params = {}, template, addOnsHan
34
34
  }
35
35
  const addOns = addOnsHandler ? await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: resp, schema, error }) : undefined
36
36
  merge(params, { form, schema, error, addOns })
37
- return reply.view(template, params)
37
+ return await reply.view(template, params)
38
38
  }
39
39
 
40
40
  export default addHandler
@@ -6,7 +6,7 @@ async function deleteHandler ({ req, reply, model, params = {}, templateDisabled
6
6
  const options = {}
7
7
  model = model ?? pascalCase(req.params.model)
8
8
  const { schema } = await getSchemaExt(model, 'add', { params })
9
- if (schema.disabled.includes('remove')) return reply.view(templateDisabled, { action: 'delete' })
9
+ if (schema.disabled.includes('remove')) return await reply.view(templateDisabled, { action: 'delete' })
10
10
  options.fields = schema.view.fields
11
11
  const ids = (req.body.ids ?? '').split(',')
12
12
  if (ids.length > 0) {
@@ -7,7 +7,7 @@ async function detailsHandler ({ req, reply, model, params = {}, id, template, a
7
7
  const options = {}
8
8
  model = model ?? pascalCase(req.params.model)
9
9
  const { schema } = await getSchemaExt(model, 'details', { params })
10
- if (schema.disabled.includes('get')) return reply.view(templateDisabled, { action: 'details' })
10
+ if (schema.disabled.includes('get')) return await reply.view(templateDisabled, { action: 'details' })
11
11
  // req.query.attachment = true
12
12
  options.fields = schema.view.fields
13
13
  id = id ?? req.params.id ?? req.query.id
@@ -16,7 +16,7 @@ async function detailsHandler ({ req, reply, model, params = {}, id, template, a
16
16
  const addOns = addOnsHandler ? await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: resp, schema }) : undefined
17
17
  const attachments = await attachmentHandler.call(this, { schema, id })
18
18
  merge(params, { form, schema, addOns, attachments })
19
- return reply.view(template, params)
19
+ return await reply.view(template, params)
20
20
  }
21
21
 
22
22
  export default detailsHandler
@@ -14,7 +14,7 @@ async function editHandler ({ req, reply, model, id, params = {}, template, addO
14
14
  let form
15
15
  model = model ?? pascalCase(req.params.model)
16
16
  const { schema } = await getSchemaExt(model, 'edit', options, { params })
17
- if (schema.disabled.includes('update')) return reply.view(templateDisabled, { action: 'edit' })
17
+ if (schema.disabled.includes('update')) return await reply.view(templateDisabled, { action: 'edit' })
18
18
  // req.query.attachment = true
19
19
  options.fields = schema.view.fields
20
20
  id = id ?? req.params.id ?? req.query.id
@@ -45,7 +45,7 @@ async function editHandler ({ req, reply, model, id, params = {}, template, addO
45
45
  const addOns = addOnsHandler ? await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: resp, schema, error }) : undefined
46
46
  const attachments = await attachmentHandler.call(this, { schema, id })
47
47
  merge(params, { form, schema, error, addOns, attachments })
48
- return reply.view(template, params)
48
+ return await reply.view(template, params)
49
49
  }
50
50
 
51
51
  export default editHandler
@@ -5,7 +5,7 @@ async function exportHandler ({ req, reply, model, params = {}, templateDisabled
5
5
  const options = {}
6
6
  model = model ?? pascalCase(req.params.model)
7
7
  const { schema } = await getSchemaExt(model, 'add', options, { params })
8
- if (schema.disabled.includes('find')) return reply.view(templateDisabled, { action: 'list' })
8
+ if (schema.disabled.includes('find')) return await reply.view(templateDisabled, { action: 'list' })
9
9
  options.fields = schema.view.fields
10
10
  const url = buildUrl({ url: req.url, base: req.body.handler })
11
11
  req.flash('notify', req.t('Data export in queue. You\'ll be notified once completed'))
@@ -6,7 +6,7 @@ async function listHandler ({ req, reply, model, template, params = {}, addOnsHa
6
6
  const options = { count: true, rels: '*' }
7
7
  model = model ?? pascalCase(req.params.model)
8
8
  const { schema } = await getSchemaExt(model, 'list', { params })
9
- if (schema.disabled.includes('find')) return reply.view(templateDisabled, { action: 'list' })
9
+ if (schema.disabled.includes('find')) return await reply.view(templateDisabled, { action: 'list' })
10
10
  for (const key of ['sort', 'limit', 'fields']) {
11
11
  const sessKey = `wdb${model}${upperFirst(key)}`
12
12
  if (!req.query[qsKey[key]]) req.query[qsKey[key]] = req.session[sessKey] ?? get(schema, `view.qs.${key}`)
@@ -21,7 +21,7 @@ async function listHandler ({ req, reply, model, template, params = {}, addOnsHa
21
21
  if (!isArray(addOns)) addOns = [addOns]
22
22
  }
23
23
  merge(params, { list, schema, addOns })
24
- return reply.view(template, params)
24
+ return await reply.view(template, params)
25
25
  }
26
26
 
27
27
  export default listHandler
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu-db",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "DB Helper",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,5 @@
1
+ async function get (req, reply) {
2
+ return await this.recordGet({ req, reply })
3
+ }
4
+
5
+ export default get
@@ -0,0 +1,5 @@
1
+ async function remove (req, reply) {
2
+ return await this.recordRemove({ req, reply })
3
+ }
4
+
5
+ export default remove
@@ -0,0 +1,5 @@
1
+ async function update (req, reply) {
2
+ return await this.recordUpdate({ req, reply })
3
+ }
4
+
5
+ export default update
@@ -0,0 +1,5 @@
1
+ async function create (req, reply) {
2
+ return await this.recordCreate({ req, reply })
3
+ }
4
+
5
+ export default create
@@ -0,0 +1,14 @@
1
+ async function find (req, reply) {
2
+ const { isSet } = this.lib.aneka
3
+ const { parseObject } = this.app.bajo
4
+ let { fields, count } = this.getParams(req)
5
+ let rels = []
6
+ const headers = parseObject(req.headers, { parseValue: true })
7
+ if (isSet(headers['x-count'])) count = headers['x-count']
8
+ if (isSet(headers['x-rels'])) rels = headers['x-rels']
9
+ if (typeof rels === 'string' && !['*', 'all'].includes(rels)) rels = [rels]
10
+ const options = { fields, count, rels }
11
+ return await this.recordFind({ req, reply, options })
12
+ }
13
+
14
+ export default find