|
| 1 | +const redis = require("redis"), |
| 2 | + util = require("util"), |
| 3 | + ResultSet = require("./resultSet"); |
| 4 | + |
| 5 | +/** |
| 6 | + * RedisGraph client |
| 7 | + */ |
| 8 | +class Graph { |
| 9 | + /** |
| 10 | + * Creates a client to a specific graph running on the specific host/post |
| 11 | + * See: node_redis for more options on createClient |
| 12 | + * |
| 13 | + * @param graphId the graph id |
| 14 | + * @param host Redis host or node_redis client |
| 15 | + * @param port Redis port |
| 16 | + * @param options node_redis options |
| 17 | + */ |
| 18 | + constructor(graphId, host, port, options) { |
| 19 | + this._graphId = graphId; // Graph ID |
| 20 | + this._labels = []; // List of node labels. |
| 21 | + this._relationshipTypes = []; // List of relation types. |
| 22 | + this._properties = []; // List of properties. |
| 23 | + |
| 24 | + this._labelsPromise = undefined; // used as a synchronization mechanizom for labels retrival |
| 25 | + this._propertyPromise = undefined; // used as a synchronization mechanizom for property names retrival |
| 26 | + this._relationshipPromise = undefined; // used as a synchronization mechanizom for relationship types retrival |
| 27 | + |
| 28 | + let client = |
| 29 | + host instanceof redis.RedisClient |
| 30 | + ? host |
| 31 | + : redis.createClient.apply(redis, [].slice.call(arguments, 1)); |
| 32 | + this._sendCommand = util.promisify(client.send_command).bind(client); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * auxilary function to extract string(s) data from procedures such as: |
| 37 | + * db.labels, db.propertyKeys and db.relationshipTypes |
| 38 | + * @param resultSet - a procedure result set |
| 39 | + */ |
| 40 | + _extractStrings(resultSet) { |
| 41 | + var strings = []; |
| 42 | + while (resultSet.hasNext()) { |
| 43 | + strings.push(resultSet.next().getString(0)); |
| 44 | + } |
| 45 | + return strings; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Execute a Cypher query (async) |
| 50 | + * |
| 51 | + * @param query Cypher query |
| 52 | + * @return a promise contains a result set |
| 53 | + */ |
| 54 | + async query(query) { |
| 55 | + var res = await this._sendCommand("graph.QUERY", [this._graphId, query, "--compact"]); |
| 56 | + var resultSet = new ResultSet(this); |
| 57 | + return resultSet.parseResponse(res); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Deletes the entire graph (async) |
| 62 | + * |
| 63 | + * @return a promise contains the delete operation running time statistics |
| 64 | + */ |
| 65 | + async deleteGraph() { |
| 66 | + var res = await this._sendCommand("graph.DELETE", [this._graphId]); |
| 67 | + //clear internal graph state |
| 68 | + this._labels = []; |
| 69 | + this._relationshipTypes = []; |
| 70 | + this._properties = []; |
| 71 | + var resultSet = new ResultSet(this); |
| 72 | + return resultSet.parseResponse(res); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Calls procedure |
| 77 | + * |
| 78 | + * @param procedure Procedure to call |
| 79 | + * @param args Arguments to pass |
| 80 | + * @param y Yield outputs |
| 81 | + * @return a promise contains the procedure result set data |
| 82 | + */ |
| 83 | + callProcedure(procedure, args = new Array(), y = new Array()) { |
| 84 | + let q = "CALL " + procedure + "(" + args.join(',') + ")" + y.join(' '); |
| 85 | + return this.query(q); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Retrieves all labels in graph. |
| 90 | + */ |
| 91 | + async labels() { |
| 92 | + if (this._labelsPromise == undefined) { |
| 93 | + this._labelsPromise = this.callProcedure("db.labels").then(response => { |
| 94 | + return this._extractStrings(response); |
| 95 | + }) |
| 96 | + this._labels = await (this._labelsPromise); |
| 97 | + this._labelsPromise = undefined; |
| 98 | + } |
| 99 | + else { |
| 100 | + await this._labelsPromise; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Retrieves all relationship types in graph. |
| 106 | + */ |
| 107 | + async relationshipTypes() { |
| 108 | + if (this._relationshipPromise == undefined) { |
| 109 | + this._relationshipPromise = this.callProcedure("db.relationshipTypes").then(response => { |
| 110 | + return this._extractStrings(response); |
| 111 | + }); |
| 112 | + this._relationshipTypes = await (this._relationshipPromise); |
| 113 | + this._relationshipPromise = undefined; |
| 114 | + } |
| 115 | + else { |
| 116 | + await this._relationshipPromise; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Retrieves all properties in graph. |
| 122 | + */ |
| 123 | + async propertyKeys() { |
| 124 | + if (this._propertyPromise == undefined) { |
| 125 | + this._propertyPromise = this.callProcedure("db.propertyKeys").then(response => { |
| 126 | + return this._extractStrings(response); |
| 127 | + }) |
| 128 | + this._properties = await this._propertyPromise; |
| 129 | + this._propertyPromise = undefined; |
| 130 | + } |
| 131 | + else{ |
| 132 | + await this._propertyPromise; |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Retrieves label by ID. |
| 140 | + * |
| 141 | + * @param id internal ID of label. |
| 142 | + * @return String label. |
| 143 | + */ |
| 144 | + getLabel(id) { |
| 145 | + return this._labels[id]; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Retrive all the labels from the graph and returns the wanted label |
| 150 | + * @param id internal ID of label. |
| 151 | + * @return String label. |
| 152 | + */ |
| 153 | + async fetchAndGetLabel(id) { |
| 154 | + await this.labels(); |
| 155 | + return this._labels[id]; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Retrieves relationship type by ID. |
| 160 | + * |
| 161 | + * @param id internal ID of relationship type. |
| 162 | + * @return String relationship type. |
| 163 | + */ |
| 164 | + getRelationship(id) { |
| 165 | + return this._relationshipTypes[id]; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Retrives al the relationshipe types from the graph, and returns the wanted type |
| 170 | + * @param id internal ID of relationship type. |
| 171 | + * @return String relationship type. |
| 172 | + */ |
| 173 | + async fetchAndGetRelationship(id) { |
| 174 | + await this.relationshipTypes(); |
| 175 | + return this._relationshipTypes[id]; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Retrieves property name by ID. |
| 180 | + * |
| 181 | + * @param id internal ID of property. |
| 182 | + * @return String property. |
| 183 | + */ |
| 184 | + getProperty(id) { |
| 185 | + return this._properties[id]; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Retrives al the properties from the graph, and returns the wanted property |
| 190 | + * @param id internal ID of property. |
| 191 | + * @return String property. |
| 192 | + */ |
| 193 | + async fetchAndGetProperty(id) { |
| 194 | + await this.propertyKeys(); |
| 195 | + return this._properties[id]; |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +module.exports = Graph; |
0 commit comments