Skip to content

Commit

Permalink
Merge pull request #47 from marmelab/update-ra-data-postgrest
Browse files Browse the repository at this point in the history
Update to latest ra-data-postgrest
  • Loading branch information
djhi authored Jan 25, 2024
2 parents dc88752 + 1e1df8c commit 6b172f2
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cypress/e2e/lists.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Lists', () => {
getPaginationText().then(el => {
const count = parseInt(el.text().split('of')[1].trim());

cy.findByText('Before last month').click();
cy.findByText('Earlier').click();
// Use should here to allow built-in retry as it may take a few ms for the list to update
getPaginationText().should(el => {
const countFiltered = parseInt(el.text().split('of')[1].trim());
Expand Down
31 changes: 25 additions & 6 deletions packages/demo/src/contacts/ContactListFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import AccessTimeIcon from '@mui/icons-material/AccessTime';
import TrendingUpIcon from '@mui/icons-material/TrendingUp';
import LocalOfferIcon from '@mui/icons-material/LocalOffer';
import SupervisorAccountIcon from '@mui/icons-material/SupervisorAccount';
import { endOfYesterday, startOfWeek, startOfMonth, subMonths } from 'date-fns';
import {
endOfYesterday,
startOfWeek,
startOfMonth,
subMonths,
subWeeks,
} from 'date-fns';

import { Status } from '../misc/Status';

Expand Down Expand Up @@ -47,21 +53,34 @@ export const ContactListFilter = () => {
}}
/>
<FilterListItem
label="Before this week"
label="Last week"
value={{
'last_seen@gte': undefined,
'last_seen@gte': subWeeks(
startOfWeek(new Date()),
1
).toISOString(),
'last_seen@lte': startOfWeek(new Date()).toISOString(),
}}
/>
<FilterListItem
label="Before this month"
label="This month"
value={{
'last_seen@gte': undefined,
'last_seen@gte': startOfMonth(new Date()).toISOString(),
'last_seen@lte': undefined,
}}
/>
<FilterListItem
label="Last month"
value={{
'last_seen@gte': subMonths(
startOfMonth(new Date()),
1
).toISOString(),
'last_seen@lte': startOfMonth(new Date()).toISOString(),
}}
/>
<FilterListItem
label="Before last month"
label="Earlier"
value={{
'last_seen@gte': undefined,
'last_seen@lte': subMonths(
Expand Down
25 changes: 24 additions & 1 deletion packages/ra-supabase-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const MyAdmin = () => (

### DataProvider

`ra-supabase` is built on [`ra-data-postgrest`](https://github.com/raphiniert-com/ra-data-postgrest/tree/v2.0.0-alpha.0) that leverages [PostgREST](https://postgrest.org/en/stable/). As such, you have access the following features:
`ra-supabase` is built on [`ra-data-postgrest`](https://github.com/raphiniert-com/ra-data-postgrest/tree/v2.0.0) that leverages [PostgREST](https://postgrest.org/en/stable/). As such, you have access the following features:

#### Filters operators

Expand All @@ -93,6 +93,29 @@ See the [PostgREST documentation](https://postgrest.org/en/stable/api.html#opera

As users authenticate through supabase, you can leverage [Row Level Security](https://supabase.com/docs/guides/auth/row-level-security). Users identity will be propagated through the dataProvider if you provided the public API (anon) key. Keep in mind that passing the `service_role` key will bypass Row Level Security. This is not recommended.

#### Customizing the dataProvider

`supabaseDataProvider` also accepts the same options as the `ra-data-postgrest` dataProvider (except `apiUrl`), like [`primaryKeys`](https://github.com/raphiniert-com/ra-data-postgrest/blob/master/README.md#compound-primary-keys) or [`schema`](https://github.com/raphiniert-com/ra-data-postgrest/blob/master/README.md#custom-schema).

```jsx
// in dataProvider.js
import { supabaseDataProvider } from 'ra-supabase-core';
import { supabaseClient } from './supabase';

export const dataProvider = supabaseDataProvider({
instanceUrl: 'YOUR_SUPABASE_URL',
apiKey: 'YOUR_SUPABASE_ANON_KEY',
supabaseClient,
primaryKeys: new Map([
['some_table', ['custom_id']],
['another_table', ['first_column', 'second_column']],
]),
schema: () => localStorage.getItem("schema") || "api",
});
```

See the [`ra-data-postgrest`` documentation](https://github.com/raphiniert-com/ra-data-postgrest/blob/master/README.md) for more details.

### Authentication

`ra-supabase` supports email/password and OAuth authentication.
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-supabase-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
"types": "esm/index.d.ts",
"sideEffects": false,
"peerDependencies": {
"@raphiniert/ra-data-postgrest": "2.0.0-alpha.0",
"@raphiniert/ra-data-postgrest": "^2.0.0",
"@supabase/supabase-js": "^2.0.0",
"ra-core": "^4.7.0"
},
"devDependencies": {
"@raphiniert/ra-data-postgrest": "2.0.0-alpha.0",
"@raphiniert/ra-data-postgrest": "^2.0.0",
"@supabase/supabase-js": "^2.4.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
Expand Down
28 changes: 22 additions & 6 deletions packages/ra-supabase-core/src/dataProvider.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
import { DataProvider, fetchUtils } from 'ra-core';
import postgrestRestProvider from '@raphiniert/ra-data-postgrest';
import postgrestRestProvider, {
IDataProviderConfig,
defaultPrimaryKeys,
defaultSchema,
} from '@raphiniert/ra-data-postgrest';
import { SupabaseClient } from '@supabase/supabase-js';

/**
* A function that returns a dataProvider for Supabase.
* @param instanceUrl The URL of the Supabase instance
* @param apiKey The API key of the Supabase instance. Prefer the anonymous key.
* @param supabaseClient The Supabase client
* @param defaultListOp Optional - The default list filter operator. Defaults to 'eq'.
* @param primaryKeys Optional - The primary keys of the tables. Defaults to 'id'.
* @param schema Optional - The custom schema to use. Defaults to none.
* @returns A dataProvider for Supabase
*/
export const supabaseDataProvider = ({
instanceUrl,
apiKey,
supabaseClient,
httpClient = supabaseHttpClient({ apiKey, supabaseClient }),
defaultListOp = 'eq',
primaryKeys = defaultPrimaryKeys,
schema = defaultSchema,
}: {
instanceUrl: string;
apiKey: string;
supabaseClient: SupabaseClient;
}): DataProvider =>
postgrestRestProvider(
`${instanceUrl}/rest/v1`,
supabaseHttpClient({ apiKey, supabaseClient })
);
} & Partial<Omit<IDataProviderConfig, 'apiUrl'>>): DataProvider => {
const config: IDataProviderConfig = {
apiUrl: `${instanceUrl}/rest/v1`,
httpClient,
defaultListOp,
primaryKeys,
schema,
};
return postgrestRestProvider(config);
};

/**
* A function that returns a httpClient for Supabase. It handles the authentication.
Expand Down
25 changes: 24 additions & 1 deletion packages/ra-supabase/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ You must wrap your `<Admin>` inside a `<BrowserRouter>` as supabase use hash par

### DataProvider

`ra-supabase` is built on [`ra-data-postgrest`](https://github.com/raphiniert-com/ra-data-postgrest/tree/v2.0.0-alpha.0) that leverages [PostgREST](https://postgrest.org/en/stable/). As such, you have access the following features:
`ra-supabase` is built on [`ra-data-postgrest`](https://github.com/raphiniert-com/ra-data-postgrest/tree/v2.0.0) that leverages [PostgREST](https://postgrest.org/en/stable/). As such, you have access the following features:

#### Filters operators

Expand All @@ -115,6 +115,29 @@ See the [PostgREST documentation](https://postgrest.org/en/stable/api.html#opera

As users authenticate through supabase, you can leverage [Row Level Security](https://supabase.com/docs/guides/auth/row-level-security). Users identity will be propagated through the dataProvider if you provided the public API (anon) key. Keep in mind that passing the `service_role` key will bypass Row Level Security. This is not recommended.

#### Customizing the dataProvider

`supabaseDataProvider` also accepts the same options as the `ra-data-postgrest` dataProvider (except `apiUrl`), like [`primaryKeys`](https://github.com/raphiniert-com/ra-data-postgrest/blob/master/README.md#compound-primary-keys) or [`schema`](https://github.com/raphiniert-com/ra-data-postgrest/blob/master/README.md#custom-schema).

```jsx
// in dataProvider.js
import { supabaseDataProvider } from 'ra-supabase-core';
import { supabaseClient } from './supabase';

export const dataProvider = supabaseDataProvider({
instanceUrl: 'YOUR_SUPABASE_URL',
apiKey: 'YOUR_SUPABASE_ANON_KEY',
supabaseClient,
primaryKeys: new Map([
['some_table', ['custom_id']],
['another_table', ['first_column', 'second_column']],
]),
schema: () => localStorage.getItem("schema") || "api",
});
```

See the [`ra-data-postgrest`` documentation](https://github.com/raphiniert-com/ra-data-postgrest/blob/master/README.md) for more details.

### Authentication

`ra-supabase` supports email/password and OAuth authentication.
Expand Down
17 changes: 13 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2344,10 +2344,12 @@
resolved "https://registry.yarnpkg.com/@popperjs/core/-/core-2.11.6.tgz#cee20bd55e68a1720bdab363ecf0c821ded4cd45"
integrity sha512-50/17A98tWUfQ176raKiOGXuYpLyyVMkxxG6oylzL3BPOlA6ADGdK7EYunSa4I064xerltq9TGXs8HmOk5E+vw==

"@raphiniert/[email protected]":
version "2.0.0-alpha.0"
resolved "https://registry.yarnpkg.com/@raphiniert/ra-data-postgrest/-/ra-data-postgrest-2.0.0-alpha.0.tgz#12bc23ba9e25388b9b630e5400d290c4b23fe1a3"
integrity sha512-9bwWARORJX6ivdCkTyt/9hQ1we4hz0pW6MiPLqK7DWLSlqWyuC7A+/U0Iix5Qd52qoiB4ppA9Xov+btrDWgLWw==
"@raphiniert/ra-data-postgrest@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@raphiniert/ra-data-postgrest/-/ra-data-postgrest-2.0.0.tgz#4038621b4fe156095dde066353e29396e33cbb39"
integrity sha512-1Ss+/xAxL62aGmCBSSJnlFMMSl0T2/bNSfeV0lplm7N9AtsOxSfySDKnCQs53ngbSBK1rPbk4ypc/mDVm4y4xQ==
dependencies:
qs "^6.11.1"

"@react-spring/animated@~9.4.5":
version "9.4.5"
Expand Down Expand Up @@ -9503,6 +9505,13 @@ q@^1.5.1:
resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7"
integrity sha512-kV/CThkXo6xyFEZUugw/+pIOywXcDbFYgSct5cT3gqlbkBE1SJdwy6UQoZvodiWF/ckQLZyDE/Bu1M6gVu5lVw==

qs@^6.11.1:
version "6.11.2"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.11.2.tgz#64bea51f12c1f5da1bc01496f48ffcff7c69d7d9"
integrity sha512-tDNIz22aBzCDxLtVH++VnTfzxlfeK5CbqohpSqpJgj1Wg/cQbStNAz3NuqCs5vV+pjBsK4x4pN9HlVh7rcYRiA==
dependencies:
side-channel "^1.0.4"

qs@~6.10.3:
version "6.10.5"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.10.5.tgz#974715920a80ff6a262264acd2c7e6c2a53282b4"
Expand Down

0 comments on commit 6b172f2

Please sign in to comment.