Bookshelf.js-nested withRelated()(相当于多个内连接)
Bookshelf.js-nested withRelated() (equivalent of multiple inner joins)
我是 Bookshelf 的新手,正在尝试弄清楚如何执行与多重联接等效的操作。我想这对 withRelated 是可行的,不知何故?我知道我可以对彼此的结果进行多次查询,但它看起来很老套。
具体来说,我有几个表:用户、组织、端点和设置。
如果给定用户名,我想 return 端点列表及其所有列、相关设置和所有列以及相关组织的名称。
Bookshelf 有什么好的方法吗?我看到这个 Bookshelf EZ Fetch plugin 似乎是为此类用例构建的,但我想知道
"native" 语法,可以说是。
这是我的模型:
用户:
'use strict';
const bookshelf = require('../config/bookshelf_instance');
const Promise = require('bluebird');
const bcrypt = Promise.promisifyAll(require('bcrypt'));
const Role = require('./role');
const securityConfig = require('../config/security_config');
const Organization = require('./organization');
module.exports = bookshelf.Model.extend({
tableName: 'user',
roles() {
return this.belongsToMany(Role, 'map_user_role');
},
validPassword(password) {
return bcrypt.compareAsync(password, this.attributes.password);
},
organization(){
return this.hasOne(Organization, 'organization_id');
},
initialize() {
this.on('saving', model => {
if (!model.hasChanged('password')) return;
return Promise.coroutine(function* () {
const salt = yield bcrypt.genSaltAsync(securityConfig.saltRounds);
const hashedPassword = yield bcrypt.hashAsync(model.attributes.password, salt);
model.set('password', hashedPassword);
})();
});
}
});
组织:
'use strict';
const bookshelf = require('../config/bookshelf_instance');
const Endpoint = require('./endpoint');
const User = require('./user');
const Group = require('./endpoint_group');
module.exports = bookshelf.Model.extend({
tableName: 'organization',
endpoints() {
return this.hasMany(Endpoint, 'organization_id');
},
users() {
return this.hasMany(User, 'organization_id');
},
groups() {
return this.hasMany(Group, 'organization_id');
}
});
端点:
'use strict';
const bookshelf = require('../config/bookshelf_instance');
const ezFetch = require('bookshelf-ez-fetch');
const Organization = require('./organization');
const Settings = require('./endpoint_settings');
const Group = require('./endpoint_group');
bookshelf.plugin(ezFetch());
module.exports = bookshelf.Model.extend({
tableName: 'endpoint',
organization() {
return this.hasOne(Organization, 'organization_id');
},
settings() {
return this.hasOne(Settings, 'settingsId')
},
group() {
return this.belongsToMany(Group, 'parentGroupIds')
}
});
设置:
'use strict';
const bookshelf = require('../config/bookshelf_instance');
const Endpoint = require('./endpoint');
module.exports = bookshelf.Model.extend({
tableName: 'endpoint_setting',
endpoint() {
return this.hasOne(Endpoint, 'endpoint_settings_id')
}
});
如果其他人遇到这个问题,一位同事告诉我 ORM 通常不擅长这类事情。我最终只是做了一系列查询。
我是 Bookshelf 的新手,正在尝试弄清楚如何执行与多重联接等效的操作。我想这对 withRelated 是可行的,不知何故?我知道我可以对彼此的结果进行多次查询,但它看起来很老套。
具体来说,我有几个表:用户、组织、端点和设置。
如果给定用户名,我想 return 端点列表及其所有列、相关设置和所有列以及相关组织的名称。
Bookshelf 有什么好的方法吗?我看到这个 Bookshelf EZ Fetch plugin 似乎是为此类用例构建的,但我想知道 "native" 语法,可以说是。
这是我的模型:
用户:
'use strict';
const bookshelf = require('../config/bookshelf_instance');
const Promise = require('bluebird');
const bcrypt = Promise.promisifyAll(require('bcrypt'));
const Role = require('./role');
const securityConfig = require('../config/security_config');
const Organization = require('./organization');
module.exports = bookshelf.Model.extend({
tableName: 'user',
roles() {
return this.belongsToMany(Role, 'map_user_role');
},
validPassword(password) {
return bcrypt.compareAsync(password, this.attributes.password);
},
organization(){
return this.hasOne(Organization, 'organization_id');
},
initialize() {
this.on('saving', model => {
if (!model.hasChanged('password')) return;
return Promise.coroutine(function* () {
const salt = yield bcrypt.genSaltAsync(securityConfig.saltRounds);
const hashedPassword = yield bcrypt.hashAsync(model.attributes.password, salt);
model.set('password', hashedPassword);
})();
});
}
});
组织:
'use strict';
const bookshelf = require('../config/bookshelf_instance');
const Endpoint = require('./endpoint');
const User = require('./user');
const Group = require('./endpoint_group');
module.exports = bookshelf.Model.extend({
tableName: 'organization',
endpoints() {
return this.hasMany(Endpoint, 'organization_id');
},
users() {
return this.hasMany(User, 'organization_id');
},
groups() {
return this.hasMany(Group, 'organization_id');
}
});
端点:
'use strict';
const bookshelf = require('../config/bookshelf_instance');
const ezFetch = require('bookshelf-ez-fetch');
const Organization = require('./organization');
const Settings = require('./endpoint_settings');
const Group = require('./endpoint_group');
bookshelf.plugin(ezFetch());
module.exports = bookshelf.Model.extend({
tableName: 'endpoint',
organization() {
return this.hasOne(Organization, 'organization_id');
},
settings() {
return this.hasOne(Settings, 'settingsId')
},
group() {
return this.belongsToMany(Group, 'parentGroupIds')
}
});
设置:
'use strict';
const bookshelf = require('../config/bookshelf_instance');
const Endpoint = require('./endpoint');
module.exports = bookshelf.Model.extend({
tableName: 'endpoint_setting',
endpoint() {
return this.hasOne(Endpoint, 'endpoint_settings_id')
}
});
如果其他人遇到这个问题,一位同事告诉我 ORM 通常不擅长这类事情。我最终只是做了一系列查询。