Multiple session.run statements
I am not sure how to run multiple statements. For instance how could I create another constraint for user label ? Should I create a session again ?
const session = driver.session();
session
.run("CREATE CONSTRAINT ON (user:USER) ASSERT user.email IS UNIQUE", { nameParam:'Jamess' })
.then((result) => {
console.log("neo4j constraints successful");
session.close();
})
.catch((error) => {
console.log("constraint.js error : ",error);
});
const session = driver.session();
Promise.all([
session.run("..."),
session.run("..."),
session.run("...")
])
.then((results) => session.close())
.catch((error) => session.close());
However sessions are pooled, so it should not be a problem to create another session.
In case you want to run a lot of statements, I don't think bolt currently supports sending multiple statements in a single http request, so you would have to use REST API I quess.
@urugator but bolt is not HTTP, it's its own binary protocol
In the manual, we have an example of running multiple statement:
session
.run("MATCH (knight:Person:Knight) WHERE knight.castle = {castle} RETURN id(knight) AS knight_id", {"castle": "Camelot"})
.subscribe({
onNext: function(record) {
session
.run("MATCH (knight) WHERE id(knight) = {id} MATCH (king:Person) WHERE king.name = {king} CREATE (knight)-[:DEFENDS]->(king)",
{"id": record.get("knight_id"), "king": "Arthur"});
},
onCompleted: function() {
session
.run("MATCH (:Knight)-[:DEFENDS]->() RETURN count(*)")
.then(function (result) {
console.log("Count is " + result.records[0].get(0).toInt());
});
},
onError: function(error) {
console.log(error);
}
});
Using chained session.run might make error handling harder, as if the first statement failed the second would be ignored too. If no special reasons, we could always run one statement in each session and close it. As sessions are pooled, so no extra overhead when we claim a new session from the session pool.
@acis good point :) What I meant was some kind of batch API...
I met the same problem and had to use the built-in REST API instead. Hi @acis , I'm not familiar with its own binary protocol. What's the differences between HTTP and the binary protocol? Is the binary protocol faster? Or safer? I guess they're nearly the same in speed and security when the neo4j server and the js file are in the same machine?
Update: I've found the differences between HTTP and the binary protocol Bolt. Here's the link: https://neo4j.com/blog/neo4j-3-0-language-drivers/. See details in HTTP vs Bolt Protocols section.
I don't get it, so how are we supposed to create multiple statements? Is there a clean way to do it? (I'm trying to avoid nested functions)
Is this the best way? (ignoring the errors for now)
run('CREATE (a:User { username: {username}})', {username: 'imadog'})
.then(()=>run('CREATE (a:User { username: {username}})', {username: 'imacat'}))
.then(()=>run('CREATE (a:User { username: {username}})', {username: 'imagiraffe'}))
.then(()=>run('CREATE (a:User { username: {username}})', {username: 'imachicken'}))
.then(function(r){
driver.close();
});
function run(statement, props){
return new Promise(function(res, rej){
let session = driver.session();
session.run(statement, props)
.then(function(r){
session.close(function(err){
res();
});
});
});
}
@jmcneves this looks like a valid way to run multiple statements. You could also use Promise.all(), as suggested earlier, to execute statements in parallel. I'd also suggest to improve error handling and create a single driver per application that is closed only when app exits.