@raphiniert/ra-data-postgrest 2.0.0-alpha.1 → 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 +38 -0
- package/IDEAS.md +46 -0
- package/README.md +50 -17
- package/esm/index.d.ts +58 -7
- package/esm/index.js +226 -219
- package/esm/urlBuilder.js +37 -8
- package/lib/index.d.ts +58 -7
- package/lib/index.js +228 -219
- package/lib/urlBuilder.js +37 -8
- package/package.json +5 -4
- package/src/index.ts +180 -103
- package/src/urlBuilder.ts +38 -8
package/esm/index.js
CHANGED
|
@@ -9,234 +9,241 @@ var __assign = (this && this.__assign) || function () {
|
|
|
9
9
|
};
|
|
10
10
|
return __assign.apply(this, arguments);
|
|
11
11
|
};
|
|
12
|
-
import { fetchUtils, } from 'ra-core';
|
|
13
12
|
import { getPrimaryKey, parseFilters, getOrderBy, dataWithId, getQuery, getKeyData, encodeId, decodeId, isCompoundKey, } from './urlBuilder';
|
|
14
13
|
import qs from 'qs';
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
*
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
var
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
var query = __assign({ offset: String((page - 1) * perPage), limit: String(perPage) }, filter);
|
|
64
|
-
if (field) {
|
|
65
|
-
query.order = getOrderBy(field, order, primaryKey);
|
|
66
|
-
}
|
|
67
|
-
if (select) {
|
|
68
|
-
query.select = select;
|
|
14
|
+
export var defaultPrimaryKeys = new Map();
|
|
15
|
+
export var defaultSchema = function () { return ''; };
|
|
16
|
+
var useCustomSchema = function (schema, metaSchema, method) {
|
|
17
|
+
var _a;
|
|
18
|
+
var funcHeaderSchema = schema;
|
|
19
|
+
if (metaSchema !== undefined) {
|
|
20
|
+
funcHeaderSchema = function () { return metaSchema; };
|
|
21
|
+
}
|
|
22
|
+
if (funcHeaderSchema().length > 0) {
|
|
23
|
+
var schemaHeader = '';
|
|
24
|
+
if (['GET', 'HEAD'].includes(method)) {
|
|
25
|
+
schemaHeader = 'Accept-Profile';
|
|
26
|
+
}
|
|
27
|
+
else if (['POST', 'PATCH', 'PUT', 'DELETE'].includes(method)) {
|
|
28
|
+
schemaHeader = 'Content-Profile';
|
|
29
|
+
}
|
|
30
|
+
else
|
|
31
|
+
return {};
|
|
32
|
+
return _a = {}, _a[schemaHeader] = funcHeaderSchema(), _a;
|
|
33
|
+
}
|
|
34
|
+
else
|
|
35
|
+
return {};
|
|
36
|
+
};
|
|
37
|
+
export default (function (config) { return ({
|
|
38
|
+
getList: function (resource, params) {
|
|
39
|
+
var _a, _b;
|
|
40
|
+
if (params === void 0) { params = {}; }
|
|
41
|
+
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
|
|
42
|
+
var _c = params.pagination, page = _c.page, perPage = _c.perPage;
|
|
43
|
+
var _d = params.sort || {}, field = _d.field, order = _d.order;
|
|
44
|
+
var _e = parseFilters(params, config.defaultListOp), filter = _e.filter, select = _e.select;
|
|
45
|
+
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
|
|
46
|
+
var query = __assign({ offset: String((page - 1) * perPage), limit: String(perPage) }, filter);
|
|
47
|
+
if (field) {
|
|
48
|
+
query.order = getOrderBy(field, order, primaryKey);
|
|
49
|
+
}
|
|
50
|
+
if (select) {
|
|
51
|
+
query.select = select;
|
|
52
|
+
}
|
|
53
|
+
// add header that Content-Range is in returned header
|
|
54
|
+
var options = {
|
|
55
|
+
headers: new Headers(__assign(__assign({ Accept: 'application/json', Prefer: 'count=exact' }, (((_b = params.meta) === null || _b === void 0 ? void 0 : _b.headers) || {})), useCustomSchema(config.schema, metaSchema, 'GET'))),
|
|
56
|
+
};
|
|
57
|
+
var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
|
|
58
|
+
return config.httpClient(url, options).then(function (_a) {
|
|
59
|
+
var headers = _a.headers, json = _a.json;
|
|
60
|
+
if (!headers.has('content-range')) {
|
|
61
|
+
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?");
|
|
69
62
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
63
|
+
return {
|
|
64
|
+
data: json.map(function (obj) { return dataWithId(obj, primaryKey); }),
|
|
65
|
+
total: parseInt(headers.get('content-range').split('/').pop(), 10),
|
|
73
66
|
};
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
var
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
var url = "".concat(apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
|
|
93
|
-
return httpClient(url, {
|
|
94
|
-
headers: new Headers(__assign({ accept: 'application/vnd.pgrst.object+json' }, (((_a = params.meta) === null || _a === void 0 ? void 0 : _a.headers) || {}))),
|
|
95
|
-
}).then(function (_a) {
|
|
96
|
-
var json = _a.json;
|
|
97
|
-
return ({
|
|
98
|
-
data: dataWithId(json, primaryKey),
|
|
99
|
-
});
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
getOne: function (resource, params) {
|
|
70
|
+
var _a, _b;
|
|
71
|
+
if (params === void 0) { params = {}; }
|
|
72
|
+
var id = params.id, meta = params.meta;
|
|
73
|
+
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
|
|
74
|
+
var query = getQuery(primaryKey, id, resource, meta);
|
|
75
|
+
var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
|
|
76
|
+
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
|
|
77
|
+
return config
|
|
78
|
+
.httpClient(url, {
|
|
79
|
+
headers: new Headers(__assign(__assign({ accept: 'application/vnd.pgrst.object+json' }, (((_b = params.meta) === null || _b === void 0 ? void 0 : _b.headers) || {})), useCustomSchema(config.schema, metaSchema, 'GET'))),
|
|
80
|
+
})
|
|
81
|
+
.then(function (_a) {
|
|
82
|
+
var json = _a.json;
|
|
83
|
+
return ({
|
|
84
|
+
data: dataWithId(json, primaryKey),
|
|
100
85
|
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
86
|
+
});
|
|
87
|
+
},
|
|
88
|
+
getMany: function (resource, params) {
|
|
89
|
+
var _a;
|
|
90
|
+
if (params === void 0) { params = {}; }
|
|
91
|
+
var ids = params.ids;
|
|
92
|
+
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
|
|
93
|
+
var query = getQuery(primaryKey, ids, resource, params.meta);
|
|
94
|
+
var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
|
|
95
|
+
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
|
|
96
|
+
return config
|
|
97
|
+
.httpClient(url, {
|
|
98
|
+
headers: new Headers(__assign({}, useCustomSchema(config.schema, metaSchema, 'GET'))),
|
|
99
|
+
})
|
|
100
|
+
.then(function (_a) {
|
|
101
|
+
var json = _a.json;
|
|
102
|
+
return ({
|
|
103
|
+
data: json.map(function (data) { return dataWithId(data, primaryKey); }),
|
|
113
104
|
});
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
getManyReference: function (resource, params) {
|
|
108
|
+
var _a;
|
|
109
|
+
var _b, _c;
|
|
110
|
+
if (params === void 0) { params = {}; }
|
|
111
|
+
var _d = params.pagination, page = _d.page, perPage = _d.perPage;
|
|
112
|
+
var _e = params.sort, field = _e.field, order = _e.order;
|
|
113
|
+
var _f = parseFilters(params, config.defaultListOp), filter = _f.filter, select = _f.select;
|
|
114
|
+
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
|
|
115
|
+
var metaSchema = (_b = params.meta) === null || _b === void 0 ? void 0 : _b.schema;
|
|
116
|
+
var query = params.target
|
|
117
|
+
? __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), filter) : __assign({ order: getOrderBy(field, order, primaryKey), offset: String((page - 1) * perPage), limit: String(perPage) }, filter);
|
|
118
|
+
if (select) {
|
|
119
|
+
query.select = select;
|
|
120
|
+
}
|
|
121
|
+
// add header that Content-Range is in returned header
|
|
122
|
+
var options = {
|
|
123
|
+
headers: new Headers(__assign(__assign({ Accept: 'application/json', Prefer: 'count=exact' }, (((_c = params.meta) === null || _c === void 0 ? void 0 : _c.headers) || {})), useCustomSchema(config.schema, metaSchema, 'GET'))),
|
|
124
|
+
};
|
|
125
|
+
var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
|
|
126
|
+
return config.httpClient(url, options).then(function (_a) {
|
|
127
|
+
var headers = _a.headers, json = _a.json;
|
|
128
|
+
if (!headers.has('content-range')) {
|
|
129
|
+
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?");
|
|
127
130
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
+
return {
|
|
132
|
+
data: json.map(function (data) { return dataWithId(data, primaryKey); }),
|
|
133
|
+
total: parseInt(headers.get('content-range').split('/').pop(), 10),
|
|
131
134
|
};
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
},
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
var
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
var
|
|
164
|
-
if (
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
var body = JSON.stringify(params.ids.map(function (id) {
|
|
168
|
-
var data = params.data;
|
|
169
|
-
var primaryKeyParams = decodeId(id, primaryKey);
|
|
170
|
-
var primaryKeyData = {};
|
|
171
|
-
if (isCompoundKey(primaryKey)) {
|
|
172
|
-
primaryKey.forEach(function (key, index) {
|
|
173
|
-
primaryKeyData[key] = primaryKeyParams[index];
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
primaryKeyData[primaryKey[0]] = primaryKeyParams[0];
|
|
178
|
-
}
|
|
179
|
-
return __assign(__assign({}, data), primaryKeyData);
|
|
180
|
-
}));
|
|
181
|
-
var url = "".concat(apiUrl, "/").concat(resource);
|
|
182
|
-
return httpClient(url, {
|
|
183
|
-
method: 'PATCH',
|
|
184
|
-
headers: new Headers(__assign({ Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_a = params.meta) === null || _a === void 0 ? void 0 : _a.headers) || {}))),
|
|
185
|
-
body: body,
|
|
186
|
-
}).then(function (_a) {
|
|
187
|
-
var json = _a.json;
|
|
188
|
-
return ({
|
|
189
|
-
data: json.map(function (data) { return encodeId(data, primaryKey); }),
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
},
|
|
193
|
-
create: function (resource, params) {
|
|
194
|
-
var _a;
|
|
195
|
-
if (params === void 0) { params = {}; }
|
|
196
|
-
var primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
197
|
-
var url = "".concat(apiUrl, "/").concat(resource);
|
|
198
|
-
return httpClient(url, {
|
|
199
|
-
method: 'POST',
|
|
200
|
-
headers: new Headers(__assign({ Accept: 'application/vnd.pgrst.object+json', Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_a = params.meta) === null || _a === void 0 ? void 0 : _a.headers) || {}))),
|
|
201
|
-
body: JSON.stringify(params.data),
|
|
202
|
-
}).then(function (_a) {
|
|
203
|
-
var json = _a.json;
|
|
204
|
-
return ({
|
|
205
|
-
data: __assign(__assign({}, json), { id: encodeId(json, primaryKey) }),
|
|
135
|
+
});
|
|
136
|
+
},
|
|
137
|
+
update: function (resource, params) {
|
|
138
|
+
var _a, _b;
|
|
139
|
+
if (params === void 0) { params = {}; }
|
|
140
|
+
var id = params.id, data = params.data, meta = params.meta;
|
|
141
|
+
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
|
|
142
|
+
var query = getQuery(primaryKey, id, resource, meta);
|
|
143
|
+
var primaryKeyData = getKeyData(primaryKey, data);
|
|
144
|
+
var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
|
|
145
|
+
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
|
|
146
|
+
var body = JSON.stringify(__assign(__assign({}, data), primaryKeyData));
|
|
147
|
+
return config
|
|
148
|
+
.httpClient(url, {
|
|
149
|
+
method: 'PATCH',
|
|
150
|
+
headers: new Headers(__assign(__assign({ Accept: 'application/vnd.pgrst.object+json', Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_b = params.meta) === null || _b === void 0 ? void 0 : _b.headers) || {})), useCustomSchema(config.schema, metaSchema, 'PATCH'))),
|
|
151
|
+
body: body,
|
|
152
|
+
})
|
|
153
|
+
.then(function (_a) {
|
|
154
|
+
var json = _a.json;
|
|
155
|
+
return ({ data: dataWithId(json, primaryKey) });
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
updateMany: function (resource, params) {
|
|
159
|
+
var _a, _b;
|
|
160
|
+
if (params === void 0) { params = {}; }
|
|
161
|
+
var ids = params.ids;
|
|
162
|
+
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
|
|
163
|
+
var body = JSON.stringify(params.ids.map(function (id) {
|
|
164
|
+
var data = params.data;
|
|
165
|
+
var primaryKeyParams = decodeId(id, primaryKey);
|
|
166
|
+
var primaryKeyData = {};
|
|
167
|
+
if (isCompoundKey(primaryKey)) {
|
|
168
|
+
primaryKey.forEach(function (key, index) {
|
|
169
|
+
primaryKeyData[key] = primaryKeyParams[index];
|
|
206
170
|
});
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
primaryKeyData[primaryKey[0]] = primaryKeyParams[0];
|
|
174
|
+
}
|
|
175
|
+
return __assign(__assign({}, data), primaryKeyData);
|
|
176
|
+
}));
|
|
177
|
+
var url = "".concat(config.apiUrl, "/").concat(resource);
|
|
178
|
+
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
|
|
179
|
+
return config
|
|
180
|
+
.httpClient(url, {
|
|
181
|
+
method: 'PATCH',
|
|
182
|
+
headers: new Headers(__assign(__assign({ Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_b = params.meta) === null || _b === void 0 ? void 0 : _b.headers) || {})), useCustomSchema(config.schema, metaSchema, 'PATCH'))),
|
|
183
|
+
body: body,
|
|
184
|
+
})
|
|
185
|
+
.then(function (_a) {
|
|
186
|
+
var json = _a.json;
|
|
187
|
+
return ({
|
|
188
|
+
data: json.map(function (data) { return encodeId(data, primaryKey); }),
|
|
207
189
|
});
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}).
|
|
220
|
-
|
|
221
|
-
|
|
190
|
+
});
|
|
191
|
+
},
|
|
192
|
+
create: function (resource, params) {
|
|
193
|
+
var _a, _b;
|
|
194
|
+
if (params === void 0) { params = {}; }
|
|
195
|
+
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
|
|
196
|
+
var url = "".concat(config.apiUrl, "/").concat(resource);
|
|
197
|
+
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
|
|
198
|
+
return config
|
|
199
|
+
.httpClient(url, {
|
|
200
|
+
method: 'POST',
|
|
201
|
+
headers: new Headers(__assign(__assign({ Accept: 'application/vnd.pgrst.object+json', Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_b = params.meta) === null || _b === void 0 ? void 0 : _b.headers) || {})), useCustomSchema(config.schema, metaSchema, 'POST'))),
|
|
202
|
+
body: JSON.stringify(params.data),
|
|
203
|
+
})
|
|
204
|
+
.then(function (_a) {
|
|
205
|
+
var json = _a.json;
|
|
206
|
+
return ({
|
|
207
|
+
data: __assign(__assign({}, json), { id: encodeId(json, primaryKey) }),
|
|
222
208
|
});
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
209
|
+
});
|
|
210
|
+
},
|
|
211
|
+
delete: function (resource, params) {
|
|
212
|
+
var _a, _b;
|
|
213
|
+
if (params === void 0) { params = {}; }
|
|
214
|
+
var id = params.id, meta = params.meta;
|
|
215
|
+
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
|
|
216
|
+
var query = getQuery(primaryKey, id, resource, meta);
|
|
217
|
+
var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
|
|
218
|
+
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
|
|
219
|
+
return config
|
|
220
|
+
.httpClient(url, {
|
|
221
|
+
method: 'DELETE',
|
|
222
|
+
headers: new Headers(__assign(__assign({ Accept: 'application/vnd.pgrst.object+json', Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_b = params.meta) === null || _b === void 0 ? void 0 : _b.headers) || {})), useCustomSchema(config.schema, metaSchema, 'DELETE'))),
|
|
223
|
+
})
|
|
224
|
+
.then(function (_a) {
|
|
225
|
+
var json = _a.json;
|
|
226
|
+
return ({ data: dataWithId(json, primaryKey) });
|
|
227
|
+
});
|
|
228
|
+
},
|
|
229
|
+
deleteMany: function (resource, params) {
|
|
230
|
+
var _a, _b;
|
|
231
|
+
if (params === void 0) { params = {}; }
|
|
232
|
+
var ids = params.ids, meta = params.meta;
|
|
233
|
+
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
|
|
234
|
+
var query = getQuery(primaryKey, ids, resource, meta);
|
|
235
|
+
var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
|
|
236
|
+
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
|
|
237
|
+
return config
|
|
238
|
+
.httpClient(url, {
|
|
239
|
+
method: 'DELETE',
|
|
240
|
+
headers: new Headers(__assign(__assign({ Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_b = params.meta) === null || _b === void 0 ? void 0 : _b.headers) || {})), useCustomSchema(config.schema, metaSchema, 'DELETE'))),
|
|
241
|
+
})
|
|
242
|
+
.then(function (_a) {
|
|
243
|
+
var json = _a.json;
|
|
244
|
+
return ({
|
|
245
|
+
data: json.map(function (data) { return encodeId(data, primaryKey); }),
|
|
239
246
|
});
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
});
|
|
247
|
+
});
|
|
248
|
+
},
|
|
249
|
+
}); });
|
package/esm/urlBuilder.js
CHANGED
|
@@ -57,19 +57,48 @@ export var parseFilters = function (params, defaultListOp) {
|
|
|
57
57
|
// we split the search term in words
|
|
58
58
|
values = filter[key].trim().split(/\s+/);
|
|
59
59
|
}
|
|
60
|
+
// CASE: Logical operator
|
|
61
|
+
else if (['or', 'and'].includes(operation)) {
|
|
62
|
+
// we extract each value entries and make it dot separated string.
|
|
63
|
+
var subFilter = parseFilters({ filter: filter[key] }, defaultListOp).filter;
|
|
64
|
+
// something like { "age@lt": 18 } and { "age@gt": 21 }
|
|
65
|
+
var filterExpressions_1 = [];
|
|
66
|
+
Object.entries(subFilter).forEach(function (_a) {
|
|
67
|
+
var op = _a[0], val = _a[1];
|
|
68
|
+
if (Array.isArray(val))
|
|
69
|
+
filterExpressions_1.push.apply(filterExpressions_1, val.map(function (v) { return [op, v].join('.'); }));
|
|
70
|
+
else
|
|
71
|
+
filterExpressions_1.push([op, val].join('.'));
|
|
72
|
+
});
|
|
73
|
+
// finally we flatten all as single string and enclose with bracket.
|
|
74
|
+
values = ["(".concat(filterExpressions_1.join(','), ")")];
|
|
75
|
+
}
|
|
60
76
|
else {
|
|
61
77
|
values = [filter[key]];
|
|
62
78
|
}
|
|
63
79
|
values.forEach(function (value) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
80
|
+
var op = (function () {
|
|
81
|
+
// if operator is intentionally blank, rpc syntax
|
|
82
|
+
if (operation.length === 0)
|
|
83
|
+
return "".concat(value);
|
|
84
|
+
if (operation.includes('like'))
|
|
85
|
+
return "".concat(operation, ".*").concat(value, "*");
|
|
86
|
+
if (['and', 'or'].includes(operation))
|
|
87
|
+
return "".concat(value);
|
|
88
|
+
if (['cs', 'cd'].includes(operation))
|
|
89
|
+
return "".concat(operation, ".{").concat(value, "}");
|
|
90
|
+
return "".concat(operation, ".").concat(value);
|
|
91
|
+
})();
|
|
92
|
+
// If resulting filter haven't contains the fieldname, then add it.
|
|
70
93
|
if (result.filter[splitKey[0]] === undefined) {
|
|
71
|
-
// first operator for the key,
|
|
72
|
-
|
|
94
|
+
// first operator for the key,
|
|
95
|
+
if (['and', 'or'].includes(operation)) {
|
|
96
|
+
result.filter[operation] = op;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
// we add it to the dict
|
|
100
|
+
result.filter[splitKey[0]] = op;
|
|
101
|
+
}
|
|
73
102
|
}
|
|
74
103
|
else {
|
|
75
104
|
if (!Array.isArray(result[splitKey[0]])) {
|
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;
|