module.exports = bookshelf => {
const proto = bookshelf.Model.prototype;
bookshelf.Model = bookshelf.Model.extend({
returningProperties: '*',
constructor: function() {
proto.constructor.apply(this, arguments);
// Workaround (see https://github.com/tgriesser/bookshelf/issues/507#issuecomment-99634467)
this.on('saving', (model, attrs, options) => {
if (options.method === 'insert') {
const returningProperties = this.returningProperties;
Object.defineProperty(options.query._single, 'returning', {
get() { return returningProperties; },
set() { return returningProperties; },
configurable: true,
enumerable: true,
writeable: true,
});
} else {
options.query.returning.call(options.query, this.returningProperties);
}
});
// Workaround (see https://github.com/tgriesser/bookshelf/issues/507#issuecomment-99634467)
this.on('saved', (model, attrs, options) => {
if (options.method === 'insert') {
model.set(model.parse(model.id));
} else {
const id = model.get(model.idAttribute);
Eif (id) {
const attr = attrs.find(attr => attr.id === id);
Eif (attr) {
model.set(model.parse(attr));
}
}
}
});
}
});
};
|