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) => {
globalName = _.upperFirst(_.camelCase(definition.globalId));
_.defaults(definition, {
primaryKey: 'id'
});
// Make sure the model has a table name.
// If not, use the model name.
if (_.isEmpty(definition.collectionName)) {

View File

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

View File

@ -55,28 +55,26 @@ function formatQueryParams(params) {
*
* @return {object} The response data
*/
export default function request(url, options) {
const optionsObj = options || {};
export default function request(url, options = {}) {
// Set headers
options.headers = {
'Content-Type': 'application/json',
};
// Set headers
optionsObj.headers = {
'Content-Type': 'text/plain',
};
// Add parameters to url
url = _.startsWith(url, '/')
? `${Strapi.apiUrl}${url}`
: url;
// Add parameters to url
let urlFormatted = _.startsWith(url, '/')
? `${Strapi.apiUrl}${url}`
: url;
if (options && options.params) {
const params = formatQueryParams(options.params);
url = `${url}?${params}`;
}
if (optionsObj && optionsObj.params) {
const params = formatQueryParams(optionsObj.params);
urlFormatted = `${url}?${params}`;
}
// Stringify body object
if (options && options.body) {
options.body = JSON.stringify(options.body);
}
// Stringify body object
if (optionsObj && optionsObj.body) {
optionsObj.body = JSON.stringify(optionsObj.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'),
database: _.get(connection.settings, 'database'),
charset: _.get(connection.settings, 'charset'),
schema: _.get(connection.settings, 'schema'),
schema: _.get(connection.settings, 'schema') || 'public',
port: _.get(connection.settings, 'port'),
},
debug: _.get(connection.options, 'debug') || false,

View File

@ -82,7 +82,7 @@ export class List extends React.Component {
// Set current model name
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
this.props.loadRecords();

View File

@ -1,70 +1,47 @@
module.exports = {
find: async function (params) {
const entries = await this
return await this
.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({
withRelated: _.map(params.model.associations, 'alias')
withRelated: this.associations.map(x => x.alias).join(' ')
});
return entries;
},
count: async function (params) {
const count = await this
return await this
.forge()
.count();
return Number(count);
},
findOne: async (params) => {
const where = {};
where[params.primaryKey] = params.id;
const entry = await params.model
.forge(where)
findOne: async function (params) {
return await this
.forge({
[this.primaryKey]: params[this.primaryKey]
})
.fetch();
return entry;
},
create: async (params) => {
const entry = await params.model
create: async function (params) {
return await this
.forge()
.save(params.values);
return entry;
},
update: async (params) => {
const where = {};
where[params.primaryKey] = params.id;
const entry = await params.model
.forge(where)
.save(params.values, {patch: true});
return entry;
update: async function (params) {
return await this
.forge({
[this.primaryKey]: params[this.primaryKey]
})
.save(params.values, {
patch: true
});
},
delete: async (params) => {
const where = {};
where[params.primaryKey] = params.id;
const entry = await params.model
.forge(where)
delete: async function (params) {
return await params.model
.forge({
[this.primaryKey]: params[this.primaryKey]
})
.destroy();
return entry;
}
};

View File

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

View File

@ -47,7 +47,7 @@ module.exports = function() {
}
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.
if (isUndefined(get(this.config.hook, `settings.${hook}`))) {