-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathmodels.js
62 lines (49 loc) · 1.11 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
50
51
52
53
54
55
56
57
58
59
60
61
62
var orm = require('orm');
var connectionString;
if (process.env.PG == 'true')
connectionString = 'postgres://cote:ohgath2ig8eoP8@postgres/cote';
else
connectionString = 'sqlite://db.sqlite';
var db = orm.connect(connectionString, function onConnect(err) {
if (err) {
console.log('Error', err);
process.exit(1);
}
});
db.settings.set('instance.cache', false);
var Product = db.define('product', {
name: String,
price: Number,
stock: Number
});
var Purchase = db.define('purchase', {});
var User = db.define('user', {
balance: { type: 'number', defaultValue: 30 }
});
Purchase.hasOne('product', Product, {
autoFetch: true
});
Purchase.hasOne('owner', User, {
autoFetch: true,
reverse: 'purchases'
});
function init(callback) {
console.log('Initializing db.');
db.sync(callback);
}
function drop(callback) {
if (process.env.DROP) {
console.log('Dropping db.');
db.drop(callback);
}
else
callback();
}
db.sync();
module.exports = {
Product: Product,
Purchase: Purchase,
User: User,
init: init,
drop: drop
};