Skip to content

Instantly share code, notes, and snippets.

@Pepeye
Forked from vslinko/createQueryExecutor.js
Created December 31, 2015 15:51
Show Gist options
  • Save Pepeye/4e40d69a39b3524b6beb to your computer and use it in GitHub Desktop.
Save Pepeye/4e40d69a39b3524b6beb to your computer and use it in GitHub Desktop.
Neo4j Helpers
'use strict';
const CypherQuery = require('./cypher').CypherQuery;
module.exports = function createQueryExecutor(context) {
const executor = function executeQuery(query) {
if (typeof query === 'function') {
return query(executor);
}
const rawQuery = query instanceof CypherQuery
? query.getRawQuery()
: query;
return new Promise((resolve, reject) => {
context.cypher(rawQuery, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
};
return executor;
}
'use strict';
const createQueryExecutor = require('./createQueryExecutor');
module.exports = function createTransactionExecutor(db) {
return function executeTransaction(cb) {
const tx = db.beginTransaction();
const executeQuery = createQueryExecutor(tx);
return Promise.resolve(executeQuery(cb))
.then((result) => {
return new Promise((resolve, reject) => {
tx.commit((err) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
})
.then(undefined, (err) => {
if (tx.state === tx.STATE_OPEN || tx.state === tx.STATE_PENDING) {
return new Promise((resolve, reject) => {
tx.rollback(() => {
reject(err);
});
});
}
throw err;
});
};
};
'use strict';
class CypherQuery {
constructor(strings, values) {
this._strings = strings;
this._values = values;
}
getRawQuery() {
return this._getRawQuery({paramPrefix: 'p_'});
}
_getRawQuery(config) {
const paramPrefix = config.paramPrefix;
return this._strings
.reduce((rawQuery, string, index) => {
const query = rawQuery.query;
const params = rawQuery.params;
if (index === 0) {
return {query: string, params: {}};
}
const paramIndex = index - 1;
const value = this._values[paramIndex];
if (value instanceof CypherQuery) {
const options = value._getRawQuery({
paramPrefix: paramPrefix + paramIndex + '_',
});
return {
query: `${query}${options.query}${string}`,
params: Object.assign({}, params, options.params),
};
}
return {
query: `${query}{${paramPrefix}${paramIndex}}${string}`,
params: Object.assign({}, params, {
[`${paramPrefix}${paramIndex}`]: value,
}),
};
}, {query: '', params: {}});
}
}
function cypher(strings) {
const values = Array.prototype.slice.call(arguments, 1);
return new CypherQuery(strings, values);
}
module.exports.CypherQuery = CypherQuery;
module.exports.cypher = cypher;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment