-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.coffee
105 lines (88 loc) · 1.67 KB
/
db.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# db.coffee
Sequelize = require("sequelize")
restTable = null
init = ->
dbSql = new Sequelize 'database', 'username', 'password',
dialect: 'sqlite'
storage: "./rest.sqlite"
# storage: ':memory:'
logging: false
restTable = dbSql.define 'Rest',
id:
type: Sequelize.INTEGER
primaryKey: true
text: Sequelize.STRING(200)
priority: Sequelize.INTEGER
getRestTable = ->
return restTable
sendObjResponse = (rows, callback) ->
ar = []
for row in rows
ar.push row.dataValues
retobj =
success: true
total: ar.length
data: ar
if callback
callback(retobj)
return
list = (callback) ->
restTable.findAll
order: 'id'
.then (rows) ->
sendObjResponse rows, callback
return
return
get = (recid, callback) ->
restTable.findAll
where:
id: recid
.then (rows) ->
sendObjResponse rows, callback
return
return
insert = (insertObj, callback) ->
restTable.max 'id'
.then (retval) ->
nextid = retval + 1
insertObj.id = nextid
restTable.create insertObj
.complete (err, results) ->
retobj =
success: true
if err
retobj.success = false
console.log 'error: ', err
callback retobj
return
return
return
update = (updateObj, callback) ->
restTable.update updateObj,
where:
id: updateObj.id
.then ->
# always returns true
retobj =
success: true
callback retobj
return
return
destroy = (recid, callback) ->
restTable.destroy
where:
id: recid
.then ->
retobj =
success: true
# return true no matter what
callback retobj
return
return
exports.init = init
exports.list = list
exports.get = get
exports.destroy = destroy
exports.insert = insert
exports.update = update
exports.getRestTable = getRestTable