Comparing version 0.1.1 to 0.1.2
@@ -8,3 +8,4 @@ /** | ||
*/ | ||
var fs = require('fs'); | ||
var fs = require('fs'), | ||
path = require('path'); | ||
@@ -71,6 +72,27 @@ // Helpers | ||
// Main function | ||
function decypher(spec, encoding) { | ||
function decypher(spec, extension) { | ||
extension = (extension || 'cypher').replace(/^\./, ''); | ||
if (typeof spec === 'string') { | ||
return resolve(fs.readFileSync(spec, 'utf-8'), spec); | ||
// Folder-behaviour | ||
if (fs.lstatSync(spec).isDirectory()) { | ||
var r = new RegExp('(.*)\\.' + extension + '$'), | ||
o = {}; | ||
var items = fs.readdirSync(spec) | ||
.filter(function(filename) { | ||
return fs.lstatSync(path.join(spec, filename)).isFile() && | ||
~filename.search(r); | ||
}) | ||
.forEach(function(filename) { | ||
var p = path.join(spec, filename); | ||
o[filename.match(r)[1]] = resolve(fs.readFileSync(p, 'utf-8'), p); | ||
}); | ||
return o; | ||
} | ||
else { | ||
return resolve(fs.readFileSync(spec, 'utf-8'), spec); | ||
} | ||
} | ||
@@ -90,3 +112,3 @@ else if (isPlainObject(spec)) { | ||
Object.defineProperty(decypher, 'version', { | ||
value: '0.1.1' | ||
value: '0.1.2' | ||
}); | ||
@@ -93,0 +115,0 @@ |
{ | ||
"name": "decypher", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Yesql-like cypher query loader for node.js", | ||
@@ -5,0 +5,0 @@ "main": "decypher.js", |
@@ -5,5 +5,5 @@ [](https://travis-ci.org/Yomguithereal/decypher) | ||
**decypher** is a node.js library aiming at loading cypher queries from external files so you can use them later in your code. | ||
**decypher** is a node.js library aiming at loading [cypher](http://neo4j.com/developer/cypher-query-language/) queries from external files so you can use them later in your code. | ||
The library's philosophy is quite similar to the Clojure [Yesql](http://neo4j.com/developer/cypher-query-language/)'s one. | ||
The library's philosophy is quite similar to the Clojure [Yesql](https://github.com/krisajenkins/yesql)'s one. | ||
@@ -82,2 +82,20 @@ ## Installation | ||
} | ||
// Loading the content of a folder | ||
// folder/ | ||
// - single.cypher | ||
// - multiple.cypher | ||
decypher('./folder'); | ||
>>> { | ||
single: 'MATCH (n)-[r]-(t)\nRETURN n,r,t LIMIT 100;', | ||
multiple: { | ||
first: 'MATCH (b:Book)\nRETURN b;', | ||
second: 'MATCH (v:Vocabulary)\nRETURN v;' | ||
} | ||
} | ||
// Choosing a different extension when loading a folder | ||
decypher('./path-to-queries-folder', 'cql'); | ||
``` | ||
@@ -84,0 +102,0 @@ |
6560
93
115