@raphiniert/ra-data-postgrest 1.1.9 → 2.0.0-alpha.0
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 +15 -1
- package/README.md +11 -4
- package/esm/index.d.ts +9 -4
- package/esm/index.js +241 -4
- package/esm/urlBuilder.d.ts +18 -0
- package/esm/urlBuilder.js +183 -0
- package/lib/index.d.ts +9 -4
- package/lib/index.js +240 -7
- package/lib/urlBuilder.d.ts +18 -0
- package/lib/urlBuilder.js +195 -0
- package/package.json +13 -5
- package/src/index.ts +302 -4
- package/src/urlBuilder.ts +221 -0
- package/esm/authProvider/index.d.ts +0 -11
- package/esm/authProvider/index.js +0 -58
- package/esm/dataProvider/index.d.ts +0 -9
- package/esm/dataProvider/index.js +0 -360
- package/lib/authProvider/index.d.ts +0 -11
- package/lib/authProvider/index.js +0 -60
- package/lib/dataProvider/index.d.ts +0 -9
- package/lib/dataProvider/index.js +0 -362
- package/src/authProvider/index.ts +0 -56
- package/src/dataProvider/index.ts +0 -412
package/CHANGELOG.md
CHANGED
|
@@ -2,7 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
Find all notable changes of this project in this file.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## v2.0.0-alpha.0 - 2022-11-23
|
|
6
|
+
- [#42](https://github.com/raphiniert-com/ra-data-postgrest/issues/42), started adding some unit tests - @[christiaanwesterbeek](https://github.com/christiaanwesterbeek)
|
|
7
|
+
- dropped authProvider
|
|
8
|
+
- code cleanup & restructuring
|
|
9
|
+
- minor bugfixes - @[christiaanwesterbeek](https://github.com/christiaanwesterbeek)
|
|
10
|
+
- added some notes to README.md
|
|
11
|
+
|
|
12
|
+
### Breaking changes
|
|
13
|
+
- dropped authProvider as this should rather be an own package in case of a generic piece of software or application specific.
|
|
14
|
+
|
|
15
|
+
## v1.2.0 - 2022-08-23
|
|
16
|
+
- [#33](https://github.com/raphiniert-com/ra-data-postgrest/pull/33), adding dependabot - @[promitheus7](https://github.com/promitheus7)
|
|
17
|
+
- [#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
|
+
|
|
19
|
+
## v1.1.9 - 2022-08-23
|
|
6
20
|
- [#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)
|
|
7
21
|
|
|
8
22
|
## v1.1.8 - 2022-01-07
|
package/README.md
CHANGED
|
@@ -5,9 +5,11 @@ PostgREST Data Provider for [react-admin](https://github.com/marmelab/react-admi
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```sh
|
|
8
|
-
npm install --save @
|
|
8
|
+
npm install --save @promitheus/ra-data-postgrest
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
**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.
|
|
12
|
+
|
|
11
13
|
## REST Dialect
|
|
12
14
|
|
|
13
15
|
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).
|
|
@@ -42,7 +44,7 @@ Access-Control-Expose-Headers: Content-Range
|
|
|
42
44
|
// in src/App.js
|
|
43
45
|
import React from 'react';
|
|
44
46
|
import { Admin, Resource } from 'react-admin';
|
|
45
|
-
import postgrestRestProvider from '@
|
|
47
|
+
import postgrestRestProvider from '@promitheus/ra-data-postgrest';
|
|
46
48
|
|
|
47
49
|
import { PostList } from './posts';
|
|
48
50
|
|
|
@@ -63,7 +65,7 @@ That means that if you need to add custom headers to your requests, you just nee
|
|
|
63
65
|
|
|
64
66
|
```jsx
|
|
65
67
|
import { fetchUtils, Admin, Resource } from 'react-admin';
|
|
66
|
-
import postgrestRestProvider from '@
|
|
68
|
+
import postgrestRestProvider from '@promitheus/ra-data-postgrest';
|
|
67
69
|
|
|
68
70
|
const httpClient = (url, options = {}) => {
|
|
69
71
|
if (!options.headers) {
|
|
@@ -107,7 +109,7 @@ With one of the starter kits it is very easy to use the authProvider:
|
|
|
107
109
|
// in src/App.js
|
|
108
110
|
import React from 'react';
|
|
109
111
|
import { Admin, Resource } from 'react-admin';
|
|
110
|
-
import postgrestRestProvider, { authProvider } from '@
|
|
112
|
+
import postgrestRestProvider, { authProvider } from '@promitheus/ra-data-postgrest';
|
|
111
113
|
|
|
112
114
|
import { PostList } from './posts';
|
|
113
115
|
|
|
@@ -155,6 +157,11 @@ const dataProvider = postgrestRestProvider(API_URL, fetchUtils.fetchJson, 'eq',
|
|
|
155
157
|
]));
|
|
156
158
|
```
|
|
157
159
|
|
|
160
|
+
## Developers notes
|
|
161
|
+
|
|
162
|
+
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
|
+
|
|
164
|
+
|
|
158
165
|
## License
|
|
159
166
|
|
|
160
167
|
This data provider is licensed under the MIT License and sponsored by [raphiniert.com](https://raphiniert.com).
|
package/esm/index.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { fetchUtils, DataProvider } from 'ra-core';
|
|
2
|
+
import { PrimaryKey, PostgRestOperator } from './urlBuilder';
|
|
3
|
+
declare const _default: (apiUrl: string, httpClient?: (url: any, options?: fetchUtils.Options) => Promise<{
|
|
4
|
+
status: number;
|
|
5
|
+
headers: Headers;
|
|
6
|
+
body: string;
|
|
7
|
+
json: any;
|
|
8
|
+
}>, defaultListOp?: PostgRestOperator, primaryKeys?: Map<string, PrimaryKey>) => DataProvider;
|
|
9
|
+
export default _default;
|
package/esm/index.js
CHANGED
|
@@ -1,4 +1,241 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
import { fetchUtils } from 'ra-core';
|
|
13
|
+
import { getPrimaryKey, parseFilters, getOrderBy, dataWithId, getQuery, getKeyData, encodeId, decodeId, isCompoundKey, } from './urlBuilder';
|
|
14
|
+
/**
|
|
15
|
+
* Maps react-admin queries to a postgrest REST API
|
|
16
|
+
*
|
|
17
|
+
* This REST dialect uses postgrest syntax
|
|
18
|
+
*
|
|
19
|
+
* @see https://postgrest.org/en/stable/api.html#embedded-filters
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
*
|
|
23
|
+
* getList => GET http://my.api.url/posts?order=title.asc&offset=0&limit=24&filterField=eq.value
|
|
24
|
+
* getOne => GET http://my.api.url/posts?id=eq.123
|
|
25
|
+
* getMany => GET http://my.api.url/posts?id=in.(123,456,789)
|
|
26
|
+
* getManyReference => GET http://my.api.url/posts?author_id=eq.345
|
|
27
|
+
* create => POST http://my.api.url/posts
|
|
28
|
+
* update => PATCH http://my.api.url/posts?id=eq.123
|
|
29
|
+
* updateMany => PATCH http://my.api.url/posts?id=in.(123,456,789)
|
|
30
|
+
* delete => DELETE http://my.api.url/posts?id=eq.123
|
|
31
|
+
* deleteMany => DELETE http://my.api.url/posts?id=in.(123,456,789)
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
*
|
|
35
|
+
* import * as React from 'react';
|
|
36
|
+
* import { Admin, Resource } from 'react-admin';
|
|
37
|
+
* import postgrestRestProvider from '@promitheus/ra-data-postgrest';
|
|
38
|
+
*
|
|
39
|
+
* import { PostList } from './posts';
|
|
40
|
+
*
|
|
41
|
+
* const App = () => (
|
|
42
|
+
* <Admin dataProvider={postgrestRestProvider('http://path.to.my.api/')}>
|
|
43
|
+
* <Resource name="posts" list={PostList} />
|
|
44
|
+
* </Admin>
|
|
45
|
+
* );
|
|
46
|
+
*
|
|
47
|
+
* export default App;
|
|
48
|
+
*/
|
|
49
|
+
var defaultPrimaryKeys = new Map();
|
|
50
|
+
export default (function (apiUrl, httpClient, defaultListOp, primaryKeys) {
|
|
51
|
+
if (httpClient === void 0) { httpClient = fetchUtils.fetchJson; }
|
|
52
|
+
if (defaultListOp === void 0) { defaultListOp = 'eq'; }
|
|
53
|
+
if (primaryKeys === void 0) { primaryKeys = defaultPrimaryKeys; }
|
|
54
|
+
return ({
|
|
55
|
+
getList: function (resource, params) {
|
|
56
|
+
var primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
57
|
+
var _a = params.pagination, page = _a.page, perPage = _a.perPage;
|
|
58
|
+
var _b = params.sort, field = _b.field, order = _b.order;
|
|
59
|
+
var parsedFilter = parseFilters(params.filter, defaultListOp);
|
|
60
|
+
var query = __assign({ order: getOrderBy(field, order, primaryKey), offset: String((page - 1) * perPage), limit: String(perPage) }, parsedFilter);
|
|
61
|
+
// add header that Content-Range is in returned header
|
|
62
|
+
var options = {
|
|
63
|
+
headers: new Headers({
|
|
64
|
+
Accept: 'application/json',
|
|
65
|
+
Prefer: 'count=exact',
|
|
66
|
+
}),
|
|
67
|
+
};
|
|
68
|
+
var url = "".concat(apiUrl, "/").concat(resource, "?").concat(new URLSearchParams(query));
|
|
69
|
+
return httpClient(url, options).then(function (_a) {
|
|
70
|
+
var headers = _a.headers, json = _a.json;
|
|
71
|
+
if (!headers.has('content-range')) {
|
|
72
|
+
throw new Error("The Content-Range header is missing in the HTTP Response. The postgREST data provider expects \n responses for lists of resources to contain this header with the total number of results to build \n the pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Expose-Headers header?");
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
data: json.map(function (obj) { return dataWithId(obj, primaryKey); }),
|
|
76
|
+
total: parseInt(headers.get('content-range').split('/').pop(), 10),
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
getOne: function (resource, params) {
|
|
81
|
+
var id = params.id;
|
|
82
|
+
var primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
83
|
+
var query = getQuery(primaryKey, id, resource);
|
|
84
|
+
var url = "".concat(apiUrl, "/").concat(resource, "?").concat(query);
|
|
85
|
+
return httpClient(url, {
|
|
86
|
+
headers: new Headers({
|
|
87
|
+
accept: 'application/vnd.pgrst.object+json',
|
|
88
|
+
}),
|
|
89
|
+
}).then(function (_a) {
|
|
90
|
+
var json = _a.json;
|
|
91
|
+
return ({
|
|
92
|
+
data: dataWithId(json, primaryKey),
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
getMany: function (resource, params) {
|
|
97
|
+
var ids = params.ids;
|
|
98
|
+
var primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
99
|
+
var query = getQuery(primaryKey, ids, resource);
|
|
100
|
+
var url = "".concat(apiUrl, "/").concat(resource, "?").concat(query);
|
|
101
|
+
return httpClient(url).then(function (_a) {
|
|
102
|
+
var json = _a.json;
|
|
103
|
+
return ({
|
|
104
|
+
data: json.map(function (data) { return dataWithId(data, primaryKey); }),
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
},
|
|
108
|
+
getManyReference: function (resource, params) {
|
|
109
|
+
var _a;
|
|
110
|
+
var _b = params.pagination, page = _b.page, perPage = _b.perPage;
|
|
111
|
+
var _c = params.sort, field = _c.field, order = _c.order;
|
|
112
|
+
var parsedFilter = parseFilters(params.filter, defaultListOp);
|
|
113
|
+
var primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
114
|
+
var query = params.target
|
|
115
|
+
? __assign((_a = {}, _a[params.target] = "eq.".concat(params.id), _a.order = getOrderBy(field, order, primaryKey), _a.offset = String((page - 1) * perPage), _a.limit = String(perPage), _a), parsedFilter) : __assign({ order: getOrderBy(field, order, primaryKey), offset: String((page - 1) * perPage), limit: String(perPage) }, parsedFilter);
|
|
116
|
+
// add header that Content-Range is in returned header
|
|
117
|
+
var options = {
|
|
118
|
+
headers: new Headers({
|
|
119
|
+
Accept: 'application/json',
|
|
120
|
+
Prefer: 'count=exact',
|
|
121
|
+
}),
|
|
122
|
+
};
|
|
123
|
+
var url = "".concat(apiUrl, "/").concat(resource, "?").concat(new URLSearchParams(query));
|
|
124
|
+
return httpClient(url, options).then(function (_a) {
|
|
125
|
+
var headers = _a.headers, json = _a.json;
|
|
126
|
+
if (!headers.has('content-range')) {
|
|
127
|
+
throw new Error("The Content-Range header is missing in the HTTP Response. The postgREST data provider expects \n responses for lists of resources to contain this header with the total number of results to build \n the pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Expose-Headers header?");
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
data: json.map(function (data) { return dataWithId(data, primaryKey); }),
|
|
131
|
+
total: parseInt(headers.get('content-range').split('/').pop(), 10),
|
|
132
|
+
};
|
|
133
|
+
});
|
|
134
|
+
},
|
|
135
|
+
update: function (resource, params) {
|
|
136
|
+
var id = params.id, data = params.data;
|
|
137
|
+
var primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
138
|
+
var query = getQuery(primaryKey, id, resource);
|
|
139
|
+
var primaryKeyData = getKeyData(primaryKey, data);
|
|
140
|
+
var url = "".concat(apiUrl, "/").concat(resource, "?").concat(query);
|
|
141
|
+
var body = JSON.stringify(__assign(__assign({}, data), primaryKeyData));
|
|
142
|
+
return httpClient(url, {
|
|
143
|
+
method: 'PATCH',
|
|
144
|
+
headers: new Headers({
|
|
145
|
+
Accept: 'application/vnd.pgrst.object+json',
|
|
146
|
+
Prefer: 'return=representation',
|
|
147
|
+
'Content-Type': 'application/json',
|
|
148
|
+
}),
|
|
149
|
+
body: body,
|
|
150
|
+
}).then(function (_a) {
|
|
151
|
+
var json = _a.json;
|
|
152
|
+
return ({ data: dataWithId(json, primaryKey) });
|
|
153
|
+
});
|
|
154
|
+
},
|
|
155
|
+
updateMany: function (resource, params) {
|
|
156
|
+
var ids = params.ids;
|
|
157
|
+
var primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
158
|
+
var body = JSON.stringify(params.ids.map(function (id) {
|
|
159
|
+
var data = params.data;
|
|
160
|
+
var primaryKeyParams = decodeId(id, primaryKey);
|
|
161
|
+
var primaryKeyData = {};
|
|
162
|
+
if (isCompoundKey(primaryKey)) {
|
|
163
|
+
primaryKey.forEach(function (key, index) {
|
|
164
|
+
primaryKeyData[key] = primaryKeyParams[index];
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
primaryKeyData[primaryKey[0]] = primaryKeyParams[0];
|
|
169
|
+
}
|
|
170
|
+
return __assign(__assign({}, data), primaryKeyData);
|
|
171
|
+
}));
|
|
172
|
+
var url = "".concat(apiUrl, "/").concat(resource);
|
|
173
|
+
return httpClient(url, {
|
|
174
|
+
method: 'PATCH',
|
|
175
|
+
headers: new Headers({
|
|
176
|
+
Prefer: 'return=representation',
|
|
177
|
+
'Content-Type': 'application/json',
|
|
178
|
+
}),
|
|
179
|
+
body: body,
|
|
180
|
+
}).then(function (_a) {
|
|
181
|
+
var json = _a.json;
|
|
182
|
+
return ({
|
|
183
|
+
data: json.map(function (data) { return encodeId(data, primaryKey); }),
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
},
|
|
187
|
+
create: function (resource, params) {
|
|
188
|
+
var primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
189
|
+
var url = "".concat(apiUrl, "/").concat(resource);
|
|
190
|
+
return httpClient(url, {
|
|
191
|
+
method: 'POST',
|
|
192
|
+
headers: new Headers({
|
|
193
|
+
Accept: 'application/vnd.pgrst.object+json',
|
|
194
|
+
Prefer: 'return=representation',
|
|
195
|
+
'Content-Type': 'application/json',
|
|
196
|
+
}),
|
|
197
|
+
body: JSON.stringify(params.data),
|
|
198
|
+
}).then(function (_a) {
|
|
199
|
+
var json = _a.json;
|
|
200
|
+
return ({
|
|
201
|
+
data: __assign(__assign({}, params.data), { id: encodeId(json, primaryKey) }),
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
},
|
|
205
|
+
delete: function (resource, params) {
|
|
206
|
+
var id = params.id;
|
|
207
|
+
var primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
208
|
+
var query = getQuery(primaryKey, id, resource);
|
|
209
|
+
var url = "".concat(apiUrl, "/").concat(resource, "?").concat(query);
|
|
210
|
+
return httpClient(url, {
|
|
211
|
+
method: 'DELETE',
|
|
212
|
+
headers: new Headers({
|
|
213
|
+
Accept: 'application/vnd.pgrst.object+json',
|
|
214
|
+
Prefer: 'return=representation',
|
|
215
|
+
'Content-Type': 'application/json',
|
|
216
|
+
}),
|
|
217
|
+
}).then(function (_a) {
|
|
218
|
+
var json = _a.json;
|
|
219
|
+
return ({ data: dataWithId(json, primaryKey) });
|
|
220
|
+
});
|
|
221
|
+
},
|
|
222
|
+
deleteMany: function (resource, params) {
|
|
223
|
+
var ids = params.ids;
|
|
224
|
+
var primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
225
|
+
var query = getQuery(primaryKey, ids, resource);
|
|
226
|
+
var url = "".concat(apiUrl, "/").concat(resource, "?").concat(query);
|
|
227
|
+
return httpClient(url, {
|
|
228
|
+
method: 'DELETE',
|
|
229
|
+
headers: new Headers({
|
|
230
|
+
Prefer: 'return=representation',
|
|
231
|
+
'Content-Type': 'application/json',
|
|
232
|
+
}),
|
|
233
|
+
}).then(function (_a) {
|
|
234
|
+
var json = _a.json;
|
|
235
|
+
return ({
|
|
236
|
+
data: json.map(function (data) { return encodeId(data, primaryKey); }),
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
},
|
|
240
|
+
});
|
|
241
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Identifier } from 'ra-core';
|
|
2
|
+
/**
|
|
3
|
+
* List of all possible operators in PostgRest
|
|
4
|
+
* (https://postgrest.org/en/stable/api.html#operators)
|
|
5
|
+
*/
|
|
6
|
+
declare const postgrestOperators: readonly ["eq", "gt", "gte", "lt", "lte", "neq", "like", "ilike", "match", "imatch", "in", "is", "fts", "plfts", "phfts", "wfts", "cs", "cd", "ov", "sl", "sr", "nxr", "nxl", "adj", "not", "or", "and"];
|
|
7
|
+
export type PostgRestOperator = typeof postgrestOperators[number];
|
|
8
|
+
export declare const parseFilters: (filter: Record<string, any>, defaultListOp: PostgRestOperator) => {};
|
|
9
|
+
export type PrimaryKey = Array<string>;
|
|
10
|
+
export declare const getPrimaryKey: (resource: string, primaryKeys: Map<string, PrimaryKey>) => PrimaryKey;
|
|
11
|
+
export declare const decodeId: (id: Identifier, primaryKey: PrimaryKey) => string[] | number[];
|
|
12
|
+
export declare const encodeId: (data: any, primaryKey: PrimaryKey) => Identifier;
|
|
13
|
+
export declare const dataWithId: (data: any, primaryKey: PrimaryKey) => any;
|
|
14
|
+
export declare const isCompoundKey: (primaryKey: PrimaryKey) => Boolean;
|
|
15
|
+
export declare const getQuery: (primaryKey: PrimaryKey, ids: Identifier | Array<Identifier>, resource: string) => string;
|
|
16
|
+
export declare const getKeyData: (primaryKey: PrimaryKey, data: object) => object;
|
|
17
|
+
export declare const getOrderBy: (field: string, order: string, primaryKey: PrimaryKey) => string;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* List of all possible operators in PostgRest
|
|
14
|
+
* (https://postgrest.org/en/stable/api.html#operators)
|
|
15
|
+
*/
|
|
16
|
+
var postgrestOperators = [
|
|
17
|
+
'eq',
|
|
18
|
+
'gt',
|
|
19
|
+
'gte',
|
|
20
|
+
'lt',
|
|
21
|
+
'lte',
|
|
22
|
+
'neq',
|
|
23
|
+
'like',
|
|
24
|
+
'ilike',
|
|
25
|
+
'match',
|
|
26
|
+
'imatch',
|
|
27
|
+
'in',
|
|
28
|
+
'is',
|
|
29
|
+
'fts',
|
|
30
|
+
'plfts',
|
|
31
|
+
'phfts',
|
|
32
|
+
'wfts',
|
|
33
|
+
'cs',
|
|
34
|
+
'cd',
|
|
35
|
+
'ov',
|
|
36
|
+
'sl',
|
|
37
|
+
'sr',
|
|
38
|
+
'nxr',
|
|
39
|
+
'nxl',
|
|
40
|
+
'adj',
|
|
41
|
+
'not',
|
|
42
|
+
'or',
|
|
43
|
+
'and',
|
|
44
|
+
];
|
|
45
|
+
export var parseFilters = function (filter, defaultListOp) {
|
|
46
|
+
var result = {};
|
|
47
|
+
Object.keys(filter).forEach(function (key) {
|
|
48
|
+
// key: the name of the object key
|
|
49
|
+
var splitKey = key.split('@');
|
|
50
|
+
var operation = splitKey.length == 2
|
|
51
|
+
? splitKey[1]
|
|
52
|
+
: defaultListOp;
|
|
53
|
+
var values;
|
|
54
|
+
if (['like', 'ilike'].includes(operation)) {
|
|
55
|
+
// we split the search term in words
|
|
56
|
+
values = filter[key].trim().split(/\s+/);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
values = [filter[key]];
|
|
60
|
+
}
|
|
61
|
+
values.forEach(function (value) {
|
|
62
|
+
// if operator is intentionally blank, rpc syntax
|
|
63
|
+
var op = operation.includes('like')
|
|
64
|
+
? "".concat(operation, ".*").concat(value, "*")
|
|
65
|
+
: operation.length == 0
|
|
66
|
+
? "".concat(value)
|
|
67
|
+
: "".concat(operation, ".").concat(value);
|
|
68
|
+
if (result[splitKey[0]] === undefined) {
|
|
69
|
+
// first operator for the key, we add it to the dict
|
|
70
|
+
result[splitKey[0]] = op;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
if (!Array.isArray(result[splitKey[0]])) {
|
|
74
|
+
// second operator, we transform to an array
|
|
75
|
+
result[splitKey[0]] = [result[splitKey[0]], op];
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
// third and subsequent, we add to array
|
|
79
|
+
result[splitKey[0]].push(op);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
return result;
|
|
85
|
+
};
|
|
86
|
+
export var getPrimaryKey = function (resource, primaryKeys) {
|
|
87
|
+
return primaryKeys.get(resource) || ['id'];
|
|
88
|
+
};
|
|
89
|
+
export var decodeId = function (id, primaryKey) {
|
|
90
|
+
if (isCompoundKey(primaryKey)) {
|
|
91
|
+
return JSON.parse(id.toString());
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
return [id.toString()];
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
export var encodeId = function (data, primaryKey) {
|
|
98
|
+
if (isCompoundKey(primaryKey)) {
|
|
99
|
+
return JSON.stringify(primaryKey.map(function (key) { return data[key]; }));
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
return data[primaryKey[0]];
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
export var dataWithId = function (data, primaryKey) {
|
|
106
|
+
if (JSON.stringify(primaryKey) === JSON.stringify(['id'])) {
|
|
107
|
+
return data;
|
|
108
|
+
}
|
|
109
|
+
return Object.assign(data, {
|
|
110
|
+
id: encodeId(data, primaryKey),
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
export var isCompoundKey = function (primaryKey) {
|
|
114
|
+
return primaryKey.length > 1;
|
|
115
|
+
};
|
|
116
|
+
export var getQuery = function (primaryKey, ids, resource) {
|
|
117
|
+
var _a;
|
|
118
|
+
if (Array.isArray(ids) && ids.length > 1) {
|
|
119
|
+
// no standardized query with multiple ids possible for rpc endpoints which are api-exposed database functions
|
|
120
|
+
if (resource.startsWith('rpc/')) {
|
|
121
|
+
console.error("PostgREST's rpc endpoints are not intended to be handled as views. Therefore, no query generation for multiple key values implemented!");
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (isCompoundKey(primaryKey)) {
|
|
125
|
+
// TODO: Should be URL encoded
|
|
126
|
+
return "or=(".concat(ids.map(function (id) {
|
|
127
|
+
var primaryKeyParams = decodeId(id, primaryKey);
|
|
128
|
+
return "and(".concat(primaryKey
|
|
129
|
+
.map(function (key, i) { return "".concat(key, ".eq.").concat(primaryKeyParams[i]); })
|
|
130
|
+
.join(','), ")");
|
|
131
|
+
}), ")");
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
return new URLSearchParams((_a = {},
|
|
135
|
+
_a[primaryKey[0]] = "in.(".concat(ids.join(','), ")"),
|
|
136
|
+
_a)).toString();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
// if ids is one Identifier
|
|
141
|
+
var id = ids.toString();
|
|
142
|
+
var primaryKeyParams_1 = decodeId(id, primaryKey);
|
|
143
|
+
if (isCompoundKey(primaryKey)) {
|
|
144
|
+
if (resource.startsWith('rpc/'))
|
|
145
|
+
// TODO: Should be URL encoded
|
|
146
|
+
return "".concat(primaryKey
|
|
147
|
+
.map(function (key, i) { return "".concat(key, "=").concat(primaryKeyParams_1[i]); })
|
|
148
|
+
.join('&'));
|
|
149
|
+
// TODO: Should be URL encoded
|
|
150
|
+
else
|
|
151
|
+
return "and=(".concat(primaryKey
|
|
152
|
+
.map(function (key, i) {
|
|
153
|
+
return "".concat(key, ".eq.").concat(primaryKeyParams_1[i]);
|
|
154
|
+
})
|
|
155
|
+
.join(','), ")");
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
return new URLSearchParams([
|
|
159
|
+
[primaryKey[0], "eq.".concat(id)],
|
|
160
|
+
]).toString();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
export var getKeyData = function (primaryKey, data) {
|
|
165
|
+
var _a;
|
|
166
|
+
if (isCompoundKey(primaryKey)) {
|
|
167
|
+
return primaryKey.reduce(function (keyData, key) {
|
|
168
|
+
var _a;
|
|
169
|
+
return (__assign(__assign({}, keyData), (_a = {}, _a[key] = data[key], _a)));
|
|
170
|
+
}, {});
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
return _a = {}, _a[primaryKey[0]] = data[primaryKey[0]], _a;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
export var getOrderBy = function (field, order, primaryKey) {
|
|
177
|
+
if (field == 'id') {
|
|
178
|
+
return primaryKey.map(function (key) { return "".concat(key, ".").concat(order.toLowerCase()); }).join(',');
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
return "".concat(field, ".").concat(order.toLowerCase());
|
|
182
|
+
}
|
|
183
|
+
};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { fetchUtils, DataProvider } from 'ra-core';
|
|
2
|
+
import { PrimaryKey, PostgRestOperator } from './urlBuilder';
|
|
3
|
+
declare const _default: (apiUrl: string, httpClient?: (url: any, options?: fetchUtils.Options) => Promise<{
|
|
4
|
+
status: number;
|
|
5
|
+
headers: Headers;
|
|
6
|
+
body: string;
|
|
7
|
+
json: any;
|
|
8
|
+
}>, defaultListOp?: PostgRestOperator, primaryKeys?: Map<string, PrimaryKey>) => DataProvider;
|
|
9
|
+
export default _default;
|