all files / restify-curd/coffee/ index.coffee

97.87% Statements 46/47
75% Branches 6/8
100% Functions 2/2
100% Lines 33/33
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49               21×                  
mongoose = require 'mongoose'
async = require 'async'
 
module.exports = (server,Model,options={})->
    Model.schema.add createAt:Number
    Model.schema.add random:Number
    methods = ['list','post','get','put','patch','del','delAll']
    opts = 
        path:Model.modelName
        childs:[]
    opts[k]=v for k,v of options
 
    addRoute = (config,ids=[])->
        copyIds = (k for k in ids)
        copyIds.push config.path
        len = copyIds.length
        basePathID = basePath = ''
        for id,k in copyIds
            basePath =  "#{basePathID}/#{id}" if k is len-1
            basePathID+="/#{id}/:#{id}id"            
 
 
        route = require('./route') Model,copyIds
        # 初始化各方法初始值,默认均为true
        config[k]=true for k in methods when typeof config[k] is 'undefined'
 
 
        config.list and server.get basePath,route.doGetList
        config.post and server.post basePath,route.doPost
        config.get and server.get basePathID,route.doGet
        config.put and server.put basePathID,route.doEdit
        config.patch and server.patch basePathID,route.doEdit
        config.del and server.del basePathID,route.doDelete
        config.delAll and server.del basePath,route.doDeleteAll
 
        # 处理内嵌文档
        childs = config.childs
        return if not childs
        Echilds = [childs] if not Array.isArray(childs)
        Ireturn if childs.length<1
        for child in childs
            addRoute child,copyIds
 
    addRoute opts,[]
 
    {
        Model:Model
    }