-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.coffee
49 lines (36 loc) · 1.38 KB
/
index.coffee
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
mongoose = require 'mongoose'
Method = require './lib/Method'
Resource = require './lib/Resource'
Get = require './lib/crud/Get'
Put = require './lib/crud/Put'
Del = require './lib/crud/Del'
List = require './lib/crud/List'
Post = require './lib/crud/Post'
#---------------------------------------------------------------------
modelNameToResourcePath = (modelName)->
'/' + modelName.toLowerCase()
createResource = (options)->
model = options.model
throw 'No model' unless model
# desc = schemaToDescription model.schema
path = options.path or modelNameToResourcePath model.modelName
hooks = options.hooks or {}
methodOptions =
upsert : options.upsert
mtimeField : options.mtimeField
ctimeField : options.ctimeField
plainOutput : options.plainOutput
listPath = path
itemPath = "#{path}/:_id"
resource = new Resource
resource.addMethod 'get' , new Get model, itemPath, methodOptions
resource.addMethod 'put' , new Put model, itemPath, methodOptions
resource.addMethod 'del' , new Del model, itemPath, methodOptions
resource.addMethod 'post', new Post model, listPath, methodOptions
resource.addMethod 'list', new List model, listPath, methodOptions
resource.use hooks
resource
#--------------------------------------------------
exports.createResource = createResource
exports.Method = Method
exports.Resource = Resource