Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor redisgraph.js to Typescript #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
package-lock.json
yarn.lock
/examples/node_modules
dist
13 changes: 0 additions & 13 deletions index.js

This file was deleted.

5 changes: 5 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./src/record"
export * from "./src/redisGraph"
export * from "./src/resultSet"
export * from "./src/statistics"
export * from "./src/label"
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
"redis": "^2.8.0"
},
"devDependencies": {
"mocha": "^5.2.0"
"@types/mocha": "^5.2.6",
"@types/redis": "^2.8.10",
"mocha": "^5.2.0",
"ts-mocha": "^2.0.0",
"typescript": "^3.3.3"
},
"scripts": {
"test": "mocha --exit"
"test": "ts-mocha test/*.ts --exit"
},
"main": "index.js"
"main": "dist/index.js",
"types": "dist/index.d.js"
}
4 changes: 1 addition & 3 deletions src/label.js → src/label.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Different Statistics labels
*/
var Label = Object.freeze({
export const Label = Object.freeze({
LABELS_ADDED: "Labels added",
NODES_CREATED: "Nodes created",
NODES_DELETED: "Nodes deleted",
Expand All @@ -10,5 +10,3 @@ var Label = Object.freeze({
RELATIONSHIPS_CREATED: "Relationships created",
QUERY_INTERNAL_EXECUTION_TIME: "Query internal execution time"
});

module.exports = Label;
21 changes: 13 additions & 8 deletions src/record.js → src/record.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
type Header = string[]
type Values = string[]


/**
* Hold a query record
*/
class Record {
constructor(header, values) {
export class Record {
private _header: Header;
private _values: Values;

constructor(header: Header, values: Values) {
this._header = header;
this._values = values;
}

getString(key) {
let index = key;
getString(key: string|number) {
let index: string|number = key;
if (typeof key === "string") {
index = this._header.indexOf(key);
}
return this._values[index];
return this._values[Number(index)];
}

keys() {
Expand All @@ -23,13 +30,11 @@ class Record {
return this._values;
}

containsKey(key) {
containsKey(key: string) {
return this._header.includes(key);
}

size() {
return this._header.length;
}
}

module.exports = Record;
18 changes: 9 additions & 9 deletions src/redisGraph.js → src/redisGraph.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const redis = require("redis"),
util = require("util"),
ResultSet = require("./resultSet");
import redis, { ClientOpts, RedisClient } from "redis";
import util from "util";
import { ResultSet } from "./resultSet";

/**
* RedisGraph client
*/
class RedisGraph {
export class RedisGraph {
private _graphId: string;
private _sendCommand: (arg1: string, options: any) => Promise<void>;
/**
* Creates a client to a specific graph running on the specific host/post
* See: node_redis for more options on createClient
Expand All @@ -15,12 +17,12 @@ class RedisGraph {
* @param port Redis port
* @param options node_redis options
*/
constructor(graphId, host, port, options) {
constructor(graphId: string, host?: string | RedisClient, port=6379, options?: ClientOpts) {
this._graphId = graphId;
let client =
host instanceof redis.RedisClient
? host
: redis.createClient.apply(redis, [].slice.call(arguments, 1));
: redis.createClient(port, host, options);
this._sendCommand = util.promisify(client.send_command).bind(client);
}

Expand All @@ -30,7 +32,7 @@ class RedisGraph {
* @param query Cypher query
* @return a result set
*/
query(query) {
query(query: string) {
return this._sendCommand("graph.QUERY", [this._graphId, query]).then(
res => {
return new ResultSet(res);
Expand All @@ -49,5 +51,3 @@ class RedisGraph {
});
}
}

module.exports = RedisGraph;
19 changes: 13 additions & 6 deletions src/resultSet.js → src/resultSet.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
const Statistics = require("./statistics"),
Record = require("./record");
import { Statistics } from "./statistics"
import { Record } from "./record";

type Header = string[]

/**
* Hold a query result
*/
class ResultSet {
constructor(resp) {
export class ResultSet {
private _position: number
private _header: Header
private _totalResults: number
private _results: Record[]
private _statistics: Statistics

constructor(resp: any) {
this._position = 0;
this._statistics = new Statistics(resp[1]);

Expand All @@ -20,6 +28,7 @@ class ResultSet {
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]);
}
Expand All @@ -42,5 +51,3 @@ class ResultSet {
return this._statistics;
}
}

module.exports = ResultSet;
16 changes: 8 additions & 8 deletions src/statistics.js → src/statistics.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const Label = require("./label");
import { Label } from "./label";

class Statistics {
constructor(raw) {
export class Statistics {
private _raw: any
private _statistics: any
constructor(raw: any) {
this._raw = raw;
}

getStringValue(label) {
getStringValue(label: string) {
return this.getStatistics()[label];
}

Expand All @@ -25,13 +27,13 @@ class Statistics {
return this._statistics;
}

getIntValue(label) {
getIntValue(label: string) {
let value = this.getStringValue(label);
return value ? parseInt(value) : 0;
}


getFloatValue(label) {
getFloatValue(label: string) {
let value = this.getStringValue(label);
return value ? parseFloat(value) : 0;
}
Expand Down Expand Up @@ -64,5 +66,3 @@ class Statistics {
return this.getFloatValue(Label.QUERY_INTERNAL_EXECUTION_TIME);
}
}

module.exports = Statistics;
24 changes: 12 additions & 12 deletions test/redisGraphAPITest.js → test/redisGraphAPITest.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const assert = require("assert"),
redis = require("redis"),
Label = require("../src/label"),
RedisGraph = require("../src/redisGraph");
import assert from "assert";
import redis from "redis";
import { Label } from "../src/label";
import { RedisGraph } from "../src/redisGraph";

describe('RedisGraphAPI Test', () =>{
const api = new RedisGraph("social");

beforeEach( () => {
return api.deleteGraph().catch(()=>{});
});
Expand All @@ -30,7 +30,7 @@ describe('RedisGraphAPI Test', () =>{
);
assert.equal(2, result.getStatistics().propertiesSet());
assert.ok( result.getStatistics().queryExecutionTime()); // not 0
assert.ok(result.getStatistics().getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME)); // exsits
assert.ok(result.getStatistics().getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME)); // exsits
});
});

Expand Down Expand Up @@ -83,9 +83,9 @@ describe('RedisGraphAPI Test', () =>{
});

it('test Query', () => {
// Create both source and destination nodes
// Create both source and destination nodes
return api.query("CREATE (:qhuman{name:'roi',age:32})")
.then( (create1Result) => {
.then( (create1Result) => {
return api.query("CREATE (:qhuman{name:'amit',age:30})");
})
.then( (create2Result) => {
Expand All @@ -96,23 +96,23 @@ describe('RedisGraphAPI Test', () =>{
// Query
return api.query("MATCH (a:qhuman)-[knows]->(:qhuman) RETURN a");
})
.then( (resultSet) => {
.then( (resultSet) => {
assert.ok(resultSet.hasNext());
assert.equal(0, resultSet.getStatistics().nodesCreated());
assert.equal(0, resultSet.getStatistics().nodesDeleted());
assert.equal(0, resultSet.getStatistics().labelsAdded());
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));
assert.ok(resultSet.getStatistics().getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME));

assert.deepStrictEqual( [ 'a.age', 'a.name' ], resultSet.getHeader());

let record = resultSet.next();
assert.equal( "roi", record.getString(1));
assert.equal( "roi", record.getString("a.name"));
assert.equal( "32.000000", record.getString(0));

assert.deepStrictEqual( [ 'a.age', 'a.name' ], record.keys());
assert.deepStrictEqual( [ '32.000000', 'roi' ], record.values());
assert.equal( false, record.containsKey("aa"));
Expand Down
17 changes: 17 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
/* Basic Options */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "./dist", /* Redirect output structure to the directory. */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */

/* Module Resolution Options */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
},
"files": ["index.ts"]
}