gpt4 book ai didi

node.js - Arangodb 中的交易

转载 作者:搜寻专家 更新时间:2023-11-01 00:43:35 25 4
gpt4 key购买 nike

我在 ArangoDB+nodejs 中的事务有一些问题。我需要做这样的事情:

transaction
{
insertedId=insertItemOneInDB();
insertItemTwoInDB();
}

但是当第二次插入失败时,第一次没有回滚!请帮我举个例子!

这是我的代码:

var transaction = function (collections,params,callback)
{
try
{

db.transaction.submit("user_merchant_tbl",params,
function ()
{
console.log("_collections:",collections);
console.log("params:");
console.log(params);
console.log("---");
console.log("+==========+");

//
var insertedDataId;
var relationsArrayIds=[];
db.document.create(collections,params.data).then(function(_insertedId)
{
insertedDataId=_insertedId;
}
,function(err)
{
console.log("ERROR: Arango--insert-->err: %j", err);
//throw "Error: "+err;
return false;
});

/////
var relations=params.relations;
for(var i=0;i<relations.length;i++)
{
db.document.create(relations[i].edge,relations[i].data).then(
function(_id)
{
relationsArrayIds.push(_id);
next(true);
}
,function(err)
{
console.log("ERROR: Arango--insert.edge-->err:23232 %j", err);
console.log("after return");
next(false);
return false
});
}

console.log("transaction before true");

function next(result)
{
if(result==true)
{
console.log("transaction is ok:",result);
callback(insertedDataId,result);
}
else
{
console.log("transaction is not OK:",result);
callback(insertedDataId,false);
}
}
}
);
}
catch(e)
{
console.log("catch->error in -->Arango.transaction: ",e);
}
}

最佳答案

首先,似乎对如何编写应该执行的操作存在误解。此操作直接在数据库服务器上执行,因此您不能使用 Arango Javascript api 提供的任何功能。如果你想设计你的 Action ,它必须在 arango shell 或服务器控制台上运行(bin/arangod data --console)我查看了您的代码并假设您想要存储用户和商家之间的关系。由于 Arango 带有一个很好的图形模块,您可以遵循以下方法:

// First we define a graph, containing of 2 document collections ("users" and "merchants")        and 2 edge collections (one per relation type, in this example "contactRequested" and "boughtSomethingFrom".
// Note that in this definition the relation "boughtSomethingFrom" is only allowed from a user to a merchant. Of course this is just one way to design it, you have to do it the way it suits you the best.
var edgeDefinitions = [{
collection: "contactRequested",
from: ["users", "merchants"],
to: ["users", "merchants"]
}, {
collection: "boughtSomethingFrom",
from: ["users"],
to: ["merchants"]
}];

// Now we create a graph called "user_merchant_graph" and in the callback function execute a transaction
db.graph.create("user_merchant_graph", edgeDefinitions, function(err, ret, message) {

// Lets define the action for the transaction, again this will be executed directly on the server ......
var action = function (params) {

// We have to require the database module ....

var db = require("internal").db;
var relationsArrayIds = [];

// now we store the user provided to the function
var insertedUserId = db["users"].insert(params.data)._id;

var relations = params.relations;
// Now we loop over through the relations object, store each merchant and it's relations to the user
Object.keys(relations).forEach(function (relation) {
// store merchant
var insertedMerchantId = db["merchants"].insert({merchantName : relation})._id;
// store relation as edge from "insertedUserId" to "insertedMerchantId".
var edgeId = db[relations[relation].relation].insert(insertedUserId, insertedMerchantId, relations[relation].additionalData)._id;
relationsArrayIds.push(edgeId);
});
};
// End of action

var options = {};
options.params = {
data: {
userName : "someUserName",
userSurname : "someUserSurname"
},
relations : {
merchantA : {relation : "contactRequested", additionalData : {data :"someData"}},
merchantB : {relation : "boughtSomethingFrom", additionalData : {data :"someData"}},
merchantC : {relation : "contactRequested", additionalData : {data :"someData"}}
}
};
// Now we call the transaction module ... a note to the collections parameter, it has to be an object containing the keys "write" and "read" which have a list of all collections as value into which the action is writing /reading from
// This collections object is NOT available within your action, the only thing passed as argument to your action is "options.params" !!
db.transaction.submit({write : ["users", "merchants", "contactRequested", "boughtSomethingFrom"]}, action, options, function(err, ret, message) {
//some callback
});

});

关于他们正在处理的交易,您可以试一试这段代码,如果您觉得。弄乱边的存储(将其更改为“var edgeId = db[relations[relation].relation].insert(relations[relation].additionalData)._id;”)你会看到你的用户和商户还没有被存储

希望对你有帮助

关于node.js - Arangodb 中的交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26466800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com