使用 Objection JS 如何使用 withGraphFetched select 特定列
Using Objection JS How can I select specific columns with withGraphFetched
我有这个问题:
如何使用 withGraphFetched
方法指示我想从数据库中获取哪些列,我有一个 BelongsToOneRelation
并且我想排除一些列,这是我的模型:
module.exports = class ProveedorModel extends Model {
...
static relationMappings = {
empresa: {
relation: Model.BelongsToOneRelation,
modelClass: EmpresaModel,
join: {
from: 'proveedor.empresa_id',
to: 'empresa.id'
}
}
};
...
}
在我的控制器中我有这个:
const payload = await ProveedorModel.query().withGraphFetched('empresa');
但是 table empresa
有很多我不会的列,那么我该如何过滤?
您可以为您的关系指定 filter
属性
class Person extends Model {
static relationMappings = {
pets: {
relation: Model.OneToManyRelation,
modelClass: Animal,
filter: query => query.select('id', 'ownerId', 'name'),
join: {
from: 'Person.id',
to: 'Animal.ownerId'
}
}
}
}
参考:https://github.com/Vincit/objection.js/issues/70#issuecomment-175143072
只是想知道为什么异议在使用 withGraphFetched
时不像 withGraphJoined
那样仅查询映射在 tableMetadata
(https://vincit.github.io/objection.js/api/model/static-methods.html#static-tablemetadata) 中的列
或者,您可以使用 parsedatabasejson 映射您想要的属性
(https://vincit.github.io/objection.js/api/model/instance-methods.html#parsedatabasejson) 但您的 SQL 查询会把它们全部带出来。
我有这个问题:
如何使用 withGraphFetched
方法指示我想从数据库中获取哪些列,我有一个 BelongsToOneRelation
并且我想排除一些列,这是我的模型:
module.exports = class ProveedorModel extends Model {
...
static relationMappings = {
empresa: {
relation: Model.BelongsToOneRelation,
modelClass: EmpresaModel,
join: {
from: 'proveedor.empresa_id',
to: 'empresa.id'
}
}
};
...
}
在我的控制器中我有这个:
const payload = await ProveedorModel.query().withGraphFetched('empresa');
但是 table empresa
有很多我不会的列,那么我该如何过滤?
您可以为您的关系指定 filter
属性
class Person extends Model {
static relationMappings = {
pets: {
relation: Model.OneToManyRelation,
modelClass: Animal,
filter: query => query.select('id', 'ownerId', 'name'),
join: {
from: 'Person.id',
to: 'Animal.ownerId'
}
}
}
}
参考:https://github.com/Vincit/objection.js/issues/70#issuecomment-175143072
只是想知道为什么异议在使用 withGraphFetched
时不像 withGraphJoined
tableMetadata
(https://vincit.github.io/objection.js/api/model/static-methods.html#static-tablemetadata) 中的列
或者,您可以使用 parsedatabasejson 映射您想要的属性 (https://vincit.github.io/objection.js/api/model/instance-methods.html#parsedatabasejson) 但您的 SQL 查询会把它们全部带出来。