-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-duplicate-indices.js
79 lines (59 loc) · 1.71 KB
/
test-duplicate-indices.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
70
71
72
73
74
75
76
77
78
79
var orm = require('orm'),
when = require('when'),
sequence = require('when/sequence'),
db = orm.connect('sqlite://test.db?debug=true');
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() {
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() {
var tasks = Object.keys(db.models).map(function(key) {
return bootstrapModel.bind(null, db.models[key]);
});
return sequence(tasks);
}
db.on('connect', function(err) {
var A,
B,
C;
A = db.define('a', { name: String });
B = db.define('b', { name: String });
C = db.define('c', { name: String });
A.hasMany('bees', B, {}, { reverse: 'eighs' });
A.hasMany('cees', C, {}, { reverse: 'eighs' });
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);
});
});