koa-restql
Advanced tools
Comparing version 1.0.4 to 1.0.5
'use strict' | ||
const _ = require('lodash') | ||
const debug = require('debug')('koa-restql:common') | ||
@@ -164,27 +165,51 @@ | ||
function unionLimit (_limit, options) { | ||
if (_limit !== null) { | ||
return +_limit || +options.query._limit | ||
} | ||
} | ||
function parseQuery (query, model, method, options) { | ||
const { | ||
_attributes, _order, _through, _include, | ||
_group, _having, _offset, _limit, | ||
_subQuery, _distinct, _ignoreDuplicates | ||
} = query | ||
const queryParsers = { | ||
'_include' : (include) => unionInclude(include, model.associations, method), | ||
'_limit' : (limit) => unionLimit(limit, options), | ||
'_offset' : (offset) => +offset || 0, | ||
'_distinct' : (distinct) => !!+distinct, | ||
'_subQuery' : (subQuery) => subQuery === undefined ? subQuery : !!+subQuery, | ||
'_ignoreDuplicates': (ignoreDuplicates) => !!+ignoreDuplicates | ||
} | ||
const parsedQuery = {} | ||
parsedQuery.where = unionWhere(query) | ||
parsedQuery.include = unionInclude(_include, model.associations, method) | ||
parsedQuery.attributes = _attributes | ||
parsedQuery.order = _order | ||
parsedQuery.through = _through | ||
parsedQuery.group = _group | ||
parsedQuery.having = _having | ||
parsedQuery.subQuery = _subQuery === undefined ? _subQuery : !!+_subQuery | ||
parsedQuery.offset = +_offset || 0 | ||
parsedQuery.distinct = !!+_distinct | ||
parsedQuery.ignoreDuplicates = !!+_ignoreDuplicates | ||
if (_limit !== null) { | ||
parsedQuery.limit = +_limit || +options.query._limit | ||
_.keys(query).forEach(key => { | ||
const regex = /^_/ | ||
const parser = queryParsers[key] | ||
if (!regex.test(key)) | ||
return | ||
const propName = key.replace(regex, '') | ||
const propValue = parser ? parser(query[key]) : query[key] | ||
if (propValue !== undefined) { | ||
parsedQuery[propName] = propValue | ||
} | ||
}) | ||
parsedQuery.where = parsedQuery.where || {} | ||
_.assign(parsedQuery.where, unionWhere(query)) | ||
if (parsedQuery.limit === undefined) { | ||
parsedQuery.limit = queryParsers['_limit'](query._limit) | ||
} | ||
if (parsedQuery.offset === undefined) { | ||
parsedQuery.offset = queryParsers['_offset'](query._offset) | ||
} | ||
debug(parsedQuery) | ||
@@ -191,0 +216,0 @@ |
@@ -337,3 +337,3 @@ 'use strict' | ||
if (!body[as]) | ||
this.throw('RestQL: ${model.name} ${as} not found', 404) | ||
this.throw(`RestQL: ${model.name} ${as} not found`, 404) | ||
@@ -408,2 +408,3 @@ yield body[as].destroy() | ||
query.include = query.include || [] | ||
query.include.push({ | ||
@@ -488,3 +489,3 @@ association : association.oneFromTarget, | ||
if (!data) | ||
this.throw('RestQL: ${model.name} not found', 404) | ||
this.throw(`RestQL: ${model.name} not found`, 404) | ||
@@ -900,3 +901,3 @@ response.body = data | ||
if (!data.length) { | ||
this.throw('RestQL: ${as} not found', 404) | ||
this.throw(`RestQL: ${as} not found`, 404) | ||
} | ||
@@ -903,0 +904,0 @@ |
{ | ||
"name": "koa-restql", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Koa RESTful API middleware based on Sequlizejs", | ||
@@ -5,0 +5,0 @@ "main": "lib/RestQL.js", |
@@ -350,5 +350,5 @@ # koa-restql | ||
[travis-image]: https://img.shields.io/travis/Meituan-Dianping/koa-restql/master.svg?maxAge=2592000 | ||
[travis-image]: https://img.shields.io/travis/Meituan-Dianping/koa-restql/master.svg?maxAge=0 | ||
[travis-url]: https://travis-ci.org/Meituan-Dianping/koa-restql | ||
[npm-image]: https://img.shields.io/npm/v/koa-restql.svg?maxAge=2592000 | ||
[npm-image]: https://img.shields.io/npm/v/koa-restql.svg?maxAge=0 | ||
[npm-url]: https://www.npmjs.com/package/koa-restql | ||
@@ -355,0 +355,0 @@ [1]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html |
54303
1579