admin管理员组文章数量:1355786
I'm building an autofill function that takes a string input and returns a list of string suggestions.
Sequelize's iLike:query
returns every string in which the queried string appears. I would like to favour strings for which the query is a prefix. For example when query='sh'
then the results should return strings that start with sh
instead of having sh
anywhere within the string.
This is relatively simple to do after receiving the data from the DB, however I was wondering if there is a way to acplish this via sequelize while querying the DB? If so how?
The DB size will be between 10,000 and 100,000 strings of no more than a handful of words (pany names to be exact).
Optional question: DB's usually have superior performance to generically written code, in this circumstance should there even be a noticeable difference? Or should I just collect all the data from the DB and apply some other filters on it after via vanilla JS.
let suggestions = yield db.Company.findAll({
limit: 7,
where: {
pany_name: {
$iLike: '%'+this.data.query
}
}
})
I'm building an autofill function that takes a string input and returns a list of string suggestions.
Sequelize's iLike:query
returns every string in which the queried string appears. I would like to favour strings for which the query is a prefix. For example when query='sh'
then the results should return strings that start with sh
instead of having sh
anywhere within the string.
This is relatively simple to do after receiving the data from the DB, however I was wondering if there is a way to acplish this via sequelize while querying the DB? If so how?
The DB size will be between 10,000 and 100,000 strings of no more than a handful of words (pany names to be exact).
Optional question: DB's usually have superior performance to generically written code, in this circumstance should there even be a noticeable difference? Or should I just collect all the data from the DB and apply some other filters on it after via vanilla JS.
let suggestions = yield db.Company.findAll({
limit: 7,
where: {
pany_name: {
$iLike: '%'+this.data.query
}
}
})
Share
Improve this question
asked Jul 12, 2016 at 18:06
Alexei DarminAlexei Darmin
2,1591 gold badge20 silver badges31 bronze badges
2 Answers
Reset to default 7Seems like this is super easy! The '%'
acts like a *
from regex. So query + '%'
returns any results where query is the prefix.
let suggestions = yield db.Company.findAll({
limit: 5,
where: { $or: [
{ stock_ticker: { $ilike: query + '%' } },
{ pany_name: { $ilike: query + '%' } }
]},
order: '"volume" DESC'
})
Sequelize @6.37.3
Based on @alexei-darmin's answer The code snippet below works for me
import { Op } from "sequelize";
let suggestions = yield db.Company.findAll({
limit: 5,
where: { $or: [
{ stock_ticker: { [Op.iLike]: query + '%' } },
{ pany_name: { [Op.iLike]: query + '%' } }
]},
order: '"volume" DESC'
})
本文标签: javascriptSequelize query string prefixstarts withStack Overflow
版权声明:本文标题:javascript - Sequelize query string prefixstarts with - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744009781a2575349.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论