2017-11-10 57 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));