Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
// # Base Model
|
|
|
|
// This is the model from which all other Ghost models extend. The model is based on Bookshelf.Model, and provides
|
|
|
|
// several basic behaviours such as UUIDs, as well as a set of Data methods for accessing information from the database.
|
|
|
|
//
|
|
|
|
// The models are internal to Ghost, only the API and some internal functions such as migration and import/export
|
|
|
|
// accesses the models directly. All other parts of Ghost, including the blog frontend, admin UI, and apps are only
|
|
|
|
// allowed to access data via the API.
|
2014-09-07 00:16:14 +04:00
|
|
|
var _ = require('lodash'),
|
|
|
|
bookshelf = require('bookshelf'),
|
2014-04-27 20:58:34 +04:00
|
|
|
config = require('../config'),
|
2014-09-07 00:16:14 +04:00
|
|
|
errors = require('../errors'),
|
|
|
|
filters = require('../filters'),
|
|
|
|
moment = require('moment'),
|
|
|
|
Promise = require('bluebird'),
|
2014-04-27 20:58:34 +04:00
|
|
|
sanitize = require('validator').sanitize,
|
|
|
|
schema = require('../data/schema'),
|
2014-09-07 00:16:14 +04:00
|
|
|
utils = require('../utils'),
|
|
|
|
uuid = require('node-uuid'),
|
2014-04-27 20:58:34 +04:00
|
|
|
validation = require('../data/validation'),
|
2014-02-19 17:57:26 +04:00
|
|
|
|
|
|
|
ghostBookshelf;
|
2013-06-25 15:43:15 +04:00
|
|
|
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
// ### ghostBookshelf
|
|
|
|
// Initializes a new Bookshelf instance called ghostBookshelf, for reference elsewhere in Ghost.
|
2014-07-17 18:33:21 +04:00
|
|
|
ghostBookshelf = bookshelf(config.database.knex);
|
2014-08-14 01:58:12 +04:00
|
|
|
|
2014-07-13 15:17:18 +04:00
|
|
|
// Load the registry plugin, which helps us avoid circular dependencies
|
|
|
|
ghostBookshelf.plugin('registry');
|
2013-06-25 15:43:15 +04:00
|
|
|
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
// ### ghostBookshelf.Model
|
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.
|
2013-09-23 02:20:08 +04:00
|
|
|
ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2013-09-14 23:01:46 +04:00
|
|
|
hasTimestamps: true,
|
|
|
|
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
// Get permitted attributes from server/data/schema.js, which is where the DB schema is defined
|
2014-02-19 17:57:26 +04:00
|
|
|
permittedAttributes: function () {
|
|
|
|
return _.keys(schema.tables[this.tableName]);
|
|
|
|
},
|
|
|
|
|
2013-09-14 23:01:46 +04:00
|
|
|
defaults: function () {
|
|
|
|
return {
|
|
|
|
uuid: uuid.v4()
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function () {
|
2014-04-27 20:58:34 +04:00
|
|
|
var self = this,
|
|
|
|
options = arguments[1] || {};
|
|
|
|
|
|
|
|
// make options include available for toJSON()
|
|
|
|
if (options.include) {
|
|
|
|
this.include = _.clone(options.include);
|
|
|
|
}
|
|
|
|
|
2013-09-14 23:01:46 +04:00
|
|
|
this.on('creating', this.creating, this);
|
2014-02-19 17:57:26 +04:00
|
|
|
this.on('saving', function (model, attributes, options) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.resolve(self.saving(model, attributes, options)).then(function () {
|
2014-02-19 17:57:26 +04:00
|
|
|
return self.validate(model, attributes, options);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
validate: function () {
|
2014-05-05 17:51:21 +04:00
|
|
|
return validation.validateSchema(this.tableName, this.toJSON());
|
2013-09-14 23:01:46 +04:00
|
|
|
},
|
|
|
|
|
2014-04-03 17:03:09 +04:00
|
|
|
creating: function (newObj, attr, options) {
|
2013-09-14 23:01:46 +04:00
|
|
|
if (!this.get('created_by')) {
|
2014-07-15 15:03:12 +04:00
|
|
|
this.set('created_by', this.contextUser(options));
|
2013-09-14 23:01:46 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-04-03 17:03:09 +04:00
|
|
|
saving: function (newObj, attr, options) {
|
|
|
|
// Remove any properties which don't belong on the model
|
2014-02-19 17:57:26 +04:00
|
|
|
this.attributes = this.pick(this.permittedAttributes());
|
2014-04-22 05:04:30 +04:00
|
|
|
// Store the previous attributes so we can tell what was updated later
|
|
|
|
this._updatedAttributes = newObj.previousAttributes();
|
|
|
|
|
2014-07-15 15:03:12 +04:00
|
|
|
this.set('updated_by', this.contextUser(options));
|
2013-09-14 23:01:46 +04:00
|
|
|
},
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// Base prototype properties will go here
|
2013-07-08 15:39:11 +04:00
|
|
|
// Fix problems with dates
|
|
|
|
fixDates: function (attrs) {
|
2014-07-05 18:57:56 +04:00
|
|
|
var self = this;
|
|
|
|
|
2013-07-08 15:39:11 +04:00
|
|
|
_.each(attrs, function (value, key) {
|
2014-07-24 12:17:10 +04:00
|
|
|
if (value !== null
|
|
|
|
&& schema.tables[self.tableName].hasOwnProperty(key)
|
|
|
|
&& schema.tables[self.tableName][key].type === 'dateTime') {
|
2014-07-05 18:57:56 +04:00
|
|
|
// convert dateTime value into a native javascript Date object
|
|
|
|
attrs[key] = moment(value).toDate();
|
2013-07-08 15:39:11 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return attrs;
|
|
|
|
},
|
|
|
|
|
2014-04-25 11:55:53 +04:00
|
|
|
// Convert integers to real booleans
|
2014-01-05 02:16:29 +04:00
|
|
|
fixBools: function (attrs) {
|
2014-04-25 11:55:53 +04:00
|
|
|
var self = this;
|
2014-01-05 02:16:29 +04:00
|
|
|
_.each(attrs, function (value, key) {
|
2014-07-24 12:17:10 +04:00
|
|
|
if (schema.tables[self.tableName].hasOwnProperty(key)
|
|
|
|
&& schema.tables[self.tableName][key].type === 'bool') {
|
2014-04-25 11:55:53 +04:00
|
|
|
attrs[key] = value ? true : false;
|
2014-01-05 02:16:29 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return attrs;
|
|
|
|
},
|
|
|
|
|
2014-07-15 15:03:12 +04:00
|
|
|
// Get the user from the options object
|
|
|
|
contextUser: function (options) {
|
|
|
|
// Default to context user
|
|
|
|
if (options.context && options.context.user) {
|
|
|
|
return options.context.user;
|
|
|
|
// Other wise use the internal override
|
|
|
|
} else if (options.context && options.context.internal) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
errors.logAndThrowError(new Error('missing context'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-04-25 11:55:53 +04:00
|
|
|
// format date before writing to DB, bools work
|
2013-07-08 15:39:11 +04:00
|
|
|
format: function (attrs) {
|
2014-04-25 11:55:53 +04:00
|
|
|
return this.fixDates(attrs);
|
|
|
|
},
|
|
|
|
|
|
|
|
// format data and bool when fetching from DB
|
|
|
|
parse: function (attrs) {
|
2014-01-05 02:16:29 +04:00
|
|
|
return this.fixBools(this.fixDates(attrs));
|
2013-07-08 15:39:11 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
toJSON: function (options) {
|
2014-04-25 11:55:53 +04:00
|
|
|
var attrs = _.extend({}, this.attributes),
|
2014-04-27 20:58:34 +04:00
|
|
|
self = this;
|
2014-07-08 20:00:59 +04:00
|
|
|
options = options || {};
|
2015-04-22 22:20:27 +03:00
|
|
|
options = _.pick(options, ['shallow', 'baseKey', 'include', 'context']);
|
2013-07-08 15:39:11 +04:00
|
|
|
|
|
|
|
if (options && options.shallow) {
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
2014-07-08 20:00:59 +04:00
|
|
|
if (options && options.include) {
|
|
|
|
this.include = _.union(this.include, options.include);
|
|
|
|
}
|
|
|
|
|
2014-04-27 20:58:34 +04:00
|
|
|
_.each(this.relations, function (relation, key) {
|
2013-09-24 14:46:30 +04:00
|
|
|
if (key.substring(0, 7) !== '_pivot_') {
|
2014-04-27 20:58:34 +04:00
|
|
|
// if include is set, expand to full object
|
2015-04-22 22:20:27 +03:00
|
|
|
var fullKey = _.isEmpty(options.baseKey) ? key : options.baseKey + '.' + key;
|
2014-07-08 20:00:59 +04:00
|
|
|
if (_.contains(self.include, fullKey)) {
|
2015-04-22 22:20:27 +03:00
|
|
|
attrs[key] = relation.toJSON(_.extend({}, options, {baseKey: fullKey, include: self.include}));
|
2014-04-27 20:58:34 +04:00
|
|
|
}
|
2013-08-21 16:55:58 +04:00
|
|
|
}
|
2013-07-08 15:39:11 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
return attrs;
|
2013-09-14 23:01:46 +04:00
|
|
|
},
|
|
|
|
|
2013-10-09 22:11:29 +04:00
|
|
|
sanitize: function (attr) {
|
|
|
|
return sanitize(this.get(attr)).xss();
|
2014-04-22 05:04:30 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Get attributes that have been updated (values before a .save() call)
|
|
|
|
updatedAttributes: function () {
|
|
|
|
return this._updatedAttributes || {};
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get a specific updated attribute value
|
|
|
|
updated: function (attr) {
|
|
|
|
return this.updatedAttributes()[attr];
|
2013-07-08 15:39:11 +04:00
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
}, {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// ## Data Utility Functions
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
|
2014-05-06 05:45:08 +04:00
|
|
|
/**
|
|
|
|
* Returns an array of keys permitted in every method's `options` hash.
|
|
|
|
* Can be overridden and added to by a model's `permittedOptions` method.
|
|
|
|
* @return {Array} Keys allowed in the `options` hash of every model's method.
|
|
|
|
*/
|
|
|
|
permittedOptions: function () {
|
|
|
|
// terms to whitelist for all methods.
|
2014-07-15 15:03:12 +04:00
|
|
|
return ['context', 'include', 'transacting'];
|
2014-05-06 05:45:08 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters potentially unsafe model attributes, so you can pass them to Bookshelf / Knex.
|
|
|
|
* @param {Object} data Has keys representing the model's attributes/fields in the database.
|
|
|
|
* @return {Object} The filtered results of the passed in data, containing only what's allowed in the schema.
|
|
|
|
*/
|
|
|
|
filterData: function (data) {
|
|
|
|
var permittedAttributes = this.prototype.permittedAttributes(),
|
|
|
|
filteredData = _.pick(data, permittedAttributes);
|
|
|
|
|
|
|
|
return filteredData;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters potentially unsafe `options` in a model method's arguments, so you can pass them to Bookshelf / Knex.
|
|
|
|
* @param {Object} options Represents options to filter in order to be passed to the Bookshelf query.
|
|
|
|
* @param {String} methodName The name of the method to check valid options for.
|
|
|
|
* @return {Object} The filtered results of `options`.
|
|
|
|
*/
|
|
|
|
filterOptions: function (options, methodName) {
|
|
|
|
var permittedOptions = this.permittedOptions(methodName),
|
|
|
|
filteredOptions = _.pick(options, permittedOptions);
|
|
|
|
|
|
|
|
return filteredOptions;
|
|
|
|
},
|
|
|
|
|
2015-05-01 00:14:19 +03:00
|
|
|
// ## Model Data Functions
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
/**
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
* ### Find All
|
|
|
|
* Naive find all fetches all the data for a particular model
|
2014-04-21 22:04:20 +04:00
|
|
|
* @param {Object} options (optional)
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
* @return {Promise(ghostBookshelf.Collection)} Collection of all Models
|
2013-06-25 15:43:15 +04:00
|
|
|
*/
|
|
|
|
findAll: function (options) {
|
2014-05-06 05:45:08 +04:00
|
|
|
options = this.filterOptions(options, 'findAll');
|
2014-04-27 20:58:34 +04:00
|
|
|
return ghostBookshelf.Collection.forge([], {model: this}).fetch(options).then(function (result) {
|
|
|
|
if (options.include) {
|
|
|
|
_.each(result.models, function (item) {
|
|
|
|
item.include = options.include;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
* ### Find One
|
|
|
|
* Naive find one where data determines what to match on
|
|
|
|
* @param {Object} data
|
2014-04-21 22:04:20 +04:00
|
|
|
* @param {Object} options (optional)
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
* @return {Promise(ghostBookshelf.Model)} Single Model
|
2013-06-25 15:43:15 +04:00
|
|
|
*/
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
findOne: function (data, options) {
|
2014-05-06 05:45:08 +04:00
|
|
|
data = this.filterData(data);
|
|
|
|
options = this.filterOptions(options, 'findOne');
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// We pass include to forge so that toJSON has access
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
return this.forge(data, {include: options.include}).fetch(options);
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
* ### Edit
|
2013-06-25 15:43:15 +04:00
|
|
|
* Naive edit
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
* @param {Object} data
|
2014-04-21 22:04:20 +04:00
|
|
|
* @param {Object} options (optional)
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
* @return {Promise(ghostBookshelf.Model)} Edited Model
|
2013-06-25 15:43:15 +04:00
|
|
|
*/
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
edit: function (data, options) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
var id = options.id;
|
2014-05-06 05:45:08 +04:00
|
|
|
data = this.filterData(data);
|
|
|
|
options = this.filterOptions(options, 'edit');
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
|
|
|
return this.forge({id: id}).fetch(options).then(function (object) {
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
if (object) {
|
|
|
|
return object.save(data, options);
|
2014-04-16 14:09:03 +04:00
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
* ### Add
|
|
|
|
* Naive add
|
|
|
|
* @param {Object} data
|
2014-04-21 22:04:20 +04:00
|
|
|
* @param {Object} options (optional)
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
* @return {Promise(ghostBookshelf.Model)} Newly Added Model
|
2013-06-25 15:43:15 +04:00
|
|
|
*/
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
add: function (data, options) {
|
2014-05-06 05:45:08 +04:00
|
|
|
data = this.filterData(data);
|
|
|
|
options = this.filterOptions(options, 'add');
|
2014-09-19 20:17:58 +04:00
|
|
|
var model = this.forge(data);
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// We allow you to disable timestamps when importing posts so that the new posts `updated_at` value is the same
|
|
|
|
// as the import json blob. More details refer to https://github.com/TryGhost/Ghost/issues/1696
|
2013-12-26 07:48:16 +04:00
|
|
|
if (options.importing) {
|
2014-09-19 20:17:58 +04:00
|
|
|
model.hasTimestamps = false;
|
2013-12-26 07:48:16 +04:00
|
|
|
}
|
2014-09-19 20:17:58 +04:00
|
|
|
return model.save(null, options);
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
* ### Destroy
|
2013-06-25 15:43:15 +04:00
|
|
|
* Naive destroy
|
2014-04-21 22:04:20 +04:00
|
|
|
* @param {Object} options (optional)
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
* @return {Promise(ghostBookshelf.Model)} Empty Model
|
2013-06-25 15:43:15 +04:00
|
|
|
*/
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
destroy: function (options) {
|
|
|
|
var id = options.id;
|
2014-05-06 05:45:08 +04:00
|
|
|
options = this.filterOptions(options, 'destroy');
|
2015-03-24 23:23:23 +03:00
|
|
|
|
|
|
|
// Fetch the object before destroying it, so that the changed data is available to events
|
|
|
|
return this.forge({id: id}).fetch(options).then(function (obj) {
|
|
|
|
return obj.destroy(options);
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
/**
|
|
|
|
* ### Generate Slug
|
|
|
|
* Create a string to act as the permalink for an object.
|
|
|
|
* @param {ghostBookshelf.Model} Model Model type to generate a slug for
|
|
|
|
* @param {String} base The string for which to generate a slug, usually a title or name
|
|
|
|
* @param {Object} options Options to pass to findOne
|
|
|
|
* @return {Promise(String)} Resolves to a unique slug string
|
|
|
|
*/
|
|
|
|
generateSlug: function (Model, base, options) {
|
2013-12-20 17:36:00 +04:00
|
|
|
var slug,
|
|
|
|
slugTryCount = 1,
|
2014-03-23 22:52:25 +04:00
|
|
|
baseName = Model.prototype.tableName.replace(/s$/, ''),
|
2014-06-04 09:47:16 +04:00
|
|
|
// Look for a matching slug, append an incrementing number if so
|
2014-09-30 02:45:58 +04:00
|
|
|
checkIfSlugExists, longSlug;
|
2013-12-20 17:36:00 +04:00
|
|
|
|
2014-03-23 22:52:25 +04:00
|
|
|
checkIfSlugExists = function (slugToFind) {
|
|
|
|
var args = {slug: slugToFind};
|
2014-09-10 08:06:24 +04:00
|
|
|
// status is needed for posts
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
if (options && options.status) {
|
|
|
|
args.status = options.status;
|
2014-03-23 22:52:25 +04:00
|
|
|
}
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
return Model.findOne(args, options).then(function (found) {
|
2014-03-23 22:52:25 +04:00
|
|
|
var trimSpace;
|
2013-12-20 17:36:00 +04:00
|
|
|
|
2014-03-23 22:52:25 +04:00
|
|
|
if (!found) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return slugToFind;
|
2014-03-23 22:52:25 +04:00
|
|
|
}
|
2013-12-20 17:36:00 +04:00
|
|
|
|
2014-03-23 22:52:25 +04:00
|
|
|
slugTryCount += 1;
|
2013-12-20 17:36:00 +04:00
|
|
|
|
2014-09-30 02:45:58 +04:00
|
|
|
// If we shortened, go back to the full version and try again
|
|
|
|
if (slugTryCount === 2 && longSlug) {
|
|
|
|
slugToFind = longSlug;
|
|
|
|
longSlug = null;
|
|
|
|
slugTryCount = 1;
|
|
|
|
return checkIfSlugExists(slugToFind);
|
|
|
|
}
|
|
|
|
|
2014-03-23 22:52:25 +04:00
|
|
|
// If this is the first time through, add the hyphen
|
|
|
|
if (slugTryCount === 2) {
|
|
|
|
slugToFind += '-';
|
|
|
|
} else {
|
|
|
|
// Otherwise, trim the number off the end
|
|
|
|
trimSpace = -(String(slugTryCount - 1).length);
|
|
|
|
slugToFind = slugToFind.slice(0, trimSpace);
|
|
|
|
}
|
2013-12-20 17:36:00 +04:00
|
|
|
|
2014-03-23 22:52:25 +04:00
|
|
|
slugToFind += slugTryCount;
|
|
|
|
|
|
|
|
return checkIfSlugExists(slugToFind);
|
|
|
|
});
|
|
|
|
};
|
2013-12-20 17:36:00 +04:00
|
|
|
|
2014-07-19 16:20:16 +04:00
|
|
|
slug = utils.safeString(base);
|
2013-12-20 17:36:00 +04:00
|
|
|
|
|
|
|
// Remove trailing hyphen
|
|
|
|
slug = slug.charAt(slug.length - 1) === '-' ? slug.substr(0, slug.length - 1) : slug;
|
2014-01-26 08:32:08 +04:00
|
|
|
|
2014-09-30 02:45:58 +04:00
|
|
|
// If it's a user, let's try to cut it down (unless this is a human request)
|
|
|
|
if (baseName === 'user' && options && options.shortSlug && slugTryCount === 1 && slug !== 'ghost-owner') {
|
|
|
|
longSlug = slug;
|
|
|
|
slug = (slug.indexOf('-') > -1) ? slug.substr(0, slug.indexOf('-')) : slug;
|
|
|
|
}
|
|
|
|
|
2013-12-20 17:36:00 +04:00
|
|
|
// Check the filtered slug doesn't match any of the reserved keywords
|
2014-09-07 00:16:14 +04:00
|
|
|
return filters.doFilter('slug.reservedSlugs', config.slugs.reserved).then(function (slugList) {
|
|
|
|
// Some keywords cannot be changed
|
|
|
|
slugList = _.union(slugList, config.slugs.protected);
|
|
|
|
|
|
|
|
return _.contains(slugList, slug) ? slug + '-' + baseName : slug;
|
|
|
|
}).then(function (slug) {
|
|
|
|
// if slug is empty after trimming use the model name
|
|
|
|
if (!slug) {
|
|
|
|
slug = baseName;
|
|
|
|
}
|
|
|
|
// Test for duplicate slugs.
|
|
|
|
return checkIfSlugExists(slug);
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
// Export ghostBookshelf for use elsewhere
|
2013-09-23 02:20:08 +04:00
|
|
|
module.exports = ghostBookshelf;
|