concat.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2015-present, Vitaly Tomilov
  3. *
  4. * See the LICENSE file at the top-level directory of this distribution
  5. * for licensing information.
  6. *
  7. * Removal or modification of this copyright notice is prohibited.
  8. */
  9. const {QueryFile} = require('../../query-file');
  10. const npm = {
  11. formatting: require('../../formatting')
  12. };
  13. /**
  14. * @method helpers.concat
  15. * @description
  16. * Formats and concatenates multiple queries into a single query string.
  17. *
  18. * Before joining the queries, the method does the following:
  19. * - Formats each query, if `values` are provided;
  20. * - Removes all leading and trailing spaces, tabs and semi-colons;
  21. * - Automatically skips all empty queries.
  22. *
  23. * @param {array<string|helpers.QueryFormat|QueryFile>} queries
  24. * Array of mixed-type elements:
  25. * - a simple query string, to be used as is
  26. * - a {@link helpers.QueryFormat QueryFormat}-like object = `{query, [values], [options]}`
  27. * - a {@link QueryFile} object
  28. *
  29. * @returns {string}
  30. * Concatenated string with all queries.
  31. *
  32. * @example
  33. *
  34. * const pgp = require('pg-promise')();
  35. *
  36. * const qf1 = new pgp.QueryFile('./query1.sql', {minify: true});
  37. * const qf2 = new pgp.QueryFile('./query2.sql', {minify: true});
  38. *
  39. * const query = pgp.helpers.concat([
  40. * {query: 'INSERT INTO Users(name, age) VALUES($1, $2)', values: ['John', 23]}, // QueryFormat-like object
  41. * {query: qf1, values: [1, 'Name']}, // QueryFile with formatting parameters
  42. * 'SELECT count(*) FROM Users', // a simple-string query,
  43. * qf2 // direct QueryFile object
  44. * ]);
  45. *
  46. * // query = concatenated string with all the queries
  47. */
  48. function concat(queries, capSQL) {
  49. if (!Array.isArray(queries)) {
  50. throw new TypeError('Parameter \'queries\' must be an array.');
  51. }
  52. const fmOptions = {capSQL};
  53. const all = queries.map((q, index) => {
  54. if (typeof q === 'string') {
  55. // a simple query string without parameters:
  56. return clean(q);
  57. }
  58. if (q && typeof q === 'object') {
  59. if (q instanceof QueryFile) {
  60. // QueryFile object:
  61. return clean(q[npm.formatting.as.ctf.toPostgres]());
  62. }
  63. if ('query' in q) {
  64. // object {query, values, options}:
  65. let opt = q.options && typeof q.options === 'object' ? q.options : {};
  66. opt = opt.capSQL === undefined ? Object.assign(opt, fmOptions) : opt;
  67. return clean(npm.formatting.as.format(q.query, q.values, opt));
  68. }
  69. }
  70. throw new Error(`Invalid query element at index ${index}.`);
  71. });
  72. return all.filter(q => q).join(';');
  73. }
  74. function clean(q) {
  75. // removes from the query all leading and trailing symbols ' ', '\t' and ';'
  76. return q.replace(/^[\s;]*|[\s;]*$/g, '');
  77. }
  78. module.exports = {concat};
  79. /**
  80. * @typedef helpers.QueryFormat
  81. * @description
  82. * A simple structure of parameters to be passed into method {@link formatting.format as.format} exactly as they are,
  83. * used by {@link helpers.concat}.
  84. *
  85. * @property {string|value|object} query
  86. * A query string or a value/object that implements $[Custom Type Formatting], to be formatted according to `values`.
  87. *
  88. * @property {array|object|value} [values]
  89. * Query-formatting values.
  90. *
  91. * @property {object} [options]
  92. * Query-formatting options, as supported by method {@link formatting.format as.format}.
  93. *
  94. * @see
  95. * {@link formatting.format as.format}
  96. */