mirror of
https://github.com/strapi/strapi.git
synced 2025-12-29 16:16:20 +00:00
Search with mongoose
This commit is contained in:
parent
a90f60ddd0
commit
d716888296
@ -15,12 +15,12 @@ import styles from './styles.scss';
|
||||
const WAIT = 400;
|
||||
|
||||
class Search extends React.Component {
|
||||
state = { value: '' };
|
||||
state = { value: this.props.initValue };
|
||||
|
||||
componentDidUpdate(prevProps) {
|
||||
const { model, value } = this.props;
|
||||
|
||||
if (prevProps.model !== model || !isEmpty(prevProps.value) && isEmpty(value) && this.timer === null) {
|
||||
if (prevProps.model !== model || !isEmpty(prevProps.value) && isEmpty(value)) {
|
||||
this.resetState();
|
||||
}
|
||||
}
|
||||
@ -85,6 +85,7 @@ Search.defaultProps = {
|
||||
|
||||
Search.propTypes = {
|
||||
changeParams: PropTypes.func,
|
||||
initValue: PropTypes.string.isRequired,
|
||||
model: PropTypes.string,
|
||||
value: PropTypes.string,
|
||||
};
|
||||
|
||||
@ -231,7 +231,8 @@ export class ListPage extends React.Component {
|
||||
history,
|
||||
listPage: { filters, params },
|
||||
} = this.props;
|
||||
const searchEnd = `&_sort=${params._sort}&source=${this.getSource()}${generateSearchFromFilters(filters)}`;
|
||||
const q = params.q !== '' ? `&q=${params.q}` : '';
|
||||
const searchEnd = `&_sort=${params._sort}${q}&source=${this.getSource()}${generateSearchFromFilters(filters)}`;
|
||||
const search =
|
||||
e.target.name === 'params._limit'
|
||||
? `_page=${params._page}&_limit=${e.target.value}${searchEnd}`
|
||||
@ -253,11 +254,12 @@ export class ListPage extends React.Component {
|
||||
const {
|
||||
listPage: { filters, params },
|
||||
} = this.props;
|
||||
const q = params.q !== '' ? `&q=${params.q}` : '';
|
||||
this.props.history.push({
|
||||
pathname: this.props.location.pathname,
|
||||
search: `?_page=${params._page}&_limit=${
|
||||
params._limit
|
||||
}&_sort=${sort}&source=${this.getSource()}${generateSearchFromFilters(filters)}`,
|
||||
}&_sort=${sort}${q}&source=${this.getSource()}${generateSearchFromFilters(filters)}`,
|
||||
});
|
||||
|
||||
this.props.changeParams({ target });
|
||||
@ -345,8 +347,9 @@ export class ListPage extends React.Component {
|
||||
<div>
|
||||
<div className={cn('container-fluid', styles.containerFluid)}>
|
||||
<Search
|
||||
model={this.getCurrentModelName()}
|
||||
changeParams={this.props.changeParams}
|
||||
initValue={getQueryParameters(this.props.location.search, 'q') || ''}
|
||||
model={this.getCurrentModelName()}
|
||||
value={params.q}
|
||||
/>
|
||||
<PluginHeader
|
||||
|
||||
@ -19,11 +19,69 @@ module.exports = {
|
||||
},
|
||||
|
||||
search: async function (params, populate) { // eslint-disable-line no-unused-vars
|
||||
return [];
|
||||
const $or = Object.keys(this.attributes).reduce((acc, curr) => {
|
||||
switch (this.attributes[curr].type) {
|
||||
case 'integer':
|
||||
case 'float':
|
||||
case 'decimal':
|
||||
if (!_.isNaN(_.toNumber(params.search))) {
|
||||
return acc.concat({ [curr]: params.search });
|
||||
}
|
||||
|
||||
return acc;
|
||||
case 'string':
|
||||
case 'text':
|
||||
case 'password':
|
||||
return acc.concat({ [curr]: { $regex: params.search, $options: 'i' } });
|
||||
case 'boolean':
|
||||
if (params.search === 'true' || params.search === 'false') {
|
||||
return acc.concat({ [curr]: params.search === 'true' });
|
||||
}
|
||||
|
||||
return acc;
|
||||
default:
|
||||
return acc;
|
||||
}
|
||||
}, []);
|
||||
|
||||
return this
|
||||
.find({ $or })
|
||||
.limit(Number(params.limit))
|
||||
.sort(params.sort)
|
||||
.skip(Number(params.skip))
|
||||
.populate(populate || this.associations.map(x => x.alias).join(' '))
|
||||
.lean();
|
||||
},
|
||||
|
||||
countSearch: async function (params = {}) { // eslint-disable-line no-unused-vars
|
||||
return 0;
|
||||
const $or = Object.keys(this.attributes).reduce((acc, curr) => {
|
||||
switch (this.attributes[curr].type) {
|
||||
case 'integer':
|
||||
case 'float':
|
||||
case 'decimal':
|
||||
if (!_.isNaN(_.toNumber(params.search))) {
|
||||
return acc.concat({ [curr]: params.search });
|
||||
}
|
||||
|
||||
return acc;
|
||||
case 'string':
|
||||
case 'text':
|
||||
case 'password':
|
||||
return acc.concat({ [curr]: { $regex: params.search, $options: 'i' } });
|
||||
case 'boolean':
|
||||
if (params.search === 'true' || params.search === 'false') {
|
||||
return acc.concat({ [curr]: params.search === 'true' });
|
||||
}
|
||||
|
||||
return acc;
|
||||
default:
|
||||
return acc;
|
||||
}
|
||||
}, []);
|
||||
|
||||
return this
|
||||
.find({ $or })
|
||||
.count();
|
||||
},
|
||||
|
||||
findOne: async function (params, populate, raw = true) {
|
||||
|
||||
@ -26,8 +26,6 @@ module.exports = {
|
||||
const { limit, skip, sort, source, q, populate = [] } = query; // eslint-disable-line no-unused-vars
|
||||
const filters = strapi.utils.models.convertParams(params.model, query);
|
||||
|
||||
console.log("SEARCH", q);
|
||||
|
||||
// Find entries using `queries` system
|
||||
return await strapi.query(params.model, source).search({
|
||||
limit: limit || filters.limit,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user