sql - Node.js - 将 SQL 查询转换为 Knex.js

标签 sql node.js postgresql knex.js

我有一个包含多个 join 的 SQL 查询 (Postgres),我无法将其转换为单个 Knex.js 语句。这是 SQL 查询:

SELECT 
    User.id, User.name, User.email,
    Role.name AS r_name,
    UserPofile.id AS p_id, UserPofile.date_of_birth AS p_dob,
    AuthToken.id AS at_id, AuthToken.token AS at_token, AuthToken.platform AS at_platform
FROM public."user" User
    LEFT JOIN public."user_role" UserRole ON User.id = UserRole.user_id
    LEFT JOIN public."role" Role ON UserRole.role_id = Role.id
    LEFT JOIN public."application" Application ON UserProfile.app_id = Application.id
    LEFT JOIN public."user_profile" UserProfile ON User.id = UserProfile.user_id
    LEFT JOIN public."auth_token" AuthToken ON User.id = AuthToken.user_id
WHERE 
    User.email LIKE 'some@email.com' AND
    Application.name LIKE 'awesome-application' AND
    AuthToken.platform LIKE 'mobile';

这是我的 Knex.js 代码:

    return knex('user').where({ email:'some@email.com' })
    .select([
        'user.id', 'user.name', 'user.email' // User
        'role.name AS rName' // Roles
        'user_profile.id AS pId', 'user_profile.date_of_birth AS pDob' // UserProfiles
        'auth_token.id AS atId' 'auth_token.platform AS atPlatform', 'auth_token.token AS atToken' // AuthTokens
    ])
    .leftJoin('user_profile', 'user_profile.user_id', 'user.id')
    .leftJoin('user_role', 'user_role.user_id', 'user.id')
    .leftJoin('role', 'role.id', 'user_role.role_id')
    .leftJoin('auth_token', 'auth_token.user_id', 'user.id')
    .then(users => {

        users = users.filter(user => { 
            return user.pApp_id === appId && user.atApp_id === appId && user.atPlatform === platform; 
        });
        return users;
    });

这会产生与 SQL 查询相同的结果,但问题是我必须在 Knex 调用的 .then() 子句中过滤返回的用户,因为我不知道如何为 Application.nameAuthToken.platform 添加 WHERE 条件。

问题:

谁能帮我弄清楚如何构造我的 Knex 代码的 .where() 子句,使其等同于 SQL 查询?

注意事项:

  • 我不知道如何console.log Knex 生成的 SQL 查询,因此我不完全确定我当前的 Knex 代码是否会生成上面的 SQL 查询(减去正确的WHERE 子句)。我已经检查过它实际上确实返回了相同的结果,方法是在 PgAdmin 中运行查询并 console.log()ing 从 Knex 函数返回的 users
  • 我没有包含定义此问题中使用的表和列的 CREATE TABLE/Knex 迁移,因为对我来说没有必要,而且我不想做一个已经很长的问题甚至更长。但是,如果您需要查看它,请随时告诉我。我很乐意将其包括在内。

最佳答案

要进行调试,您可以使用.toSQL() 来调试您的knex 查询documentation .
还有,很好cheatsheet

作为热修复解决方案,您可以使用 .raw()并将您的 SQL 代码粘贴到那里。

关于 WHERE 条件,您可以将它们链接在 knex 查询的末尾。
像这样:

return knex('user')
  .select('user.id', 'user.name', 'user.email', 
    'role.name AS rName'
    'user_profile.id AS pId', 'user_profile.date_of_birth AS pDob', 
    'auth_token.id AS atId' 'auth_token.platform AS atPlatform', 'auth_token.token AS atToken'
   )
  .leftJoin('user_profile', 'user_profile.user_id', 'user.id')
  .leftJoin('user_role', 'user_role.user_id', 'user.id')
  .leftJoin('role', 'role.id', 'user_role.role_id')
  .leftJoin('auth_token', 'auth_token.user_id', 'user.id') 
  .where('user.email', 'like', '%some@email.com%')
  .andWhere('application.name', 'like' '%awesome-application%')

//...etc

关于sql - Node.js - 将 SQL 查询转换为 Knex.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54094093/

相关文章:

mysql - 在 SQL 中插入多系列数据

c# - 如何在 MS Visual Studio 2013 中创建数据库?

sockets - 握手后立即发送 WebSocket 消息

node.js - Node js (Express) 在 mongodb 操作中卡住

python - 从 MySQL 迁移到 Postgres 后了解 "SEPARATOR"处或附近的语法错误

postgresql - 在 pgAdmin 中选择整列内容

postgresql - 在访问共享模式下锁定表 public.users_receipthash

sql - 如何在 GROUP BY 之间插入附加值

MySQL 多个内部连接与 Where 子句

node.js - 类型错误 : Cannot use 'in' operator to search for '$__firebase' in undefined