-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.js
71 lines (52 loc) · 1.51 KB
/
bootstrap.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
63
64
65
66
67
68
69
var when = require('when'),
sequence = require('when/sequence'),
database = require('./server/database'),
bootstrapData = require('./server/bootstrap_data');
function dropModel(model) {
var deferred = when.defer();
model.drop(function(err) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(true);
}
});
return deferred.promise;
}
function dropModels(db) {
var tasks = Object.keys(db.models).map(function(key) {
return dropModel.bind(null, db.models[key]);
});
return sequence(tasks);
}
function bootstrapModel(model) {
var deferred = when.defer();
model.sync(function(err) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(true);
}
});
return deferred.promise;
}
function bootstrapModels(db) {
var tasks = Object.keys(db.models).map(function(key) {
return bootstrapModel.bind(null, db.models[key]);
});
return sequence(tasks);
}
database.then(function(db) {
console.log('Dropping models...');
dropModels(db).then(function() {
console.log('Successfully dropped models.');
console.log('Bootstrapping models...');
bootstrapModels(db).then(function() {
console.log('Successfully bootstrapped models.');
}, function(err) {
console.log('Failed to bootsrap models:', err);
});
}, function(err) {
console.log('Failed to drop models:', err);
});
});