Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,9 @@ export const escapeId = (
}

const identifier = String(value);
const hasJsonOperator = identifier.indexOf('->') !== -1;

if (forbidQualified) {
if (forbidQualified || hasJsonOperator) {
if (identifier.indexOf('`') === -1) return `\`${identifier}\``;

return `\`${identifier.replace(regex.backtick, '``')}\``;
Expand Down
29 changes: 29 additions & 0 deletions test/everyday-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,35 @@ describe('Nice to have: escapeId edge cases', () => {
});
});

describe('JSON path expressions with ?? placeholder', () => {
test('should handle JSON arrow operator with path expression', () => {
const query = format('SELECT * FROM ?? WHERE (?? = ?) LIMIT ?, ?', [
'certification',
"cert->>'$.name'",
'myname',
0,
20,
]);

assert.equal(
query,
"SELECT * FROM `certification` WHERE (`cert->>'$.name'` = 'myname') LIMIT 0, 20"
);
});

test('escapeId should not break JSON path expressions', () => {
const escaped = escapeId("cert->>'$.name'");

assert.equal(escaped, "`cert->>'$.name'`");
});

test('escapeId with JSON double arrow operator', () => {
const escaped = escapeId("data->'$.user.address.city'");

assert.equal(escaped, "`data->'$.user.address.city'`");
});
});

describe('Nice to have: Deeply nested arrays', () => {
test('triple nested array for bulk insert', () => {
const data = [
Expand Down
Loading