Feathersjs 通过 API 检索外键值
Feathersjs retrieve foreign key value through API
我正在使用带有 Feathersjs 的 PostgresSQL 数据库,通过 Knex 连接两者。数据库 table (NewTable) 是使用外键创建的,引用了用户服务。
下面描述数据库table。
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 列,或者有一个 better/easier 方法可以做到?
在 feathersjs 的 hook 中创建如下函数。
function populateUser() {
return populate({
schema: {
include: [{
nameAs: 'sentBy',
service: 'users',
parentField: 'ownerId',
childField: 'id'
}]
}
});
}
在钩子中使用它在 GET 之后调用它。用户服务的列将包含在响应中。
after: {
all: [],
find: [],
get: [
populateUser(),
}
],
create: [],
update: [],
patch: [],
remove: []
},
我正在使用带有 Feathersjs 的 PostgresSQL 数据库,通过 Knex 连接两者。数据库 table (NewTable) 是使用外键创建的,引用了用户服务。
下面描述数据库table。
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 列,或者有一个 better/easier 方法可以做到?
在 feathersjs 的 hook 中创建如下函数。
function populateUser() {
return populate({
schema: {
include: [{
nameAs: 'sentBy',
service: 'users',
parentField: 'ownerId',
childField: 'id'
}]
}
});
}
在钩子中使用它在 GET 之后调用它。用户服务的列将包含在响应中。
after: {
all: [],
find: [],
get: [
populateUser(),
}
],
create: [],
update: [],
patch: [],
remove: []
},