cozy-pouch-link 57.5.0 → 57.6.1

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.
Files changed (45) hide show
  1. package/dist/CozyPouchLink.js +234 -470
  2. package/dist/CozyPouchLink.spec.js +6 -147
  3. package/dist/PouchManager.js +43 -8
  4. package/dist/PouchManager.spec.js +21 -12
  5. package/dist/__mocks__/@op-engineering/op-sqlite.js +11 -0
  6. package/dist/db/dbInterface.js +190 -0
  7. package/dist/db/helpers.js +128 -0
  8. package/dist/db/pouchdb/getDocs.js +157 -0
  9. package/dist/db/pouchdb/getDocs.spec.js +63 -0
  10. package/dist/db/pouchdb/pouchdb.js +264 -0
  11. package/dist/db/pouchdb/pouchdb.spec.js +151 -0
  12. package/dist/db/sqlite/sql.js +422 -0
  13. package/dist/db/sqlite/sql.spec.js +419 -0
  14. package/dist/db/sqlite/sqliteDb.js +41 -0
  15. package/dist/db/sqlite/sqliteDb.native.js +317 -0
  16. package/dist/errors.js +17 -2
  17. package/dist/helpers.js +21 -147
  18. package/dist/helpers.spec.js +1 -98
  19. package/dist/index.js +9 -1
  20. package/dist/jsonapi.js +57 -21
  21. package/dist/jsonapi.spec.js +105 -32
  22. package/dist/mango.js +146 -3
  23. package/dist/migrations/pouchdb.js +32 -0
  24. package/dist/replicateOnce.js +25 -23
  25. package/dist/types.js +5 -0
  26. package/dist/utils.js +33 -3
  27. package/package.json +5 -3
  28. package/types/CozyPouchLink.d.ts +6 -63
  29. package/types/PouchManager.d.ts +6 -1
  30. package/types/__mocks__/@op-engineering/op-sqlite.d.ts +1 -0
  31. package/types/db/dbInterface.d.ts +117 -0
  32. package/types/db/helpers.d.ts +4 -0
  33. package/types/db/pouchdb/getDocs.d.ts +18 -0
  34. package/types/db/pouchdb/pouchdb.d.ts +8 -0
  35. package/types/db/sqlite/sql.d.ts +45 -0
  36. package/types/db/sqlite/sqliteDb.d.ts +4 -0
  37. package/types/db/sqlite/sqliteDb.native.d.ts +7 -0
  38. package/types/errors.d.ts +2 -0
  39. package/types/helpers.d.ts +1 -4
  40. package/types/index.d.ts +1 -0
  41. package/types/jsonapi.d.ts +4 -2
  42. package/types/mango.d.ts +19 -1
  43. package/types/migrations/pouchdb.d.ts +1 -0
  44. package/types/types.d.ts +2 -0
  45. package/types/utils.d.ts +3 -0
@@ -33,17 +33,15 @@ var _pouchdbBrowser = _interopRequireDefault(require("pouchdb-browser"));
33
33
 
34
34
  var _pouchdbFind = _interopRequireDefault(require("pouchdb-find"));
35
35
 
36
- var _omit = _interopRequireDefault(require("lodash/omit"));
37
-
38
36
  var _defaults = _interopRequireDefault(require("lodash/defaults"));
39
37
 
40
38
  var _zipWith = _interopRequireDefault(require("lodash/zipWith"));
41
39
 
42
40
  var _debounce = _interopRequireDefault(require("lodash/debounce"));
43
41
 
44
- var _helpers = _interopRequireDefault(require("./helpers"));
42
+ var _omit = _interopRequireDefault(require("lodash/omit"));
45
43
 
46
- var _mango = require("./mango");
44
+ var _helpers = _interopRequireDefault(require("./helpers"));
47
45
 
48
46
  var jsonapi = _interopRequireWildcard(require("./jsonapi"));
49
47
 
@@ -61,6 +59,10 @@ var _utils = require("./utils");
61
59
 
62
60
  var _errors = require("./errors");
63
61
 
62
+ var _pouchdb = _interopRequireDefault(require("./db/pouchdb/pouchdb"));
63
+
64
+ var _helpers2 = require("./db/helpers");
65
+
64
66
  function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
65
67
 
66
68
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
@@ -77,10 +79,6 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
77
79
 
78
80
  _pouchdbBrowser.default.plugin(_pouchdbFind.default);
79
81
 
80
- var find = _helpers.default.find,
81
- allDocs = _helpers.default.allDocs,
82
- withoutDesignDocuments = _helpers.default.withoutDesignDocuments;
83
-
84
82
  var parseMutationResult = function parseMutationResult(original, res) {
85
83
  if (!res.ok) {
86
84
  throw new Error('Pouch response is not OK');
@@ -99,10 +97,6 @@ var addBasicAuth = function addBasicAuth(url, basicAuth) {
99
97
  return url.replace('//', "//".concat(basicAuth));
100
98
  };
101
99
 
102
- var sanitized = function sanitized(doc) {
103
- return (0, _omit.default)(doc, '_type');
104
- };
105
-
106
100
  var _getReplicationURL = function getReplicationURL(uri, token, doctype) {
107
101
  var basicAuth = token.toBasicAuth();
108
102
  var authenticatedURL = addBasicAuth(uri, basicAuth);
@@ -115,8 +109,7 @@ var doNothing = function doNothing(operation) {
115
109
  var result = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
116
110
  };
117
111
  /**
118
- * @typedef {import('cozy-client/src/types').CozyClientDocument} CozyClientDocument
119
- *
112
+ * @typedef {import('cozy-client/src/types').CozyClientDocument & { cozyPouchData: any }} CozyPouchDocument *
120
113
  * @typedef {"idle"|"replicating"} ReplicationStatus
121
114
  */
122
115
 
@@ -343,6 +336,7 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
343
336
  var _this$options,
344
337
  _this$options$pouch,
345
338
  _this$options$pouch$o,
339
+ _this$options$platfor,
346
340
  _this2 = this;
347
341
 
348
342
  var markName, prefix, shouldDestroyDatabases, adapterName;
@@ -420,6 +414,7 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
420
414
  return this.storage.persistAdapterName(adapterName);
421
415
 
422
416
  case 26:
417
+ this.queryEngine = ((_this$options$platfor = this.options.platform) === null || _this$options$platfor === void 0 ? void 0 : _this$options$platfor.queryEngine) || _pouchdb.default;
423
418
  this.pouches = new _PouchManager.default(this.doctypes, {
424
419
  pouch: this.options.pouch,
425
420
  getReplicationURL: this.getReplicationURL.bind(this),
@@ -432,12 +427,14 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
432
427
  onDoctypeSyncEnd: this.handleDoctypeSyncEnd.bind(this),
433
428
  prefix: prefix,
434
429
  executeQuery: this.executeQuery.bind(this),
435
- platform: this.options.platform
430
+ platform: this.options.platform,
431
+ queryEngine: this.queryEngine,
432
+ client: this.client
436
433
  });
437
- _context3.next = 29;
434
+ _context3.next = 30;
438
435
  return this.pouches.init();
439
436
 
440
- case 29:
437
+ case 30:
441
438
  if (this.client && this.initialSync) {
442
439
  this.startReplication();
443
440
  }
@@ -447,7 +444,7 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
447
444
  category: 'CozyPouchLink'
448
445
  });
449
446
 
450
- case 31:
447
+ case 32:
451
448
  case "end":
452
449
  return _context3.stop();
453
450
  }
@@ -707,10 +704,17 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
707
704
  value: function getSyncInfo(doctype) {
708
705
  return this.pouches.getSyncInfo(doctype);
709
706
  }
707
+ }, {
708
+ key: "getQueryEngineFromDoctype",
709
+ value: function getQueryEngineFromDoctype(doctype) {
710
+ var dbName = (0, _utils.getDatabaseName)((0, _utils.getPrefix)(this.client.stackClient.uri), doctype);
711
+ return this.pouches.getQueryEngine(dbName, doctype);
712
+ }
710
713
  }, {
711
714
  key: "getPouch",
712
715
  value: function getPouch(doctype) {
713
- return this.pouches.getPouch(doctype);
716
+ var dbName = (0, _utils.getDatabaseName)((0, _utils.getPrefix)(this.client.stackClient.uri), doctype);
717
+ return this.pouches.getPouch(dbName);
714
718
  }
715
719
  }, {
716
720
  key: "supportsOperation",
@@ -908,146 +912,100 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
908
912
 
909
913
  return getDbInfo;
910
914
  }()
911
- }, {
912
- key: "sanitizeJsonApi",
913
- value: function sanitizeJsonApi(data) {
914
- var docWithoutType = sanitized(data);
915
- /*
916
- We persist in the local Pouch database all the documents that do not
917
- exist on the remote Couch database
918
- Those documents are computed by the cozy-stack then are sent to the
919
- client using JSON-API format containing `attributes` and `meta`
920
- attributes
921
- Then the cozy-stack-client would normalize those documents by spreading
922
- `attributes` and `meta` content into the document's root
923
- So we don't need to store `attributes` and `meta` data into the Pouch
924
- database as their data already exists in the document's root
925
- Note that this is also the case for `links` and `relationships`
926
- attributes, but we don't remove them for now. They are also part of the
927
- JSON-API, but the normalization do not spread them in the document's
928
- root, so we have to check their usefulnes first
929
- */
930
-
931
- var sanitizedDoc = (0, _omit.default)(docWithoutType, ['attributes', 'meta']);
932
- return sanitizedDoc;
933
- }
915
+ /**
916
+ * We persist in the local Pouch database all the documents that do not
917
+ * exist on the remote Couch database.
918
+ *
919
+ * Those documents are computed by the cozy-stack then are sent to the
920
+ * client using JSON-API format
921
+ */
922
+
934
923
  }, {
935
924
  key: "persistCozyData",
936
925
  value: function () {
937
- var _persistCozyData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee9(data) {
926
+ var _persistCozyData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee9(doc) {
938
927
  var forward,
939
928
  markName,
940
929
  sanitizedDoc,
930
+ engine,
931
+ pouch,
932
+ resp,
941
933
  oldDoc,
942
- db,
943
934
  _args9 = arguments;
944
935
  return _regenerator.default.wrap(function _callee9$(_context9) {
945
936
  while (1) {
946
937
  switch (_context9.prev = _context9.next) {
947
938
  case 0:
948
939
  forward = _args9.length > 1 && _args9[1] !== undefined ? _args9[1] : doNothing;
949
- markName = this.performanceApi.mark('persistCozyData');
950
- sanitizedDoc = this.sanitizeJsonApi(data);
940
+ markName = this.performanceApi.mark('persistCozyData'); // XXX - We don't need to store `attributes` and `meta` data into the Pouch
941
+ // database as their data already exists in the document's root, as its
942
+ // already done by cozy-stack-client.
943
+
944
+ sanitizedDoc = (0, jsonapi.sanitizeJsonApi)(doc);
945
+ _context9.prev = 3;
951
946
  sanitizedDoc.cozyLocalOnly = true;
952
- _context9.next = 6;
953
- return this.getExistingDocument(data._id, data._type);
947
+ engine = this.getQueryEngineFromDoctype(doc._type);
948
+ pouch = this.getPouch(doc._type);
949
+ _context9.next = 9;
950
+ return (0, _helpers2.getExistingDocument)(engine, sanitizedDoc._id);
954
951
 
955
- case 6:
956
- oldDoc = _context9.sent;
952
+ case 9:
953
+ resp = _context9.sent;
957
954
 
958
- if (oldDoc) {
959
- sanitizedDoc._rev = oldDoc._rev;
955
+ if (!(!(resp !== null && resp !== void 0 && resp.data) || Object.keys(resp === null || resp === void 0 ? void 0 : resp.data).length < 1)) {
956
+ _context9.next = 12;
957
+ break;
960
958
  }
961
959
 
962
- db = this.pouches.getPouch(data._type);
963
- _context9.next = 11;
964
- return db.put(sanitizedDoc);
965
-
966
- case 11:
967
- this.performanceApi.measure({
968
- markName: markName,
969
- category: 'CozyPouchLink'
970
- });
960
+ return _context9.abrupt("return", pouch.put(sanitizedDoc));
971
961
 
972
962
  case 12:
973
- case "end":
974
- return _context9.stop();
975
- }
976
- }
977
- }, _callee9, this);
978
- }));
963
+ oldDoc = (0, jsonapi.sanitizeJsonApi)(resp.data);
979
964
 
980
- function persistCozyData(_x9) {
981
- return _persistCozyData.apply(this, arguments);
982
- }
965
+ if (!(0, _helpers2.areDocsEqual)(oldDoc, sanitizedDoc)) {
966
+ _context9.next = 15;
967
+ break;
968
+ }
983
969
 
984
- return persistCozyData;
985
- }()
986
- /**
987
- * Retrieve the existing document from Pouch
988
- *
989
- * @private
990
- * @param {*} id - ID of the document to retrieve
991
- * @param {*} type - Doctype of the document to retrieve
992
- * @param {*} throwIfNotFound - If true the method will throw when the document is not found. Otherwise it will return null
993
- * @returns {Promise<CozyClientDocument | null>}
994
- */
970
+ return _context9.abrupt("return");
995
971
 
996
- }, {
997
- key: "getExistingDocument",
998
- value: function () {
999
- var _getExistingDocument = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee10(id, type) {
1000
- var throwIfNotFound,
1001
- db,
1002
- markName,
1003
- existingDoc,
1004
- _args10 = arguments;
1005
- return _regenerator.default.wrap(function _callee10$(_context10) {
1006
- while (1) {
1007
- switch (_context10.prev = _context10.next) {
1008
- case 0:
1009
- throwIfNotFound = _args10.length > 2 && _args10[2] !== undefined ? _args10[2] : false;
1010
- _context10.prev = 1;
1011
- db = this.pouches.getPouch(type);
1012
- markName = this.performanceApi.mark('db.get from getExistingDocument');
1013
- _context10.next = 6;
1014
- return db.get(id);
972
+ case 15:
973
+ sanitizedDoc._rev = oldDoc._rev;
974
+ _context9.next = 18;
975
+ return pouch.put(sanitizedDoc);
1015
976
 
1016
- case 6:
1017
- existingDoc = _context10.sent;
1018
- this.performanceApi.measure({
1019
- markName: markName,
1020
- category: 'PouchDB'
1021
- });
1022
- return _context10.abrupt("return", existingDoc);
977
+ case 18:
978
+ _context9.next = 24;
979
+ break;
1023
980
 
1024
- case 11:
1025
- _context10.prev = 11;
1026
- _context10.t0 = _context10["catch"](1);
981
+ case 20:
982
+ _context9.prev = 20;
983
+ _context9.t0 = _context9["catch"](3);
1027
984
 
1028
- if (!(_context10.t0.name === 'not_found' && !throwIfNotFound)) {
1029
- _context10.next = 17;
1030
- break;
1031
- }
985
+ _logger.default.error("PersistCozyData failed: with ".concat(_context9.t0)); // Do nothing on catch, to avoid throwing a read query
1032
986
 
1033
- return _context10.abrupt("return", null);
1034
987
 
1035
- case 17:
1036
- throw _context10.t0;
988
+ return _context9.abrupt("return", null);
1037
989
 
1038
- case 18:
990
+ case 24:
991
+ this.performanceApi.measure({
992
+ markName: markName,
993
+ category: 'CozyPouchLink'
994
+ });
995
+
996
+ case 25:
1039
997
  case "end":
1040
- return _context10.stop();
998
+ return _context9.stop();
1041
999
  }
1042
1000
  }
1043
- }, _callee10, this, [[1, 11]]);
1001
+ }, _callee9, this, [[3, 20]]);
1044
1002
  }));
1045
1003
 
1046
- function getExistingDocument(_x10, _x11) {
1047
- return _getExistingDocument.apply(this, arguments);
1004
+ function persistCozyData(_x9) {
1005
+ return _persistCozyData.apply(this, arguments);
1048
1006
  }
1049
1007
 
1050
- return getExistingDocument;
1008
+ return persistCozyData;
1051
1009
  }()
1052
1010
  /**
1053
1011
  *
@@ -1061,34 +1019,34 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1061
1019
  }, {
1062
1020
  key: "needsToWaitWarmup",
1063
1021
  value: function () {
1064
- var _needsToWaitWarmup = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee11(doctype) {
1065
- return _regenerator.default.wrap(function _callee11$(_context11) {
1022
+ var _needsToWaitWarmup = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee10(doctype) {
1023
+ return _regenerator.default.wrap(function _callee10$(_context10) {
1066
1024
  while (1) {
1067
- switch (_context11.prev = _context11.next) {
1025
+ switch (_context10.prev = _context10.next) {
1068
1026
  case 0:
1069
1027
  if (!(this.doctypesReplicationOptions && this.doctypesReplicationOptions[doctype] && this.doctypesReplicationOptions[doctype].warmupQueries)) {
1070
- _context11.next = 4;
1028
+ _context10.next = 4;
1071
1029
  break;
1072
1030
  }
1073
1031
 
1074
- _context11.next = 3;
1032
+ _context10.next = 3;
1075
1033
  return this.pouches.areQueriesWarmedUp(doctype, this.doctypesReplicationOptions[doctype].warmupQueries);
1076
1034
 
1077
1035
  case 3:
1078
- return _context11.abrupt("return", !_context11.sent);
1036
+ return _context10.abrupt("return", !_context10.sent);
1079
1037
 
1080
1038
  case 4:
1081
- return _context11.abrupt("return", false);
1039
+ return _context10.abrupt("return", false);
1082
1040
 
1083
1041
  case 5:
1084
1042
  case "end":
1085
- return _context11.stop();
1043
+ return _context10.stop();
1086
1044
  }
1087
1045
  }
1088
- }, _callee11, this);
1046
+ }, _callee10, this);
1089
1047
  }));
1090
1048
 
1091
- function needsToWaitWarmup(_x12) {
1049
+ function needsToWaitWarmup(_x10) {
1092
1050
  return _needsToWaitWarmup.apply(this, arguments);
1093
1051
  }
1094
1052
 
@@ -1099,241 +1057,63 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1099
1057
  value: function hasIndex(name) {
1100
1058
  return Boolean(this.indexes[name]);
1101
1059
  }
1102
- /**
1103
- * Create the PouchDB index if not existing
1104
- *
1105
- * @param {Array} fields - Fields to index
1106
- * @param {object} indexOption - Options for the index
1107
- * @param {object} [indexOption.partialFilter] - partialFilter
1108
- * @param {string} [indexOption.indexName] - indexName
1109
- * @param {string} [indexOption.doctype] - doctype
1110
- * @returns {Promise<import('./types').PouchDbIndex>}
1111
- */
1112
-
1113
- }, {
1114
- key: "createIndex",
1115
- value: function () {
1116
- var _createIndex = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee12(fields) {
1117
- var _ref5,
1118
- partialFilter,
1119
- indexName,
1120
- doctype,
1121
- absName,
1122
- db,
1123
- index,
1124
- _args12 = arguments;
1125
-
1126
- return _regenerator.default.wrap(function _callee12$(_context12) {
1127
- while (1) {
1128
- switch (_context12.prev = _context12.next) {
1129
- case 0:
1130
- _ref5 = _args12.length > 1 && _args12[1] !== undefined ? _args12[1] : {}, partialFilter = _ref5.partialFilter, indexName = _ref5.indexName, doctype = _ref5.doctype;
1131
- absName = "".concat(doctype, "/").concat(indexName);
1132
- db = this.pouches.getPouch(doctype);
1133
- _context12.next = 5;
1134
- return db.createIndex({
1135
- index: {
1136
- fields: fields,
1137
- ddoc: indexName,
1138
- indexName: indexName,
1139
- partial_filter_selector: partialFilter
1140
- }
1141
- });
1142
-
1143
- case 5:
1144
- index = _context12.sent;
1145
- this.indexes[absName] = index;
1146
- return _context12.abrupt("return", index);
1147
-
1148
- case 8:
1149
- case "end":
1150
- return _context12.stop();
1151
- }
1152
- }
1153
- }, _callee12, this);
1154
- }));
1155
-
1156
- function createIndex(_x13) {
1157
- return _createIndex.apply(this, arguments);
1158
- }
1159
-
1160
- return createIndex;
1161
- }()
1162
- /**
1163
- * Retrieve the PouchDB index if exist, undefined otherwise
1164
- *
1165
- * @param {string} doctype - The query's doctype
1166
- * @param {import('./types').MangoQueryOptions} options - The find options
1167
- * @param {string} indexName - The index name
1168
- * @returns {import('./types').PouchDbIndex | undefined}
1169
- */
1170
-
1171
- }, {
1172
- key: "findExistingIndex",
1173
- value: function findExistingIndex(doctype, options, indexName) {
1174
- var absName = "".concat(doctype, "/").concat(indexName);
1175
- return this.indexes[absName];
1176
- }
1177
- /**
1178
- * Handle index creation if it is missing.
1179
- *
1180
- * When an index is missing, we first check if there is one with a different
1181
- * name but the same definition. If there is none, we create the new index.
1182
- *
1183
- * /!\ Warning: this method is similar to DocumentCollection.handleMissingIndex()
1184
- * If you edit this method, please check if the change is also needed in DocumentCollection
1185
- *
1186
- * @param {string} doctype The mango selector
1187
- * @param {import('./types').MangoQueryOptions} options The find options
1188
- * @returns {Promise<import('./types').PouchDbIndex>} index
1189
- * @private
1190
- */
1191
-
1192
- }, {
1193
- key: "ensureIndex",
1194
- value: function () {
1195
- var _ensureIndex = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee13(doctype, options) {
1196
- var indexedFields, partialFilter, indexName, existingIndex;
1197
- return _regenerator.default.wrap(function _callee13$(_context13) {
1198
- while (1) {
1199
- switch (_context13.prev = _context13.next) {
1200
- case 0:
1201
- indexedFields = options.indexedFields, partialFilter = options.partialFilter;
1202
-
1203
- if (!indexedFields) {
1204
- indexedFields = (0, _mango.getIndexFields)(options);
1205
- } else if (partialFilter) {
1206
- // Some pouch adapters does not support partialIndex, e.g. with websql in react-native
1207
- // Therefore, we need to force the indexing the partialIndex fields to ensure they will be
1208
- // included in the actual index. Thanks to this, docs with missing fields will be excluded
1209
- // from the index.
1210
- // Note the $exists: false case should be handled in-memory.
1211
- indexedFields = Array.from(new Set([].concat((0, _toConsumableArray2.default)(indexedFields), (0, _toConsumableArray2.default)(Object.keys(partialFilter))))); // FIXME: should properly handle n-level attributes
1212
-
1213
- indexedFields = indexedFields.filter(function (field) {
1214
- return field !== '$and' && field !== '$or';
1215
- });
1216
- }
1217
-
1218
- indexName = (0, _mango.getIndexNameFromFields)(indexedFields, partialFilter);
1219
- existingIndex = this.findExistingIndex(doctype, options, indexName);
1220
-
1221
- if (existingIndex) {
1222
- _context13.next = 10;
1223
- break;
1224
- }
1225
-
1226
- _context13.next = 7;
1227
- return this.createIndex(indexedFields, {
1228
- partialFilter: partialFilter,
1229
- indexName: indexName,
1230
- doctype: doctype
1231
- });
1232
-
1233
- case 7:
1234
- return _context13.abrupt("return", _context13.sent);
1235
-
1236
- case 10:
1237
- return _context13.abrupt("return", existingIndex);
1238
-
1239
- case 11:
1240
- case "end":
1241
- return _context13.stop();
1242
- }
1243
- }
1244
- }, _callee13, this);
1245
- }));
1246
-
1247
- function ensureIndex(_x14, _x15) {
1248
- return _ensureIndex.apply(this, arguments);
1249
- }
1250
-
1251
- return ensureIndex;
1252
- }()
1253
1060
  }, {
1254
1061
  key: "executeQuery",
1255
1062
  value: function () {
1256
- var _executeQuery = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee14(_ref6) {
1257
- var doctype, selector, sort, fields, limit, id, ids, skip, indexedFields, partialFilter, markName, db, res, withRows, _markName, _markName2, _markName3, findSelector, findOpts, index, _markName4, jsonResult;
1258
-
1259
- return _regenerator.default.wrap(function _callee14$(_context14) {
1063
+ var _executeQuery = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee11(_ref5) {
1064
+ var doctype, selector, sort, fields, limit, id, ids, skip, indexedFields, partialFilter, engine, res, findSelector, findOpts;
1065
+ return _regenerator.default.wrap(function _callee11$(_context11) {
1260
1066
  while (1) {
1261
- switch (_context14.prev = _context14.next) {
1067
+ switch (_context11.prev = _context11.next) {
1262
1068
  case 0:
1263
- doctype = _ref6.doctype, selector = _ref6.selector, sort = _ref6.sort, fields = _ref6.fields, limit = _ref6.limit, id = _ref6.id, ids = _ref6.ids, skip = _ref6.skip, indexedFields = _ref6.indexedFields, partialFilter = _ref6.partialFilter;
1264
- markName = this.performanceApi.mark('executeQuery');
1265
- db = this.getPouch(doctype);
1069
+ doctype = _ref5.doctype, selector = _ref5.selector, sort = _ref5.sort, fields = _ref5.fields, limit = _ref5.limit, id = _ref5.id, ids = _ref5.ids, skip = _ref5.skip, indexedFields = _ref5.indexedFields, partialFilter = _ref5.partialFilter;
1070
+ engine = this.getQueryEngineFromDoctype(doctype);
1266
1071
 
1267
1072
  if (!id) {
1268
- _context14.next = 12;
1073
+ _context11.next = 8;
1269
1074
  break;
1270
1075
  }
1271
1076
 
1272
- _markName = this.performanceApi.mark('db.get from executeQuery');
1273
- _context14.next = 7;
1274
- return db.get(id);
1077
+ _context11.next = 5;
1078
+ return engine.getById(id);
1275
1079
 
1276
- case 7:
1277
- res = _context14.sent;
1278
- this.performanceApi.measure({
1279
- markName: _markName,
1280
- category: 'PouchDB'
1281
- });
1282
- withRows = false;
1283
- _context14.next = 47;
1080
+ case 5:
1081
+ res = _context11.sent;
1082
+ _context11.next = 25;
1284
1083
  break;
1285
1084
 
1286
- case 12:
1085
+ case 8:
1287
1086
  if (!ids) {
1288
- _context14.next = 23;
1087
+ _context11.next = 14;
1289
1088
  break;
1290
1089
  }
1291
1090
 
1292
- _markName2 = this.performanceApi.mark('allDocs from executeQuery with ids');
1293
- _context14.next = 16;
1294
- return allDocs(db, {
1295
- include_docs: true,
1296
- keys: ids
1297
- });
1091
+ _context11.next = 11;
1092
+ return engine.getByIds(ids);
1298
1093
 
1299
- case 16:
1300
- res = _context14.sent;
1301
- this.performanceApi.measure({
1302
- markName: _markName2,
1303
- category: 'PouchDB'
1304
- });
1305
- res = withoutDesignDocuments(res);
1306
- res.total_rows = null; // pouch indicates the total number of docs in res.total_rows, even though we use "keys". Setting it to null avoids cozy-client thinking there are more docs to fetch.
1307
-
1308
- withRows = true;
1309
- _context14.next = 47;
1094
+ case 11:
1095
+ res = _context11.sent;
1096
+ _context11.next = 25;
1310
1097
  break;
1311
1098
 
1312
- case 23:
1099
+ case 14:
1313
1100
  if (!(!selector && !partialFilter && !fields && !sort)) {
1314
- _context14.next = 33;
1101
+ _context11.next = 20;
1315
1102
  break;
1316
1103
  }
1317
1104
 
1318
- _markName3 = this.performanceApi.mark('allDocs from executeQuery');
1319
- _context14.next = 27;
1320
- return allDocs(db, {
1105
+ _context11.next = 17;
1106
+ return engine.allDocs({
1321
1107
  include_docs: true,
1322
1108
  limit: limit
1323
1109
  });
1324
1110
 
1325
- case 27:
1326
- res = _context14.sent;
1327
- this.performanceApi.measure({
1328
- markName: _markName3,
1329
- category: 'PouchDB'
1330
- });
1331
- res = withoutDesignDocuments(res);
1332
- withRows = true;
1333
- _context14.next = 47;
1111
+ case 17:
1112
+ res = _context11.sent;
1113
+ _context11.next = 25;
1334
1114
  break;
1335
1115
 
1336
- case 33:
1116
+ case 20:
1337
1117
  findSelector = _helpers.default.normalizeFindSelector({
1338
1118
  selector: selector,
1339
1119
  sort: sort,
@@ -1347,53 +1127,37 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1347
1127
  // _id is necessary for the store, and _rev is required for offline. See https://github.com/cozy/cozy-client/blob/95978d39546023920b0c01d689fed5dd41577a02/packages/cozy-client/src/CozyClient.js#L1153
1348
1128
  fields: fields ? [].concat((0, _toConsumableArray2.default)(fields), ['_id', '_rev']) : undefined,
1349
1129
  limit: limit,
1350
- skip: skip
1130
+ skip: skip,
1131
+ doctype: doctype
1351
1132
  };
1352
- _context14.next = 37;
1353
- return this.ensureIndex(doctype, _objectSpread(_objectSpread({}, findOpts), {}, {
1354
- indexedFields: indexedFields,
1355
- partialFilter: partialFilter
1356
- }));
1133
+ _context11.next = 24;
1134
+ return engine.find(findOpts);
1357
1135
 
1358
- case 37:
1359
- index = _context14.sent;
1360
- findOpts.use_index = index.id;
1361
- _markName4 = this.performanceApi.mark('find from executeQuery');
1362
- _context14.next = 42;
1363
- return find(db, findOpts);
1136
+ case 24:
1137
+ res = _context11.sent;
1364
1138
 
1365
- case 42:
1366
- res = _context14.sent;
1367
- this.performanceApi.measure({
1368
- markName: _markName4,
1369
- category: 'PouchDB'
1370
- });
1371
- res.offset = skip;
1372
- res.limit = limit;
1373
- withRows = true;
1139
+ case 25:
1140
+ if (res) {
1141
+ _context11.next = 27;
1142
+ break;
1143
+ }
1374
1144
 
1375
- case 47:
1376
- jsonResult = jsonapi.fromPouchResult({
1377
- res: res,
1378
- withRows: withRows,
1379
- doctype: doctype,
1380
- client: this.client
1145
+ return _context11.abrupt("return", {
1146
+ data: []
1381
1147
  });
1382
- this.performanceApi.measure({
1383
- markName: markName,
1384
- category: 'CozyPouchLink'
1385
- });
1386
- return _context14.abrupt("return", jsonResult);
1387
1148
 
1388
- case 50:
1149
+ case 27:
1150
+ return _context11.abrupt("return", res);
1151
+
1152
+ case 28:
1389
1153
  case "end":
1390
- return _context14.stop();
1154
+ return _context11.stop();
1391
1155
  }
1392
1156
  }
1393
- }, _callee14, this);
1157
+ }, _callee11, this);
1394
1158
  }));
1395
1159
 
1396
- function executeQuery(_x16) {
1160
+ function executeQuery(_x11) {
1397
1161
  return _executeQuery.apply(this, arguments);
1398
1162
  }
1399
1163
 
@@ -1402,59 +1166,59 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1402
1166
  }, {
1403
1167
  key: "executeMutation",
1404
1168
  value: function () {
1405
- var _executeMutation = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee15(mutation, options, result, forward) {
1169
+ var _executeMutation = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee12(mutation, options, result, forward) {
1406
1170
  var markName, pouchRes, jsonResult;
1407
- return _regenerator.default.wrap(function _callee15$(_context15) {
1171
+ return _regenerator.default.wrap(function _callee12$(_context12) {
1408
1172
  while (1) {
1409
- switch (_context15.prev = _context15.next) {
1173
+ switch (_context12.prev = _context12.next) {
1410
1174
  case 0:
1411
1175
  markName = this.performanceApi.mark('executeMutation');
1412
- _context15.t0 = mutation.mutationType;
1413
- _context15.next = _context15.t0 === _cozyClient.MutationTypes.CREATE_DOCUMENT ? 4 : _context15.t0 === _cozyClient.MutationTypes.UPDATE_DOCUMENT ? 8 : _context15.t0 === _cozyClient.MutationTypes.UPDATE_DOCUMENTS ? 12 : _context15.t0 === _cozyClient.MutationTypes.DELETE_DOCUMENT ? 16 : _context15.t0 === _cozyClient.MutationTypes.ADD_REFERENCES_TO ? 20 : 24;
1176
+ _context12.t0 = mutation.mutationType;
1177
+ _context12.next = _context12.t0 === _cozyClient.MutationTypes.CREATE_DOCUMENT ? 4 : _context12.t0 === _cozyClient.MutationTypes.UPDATE_DOCUMENT ? 8 : _context12.t0 === _cozyClient.MutationTypes.UPDATE_DOCUMENTS ? 12 : _context12.t0 === _cozyClient.MutationTypes.DELETE_DOCUMENT ? 16 : _context12.t0 === _cozyClient.MutationTypes.ADD_REFERENCES_TO ? 20 : 24;
1414
1178
  break;
1415
1179
 
1416
1180
  case 4:
1417
- _context15.next = 6;
1181
+ _context12.next = 6;
1418
1182
  return this.createDocument(mutation);
1419
1183
 
1420
1184
  case 6:
1421
- pouchRes = _context15.sent;
1422
- return _context15.abrupt("break", 25);
1185
+ pouchRes = _context12.sent;
1186
+ return _context12.abrupt("break", 25);
1423
1187
 
1424
1188
  case 8:
1425
- _context15.next = 10;
1189
+ _context12.next = 10;
1426
1190
  return this.updateDocument(mutation);
1427
1191
 
1428
1192
  case 10:
1429
- pouchRes = _context15.sent;
1430
- return _context15.abrupt("break", 25);
1193
+ pouchRes = _context12.sent;
1194
+ return _context12.abrupt("break", 25);
1431
1195
 
1432
1196
  case 12:
1433
- _context15.next = 14;
1197
+ _context12.next = 14;
1434
1198
  return this.updateDocuments(mutation);
1435
1199
 
1436
1200
  case 14:
1437
- pouchRes = _context15.sent;
1438
- return _context15.abrupt("break", 25);
1201
+ pouchRes = _context12.sent;
1202
+ return _context12.abrupt("break", 25);
1439
1203
 
1440
1204
  case 16:
1441
- _context15.next = 18;
1205
+ _context12.next = 18;
1442
1206
  return this.deleteDocument(mutation);
1443
1207
 
1444
1208
  case 18:
1445
- pouchRes = _context15.sent;
1446
- return _context15.abrupt("break", 25);
1209
+ pouchRes = _context12.sent;
1210
+ return _context12.abrupt("break", 25);
1447
1211
 
1448
1212
  case 20:
1449
- _context15.next = 22;
1213
+ _context12.next = 22;
1450
1214
  return this.addReferencesTo(mutation);
1451
1215
 
1452
1216
  case 22:
1453
- pouchRes = _context15.sent;
1454
- return _context15.abrupt("break", 25);
1217
+ pouchRes = _context12.sent;
1218
+ return _context12.abrupt("break", 25);
1455
1219
 
1456
1220
  case 24:
1457
- return _context15.abrupt("return", forward(mutation, options, result));
1221
+ return _context12.abrupt("return", forward(mutation, options, result));
1458
1222
 
1459
1223
  case 25:
1460
1224
  jsonResult = jsonapi.fromPouchResult({
@@ -1467,17 +1231,17 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1467
1231
  markName: markName,
1468
1232
  category: 'CozyPouchLink'
1469
1233
  });
1470
- return _context15.abrupt("return", jsonResult);
1234
+ return _context12.abrupt("return", jsonResult);
1471
1235
 
1472
1236
  case 28:
1473
1237
  case "end":
1474
- return _context15.stop();
1238
+ return _context12.stop();
1475
1239
  }
1476
1240
  }
1477
- }, _callee15, this);
1241
+ }, _callee12, this);
1478
1242
  }));
1479
1243
 
1480
- function executeMutation(_x17, _x18, _x19, _x20) {
1244
+ function executeMutation(_x12, _x13, _x14, _x15) {
1481
1245
  return _executeMutation.apply(this, arguments);
1482
1246
  }
1483
1247
 
@@ -1486,28 +1250,28 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1486
1250
  }, {
1487
1251
  key: "createDocument",
1488
1252
  value: function () {
1489
- var _createDocument = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee16(mutation) {
1253
+ var _createDocument = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee13(mutation) {
1490
1254
  var res;
1491
- return _regenerator.default.wrap(function _callee16$(_context16) {
1255
+ return _regenerator.default.wrap(function _callee13$(_context13) {
1492
1256
  while (1) {
1493
- switch (_context16.prev = _context16.next) {
1257
+ switch (_context13.prev = _context13.next) {
1494
1258
  case 0:
1495
- _context16.next = 2;
1259
+ _context13.next = 2;
1496
1260
  return this.dbMethod('post', mutation);
1497
1261
 
1498
1262
  case 2:
1499
- res = _context16.sent;
1500
- return _context16.abrupt("return", parseMutationResult(mutation.document, res));
1263
+ res = _context13.sent;
1264
+ return _context13.abrupt("return", parseMutationResult(mutation.document, res));
1501
1265
 
1502
1266
  case 4:
1503
1267
  case "end":
1504
- return _context16.stop();
1268
+ return _context13.stop();
1505
1269
  }
1506
1270
  }
1507
- }, _callee16, this);
1271
+ }, _callee13, this);
1508
1272
  }));
1509
1273
 
1510
- function createDocument(_x21) {
1274
+ function createDocument(_x16) {
1511
1275
  return _createDocument.apply(this, arguments);
1512
1276
  }
1513
1277
 
@@ -1516,28 +1280,28 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1516
1280
  }, {
1517
1281
  key: "updateDocument",
1518
1282
  value: function () {
1519
- var _updateDocument = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee17(mutation) {
1283
+ var _updateDocument = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee14(mutation) {
1520
1284
  var res;
1521
- return _regenerator.default.wrap(function _callee17$(_context17) {
1285
+ return _regenerator.default.wrap(function _callee14$(_context14) {
1522
1286
  while (1) {
1523
- switch (_context17.prev = _context17.next) {
1287
+ switch (_context14.prev = _context14.next) {
1524
1288
  case 0:
1525
- _context17.next = 2;
1289
+ _context14.next = 2;
1526
1290
  return this.dbMethod('put', mutation);
1527
1291
 
1528
1292
  case 2:
1529
- res = _context17.sent;
1530
- return _context17.abrupt("return", parseMutationResult(mutation.document, res));
1293
+ res = _context14.sent;
1294
+ return _context14.abrupt("return", parseMutationResult(mutation.document, res));
1531
1295
 
1532
1296
  case 4:
1533
1297
  case "end":
1534
- return _context17.stop();
1298
+ return _context14.stop();
1535
1299
  }
1536
1300
  }
1537
- }, _callee17, this);
1301
+ }, _callee14, this);
1538
1302
  }));
1539
1303
 
1540
- function updateDocument(_x22) {
1304
+ function updateDocument(_x17) {
1541
1305
  return _updateDocument.apply(this, arguments);
1542
1306
  }
1543
1307
 
@@ -1546,18 +1310,18 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1546
1310
  }, {
1547
1311
  key: "updateDocuments",
1548
1312
  value: function () {
1549
- var _updateDocuments = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee18(mutation) {
1313
+ var _updateDocuments = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee15(mutation) {
1550
1314
  var docs, bulkResponse, updatedDocs;
1551
- return _regenerator.default.wrap(function _callee18$(_context18) {
1315
+ return _regenerator.default.wrap(function _callee15$(_context15) {
1552
1316
  while (1) {
1553
- switch (_context18.prev = _context18.next) {
1317
+ switch (_context15.prev = _context15.next) {
1554
1318
  case 0:
1555
1319
  docs = mutation.documents;
1556
- _context18.next = 3;
1320
+ _context15.next = 3;
1557
1321
  return this.dbMethod('bulkDocs', mutation);
1558
1322
 
1559
1323
  case 3:
1560
- bulkResponse = _context18.sent;
1324
+ bulkResponse = _context15.sent;
1561
1325
  updatedDocs = (0, _zipWith.default)(bulkResponse, docs, function (bulkResult, originalDoc) {
1562
1326
  return _objectSpread(_objectSpread({}, originalDoc), {}, {
1563
1327
  _id: bulkResult.id,
@@ -1568,24 +1332,24 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1568
1332
  if (!bulkResponse.find(function (x) {
1569
1333
  return !x.ok;
1570
1334
  })) {
1571
- _context18.next = 7;
1335
+ _context15.next = 7;
1572
1336
  break;
1573
1337
  }
1574
1338
 
1575
1339
  throw new _cozyClient.BulkEditError(bulkResponse, updatedDocs);
1576
1340
 
1577
1341
  case 7:
1578
- return _context18.abrupt("return", updatedDocs);
1342
+ return _context15.abrupt("return", updatedDocs);
1579
1343
 
1580
1344
  case 8:
1581
1345
  case "end":
1582
- return _context18.stop();
1346
+ return _context15.stop();
1583
1347
  }
1584
1348
  }
1585
- }, _callee18, this);
1349
+ }, _callee15, this);
1586
1350
  }));
1587
1351
 
1588
- function updateDocuments(_x23) {
1352
+ function updateDocuments(_x18) {
1589
1353
  return _updateDocuments.apply(this, arguments);
1590
1354
  }
1591
1355
 
@@ -1594,33 +1358,33 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1594
1358
  }, {
1595
1359
  key: "deleteDocument",
1596
1360
  value: function () {
1597
- var _deleteDocument = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee19(mutation) {
1361
+ var _deleteDocument = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee16(mutation) {
1598
1362
  var res, document;
1599
- return _regenerator.default.wrap(function _callee19$(_context19) {
1363
+ return _regenerator.default.wrap(function _callee16$(_context16) {
1600
1364
  while (1) {
1601
- switch (_context19.prev = _context19.next) {
1365
+ switch (_context16.prev = _context16.next) {
1602
1366
  case 0:
1603
- _context19.next = 2;
1367
+ _context16.next = 2;
1604
1368
  return this.dbMethod('remove', mutation);
1605
1369
 
1606
1370
  case 2:
1607
- res = _context19.sent;
1371
+ res = _context16.sent;
1608
1372
  document = _objectSpread(_objectSpread({}, mutation.document), {}, {
1609
1373
  _id: res.id,
1610
1374
  _rev: res.rev,
1611
1375
  _deleted: true
1612
1376
  });
1613
- return _context19.abrupt("return", parseMutationResult(document, res));
1377
+ return _context16.abrupt("return", parseMutationResult(document, res));
1614
1378
 
1615
1379
  case 5:
1616
1380
  case "end":
1617
- return _context19.stop();
1381
+ return _context16.stop();
1618
1382
  }
1619
1383
  }
1620
- }, _callee19, this);
1384
+ }, _callee16, this);
1621
1385
  }));
1622
1386
 
1623
- function deleteDocument(_x24) {
1387
+ function deleteDocument(_x19) {
1624
1388
  return _deleteDocument.apply(this, arguments);
1625
1389
  }
1626
1390
 
@@ -1629,22 +1393,22 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1629
1393
  }, {
1630
1394
  key: "addReferencesTo",
1631
1395
  value: function () {
1632
- var _addReferencesTo = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee20(mutation) {
1633
- return _regenerator.default.wrap(function _callee20$(_context20) {
1396
+ var _addReferencesTo = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee17(mutation) {
1397
+ return _regenerator.default.wrap(function _callee17$(_context17) {
1634
1398
  while (1) {
1635
- switch (_context20.prev = _context20.next) {
1399
+ switch (_context17.prev = _context17.next) {
1636
1400
  case 0:
1637
1401
  throw new Error('addReferencesTo is not implemented in CozyPouchLink');
1638
1402
 
1639
1403
  case 1:
1640
1404
  case "end":
1641
- return _context20.stop();
1405
+ return _context17.stop();
1642
1406
  }
1643
1407
  }
1644
- }, _callee20);
1408
+ }, _callee17);
1645
1409
  }));
1646
1410
 
1647
- function addReferencesTo(_x25) {
1411
+ function addReferencesTo(_x20) {
1648
1412
  return _addReferencesTo.apply(this, arguments);
1649
1413
  }
1650
1414
 
@@ -1653,45 +1417,45 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1653
1417
  }, {
1654
1418
  key: "dbMethod",
1655
1419
  value: function () {
1656
- var _dbMethod = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee21(method, mutation) {
1420
+ var _dbMethod = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee18(method, mutation) {
1657
1421
  var markName, doctype, doc, docs, db, res;
1658
- return _regenerator.default.wrap(function _callee21$(_context21) {
1422
+ return _regenerator.default.wrap(function _callee18$(_context18) {
1659
1423
  while (1) {
1660
- switch (_context21.prev = _context21.next) {
1424
+ switch (_context18.prev = _context18.next) {
1661
1425
  case 0:
1662
1426
  markName = this.performanceApi.mark("dbMethod ".concat(method));
1663
1427
  doctype = (0, _cozyClient.getDoctypeFromOperation)(mutation);
1664
1428
  doc = mutation.document, docs = mutation.documents;
1665
1429
  db = this.getPouch(doctype);
1666
- _context21.prev = 4;
1430
+ _context18.prev = 4;
1667
1431
 
1668
1432
  if (!docs) {
1669
- _context21.next = 11;
1433
+ _context18.next = 11;
1670
1434
  break;
1671
1435
  }
1672
1436
 
1673
- _context21.next = 8;
1437
+ _context18.next = 8;
1674
1438
  return db[method](docs.map(function (doc) {
1675
- return sanitized(doc);
1439
+ return (0, jsonapi.sanitized)(doc);
1676
1440
  }));
1677
1441
 
1678
1442
  case 8:
1679
- res = _context21.sent;
1680
- _context21.next = 18;
1443
+ res = _context18.sent;
1444
+ _context18.next = 18;
1681
1445
  break;
1682
1446
 
1683
1447
  case 11:
1684
1448
  if (!doc) {
1685
- _context21.next = 17;
1449
+ _context18.next = 17;
1686
1450
  break;
1687
1451
  }
1688
1452
 
1689
- _context21.next = 14;
1690
- return db[method](sanitized(doc));
1453
+ _context18.next = 14;
1454
+ return db[method]((0, jsonapi.sanitized)(doc));
1691
1455
 
1692
1456
  case 14:
1693
- res = _context21.sent;
1694
- _context21.next = 18;
1457
+ res = _context18.sent;
1458
+ _context18.next = 18;
1695
1459
  break;
1696
1460
 
1697
1461
  case 17:
@@ -1702,28 +1466,28 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1702
1466
  markName: markName,
1703
1467
  category: 'PouchDB'
1704
1468
  });
1705
- return _context21.abrupt("return", res);
1469
+ return _context18.abrupt("return", res);
1706
1470
 
1707
1471
  case 22:
1708
- _context21.prev = 22;
1709
- _context21.t0 = _context21["catch"](4);
1472
+ _context18.prev = 22;
1473
+ _context18.t0 = _context18["catch"](4);
1710
1474
  this.performanceApi.measure({
1711
1475
  markName: markName,
1712
1476
  measureName: "".concat(markName, " error"),
1713
1477
  category: 'PouchDB',
1714
1478
  color: 'error'
1715
1479
  });
1716
- throw new Error("Coud not apply mutation: ".concat(_context21.t0.message));
1480
+ throw new Error("Coud not apply mutation: ".concat(_context18.t0.message));
1717
1481
 
1718
1482
  case 26:
1719
1483
  case "end":
1720
- return _context21.stop();
1484
+ return _context18.stop();
1721
1485
  }
1722
1486
  }
1723
- }, _callee21, this, [[4, 22]]);
1487
+ }, _callee18, this, [[4, 22]]);
1724
1488
  }));
1725
1489
 
1726
- function dbMethod(_x26, _x27) {
1490
+ function dbMethod(_x21, _x22) {
1727
1491
  return _dbMethod.apply(this, arguments);
1728
1492
  }
1729
1493
 
@@ -1732,29 +1496,29 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1732
1496
  }, {
1733
1497
  key: "syncImmediately",
1734
1498
  value: function () {
1735
- var _syncImmediately = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee22() {
1736
- return _regenerator.default.wrap(function _callee22$(_context22) {
1499
+ var _syncImmediately = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee19() {
1500
+ return _regenerator.default.wrap(function _callee19$(_context19) {
1737
1501
  while (1) {
1738
- switch (_context22.prev = _context22.next) {
1502
+ switch (_context19.prev = _context19.next) {
1739
1503
  case 0:
1740
1504
  if (this.pouches) {
1741
- _context22.next = 3;
1505
+ _context19.next = 3;
1742
1506
  break;
1743
1507
  }
1744
1508
 
1745
1509
  _logger.default.warn('Cannot sync immediately, no PouchManager');
1746
1510
 
1747
- return _context22.abrupt("return");
1511
+ return _context19.abrupt("return");
1748
1512
 
1749
1513
  case 3:
1750
1514
  this.pouches.syncImmediately();
1751
1515
 
1752
1516
  case 4:
1753
1517
  case "end":
1754
- return _context22.stop();
1518
+ return _context19.stop();
1755
1519
  }
1756
1520
  }
1757
- }, _callee22, this);
1521
+ }, _callee19, this);
1758
1522
  }));
1759
1523
 
1760
1524
  function syncImmediately() {