graph.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. "use strict";
  2. var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  3. if (kind === "m") throw new TypeError("Private method is not writable");
  4. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
  5. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
  6. return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
  7. };
  8. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  9. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  10. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  11. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  12. };
  13. var _Graph_instances, _Graph_client, _Graph_name, _Graph_metadata, _Graph_setMetadataPromise, _Graph_updateMetadata, _Graph_setMetadata, _Graph_cleanMetadataArray, _Graph_getMetadata, _Graph_getMetadataAsync, _Graph_parseReply, _Graph_parseValue, _Graph_parseEdge, _Graph_parseNode, _Graph_parseProperties;
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. // https://github.com/RedisGraph/RedisGraph/blob/master/src/resultset/formatters/resultset_formatter.h#L20
  16. var GraphValueTypes;
  17. (function (GraphValueTypes) {
  18. GraphValueTypes[GraphValueTypes["UNKNOWN"] = 0] = "UNKNOWN";
  19. GraphValueTypes[GraphValueTypes["NULL"] = 1] = "NULL";
  20. GraphValueTypes[GraphValueTypes["STRING"] = 2] = "STRING";
  21. GraphValueTypes[GraphValueTypes["INTEGER"] = 3] = "INTEGER";
  22. GraphValueTypes[GraphValueTypes["BOOLEAN"] = 4] = "BOOLEAN";
  23. GraphValueTypes[GraphValueTypes["DOUBLE"] = 5] = "DOUBLE";
  24. GraphValueTypes[GraphValueTypes["ARRAY"] = 6] = "ARRAY";
  25. GraphValueTypes[GraphValueTypes["EDGE"] = 7] = "EDGE";
  26. GraphValueTypes[GraphValueTypes["NODE"] = 8] = "NODE";
  27. GraphValueTypes[GraphValueTypes["PATH"] = 9] = "PATH";
  28. GraphValueTypes[GraphValueTypes["MAP"] = 10] = "MAP";
  29. GraphValueTypes[GraphValueTypes["POINT"] = 11] = "POINT";
  30. })(GraphValueTypes || (GraphValueTypes = {}));
  31. class Graph {
  32. constructor(client, name) {
  33. _Graph_instances.add(this);
  34. _Graph_client.set(this, void 0);
  35. _Graph_name.set(this, void 0);
  36. _Graph_metadata.set(this, void 0);
  37. _Graph_setMetadataPromise.set(this, void 0);
  38. __classPrivateFieldSet(this, _Graph_client, client, "f");
  39. __classPrivateFieldSet(this, _Graph_name, name, "f");
  40. }
  41. async query(query, options) {
  42. return __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_parseReply).call(this, await __classPrivateFieldGet(this, _Graph_client, "f").graph.query(__classPrivateFieldGet(this, _Graph_name, "f"), query, options, true));
  43. }
  44. async roQuery(query, options) {
  45. return __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_parseReply).call(this, await __classPrivateFieldGet(this, _Graph_client, "f").graph.roQuery(__classPrivateFieldGet(this, _Graph_name, "f"), query, options, true));
  46. }
  47. }
  48. _Graph_client = new WeakMap(), _Graph_name = new WeakMap(), _Graph_metadata = new WeakMap(), _Graph_setMetadataPromise = new WeakMap(), _Graph_instances = new WeakSet(), _Graph_updateMetadata = function _Graph_updateMetadata() {
  49. __classPrivateFieldSet(this, _Graph_setMetadataPromise, __classPrivateFieldGet(this, _Graph_setMetadataPromise, "f") ?? __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_setMetadata).call(this)
  50. .finally(() => __classPrivateFieldSet(this, _Graph_setMetadataPromise, undefined, "f")), "f");
  51. return __classPrivateFieldGet(this, _Graph_setMetadataPromise, "f");
  52. }, _Graph_setMetadata =
  53. // DO NOT use directly, use #updateMetadata instead
  54. async function _Graph_setMetadata() {
  55. const [labels, relationshipTypes, propertyKeys] = await Promise.all([
  56. __classPrivateFieldGet(this, _Graph_client, "f").graph.roQuery(__classPrivateFieldGet(this, _Graph_name, "f"), 'CALL db.labels()'),
  57. __classPrivateFieldGet(this, _Graph_client, "f").graph.roQuery(__classPrivateFieldGet(this, _Graph_name, "f"), 'CALL db.relationshipTypes()'),
  58. __classPrivateFieldGet(this, _Graph_client, "f").graph.roQuery(__classPrivateFieldGet(this, _Graph_name, "f"), 'CALL db.propertyKeys()')
  59. ]);
  60. __classPrivateFieldSet(this, _Graph_metadata, {
  61. labels: __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_cleanMetadataArray).call(this, labels.data),
  62. relationshipTypes: __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_cleanMetadataArray).call(this, relationshipTypes.data),
  63. propertyKeys: __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_cleanMetadataArray).call(this, propertyKeys.data)
  64. }, "f");
  65. return __classPrivateFieldGet(this, _Graph_metadata, "f");
  66. }, _Graph_cleanMetadataArray = function _Graph_cleanMetadataArray(arr) {
  67. return arr.map(([value]) => value);
  68. }, _Graph_getMetadata = function _Graph_getMetadata(key, id) {
  69. return __classPrivateFieldGet(this, _Graph_metadata, "f")?.[key][id] ?? __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_getMetadataAsync).call(this, key, id);
  70. }, _Graph_getMetadataAsync =
  71. // DO NOT use directly, use #getMetadata instead
  72. async function _Graph_getMetadataAsync(key, id) {
  73. const value = (await __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_updateMetadata).call(this))[key][id];
  74. if (value === undefined)
  75. throw new Error(`Cannot find value from ${key}[${id}]`);
  76. return value;
  77. }, _Graph_parseReply = async function _Graph_parseReply(reply) {
  78. if (!reply.data)
  79. return reply;
  80. const promises = [], parsed = {
  81. metadata: reply.metadata,
  82. data: reply.data.map((row) => {
  83. const data = {};
  84. for (let i = 0; i < row.length; i++) {
  85. data[reply.headers[i][1]] = __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_parseValue).call(this, row[i], promises);
  86. }
  87. return data;
  88. })
  89. };
  90. if (promises.length)
  91. await Promise.all(promises);
  92. return parsed;
  93. }, _Graph_parseValue = function _Graph_parseValue([valueType, value], promises) {
  94. switch (valueType) {
  95. case GraphValueTypes.NULL:
  96. return null;
  97. case GraphValueTypes.STRING:
  98. case GraphValueTypes.INTEGER:
  99. return value;
  100. case GraphValueTypes.BOOLEAN:
  101. return value === 'true';
  102. case GraphValueTypes.DOUBLE:
  103. return parseFloat(value);
  104. case GraphValueTypes.ARRAY:
  105. return value.map(x => __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_parseValue).call(this, x, promises));
  106. case GraphValueTypes.EDGE:
  107. return __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_parseEdge).call(this, value, promises);
  108. case GraphValueTypes.NODE:
  109. return __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_parseNode).call(this, value, promises);
  110. case GraphValueTypes.PATH:
  111. return {
  112. nodes: value[0][1].map(([, node]) => __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_parseNode).call(this, node, promises)),
  113. edges: value[1][1].map(([, edge]) => __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_parseEdge).call(this, edge, promises))
  114. };
  115. case GraphValueTypes.MAP:
  116. const map = {};
  117. for (let i = 0; i < value.length; i++) {
  118. map[value[i++]] = __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_parseValue).call(this, value[i], promises);
  119. }
  120. return map;
  121. case GraphValueTypes.POINT:
  122. return {
  123. latitude: parseFloat(value[0]),
  124. longitude: parseFloat(value[1])
  125. };
  126. default:
  127. throw new Error(`unknown scalar type: ${valueType}`);
  128. }
  129. }, _Graph_parseEdge = function _Graph_parseEdge([id, relationshipTypeId, sourceId, destinationId, properties], promises) {
  130. const edge = {
  131. id,
  132. sourceId,
  133. destinationId,
  134. properties: __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_parseProperties).call(this, properties, promises)
  135. };
  136. const relationshipType = __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_getMetadata).call(this, 'relationshipTypes', relationshipTypeId);
  137. if (relationshipType instanceof Promise) {
  138. promises.push(relationshipType.then(value => edge.relationshipType = value));
  139. }
  140. else {
  141. edge.relationshipType = relationshipType;
  142. }
  143. return edge;
  144. }, _Graph_parseNode = function _Graph_parseNode([id, labelIds, properties], promises) {
  145. const labels = new Array(labelIds.length);
  146. for (let i = 0; i < labelIds.length; i++) {
  147. const value = __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_getMetadata).call(this, 'labels', labelIds[i]);
  148. if (value instanceof Promise) {
  149. promises.push(value.then(value => labels[i] = value));
  150. }
  151. else {
  152. labels[i] = value;
  153. }
  154. }
  155. return {
  156. id,
  157. labels,
  158. properties: __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_parseProperties).call(this, properties, promises)
  159. };
  160. }, _Graph_parseProperties = function _Graph_parseProperties(raw, promises) {
  161. const parsed = {};
  162. for (const [id, type, value] of raw) {
  163. const parsedValue = __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_parseValue).call(this, [type, value], promises), key = __classPrivateFieldGet(this, _Graph_instances, "m", _Graph_getMetadata).call(this, 'propertyKeys', id);
  164. if (key instanceof Promise) {
  165. promises.push(key.then(key => parsed[key] = parsedValue));
  166. }
  167. else {
  168. parsed[key] = parsedValue;
  169. }
  170. }
  171. return parsed;
  172. };
  173. exports.default = Graph;