This repository has been archived by the owner on Nov 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
model.js
62 lines (57 loc) · 1.53 KB
/
model.js
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
const config = require('./config');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
mongoose.connect(config.mongodb);
const AppSchema = new Schema({
title : String,
userid: String,
date : Date
});
mongoose.model('App', AppSchema);
function eq(x, k) {
return function(_, y) {
k(x == y);
}
}
function neq(x, k) {
return function(_, y) {
k(x != y);
}
}
function success(k) {
return function(error, x) {
if(!error){ return k(x); }
else { throw error; }
}
}
const App = mongoose.model('App');
exports.App = {
create : function(title, userid , k) {
App.count({ title : title },
eq(0,
function(b) {
if(b){
const app = new App();
app.title = title;
app.userid = userid;
app.date = new Date();
app.save(k);
} else {
k("dup error")
}
}));
},
all : function(userid, k) {
App.find({ userid : userid }).sort({'title' : 'asc'}).exec(success(k));
},
get : function(id, k) {
App.findById(id, success(k));
},
update : function(id, obj, k) {
obj.date = new Date();
App.update({ _id : id }, obj, success(k));
},
remove : function(id, k) {
App.remove({ _id : id }, success(k));
}
}