很难表达的问题,但现在开始了。最后,我在RethinkDB中有一组模型,它们以一种链式模式相互关联。当我想使用一个使用"hasMany“关系定义的字段(使用Thinky )放置一个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'和name Jack-O-Trades是一个遇到问题的原型,另外两个原型构建得很好。问题是,只有jackBuildNode是通过与skill模型的适当关联保存的。第二个节点,jackDebugNode在Jack-O-Trades上的nodes数组中保存并创建在数据库中,但是没有保存节点与skillDebugging技能模型应该具有的关系。
有人能看到我在代码和/或逻辑中可能出错的地方吗?我确实理解,有一些方法可以通过模块化保存和只更新后置关系来解决这个问题,但是让单个saveAll()操作负责关联和创建该数据会安全得多,因为单个操作更有可能在没有损坏的情况下成功,当将其分解为多个保存/更新调用时,可能会产生不完整的保存/更新。任何洞察力都将不胜感激。
发布于 2016-06-28 16:22:07
在将这个问题作为一个问题在thinky github上发布之后,我从作者那里得到了一个解决方案,这个问题在这个问题中得到了描述。对于那些在这里寻找答案的人,问题的总结如下:
在未传递参数的情况下使用的thinky .saveAll()方法被作者描述为不推荐使用,并且应该始终指定要保存的关系,以便使.saveAll()方法正确地恢复。检查思考型博士在.saveAll()中传递的参数,并查看neumino发布的修改过的代码示例,以了解如何对此代码示例进行轻微修改以插入saveAll参数并获得保存的适当关系。
https://stackoverflow.com/questions/37983102
复制相似问题