From 90f057ca10a13404ca39f40fb5d0d068905408cc Mon Sep 17 00:00:00 2001 From: David Leonardi Date: Fri, 11 Jan 2019 19:38:31 +0100 Subject: [PATCH 1/5] change from require to imports, and make module more easily consumable. Also fix tests which were not passing --- .babelrc | 4 + .gitignore | 3 +- .jshintrc | 127 ---------------------- examples/RedisGraphExample.js | 47 +++++---- package.json | 40 +++---- src/index.js | 15 +++ src/label.js | 14 +++ src/record.js | 53 +++++----- src/redisGraph.js | 93 ++++++++-------- src/resultSet.js | 81 +++++++------- src/statistics.js | 139 +++++++++++------------- test/redisGraphAPITest.js | 192 ++++++++++++++++++++-------------- 12 files changed, 373 insertions(+), 435 deletions(-) create mode 100644 .babelrc delete mode 100644 .jshintrc create mode 100644 src/index.js create mode 100644 src/label.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000000..b592f4060f --- /dev/null +++ b/.babelrc @@ -0,0 +1,4 @@ +{ + "presets": ["es2015", "stage-3"], + "plugins": [] +} diff --git a/.gitignore b/.gitignore index 0b80d7ad76..bc446e8f38 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /.settings/ /node_modules/ .project -package-lock.json \ No newline at end of file +package-lock.json +yarn.lock diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 5b7ff541c3..0000000000 --- a/.jshintrc +++ /dev/null @@ -1,127 +0,0 @@ -{ - "esversion": 6, - - // -------------------------------------------------------------------- - // JSHint Nodeclipse Configuration v0.18 - // Strict Edition with some relaxations and switch to Node.js, no `use strict` - // by Ory Band, Michael Haschke, Paul Verest - // https://github.com/Nodeclipse/nodeclipse-1/blob/master/org.nodeclipse.ui/templates/common-templates/.jshintrc - // JSHint Documentation is at http://www.jshint.com/docs/options/ - // JSHint Integration v0.9.10 comes with JSHInt 2.5.6 , see https://github.com/eclipsesource/jshint-eclipse - // -------------------------------------------------------------------- - // from https://gist.github.com/haschek/2595796 - // - // This is a options template for [JSHint][1], using [JSHint example][2] - // and [Ory Band's example][3] as basis and setting config values to - // be most strict: - // - // * set all enforcing options to true - // * set all relaxing options to false - // * set all environment options to false, except the node value - // * set all JSLint legacy options to false - // - // [1]: http://www.jshint.com/ - // [2]: https://github.com/jshint/node-jshint/blob/master/example/config.json //404 - // [3]: https://github.com/oryband/dotfiles/blob/master/jshintrc - // [4]: http://www.jshint.com/options/ - // - // @author http://michael.haschke.biz/ - // @license http://unlicense.org/ - - // == Enforcing Options =============================================== - // - // These options tell JSHint to be more strict towards your code. Use - // them if you want to allow only a safe subset of JavaScript, very - // useful when your codebase is shared with a big number of developers - // with different skill levels. Was all true. - - "bitwise" : true, // Prohibit bitwise operators (&, |, ^, etc.). - "curly" : true, // Require {} for every new block or scope. - "eqeqeq" : true, // Require triple equals i.e. `===`. - "forin" : true, // Tolerate `for in` loops without `hasOwnPrototype`. - "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` - "latedef" : true, // Prohibit variable use before definition. - "newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`. - "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`. - "noempty" : true, // Prohibit use of empty blocks. - "nonew" : true, // Prohibit use of constructors for side-effects. - "plusplus" : false, // Prohibit use of `++` & `--`. //coding style related only - "regexp" : true, // Prohibit `.` and `[^...]` in regular expressions. - "undef" : true, // Require all non-global variables be declared before they are used. - "strict" : false, // Require `use strict` pragma in every file. - "trailing" : true, // Prohibit trailing whitespaces. - - // == Relaxing Options ================================================ - // - // These options allow you to suppress certain types of warnings. Use - // them only if you are absolutely positive that you know what you are - // doing. Was all false. - "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons). - "boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments. - "debug" : false, // Allow debugger statements e.g. browser breakpoints. - "eqnull" : false, // Tolerate use of `== null`. - //"es5" : true, // Allow EcmaScript 5 syntax. // es5 is default https://github.com/jshint/jshint/issues/1411 - "esnext" : false, // Allow ES.next (ECMAScript 6) specific features such as `const` and `let`. - "evil" : false, // Tolerate use of `eval`. - "expr" : false, // Tolerate `ExpressionStatement` as Programs. - "funcscope" : false, // Tolerate declarations of variables inside of control structures while accessing them later from the outside. - "globalstrict" : false, // Allow global "use strict" (also enables 'strict'). - "iterator" : false, // Allow usage of __iterator__ property. - "lastsemic" : false, // Tolerat missing semicolons when the it is omitted for the last statement in a one-line block. - "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons. - "laxcomma" : true, // Suppress warnings about comma-first coding style. - "loopfunc" : false, // Allow functions to be defined within loops. - "maxerr" : 100, // This options allows you to set the maximum amount of warnings JSHint will produce before giving up. Default is 50. - "multistr" : false, // Tolerate multi-line strings. - "onecase" : false, // Tolerate switches with just one case. - "proto" : false, // Tolerate __proto__ property. This property is deprecated. - "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`. - "scripturl" : false, // Tolerate script-targeted URLs. - "smarttabs" : false, // Tolerate mixed tabs and spaces when the latter are used for alignmnent only. - "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`. - "sub" : false, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`. - "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`. - "validthis" : false, // Tolerate strict violations when the code is running in strict mode and you use this in a non-constructor function. - - // == Environments ==================================================== - // - // These options pre-define global variables that are exposed by - // popular JavaScript libraries and runtime environments—such as - // browser or node.js. TODO JSHint Documentation has more, but it is not clear since what JSHint version they appeared - "browser" : false, // Standard browser globals e.g. `window`, `document`. - "couch" : false, // Enable globals exposed by CouchDB. - "devel" : false, // Allow development statements e.g. `console.log();`. - "dojo" : false, // Enable globals exposed by Dojo Toolkit. - "jquery" : false, // Enable globals exposed by jQuery JavaScript library. - "mootools" : false, // Enable globals exposed by MooTools JavaScript framework. - "node" : true, // Enable globals available when code is running inside of the NodeJS runtime environment. - "nonstandard" : false, // Define non-standard but widely adopted globals such as escape and unescape. - "phantom" : false, //?since version? This option defines globals available when your core is running inside of the PhantomJS runtime environment. - "prototypejs" : false, // Enable globals exposed by Prototype JavaScript framework. - "rhino" : false, // Enable globals available when your code is running inside of the Rhino runtime environment. - "worker" : false, //?since version? This option defines globals available when your code is running inside of a Web Worker. - "wsh" : false, // Enable globals available when your code is running as a script for the Windows Script Host. - "yui" : false, //?since version? This option defines globals exposed by the YUI JavaScript framework. - - // == JSLint Legacy =================================================== - // - // These options are legacy from JSLint. Aside from bug fixes they will - // not be improved in any way and might be removed at any point. - "nomen" : false, // Prohibit use of initial or trailing underbars in names. - "onevar" : false, // Allow only one `var` statement per function. - "passfail" : false, // Stop on first error. - "white" : false, // Check against strict whitespace and indentation rules. - - // == Undocumented Options ============================================ - // - // While Michael have found these options in [example1][2] and [example2][3] (already gone 404) - // they are not described in the [JSHint Options documentation][4]. - - "predef" : [ // Extra globals. - //"exampleVar", - //"anotherCoolGlobal", - //"iLoveDouglas" - "Java", "JavaFX", "$ARG" //no effect - ] - //, "indent" : 2 // Specify indentation spacing -} \ No newline at end of file diff --git a/examples/RedisGraphExample.js b/examples/RedisGraphExample.js index eb737c5155..a9e1e8eeee 100644 --- a/examples/RedisGraphExample.js +++ b/examples/RedisGraphExample.js @@ -1,26 +1,27 @@ -const RedisGraph = require('../src/redisGraph'); +const RedisGraph = require("../src/redisGraph"); - -let graph = new RedisGraph('social'); +let graph = new RedisGraph("social"); graph -.query("CREATE (:person{name:'roi',age:32})") -.then( () => { - return graph.query("CREATE (:person{name:'amit',age:30})"); -}) -.then( () => { - return graph.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)"); -}) -.then( () => { - return graph.query("MATCH (a:person)-[:knows]->(:person) RETURN a"); -}) -.then( (res) => { - while(res.hasNext()){ - let record = res.next(); - console.log(record.getString('a.name')); - } - console.log(res.getStatistics().queryExecutionTime()); -}) -.catch((err) => { - console.log(err); -}); + .query("CREATE (:person{name:'roi',age:32})") + .then(() => { + return graph.query("CREATE (:person{name:'amit',age:30})"); + }) + .then(() => { + return graph.query( + "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)" + ); + }) + .then(() => { + return graph.query("MATCH (a:person)-[:knows]->(:person) RETURN a"); + }) + .then(res => { + while (res.hasNext()) { + let record = res.next(); + console.log(record.getString("a.name")); + } + console.log(res.getStatistics().queryExecutionTime()); + }) + .catch(err => { + console.log(err); + }); diff --git a/package.json b/package.json index b723a022ee..6de7858097 100644 --- a/package.json +++ b/package.json @@ -1,20 +1,24 @@ { - "name": "redisgraph.js", - "version": "1.0.2", - "description": "Connect to RedisGraph 1.0.0 and up from JavaScript", - "author": "RedisLabs", - "license": "BSD 3", - "repository": { - "type": "git", - "url": "git://github.com/redislabs/redisgraph.js.git" - }, - "dependencies": { - "redis": "^2.8.0" - }, - "devDependencies": { - "mocha": "^5.2.0" - }, - "scripts": { - "test": "mocha" - } + "name": "redisgraph.js", + "version": "1.0.2", + "description": "Connect to RedisGraph 1.0.0 and up from JavaScript", + "author": "RedisLabs", + "license": "BSD 3", + "repository": { + "type": "git", + "url": "git://github.com/redislabs/redisgraph.js.git" + }, + "dependencies": { + "redis": "^2.8.0" + }, + "devDependencies": { + "babel-core": "^6.26.3", + "babel-preset-latest": "^6.24.1", + "babel-preset-stage-3": "^6.24.1", + "mocha": "^5.2.0" + }, + "scripts": { + "test": "mocha -R spec --require babel-core/register --exit" + }, + "main": "./src/index.js" } diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000000..3a107a3426 --- /dev/null +++ b/src/index.js @@ -0,0 +1,15 @@ +import Record from "./record"; +import RedisGraph from "./redisGraph"; +import ResultSet from "./resultSet"; +import Statistics from "./statistics"; +import Label from "./label"; + +const redisGraph = { + Record: Record, + RedisGraph: RedisGraph, + ResultSet: ResultSet, + Statistics: Statistics, + Label: Label +}; + +export default redisGraph; diff --git a/src/label.js b/src/label.js new file mode 100644 index 0000000000..0c80ac4d3c --- /dev/null +++ b/src/label.js @@ -0,0 +1,14 @@ +/** + * Different Statistics labels + */ +const Label = Object.freeze({ + LABELS_ADDED: "Labels added", + NODES_CREATED: "Nodes created", + NODES_DELETED: "Nodes deleted", + RELATIONSHIPS_DELETED: "Relationships deleted", + PROPERTIES_SET: "Properties set", + RELATIONSHIPS_CREATED: "Relationships created", + QUERY_INTERNAL_EXECUTION_TIME: "Query internal execution time" +}); + +export default Label; diff --git a/src/record.js b/src/record.js index b0978f5b64..ad46c463d7 100644 --- a/src/record.js +++ b/src/record.js @@ -1,34 +1,35 @@ /** * Hold a query record */ -module.exports = class Record { - - constructor(header, values){ - this._header = header; - this._values = values; +class Record { + constructor(header, values) { + this._header = header; + this._values = values; + } + + getString(key) { + let index = key; + if (typeof key === "string") { + index = this._header.indexOf(key); } - - getString(key) { - let index = key; - if(typeof key === "string"){ - index = this._header.indexOf(key); - } - return this._values[index]; - } + return this._values[index]; + } + + keys() { + return this._header; + } - keys() { - return this._header; - } + values() { + return this._values; + } - values() { - return this._values; - } + containsKey(key) { + return this._header.includes(key); + } - containsKey(key) { - return this._header.includes(key); - } + size() { + return this._header.length; + } +} - size() { - return this._header.length; - } -} \ No newline at end of file +export default Record; diff --git a/src/redisGraph.js b/src/redisGraph.js index bdb87a591b..87fb2ac85f 100644 --- a/src/redisGraph.js +++ b/src/redisGraph.js @@ -1,50 +1,53 @@ -const redis = require('redis'), -util = require('util'), -ResultSet = require('./resultSet'); +import redis from "redis"; +import util from "util"; +import ResultSet from "./ResultSet"; /** * RedisGraph client */ -module.exports = class RedisGraph { +class RedisGraph { + /** + * Creates a client to a specific graph running on the specific host/post + * See: node_redis for more options on createClient + * + * @param graphId the graph id + * @param host Redis host or node_redis client + * @param port Redis port + * @param options node_redis options + */ + constructor(graphId, host, port, options) { + this._graphId = graphId; + let client = + host instanceof redis.RedisClient + ? host + : redis.createClient.apply(redis, [].slice.call(arguments, 1)); + this._sendCommand = util.promisify(client.send_command).bind(client); + } - /** - * Creates a client to a specific graph running on the specific host/post - * See: node_redis for more options on createClient - * - * @param graphId the graph id - * @param host Redis host or node_redis client - * @param port Redis port - * @param options node_redis options - */ - constructor(graphId, host, port, options) { - this._graphId = graphId; - let client = (host instanceof redis.RedisClient) ? host : redis.createClient.apply(redis, [].slice.call(arguments,1)); - this._sendCommand = util.promisify(client.send_command).bind(client); - } - - /** - * Execute a Cypher query - * - * @param query Cypher query - * @return a result set - */ - query(query) { - return this._sendCommand('graph.QUERY',[this._graphId, query]) - .then((res) => { - return new ResultSet(res); - }); - } - - /** - * Deletes the entire graph - * - * @return delete running time statistics - */ - deleteGraph() { - return this._sendCommand('graph.DELETE',[this._graphId]) - .then((res) => { - return new ResultSet(res); - }); - } - -}; + /** + * Execute a Cypher query + * + * @param query Cypher query + * @return a result set + */ + query(query) { + return this._sendCommand("graph.QUERY", [this._graphId, query]).then( + res => { + return new ResultSet(res); + } + ); + } + + /** + * Deletes the entire graph + * + * @return delete running time statistics + */ + deleteGraph() { + return this._sendCommand("graph.DELETE", [this._graphId]).then(res => { + return new ResultSet(res); + }); + } +} + +export default RedisGraph; diff --git a/src/resultSet.js b/src/resultSet.js index f01009b14c..bb192bddef 100644 --- a/src/resultSet.js +++ b/src/resultSet.js @@ -1,45 +1,46 @@ -const Statistics = require('./statistics').Statistics, - Record = require('./record'); +import Statistics from "./statistics"; +import Record from "./record"; /** * Hold a query result */ -module.exports = class ResultSet { - - constructor(resp) { - this._position = 0; - this._statistics = new Statistics(resp[1]); - - let result = resp[0]; - - // Empty result set - if(result === null || result.length === 0) { - this._header = []; - this._totalResults = 0; - this._results = []; - } else { - this._header = result[0]; - this._totalResults = result.length - 1; - this._results = new Array(this._totalResults); - for(let i = 0 ; i < this._totalResults; ++i){ - this._results[i] = new Record(this._header, result[i+1]); - } - } - } - - getHeader(){ - return this._header; +class ResultSet { + constructor(resp) { + this._position = 0; + this._statistics = new Statistics(resp[1]); + + let result = resp[0]; + + // Empty result set + if (result === null || result.length === 0) { + this._header = []; + this._totalResults = 0; + this._results = []; + } else { + this._header = result[0]; + this._totalResults = result.length - 1; + this._results = new Array(this._totalResults); + for (let i = 0; i < this._totalResults; ++i) { + this._results[i] = new Record(this._header, result[i + 1]); + } } - - hasNext() { - return this._position < this._totalResults; - } - - next() { - return this._results[this._position++]; - } - - getStatistics() { - return this._statistics; - } -}; + } + + getHeader() { + return this._header; + } + + hasNext() { + return this._position < this._totalResults; + } + + next() { + return this._results[this._position++]; + } + + getStatistics() { + return this._statistics; + } +} + +export default ResultSet; diff --git a/src/statistics.js b/src/statistics.js index dc5ce9b31e..dec6372cce 100644 --- a/src/statistics.js +++ b/src/statistics.js @@ -1,77 +1,62 @@ -/** - * Different Statistics labels - */ -const Label = Object.freeze({ - LABELS_ADDED: "Labels added", - NODES_CREATED: "Nodes created", - NODES_DELETED: "Nodes deleted", - RELATIONSHIPS_DELETED: "Relationships deleted", - PROPERTIES_SET: "Properties set", - RELATIONSHIPS_CREATED: "Relationships created", - QUERY_INTERNAL_EXECUTION_TIME: "Query internal execution time" -}); - -module.exports ={ - Label: Label, - Statistics : class Statistics { - - - constructor(raw){ - this._raw = raw; - } - - getStringValue(label) { - return this.getStatistics()[label]; - } - - /** - * Return the query statistics - * - * @return statistics object - */ - getStatistics(){ - if(!this._statistics) { - this._statistics = {}; - for(let row of this._raw) { - let touple = row.split(':'); - this._statistics[touple[0]] = touple[1].trim(); - } - } - return this._statistics; - } - - getIntValue(label) { - let value = this.getStringValue(label); - return value ? parseInt(value) : 0; - } - - nodesCreated() { - return this.getIntValue(Label.NODES_CREATED); - } - - nodesDeleted() { - return this.getIntValue(Label.NODES_DELETED); - } - - labelsAdded() { - return this.getIntValue(Label.LABELS_ADDED); - } - - relationshipsDeleted() { - return this.getIntValue(Label.RELATIONSHIPS_DELETED); - } - - relationshipsCreated() { - return this.getIntValue(Label.RELATIONSHIPS_CREATED); - } - - propertiesSet() { - return this.getIntValue(Label.PROPERTIES_SET); - } - - queryExecutionTime() { - return this.getIntValue(Label.QUERY_INTERNAL_EXECUTION_TIME); - } - - } -} \ No newline at end of file +import Label from "./label"; + +class Statistics { + constructor(raw) { + this._raw = raw; + } + + getStringValue(label) { + return this.getStatistics()[label]; + } + + /** + * Return the query statistics + * + * @return statistics object + */ + getStatistics() { + if (!this._statistics) { + this._statistics = {}; + for (let row of this._raw) { + let touple = row.split(":"); + this._statistics[touple[0]] = touple[1].trim(); + } + } + return this._statistics; + } + + getIntValue(label) { + let value = this.getStringValue(label); + return value ? parseInt(value) : 0; + } + + nodesCreated() { + return this.getIntValue(Label.NODES_CREATED); + } + + nodesDeleted() { + return this.getIntValue(Label.NODES_DELETED); + } + + labelsAdded() { + return this.getIntValue(Label.LABELS_ADDED); + } + + relationshipsDeleted() { + return this.getIntValue(Label.RELATIONSHIPS_DELETED); + } + + relationshipsCreated() { + return this.getIntValue(Label.RELATIONSHIPS_CREATED); + } + + propertiesSet() { + return this.getIntValue(Label.PROPERTIES_SET); + } + + queryExecutionTime() { + return this.getIntValue(Label.QUERY_INTERNAL_EXECUTION_TIME); + } +} + +export default Statistics; diff --git a/test/redisGraphAPITest.js b/test/redisGraphAPITest.js index 7c64a79008..1dae08e81a 100644 --- a/test/redisGraphAPITest.js +++ b/test/redisGraphAPITest.js @@ -1,87 +1,123 @@ -const assert = require('assert'), -redis = require('redis'), -Label = require('../src/statistics').Label, -RedisGraphAPI = require('../src/redisGraph'); +import { describe, before, it } from "mocha"; +import assert from "assert"; +import redis from "redis"; +import Label from "../src/label"; +import RedisGraph from "../src/redisGraph"; -describe('RedisGraphAPI Test', () =>{ - const api = new RedisGraphAPI("social"); - - beforeEach( () => { - return api.deleteGraph(); - }); +describe("RedisGraph Test", () => { + const api = new RedisGraph("social"); - it('test bring your client', () => { - return new RedisGraphAPI( "social", redis.createClient()); - }); - - it('test Create Node', () => { - // Create a node - return api.query("CREATE ({name:'roi',age:32})") - .then( (result) => { - assert.ok(!result.hasNext()); - assert.equal(1, result.getStatistics().nodesCreated()); - assert.ifError(result.getStatistics().getStringValue(Label.NODES_DELETED)); - assert.ifError(result.getStatistics().getStringValue(Label.RELATIONSHIPS_CREATED)); - assert.ifError(result.getStatistics().getStringValue(Label.RELATIONSHIPS_DELETED)); - assert.equal(2, result.getStatistics().propertiesSet()); - assert.ok(result.getStatistics().getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME)); - }); - }); + it("test bring your client", () => { + return new RedisGraph("social", redis.createClient()); + }); - it('test Create Labeled Node', () => { - // Create a node with a label - return api.query("CREATE (:human{name:'danny',age:12})") - .then( (result) => { - assert.ok(!result.hasNext()); - assert.equal("1", result.getStatistics().getStringValue(Label.NODES_CREATED)); - assert.equal("2", result.getStatistics().getStringValue(Label.PROPERTIES_SET)); - assert.ok(result.getStatistics().getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME)); - }); - }); + it("test Create Node", () => { + api.deleteGraph("social"); + // Create a node + return api.query("CREATE ({name:'roi',age:32})").then(result => { + assert.ok(!result.hasNext()); + assert.equal(1, result.getStatistics().nodesCreated()); + assert.ifError( + result.getStatistics().getStringValue(Label.NODES_DELETED) + ); + assert.ifError( + result.getStatistics().getStringValue(Label.RELATIONSHIPS_CREATED) + ); + assert.ifError( + result.getStatistics().getStringValue(Label.RELATIONSHIPS_DELETED) + ); + assert.equal(2, result.getStatistics().propertiesSet()); + assert.ok( + result + .getStatistics() + .getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME) + ); + }); + }); - it('test Connect Nodes', () => { - // Create both source and destination nodes - let createResult1 = api.query("CREATE (:person{name:'roi',age:32})"); - let createResult2 = api.query("CREATE (:person{name:'amit',age:30})"); + it("test Create Labeled Node", () => { + api.deleteGraph("social"); + // Create a node with a label + return api.query("CREATE (:human{name:'danny',age:12})").then(result => { + assert.ok(!result.hasNext()); + assert.equal( + "1", + result.getStatistics().getStringValue(Label.NODES_CREATED) + ); + assert.equal( + "2", + result.getStatistics().getStringValue(Label.PROPERTIES_SET) + ); + assert.ok( + result + .getStatistics() + .getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME) + ); + }); + }); - // Connect source and destination nodes. - return api.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)") - .then( (matchResult) => { - assert.ok(!matchResult.hasNext()); - assert.ifError(matchResult.getStatistics().getStringValue(Label.NODES_CREATED)); - assert.ifError(matchResult.getStatistics().getStringValue(Label.PROPERTIES_SET)); - assert.equal(1, matchResult.getStatistics().relationshipsCreated()); - assert.equal(0, matchResult.getStatistics().relationshipsDeleted()); - assert.ok(matchResult.getStatistics().getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME)); - }); - }); + it("test Connect Nodes", () => { + api.deleteGraph("social"); + // Create both source and destination nodes + let createResult1 = api.query("CREATE (:person{name:'roi',age:32})"); + let createResult2 = api.query("CREATE (:person{name:'amit',age:30})"); - it('test Query', () => { + // Connect source and destination nodes. + return api + .query( + "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)" + ) + .then(matchResult => { + assert.ok(!matchResult.hasNext()); + assert.ifError( + matchResult.getStatistics().getStringValue(Label.NODES_CREATED) + ); + assert.ifError( + matchResult.getStatistics().getStringValue(Label.PROPERTIES_SET) + ); + assert.equal(1, matchResult.getStatistics().relationshipsCreated()); + assert.equal(0, matchResult.getStatistics().relationshipsDeleted()); + assert.ok( + matchResult + .getStatistics() + .getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME) + ); + }); + }); - // Create both source and destination nodes - return api.query("CREATE (:qhuman{name:'roi',age:32})") - .then( (create1Result) => { - return api.query("CREATE (:qhuman{name:'amit',age:30})"); - }) - .then( (create2Result) => { - // Connect source and destination nodes. - return api.query("MATCH (a:qhuman), (b:qhuman) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)"); - }) - .then( (connectResult) => { - // Query - return api.query("MATCH (a:qhuman)-[knows]->(:qhuman) RETURN a"); - }) - .then( (resultSet) => { - assert.ok(resultSet.hasNext()); - assert.equal(0, resultSet.getStatistics().nodesCreated()); - assert.equal(0, resultSet.getStatistics().propertiesSet()); - assert.equal(0, resultSet.getStatistics().relationshipsCreated()); - assert.equal(0, resultSet.getStatistics().relationshipsDeleted()); - assert.ok(resultSet.getStatistics().getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME)); + it("test Query", () => { + api.deleteGraph("social"); + // Create both source and destination nodes + return api + .query("CREATE (:qhuman{name:'roi',age:32})") + .then(create1Result => { + return api.query("CREATE (:qhuman{name:'amit',age:30})"); + }) + .then(create2Result => { + // Connect source and destination nodes. + return api.query( + "MATCH (a:qhuman), (b:qhuman) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)" + ); + }) + .then(connectResult => { + // Query + return api.query("MATCH (a:qhuman)-[knows]->(:qhuman) RETURN a"); + }) + .then(resultSet => { + assert.ok(resultSet.hasNext()); + assert.equal(0, resultSet.getStatistics().nodesCreated()); + assert.equal(0, resultSet.getStatistics().propertiesSet()); + assert.equal(0, resultSet.getStatistics().relationshipsCreated()); + assert.equal(0, resultSet.getStatistics().relationshipsDeleted()); + assert.ok( + resultSet + .getStatistics() + .getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME) + ); - let record = resultSet.next(); - assert.equal( "roi", record.getString(1)); - assert.equal( "32.000000", record.getString(0)); - }); - }); + let record = resultSet.next(); + assert.equal("roi", record.getString(1)); + assert.equal("32.000000", record.getString(0)); + }); + }); }); From 402418d4f4e085d8ec440b226882538a54391712 Mon Sep 17 00:00:00 2001 From: David Leonardi Date: Fri, 11 Jan 2019 19:42:44 +0100 Subject: [PATCH 2/5] add editorconfig and provide correct indentation settings compliant with original project --- .editorconfig | 19 +++ examples/RedisGraphExample.js | 44 +++---- src/index.js | 10 +- src/label.js | 14 +-- src/record.js | 46 ++++---- src/redisGraph.js | 80 ++++++------- src/resultSet.js | 72 ++++++------ src/statistics.js | 112 +++++++++--------- test/redisGraphAPITest.js | 216 +++++++++++++++++----------------- 9 files changed, 316 insertions(+), 297 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000000..df3a0a8c24 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,19 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true + +# Matches multiple files with brace expansion notation +# Set default charset +[*.{js}] +charset = utf-8 + +# Indentation override for all JS +[**.js] +indent_style = tab +indent_size = 2 diff --git a/examples/RedisGraphExample.js b/examples/RedisGraphExample.js index a9e1e8eeee..ee25c3afde 100644 --- a/examples/RedisGraphExample.js +++ b/examples/RedisGraphExample.js @@ -3,25 +3,25 @@ const RedisGraph = require("../src/redisGraph"); let graph = new RedisGraph("social"); graph - .query("CREATE (:person{name:'roi',age:32})") - .then(() => { - return graph.query("CREATE (:person{name:'amit',age:30})"); - }) - .then(() => { - return graph.query( - "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)" - ); - }) - .then(() => { - return graph.query("MATCH (a:person)-[:knows]->(:person) RETURN a"); - }) - .then(res => { - while (res.hasNext()) { - let record = res.next(); - console.log(record.getString("a.name")); - } - console.log(res.getStatistics().queryExecutionTime()); - }) - .catch(err => { - console.log(err); - }); + .query("CREATE (:person{name:'roi',age:32})") + .then(() => { + return graph.query("CREATE (:person{name:'amit',age:30})"); + }) + .then(() => { + return graph.query( + "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)" + ); + }) + .then(() => { + return graph.query("MATCH (a:person)-[:knows]->(:person) RETURN a"); + }) + .then(res => { + while (res.hasNext()) { + let record = res.next(); + console.log(record.getString("a.name")); + } + console.log(res.getStatistics().queryExecutionTime()); + }) + .catch(err => { + console.log(err); + }); diff --git a/src/index.js b/src/index.js index 3a107a3426..01dce84ced 100644 --- a/src/index.js +++ b/src/index.js @@ -5,11 +5,11 @@ import Statistics from "./statistics"; import Label from "./label"; const redisGraph = { - Record: Record, - RedisGraph: RedisGraph, - ResultSet: ResultSet, - Statistics: Statistics, - Label: Label + Record: Record, + RedisGraph: RedisGraph, + ResultSet: ResultSet, + Statistics: Statistics, + Label: Label }; export default redisGraph; diff --git a/src/label.js b/src/label.js index 0c80ac4d3c..b0329fed89 100644 --- a/src/label.js +++ b/src/label.js @@ -2,13 +2,13 @@ * Different Statistics labels */ const Label = Object.freeze({ - LABELS_ADDED: "Labels added", - NODES_CREATED: "Nodes created", - NODES_DELETED: "Nodes deleted", - RELATIONSHIPS_DELETED: "Relationships deleted", - PROPERTIES_SET: "Properties set", - RELATIONSHIPS_CREATED: "Relationships created", - QUERY_INTERNAL_EXECUTION_TIME: "Query internal execution time" + LABELS_ADDED: "Labels added", + NODES_CREATED: "Nodes created", + NODES_DELETED: "Nodes deleted", + RELATIONSHIPS_DELETED: "Relationships deleted", + PROPERTIES_SET: "Properties set", + RELATIONSHIPS_CREATED: "Relationships created", + QUERY_INTERNAL_EXECUTION_TIME: "Query internal execution time" }); export default Label; diff --git a/src/record.js b/src/record.js index ad46c463d7..6f4a27668d 100644 --- a/src/record.js +++ b/src/record.js @@ -2,34 +2,34 @@ * Hold a query record */ class Record { - constructor(header, values) { - this._header = header; - this._values = values; - } + constructor(header, values) { + this._header = header; + this._values = values; + } - getString(key) { - let index = key; - if (typeof key === "string") { - index = this._header.indexOf(key); - } - return this._values[index]; - } + getString(key) { + let index = key; + if (typeof key === "string") { + index = this._header.indexOf(key); + } + return this._values[index]; + } - keys() { - return this._header; - } + keys() { + return this._header; + } - values() { - return this._values; - } + values() { + return this._values; + } - containsKey(key) { - return this._header.includes(key); - } + containsKey(key) { + return this._header.includes(key); + } - size() { - return this._header.length; - } + size() { + return this._header.length; + } } export default Record; diff --git a/src/redisGraph.js b/src/redisGraph.js index 87fb2ac85f..3b1f1137da 100644 --- a/src/redisGraph.js +++ b/src/redisGraph.js @@ -6,48 +6,48 @@ import ResultSet from "./ResultSet"; * RedisGraph client */ class RedisGraph { - /** - * Creates a client to a specific graph running on the specific host/post - * See: node_redis for more options on createClient - * - * @param graphId the graph id - * @param host Redis host or node_redis client - * @param port Redis port - * @param options node_redis options - */ - constructor(graphId, host, port, options) { - this._graphId = graphId; - let client = - host instanceof redis.RedisClient - ? host - : redis.createClient.apply(redis, [].slice.call(arguments, 1)); - this._sendCommand = util.promisify(client.send_command).bind(client); - } + /** + * Creates a client to a specific graph running on the specific host/post + * See: node_redis for more options on createClient + * + * @param graphId the graph id + * @param host Redis host or node_redis client + * @param port Redis port + * @param options node_redis options + */ + constructor(graphId, host, port, options) { + this._graphId = graphId; + let client = + host instanceof redis.RedisClient + ? host + : redis.createClient.apply(redis, [].slice.call(arguments, 1)); + this._sendCommand = util.promisify(client.send_command).bind(client); + } - /** - * Execute a Cypher query - * - * @param query Cypher query - * @return a result set - */ - query(query) { - return this._sendCommand("graph.QUERY", [this._graphId, query]).then( - res => { - return new ResultSet(res); - } - ); - } + /** + * Execute a Cypher query + * + * @param query Cypher query + * @return a result set + */ + query(query) { + return this._sendCommand("graph.QUERY", [this._graphId, query]).then( + res => { + return new ResultSet(res); + } + ); + } - /** - * Deletes the entire graph - * - * @return delete running time statistics - */ - deleteGraph() { - return this._sendCommand("graph.DELETE", [this._graphId]).then(res => { - return new ResultSet(res); - }); - } + /** + * Deletes the entire graph + * + * @return delete running time statistics + */ + deleteGraph() { + return this._sendCommand("graph.DELETE", [this._graphId]).then(res => { + return new ResultSet(res); + }); + } } export default RedisGraph; diff --git a/src/resultSet.js b/src/resultSet.js index bb192bddef..2925ed0b2e 100644 --- a/src/resultSet.js +++ b/src/resultSet.js @@ -5,42 +5,42 @@ import Record from "./record"; * Hold a query result */ class ResultSet { - constructor(resp) { - this._position = 0; - this._statistics = new Statistics(resp[1]); - - let result = resp[0]; - - // Empty result set - if (result === null || result.length === 0) { - this._header = []; - this._totalResults = 0; - this._results = []; - } else { - this._header = result[0]; - this._totalResults = result.length - 1; - this._results = new Array(this._totalResults); - for (let i = 0; i < this._totalResults; ++i) { - this._results[i] = new Record(this._header, result[i + 1]); - } - } - } - - getHeader() { - return this._header; - } - - hasNext() { - return this._position < this._totalResults; - } - - next() { - return this._results[this._position++]; - } - - getStatistics() { - return this._statistics; - } + constructor(resp) { + this._position = 0; + this._statistics = new Statistics(resp[1]); + + let result = resp[0]; + + // Empty result set + if (result === null || result.length === 0) { + this._header = []; + this._totalResults = 0; + this._results = []; + } else { + this._header = result[0]; + this._totalResults = result.length - 1; + this._results = new Array(this._totalResults); + for (let i = 0; i < this._totalResults; ++i) { + this._results[i] = new Record(this._header, result[i + 1]); + } + } + } + + getHeader() { + return this._header; + } + + hasNext() { + return this._position < this._totalResults; + } + + next() { + return this._results[this._position++]; + } + + getStatistics() { + return this._statistics; + } } export default ResultSet; diff --git a/src/statistics.js b/src/statistics.js index dec6372cce..d004971d82 100644 --- a/src/statistics.js +++ b/src/statistics.js @@ -1,62 +1,62 @@ import Label from "./label"; class Statistics { - constructor(raw) { - this._raw = raw; - } - - getStringValue(label) { - return this.getStatistics()[label]; - } - - /** - * Return the query statistics - * - * @return statistics object - */ - getStatistics() { - if (!this._statistics) { - this._statistics = {}; - for (let row of this._raw) { - let touple = row.split(":"); - this._statistics[touple[0]] = touple[1].trim(); - } - } - return this._statistics; - } - - getIntValue(label) { - let value = this.getStringValue(label); - return value ? parseInt(value) : 0; - } - - nodesCreated() { - return this.getIntValue(Label.NODES_CREATED); - } - - nodesDeleted() { - return this.getIntValue(Label.NODES_DELETED); - } - - labelsAdded() { - return this.getIntValue(Label.LABELS_ADDED); - } - - relationshipsDeleted() { - return this.getIntValue(Label.RELATIONSHIPS_DELETED); - } - - relationshipsCreated() { - return this.getIntValue(Label.RELATIONSHIPS_CREATED); - } - - propertiesSet() { - return this.getIntValue(Label.PROPERTIES_SET); - } - - queryExecutionTime() { - return this.getIntValue(Label.QUERY_INTERNAL_EXECUTION_TIME); - } + constructor(raw) { + this._raw = raw; + } + + getStringValue(label) { + return this.getStatistics()[label]; + } + + /** + * Return the query statistics + * + * @return statistics object + */ + getStatistics() { + if (!this._statistics) { + this._statistics = {}; + for (let row of this._raw) { + let touple = row.split(":"); + this._statistics[touple[0]] = touple[1].trim(); + } + } + return this._statistics; + } + + getIntValue(label) { + let value = this.getStringValue(label); + return value ? parseInt(value) : 0; + } + + nodesCreated() { + return this.getIntValue(Label.NODES_CREATED); + } + + nodesDeleted() { + return this.getIntValue(Label.NODES_DELETED); + } + + labelsAdded() { + return this.getIntValue(Label.LABELS_ADDED); + } + + relationshipsDeleted() { + return this.getIntValue(Label.RELATIONSHIPS_DELETED); + } + + relationshipsCreated() { + return this.getIntValue(Label.RELATIONSHIPS_CREATED); + } + + propertiesSet() { + return this.getIntValue(Label.PROPERTIES_SET); + } + + queryExecutionTime() { + return this.getIntValue(Label.QUERY_INTERNAL_EXECUTION_TIME); + } } export default Statistics; diff --git a/test/redisGraphAPITest.js b/test/redisGraphAPITest.js index 1dae08e81a..10d878d341 100644 --- a/test/redisGraphAPITest.js +++ b/test/redisGraphAPITest.js @@ -5,119 +5,119 @@ import Label from "../src/label"; import RedisGraph from "../src/redisGraph"; describe("RedisGraph Test", () => { - const api = new RedisGraph("social"); + const api = new RedisGraph("social"); - it("test bring your client", () => { - return new RedisGraph("social", redis.createClient()); - }); + it("test bring your client", () => { + return new RedisGraph("social", redis.createClient()); + }); - it("test Create Node", () => { - api.deleteGraph("social"); - // Create a node - return api.query("CREATE ({name:'roi',age:32})").then(result => { - assert.ok(!result.hasNext()); - assert.equal(1, result.getStatistics().nodesCreated()); - assert.ifError( - result.getStatistics().getStringValue(Label.NODES_DELETED) - ); - assert.ifError( - result.getStatistics().getStringValue(Label.RELATIONSHIPS_CREATED) - ); - assert.ifError( - result.getStatistics().getStringValue(Label.RELATIONSHIPS_DELETED) - ); - assert.equal(2, result.getStatistics().propertiesSet()); - assert.ok( - result - .getStatistics() - .getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME) - ); - }); - }); + it("test Create Node", () => { + api.deleteGraph("social"); + // Create a node + return api.query("CREATE ({name:'roi',age:32})").then(result => { + assert.ok(!result.hasNext()); + assert.equal(1, result.getStatistics().nodesCreated()); + assert.ifError( + result.getStatistics().getStringValue(Label.NODES_DELETED) + ); + assert.ifError( + result.getStatistics().getStringValue(Label.RELATIONSHIPS_CREATED) + ); + assert.ifError( + result.getStatistics().getStringValue(Label.RELATIONSHIPS_DELETED) + ); + assert.equal(2, result.getStatistics().propertiesSet()); + assert.ok( + result + .getStatistics() + .getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME) + ); + }); + }); - it("test Create Labeled Node", () => { - api.deleteGraph("social"); - // Create a node with a label - return api.query("CREATE (:human{name:'danny',age:12})").then(result => { - assert.ok(!result.hasNext()); - assert.equal( - "1", - result.getStatistics().getStringValue(Label.NODES_CREATED) - ); - assert.equal( - "2", - result.getStatistics().getStringValue(Label.PROPERTIES_SET) - ); - assert.ok( - result - .getStatistics() - .getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME) - ); - }); - }); + it("test Create Labeled Node", () => { + api.deleteGraph("social"); + // Create a node with a label + return api.query("CREATE (:human{name:'danny',age:12})").then(result => { + assert.ok(!result.hasNext()); + assert.equal( + "1", + result.getStatistics().getStringValue(Label.NODES_CREATED) + ); + assert.equal( + "2", + result.getStatistics().getStringValue(Label.PROPERTIES_SET) + ); + assert.ok( + result + .getStatistics() + .getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME) + ); + }); + }); - it("test Connect Nodes", () => { - api.deleteGraph("social"); - // Create both source and destination nodes - let createResult1 = api.query("CREATE (:person{name:'roi',age:32})"); - let createResult2 = api.query("CREATE (:person{name:'amit',age:30})"); + it("test Connect Nodes", () => { + api.deleteGraph("social"); + // Create both source and destination nodes + let createResult1 = api.query("CREATE (:person{name:'roi',age:32})"); + let createResult2 = api.query("CREATE (:person{name:'amit',age:30})"); - // Connect source and destination nodes. - return api - .query( - "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)" - ) - .then(matchResult => { - assert.ok(!matchResult.hasNext()); - assert.ifError( - matchResult.getStatistics().getStringValue(Label.NODES_CREATED) - ); - assert.ifError( - matchResult.getStatistics().getStringValue(Label.PROPERTIES_SET) - ); - assert.equal(1, matchResult.getStatistics().relationshipsCreated()); - assert.equal(0, matchResult.getStatistics().relationshipsDeleted()); - assert.ok( - matchResult - .getStatistics() - .getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME) - ); - }); - }); + // Connect source and destination nodes. + return api + .query( + "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)" + ) + .then(matchResult => { + assert.ok(!matchResult.hasNext()); + assert.ifError( + matchResult.getStatistics().getStringValue(Label.NODES_CREATED) + ); + assert.ifError( + matchResult.getStatistics().getStringValue(Label.PROPERTIES_SET) + ); + assert.equal(1, matchResult.getStatistics().relationshipsCreated()); + assert.equal(0, matchResult.getStatistics().relationshipsDeleted()); + assert.ok( + matchResult + .getStatistics() + .getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME) + ); + }); + }); - it("test Query", () => { - api.deleteGraph("social"); - // Create both source and destination nodes - return api - .query("CREATE (:qhuman{name:'roi',age:32})") - .then(create1Result => { - return api.query("CREATE (:qhuman{name:'amit',age:30})"); - }) - .then(create2Result => { - // Connect source and destination nodes. - return api.query( - "MATCH (a:qhuman), (b:qhuman) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)" - ); - }) - .then(connectResult => { - // Query - return api.query("MATCH (a:qhuman)-[knows]->(:qhuman) RETURN a"); - }) - .then(resultSet => { - assert.ok(resultSet.hasNext()); - assert.equal(0, resultSet.getStatistics().nodesCreated()); - assert.equal(0, resultSet.getStatistics().propertiesSet()); - assert.equal(0, resultSet.getStatistics().relationshipsCreated()); - assert.equal(0, resultSet.getStatistics().relationshipsDeleted()); - assert.ok( - resultSet - .getStatistics() - .getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME) - ); + it("test Query", () => { + api.deleteGraph("social"); + // Create both source and destination nodes + return api + .query("CREATE (:qhuman{name:'roi',age:32})") + .then(create1Result => { + return api.query("CREATE (:qhuman{name:'amit',age:30})"); + }) + .then(create2Result => { + // Connect source and destination nodes. + return api.query( + "MATCH (a:qhuman), (b:qhuman) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)" + ); + }) + .then(connectResult => { + // Query + return api.query("MATCH (a:qhuman)-[knows]->(:qhuman) RETURN a"); + }) + .then(resultSet => { + assert.ok(resultSet.hasNext()); + assert.equal(0, resultSet.getStatistics().nodesCreated()); + assert.equal(0, resultSet.getStatistics().propertiesSet()); + assert.equal(0, resultSet.getStatistics().relationshipsCreated()); + assert.equal(0, resultSet.getStatistics().relationshipsDeleted()); + assert.ok( + resultSet + .getStatistics() + .getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME) + ); - let record = resultSet.next(); - assert.equal("roi", record.getString(1)); - assert.equal("32.000000", record.getString(0)); - }); - }); + let record = resultSet.next(); + assert.equal("roi", record.getString(1)); + assert.equal("32.000000", record.getString(0)); + }); + }); }); From b4499bdeb76e304f58f425ae9163d9fb00113b24 Mon Sep 17 00:00:00 2001 From: David Leonardi Date: Fri, 11 Jan 2019 20:59:39 +0100 Subject: [PATCH 3/5] back to commonjs --- package.json | 7 ++----- src/index.js | 14 ++++++-------- src/label.js | 4 ++-- src/record.js | 2 +- src/redisGraph.js | 8 ++++---- src/resultSet.js | 6 +++--- src/statistics.js | 4 ++-- test/redisGraphAPITest.js | 9 ++++----- 8 files changed, 24 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 6de7858097..fc96f95fd5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "redisgraph.js", - "version": "1.0.2", + "version": "1.0.3", "description": "Connect to RedisGraph 1.0.0 and up from JavaScript", "author": "RedisLabs", "license": "BSD 3", @@ -12,13 +12,10 @@ "redis": "^2.8.0" }, "devDependencies": { - "babel-core": "^6.26.3", - "babel-preset-latest": "^6.24.1", - "babel-preset-stage-3": "^6.24.1", "mocha": "^5.2.0" }, "scripts": { - "test": "mocha -R spec --require babel-core/register --exit" + "test": "mocha --exit" }, "main": "./src/index.js" } diff --git a/src/index.js b/src/index.js index 01dce84ced..e686f1205e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,15 +1,13 @@ -import Record from "./record"; -import RedisGraph from "./redisGraph"; -import ResultSet from "./resultSet"; -import Statistics from "./statistics"; -import Label from "./label"; +const Record = require("./record"); +const RedisGraph = require("./redisGraph"); +const ResultSet = require("./resultSet"); +const Statistics = require("./statistics"); +const Label = require("./label"); -const redisGraph = { +module.exports = { Record: Record, RedisGraph: RedisGraph, ResultSet: ResultSet, Statistics: Statistics, Label: Label }; - -export default redisGraph; diff --git a/src/label.js b/src/label.js index b0329fed89..33e1565dc2 100644 --- a/src/label.js +++ b/src/label.js @@ -1,7 +1,7 @@ /** * Different Statistics labels */ -const Label = Object.freeze({ +var Label = Object.freeze({ LABELS_ADDED: "Labels added", NODES_CREATED: "Nodes created", NODES_DELETED: "Nodes deleted", @@ -11,4 +11,4 @@ const Label = Object.freeze({ QUERY_INTERNAL_EXECUTION_TIME: "Query internal execution time" }); -export default Label; +module.exports = Label; diff --git a/src/record.js b/src/record.js index 6f4a27668d..d661e7e708 100644 --- a/src/record.js +++ b/src/record.js @@ -32,4 +32,4 @@ class Record { } } -export default Record; +module.exports = Record; diff --git a/src/redisGraph.js b/src/redisGraph.js index 3b1f1137da..cb42b926b8 100644 --- a/src/redisGraph.js +++ b/src/redisGraph.js @@ -1,6 +1,6 @@ -import redis from "redis"; -import util from "util"; -import ResultSet from "./ResultSet"; +const redis = require("redis"); +const util = require("util"); +const ResultSet = require("./ResultSet"); /** * RedisGraph client @@ -50,4 +50,4 @@ class RedisGraph { } } -export default RedisGraph; +module.exports = RedisGraph; diff --git a/src/resultSet.js b/src/resultSet.js index 2925ed0b2e..52c90aee89 100644 --- a/src/resultSet.js +++ b/src/resultSet.js @@ -1,5 +1,5 @@ -import Statistics from "./statistics"; -import Record from "./record"; +const Statistics = require("./statistics"); +const Record = require("./record"); /** * Hold a query result @@ -43,4 +43,4 @@ class ResultSet { } } -export default ResultSet; +module.exports = ResultSet; diff --git a/src/statistics.js b/src/statistics.js index d004971d82..09a733461e 100644 --- a/src/statistics.js +++ b/src/statistics.js @@ -1,4 +1,4 @@ -import Label from "./label"; +const Label = require("./label"); class Statistics { constructor(raw) { @@ -59,4 +59,4 @@ class Statistics { } } -export default Statistics; +module.exports = Statistics; diff --git a/test/redisGraphAPITest.js b/test/redisGraphAPITest.js index 10d878d341..cab6589f2b 100644 --- a/test/redisGraphAPITest.js +++ b/test/redisGraphAPITest.js @@ -1,8 +1,7 @@ -import { describe, before, it } from "mocha"; -import assert from "assert"; -import redis from "redis"; -import Label from "../src/label"; -import RedisGraph from "../src/redisGraph"; +const assert = require("assert"); +const redis = require("redis"); +const Label = require("../src/label"); +const RedisGraph = require("../src/redisGraph"); describe("RedisGraph Test", () => { const api = new RedisGraph("social"); From d6360009f00f1723d80f7572fdec411c4d8abd16 Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Sun, 3 Feb 2019 11:36:49 +0200 Subject: [PATCH 4/5] fix JSON --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8088919a12..237dd4f17a 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,6 @@ }, "scripts": { "test": "mocha --exit" - } + }, "main": "./src/index.js" } From bcfaf042fdc784aa5a2273e51f0b8f4e0b4aaabe Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Sun, 3 Feb 2019 11:48:22 +0200 Subject: [PATCH 5/5] fix example --- .babelrc | 4 ---- .editorconfig | 19 ------------------- .gitignore | 1 + examples/package.json | 15 +++++++++++++++ ...isGraphExample.js => redisGraphExample.js} | 2 +- index.js | 13 +++++++++++++ package.json | 4 ++-- src/index.js | 13 ------------- src/redisGraph.js | 6 +++--- src/resultSet.js | 4 ++-- src/statistics.js | 2 +- test/redisGraphAPITest.js | 10 +++++----- 12 files changed, 43 insertions(+), 50 deletions(-) delete mode 100644 .babelrc delete mode 100644 .editorconfig create mode 100644 examples/package.json rename examples/{RedisGraphExample.js => redisGraphExample.js} (91%) create mode 100644 index.js delete mode 100644 src/index.js diff --git a/.babelrc b/.babelrc deleted file mode 100644 index b592f4060f..0000000000 --- a/.babelrc +++ /dev/null @@ -1,4 +0,0 @@ -{ - "presets": ["es2015", "stage-3"], - "plugins": [] -} diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index df3a0a8c24..0000000000 --- a/.editorconfig +++ /dev/null @@ -1,19 +0,0 @@ -# EditorConfig is awesome: https://EditorConfig.org - -# top-most EditorConfig file -root = true - -# Unix-style newlines with a newline ending every file -[*] -end_of_line = lf -insert_final_newline = true - -# Matches multiple files with brace expansion notation -# Set default charset -[*.{js}] -charset = utf-8 - -# Indentation override for all JS -[**.js] -indent_style = tab -indent_size = 2 diff --git a/.gitignore b/.gitignore index bc446e8f38..e171686cee 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .project package-lock.json yarn.lock +/examples/node_modules diff --git a/examples/package.json b/examples/package.json new file mode 100644 index 0000000000..0aa40c1593 --- /dev/null +++ b/examples/package.json @@ -0,0 +1,15 @@ +{ + "name": "redisgraph-example", + "version": "1.0.0", + "description": "Example using redisgraph.js", + "author": "RedisLabs", + "license": "BSD 3", + "repository": { + "type": "git", + "url": "git://github.com/redislabs/redisgraph.js.git" + }, + "dependencies": { + "redisgraph.js": "^1.1.0" + }, + "main": "redisGraphExample.js" +} diff --git a/examples/RedisGraphExample.js b/examples/redisGraphExample.js similarity index 91% rename from examples/RedisGraphExample.js rename to examples/redisGraphExample.js index ee25c3afde..7097923bba 100644 --- a/examples/RedisGraphExample.js +++ b/examples/redisGraphExample.js @@ -1,4 +1,4 @@ -const RedisGraph = require("../src/redisGraph"); +const RedisGraph = require("redisgraph.js").RedisGraph; let graph = new RedisGraph("social"); diff --git a/index.js b/index.js new file mode 100644 index 0000000000..8264408085 --- /dev/null +++ b/index.js @@ -0,0 +1,13 @@ +const Record = require("./src/record"), + RedisGraph = require("./src/redisGraph"), + ResultSet = require("./src/resultSet"), + Statistics = require("./src/statistics"), + Label = require("./label"); + +module.exports = { + Record: Record, + RedisGraph: RedisGraph, + ResultSet: ResultSet, + Statistics: Statistics, + Label: Label +}; diff --git a/package.json b/package.json index 237dd4f17a..db8aa00456 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "redisgraph.js", - "version": "1.0.4", + "version": "1.1.0", "description": "Connect to RedisGraph 1.0.0 and up from JavaScript", "author": "RedisLabs", "license": "BSD 3", @@ -17,5 +17,5 @@ "scripts": { "test": "mocha --exit" }, - "main": "./src/index.js" + "main": "index.js" } diff --git a/src/index.js b/src/index.js deleted file mode 100644 index e686f1205e..0000000000 --- a/src/index.js +++ /dev/null @@ -1,13 +0,0 @@ -const Record = require("./record"); -const RedisGraph = require("./redisGraph"); -const ResultSet = require("./resultSet"); -const Statistics = require("./statistics"); -const Label = require("./label"); - -module.exports = { - Record: Record, - RedisGraph: RedisGraph, - ResultSet: ResultSet, - Statistics: Statistics, - Label: Label -}; diff --git a/src/redisGraph.js b/src/redisGraph.js index cb42b926b8..4dbab581c4 100644 --- a/src/redisGraph.js +++ b/src/redisGraph.js @@ -1,6 +1,6 @@ -const redis = require("redis"); -const util = require("util"); -const ResultSet = require("./ResultSet"); +const redis = require("redis"), + util = require("util"), + ResultSet = require("./resultSet"); /** * RedisGraph client diff --git a/src/resultSet.js b/src/resultSet.js index 52c90aee89..f496a9c871 100644 --- a/src/resultSet.js +++ b/src/resultSet.js @@ -1,5 +1,5 @@ -const Statistics = require("./statistics"); -const Record = require("./record"); +const Statistics = require("./statistics"), + Record = require("./record"); /** * Hold a query result diff --git a/src/statistics.js b/src/statistics.js index 9b8b537f64..711ee7d36f 100644 --- a/src/statistics.js +++ b/src/statistics.js @@ -61,7 +61,7 @@ class Statistics { } queryExecutionTime() { - return this.getIntValue(Label.QUERY_INTERNAL_EXECUTION_TIME); + return this.getFloatValue(Label.QUERY_INTERNAL_EXECUTION_TIME); } } diff --git a/test/redisGraphAPITest.js b/test/redisGraphAPITest.js index ae0fabd4d4..40c346021e 100644 --- a/test/redisGraphAPITest.js +++ b/test/redisGraphAPITest.js @@ -1,10 +1,10 @@ -const assert = require("assert"); -const redis = require("redis"); -const Label = require("../src/label"); -const RedisGraph = require("../src/redisGraph"); +const assert = require("assert"), + redis = require("redis"), + Label = require("../src/label"), + RedisGraph = require("../src/redisGraph"); describe('RedisGraphAPI Test', () =>{ - const api = new RedisGraphAPI("social"); + const api = new RedisGraph("social"); beforeEach( () => { return api.deleteGraph().catch(()=>{});