Solve issues to be able to use bookshelf correctly

This commit is contained in:
Aurelsicoko 2017-09-14 16:33:56 +02:00
parent d085dcf10e
commit 4e089f7909
9 changed files with 52 additions and 74 deletions

View File

@ -104,6 +104,10 @@ module.exports = function(strapi) {
_.forEach(models, (definition, model) => { _.forEach(models, (definition, model) => {
globalName = _.upperFirst(_.camelCase(definition.globalId)); globalName = _.upperFirst(_.camelCase(definition.globalId));
_.defaults(definition, {
primaryKey: 'id'
});
// Make sure the model has a table name. // Make sure the model has a table name.
// If not, use the model name. // If not, use the model name.
if (_.isEmpty(definition.collectionName)) { if (_.isEmpty(definition.collectionName)) {

View File

@ -19,7 +19,6 @@
"bookshelf": "^0.10.3", "bookshelf": "^0.10.3",
"lodash": "^4.17.4", "lodash": "^4.17.4",
"pluralize": "^6.0.0", "pluralize": "^6.0.0",
"strapi-bookshelf": "3.0.0-alpha.5.5",
"strapi-utils": "3.0.0-alpha.5.5" "strapi-utils": "3.0.0-alpha.5.5"
}, },
"strapi": { "strapi": {

View File

@ -55,28 +55,26 @@ function formatQueryParams(params) {
* *
* @return {object} The response data * @return {object} The response data
*/ */
export default function request(url, options) { export default function request(url, options = {}) {
const optionsObj = options || {};
// Set headers // Set headers
optionsObj.headers = { options.headers = {
'Content-Type': 'text/plain', 'Content-Type': 'application/json',
}; };
// Add parameters to url // Add parameters to url
let urlFormatted = _.startsWith(url, '/') url = _.startsWith(url, '/')
? `${Strapi.apiUrl}${url}` ? `${Strapi.apiUrl}${url}`
: url; : url;
if (optionsObj && optionsObj.params) { if (options && options.params) {
const params = formatQueryParams(optionsObj.params); const params = formatQueryParams(options.params);
urlFormatted = `${url}?${params}`; url = `${url}?${params}`;
} }
// Stringify body object // Stringify body object
if (optionsObj && optionsObj.body) { if (options && options.body) {
optionsObj.body = JSON.stringify(optionsObj.body); options.body = JSON.stringify(options.body);
} }
return fetch(urlFormatted, optionsObj).then(checkStatus).then(parseJSON); return fetch(url, options).then(checkStatus).then(parseJSON);
} }

View File

@ -93,7 +93,7 @@ module.exports = strapi => {
password: _.get(connection.settings, 'password'), password: _.get(connection.settings, 'password'),
database: _.get(connection.settings, 'database'), database: _.get(connection.settings, 'database'),
charset: _.get(connection.settings, 'charset'), charset: _.get(connection.settings, 'charset'),
schema: _.get(connection.settings, 'schema'), schema: _.get(connection.settings, 'schema') || 'public',
port: _.get(connection.settings, 'port'), port: _.get(connection.settings, 'port'),
}, },
debug: _.get(connection.options, 'debug') || false, debug: _.get(connection.options, 'debug') || false,

View File

@ -82,7 +82,7 @@ export class List extends React.Component {
// Set current model name // Set current model name
this.props.setCurrentModelName(slug.toLowerCase()); this.props.setCurrentModelName(slug.toLowerCase());
this.props.changeSort(this.props.models[slug.toLowerCase()].primaryKey || 'desc'); this.props.changeSort(this.props.models[slug.toLowerCase()].primaryKey || 'id');
// Load records // Load records
this.props.loadRecords(); this.props.loadRecords();

View File

@ -1,70 +1,47 @@
module.exports = { module.exports = {
find: async function (params) { find: async function (params) {
const entries = await this return await this
.forge() .forge()
.query((qb) => {
qb.limit(Number(params.limit));
qb.orderBy(params.sort);
qb.offset(Number(params.skip));
if (params.query && params.queryAttribute) {
qb.whereRaw(`LOWER(${params.queryAttribute}) LIKE '%' || LOWER(?) || '%'`, params.query);
}
})
.fetchAll({ .fetchAll({
withRelated: _.map(params.model.associations, 'alias') withRelated: this.associations.map(x => x.alias).join(' ')
}); });
return entries;
}, },
count: async function (params) { count: async function (params) {
const count = await this return await this
.forge() .forge()
.count(); .count();
return Number(count);
}, },
findOne: async (params) => { findOne: async function (params) {
const where = {}; return await this
where[params.primaryKey] = params.id; .forge({
[this.primaryKey]: params[this.primaryKey]
const entry = await params.model })
.forge(where)
.fetch(); .fetch();
return entry;
}, },
create: async (params) => { create: async function (params) {
const entry = await params.model return await this
.forge() .forge()
.save(params.values); .save(params.values);
return entry;
}, },
update: async (params) => { update: async function (params) {
const where = {}; return await this
where[params.primaryKey] = params.id; .forge({
[this.primaryKey]: params[this.primaryKey]
const entry = await params.model })
.forge(where) .save(params.values, {
.save(params.values, {patch: true}); patch: true
});
return entry;
}, },
delete: async (params) => { delete: async function (params) {
const where = {}; return await params.model
where[params.primaryKey] = params.id; .forge({
[this.primaryKey]: params[this.primaryKey]
const entry = await params.model })
.forge(where)
.destroy(); .destroy();
return entry;
} }
}; };

View File

@ -45,7 +45,7 @@ module.exports = {
const count = await strapi.query(ctx.params.model).count(); const count = await strapi.query(ctx.params.model).count();
ctx.body = { ctx.body = {
count, count: _.isNumber(count) ? count : _.toNumber(count)
}; };
}, },

View File

@ -47,7 +47,7 @@ module.exports = function() {
} }
const module = this.hook[hook].load; const module = this.hook[hook].load;
let dependencies = this.hook[hook].dependencies || []; let dependencies = this.hook[hook].dependencies.map(x => x.replace('strapi-', '')) || [];
// Apply default configurations to middleware. // Apply default configurations to middleware.
if (isUndefined(get(this.config.hook, `settings.${hook}`))) { if (isUndefined(get(this.config.hook, `settings.${hook}`))) {