From 548ea28726acfa0cfc840b1c5539a6cf6e15fcd5 Mon Sep 17 00:00:00 2001 From: Bobbie Soedirgo Date: Thu, 12 Nov 2020 17:10:35 +0800 Subject: [PATCH] fix: ordering on JSON columns Previously, `order` columns wrap the column name in quotes to allow characters like `.` without having the user wrap it manually, but this breaks on JSON columns, so we're removing the quotes. --- src/lib/PostgrestTransformBuilder.ts | 2 +- test/__snapshots__/basic.test.ts.snap | 33 +++++++++++++++++++++++++++ test/basic.test.ts | 5 ++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/lib/PostgrestTransformBuilder.ts b/src/lib/PostgrestTransformBuilder.ts index d4b78a9f..9ecea0b9 100644 --- a/src/lib/PostgrestTransformBuilder.ts +++ b/src/lib/PostgrestTransformBuilder.ts @@ -24,7 +24,7 @@ export default class PostgrestTransformBuilder extends PostgrestBuilder { const key = typeof foreignTable === 'undefined' ? 'order' : `"${foreignTable}".order` this.url.searchParams.set( key, - `"${column}".${ascending ? 'asc' : 'desc'}.${nullsFirst ? 'nullsfirst' : 'nullslast'}` + `${column}.${ascending ? 'asc' : 'desc'}.${nullsFirst ? 'nullsfirst' : 'nullslast'}` ) return this } diff --git a/test/__snapshots__/basic.test.ts.snap b/test/__snapshots__/basic.test.ts.snap index 33d1e4ea..5ee859f5 100644 --- a/test/__snapshots__/basic.test.ts.snap +++ b/test/__snapshots__/basic.test.ts.snap @@ -1,5 +1,38 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`allow ordering on JSON column 1`] = ` +Array [ + Object { + "age_range": "[1,2)", + "catchphrase": "'cat' 'fat'", + "data": null, + "status": "ONLINE", + "username": "supabot", + }, + Object { + "age_range": "[25,35)", + "catchphrase": "'bat' 'cat'", + "data": null, + "status": "OFFLINE", + "username": "kiwicopple", + }, + Object { + "age_range": "[25,35)", + "catchphrase": "'bat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "awailas", + }, + Object { + "age_range": "[20,30)", + "catchphrase": "'fat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, +] +`; + exports[`basic insert, update, delete basic delete 1`] = ` Object { "body": Array [ diff --git a/test/basic.test.ts b/test/basic.test.ts index f0b2f31f..e5a64ae3 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -126,3 +126,8 @@ test("don't mutate PostgrestClient.headers", async () => { const { error } = await postgrest.from('users').select() expect(error).toMatchSnapshot() }) + +test("allow ordering on JSON column", async () => { + const { data } = await postgrest.from('users').select().order('data->something') + expect(data).toMatchSnapshot() +})