thingdom / node-neo4j

[RETIRED] Neo4j graph database driver (REST API client) for Node.js
Apache License 2.0
925 stars 135 forks source link

multiple statements #203

Closed ekkis closed 8 years ago

ekkis commented 8 years ago

I have a file with a bunch of cypher. I tried sending it to the server but it pukes with "neo4j.ClientError: [Neo.ClientError.Statement.InvalidSyntax] Expected exactly one statement per query but got: 7"

is there a method I can use to give it a file name and have it run its contents?

alternatively, can anyone suggest how I could run all the lines individually (each with its own callback) and only once it's complete I could print a message? it seems I may need promises for this but I'm hoping for a better approach

sauravskumar commented 8 years ago

@ekkis were you able to get a solution...?

ekkis commented 8 years ago

I did. it was a long journey. here's what I ended up writing to accomplish this:

var fs = require('fs');
var neo = require('neo4j');
var Promise = require('bluebird');

var readFileAsync = Promise.promisify(
    fs.readFile, { context: fs }
);
init: function(env, opts) {
    self.cypherAsync = Promise.promisify(
        self.db.cypher, { context: self.db }
    );  
},
sendFile: function(fn) {
    var self = this;
    return Promise.try(function () {
        return readFileAsync(fn, 'utf8');
    }).then(function (data) {
        return data.split('\n');
    }).each(function (q) {
        q = q.replace(/^\/\/.*/, '');
        if (q.match('^\s*$')) return;
        return self.cypherAsync({ query: q }); 
    }).then(function() {
        return true;
    }); 
},  

which can be called like this:

.sendFile('/tmp/myscript.cph').then(() => {
    console.log('loaded');
});
sauravskumar commented 8 years ago

thank you

iamvajid commented 7 years ago

You can simply use neo4j-shell with -file command. .bin/neo4j-shell -file filepath -path your_databasefile_path -v