-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.js
49 lines (45 loc) · 1.43 KB
/
models.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
var orm = require('orm');
var moment = require('moment');
module.exports = function (db, cb) {
var User = db.define("users", {
id : { type: "serial", key: true },
email : { type: "text" },
password: { type: "text" }
}, {
validations: {
email: [orm.enforce.unique("User with given email already exists!"),
orm.enforce.patterns.email("Invalid email")],
password: orm.enforce.notEmptyString("Password is required"),
// @TODO add "match passwords" validation
}
});
var Pad = db.define("pads", {
id : { type: "serial", key: true },
name : { type: "text" },
}, {
validations: {
name: orm.enforce.notEmptyString("Name is required"),
}
});
Pad.hasOne("user", User, { required: true, reverse: 'pads' });
var Note = db.define("notes", {
id : { type: "serial", key: true },
name : { type: "text" },
text : { type: "text" },
created_at : { type: "date", time: true },
updated_at : { type: "date", time: true }
}, {
methods: {
updatedAt: function () {
return moment(this.updated_at).fromNow();
}
},
validations: {
name: orm.enforce.notEmptyString("Name is required"),
text: orm.enforce.notEmptyString("Text is required"),
}
});
Note.hasOne("user", User, { required: true, reverse: 'notes' });
Note.hasOne("pad", Pad, { required: false, reverse: 'notes' });
return cb();
};