簡體   English   中英

Node.js-將SQL查詢轉換為Knex.js

[英]Node.js - Converting a SQL query into Knex.js

我有一個SQL查詢(Postgres),其中包含多個join ,但我很難將其轉換為單個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 .then()子句中過濾返回的用戶,因為我不知道如何為Application.name添加WHERE條件。和AuthToken.platform

題:

有人可以幫我找出如何構造我的Knex代碼的.where .where()子句,使其等同於SQL查詢嗎?

筆記:

  • 我不知道如何console.log Knex生成的SQL查詢,因此我不能完全確定我當前的Knex代碼是否會在它上面生成SQL查詢(減去正確的WHERE子句)。 我已經檢查了它是否確實返回了相同的結果,方法是在PgAdmin和console.log()運行查詢,以查看從Knex函數返回的users
  • 我沒有包括定義此問題中使用的表和列的CREATE TABLE / Knex遷移,因為對我而言,這沒有必要,並且我不想將一個已經很長的問題變得更長。 但是,如果您需要查看它,請不要猶豫,讓我知道。 我會很樂意包括在內。

要進行調試,您可以使用.toSQL()來調試knex查詢文檔
還不錯的備忘單

作為熱修復解決方案,您可以使用.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

最簡單的解決方案是使用.whereRaw()

原始位置

例:

.leftJoin('auth_token', 'auth_token.user_id', 'user.id')
.whereRaw('User.email LIKE ?', `${your_param_here}`)
.andWhereRaw('AuthToken.platform LIKE ?', `${your_param_here}`)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM