@raphiniert/ra-data-postgrest 2.0.0-alpha.0 → 2.0.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +137 -41
- package/IDEAS.md +46 -0
- package/README.md +120 -55
- package/esm/index.d.ts +58 -7
- package/esm/index.js +229 -221
- package/esm/urlBuilder.d.ts +7 -3
- package/esm/urlBuilder.js +87 -38
- package/lib/index.d.ts +58 -7
- package/lib/index.js +234 -221
- package/lib/urlBuilder.d.ts +7 -3
- package/lib/urlBuilder.js +87 -38
- package/package.json +11 -6
- package/src/index.ts +237 -125
- package/src/urlBuilder.ts +100 -43
package/CHANGELOG.md
CHANGED
|
@@ -2,105 +2,201 @@
|
|
|
2
2
|
|
|
3
3
|
Find all notable changes of this project in this file.
|
|
4
4
|
|
|
5
|
+
## v2.0.0-alpha.3 - 2023-06-16
|
|
6
|
+
### Improvements
|
|
7
|
+
- updated dependencies
|
|
8
|
+
|
|
9
|
+
### New feature
|
|
10
|
+
- [#82](https://github.com/raphiniert-com/ra-data-postgrest/pull/82), add logical operator filter parsing - @[jordiyapz](https://github.com/jordiyapz)
|
|
11
|
+
|
|
12
|
+
### Fixes
|
|
13
|
+
- [#81](https://github.com/raphiniert-com/ra-data-postgrest/pull/81), use {} with cs and cd operators url - @[YannBeauxis](https://github.com/YannBeauxis)
|
|
14
|
+
|
|
15
|
+
### Breaking changes
|
|
16
|
+
- Changed from from regular parameters to instantiate the dataProvider to a configuration interface/class. Thus, in future initalize the dataProvider by creating a structure which implements the interface:
|
|
17
|
+
|
|
18
|
+
```jsx
|
|
19
|
+
import { fetchUtils } from 'react-admin';
|
|
20
|
+
import postgrestRestProvider,
|
|
21
|
+
{ IDataProviderConfig,
|
|
22
|
+
defaultPrimaryKeys,
|
|
23
|
+
defaultSchema } from '@raphiniert/ra-data-postgrest';
|
|
24
|
+
|
|
25
|
+
const config: IDataProviderConfig = {
|
|
26
|
+
apiUrl: 'http://path.to.my.api/',
|
|
27
|
+
httpClient: fetchUtils.fetchJson,
|
|
28
|
+
defaultListOp: 'eq',
|
|
29
|
+
primaryKeys: defaultPrimaryKeys,
|
|
30
|
+
schema: defaultSchema
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
dp = postgrestRestProvider(config);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## v2.0.0-alpha.2 - 2023-04-24
|
|
37
|
+
### Improvements
|
|
38
|
+
- updated dependencies
|
|
39
|
+
|
|
40
|
+
### New feature
|
|
41
|
+
- [#49](https://github.com/raphiniert-com/ra-data-postgrest/issues/49), custom schema name can be used
|
|
42
|
+
|
|
43
|
+
## v2.0.0-alpha.1 - 2023-04-09
|
|
44
|
+
### Improvements
|
|
45
|
+
- updated dependencies
|
|
46
|
+
- updated `urlBuilder` and integrated URL encoding
|
|
47
|
+
|
|
48
|
+
### New feature
|
|
49
|
+
- [#48](https://github.com/raphiniert-com/ra-data-postgrest/issues/48) solved by pull request [#55](https://github.com/raphiniert-com/ra-data-postgrest/pull/55), the create method should return the data returned by postgrest and not the posted data - @[christiaanwesterbeek](https://github.com/christiaanwesterbeek)
|
|
50
|
+
- [#39](https://github.com/raphiniert-com/ra-data-postgrest/pull/39), allow passing extra headers via meta for react-admin hooks - @[christiaanwesterbeek](https://github.com/christiaanwesterbeek)
|
|
51
|
+
- [#41](https://github.com/raphiniert-com/ra-data-postgrest/pull/41), allow columns filtering for getList and getManyReference - @[christiaanwesterbeek](https://github.com/christiaanwesterbeek)
|
|
52
|
+
- added support of [#41](https://github.com/raphiniert-com/ra-data-postgrest/pull/41) for other functions, but `create` and `updateMany`
|
|
53
|
+
- [#38](https://github.com/raphiniert-com/ra-data-postgrest/pull/38), Let the sort param in getList be optional - @[christiaanwesterbeek](https://github.com/christiaanwesterbeek)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
## v1.2.1 - 2023-04-07
|
|
57
|
+
- small fix in condition
|
|
58
|
+
- fixed package name
|
|
59
|
+
|
|
5
60
|
## v2.0.0-alpha.0 - 2022-11-23
|
|
6
|
-
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
61
|
+
|
|
62
|
+
- [#42](https://github.com/raphiniert-com/ra-data-postgrest/issues/42), started adding some unit tests - @[christiaanwesterbeek](https://github.com/christiaanwesterbeek)
|
|
63
|
+
- dropped authProvider
|
|
64
|
+
- code cleanup & restructuring
|
|
65
|
+
- minor bugfixes - @[christiaanwesterbeek](https://github.com/christiaanwesterbeek)
|
|
66
|
+
- added some notes to README.md
|
|
11
67
|
|
|
12
68
|
### Breaking changes
|
|
13
|
-
|
|
69
|
+
|
|
70
|
+
- dropped authProvider as this should rather be an own package in case of a generic piece of software or application specific.
|
|
14
71
|
|
|
15
72
|
## v1.2.0 - 2022-08-23
|
|
16
|
-
|
|
17
|
-
-
|
|
73
|
+
|
|
74
|
+
- [#33](https://github.com/raphiniert-com/ra-data-postgrest/pull/33), adding dependabot - @[promitheus7](https://github.com/promitheus7)
|
|
75
|
+
- [#30](https://github.com/raphiniert-com/ra-data-postgrest/pull/30) and [#33](https://github.com/raphiniert-com/ra-data-postgrest/pull/33), updated dependency to compatible starting from `ra-core` 4.1.x - @[corrideat](https://github.com/corrideat), @[promitheus7](https://github.com/promitheus7)
|
|
18
76
|
|
|
19
77
|
## v1.1.9 - 2022-08-23
|
|
20
|
-
|
|
78
|
+
|
|
79
|
+
- [#30](https://github.com/raphiniert-com/ra-data-postgrest/pull/30), updated ts and modified code, such that some package dependencies became unrequired - @[corrideat](https://github.com/corrideat)
|
|
21
80
|
|
|
22
81
|
## v1.1.8 - 2022-01-07
|
|
23
|
-
|
|
82
|
+
|
|
83
|
+
- required re-release as I missed to transpile all and upload the features of 1.1.7 to npm.
|
|
24
84
|
|
|
25
85
|
## v1.1.7 - 2022-01-07
|
|
86
|
+
|
|
26
87
|
### Fixes
|
|
27
|
-
|
|
88
|
+
|
|
89
|
+
- fixed bad behaivor with compound keys while using field named `id` [issue #27](https://github.com/raphiniert-com/ra-data-postgrest/issues/27) and [issue #19](https://github.com/raphiniert-com/ra-data-postgrest/pull/19) - @[kenfehling](https://github.com/kenfehling)
|
|
28
90
|
|
|
29
91
|
### New feature
|
|
30
|
-
- [#26](https://github.com/raphiniert-com/ra-data-postgrest/pull/26), added rpc filter support, which is explained [here](https://github.com/raphiniert-com/ra-data-postgrest#special-filter-feature-rpc-functions) - @[TheRealAstroboy](https://github.com/TheRealAstroboy)
|
|
31
92
|
|
|
93
|
+
- [#26](https://github.com/raphiniert-com/ra-data-postgrest/pull/26), added rpc filter support, which is explained [here](https://github.com/raphiniert-com/ra-data-postgrest#special-filter-feature-rpc-functions) - @[TheRealAstroboy](https://github.com/TheRealAstroboy)
|
|
32
94
|
|
|
33
95
|
## v1.1.6 - 2021-01-16
|
|
96
|
+
|
|
34
97
|
### Fixes
|
|
35
|
-
|
|
98
|
+
|
|
99
|
+
- [#24](https://github.com/raphiniert-com/ra-data-postgrest/pull/24), fixed eq. prefix for non-compound keys [issue #23](https://github.com/raphiniert-com/ra-data-postgrest/issues/23) - @[andymed-jlp](https://github.com/andymed-jlp)
|
|
36
100
|
|
|
37
101
|
## v1.1.5 - 2021-01-16
|
|
102
|
+
|
|
38
103
|
### Fixes
|
|
39
|
-
|
|
104
|
+
|
|
105
|
+
- fixed query string generation of rpc endpoints [issue #22](https://github.com/raphiniert-com/ra-data-postgrest/issues/22)
|
|
40
106
|
|
|
41
107
|
### Breaking changes
|
|
42
|
-
|
|
108
|
+
|
|
109
|
+
- Support was dropped for generating the query string given many ids in case of an rpc endpoint. If your application has some psql functions which parse the standard input, I recommend to rework your db design. If this change causes strong damage to your application, please contact me in order to find a proper generic solution. Query strings of single rpc calls now drop the operator (`eq.`) and are generated properly.
|
|
43
110
|
|
|
44
111
|
## v1.1.4 - 2021-01-16
|
|
45
|
-
|
|
112
|
+
|
|
113
|
+
- deployment error
|
|
46
114
|
|
|
47
115
|
## v1.1.3 - 2020-11-30
|
|
116
|
+
|
|
48
117
|
### Fixes
|
|
49
|
-
|
|
50
|
-
-
|
|
118
|
+
|
|
119
|
+
- [#20](https://github.com/raphiniert-com/ra-data-postgrest/pull/20), fixed small bug in update - @[mkkane](https://github.com/mkkane)
|
|
120
|
+
- removed console log
|
|
51
121
|
|
|
52
122
|
## v1.1.2 - 2020-10-05
|
|
123
|
+
|
|
53
124
|
### Fixes
|
|
54
|
-
|
|
125
|
+
|
|
126
|
+
- fixed the last fix of getMany and getManyReferecnce for compound keys :)
|
|
55
127
|
|
|
56
128
|
## v1.1.1 - 2020-09-28
|
|
129
|
+
|
|
57
130
|
### Fixes
|
|
58
|
-
|
|
131
|
+
|
|
132
|
+
- fixed getMany and getManyReferecnce for compound keys
|
|
59
133
|
|
|
60
134
|
## v1.1.0 - 2020-09-16
|
|
135
|
+
|
|
61
136
|
### Fixes
|
|
62
|
-
|
|
137
|
+
|
|
138
|
+
- removed requirement to define order key in react admin, when using compound keys: default was `id`, now it's the compound key
|
|
63
139
|
|
|
64
140
|
### Breaking changes
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
141
|
+
|
|
142
|
+
- refactored compound primary keys:
|
|
143
|
+
For single custom keys wrap an array as follows:
|
|
144
|
+
```jsx
|
|
145
|
+
const dataProvider = postgrestRestProvider(
|
|
146
|
+
API_URL,
|
|
147
|
+
fetchUtils.fetchJson,
|
|
148
|
+
'eq',
|
|
149
|
+
new Map([
|
|
150
|
+
['some_table', ['custom_id']], // <- instead of ['some_table','custom_id']
|
|
151
|
+
['another_table', ['first_column', 'second_column']],
|
|
152
|
+
])
|
|
153
|
+
);
|
|
154
|
+
```
|
|
73
155
|
|
|
74
156
|
## v1.0.7 - 2020-08-23
|
|
157
|
+
|
|
75
158
|
### New feature
|
|
76
|
-
|
|
159
|
+
|
|
160
|
+
- [#14](https://github.com/raphiniert-com/ra-data-postgrest/pull/14), added compound primary keys (see [README.md](https://github.com/raphiniert-com/ra-data-postgrest/blob/master/README.md#compound-primary-keys)) - @[programmarchy](https://github.com/programmarchy)
|
|
77
161
|
|
|
78
162
|
### Maintenance
|
|
79
|
-
|
|
80
|
-
-
|
|
163
|
+
|
|
164
|
+
- `import React from 'react';` -> `import * as React from 'react';`
|
|
165
|
+
- Some minor code cleanup
|
|
81
166
|
|
|
82
167
|
## v1.0.6 - 2020-06-15
|
|
168
|
+
|
|
83
169
|
### Fixes
|
|
84
|
-
|
|
170
|
+
|
|
171
|
+
- Fixed wrong support statement concerning [postgrest starter kit](https://github.com/subzerocloud/postgrest-starter-kit) in readme
|
|
85
172
|
|
|
86
173
|
## v1.0.5 - 2020-06-15
|
|
174
|
+
|
|
87
175
|
### Fixes
|
|
88
|
-
|
|
89
|
-
-
|
|
176
|
+
|
|
177
|
+
- [#13](https://github.com/raphiniert-com/ra-data-postgrest/pull/13), Remove console.log - @[jpagex](https://github.com/jpagex)
|
|
178
|
+
- [#12](https://github.com/raphiniert-com/ra-data-postgrest/pull/12), Fixed limit parameter - @[jpagex](https://github.com/jpagex)
|
|
90
179
|
|
|
91
180
|
## v1.0.4 - 2020-06-08
|
|
181
|
+
|
|
92
182
|
### Fixes
|
|
93
|
-
|
|
94
|
-
- [#
|
|
183
|
+
|
|
184
|
+
- Fixed missing js files in npm package [#8](https://github.com/raphiniert-com/ra-data-postgrest/issues/8)
|
|
185
|
+
- [#9](https://github.com/raphiniert-com/ra-data-postgrest/pull/9), Fixed typo in readme - @[seclace](https://github.com/seclace)
|
|
95
186
|
|
|
96
187
|
## v1.0.3 - 2020-05-17
|
|
188
|
+
|
|
97
189
|
### Fixes
|
|
98
|
-
|
|
99
|
-
-
|
|
190
|
+
|
|
191
|
+
- Fixed bug when multidelete - @[colonist4096](https://github.com/colonist4096)
|
|
192
|
+
- [#5](https://github.com/raphiniert-com/ra-data-postgrest/pull/6), Fixed bug while using multiple filters on the same attribute - @[olivierdalang](https://github.com/olivierdalang)
|
|
100
193
|
|
|
101
194
|
## v1.0.2 - 2020-03-10
|
|
195
|
+
|
|
102
196
|
### Fixes
|
|
103
|
-
|
|
197
|
+
|
|
198
|
+
- [#4](https://github.com/raphiniert-com/ra-data-postgrest/pull/4), Fixed bug while using ReferenceField with UUID - @[xero88](https://github.com/xero88)
|
|
104
199
|
|
|
105
200
|
## v1.0.1 - 2020-02-01
|
|
106
|
-
|
|
201
|
+
|
|
202
|
+
- initial release
|
package/IDEAS.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Here's a list of customizations that I have implemented locally in an old local version of ra-data-postgrest. The gaps in the numbers means it has already been imlemented in master. My customizations are relatively low quality and without tests. I thought I'd leave it here with you for inspiration or maybe it has overlap with one of the thins you want to work on.
|
|
2
|
+
- 1: Allowing calls like dataProvider.getOne('rpc/xxx') that actually returns a single object
|
|
3
|
+
- 6: column filtering voor getOne en getMany
|
|
4
|
+
- 8: Be able to use useGetOne with an array of ids for rpc/xxx functions
|
|
5
|
+
- 8b: Be able to use useGetMany with an array of ids for rpc/xxx functions
|
|
6
|
+
- 9: Be able to use useGetList with rpc functions (that return a list)
|
|
7
|
+
- 10: we could do null valued parameters, but undefined valued paramters do not make sense to pass to Postgrest
|
|
8
|
+
- 11: be able to filter on an array of values (and use 'in' operator by default)
|
|
9
|
+
- 12: be able to sort on multiple fields
|
|
10
|
+
- 13: with calls to getOne using rpc functions do not use eq.xxx but simply xxx
|
|
11
|
+
- 17: Allow a null response with getManyReference instead of an empty array (because that's what som of my rpcs return)
|
|
12
|
+
Some might be so niche that you should ignore them and that I instead should actually implement in a decorator (a function that alters the params a bit before the dataprovider function is called)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
About the decorator. Not sure if know how to use it, but I use it like this:
|
|
16
|
+
```jsx
|
|
17
|
+
import { CreateParams, DataProvider } from 'react-admin';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Gives opportunity to alter method, resource and params before calling the original dataProvider
|
|
21
|
+
*/
|
|
22
|
+
export const decorator = (dataProvider: DataProvider) => ({
|
|
23
|
+
...dataProvider,
|
|
24
|
+
create: (originalResource: string, originalParams: CreateParams) => {
|
|
25
|
+
const method = 'create';
|
|
26
|
+
|
|
27
|
+
const newParams = {
|
|
28
|
+
...originalParams,
|
|
29
|
+
// alterations
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return dataProvider[method](originalResource, newParams);
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
And then to use it.
|
|
38
|
+
```jsx
|
|
39
|
+
const restClient = postgrestRestProvider(
|
|
40
|
+
httpClientApi,
|
|
41
|
+
defaultListOp,
|
|
42
|
+
resourcePimaryKeys
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
export const dataProvider = decorator(restClient);
|
|
46
|
+
```
|
package/README.md
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
# PostgREST Data Provider For React-Admin
|
|
2
|
+
[](https://www.npmjs.com/package/@raphiniert/ra-data-postgrest)
|
|
3
|
+
[](https://www.npmjs.com/package/@raphiniert/ra-data-postgrest)
|
|
4
|
+
[](https://coveralls.io/github/raphiniert-com/ra-data-postgrest?branch=master)
|
|
5
|
+
[](https://github.com/raphiniert-com/ra-data-postgrest/blob/master/LICENSE)
|
|
6
|
+
|
|
2
7
|
|
|
3
8
|
PostgREST Data Provider for [react-admin](https://github.com/marmelab/react-admin), the frontend framework for building admin applications on top of REST/GraphQL services.
|
|
4
9
|
|
|
5
10
|
## Installation
|
|
6
11
|
|
|
7
12
|
```sh
|
|
8
|
-
npm install --save @
|
|
13
|
+
npm install --save @raphiniert/ra-data-postgrest
|
|
9
14
|
```
|
|
10
15
|
|
|
11
16
|
**NOTE**: When using RA 3.x.x, use the data-provider 1.x.x. For RA >= 4.1.x use the data-provider starting from 1.2.x.
|
|
@@ -14,17 +19,17 @@ npm install --save @promitheus/ra-data-postgrest
|
|
|
14
19
|
|
|
15
20
|
This Data Provider fits REST APIs using simple GET parameters for filters and sorting. This is the dialect used for instance in [PostgREST](http://postgrest.org).
|
|
16
21
|
|
|
17
|
-
| Method | API calls
|
|
18
|
-
|
|
19
|
-
| `getList` | `GET http://my.api.url/posts?order=title.asc&offset=0&limit=24&filterField=eq.value`
|
|
20
|
-
| `getOne` | `GET http://my.api.url/posts?id=eq.123`
|
|
21
|
-
| `getMany` | `GET http://my.api.url/posts?id=in.(123,456,789)`
|
|
22
|
-
| `getManyReference` | `GET http://my.api.url/posts?author_id=eq.345`
|
|
23
|
-
| `create` | `POST http://my.api.url/posts`
|
|
24
|
-
| `update` | `PATCH http://my.api.url/posts?id=eq.123`
|
|
25
|
-
| `updateMany` | `PATCH http://my.api.url/posts?id=in.(123,456,789)`
|
|
26
|
-
| `delete` | `DELETE http://my.api.url/posts?id=eq.123`
|
|
27
|
-
| `deleteMany` | `DELETE http://my.api.url/posts?id=in.(123,456,789)`
|
|
22
|
+
| Method | API calls |
|
|
23
|
+
| ------------------ | ------------------------------------------------------------------------------------ |
|
|
24
|
+
| `getList` | `GET http://my.api.url/posts?order=title.asc&offset=0&limit=24&filterField=eq.value` |
|
|
25
|
+
| `getOne` | `GET http://my.api.url/posts?id=eq.123` |
|
|
26
|
+
| `getMany` | `GET http://my.api.url/posts?id=in.(123,456,789)` |
|
|
27
|
+
| `getManyReference` | `GET http://my.api.url/posts?author_id=eq.345` |
|
|
28
|
+
| `create` | `POST http://my.api.url/posts` |
|
|
29
|
+
| `update` | `PATCH http://my.api.url/posts?id=eq.123` |
|
|
30
|
+
| `updateMany` | `PATCH http://my.api.url/posts?id=in.(123,456,789)` |
|
|
31
|
+
| `delete` | `DELETE http://my.api.url/posts?id=eq.123` |
|
|
32
|
+
| `deleteMany` | `DELETE http://my.api.url/posts?id=in.(123,456,789)` |
|
|
28
33
|
|
|
29
34
|
**Note**: The PostgREST data provider expects the API to include a `Content-Range` header in the response to `getList` calls. The value must be the total number of resources in the collection. This allows react-admin to know how many pages of resources there are in total, and build the pagination controls.
|
|
30
35
|
|
|
@@ -42,14 +47,25 @@ Access-Control-Expose-Headers: Content-Range
|
|
|
42
47
|
|
|
43
48
|
```jsx
|
|
44
49
|
// in src/App.js
|
|
45
|
-
import React from 'react';
|
|
46
|
-
import { Admin, Resource } from 'react-admin';
|
|
47
|
-
import postgrestRestProvider
|
|
50
|
+
import * as React from 'react';
|
|
51
|
+
import { Admin, Resource, fetchUtils } from 'react-admin';
|
|
52
|
+
import postgrestRestProvider,
|
|
53
|
+
{ IDataProviderConfig,
|
|
54
|
+
defaultPrimaryKeys,
|
|
55
|
+
defaultSchema } from '@raphiniert/ra-data-postgrest';
|
|
48
56
|
|
|
49
57
|
import { PostList } from './posts';
|
|
50
58
|
|
|
59
|
+
const config: IDataProviderConfig = {
|
|
60
|
+
apiUrl: 'http://path.to.my.api/',
|
|
61
|
+
httpClient: fetchUtils.fetchJson,
|
|
62
|
+
defaultListOp: 'eq',
|
|
63
|
+
primaryKeys: defaultPrimaryKeys,
|
|
64
|
+
schema: defaultSchema
|
|
65
|
+
}
|
|
66
|
+
|
|
51
67
|
const App = () => (
|
|
52
|
-
<Admin dataProvider={postgrestRestProvider(
|
|
68
|
+
<Admin dataProvider={postgrestRestProvider(config)}>
|
|
53
69
|
<Resource name="posts" list={PostList} />
|
|
54
70
|
</Admin>
|
|
55
71
|
);
|
|
@@ -61,11 +77,11 @@ export default App;
|
|
|
61
77
|
|
|
62
78
|
The provider function accepts an HTTP client function as second argument. By default, they use react-admin's `fetchUtils.fetchJson()` as HTTP client. It's similar to HTML5 `fetch()`, except it handles JSON decoding and HTTP error codes automatically.
|
|
63
79
|
|
|
64
|
-
That means that if you need to add custom headers to your requests, you just need to
|
|
80
|
+
That means that if you need to add custom headers to your requests, you just need to _wrap_ the `fetchJson()` call inside your own function:
|
|
65
81
|
|
|
66
82
|
```jsx
|
|
67
83
|
import { fetchUtils, Admin, Resource } from 'react-admin';
|
|
68
|
-
import postgrestRestProvider from '@
|
|
84
|
+
import postgrestRestProvider from '@raphiniert/ra-data-postgrest';
|
|
69
85
|
|
|
70
86
|
const httpClient = (url, options = {}) => {
|
|
71
87
|
if (!options.headers) {
|
|
@@ -75,11 +91,18 @@ const httpClient = (url, options = {}) => {
|
|
|
75
91
|
options.headers.set('X-Custom-Header', 'foobar');
|
|
76
92
|
return fetchUtils.fetchJson(url, options);
|
|
77
93
|
};
|
|
78
|
-
|
|
94
|
+
|
|
95
|
+
const config: IDataProviderConfig = {
|
|
96
|
+
...
|
|
97
|
+
httpClient: httpClient,
|
|
98
|
+
...
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const dataProvider = postgrestRestProvider(config);
|
|
79
102
|
|
|
80
103
|
render(
|
|
81
104
|
<Admin dataProvider={dataProvider} title="Example Admin">
|
|
82
|
-
|
|
105
|
+
...
|
|
83
106
|
</Admin>,
|
|
84
107
|
document.getElementById('root')
|
|
85
108
|
);
|
|
@@ -93,7 +116,7 @@ Now all the requests to the REST API will contain the `X-Custom-Header: foobar`
|
|
|
93
116
|
const httpClient = (url, options = {}) => {
|
|
94
117
|
options.user = {
|
|
95
118
|
authenticated: true,
|
|
96
|
-
token: 'SRTRDFVESGNJYTUKTYTHRG'
|
|
119
|
+
token: 'SRTRDFVESGNJYTUKTYTHRG',
|
|
97
120
|
};
|
|
98
121
|
return fetchUtils.fetchJson(url, options);
|
|
99
122
|
};
|
|
@@ -101,67 +124,109 @@ const httpClient = (url, options = {}) => {
|
|
|
101
124
|
|
|
102
125
|
Now all the requests to the REST API will contain the `Authorization: SRTRDFVESGNJYTUKTYTHRG` header.
|
|
103
126
|
|
|
104
|
-
### Using authProvider
|
|
105
|
-
This package also comes with an [authProvider](https://github.com/marmelab/react-admin/blob/master/docs/Authentication.md) for react-admin which enables you to enable authentification. The provider is designed to work together with [subzero-starter-kit](https://github.com/subzerocloud/subzero-starter-kit). This starter kit sends the JWT within a session cookie. The authProvider expects that. If you want to use postgREST without the starter kit you'll need to write your own. Feel free to contribute!
|
|
106
|
-
|
|
107
|
-
With one of the starter kits it is very easy to use the authProvider:
|
|
108
|
-
```jsx
|
|
109
|
-
// in src/App.js
|
|
110
|
-
import React from 'react';
|
|
111
|
-
import { Admin, Resource } from 'react-admin';
|
|
112
|
-
import postgrestRestProvider, { authProvider } from '@promitheus/ra-data-postgrest';
|
|
113
|
-
|
|
114
|
-
import { PostList } from './posts';
|
|
115
|
-
|
|
116
|
-
const App = () => (
|
|
117
|
-
<Admin dataProvider={postgrestRestProvider('http://path.to.my.api/')}
|
|
118
|
-
authProvider={authProvider}>
|
|
119
|
-
<Resource name="posts" list={PostList} />
|
|
120
|
-
</Admin>
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
export default App;
|
|
124
|
-
```
|
|
125
|
-
|
|
126
127
|
### Special Filter Feature
|
|
127
|
-
|
|
128
|
+
|
|
129
|
+
As postgRest allows several comparators, e.g. `ilike`, `like`, `eq`...
|
|
128
130
|
The dataProvider is designed to enable you to specify the comparator in your react filter component:
|
|
129
131
|
|
|
130
132
|
```jsx
|
|
131
133
|
<Filter {...props}>
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
134
|
+
<TextInput label="Search" source="post_title@ilike" alwaysOn />
|
|
135
|
+
<TextInput label="Search" source="post_author" alwaysOn />
|
|
136
|
+
// some more filters
|
|
135
137
|
</Filter>
|
|
136
138
|
```
|
|
137
139
|
|
|
138
140
|
One can simply append the comparator with an `@` to the source. In this example the field `post_title` would be filtered with `ilike` whereas `post_author` would be filtered using `eq` which is the default if no special comparator is specified.
|
|
139
141
|
|
|
140
142
|
#### RPC Functions
|
|
141
|
-
|
|
143
|
+
|
|
144
|
+
Given a RPC call as `GET /rpc/add_them?post_author=Herbert HTTP/1.1`, the dataProvider allows you to filter such endpoints. As they are no view, but a SQL procedure, several postgREST features do not apply. I.e. no comparators such as `ilike`, `like`, `eq` are applicable. Only the raw value without comparator needs to be send to the API. In order to realize this behavior, just add an "empty" comparator to the field, i.e. end `source` with an `@` as in the example:
|
|
142
145
|
|
|
143
146
|
```jsx
|
|
144
147
|
<Filter {...props}>
|
|
145
|
-
|
|
146
|
-
|
|
148
|
+
<TextInput label="Search" source="post_author@" alwaysOn />
|
|
149
|
+
// some more filters
|
|
147
150
|
</Filter>
|
|
148
151
|
```
|
|
149
152
|
|
|
150
153
|
### Compound primary keys
|
|
154
|
+
|
|
151
155
|
If one has data resources without primary keys named `id`, one will have to define this specifically. Also, if there is a primary key, which is defined over multiple columns:
|
|
152
156
|
|
|
153
157
|
```jsx
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
]
|
|
158
|
+
const config: IDataProviderConfig = {
|
|
159
|
+
...
|
|
160
|
+
primaryKeys: new Map([
|
|
161
|
+
['some_table', ['custom_id']],
|
|
162
|
+
['another_table', ['first_column', 'second_column']],
|
|
163
|
+
]),
|
|
164
|
+
...
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const dataProvider = postgrestRestProvider(config);
|
|
158
168
|
```
|
|
159
169
|
|
|
170
|
+
### Custom schema
|
|
171
|
+
PostgREST allows to [select and switch the database schema](https://postgrest.org/en/stable/api.html#switching-schemas) by setting a custom header. Thus, one way to use this function would be adding the custom header as a string while using react-admin hooks within `meta.schema` (compare to next section) or to set it up as function of `() => (string)` while using the data provider just component based. The latter can be done as follows and gives the opportunity to use some central storage (e.g. localStorage) which can be changed at multiple points of the application:
|
|
172
|
+
```jsx
|
|
173
|
+
const config: IDataProviderConfig = {
|
|
174
|
+
...
|
|
175
|
+
schema: localStorage.getItem("schema") || "api",
|
|
176
|
+
...
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const dataProvider = postgrestRestProvider(config);
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### Passing extra headers via meta
|
|
183
|
+
Postgrest supports calling functions with a single JSON parameter by sending the header Prefer: params=single-object with your request according to its [docs](https://postgrest.org/en/stable/api.html#calling-functions-with-a-single-json-parameter).
|
|
184
|
+
|
|
185
|
+
Within the data provider one can add any kind of header to the request while calling react-admin hooks, e.g.:
|
|
186
|
+
|
|
187
|
+
```jsx
|
|
188
|
+
const [create, { isLoading, error }] = useCreate(
|
|
189
|
+
'rpc/my-function',
|
|
190
|
+
{
|
|
191
|
+
data: { ... },
|
|
192
|
+
meta: { headers: { Prefer: 'params=single-object' } },
|
|
193
|
+
}
|
|
194
|
+
);
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Vertical filtering
|
|
198
|
+
Postgrest supports a feature of [Vertical Filtering (Columns)](https://postgrest.org/en/stable/api.html#vertical-filtering-columns). Within the react-admin hooks this feature can be used as in the following example:
|
|
199
|
+
|
|
200
|
+
```jsx
|
|
201
|
+
const { data, total, isLoading, error } = useGetList(
|
|
202
|
+
'posts',
|
|
203
|
+
{
|
|
204
|
+
pagination: { page: 1, perPage: 10 },
|
|
205
|
+
sort: { field: 'published_at', order: 'DESC' }
|
|
206
|
+
meta: { columns: ['id', 'title'] }
|
|
207
|
+
}
|
|
208
|
+
);
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Further, one should be able to leverage this feature to rename columns:
|
|
212
|
+
```jsx
|
|
213
|
+
columns: ['id', 'somealias:title']
|
|
214
|
+
```
|
|
215
|
+
, to cast columns:
|
|
216
|
+
```jsx
|
|
217
|
+
columns: ['id::text', 'title']
|
|
218
|
+
```
|
|
219
|
+
and even get bits from a json or jsonb column"
|
|
220
|
+
```jsx
|
|
221
|
+
columns: ['id', 'json_data->>blood_type', 'json_data->phones']
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
**Note**: not working for `create` and `updateMany`.
|
|
225
|
+
|
|
160
226
|
## Developers notes
|
|
161
227
|
|
|
162
228
|
The current development of this library was done with node v19.10 and npm 8.19.3. In this version the unit tests and the development environment should work.
|
|
163
229
|
|
|
164
|
-
|
|
165
230
|
## License
|
|
166
231
|
|
|
167
232
|
This data provider is licensed under the MIT License and sponsored by [raphiniert.com](https://raphiniert.com).
|
package/esm/index.d.ts
CHANGED
|
@@ -1,9 +1,60 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DataProvider } from 'ra-core';
|
|
2
2
|
import { PrimaryKey, PostgRestOperator } from './urlBuilder';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Maps react-admin queries to a postgrest REST API
|
|
5
|
+
*
|
|
6
|
+
* This REST dialect uses postgrest syntax
|
|
7
|
+
*
|
|
8
|
+
* @see https://postgrest.org/en/stable/api.html#embedded-filters
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* getList => GET http://my.api.url/posts?order=title.asc&offset=0&limit=24&filterField=eq.value
|
|
13
|
+
* getOne => GET http://my.api.url/posts?id=eq.123
|
|
14
|
+
* getMany => GET http://my.api.url/posts?id=in.(123,456,789)
|
|
15
|
+
* getManyReference => GET http://my.api.url/posts?author_id=eq.345
|
|
16
|
+
* create => POST http://my.api.url/posts
|
|
17
|
+
* update => PATCH http://my.api.url/posts?id=eq.123
|
|
18
|
+
* updateMany => PATCH http://my.api.url/posts?id=in.(123,456,789)
|
|
19
|
+
* delete => DELETE http://my.api.url/posts?id=eq.123
|
|
20
|
+
* deleteMany => DELETE http://my.api.url/posts?id=in.(123,456,789)
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
*
|
|
24
|
+
* import * as React from 'react';
|
|
25
|
+
* import { Admin, Resource, fetchUtils } from 'react-admin';
|
|
26
|
+
* import postgrestRestProvider,
|
|
27
|
+
* { IDataProviderConfig,
|
|
28
|
+
* defaultPrimaryKeys,
|
|
29
|
+
* defaultSchema } from '@raphiniert/ra-data-postgrest';
|
|
30
|
+
*
|
|
31
|
+
* import { PostList } from './posts';
|
|
32
|
+
*
|
|
33
|
+
* const config: IDataProviderConfig = {
|
|
34
|
+
* apiUrl: 'http://path.to.my.api/',
|
|
35
|
+
* httpClient: fetchUtils.fetchJson,
|
|
36
|
+
* defaultListOp: 'eq',
|
|
37
|
+
* primaryKeys: defaultPrimaryKeys,
|
|
38
|
+
* schema: defaultSchema
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* const App = () => (
|
|
42
|
+
* <Admin dataProvider={postgrestRestProvider(config)}>
|
|
43
|
+
* <Resource name="posts" list={PostList} />
|
|
44
|
+
* </Admin>
|
|
45
|
+
* );
|
|
46
|
+
*
|
|
47
|
+
* export default App;
|
|
48
|
+
*/
|
|
49
|
+
export type MetaOption = string | undefined;
|
|
50
|
+
export declare const defaultPrimaryKeys: Map<string, PrimaryKey>;
|
|
51
|
+
export declare const defaultSchema: () => string;
|
|
52
|
+
export interface IDataProviderConfig {
|
|
53
|
+
apiUrl: string;
|
|
54
|
+
httpClient: (string: any, Options: any) => Promise<any>;
|
|
55
|
+
defaultListOp: PostgRestOperator;
|
|
56
|
+
primaryKeys: Map<string, PrimaryKey>;
|
|
57
|
+
schema: () => string;
|
|
58
|
+
}
|
|
59
|
+
declare const _default: (config: IDataProviderConfig) => DataProvider;
|
|
9
60
|
export default _default;
|