代码之家  ›  专栏  ›  技术社区  ›  Boris K

bookself.js-nested withrelevant()(相当于多个内部联接)

  •  0
  • Boris K  · 技术社区  · 7 年前

    我刚开始使用Bookself,现在正试图弄清楚如何进行等价的多重连接。我想这是可行的与相关,不知何故?我知道我可以用对方的结果做几个查询,但这看起来很糟糕。

    具体来说,我有几个表:用户、组织、端点和设置。

    我希望在给定用户名的情况下,返回一个端点列表及其所有列、关联设置及其所有列以及关联组织的名称。

    有没有一个很好的方法来处理书架?我看到这个了 Bookshelf EZ Fetch plugin 它似乎是为此类用例而构建的,但希望知道 “本机”语法。

    以下是我的模型:

    用户:

    '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')
        }
    });
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Boris K    7 年前

    如果有其他人有这个问题,我的同事告诉我,ORM通常不擅长这种事情。我最后只做了一连串的查询。