@raphiniert/ra-data-postgrest 2.0.0 → 2.2.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/lib/index.js CHANGED
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.defaultSchema = exports.defaultPrimaryKeys = void 0;
18
18
  var urlBuilder_1 = require("./urlBuilder");
19
19
  var qs_1 = __importDefault(require("qs"));
20
+ var isEqual_1 = __importDefault(require("lodash/isEqual"));
20
21
  exports.defaultPrimaryKeys = new Map();
21
22
  var defaultSchema = function () { return ''; };
22
23
  exports.defaultSchema = defaultSchema;
@@ -65,7 +66,7 @@ exports.default = (function (config) { return ({
65
66
  return config.httpClient(url, options).then(function (_a) {
66
67
  var headers = _a.headers, json = _a.json;
67
68
  if (!headers.has('content-range')) {
68
- 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
+ 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
70
  }
70
71
  return {
71
72
  data: json.map(function (obj) { return (0, urlBuilder_1.dataWithVirtualId)(obj, primaryKey); }),
@@ -121,7 +122,7 @@ exports.default = (function (config) { return ({
121
122
  var primaryKey = (0, urlBuilder_1.getPrimaryKey)(resource, config.primaryKeys);
122
123
  var metaSchema = (_b = params.meta) === null || _b === void 0 ? void 0 : _b.schema;
123
124
  var query = params.target
124
- ? __assign((_a = {}, _a[params.target] = "eq.".concat(params.id), _a.order = (0, urlBuilder_1.getOrderBy)(field, order, primaryKey), _a.offset = String((page - 1) * perPage), _a.limit = String(perPage), _a), filter) : __assign({ order: (0, urlBuilder_1.getOrderBy)(field, order, primaryKey), offset: String((page - 1) * perPage), limit: String(perPage) }, filter);
125
+ ? __assign((_a = {}, _a[params.target] = "eq.".concat(params.id), _a.order = (0, urlBuilder_1.getOrderBy)(field, order, primaryKey, config.sortOrder), _a.offset = String((page - 1) * perPage), _a.limit = String(perPage), _a), filter) : __assign({ order: (0, urlBuilder_1.getOrderBy)(field, order, primaryKey), offset: String((page - 1) * perPage), limit: String(perPage) }, filter);
125
126
  if (select) {
126
127
  query.select = select;
127
128
  }
@@ -133,7 +134,7 @@ exports.default = (function (config) { return ({
133
134
  return config.httpClient(url, options).then(function (_a) {
134
135
  var headers = _a.headers, json = _a.json;
135
136
  if (!headers.has('content-range')) {
136
- 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?");
137
+ 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?");
137
138
  }
138
139
  return {
139
140
  data: json.map(function (data) { return (0, urlBuilder_1.dataWithVirtualId)(data, primaryKey); }),
@@ -144,12 +145,13 @@ exports.default = (function (config) { return ({
144
145
  update: function (resource, params) {
145
146
  var _a, _b;
146
147
  if (params === void 0) { params = {}; }
147
- var id = params.id, data = params.data, meta = params.meta;
148
+ var id = params.id, data = params.data, meta = params.meta, previousData = params.previousData;
148
149
  var primaryKey = (0, urlBuilder_1.getPrimaryKey)(resource, config.primaryKeys);
149
150
  var query = (0, urlBuilder_1.getQuery)(primaryKey, id, resource, meta);
150
151
  var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs_1.default.stringify(query));
152
+ var changedData = getChanges(data, previousData);
151
153
  var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
152
- var body = JSON.stringify(__assign({}, (0, urlBuilder_1.dataWithoutVirtualId)((0, urlBuilder_1.removePrimaryKey)(data, primaryKey), primaryKey)));
154
+ var body = JSON.stringify(__assign({}, (0, urlBuilder_1.dataWithoutVirtualId)((0, urlBuilder_1.removePrimaryKey)(changedData, primaryKey), primaryKey)));
153
155
  return config
154
156
  .httpClient(url, {
155
157
  method: 'PATCH',
@@ -248,3 +250,12 @@ exports.default = (function (config) { return ({
248
250
  });
249
251
  },
250
252
  }); });
253
+ var getChanges = function (data, previousData) {
254
+ var changes = Object.keys(data).reduce(function (changes, key) {
255
+ if (!(0, isEqual_1.default)(data[key], previousData[key])) {
256
+ changes[key] = data[key];
257
+ }
258
+ return changes;
259
+ }, {});
260
+ return changes;
261
+ };
@@ -18,5 +18,11 @@ export declare const removePrimaryKey: (data: any, primaryKey: PrimaryKey) => an
18
18
  export declare const dataWithVirtualId: (data: any, primaryKey: PrimaryKey) => any;
19
19
  export declare const dataWithoutVirtualId: (data: any, primaryKey: PrimaryKey) => any;
20
20
  export declare const getQuery: (primaryKey: PrimaryKey, ids: Identifier | Array<Identifier> | undefined, resource: string, meta?: any) => any;
21
- export declare const getOrderBy: (field: string, order: string, primaryKey: PrimaryKey) => string;
21
+ export declare enum PostgRestSortOrder {
22
+ AscendingNullsLastDescendingNullsFirst = "asc,desc",
23
+ AscendingNullsLastDescendingNullsLast = "asc,desc.nullslast",
24
+ AscendingNullsFirstDescendingNullsFirst = "asc.nullsfirst,desc",
25
+ AscendingNullsFirstDescendingNullsLast = "asc.nullsfirst,desc.nullslast"
26
+ }
27
+ export declare const getOrderBy: (field: string, order: string, primaryKey: PrimaryKey, sortOrder?: PostgRestSortOrder) => string;
22
28
  export {};
package/lib/urlBuilder.js CHANGED
@@ -22,7 +22,7 @@ var __rest = (this && this.__rest) || function (s, e) {
22
22
  return t;
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.getOrderBy = exports.getQuery = exports.dataWithoutVirtualId = exports.dataWithVirtualId = exports.removePrimaryKey = exports.encodeId = exports.decodeId = exports.getPrimaryKey = exports.parseFilters = void 0;
25
+ exports.getOrderBy = exports.PostgRestSortOrder = exports.getQuery = exports.dataWithoutVirtualId = exports.dataWithVirtualId = exports.removePrimaryKey = exports.encodeId = exports.decodeId = exports.getPrimaryKey = exports.parseFilters = void 0;
26
26
  /**
27
27
  * List of all possible operators in PostgRest
28
28
  * (https://postgrest.org/en/stable/api.html#operators)
@@ -269,12 +269,21 @@ var getQuery = function (primaryKey, ids, resource, meta) {
269
269
  return result;
270
270
  };
271
271
  exports.getQuery = getQuery;
272
- var getOrderBy = function (field, order, primaryKey) {
272
+ var PostgRestSortOrder;
273
+ (function (PostgRestSortOrder) {
274
+ PostgRestSortOrder["AscendingNullsLastDescendingNullsFirst"] = "asc,desc";
275
+ PostgRestSortOrder["AscendingNullsLastDescendingNullsLast"] = "asc,desc.nullslast";
276
+ PostgRestSortOrder["AscendingNullsFirstDescendingNullsFirst"] = "asc.nullsfirst,desc";
277
+ PostgRestSortOrder["AscendingNullsFirstDescendingNullsLast"] = "asc.nullsfirst,desc.nullslast";
278
+ })(PostgRestSortOrder || (exports.PostgRestSortOrder = PostgRestSortOrder = {}));
279
+ var getOrderBy = function (field, order, primaryKey, sortOrder) {
280
+ if (sortOrder === void 0) { sortOrder = PostgRestSortOrder.AscendingNullsLastDescendingNullsFirst; }
281
+ var postgRestOrder = sortOrder.split(',')[order === 'ASC' ? 0 : 1];
273
282
  if (field == 'id') {
274
- return primaryKey.map(function (key) { return "".concat(key, ".").concat(order.toLowerCase()); }).join(',');
283
+ return primaryKey.map(function (key) { return "".concat(key, ".").concat(postgRestOrder); }).join(',');
275
284
  }
276
285
  else {
277
- return "".concat(field, ".").concat(order.toLowerCase());
286
+ return "".concat(field, ".").concat(postgRestOrder);
278
287
  }
279
288
  };
280
289
  exports.getOrderBy = getOrderBy;
package/package.json CHANGED
@@ -1,50 +1,51 @@
1
- {
2
- "name": "@raphiniert/ra-data-postgrest",
3
- "version": "2.0.0",
4
- "description": "postgREST data provider for react-admin",
5
- "main": "lib/index.js",
6
- "module": "esm/index.js",
7
- "sideEffects": false,
8
- "files": [
9
- "*.md",
10
- "lib",
11
- "esm",
12
- "src"
13
- ],
14
- "authors": [
15
- "Raphael Scheible"
16
- ],
17
- "repository": "raphiniert-com/ra-data-postgrest",
18
- "homepage": "https://github.com/raphiniert-com/ra-data-postgrest",
19
- "bugs": "https://github.com/raphiniert-com/ra-data-postgrest/issues",
20
- "license": "MIT",
21
- "scripts": {
22
- "coverage": "jest --coverage",
23
- "test": "jest --watchAll",
24
- "build": "npm run build-cjs && npm run build-esm",
25
- "build-cjs": "rimraf ./lib && tsc",
26
- "build-esm": "rimraf ./esm && tsc --outDir esm --module es2015",
27
- "watch": "tsc --outDir esm --module es2015 --watch",
28
- "prepare": "install-peers",
29
- "prettier": "prettier --config ./.prettierrc.js --write --list-different \"src/**/*.{js,json,ts,tsx,css,md}\""
30
- },
31
- "devDependencies": {
32
- "@types/jest": "^29.5.11",
33
- "@types/qs": "^6.9.11",
34
- "cross-env": "^7.0.3",
35
- "eslint-config-prettier": "^9.1.0",
36
- "eslint-plugin-prettier": "^5.0.1",
37
- "install-peers-cli": "^2.2.0",
38
- "jest": "^29.7.0",
39
- "prettier": "^3.1.1",
40
- "rimraf": "^5.0.0",
41
- "ts-jest": "^29.1.1",
42
- "typescript": "^5.3.3"
43
- },
44
- "peerDependencies": {
45
- "ra-core": "^4.1.0"
46
- },
47
- "dependencies": {
48
- "qs": "^6.11.1"
49
- }
50
- }
1
+ {
2
+ "name": "@raphiniert/ra-data-postgrest",
3
+ "version": "2.2.0",
4
+ "description": "postgREST data provider for react-admin",
5
+ "main": "lib/index.js",
6
+ "module": "esm/index.js",
7
+ "sideEffects": false,
8
+ "files": [
9
+ "*.md",
10
+ "lib",
11
+ "esm",
12
+ "src"
13
+ ],
14
+ "authors": [
15
+ "Raphael Scheible"
16
+ ],
17
+ "repository": "raphiniert-com/ra-data-postgrest",
18
+ "homepage": "https://github.com/raphiniert-com/ra-data-postgrest",
19
+ "bugs": "https://github.com/raphiniert-com/ra-data-postgrest/issues",
20
+ "license": "MIT",
21
+ "scripts": {
22
+ "coverage": "jest --coverage",
23
+ "test": "jest --watchAll",
24
+ "build": "npm run build-cjs && npm run build-esm",
25
+ "build-cjs": "rimraf ./lib && tsc",
26
+ "build-esm": "rimraf ./esm && tsc --outDir esm --module es2015",
27
+ "watch": "tsc --outDir esm --module es2015 --watch",
28
+ "prepare": "install-peers",
29
+ "prettier": "prettier --config ./.prettierrc.js --write --list-different \"src/**/*.{js,json,ts,tsx,css,md}\""
30
+ },
31
+ "devDependencies": {
32
+ "@types/jest": "^29.5.11",
33
+ "@types/qs": "^6.9.11",
34
+ "cross-env": "^7.0.3",
35
+ "eslint-config-prettier": "^9.1.0",
36
+ "eslint-plugin-prettier": "^5.0.1",
37
+ "install-peers-cli": "^2.2.0",
38
+ "jest": "^29.7.0",
39
+ "prettier": "^3.1.1",
40
+ "rimraf": "^5.0.0",
41
+ "ts-jest": "^29.1.1",
42
+ "typescript": "^5.3.3"
43
+ },
44
+ "peerDependencies": {
45
+ "ra-core": "^4.1.0"
46
+ },
47
+ "dependencies": {
48
+ "lodash.isequal": "^4.5.0",
49
+ "qs": "^6.11.1"
50
+ }
51
+ }