diff --git a/packages/strapi-bookshelf/lib/index.js b/packages/strapi-bookshelf/lib/index.js index 2ab4c023ff..10fc70a9c9 100755 --- a/packages/strapi-bookshelf/lib/index.js +++ b/packages/strapi-bookshelf/lib/index.js @@ -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)) { diff --git a/packages/strapi-bookshelf/package.json b/packages/strapi-bookshelf/package.json index 8e100c7be8..559077f39a 100644 --- a/packages/strapi-bookshelf/package.json +++ b/packages/strapi-bookshelf/package.json @@ -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": { @@ -55,4 +54,4 @@ "npm": ">= 5.3.0" }, "license": "MIT" -} \ No newline at end of file +} diff --git a/packages/strapi-helper-plugin/lib/src/utils/request.js b/packages/strapi-helper-plugin/lib/src/utils/request.js index fe69e3b848..a5ef761ec7 100644 --- a/packages/strapi-helper-plugin/lib/src/utils/request.js +++ b/packages/strapi-helper-plugin/lib/src/utils/request.js @@ -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); + } diff --git a/packages/strapi-knex/lib/index.js b/packages/strapi-knex/lib/index.js index 25f3ad5eb6..4325737ef6 100755 --- a/packages/strapi-knex/lib/index.js +++ b/packages/strapi-knex/lib/index.js @@ -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, diff --git a/packages/strapi-knex/package.json b/packages/strapi-knex/package.json index c4589643cd..050d416f39 100644 --- a/packages/strapi-knex/package.json +++ b/packages/strapi-knex/package.json @@ -47,4 +47,4 @@ "npm": ">= 5.0.0" }, "license": "MIT" -} \ No newline at end of file +} diff --git a/packages/strapi-plugin-content-manager/admin/src/containers/List/index.js b/packages/strapi-plugin-content-manager/admin/src/containers/List/index.js index dd64629859..3b47dfafc9 100644 --- a/packages/strapi-plugin-content-manager/admin/src/containers/List/index.js +++ b/packages/strapi-plugin-content-manager/admin/src/containers/List/index.js @@ -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(); diff --git a/packages/strapi-plugin-content-manager/config/queries/bookshelf.js b/packages/strapi-plugin-content-manager/config/queries/bookshelf.js index 1f1a091521..ee60ae7e0f 100644 --- a/packages/strapi-plugin-content-manager/config/queries/bookshelf.js +++ b/packages/strapi-plugin-content-manager/config/queries/bookshelf.js @@ -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; } }; diff --git a/packages/strapi-plugin-content-manager/controllers/ContentManager.js b/packages/strapi-plugin-content-manager/controllers/ContentManager.js index 61f621c726..2603cf9b00 100644 --- a/packages/strapi-plugin-content-manager/controllers/ContentManager.js +++ b/packages/strapi-plugin-content-manager/controllers/ContentManager.js @@ -45,7 +45,7 @@ module.exports = { const count = await strapi.query(ctx.params.model).count(); ctx.body = { - count, + count: _.isNumber(count) ? count : _.toNumber(count) }; }, diff --git a/packages/strapi/lib/hooks/index.js b/packages/strapi/lib/hooks/index.js index c7844472ed..ce7785d0e1 100644 --- a/packages/strapi/lib/hooks/index.js +++ b/packages/strapi/lib/hooks/index.js @@ -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}`))) {