diff --git a/README.md b/README.md index 0e4c923..f4d6779 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ __Special Functions Config__ * [`writableCols`](#model-writableCols) * [`editableCols`](#model-editableCols) * [`onlyAdminCols`](#model-onlyAdminCols) +* [`allowIncludeCols`](#model-allowIncludeCols) * [`searchCols`](#model-searchCols) * [`stats`](#model-stats) @@ -695,8 +696,11 @@ module.exports = (sequelize) -> ### onlyAdminCols * 定义一个数组,用来指定添加或编辑时哪些字段只允许管理员指定,在使用 helper.rest.modify, helper.rest.add 时生效 + +### allowIncludeCols +* 定义一个数组,用来指定当资源被其他资源包含的时候(include)的时候那些列可以被查询返回,常见于 User 中,User 会被其他资源包含,但是 User 里的某些列需要隐藏,例如: password, email 等 -__Define writableCols, editableCols, onlyAdminCols example__ +__Define writableCols, editableCols, onlyAdminCols allowIncludeCols example__ ```coffee module.exports = (sequelize) -> @@ -754,6 +758,8 @@ module.exports = (sequelize) -> onlyAdminCols: [ 'role', 'status', 'switchs' ] + # 当 `user` 被其他资源包含的时候仅返回 `id`, `name`, `status` 三个字段,其余的不返回 + allowIncludeCols: ['id', 'name', 'status'] } ``` diff --git a/bin/openrest b/bin/openrest index 5444895..7e19101 100755 --- a/bin/openrest +++ b/bin/openrest @@ -10,6 +10,7 @@ params .version(pkg.version) .option('table-sync', '根据model定义同步表结构') .option('init', '初始化一个新的应用') + .option('ls', '列出有那些可用的样板工程') .parse(process.argv) # 初始化一个应用 diff --git a/bin/src/table-sync.coffee b/bin/src/table-sync.coffee index 3895aaf..be3644e 100644 --- a/bin/src/table-sync.coffee +++ b/bin/src/table-sync.coffee @@ -1,8 +1,20 @@ +async = require 'async' + module.exports = (root) -> rest = require "#{root}/node_modules/open-rest" db = require("#{root}/app/configs").db rest.model.init(db, "#{root}/app/models") + models = [] for name, Model of rest.model() - Model.sync() - console.log "#{name} sync done." + models.push(Model) + + async.map(models, (Model, callback) -> + Model.sync().then((name) -> + callback(null, Model.name) + ).catch(callback) + , (error, results) -> + console.log "#{results} sync done." + process.exit(0) + ) + diff --git a/lib/model.coffee b/lib/model.coffee index 8f403f0..ee84b40 100644 --- a/lib/model.coffee +++ b/lib/model.coffee @@ -1,7 +1,8 @@ # model of open-rest -_ = require 'underscore' -utils = require './utils' -Sequelize = require 'sequelize' +_ = require 'underscore' +utils = require './utils' +Sequelize = require 'sequelize' +_pageParams = require './page-params' # 存放 models 的定义 Models = {} @@ -67,8 +68,8 @@ model.statistics = statistics = (params, where, conf, callback) -> where: Sequelize.and.apply(Sequelize, ands) group: utils.stats.group(dims) order: utils.stats.sort(Model, params) - offset: limit[0] - limit: limit[1] + offset: limit.offset + limit: limit.limit raw: yes if listOpts.include option.include = _.map(listOpts.include, (x) -> @@ -112,6 +113,7 @@ model.findAllOpts = findAllOpts = (params, isAll = no) -> searchOrs.push utils.searchOpt(Model, params._searchs, params.q) # 处理关联资源的过滤条件 + # 以及关联资源允许返回的字段 if includes _.each(includes, (x) -> includeWhere = {} @@ -126,6 +128,9 @@ model.findAllOpts = findAllOpts = (params, isAll = no) -> searchOrs.push utils.searchOpt(x.model, params._searchs, params.q, x.as) x.where = includeWhere if _.size(includeWhere) + + # 以及关联资源允许返回的字段 + x.attributes = x.model.allowIncludeCols if x.model.allowIncludeCols ) # 将 searchOrs 赋到 where 上 @@ -173,11 +178,7 @@ model.modelInclude = modelInclude = (params, includes) -> # } ### model.pageParams = pageParams = (params) -> - pagination = @pagination - startIndex = (+params.startIndex or 0) - maxResults = (+params.maxResults or +pagination.maxResults) - limit: Math.min(maxResults, pagination.maxResultsLimit) - offset: Math.min(startIndex, pagination.maxStartIndex) + _pageParams(@pagination, params) ### # 处理排序参数 diff --git a/lib/page-params.coffee b/lib/page-params.coffee new file mode 100644 index 0000000..0156677 --- /dev/null +++ b/lib/page-params.coffee @@ -0,0 +1,5 @@ +module.exports = (pagination, params) -> + startIndex = Math.max((+params.startIndex or 0), 0) + maxResults = Math.max((+params.maxResults or +pagination.maxResults), 0) + offset: Math.min(startIndex, pagination.maxStartIndex) + limit: Math.min(maxResults, pagination.maxResultsLimit) diff --git a/lib/stats.coffee b/lib/stats.coffee index 9127890..b080990 100644 --- a/lib/stats.coffee +++ b/lib/stats.coffee @@ -1,6 +1,7 @@ -_ = require 'underscore' -Sequelize = require 'sequelize' -dc = decodeURIComponent +_ = require 'underscore' +Sequelize = require 'sequelize' +dc = decodeURIComponent +pageParams = require './page-params' defaultPagination = maxResults: 10 @@ -90,8 +91,4 @@ module.exports = pageParams: (Model, params) -> pagination = Model.stats.pagination or defaultPagination - startIndex = (+params.startIndex or 0) - maxResults = (+params.maxResults or +pagination.maxResults) - limit = Math.min(maxResults, pagination.maxResultsLimit) - offset = Math.min(startIndex, pagination.maxStartIndex) - [offset, limit] + pageParams(pagination, params) diff --git a/package.json b/package.json index ca3c978..dab917d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "open-rest", - "version": "6.0.0", + "version": "6.0.1", "description": "Standard rest server, Base on restify and sequelize", "main": "index.js", "scripts": { diff --git a/test/stats.coffee b/test/stats.coffee index acd9889..04d80f1 100644 --- a/test/stats.coffee +++ b/test/stats.coffee @@ -249,7 +249,7 @@ describe 'stats', -> Model = stats: {} params = {} - expected = [0, 10] + expected = offset: 0, limit: 10 assert.deepEqual stats.pageParams(Model, params), expected done() @@ -259,7 +259,7 @@ describe 'stats', -> params = startIndex: 20 maxResults: 15 - expected = [20, 15] + expected = offset: 20, limit: 15 assert.deepEqual stats.pageParams(Model, params), expected done() @@ -271,7 +271,7 @@ describe 'stats', -> maxResultsLimit: 2000 maxStartIndex: 50000 params = {} - expected = [0, 20] + expected = offset: 0, limit: 20 assert.deepEqual stats.pageParams(Model, params), expected done() @@ -284,7 +284,7 @@ describe 'stats', -> maxStartIndex: 50000 params = startIndex: 50 - expected = [50, 20] + expected = offset: 50, limit: 20 assert.deepEqual stats.pageParams(Model, params), expected done() @@ -297,7 +297,7 @@ describe 'stats', -> maxStartIndex: 50000 params = startIndex: 5000000 - expected = [50000, 20] + expected = offset: 50000, limit: 20 assert.deepEqual stats.pageParams(Model, params), expected done() @@ -311,6 +311,20 @@ describe 'stats', -> params = startIndex: 5000000 maxResults: 10000 - expected = [50000, 2000] + expected = offset: 50000, limit: 2000 + assert.deepEqual stats.pageParams(Model, params), expected + done() + + it "set pagination lt 0", (done) -> + Model = + stats: + pagination: + maxResults: 20 + maxResultsLimit: 2000 + maxStartIndex: 50000 + params = + startIndex: -1 + maxResults: -10 + expected = offset: 0, limit: 0 assert.deepEqual stats.pageParams(Model, params), expected done()