Handle boolean and numbers in search bar (bookshelf)

This commit is contained in:
Aurelsicoko 2018-06-12 11:32:13 +02:00
parent ddc1258429
commit 7d6e7b9d0a
2 changed files with 56 additions and 10 deletions

View File

@ -78,6 +78,7 @@ TableHeader.defaultProps = {
};
TableHeader.propTypes = {
entriesToDelete: PropTypes.array.isRequired,
headers: PropTypes.array.isRequired,
onChangeSort: PropTypes.func.isRequired,
onClickSelectAll: PropTypes.func.isRequired,

View File

@ -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();