import { expectPerDialect, sequelize } from '../../support'; const dialect = sequelize.dialect; const dialectName = dialect.name; const notSupportedError = new Error(`JSON Paths are not supported in ${dialectName}.`); describe('QueryGenerator#jsonPathExtractionQuery', () => { const queryGenerator = sequelize.getQueryInterface().queryGenerator; it('creates a json extract operation (object)', () => { // "jsonPathExtractionQuery" does not quote the first parameter, because the first parameter is *not* an identifier, // it can be any SQL expression, e.g. a column name, a function call, a subquery, etc. expectPerDialect(() => queryGenerator.jsonPathExtractionQuery(queryGenerator.quoteIdentifier('profile'), ['id'], false), { default: notSupportedError, mariadb: `json_compact(json_extract(\`profile\`,'$.id'))`, 'mysql sqlite': `json_extract(\`profile\`,'$.id')`, postgres: `"profile"->'id'`, }); }); it('creates a json extract operation (array)', () => { expectPerDialect(() => queryGenerator.jsonPathExtractionQuery(queryGenerator.quoteIdentifier('profile'), [0], false), { default: notSupportedError, mariadb: `json_compact(json_extract(\`profile\`,'$[0]'))`, 'mysql sqlite': `json_extract(\`profile\`,'$[0]')`, postgres: `"profile"->0`, }); }); it('creates a nested json extract operation', () => { expectPerDialect(() => queryGenerator.jsonPathExtractionQuery(queryGenerator.quoteIdentifier('profile'), ['id', 'username', 0, '0', 'name'], false), { default: notSupportedError, mariadb: `json_compact(json_extract(\`profile\`,'$.id.username[0]."0".name'))`, 'mysql sqlite': `json_extract(\`profile\`,'$.id.username[0]."0".name')`, postgres: `"profile"#>ARRAY['id','username','0','0','name']`, }); }); it(`escapes characters such as ", $, and '`, () => { expectPerDialect(() => queryGenerator.jsonPathExtractionQuery(queryGenerator.quoteIdentifier('profile'), [`"`, `'`, `$`], false), { default: notSupportedError, mysql: `json_extract(\`profile\`,'$."\\\\""."\\'"."$"')`, mariadb: `json_compact(json_extract(\`profile\`,'$."\\\\""."\\'"."$"'))`, sqlite: `json_extract(\`profile\`,'$."\\""."''"."$"')`, postgres: `"profile"#>ARRAY['"','''','$']`, }); }); it('creates a json extract+unquote operation (object)', () => { // "jsonPathExtractionQuery" does not quote the first parameter, because the first parameter is *not* an identifier, // it can be any SQL expression, e.g. a column name, a function call, a subquery, etc. expectPerDialect(() => queryGenerator.jsonPathExtractionQuery(queryGenerator.quoteIdentifier('profile'), ['id'], true), { default: notSupportedError, 'mariadb mysql sqlite': `json_unquote(json_extract(\`profile\`,'$.id'))`, postgres: `"profile"->>'id'`, }); }); it('creates a json extract+unquote operation (array)', () => { expectPerDialect(() => queryGenerator.jsonPathExtractionQuery(queryGenerator.quoteIdentifier('profile'), [0], true), { default: notSupportedError, 'mariadb mysql sqlite': `json_unquote(json_extract(\`profile\`,'$[0]'))`, postgres: `"profile"->>0`, }); }); it('creates a nested json extract+unquote operation', () => { expectPerDialect(() => queryGenerator.jsonPathExtractionQuery(queryGenerator.quoteIdentifier('profile'), ['id', 'username', 0, '0', 'name'], true), { default: notSupportedError, 'mysql mariadb sqlite': `json_unquote(json_extract(\`profile\`,'$.id.username[0]."0".name'))`, postgres: `"profile"#>>ARRAY['id','username','0','0','name']`, }); }); });