From 7d6e7b9d0a813e53f4ac6cdca5ec42de6c406cbb Mon Sep 17 00:00:00 2001 From: Aurelsicoko Date: Tue, 12 Jun 2018 11:32:13 +0200 Subject: [PATCH] Handle boolean and numbers in search bar (bookshelf) --- .../admin/src/components/TableHeader/index.js | 1 + .../config/queries/bookshelf.js | 65 ++++++++++++++++--- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/packages/strapi-plugin-content-manager/admin/src/components/TableHeader/index.js b/packages/strapi-plugin-content-manager/admin/src/components/TableHeader/index.js index 0805d4e97c..f7fe1923c2 100755 --- a/packages/strapi-plugin-content-manager/admin/src/components/TableHeader/index.js +++ b/packages/strapi-plugin-content-manager/admin/src/components/TableHeader/index.js @@ -78,6 +78,7 @@ TableHeader.defaultProps = { }; TableHeader.propTypes = { + entriesToDelete: PropTypes.array.isRequired, headers: PropTypes.array.isRequired, onChangeSort: PropTypes.func.isRequired, onClickSelectAll: PropTypes.func.isRequired, diff --git a/packages/strapi-plugin-content-manager/config/queries/bookshelf.js b/packages/strapi-plugin-content-manager/config/queries/bookshelf.js index c5f90896d9..2ac90f343f 100755 --- a/packages/strapi-plugin-content-manager/config/queries/bookshelf.js +++ b/packages/strapi-plugin-content-manager/config/queries/bookshelf.js @@ -50,18 +50,40 @@ module.exports = { const associations = this.associations.map(x => x.alias); const searchText = Object.keys(this._attributes) .filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute)) - .filter(attribute => ['string', 'type'].includes(this._attributes[attribute].type)); + .filter(attribute => ['string', 'text'].includes(this._attributes[attribute].type)); const searchNoText = Object.keys(this._attributes) .filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute)) - .filter(attribute => !['string', 'type'].includes(this._attributes[attribute].type)); + .filter(attribute => !['string', 'text', 'boolean', 'integer', 'decimal', 'float'].includes(this._attributes[attribute].type)); + + const searchInt = Object.keys(this._attributes) + .filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute)) + .filter(attribute => ['integer', 'decimal', 'float'].includes(this._attributes[attribute].type)); + + const searchBool = Object.keys(this._attributes) + .filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute)) + .filter(attribute => ['boolean'].includes(this._attributes[attribute].type)); + + const query = (params.search || '').replace(/[^a-zA-Z0-9.-\s]+/g, ''); return this.query(qb => { // Search in columns which are not text value. searchNoText.forEach(attribute => { - qb.orWhereRaw(`LOWER(${attribute}) LIKE '%${_.toLower(params.search).replace(/[^a-zA-Z. ]/g, '')}%'`); + qb.orWhereRaw(`LOWER(${attribute}) LIKE '%${_.toLower(query)}%'`); }); + if (!_.isNaN(_.toNumber(query))) { + searchInt.forEach(attribute => { + qb.orWhereRaw(`${attribute} = ${_.toNumber(query)}`); + }); + } + + if (query === 'true' || query === 'false') { + searchBool.forEach(attribute => { + qb.orWhereRaw(`${attribute} = ${_.toNumber(query === 'true')}`); + }); + } + // Search in columns with text using index. switch (this.client) { case 'pg': { @@ -71,11 +93,11 @@ module.exports = { : `to_tsvector('${attribute}')` ); - qb.orWhereRaw(`${searchQuery.join(' || ')} @@ to_tsquery(?)`, params.search.replace(/[^a-zA-Z. ]/g, '')); + qb.orWhereRaw(`${searchQuery.join(' || ')} @@ to_tsquery(?)`, query); break; } default: - qb.orWhereRaw(`MATCH(${searchText.join(',')}) AGAINST(? IN BOOLEAN MODE)`, `*${params.search.replace(/[^a-zA-Z. ]/g, '')}*`); + qb.orWhereRaw(`MATCH(${searchText.join(',')}) AGAINST(? IN BOOLEAN MODE)`, `*${query}*`); break; } @@ -99,18 +121,41 @@ module.exports = { const associations = this.associations.map(x => x.alias); const searchText = Object.keys(this._attributes) .filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute)) - .filter(attribute => ['string', 'type'].includes(this._attributes[attribute].type)); + .filter(attribute => ['string', 'text'].includes(this._attributes[attribute].type)); const searchNoText = Object.keys(this._attributes) .filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute)) - .filter(attribute => !['string', 'type'].includes(this._attributes[attribute].type)); + .filter(attribute => !['string', 'text', 'boolean', 'integer', 'decimal', 'float'].includes(this._attributes[attribute].type)); + + const searchInt = Object.keys(this._attributes) + .filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute)) + .filter(attribute => ['integer', 'decimal', 'float'].includes(this._attributes[attribute].type)); + + const searchBool = Object.keys(this._attributes) + .filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute)) + .filter(attribute => ['boolean'].includes(this._attributes[attribute].type)); + + const query = (params.search || '').replace(/[^a-zA-Z0-9.-\s]+/g, ''); + return this.query(qb => { // Search in columns which are not text value. searchNoText.forEach(attribute => { - qb.orWhereRaw(`LOWER(${attribute}) LIKE '%${_.toLower(params.search).replace(/[^a-zA-Z. ]/g, '')}%'`); + qb.orWhereRaw(`LOWER(${attribute}) LIKE '%${_.toLower(query)}%'`); }); + if (!_.isNaN(_.toNumber(query))) { + searchInt.forEach(attribute => { + qb.orWhereRaw(`${attribute} = ${_.toNumber(query)}`); + }); + } + + if (query === 'true' || query === 'false') { + searchBool.forEach(attribute => { + qb.orWhereRaw(`${attribute} = ${_.toNumber(query === 'true')}`); + }); + } + // Search in columns with text using index. switch (this.client) { case 'pg': { @@ -120,11 +165,11 @@ module.exports = { : `to_tsvector('${attribute}')` ); - qb.orWhereRaw(`${searchQuery.join(' || ')} @@ to_tsquery(?)`, params.search.replace(/[^a-zA-Z. ]/g, '')); + qb.orWhereRaw(`${searchQuery.join(' || ')} @@ to_tsquery(?)`, query); break; } default: - qb.orWhereRaw(`MATCH(${searchText.join(',')}) AGAINST(? IN BOOLEAN MODE)`, `*${params.search.replace(/[^a-zA-Z. ]/g, '')}*`); + qb.orWhereRaw(`MATCH(${searchText.join(',')}) AGAINST(? IN BOOLEAN MODE)`, `*${query}*`); break; } }).count();