@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/lib/urlBuilder.js CHANGED
@@ -10,8 +10,19 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
+ var __rest = (this && this.__rest) || function (s, e) {
14
+ var t = {};
15
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16
+ t[p] = s[p];
17
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
18
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
19
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
20
+ t[p[i]] = s[p[i]];
21
+ }
22
+ return t;
23
+ };
13
24
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.getOrderBy = exports.getKeyData = exports.getQuery = exports.isCompoundKey = exports.dataWithId = exports.encodeId = exports.decodeId = exports.getPrimaryKey = exports.parseFilters = void 0;
25
+ exports.getOrderBy = exports.getQuery = exports.dataWithoutVirtualId = exports.dataWithVirtualId = exports.removePrimaryKey = exports.encodeId = exports.decodeId = exports.getPrimaryKey = exports.parseFilters = void 0;
15
26
  /**
16
27
  * List of all possible operators in PostgRest
17
28
  * (https://postgrest.org/en/stable/api.html#operators)
@@ -45,34 +56,88 @@ var postgrestOperators = [
45
56
  'or',
46
57
  'and',
47
58
  ];
59
+ var isObject = function (obj) {
60
+ return typeof obj === 'object' && !Array.isArray(obj) && obj !== null;
61
+ };
62
+ var resolveKeys = function (filter, keys) {
63
+ var result = filter[keys[0]];
64
+ for (var i = 1; i < keys.length; ++i) {
65
+ result = result[keys[i]];
66
+ }
67
+ return result;
68
+ };
48
69
  var parseFilters = function (params, defaultListOp) {
49
70
  var filter = params.filter, _a = params.meta, meta = _a === void 0 ? {} : _a;
50
71
  var result = {};
51
72
  result.filter = {};
52
73
  Object.keys(filter).forEach(function (key) {
53
74
  // key: the name of the object key
75
+ var keyArray = [key];
76
+ var values;
77
+ // in case of a nested object selection in a text field
78
+ // RA returns object{ object } structure which is resolved here
79
+ // see issue https://github.com/raphiniert-com/ra-data-postgrest/issues/58
80
+ if (key.split('@')[0] !== '' && isObject(filter[key])) {
81
+ var innerVal = filter[key];
82
+ do {
83
+ var inner = resolveKeys(filter, keyArray);
84
+ var innerKey = Object.keys(inner)[0];
85
+ keyArray.push(innerKey);
86
+ innerVal = inner[innerKey];
87
+ } while (isObject(innerVal));
88
+ key = keyArray.join('.');
89
+ values = [innerVal];
90
+ }
91
+ else {
92
+ values = [filter[key]];
93
+ }
54
94
  var splitKey = key.split('@');
55
95
  var operation = splitKey.length == 2
56
96
  ? splitKey[1]
57
97
  : defaultListOp;
58
- var values;
59
98
  if (['like', 'ilike'].includes(operation)) {
60
99
  // we split the search term in words
61
- values = filter[key].trim().split(/\s+/);
100
+ values = resolveKeys(filter, keyArray).trim().split(/\s+/);
62
101
  }
63
- else {
64
- values = [filter[key]];
102
+ // CASE: Logical operator
103
+ else if (['or', 'and'].includes(operation)) {
104
+ // we extract each value entries and make it dot separated string.
105
+ var subFilter = (0, exports.parseFilters)({ filter: resolveKeys(filter, keyArray) }, defaultListOp).filter;
106
+ // something like { "age@lt": 18 } and { "age@gt": 21 }
107
+ var filterExpressions_1 = [];
108
+ Object.entries(subFilter).forEach(function (_a) {
109
+ var op = _a[0], val = _a[1];
110
+ if (Array.isArray(val))
111
+ filterExpressions_1.push.apply(filterExpressions_1, val.map(function (v) { return [op, v].join('.'); }));
112
+ else
113
+ filterExpressions_1.push([op, val].join('.'));
114
+ });
115
+ // finally we flatten all as single string and enclose with bracket.
116
+ values = ["(".concat(filterExpressions_1.join(','), ")")];
65
117
  }
66
118
  values.forEach(function (value) {
67
- // if operator is intentionally blank, rpc syntax
68
- var op = operation.includes('like')
69
- ? "".concat(operation, ".*").concat(value, "*")
70
- : operation.length == 0
71
- ? "".concat(value)
72
- : "".concat(operation, ".").concat(value);
119
+ var op = (function () {
120
+ // if operator is intentionally blank, rpc syntax
121
+ if (operation.length === 0)
122
+ return "".concat(value);
123
+ if (operation.includes('like'))
124
+ return "".concat(operation, ".*").concat(value, "*");
125
+ if (['and', 'or'].includes(operation))
126
+ return "".concat(value);
127
+ if (['cs', 'cd'].includes(operation))
128
+ return "".concat(operation, ".{").concat(value, "}");
129
+ return "".concat(operation, ".").concat(value);
130
+ })();
131
+ // If resulting filter doesn't contain the fieldname, then add it.
73
132
  if (result.filter[splitKey[0]] === undefined) {
74
- // first operator for the key, we add it to the dict
75
- result.filter[splitKey[0]] = op;
133
+ // first operator for the key,
134
+ if (['and', 'or'].includes(operation)) {
135
+ result.filter[operation] = op;
136
+ }
137
+ else {
138
+ // we add it to the dict
139
+ result.filter[splitKey[0]] = op;
140
+ }
76
141
  }
77
142
  else {
78
143
  if (!Array.isArray(result[splitKey[0]])) {
@@ -102,7 +167,7 @@ var getPrimaryKey = function (resource, primaryKeys) {
102
167
  };
103
168
  exports.getPrimaryKey = getPrimaryKey;
104
169
  var decodeId = function (id, primaryKey) {
105
- if ((0, exports.isCompoundKey)(primaryKey)) {
170
+ if (isCompoundKey(primaryKey)) {
106
171
  return JSON.parse(id.toString());
107
172
  }
108
173
  else {
@@ -111,7 +176,7 @@ var decodeId = function (id, primaryKey) {
111
176
  };
112
177
  exports.decodeId = decodeId;
113
178
  var encodeId = function (data, primaryKey) {
114
- if ((0, exports.isCompoundKey)(primaryKey)) {
179
+ if (isCompoundKey(primaryKey)) {
115
180
  return JSON.stringify(primaryKey.map(function (key) { return data[key]; }));
116
181
  }
117
182
  else {
@@ -119,19 +184,34 @@ var encodeId = function (data, primaryKey) {
119
184
  }
120
185
  };
121
186
  exports.encodeId = encodeId;
122
- var dataWithId = function (data, primaryKey) {
123
- if (JSON.stringify(primaryKey) === JSON.stringify(['id'])) {
187
+ var removePrimaryKey = function (data, primaryKey) {
188
+ var newData = __assign({}, data);
189
+ primaryKey.forEach(function (key) {
190
+ delete newData[key];
191
+ });
192
+ return newData;
193
+ };
194
+ exports.removePrimaryKey = removePrimaryKey;
195
+ var dataWithVirtualId = function (data, primaryKey) {
196
+ if (primaryKey.length === 1 && primaryKey[0] === 'id') {
124
197
  return data;
125
198
  }
126
199
  return Object.assign(data, {
127
200
  id: (0, exports.encodeId)(data, primaryKey),
128
201
  });
129
202
  };
130
- exports.dataWithId = dataWithId;
203
+ exports.dataWithVirtualId = dataWithVirtualId;
204
+ var dataWithoutVirtualId = function (data, primaryKey) {
205
+ if (primaryKey.length === 1 && primaryKey[0] === 'id') {
206
+ return data;
207
+ }
208
+ var id = data.id, dataWithoutId = __rest(data, ["id"]);
209
+ return dataWithoutId;
210
+ };
211
+ exports.dataWithoutVirtualId = dataWithoutVirtualId;
131
212
  var isCompoundKey = function (primaryKey) {
132
213
  return primaryKey.length > 1;
133
214
  };
134
- exports.isCompoundKey = isCompoundKey;
135
215
  var getQuery = function (primaryKey, ids, resource, meta) {
136
216
  var _a, _b;
137
217
  if (meta === void 0) { meta = null; }
@@ -142,7 +222,7 @@ var getQuery = function (primaryKey, ids, resource, meta) {
142
222
  console.error("PostgREST's rpc endpoints are not intended to be handled as views. Therefore, no query generation for multiple key values implemented!");
143
223
  return;
144
224
  }
145
- if ((0, exports.isCompoundKey)(primaryKey)) {
225
+ if (isCompoundKey(primaryKey)) {
146
226
  result = {
147
227
  or: "(".concat(ids.map(function (id) {
148
228
  var primaryKeyParams = (0, exports.decodeId)(id, primaryKey);
@@ -158,11 +238,11 @@ var getQuery = function (primaryKey, ids, resource, meta) {
158
238
  _a);
159
239
  }
160
240
  }
161
- else {
241
+ else if (ids) {
162
242
  // if ids is one Identifier
163
243
  var id = ids.toString();
164
244
  var primaryKeyParams_1 = (0, exports.decodeId)(id, primaryKey);
165
- if ((0, exports.isCompoundKey)(primaryKey)) {
245
+ if (isCompoundKey(primaryKey)) {
166
246
  if (resource.startsWith('rpc/')) {
167
247
  result = {};
168
248
  primaryKey.map(function (key, i) {
@@ -191,19 +271,6 @@ var getQuery = function (primaryKey, ids, resource, meta) {
191
271
  return result;
192
272
  };
193
273
  exports.getQuery = getQuery;
194
- var getKeyData = function (primaryKey, data) {
195
- var _a;
196
- if ((0, exports.isCompoundKey)(primaryKey)) {
197
- return primaryKey.reduce(function (keyData, key) {
198
- var _a;
199
- return (__assign(__assign({}, keyData), (_a = {}, _a[key] = data[key], _a)));
200
- }, {});
201
- }
202
- else {
203
- return _a = {}, _a[primaryKey[0]] = data[primaryKey[0]], _a;
204
- }
205
- };
206
- exports.getKeyData = getKeyData;
207
274
  var getOrderBy = function (field, order, primaryKey) {
208
275
  if (field == 'id') {
209
276
  return primaryKey.map(function (key) { return "".concat(key, ".").concat(order.toLowerCase()); }).join(',');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raphiniert/ra-data-postgrest",
3
- "version": "2.0.0-alpha.1",
3
+ "version": "2.0.0-alpha.4",
4
4
  "description": "postgREST data provider for react-admin",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -12,7 +12,6 @@
12
12
  "src"
13
13
  ],
14
14
  "authors": [
15
- "François Zaninotto",
16
15
  "Raphael Scheible"
17
16
  ],
18
17
  "repository": "raphiniert-com/ra-data-postgrest",
@@ -30,16 +29,17 @@
30
29
  "prettier": "prettier --config ./.prettierrc.js --write --list-different \"src/**/*.{js,json,ts,tsx,css,md}\""
31
30
  },
32
31
  "devDependencies": {
33
- "@types/jest": "^28.1.4",
32
+ "@types/jest": "^29.5.6",
33
+ "@types/qs": "^6.9.7",
34
34
  "cross-env": "^7.0.3",
35
- "eslint-config-prettier": "^8.8.0",
36
- "eslint-plugin-prettier": "^4.2.1",
35
+ "eslint-config-prettier": "^9.0.0",
36
+ "eslint-plugin-prettier": "^5.0.1",
37
37
  "install-peers-cli": "^2.2.0",
38
- "jest": "^28.1.2",
39
- "prettier": "~2.8.7",
40
- "rimraf": "^4.4.1",
41
- "ts-jest": "^28.0.5",
42
- "typescript": "^4.9.5"
38
+ "jest": "^29.7.0",
39
+ "prettier": "^3.0.3",
40
+ "rimraf": "^5.0.0",
41
+ "ts-jest": "^29.1.1",
42
+ "typescript": "^5.0.4"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "ra-core": "^4.1.0"