index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.pushQueryArguments = void 0;
  4. const CONFIG_GET = require("./CONFIG_GET");
  5. const CONFIG_SET = require("./CONFIG_SET");
  6. ;
  7. const DELETE = require("./DELETE");
  8. const EXPLAIN = require("./EXPLAIN");
  9. const LIST = require("./LIST");
  10. const PROFILE = require("./PROFILE");
  11. const QUERY = require("./QUERY");
  12. const RO_QUERY = require("./RO_QUERY");
  13. const SLOWLOG = require("./SLOWLOG");
  14. exports.default = {
  15. CONFIG_GET,
  16. configGet: CONFIG_GET,
  17. CONFIG_SET,
  18. configSet: CONFIG_SET,
  19. DELETE,
  20. delete: DELETE,
  21. EXPLAIN,
  22. explain: EXPLAIN,
  23. LIST,
  24. list: LIST,
  25. PROFILE,
  26. profile: PROFILE,
  27. QUERY,
  28. query: QUERY,
  29. RO_QUERY,
  30. roQuery: RO_QUERY,
  31. SLOWLOG,
  32. slowLog: SLOWLOG
  33. };
  34. function pushQueryArguments(args, graph, query, options, compact) {
  35. args.push(graph);
  36. if (typeof options === 'number') {
  37. args.push(query);
  38. pushTimeout(args, options);
  39. }
  40. else {
  41. args.push(options?.params ?
  42. `CYPHER ${queryParamsToString(options.params)} ${query}` :
  43. query);
  44. if (options?.TIMEOUT !== undefined) {
  45. pushTimeout(args, options.TIMEOUT);
  46. }
  47. }
  48. if (compact) {
  49. args.push('--compact');
  50. }
  51. return args;
  52. }
  53. exports.pushQueryArguments = pushQueryArguments;
  54. function pushTimeout(args, timeout) {
  55. args.push('TIMEOUT', timeout.toString());
  56. }
  57. function queryParamsToString(params) {
  58. const parts = [];
  59. for (const [key, value] of Object.entries(params)) {
  60. parts.push(`${key}=${queryParamToString(value)}`);
  61. }
  62. return parts.join(' ');
  63. }
  64. function queryParamToString(param) {
  65. if (param === null) {
  66. return 'null';
  67. }
  68. switch (typeof param) {
  69. case 'string':
  70. return `"${param.replace(/["\\]/g, '\\$&')}"`;
  71. case 'number':
  72. case 'boolean':
  73. return param.toString();
  74. }
  75. if (Array.isArray(param)) {
  76. return `[${param.map(queryParamToString).join(',')}]`;
  77. }
  78. else if (typeof param === 'object') {
  79. const body = [];
  80. for (const [key, value] of Object.entries(param)) {
  81. body.push(`${key}:${queryParamToString(value)}`);
  82. }
  83. return `{${body.join(',')}}`;
  84. }
  85. else {
  86. throw new TypeError(`Unexpected param type ${typeof param} ${param}`);
  87. }
  88. }