json-path-extraction-query.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { expectPerDialect, sequelize } from '../../support';
  2. const dialect = sequelize.dialect;
  3. const dialectName = dialect.name;
  4. const notSupportedError = new Error(`JSON Paths are not supported in ${dialectName}.`);
  5. describe('QueryGenerator#jsonPathExtractionQuery', () => {
  6. const queryGenerator = sequelize.queryGenerator;
  7. if (dialect.supports.jsonExtraction.quoted) {
  8. it('creates a json extract operation (object)', () => {
  9. // "jsonPathExtractionQuery" does not quote the first parameter, because the first parameter is *not* an identifier,
  10. // it can be any SQL expression, e.g. a column name, a function call, a subquery, etc.
  11. expectPerDialect(
  12. () =>
  13. queryGenerator.jsonPathExtractionQuery(
  14. queryGenerator.quoteIdentifier('profile'),
  15. ['id'],
  16. false,
  17. ),
  18. {
  19. default: notSupportedError,
  20. mariadb: `json_compact(json_extract(\`profile\`,'$.id'))`,
  21. 'mysql sqlite3': `json_extract(\`profile\`,'$.id')`,
  22. postgres: `"profile"->'id'`,
  23. },
  24. );
  25. });
  26. it('creates a json extract operation (array)', () => {
  27. expectPerDialect(
  28. () =>
  29. queryGenerator.jsonPathExtractionQuery(
  30. queryGenerator.quoteIdentifier('profile'),
  31. [0],
  32. false,
  33. ),
  34. {
  35. default: notSupportedError,
  36. mariadb: `json_compact(json_extract(\`profile\`,'$[0]'))`,
  37. 'mysql sqlite3': `json_extract(\`profile\`,'$[0]')`,
  38. postgres: `"profile"->0`,
  39. },
  40. );
  41. });
  42. it('creates a nested json extract operation', () => {
  43. expectPerDialect(
  44. () =>
  45. queryGenerator.jsonPathExtractionQuery(
  46. queryGenerator.quoteIdentifier('profile'),
  47. ['id', 'username', 0, '0', 'name'],
  48. false,
  49. ),
  50. {
  51. default: notSupportedError,
  52. mariadb: `json_compact(json_extract(\`profile\`,'$.id.username[0]."0".name'))`,
  53. 'mysql sqlite3': `json_extract(\`profile\`,'$.id.username[0]."0".name')`,
  54. postgres: `"profile"#>ARRAY['id','username','0','0','name']::VARCHAR(255)[]`,
  55. },
  56. );
  57. });
  58. it(`escapes characters such as ", $, and '`, () => {
  59. expectPerDialect(
  60. () =>
  61. queryGenerator.jsonPathExtractionQuery(
  62. queryGenerator.quoteIdentifier('profile'),
  63. [`"`, `'`, `$`],
  64. false,
  65. ),
  66. {
  67. default: notSupportedError,
  68. mysql: `json_extract(\`profile\`,'$."\\\\""."\\'"."$"')`,
  69. mariadb: `json_compact(json_extract(\`profile\`,'$."\\\\""."\\'"."$"'))`,
  70. sqlite3: `json_extract(\`profile\`,'$."\\""."''"."$"')`,
  71. postgres: `"profile"#>ARRAY['"','''','$']::VARCHAR(255)[]`,
  72. },
  73. );
  74. });
  75. }
  76. if (dialect.supports.jsonExtraction.unquoted) {
  77. it('creates a json extract+unquote operation (object)', () => {
  78. // "jsonPathExtractionQuery" does not quote the first parameter, because the first parameter is *not* an identifier,
  79. // it can be any SQL expression, e.g. a column name, a function call, a subquery, etc.
  80. expectPerDialect(
  81. () =>
  82. queryGenerator.jsonPathExtractionQuery(
  83. queryGenerator.quoteIdentifier('profile'),
  84. ['id'],
  85. true,
  86. ),
  87. {
  88. default: notSupportedError,
  89. mssql: `JSON_VALUE([profile], N'$.id')`,
  90. 'mariadb mysql sqlite3': `json_unquote(json_extract(\`profile\`,'$.id'))`,
  91. postgres: `"profile"->>'id'`,
  92. },
  93. );
  94. });
  95. it('creates a json extract+unquote operation (array)', () => {
  96. expectPerDialect(
  97. () =>
  98. queryGenerator.jsonPathExtractionQuery(
  99. queryGenerator.quoteIdentifier('profile'),
  100. [0],
  101. true,
  102. ),
  103. {
  104. default: notSupportedError,
  105. mssql: `JSON_VALUE([profile], N'$[0]')`,
  106. 'mariadb mysql sqlite3': `json_unquote(json_extract(\`profile\`,'$[0]'))`,
  107. postgres: `"profile"->>0`,
  108. },
  109. );
  110. });
  111. it('creates a nested json extract+unquote operation', () => {
  112. expectPerDialect(
  113. () =>
  114. queryGenerator.jsonPathExtractionQuery(
  115. queryGenerator.quoteIdentifier('profile'),
  116. ['id', 'username', 0, '0', 'name'],
  117. true,
  118. ),
  119. {
  120. default: notSupportedError,
  121. mssql: `JSON_VALUE([profile], N'$.id.username[0]."0".name')`,
  122. 'mysql mariadb sqlite3': `json_unquote(json_extract(\`profile\`,'$.id.username[0]."0".name'))`,
  123. postgres: `"profile"#>>ARRAY['id','username','0','0','name']::VARCHAR(255)[]`,
  124. },
  125. );
  126. });
  127. }
  128. });