@raphiniert/ra-data-postgrest 2.0.0-alpha.1 → 2.0.0-alpha.4
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 +49 -0
- package/IDEAS.md +46 -0
- package/README.md +50 -17
- package/esm/index.d.ts +58 -7
- package/esm/index.js +220 -219
- package/esm/urlBuilder.d.ts +4 -4
- package/esm/urlBuilder.js +95 -28
- package/lib/index.d.ts +58 -7
- package/lib/index.js +221 -218
- package/lib/urlBuilder.d.ts +4 -4
- package/lib/urlBuilder.js +102 -35
- package/package.json +10 -10
- package/src/index.ts +208 -138
- package/src/urlBuilder.ts +96 -31
package/esm/urlBuilder.js
CHANGED
|
@@ -9,6 +9,17 @@ var __assign = (this && this.__assign) || function () {
|
|
|
9
9
|
};
|
|
10
10
|
return __assign.apply(this, arguments);
|
|
11
11
|
};
|
|
12
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
13
|
+
var t = {};
|
|
14
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
15
|
+
t[p] = s[p];
|
|
16
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
17
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
18
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
19
|
+
t[p[i]] = s[p[i]];
|
|
20
|
+
}
|
|
21
|
+
return t;
|
|
22
|
+
};
|
|
12
23
|
/**
|
|
13
24
|
* List of all possible operators in PostgRest
|
|
14
25
|
* (https://postgrest.org/en/stable/api.html#operators)
|
|
@@ -42,34 +53,88 @@ var postgrestOperators = [
|
|
|
42
53
|
'or',
|
|
43
54
|
'and',
|
|
44
55
|
];
|
|
56
|
+
var isObject = function (obj) {
|
|
57
|
+
return typeof obj === 'object' && !Array.isArray(obj) && obj !== null;
|
|
58
|
+
};
|
|
59
|
+
var resolveKeys = function (filter, keys) {
|
|
60
|
+
var result = filter[keys[0]];
|
|
61
|
+
for (var i = 1; i < keys.length; ++i) {
|
|
62
|
+
result = result[keys[i]];
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
};
|
|
45
66
|
export var parseFilters = function (params, defaultListOp) {
|
|
46
67
|
var filter = params.filter, _a = params.meta, meta = _a === void 0 ? {} : _a;
|
|
47
68
|
var result = {};
|
|
48
69
|
result.filter = {};
|
|
49
70
|
Object.keys(filter).forEach(function (key) {
|
|
50
71
|
// key: the name of the object key
|
|
72
|
+
var keyArray = [key];
|
|
73
|
+
var values;
|
|
74
|
+
// in case of a nested object selection in a text field
|
|
75
|
+
// RA returns object{ object } structure which is resolved here
|
|
76
|
+
// see issue https://github.com/raphiniert-com/ra-data-postgrest/issues/58
|
|
77
|
+
if (key.split('@')[0] !== '' && isObject(filter[key])) {
|
|
78
|
+
var innerVal = filter[key];
|
|
79
|
+
do {
|
|
80
|
+
var inner = resolveKeys(filter, keyArray);
|
|
81
|
+
var innerKey = Object.keys(inner)[0];
|
|
82
|
+
keyArray.push(innerKey);
|
|
83
|
+
innerVal = inner[innerKey];
|
|
84
|
+
} while (isObject(innerVal));
|
|
85
|
+
key = keyArray.join('.');
|
|
86
|
+
values = [innerVal];
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
values = [filter[key]];
|
|
90
|
+
}
|
|
51
91
|
var splitKey = key.split('@');
|
|
52
92
|
var operation = splitKey.length == 2
|
|
53
93
|
? splitKey[1]
|
|
54
94
|
: defaultListOp;
|
|
55
|
-
var values;
|
|
56
95
|
if (['like', 'ilike'].includes(operation)) {
|
|
57
96
|
// we split the search term in words
|
|
58
|
-
values = filter
|
|
97
|
+
values = resolveKeys(filter, keyArray).trim().split(/\s+/);
|
|
59
98
|
}
|
|
60
|
-
|
|
61
|
-
|
|
99
|
+
// CASE: Logical operator
|
|
100
|
+
else if (['or', 'and'].includes(operation)) {
|
|
101
|
+
// we extract each value entries and make it dot separated string.
|
|
102
|
+
var subFilter = parseFilters({ filter: resolveKeys(filter, keyArray) }, defaultListOp).filter;
|
|
103
|
+
// something like { "age@lt": 18 } and { "age@gt": 21 }
|
|
104
|
+
var filterExpressions_1 = [];
|
|
105
|
+
Object.entries(subFilter).forEach(function (_a) {
|
|
106
|
+
var op = _a[0], val = _a[1];
|
|
107
|
+
if (Array.isArray(val))
|
|
108
|
+
filterExpressions_1.push.apply(filterExpressions_1, val.map(function (v) { return [op, v].join('.'); }));
|
|
109
|
+
else
|
|
110
|
+
filterExpressions_1.push([op, val].join('.'));
|
|
111
|
+
});
|
|
112
|
+
// finally we flatten all as single string and enclose with bracket.
|
|
113
|
+
values = ["(".concat(filterExpressions_1.join(','), ")")];
|
|
62
114
|
}
|
|
63
115
|
values.forEach(function (value) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
116
|
+
var op = (function () {
|
|
117
|
+
// if operator is intentionally blank, rpc syntax
|
|
118
|
+
if (operation.length === 0)
|
|
119
|
+
return "".concat(value);
|
|
120
|
+
if (operation.includes('like'))
|
|
121
|
+
return "".concat(operation, ".*").concat(value, "*");
|
|
122
|
+
if (['and', 'or'].includes(operation))
|
|
123
|
+
return "".concat(value);
|
|
124
|
+
if (['cs', 'cd'].includes(operation))
|
|
125
|
+
return "".concat(operation, ".{").concat(value, "}");
|
|
126
|
+
return "".concat(operation, ".").concat(value);
|
|
127
|
+
})();
|
|
128
|
+
// If resulting filter doesn't contain the fieldname, then add it.
|
|
70
129
|
if (result.filter[splitKey[0]] === undefined) {
|
|
71
|
-
// first operator for the key,
|
|
72
|
-
|
|
130
|
+
// first operator for the key,
|
|
131
|
+
if (['and', 'or'].includes(operation)) {
|
|
132
|
+
result.filter[operation] = op;
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
// we add it to the dict
|
|
136
|
+
result.filter[splitKey[0]] = op;
|
|
137
|
+
}
|
|
73
138
|
}
|
|
74
139
|
else {
|
|
75
140
|
if (!Array.isArray(result[splitKey[0]])) {
|
|
@@ -112,15 +177,29 @@ export var encodeId = function (data, primaryKey) {
|
|
|
112
177
|
return data[primaryKey[0]];
|
|
113
178
|
}
|
|
114
179
|
};
|
|
115
|
-
export var
|
|
116
|
-
|
|
180
|
+
export var removePrimaryKey = function (data, primaryKey) {
|
|
181
|
+
var newData = __assign({}, data);
|
|
182
|
+
primaryKey.forEach(function (key) {
|
|
183
|
+
delete newData[key];
|
|
184
|
+
});
|
|
185
|
+
return newData;
|
|
186
|
+
};
|
|
187
|
+
export var dataWithVirtualId = function (data, primaryKey) {
|
|
188
|
+
if (primaryKey.length === 1 && primaryKey[0] === 'id') {
|
|
117
189
|
return data;
|
|
118
190
|
}
|
|
119
191
|
return Object.assign(data, {
|
|
120
192
|
id: encodeId(data, primaryKey),
|
|
121
193
|
});
|
|
122
194
|
};
|
|
123
|
-
export var
|
|
195
|
+
export var dataWithoutVirtualId = function (data, primaryKey) {
|
|
196
|
+
if (primaryKey.length === 1 && primaryKey[0] === 'id') {
|
|
197
|
+
return data;
|
|
198
|
+
}
|
|
199
|
+
var id = data.id, dataWithoutId = __rest(data, ["id"]);
|
|
200
|
+
return dataWithoutId;
|
|
201
|
+
};
|
|
202
|
+
var isCompoundKey = function (primaryKey) {
|
|
124
203
|
return primaryKey.length > 1;
|
|
125
204
|
};
|
|
126
205
|
export var getQuery = function (primaryKey, ids, resource, meta) {
|
|
@@ -149,7 +228,7 @@ export var getQuery = function (primaryKey, ids, resource, meta) {
|
|
|
149
228
|
_a);
|
|
150
229
|
}
|
|
151
230
|
}
|
|
152
|
-
else {
|
|
231
|
+
else if (ids) {
|
|
153
232
|
// if ids is one Identifier
|
|
154
233
|
var id = ids.toString();
|
|
155
234
|
var primaryKeyParams_1 = decodeId(id, primaryKey);
|
|
@@ -181,18 +260,6 @@ export var getQuery = function (primaryKey, ids, resource, meta) {
|
|
|
181
260
|
}
|
|
182
261
|
return result;
|
|
183
262
|
};
|
|
184
|
-
export var getKeyData = function (primaryKey, data) {
|
|
185
|
-
var _a;
|
|
186
|
-
if (isCompoundKey(primaryKey)) {
|
|
187
|
-
return primaryKey.reduce(function (keyData, key) {
|
|
188
|
-
var _a;
|
|
189
|
-
return (__assign(__assign({}, keyData), (_a = {}, _a[key] = data[key], _a)));
|
|
190
|
-
}, {});
|
|
191
|
-
}
|
|
192
|
-
else {
|
|
193
|
-
return _a = {}, _a[primaryKey[0]] = data[primaryKey[0]], _a;
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
263
|
export var getOrderBy = function (field, order, primaryKey) {
|
|
197
264
|
if (field == 'id') {
|
|
198
265
|
return primaryKey.map(function (key) { return "".concat(key, ".").concat(order.toLowerCase()); }).join(',');
|
package/lib/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;
|