动态创建 .where 函数 knexjs
Dynamically create .where function knexjs
有没有办法在 knex 中动态使用 .while
?
我有以下内容:
const user = await Users.findOne({id}, "id username email");
哪个
findOne(data, returns) {
return knex("users").select(returns && typeof returns === "string" ? returns.split(" ") : "*").where(data).first();
}
效果很好。如果我想让 id
和 username
匹配,我可以这样做:
const user = await Users.findOne({id, username}, "id username email");
但是,我需要 username
不区分大小写。
使用 mongo(猫鼬),我会这样做:
{username: new RegExp("^" + username + "$", "I")}
但使用 knex 查询实验室:
http://michaelavila.com/knex-querylab/
那确实 where username = {}
所以我发现我需要做 where username ilike %username%
,在 knex 中是
.where('username', 'ilike', `%${username}%`)
所以我有一个新功能:
//users route
const user = await Users.findOneByArray(['username', 'ilike', `%${username}%`]);
//queries file
findOneByArray(data) {
return knex("users").where(...data).first();
}
问题是,如果我现在有多个查询,我不能像处理对象那样处理它们。我目前正在为更复杂的查询做的是这种混乱:
//users route
const user = await Users.findTokenMatchesAccount(['id', '=', `${token.user_id}`], ['username', 'ilike', `%${username}%`], ['email', 'ilike', `%${email}%`]);
//query file
findTokenMatchesAccount(id, username, email) {
return knex("users").where(...id).where(...username).where(...email).first();
}
3 个人 .where
他们。有什么方法可以 automatically/dynamically 创建 where 函数,例如:
//users route
const user = await Users.findTokenMatchesAccount([['id', '=', `${token.user_id}`], ['username', 'ilike', `%${username}%`], ['email', 'ilike', `%${email}%`]]);
//query file
findTokenMatchesAccount(data) {
return knex("users").where(function() {
for(const i in data) return(where(data[i])}).first();
}
}
一些神奇的东西可以获取数据参数中的所有值并动态添加 .where
到它。或者我是否必须手动将其设置为我可能需要的任何查询的一部分? (如上所示 3 .where
秒,如果我有其他不区分大小写的选项要查找,下次可能需要 4 .where
秒)
或者获取数据数组并创建字符串并使用 knex.raw
会更容易吗?不过,我不确定是否会逃脱 drop table
.
knex 似乎是使用构建器模式实现的。也许尝试利用 .reduce()
?它通常用于 chaining/creating 管道。
async function findTokenMatchesAccount(whereStmtArgs) {
// Store the query
let query = knex('users');
// Pick one from the 2 options:
// Option 1
whereStmtArgs.reduce((q, currentWhereStmtArg) => q.where(...currentWhereStmtArg), query);
// Option 2 (if you're not comfortable with reduce)
for (const whereStmtArg for whereStmtArgs) {
query = query.where(...whereStmtArg);
}
return query.first();
}
const dynamicWhereStatements = [['id', '=', `${token.user_id}`], ['username', 'ilike', `%${username}%`], ['email', 'ilike', `%${email}%`]];
const user = await findTokenMatchesAccount(dynamicWhereStatements);
有没有办法在 knex 中动态使用 .while
?
我有以下内容:
const user = await Users.findOne({id}, "id username email");
哪个
findOne(data, returns) {
return knex("users").select(returns && typeof returns === "string" ? returns.split(" ") : "*").where(data).first();
}
效果很好。如果我想让 id
和 username
匹配,我可以这样做:
const user = await Users.findOne({id, username}, "id username email");
但是,我需要 username
不区分大小写。
使用 mongo(猫鼬),我会这样做:
{username: new RegExp("^" + username + "$", "I")}
但使用 knex 查询实验室:
http://michaelavila.com/knex-querylab/
那确实 where username = {}
所以我发现我需要做 where username ilike %username%
,在 knex 中是
.where('username', 'ilike', `%${username}%`)
所以我有一个新功能:
//users route
const user = await Users.findOneByArray(['username', 'ilike', `%${username}%`]);
//queries file
findOneByArray(data) {
return knex("users").where(...data).first();
}
问题是,如果我现在有多个查询,我不能像处理对象那样处理它们。我目前正在为更复杂的查询做的是这种混乱:
//users route
const user = await Users.findTokenMatchesAccount(['id', '=', `${token.user_id}`], ['username', 'ilike', `%${username}%`], ['email', 'ilike', `%${email}%`]);
//query file
findTokenMatchesAccount(id, username, email) {
return knex("users").where(...id).where(...username).where(...email).first();
}
3 个人 .where
他们。有什么方法可以 automatically/dynamically 创建 where 函数,例如:
//users route
const user = await Users.findTokenMatchesAccount([['id', '=', `${token.user_id}`], ['username', 'ilike', `%${username}%`], ['email', 'ilike', `%${email}%`]]);
//query file
findTokenMatchesAccount(data) {
return knex("users").where(function() {
for(const i in data) return(where(data[i])}).first();
}
}
一些神奇的东西可以获取数据参数中的所有值并动态添加 .where
到它。或者我是否必须手动将其设置为我可能需要的任何查询的一部分? (如上所示 3 .where
秒,如果我有其他不区分大小写的选项要查找,下次可能需要 4 .where
秒)
或者获取数据数组并创建字符串并使用 knex.raw
会更容易吗?不过,我不确定是否会逃脱 drop table
.
knex 似乎是使用构建器模式实现的。也许尝试利用 .reduce()
?它通常用于 chaining/creating 管道。
async function findTokenMatchesAccount(whereStmtArgs) {
// Store the query
let query = knex('users');
// Pick one from the 2 options:
// Option 1
whereStmtArgs.reduce((q, currentWhereStmtArg) => q.where(...currentWhereStmtArg), query);
// Option 2 (if you're not comfortable with reduce)
for (const whereStmtArg for whereStmtArgs) {
query = query.where(...whereStmtArg);
}
return query.first();
}
const dynamicWhereStatements = [['id', '=', `${token.user_id}`], ['username', 'ilike', `%${username}%`], ['email', 'ilike', `%${email}%`]];
const user = await findTokenMatchesAccount(dynamicWhereStatements);