@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/lib/urlBuilder.js CHANGED
@@ -45,8 +45,10 @@ var postgrestOperators = [
45
45
  'or',
46
46
  'and',
47
47
  ];
48
- var parseFilters = function (filter, defaultListOp) {
48
+ var parseFilters = function (params, defaultListOp) {
49
+ var filter = params.filter, _a = params.meta, meta = _a === void 0 ? {} : _a;
49
50
  var result = {};
51
+ result.filter = {};
50
52
  Object.keys(filter).forEach(function (key) {
51
53
  // key: the name of the object key
52
54
  var splitKey = key.split('@');
@@ -58,32 +60,69 @@ var parseFilters = function (filter, defaultListOp) {
58
60
  // we split the search term in words
59
61
  values = filter[key].trim().split(/\s+/);
60
62
  }
63
+ // CASE: Logical operator
64
+ else if (['or', 'and'].includes(operation)) {
65
+ // we extract each value entries and make it dot separated string.
66
+ var subFilter = (0, exports.parseFilters)({ filter: filter[key] }, defaultListOp).filter;
67
+ // something like { "age@lt": 18 } and { "age@gt": 21 }
68
+ var filterExpressions_1 = [];
69
+ Object.entries(subFilter).forEach(function (_a) {
70
+ var op = _a[0], val = _a[1];
71
+ if (Array.isArray(val))
72
+ filterExpressions_1.push.apply(filterExpressions_1, val.map(function (v) { return [op, v].join('.'); }));
73
+ else
74
+ filterExpressions_1.push([op, val].join('.'));
75
+ });
76
+ // finally we flatten all as single string and enclose with bracket.
77
+ values = ["(".concat(filterExpressions_1.join(','), ")")];
78
+ }
61
79
  else {
62
80
  values = [filter[key]];
63
81
  }
64
82
  values.forEach(function (value) {
65
- // if operator is intentionally blank, rpc syntax
66
- var op = operation.includes('like')
67
- ? "".concat(operation, ".*").concat(value, "*")
68
- : operation.length == 0
69
- ? "".concat(value)
70
- : "".concat(operation, ".").concat(value);
71
- if (result[splitKey[0]] === undefined) {
72
- // first operator for the key, we add it to the dict
73
- result[splitKey[0]] = op;
83
+ var op = (function () {
84
+ // if operator is intentionally blank, rpc syntax
85
+ if (operation.length === 0)
86
+ return "".concat(value);
87
+ if (operation.includes('like'))
88
+ return "".concat(operation, ".*").concat(value, "*");
89
+ if (['and', 'or'].includes(operation))
90
+ return "".concat(value);
91
+ if (['cs', 'cd'].includes(operation))
92
+ return "".concat(operation, ".{").concat(value, "}");
93
+ return "".concat(operation, ".").concat(value);
94
+ })();
95
+ // If resulting filter haven't contains the fieldname, then add it.
96
+ if (result.filter[splitKey[0]] === undefined) {
97
+ // first operator for the key,
98
+ if (['and', 'or'].includes(operation)) {
99
+ result.filter[operation] = op;
100
+ }
101
+ else {
102
+ // we add it to the dict
103
+ result.filter[splitKey[0]] = op;
104
+ }
74
105
  }
75
106
  else {
76
107
  if (!Array.isArray(result[splitKey[0]])) {
77
108
  // second operator, we transform to an array
78
- result[splitKey[0]] = [result[splitKey[0]], op];
109
+ result.filter[splitKey[0]] = [
110
+ result.filter[splitKey[0]],
111
+ op,
112
+ ];
79
113
  }
80
114
  else {
81
115
  // third and subsequent, we add to array
82
- result[splitKey[0]].push(op);
116
+ result.filter[splitKey[0]].push(op);
83
117
  }
84
118
  }
85
119
  });
86
120
  });
121
+ if (meta === null || meta === void 0 ? void 0 : meta.columns) {
122
+ result.select = Array.isArray(meta.columns)
123
+ ? meta.columns.join(',')
124
+ : meta.columns;
125
+ }
87
126
  return result;
88
127
  };
89
128
  exports.parseFilters = parseFilters;
@@ -122,8 +161,10 @@ var isCompoundKey = function (primaryKey) {
122
161
  return primaryKey.length > 1;
123
162
  };
124
163
  exports.isCompoundKey = isCompoundKey;
125
- var getQuery = function (primaryKey, ids, resource) {
126
- var _a;
164
+ var getQuery = function (primaryKey, ids, resource, meta) {
165
+ var _a, _b;
166
+ if (meta === void 0) { meta = null; }
167
+ var result = {};
127
168
  if (Array.isArray(ids) && ids.length > 1) {
128
169
  // no standardized query with multiple ids possible for rpc endpoints which are api-exposed database functions
129
170
  if (resource.startsWith('rpc/')) {
@@ -131,18 +172,19 @@ var getQuery = function (primaryKey, ids, resource) {
131
172
  return;
132
173
  }
133
174
  if ((0, exports.isCompoundKey)(primaryKey)) {
134
- // TODO: Should be URL encoded
135
- return "or=(".concat(ids.map(function (id) {
136
- var primaryKeyParams = (0, exports.decodeId)(id, primaryKey);
137
- return "and(".concat(primaryKey
138
- .map(function (key, i) { return "".concat(key, ".eq.").concat(primaryKeyParams[i]); })
139
- .join(','), ")");
140
- }), ")");
175
+ result = {
176
+ or: "(".concat(ids.map(function (id) {
177
+ var primaryKeyParams = (0, exports.decodeId)(id, primaryKey);
178
+ return "and(".concat(primaryKey
179
+ .map(function (key, i) { return "".concat(key, ".eq.").concat(primaryKeyParams[i]); })
180
+ .join(','), ")");
181
+ }), ")"),
182
+ };
141
183
  }
142
184
  else {
143
- return new URLSearchParams((_a = {},
185
+ result = (_a = {},
144
186
  _a[primaryKey[0]] = "in.(".concat(ids.join(','), ")"),
145
- _a)).toString();
187
+ _a);
146
188
  }
147
189
  }
148
190
  else {
@@ -150,25 +192,32 @@ var getQuery = function (primaryKey, ids, resource) {
150
192
  var id = ids.toString();
151
193
  var primaryKeyParams_1 = (0, exports.decodeId)(id, primaryKey);
152
194
  if ((0, exports.isCompoundKey)(primaryKey)) {
153
- if (resource.startsWith('rpc/'))
154
- // TODO: Should be URL encoded
155
- return "".concat(primaryKey
156
- .map(function (key, i) { return "".concat(key, "=").concat(primaryKeyParams_1[i]); })
157
- .join('&'));
158
- // TODO: Should be URL encoded
159
- else
160
- return "and=(".concat(primaryKey
161
- .map(function (key, i) {
162
- return "".concat(key, ".eq.").concat(primaryKeyParams_1[i]);
163
- })
164
- .join(','), ")");
195
+ if (resource.startsWith('rpc/')) {
196
+ result = {};
197
+ primaryKey.map(function (key, i) {
198
+ return (result[key] = "".concat(primaryKeyParams_1[i]));
199
+ });
200
+ }
201
+ else {
202
+ result = {
203
+ and: "(".concat(primaryKey.map(function (key, i) {
204
+ return "".concat(key, ".eq.").concat(primaryKeyParams_1[i]);
205
+ }), ")"),
206
+ };
207
+ }
165
208
  }
166
209
  else {
167
- return new URLSearchParams([
168
- [primaryKey[0], "eq.".concat(id)],
169
- ]).toString();
210
+ result = (_b = {},
211
+ _b[primaryKey[0]] = "eq.".concat(id),
212
+ _b);
170
213
  }
171
214
  }
215
+ if (meta && meta.columns) {
216
+ result.select = Array.isArray(meta.columns)
217
+ ? meta.columns.join(',')
218
+ : meta.columns;
219
+ }
220
+ return result;
172
221
  };
173
222
  exports.getQuery = getQuery;
174
223
  var getKeyData = function (primaryKey, data) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raphiniert/ra-data-postgrest",
3
- "version": "2.0.0-alpha.0",
3
+ "version": "2.0.0-alpha.3",
4
4
  "description": "postgREST data provider for react-admin",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -20,7 +20,8 @@
20
20
  "bugs": "https://github.com/raphiniert-com/ra-data-postgrest/issues",
21
21
  "license": "MIT",
22
22
  "scripts": {
23
- "test": "jest --watch",
23
+ "coverage": "jest --coverage",
24
+ "test": "jest --watchAll",
24
25
  "build": "npm run build-cjs && npm run build-esm",
25
26
  "build-cjs": "rimraf ./lib && tsc",
26
27
  "build-esm": "rimraf ./esm && tsc --outDir esm --module es2015",
@@ -30,17 +31,21 @@
30
31
  },
31
32
  "devDependencies": {
32
33
  "@types/jest": "^28.1.4",
34
+ "@types/qs": "^6.9.7",
33
35
  "cross-env": "^7.0.3",
34
- "eslint-config-prettier": "^8.5.0",
36
+ "eslint-config-prettier": "^8.8.0",
35
37
  "eslint-plugin-prettier": "^4.2.1",
36
38
  "install-peers-cli": "^2.2.0",
37
39
  "jest": "^28.1.2",
38
- "prettier": "~2.1.1",
39
- "rimraf": "^3.0.2",
40
+ "prettier": "~2.8.8",
41
+ "rimraf": "^5.0.0",
40
42
  "ts-jest": "^28.0.5",
41
- "typescript": "^4.9.3"
43
+ "typescript": "^5.0.4"
42
44
  },
43
45
  "peerDependencies": {
44
46
  "ra-core": "^4.1.0"
47
+ },
48
+ "dependencies": {
49
+ "qs": "^6.11.1"
45
50
  }
46
51
  }