Rethinkdb和Thinky如何在具有'hasMany'关系的模型中保存所有多个模型,其中父模型包含子模型数组

时间:2016-06-23 05:34:31

标签: javascript rethinkdb thinky

非常难以明确表达,但这里有。最后,我在RethinkDB中有一组模型,它们以某种链模式相互关联。当我想使用一个“hasMany”关系定义的字段,使用Thinky ORM来放置一个rethinkdb模型数组以保存到数据库并创建与对象的关系时,我遇到了问题。

由于在段落中解释非常困难和冗长,我创建了一个独立的测试用例,用于在代码中说明我面临的问题。

var Reqlite = require('reqlite');
var assert = require('assert');

var thinky = require('thinky')({
    "host": "localhost",
    "port": 28016,
    "db": "test"
});

var server = new Reqlite({"driver-port": 28016});

var r = thinky.r;
var type = thinky.type;


// Models
var Account = thinky.createModel('accounts', {
    id: type.string(),
    username: type.string(),
});

var User = thinky.createModel('users', {
    id: type.string(),
    name: type.string(),
    accountId: type.string(),
    archetypeId: type.string(),
});

var Archetype = thinky.createModel('archetypes', {
    id: type.string(),
    name: type.string(),
});

var Node = thinky.createModel('nodes', {
    id: type.string(),
    skillId: type.string(),
    archetypeId: type.string(),
});

var Skill = thinky.createModel('skills', {
    id: type.string(),
    name: type.string(),
    desc: type.string(),
});


// Relationships
// Account <--> User
Account.hasMany(User, 'users', 'id', 'accountId');
User.belongsTo(Account, 'owner', 'accountId', 'id');

// User <--> Archetype
User.belongsTo(Archetype, 'archetype', 'archetypeId', 'id');

// Archetype <--> Node
Archetype.hasMany(Node, 'nodes', 'id', 'archetypeId');
Node.belongsTo(Archetype, 'archetype', 'archetypeId', 'id');

// Node <--> Skill
Node.belongsTo(Skill, 'skill', 'skillId', 'id');
Skill.hasMany(Node, 'nodes', 'id', 'skillId');


before(function(done) {

    var skillDebugging = Skill({
        id: '100',
        name: 'Debugging',
        desc: 'Increase knowledge of the inner working of things',
    });
    var skillBuilding = Skill({
        id: '110',
        name: 'Building',
        desc: 'Survive the Apocalypse with this',
    });

    var debuggerNode = Node({
        id: '200',
        skill: skillDebugging,
    });

    var builderNode = Node({
        id: '210',
        skill: skillBuilding,
    });

    var jackBuildNode = Node({
        id: '220',
        skill: skillBuilding,
    });
    var jackDebugNode = Node({
        id: '230',
        skill: skillDebugging,
    });

    var archetype1 = Archetype({
        id: '300',
        name: 'Debugger',
        nodes: [debuggerNode],
    });
    var archetype2 = Archetype({
        id: '310',
        name: 'Builder',
        nodes: [builderNode],
    });
    var archetype3 = Archetype({
        id: '320',
        name: 'Jack-O-Trades',
        nodes: [jackBuildNode, jackDebugNode],
    });

    archetype1.saveAll().then(function(result) {
        archetype2.saveAll().then(function(result1) {
            archetype3.saveAll().then(function(result2) {
                done();
            }).error(done).catch(done);
        }).error(done).catch(done);
    }).error(done).catch(done);
});

describe('New Test', function() {
    it('should return a list of archetypes with all joined nodes and skills', function(done) {
        Archetype.getJoin().then(function(archetypes) {
            // here we should expect to see the data saved and joined in each archetype as it was defined
            // above in the "before" block
            console.info('\nList of archetypes: ', JSON.stringify(archetypes));
            assert.ok(archetypes);

            archetypes.forEach(function(archetype) {
                assert.ok(archetype);
                assert.ok(archetype.nodes);
                archetype.nodes.forEach(function(node) {
                    assert.ok(node);
                    assert.ok(node.skill);
                });
            });
            done();
        }).error(done).catch(done);
    });

    it('should return a skill with a list of nodes joined', function(done) {
        Skill.get('100').getJoin().then(function(debugSkill) {
            console.info('\nDebug skill JSON: ', JSON.stringify(debugSkill));
            // if successful, we should see two nodes associated with this skill
            assert.ok(debugSkill);
            assert.ok(debugSkill.nodes);
            assert.ok(debugSkill.nodes.length > 1);
            done();
        }).error(done).catch(done);
    });
});

这是一个测试案例,使用Reqlite来模拟RethinkDB数据库,可以认为是ORM,mocha是运行测试的测试套件。在代码示例中,最后一个原型id: '320'和名称Jack-O-Trades是遇到问题的原型,其他两个原型很好。问题是,只有jackBuildNodeskill模型正确关联才能保存。 jackDebugNode上的nodes数组中的第二个节点Jack-O-Trades在数据库中保存并创建,但不保存节点与skillDebugging技能模型应具有的关系

任何人都可以在这里看到代码和/或逻辑中可能出错的地方吗?我确实理解有些方法可以通过模块化存储来解决这个问题,并且只是更新关系后的关系,但是单个saveAll()操作负责关联和创建此数据,因为单个操作会更安全更有可能在没有腐败的情况下成功,因为将其分解为多个保存/更新调用会产生不完整的保存/更新的可能性。任何见解将不胜感激。

1 个答案:

答案 0 :(得分:0)

thinky github上将此问题作为问题发布后,我收到了问题中描述的作者的解决方案。对于那些可能在这里寻找答案的人来说,问题的摘要是这样的:

在没有参数传递的情况下使用的thinky .saveAll()方法被作者描述为 deprecated ,并且应始终指定要保存的关系,以便使.saveAll()方法递归正常。查看传入.saveAll()的参数的thinky docs,并查看neumino发布的修改后的代码示例,看看这个代码示例是如何被略微修改的,以插入saveAll参数并保存正确的关系。 / p>