2017-06-22 45 views
0

我正在使用带有Feathersjs的PostgresSQL数据库,通过Knex连接这两个数据库。数据库表(NewTable)是使用外键创建的,引用了用户服务。Feathersjs通过API检索外键值

以下描述数据库表。

module.exports = function (app) { 
    const db = app.get('knexClient'); 
    db.schema.createTableIfNotExists('NewTable', table => { 
    table.increments('id').primary(); 
    table.integer('ownerId').references('users.id'); 
    }) 

当我通过GET查询它时,会收到ownerId的外键值(以整数形式)。

export function load(id) { 
    return { 
    types: [LOAD, LOAD_SUCCESS, LOAD_FAIL], 
    promise: ({ app }) => app.service('NewTable').get(id) 
    }; 
} 

什么是通过GET api检索用户的first_name列而不是userId的正确方法?

我是否需要在NewTable服务的钩子中运行另一个GET api以获取first_name列,或者有更好/更简单的方法来执行此操作?

回答

0

在feathersjs的钩子内创建一个如下所示的函数。

function populateUser() { 
    return populate({ 
    schema: { 
     include: [{ 
     nameAs: 'sentBy', 
     service: 'users', 
     parentField: 'ownerId', 
     childField: 'id' 
     }] 
    } 
    }); 
} 

在GET中使用它在钩子内调用它。用户服务列将包含在响应中。

after: { 
    all: [], 
    find: [], 
    get: [ 
    populateUser(), 
    } 
    ], 
    create: [], 
    update: [], 
    patch: [], 
    remove: [] 
},