mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
commit
f4368a2744
4 changed files with 182 additions and 18 deletions
|
@ -21,26 +21,31 @@
|
|||
/**
|
||||
* Naive find all
|
||||
* @param args (optional)
|
||||
* @param opts (optional)
|
||||
*/
|
||||
BookshelfBase.prototype.findAll = BookshelfBase.prototype.browse = function (args) {
|
||||
args = args || {};
|
||||
return this.collection.forge(args).fetch();
|
||||
BookshelfBase.prototype.findAll = BookshelfBase.prototype.browse = function (opts) {
|
||||
opts = opts || {};
|
||||
return this.collection.forge().fetch(opts);
|
||||
};
|
||||
|
||||
/**
|
||||
* Naive find one where args match
|
||||
* @param args
|
||||
* @param opts (optional)
|
||||
*/
|
||||
BookshelfBase.prototype.findOne = BookshelfBase.prototype.read = function (args) {
|
||||
return this.model.forge(args).fetch();
|
||||
BookshelfBase.prototype.findOne = BookshelfBase.prototype.read = function (args, opts) {
|
||||
opts = opts || {};
|
||||
return this.model.forge(args).fetch(opts);
|
||||
};
|
||||
|
||||
/**
|
||||
* Naive edit
|
||||
* @param editedObj
|
||||
* @param opts (optional)
|
||||
*/
|
||||
BookshelfBase.prototype.edit = BookshelfBase.prototype.update = function (editedObj) {
|
||||
return this.model.forge({id: editedObj.id}).fetch().then(function (foundObj) {
|
||||
BookshelfBase.prototype.edit = BookshelfBase.prototype.update = function (editedObj, opts) {
|
||||
opts = opts || {};
|
||||
return this.model.forge({id: editedObj.id}).fetch(opts).then(function (foundObj) {
|
||||
return foundObj.set(editedObj).save();
|
||||
});
|
||||
};
|
||||
|
@ -48,17 +53,21 @@
|
|||
/**
|
||||
* Naive add
|
||||
* @param newObj
|
||||
* @param opts (optional)
|
||||
*/
|
||||
BookshelfBase.prototype.add = BookshelfBase.prototype.create = function (newObj) {
|
||||
return this.model.forge(newObj).save();
|
||||
BookshelfBase.prototype.add = BookshelfBase.prototype.create = function (newObj, opts) {
|
||||
opts = opts || {};
|
||||
return this.model.forge(newObj).save(opts);
|
||||
};
|
||||
|
||||
/**
|
||||
* Naive destroy
|
||||
* @param _identifier
|
||||
* @param opts (optional)
|
||||
*/
|
||||
BookshelfBase.prototype.destroy = BookshelfBase.prototype['delete'] = function (_identifier) {
|
||||
return this.model.forge({id: _identifier}).destroy();
|
||||
BookshelfBase.prototype.destroy = BookshelfBase.prototype['delete'] = function (_identifier, opts) {
|
||||
opts = opts || {};
|
||||
return this.model.forge({id: _identifier}).destroy(opts);
|
||||
};
|
||||
|
||||
module.exports = BookshelfBase;
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
(function () {
|
||||
"use strict";
|
||||
|
||||
var util = require('util'),
|
||||
var _ = require('underscore'),
|
||||
util = require('util'),
|
||||
models = require('./models'),
|
||||
Bookshelf = require('bookshelf'),
|
||||
BaseProvider = require('./dataProvider.bookshelf.base'),
|
||||
PostsProvider;
|
||||
|
||||
|
@ -15,5 +17,75 @@
|
|||
|
||||
util.inherits(PostsProvider, BaseProvider);
|
||||
|
||||
/**
|
||||
* Find results by page - returns an object containing the
|
||||
* information about the request (page, limit), along with the
|
||||
* info needed for pagination (pages, total).
|
||||
*
|
||||
* {
|
||||
* posts: [
|
||||
* {...}, {...}, {...}
|
||||
* ],
|
||||
* page: __,
|
||||
* limit: __,
|
||||
* pages: __,
|
||||
* total: __
|
||||
* }
|
||||
*
|
||||
* @params opts
|
||||
*/
|
||||
PostsProvider.prototype.findPage = function (opts) {
|
||||
var postCollection;
|
||||
|
||||
// Allow findPage(n)
|
||||
if (!_.isObject(opts)) {
|
||||
opts = {page: opts};
|
||||
}
|
||||
|
||||
opts = _.defaults(opts || {}, {
|
||||
page: 1,
|
||||
limit: 15,
|
||||
where: null
|
||||
});
|
||||
postCollection = this.collection.forge();
|
||||
|
||||
// If there are where conditionals specified, add those
|
||||
// to the query.
|
||||
if (opts.where) {
|
||||
postCollection.query('where', opts.where);
|
||||
}
|
||||
|
||||
// Set the limit & offset for the query, fetching
|
||||
// with the opts (to specify any eager relations, etc.)
|
||||
// Omitting the `page`, `limit`, `where` just to be sure
|
||||
// aren't used for other purposes.
|
||||
return postCollection
|
||||
.query('limit', opts.limit)
|
||||
.query('offset', opts.limit * (opts.page - 1))
|
||||
.fetch(_.omit(opts, 'page', 'limit', 'where'))
|
||||
.then(function (collection) {
|
||||
var qb;
|
||||
|
||||
// After we're done, we need to figure out what
|
||||
// the limits are for the pagination values.
|
||||
qb = Bookshelf.Knex(_.result(collection, 'tableName'));
|
||||
|
||||
if (opts.where) {
|
||||
qb.where(opts.where);
|
||||
}
|
||||
|
||||
return qb.count(_.result(collection, 'idAttribute')).then(function (resp) {
|
||||
var totalPosts = resp[0].aggregate;
|
||||
return {
|
||||
posts: collection.toJSON(),
|
||||
page: opts.page,
|
||||
limit: opts.limit,
|
||||
pages: Math.ceil(totalPosts / opts.limit),
|
||||
total: totalPosts
|
||||
};
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = PostsProvider;
|
||||
}());
|
|
@ -6,7 +6,8 @@
|
|||
var _ = require("underscore"),
|
||||
should = require('should'),
|
||||
helpers = require('./helpers'),
|
||||
PostProvider = require('../../shared/models/dataProvider.bookshelf.posts');
|
||||
PostProvider = require('../../shared/models/dataProvider.bookshelf.posts'),
|
||||
Bookshelf = require('bookshelf');
|
||||
|
||||
describe('Bookshelf PostsProvider', function () {
|
||||
|
||||
|
@ -16,7 +17,7 @@
|
|||
helpers.resetData().then(function () {
|
||||
posts = new PostProvider();
|
||||
done();
|
||||
});
|
||||
}, done);
|
||||
});
|
||||
|
||||
it('can browse', function (done) {
|
||||
|
@ -121,5 +122,64 @@
|
|||
|
||||
}).then(null, done);
|
||||
});
|
||||
|
||||
it('can fetch a paginated set, with various options', function (done) {
|
||||
|
||||
helpers.insertMorePosts().then(function () {
|
||||
|
||||
return posts.findPage({page: 2});
|
||||
|
||||
}).then(function (paginationResult) {
|
||||
|
||||
paginationResult.page.should.equal(2);
|
||||
|
||||
paginationResult.limit.should.equal(15);
|
||||
|
||||
paginationResult.posts.length.should.equal(15);
|
||||
|
||||
paginationResult.pages.should.equal(4);
|
||||
|
||||
return posts.findPage({page: 5});
|
||||
|
||||
}).then(function (paginationResult) {
|
||||
|
||||
paginationResult.page.should.equal(5);
|
||||
|
||||
paginationResult.limit.should.equal(15);
|
||||
|
||||
paginationResult.posts.length.should.equal(0);
|
||||
|
||||
paginationResult.pages.should.equal(4);
|
||||
|
||||
return posts.findPage({limit: 30});
|
||||
|
||||
}).then(function (paginationResult) {
|
||||
|
||||
paginationResult.page.should.equal(1);
|
||||
|
||||
paginationResult.limit.should.equal(30);
|
||||
|
||||
paginationResult.posts.length.should.equal(30);
|
||||
|
||||
paginationResult.pages.should.equal(2);
|
||||
|
||||
return posts.findPage({limit: 10, page: 2, where: {language: 'fr'}});
|
||||
|
||||
}).then(function (paginationResult) {
|
||||
|
||||
paginationResult.page.should.equal(2);
|
||||
|
||||
paginationResult.limit.should.equal(10);
|
||||
|
||||
paginationResult.posts.length.should.equal(10);
|
||||
|
||||
paginationResult.pages.should.equal(3);
|
||||
|
||||
done();
|
||||
|
||||
}).then(null, done);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
}());
|
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue