2017-11-10 80 views
0

試圖選擇帶有書架和連字符號的列。我的db是postgresql。在knex,我不喜歡這樣寫道:如何連接bookshelf.js中的列?

knex('client_users') 
    .select(
     'client_user_id as id', 
     'status', 
     'email', 
     'last_login_date as lastLoginDate', 
     knex.raw(`CONCAT(first_name, ' ', last_name) as "displayName"`) 
    ) 
    .whereIn('client_user_id', clientUserIds) 
    .andWhere('status', 'pending') 
    .tap(clientUsers => console.log('clientUsers retrieved:', clientUsers)); 

它的工作和回報:

[{ 
    id: 1, 
    status: 'pending', 
    email: '[email protected], 
    lastLoginDate: [Date Object], 
    displayName: 'John Smith'}, 
    {...}, {...}, etc] 

該文檔不明確,我在他們選擇的文檔看,但沒有什麼它需要或如何做我想要的東西:(

回答

0

好吧,解決方案是非常愚蠢的,在我看來,所以我有我的模型命名爲ClientUsers,我所要做的只是添加.query()文檔真的不是明白這一點哦,呃...不是我期望的那麼誠實,也許在那裏是一個更好的方法。

ClientUsers.query() 
    .select(
     'client_user_id as id', 
     'status', 
     'email', 
     'last_login_date as lastLoginDate', 
     knex.raw('CONCAT(first_name, \' \', last_name) as "displayName"') 
    ) 
    .whereIn('client_user_id', clientUserIds) 
    .andWhere('status', 'pending') 
    .tap(clientUsers => console.log('clientUsers retrieved:', clientUsers));