使用 feathers-client 和 sequelize 多对多关系
Using feathers-client and sequelize many-to-many relation
我正在尝试使用羽毛从 n:m 协会获取数据。我在前端使用 React,并使用 feathers-client 和 socketio 连接到 feathers。
//connection string where the feathers api is running
const host = 'http://localhost:3030';
const socket = io(host, {
transports: ['websocket'],
forceNew: true
});
// construct feathers app
export const client = feathers()
.configure(hooks())
.configure(auth({ storage: window.localStorage }))
.configure(socketio(socket));
但是当我使用 myService.find() 时,包含参数会从 hook.params
中删除
function (hook) {
hook.params.sequelize = {
include: [{ model: hook.app.services.users.Model,
as: 'teamOwner'
},{
model: hook.app.services.users.Model,
as: 'Trainer'
},{
model: hook.app.services.users.Model,
as: 'Member'
}
]
};
return Promise.resolve(hook);
}
当我在我的 before 挂钩中配置它时,它工作正常,但每次我们使用的服务时,所有表都连接在一起。所以我想在查询中指定要包含哪些表。像这样
client.service('team').find({
include: [
{ model:client.service('users').Model,
as: 'teamOwner'
},{
model:client.service('users').Model,
as: 'Trainer'
},{
model:client.service('users').Model,
as: 'Member'
}],
query: {
$sort: {
id: 1
}
}
})
提前致谢
因为只有 params.query
在客户端和服务器之间传递,最好的方法通常是创建一个自定义查询参数来指示您想要包含的内容:
function (hook) {
const { $include } = hook.params.query;
// Remove from the query so that it doesn't get included
// in the actual database query
delete hook.params.query.$include;
const sequelize = {
include: []
};
if(Array.isArray($include)) {
$include.forEach(name => {
sequelize.include.push({
model: hook.app.services.users.Model,
as: name
});
});
}
return Promise.resolve(hook);
}
现在客户可以在查询中指明他们想要什么,例如{ query: { $include: [ 'teamOwner', 'Trainer' ] } }
我正在尝试使用羽毛从 n:m 协会获取数据。我在前端使用 React,并使用 feathers-client 和 socketio 连接到 feathers。
//connection string where the feathers api is running
const host = 'http://localhost:3030';
const socket = io(host, {
transports: ['websocket'],
forceNew: true
});
// construct feathers app
export const client = feathers()
.configure(hooks())
.configure(auth({ storage: window.localStorage }))
.configure(socketio(socket));
但是当我使用 myService.find() 时,包含参数会从 hook.params
中删除function (hook) {
hook.params.sequelize = {
include: [{ model: hook.app.services.users.Model,
as: 'teamOwner'
},{
model: hook.app.services.users.Model,
as: 'Trainer'
},{
model: hook.app.services.users.Model,
as: 'Member'
}
]
};
return Promise.resolve(hook);
}
当我在我的 before 挂钩中配置它时,它工作正常,但每次我们使用的服务时,所有表都连接在一起。所以我想在查询中指定要包含哪些表。像这样
client.service('team').find({
include: [
{ model:client.service('users').Model,
as: 'teamOwner'
},{
model:client.service('users').Model,
as: 'Trainer'
},{
model:client.service('users').Model,
as: 'Member'
}],
query: {
$sort: {
id: 1
}
}
})
提前致谢
因为只有 params.query
在客户端和服务器之间传递,最好的方法通常是创建一个自定义查询参数来指示您想要包含的内容:
function (hook) {
const { $include } = hook.params.query;
// Remove from the query so that it doesn't get included
// in the actual database query
delete hook.params.query.$include;
const sequelize = {
include: []
};
if(Array.isArray($include)) {
$include.forEach(name => {
sequelize.include.push({
model: hook.app.services.users.Model,
as: name
});
});
}
return Promise.resolve(hook);
}
现在客户可以在查询中指明他们想要什么,例如{ query: { $include: [ 'teamOwner', 'Trainer' ] } }