-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (28 loc) · 907 Bytes
/
index.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
var assign = require('object-assign');
var factory = require('factory-girl');
var Adapter = factory.Adapter;
var ObjectionAdapter = function () { }
ObjectionAdapter.prototype = new Adapter();
ObjectionAdapter.prototype.save = function (doc, Model, cb) {
Model.query().insert(doc.toJSON())
.then(function (record) {
assign(doc, record);
cb(null, doc);
}).catch(cb);
};
ObjectionAdapter.prototype.destroy = function (doc, Model, cb) {
if (!doc[Model.idColumn]) return process.nextTick(cb);
Model.query().deleteById(doc[Model.idColumn]).nodeify(cb);
}
var adapter = new ObjectionAdapter();
module.exports = function (models) {
if (models && models.length) {
for (var i = 0; i < models.length; i++) {
factory.setAdapter(adapter, models[i]);
}
} else {
factory.setAdapter(adapter);
}
return adapter;
}
module.exports.ObjectionAdapter = ObjectionAdapter;