Skip to content

Commit

Permalink
修正table-sync 工具,修正 pageParams 的处理
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoxiongfei committed Apr 27, 2016
1 parent b884652 commit 0232e6f
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 28 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -695,8 +696,11 @@ module.exports = (sequelize) ->
### onlyAdminCols
* 定义一个数组,用来指定添加或编辑时哪些字段只允许管理员指定,在使用 helper.rest.modify, helper.rest.add 时生效

<a name="model-allowIncludeCols"></a>
### allowIncludeCols
* 定义一个数组,用来指定当资源被其他资源包含的时候(include)的时候那些列可以被查询返回,常见于 User 中,User 会被其他资源包含,但是 User 里的某些列需要隐藏,例如: password, email 等

__Define writableCols, editableCols, onlyAdminCols example__
__Define writableCols, editableCols, onlyAdminCols allowIncludeCols example__

```coffee
module.exports = (sequelize) ->
Expand Down Expand Up @@ -754,6 +758,8 @@ module.exports = (sequelize) ->
onlyAdminCols: [
'role', 'status', 'switchs'
]
# 当 `user` 被其他资源包含的时候仅返回 `id`, `name`, `status` 三个字段,其余的不返回
allowIncludeCols: ['id', 'name', 'status']
}
```

Expand Down
1 change: 1 addition & 0 deletions bin/openrest
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ params
.version(pkg.version)
.option('table-sync', '根据model定义同步表结构')
.option('init', '初始化一个新的应用')
.option('ls', '列出有那些可用的样板工程')
.parse(process.argv)

# 初始化一个应用
Expand Down
16 changes: 14 additions & 2 deletions bin/src/table-sync.coffee
Original file line number Diff line number Diff line change
@@ -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)
)

21 changes: 11 additions & 10 deletions lib/model.coffee
Original file line number Diff line number Diff line change
@@ -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 = {}
Expand Down Expand Up @@ -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) ->
Expand Down Expand Up @@ -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 = {}
Expand All @@ -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 上
Expand Down Expand Up @@ -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)

###
# 处理排序参数
Expand Down
5 changes: 5 additions & 0 deletions lib/page-params.coffee
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 5 additions & 8 deletions lib/stats.coffee
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
26 changes: 20 additions & 6 deletions test/stats.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

0 comments on commit 0232e6f

Please sign in to comment.