Skip to content

Commit a5dbda5

Browse files
committed
fix: preserve JSON path expressions
1 parent b07edbe commit a5dbda5

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,9 @@ export const escapeId = (
312312
}
313313

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

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

319320
return `\`${identifier.replace(regex.backtick, '``')}\``;

test/everyday-queries.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,35 @@ describe('Nice to have: escapeId edge cases', () => {
459459
});
460460
});
461461

462+
describe('JSON path expressions with ?? placeholder', () => {
463+
test('should handle JSON arrow operator with path expression', () => {
464+
const query = format('SELECT * FROM ?? WHERE (?? = ?) LIMIT ?, ?', [
465+
'certification',
466+
"cert->>'$.name'",
467+
'myname',
468+
0,
469+
20,
470+
]);
471+
472+
assert.equal(
473+
query,
474+
"SELECT * FROM `certification` WHERE (`cert->>'$.name'` = 'myname') LIMIT 0, 20"
475+
);
476+
});
477+
478+
test('escapeId should not break JSON path expressions', () => {
479+
const escaped = escapeId("cert->>'$.name'");
480+
481+
assert.equal(escaped, "`cert->>'$.name'`");
482+
});
483+
484+
test('escapeId with JSON double arrow operator', () => {
485+
const escaped = escapeId("data->'$.user.address.city'");
486+
487+
assert.equal(escaped, "`data->'$.user.address.city'`");
488+
});
489+
});
490+
462491
describe('Nice to have: Deeply nested arrays', () => {
463492
test('triple nested array for bulk insert', () => {
464493
const data = [

0 commit comments

Comments
 (0)