2013-06-25 15:43:15 +04:00
|
|
|
var GhostBookshelf,
|
|
|
|
Bookshelf = require('bookshelf'),
|
2013-07-08 15:39:11 +04:00
|
|
|
_ = require('underscore'),
|
2013-06-25 15:43:15 +04:00
|
|
|
config = require('../../../config');
|
|
|
|
|
|
|
|
// Initializes Bookshelf as its own instance, so we can modify the Models and not mess up
|
|
|
|
// others' if they're using the library outside of ghost.
|
2013-07-07 19:50:50 +04:00
|
|
|
GhostBookshelf = Bookshelf.Initialize('ghost', config.env[process.env.NODE_ENV || 'development'].database);
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
// The Base Model which other Ghost objects will inherit from,
|
|
|
|
// including some convenience functions as static properties on the model.
|
|
|
|
GhostBookshelf.Model = GhostBookshelf.Model.extend({
|
|
|
|
|
|
|
|
// Base prototype properties will go here
|
2013-07-08 15:39:11 +04:00
|
|
|
// Fix problems with dates
|
|
|
|
fixDates: function (attrs) {
|
|
|
|
_.each(attrs, function (value, key) {
|
|
|
|
if (key.substr(-3) === '_at' && value !== null) {
|
|
|
|
attrs[key] = new Date(attrs[key]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return attrs;
|
|
|
|
},
|
|
|
|
|
|
|
|
format: function (attrs) {
|
|
|
|
return this.fixDates(attrs);
|
|
|
|
},
|
|
|
|
|
|
|
|
toJSON: function (options) {
|
|
|
|
var attrs = this.fixDates(_.extend({}, this.attributes)),
|
|
|
|
relations = this.relations;
|
|
|
|
|
|
|
|
if (options && options.shallow) {
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
2013-08-22 21:53:46 +04:00
|
|
|
_.each(relations, function (relation, key) {
|
|
|
|
attrs[key] = relation.toJSON ? relation.toJSON() : relation;
|
2013-07-08 15:39:11 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
return attrs;
|
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
}, {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Naive find all
|
|
|
|
* @param options (optional)
|
|
|
|
*/
|
|
|
|
findAll: function (options) {
|
|
|
|
options = options || {};
|
|
|
|
return GhostBookshelf.Collection.forge([], {model: this}).fetch(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
browse: function () {
|
|
|
|
return this.findAll.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Naive find one where args match
|
|
|
|
* @param args
|
|
|
|
* @param options (optional)
|
|
|
|
*/
|
|
|
|
findOne: function (args, options) {
|
|
|
|
options = options || {};
|
|
|
|
return this.forge(args).fetch(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
read: function () {
|
|
|
|
return this.findOne.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Naive edit
|
|
|
|
* @param editedObj
|
|
|
|
* @param options (optional)
|
|
|
|
*/
|
|
|
|
edit: function (editedObj, options) {
|
|
|
|
options = options || {};
|
|
|
|
return this.forge({id: editedObj.id}).fetch(options).then(function (foundObj) {
|
2013-08-09 05:22:49 +04:00
|
|
|
return foundObj.save(editedObj);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function () {
|
|
|
|
return this.edit.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Naive create
|
|
|
|
* @param editedObj
|
|
|
|
* @param options (optional)
|
|
|
|
*/
|
|
|
|
add: function (newObj, options) {
|
|
|
|
options = options || {};
|
|
|
|
return this.forge(newObj).save(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
create: function () {
|
|
|
|
return this.add.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Naive destroy
|
|
|
|
* @param _identifier
|
|
|
|
* @param options (optional)
|
|
|
|
*/
|
|
|
|
destroy: function (_identifier, options) {
|
|
|
|
options = options || {};
|
|
|
|
return this.forge({id: _identifier}).destroy(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
'delete': function () {
|
|
|
|
return this.destroy.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-08-09 05:22:49 +04:00
|
|
|
module.exports = GhostBookshelf;
|