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
package/dist/jsonapi.js CHANGED
@@ -2,19 +2,33 @@
2
2
 
3
3
  var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
4
 
5
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
+
5
7
  Object.defineProperty(exports, "__esModule", {
6
8
  value: true
7
9
  });
8
- exports.fromPouchResult = exports.normalizeDoc = exports.normalizeDocs = void 0;
10
+ exports.sanitizeJsonApi = exports.sanitized = exports.fromPouchResult = exports.normalizeDoc = exports.normalizeDocs = void 0;
11
+
12
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
13
 
10
14
  var _cozyClient = _interopRequireWildcard(require("cozy-client"));
11
15
 
16
+ var _omit = _interopRequireDefault(require("lodash/omit"));
17
+
18
+ var _startsWith = _interopRequireDefault(require("lodash/startsWith"));
19
+
20
+ var _helpers = require("./db/helpers");
21
+
22
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
23
+
24
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
25
+
12
26
  /**
13
27
  * Normalize several PouchDB document
14
28
  *
15
29
  * @param {CozyClient} client - The CozyClient instance
16
30
  * @param {string} doctype - The document's doctype
17
- * @param {Array<import('./CozyPouchLink').CozyClientDocument>} docs - The documents to normalize
31
+ * @param {Array<import('./CozyPouchLink').CozyPouchDocument>} docs - The documents to normalize
18
32
  */
19
33
  var normalizeDocs = function normalizeDocs(client, doctype, docs) {
20
34
  for (var i = docs.length; i >= 0; i--) {
@@ -25,6 +39,11 @@ var normalizeDocs = function normalizeDocs(client, doctype, docs) {
25
39
  continue;
26
40
  }
27
41
 
42
+ if ((0, _startsWith.default)(doc.id, '_design/')) {
43
+ docs.splice(i, 1);
44
+ continue;
45
+ }
46
+
28
47
  normalizeDoc(client, doctype, doc);
29
48
  }
30
49
  };
@@ -35,7 +54,7 @@ var normalizeDocs = function normalizeDocs(client, doctype, docs) {
35
54
  *
36
55
  * @param {CozyClient} client - The CozyClient instance
37
56
  * @param {string} doctype - The document's doctype
38
- * @param {import('./CozyPouchLink').CozyClientDocument} doc - The document to normalize
57
+ * @param {import('./CozyPouchLink').CozyPouchDocument} doc - The document to normalize
39
58
  */
40
59
 
41
60
 
@@ -48,12 +67,15 @@ var normalizeDoc = function normalizeDoc(client, doctype, doc) {
48
67
  doc._rev = doc._rev || doc.rev;
49
68
  doc._type = doctype;
50
69
 
51
- if (doc.relationships) {
52
- doc.relationships.referenced_by = doc.referenced_by;
53
- } else {
54
- doc.relationships = {
55
- referenced_by: doc.referenced_by
56
- };
70
+ if (doc.referenced_by) {
71
+ // TODO: should we remove referenced_by at the doc root?
72
+ // For now, we keep it for safety
73
+ doc.relationships = _objectSpread(_objectSpread({}, doc.relationships), {}, {
74
+ referenced_by: {
75
+ links: undefined,
76
+ data: doc.referenced_by
77
+ }
78
+ });
57
79
  }
58
80
 
59
81
  if (doc.rev) {
@@ -93,17 +115,16 @@ var fromPouchResult = function fromPouchResult(_ref) {
93
115
  doctype = _ref.doctype,
94
116
  client = _ref.client;
95
117
 
96
- // Sometimes, queries are transformed by Collections and they call a dedicated
97
- // cozy-stack route. When this is the case, we want to be able to replicate the same
98
- // query from cozy-pouch-link. It is not possible as-is because the received data
99
- // is not the same as the one stored in the Couch database
100
- // To handle this, we store the received data in the Pouch with a dedicated id and
101
- // we store the query result in a `cozyPouchData` attribute
102
- // So when `cozyPouchData` attribute exists, we know that we want to return its content
103
- // as the result of the query
104
- if (res.cozyPouchData) {
118
+ if (!res) {
119
+ return null;
120
+ } // Handle special case for docs with `cozyPouchData`
121
+
122
+
123
+ var cozyPouchData = (0, _helpers.getCozyPouchData)(res);
124
+
125
+ if (cozyPouchData) {
105
126
  return {
106
- data: res.cozyPouchData
127
+ data: cozyPouchData
107
128
  };
108
129
  }
109
130
 
@@ -112,6 +133,7 @@ var fromPouchResult = function fromPouchResult(_ref) {
112
133
  return row.doc;
113
134
  }) : res.docs;
114
135
  var offset = res.offset || 0;
136
+ var next = offset + docs.length < res.total_rows || docs.length >= res.limit;
115
137
  normalizeDocs(client, doctype, docs);
116
138
  var result = {
117
139
  data: docs,
@@ -119,7 +141,7 @@ var fromPouchResult = function fromPouchResult(_ref) {
119
141
  count: docs.length
120
142
  },
121
143
  skip: offset,
122
- next: offset + docs.length < res.total_rows || docs.length >= res.limit
144
+ next: next
123
145
  };
124
146
  return result;
125
147
  } else {
@@ -130,4 +152,18 @@ var fromPouchResult = function fromPouchResult(_ref) {
130
152
  }
131
153
  };
132
154
 
133
- exports.fromPouchResult = fromPouchResult;
155
+ exports.fromPouchResult = fromPouchResult;
156
+
157
+ var sanitized = function sanitized(doc) {
158
+ return (0, _omit.default)(doc, '_type');
159
+ };
160
+
161
+ exports.sanitized = sanitized;
162
+
163
+ var sanitizeJsonApi = function sanitizeJsonApi(doc) {
164
+ var docWithoutType = sanitized(doc);
165
+ var sanitizedDoc = (0, _omit.default)(docWithoutType, ['attributes', 'meta']);
166
+ return sanitizedDoc;
167
+ };
168
+
169
+ exports.sanitizeJsonApi = sanitizeJsonApi;
@@ -34,24 +34,99 @@ const client = new CozyClient({ token, uri })
34
34
  client.capabilities = { flat_subdomains: true }
35
35
 
36
36
  describe('doc normalization', () => {
37
+ it('should correctly normalize simple doc', () => {
38
+ const doc = {
39
+ _id: '1234',
40
+ _rev: '1-1234',
41
+ name: 'Simple doc'
42
+ }
43
+ normalizeDoc(client, 'io.cozy.todos', doc)
44
+ expect(doc).toEqual({
45
+ _id: '1234',
46
+ id: '1234',
47
+ _rev: '1-1234',
48
+ _type: 'io.cozy.todos',
49
+ name: 'Simple doc'
50
+ })
51
+ })
52
+
53
+ it('should handle wrong rev format', () => {
54
+ const doc = {
55
+ _id: '1234',
56
+ rev: '1-1234',
57
+ name: 'Simple doc'
58
+ }
59
+ normalizeDoc(client, 'io.cozy.todos', doc)
60
+ expect(doc).toEqual({
61
+ _id: '1234',
62
+ id: '1234',
63
+ _rev: '1-1234',
64
+ _type: 'io.cozy.todos',
65
+ name: 'Simple doc'
66
+ })
67
+ })
68
+
69
+ it('should normalize relationships', () => {
70
+ const doc = {
71
+ _id: '1234',
72
+ rev: '1-1234',
73
+ name: 'Simple doc',
74
+ relationships: {
75
+ contact: {
76
+ data: {
77
+ _id: '4567',
78
+ _type: 'io.cozy.contacts'
79
+ }
80
+ }
81
+ },
82
+ referenced_by: [
83
+ {
84
+ type: 'io.cozy.albums',
85
+ id: 'thegreatalbum'
86
+ }
87
+ ]
88
+ }
89
+ normalizeDoc(client, 'io.cozy.todos', doc)
90
+ expect(doc).toMatchObject({
91
+ _id: '1234',
92
+ id: '1234',
93
+ _rev: '1-1234',
94
+ _type: 'io.cozy.todos',
95
+ name: 'Simple doc',
96
+ relationships: {
97
+ contact: {
98
+ data: {
99
+ _id: '4567',
100
+ _type: 'io.cozy.contacts'
101
+ }
102
+ },
103
+ referenced_by: {
104
+ data: [
105
+ {
106
+ type: 'io.cozy.albums',
107
+ id: 'thegreatalbum'
108
+ }
109
+ ]
110
+ }
111
+ }
112
+ })
113
+ })
114
+
37
115
  it('should normalize apps links', () => {
38
116
  const doc = {
39
- _id: 1234,
117
+ _id: '1234',
40
118
  _rev: '3-deadbeef',
41
119
  slug: 'contact',
42
120
  version: '1.2.0'
43
121
  }
44
122
  normalizeDoc(client, 'io.cozy.apps', doc)
45
123
  expect(doc).toEqual({
46
- _id: 1234,
47
- id: 1234,
124
+ _id: '1234',
125
+ id: '1234',
48
126
  _rev: '3-deadbeef',
49
127
  _type: 'io.cozy.apps',
50
128
  slug: 'contact',
51
129
  version: '1.2.0',
52
- relationships: {
53
- referenced_by: undefined
54
- },
55
130
  links: {
56
131
  icon: '/apps/contact/icon/1.2.0',
57
132
  related: 'https://claude-contact.mycozy.cloud/#/',
@@ -188,9 +263,6 @@ describe('jsonapi', () => {
188
263
  links: {
189
264
  self: '/settings/flags'
190
265
  },
191
- relationships: {
192
- referenced_by: undefined
193
- },
194
266
  'some.boolean.flag': true,
195
267
  'some.number.flag': 30,
196
268
  'some.object.flag': {
@@ -214,9 +286,6 @@ describe('jsonapi', () => {
214
286
  expect(normalized).toEqual({
215
287
  data: [
216
288
  {
217
- relationships: {
218
- referenced_by: undefined
219
- },
220
289
  id: '018bdcec-00c8-7155-b352-c8a8f472f882',
221
290
  type: 'file',
222
291
  _type: 'io.cozy.files',
@@ -258,12 +327,14 @@ describe('jsonapi', () => {
258
327
  },
259
328
  {
260
329
  relationships: {
261
- referenced_by: [
262
- {
263
- id: '536bde9aef87dde16630d3c99d26453f',
264
- type: 'io.cozy.photos.albums'
265
- }
266
- ]
330
+ referenced_by: {
331
+ data: [
332
+ {
333
+ id: '536bde9aef87dde16630d3c99d26453f',
334
+ type: 'io.cozy.photos.albums'
335
+ }
336
+ ]
337
+ }
267
338
  },
268
339
  id: '018c7cf1-1d00-73ac-9a7f-ee3190638183',
269
340
  type: 'file',
@@ -315,18 +386,26 @@ describe('jsonapi', () => {
315
386
  _rev: '2-02e800df012ea1cc740e5ad1554cefe6'
316
387
  },
317
388
  {
318
- relationships: {
319
- referenced_by: [
320
- {
321
- id: '536bde9aef87dde16630d3c99d26453f',
322
- type: 'io.cozy.photos.albums'
323
- }
324
- ]
325
- },
326
389
  id: '018ca6a8-8292-7acb-bcaf-a95ccfd83662',
327
390
  _type: 'io.cozy.files',
328
391
  type: 'file',
329
392
  name: 'IMG_0047.jpg',
393
+ relationships: {
394
+ referenced_by: {
395
+ data: [
396
+ {
397
+ id: '536bde9aef87dde16630d3c99d26453f',
398
+ type: 'io.cozy.photos.albums'
399
+ }
400
+ ]
401
+ }
402
+ },
403
+ referenced_by: [
404
+ {
405
+ id: '536bde9aef87dde16630d3c99d26453f',
406
+ type: 'io.cozy.photos.albums'
407
+ }
408
+ ],
330
409
  dir_id: 'io.cozy.files.root-dir',
331
410
  created_at: '2023-11-19T13:31:47+01:00',
332
411
  updated_at: '2023-12-26T15:05:10.256Z',
@@ -346,12 +425,6 @@ describe('jsonapi', () => {
346
425
  orientation: 6,
347
426
  width: 4032
348
427
  },
349
- referenced_by: [
350
- {
351
- id: '536bde9aef87dde16630d3c99d26453f',
352
- type: 'io.cozy.photos.albums'
353
- }
354
- ],
355
428
  cozyMetadata: {
356
429
  doctypeVersion: '1',
357
430
  metadataVersion: 1,
package/dist/mango.js CHANGED
@@ -5,7 +5,11 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.getIndexFields = exports.getIndexNameFromFields = exports.makeKeyFromPartialFilter = void 0;
8
+ exports.getIndexName = exports.getIndexFields = exports.findExistingIndex = exports.createIndex = exports.getIndexFieldsFromFind = exports.getIndexNameFromFields = exports.makeKeyFromPartialFilter = void 0;
9
+
10
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
+
12
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
9
13
 
10
14
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
11
15
 
@@ -13,6 +17,7 @@ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/sli
13
17
 
14
18
  var _head = _interopRequireDefault(require("lodash/head"));
15
19
 
20
+ var indexes = {};
16
21
  /**
17
22
  * Process a partial filter to generate a string key
18
23
  *
@@ -22,6 +27,7 @@ var _head = _interopRequireDefault(require("lodash/head"));
22
27
  * @param {object} condition - An object representing the partial filter or a sub-condition of the partial filter
23
28
  * @returns {string} - The string key of the processed partial filter
24
29
  */
30
+
25
31
  var makeKeyFromPartialFilter = function makeKeyFromPartialFilter(condition) {
26
32
  if (typeof condition !== 'object' || condition === null) {
27
33
  return String(condition);
@@ -92,7 +98,7 @@ var defaultSelector = {
92
98
  }
93
99
  };
94
100
 
95
- var getIndexFields = function getIndexFields(
101
+ var getIndexFieldsFromFind = function getIndexFieldsFromFind(
96
102
  /** @type {import('./types').MangoQueryOptions} */
97
103
  _ref3) {
98
104
  var _ref3$selector = _ref3.selector,
@@ -104,5 +110,142 @@ _ref3) {
104
110
  return (0, _head.default)(Object.keys(sortOption));
105
111
  })), (0, _toConsumableArray2.default)(selector ? Object.keys(selector) : []), (0, _toConsumableArray2.default)(partialFilter ? Object.keys(partialFilter) : []))));
106
112
  };
113
+ /**
114
+ * Create the PouchDB index if not existing
115
+ *
116
+ * @param {object} db - The pouch db
117
+ * @param {Array} fields - Fields to index
118
+ * @param {object} indexOption - Options for the index
119
+ * @param {object} [indexOption.partialFilter] - partialFilter
120
+ * @param {string} [indexOption.indexName] - indexName
121
+ * @param {string} [indexOption.doctype] - doctype
122
+ * @returns {Promise<import('./types').PouchDbIndex>}
123
+ */
124
+
125
+
126
+ exports.getIndexFieldsFromFind = getIndexFieldsFromFind;
127
+
128
+ var createIndex = /*#__PURE__*/function () {
129
+ var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(db, fields) {
130
+ var _ref5,
131
+ partialFilter,
132
+ indexName,
133
+ doctype,
134
+ absName,
135
+ index,
136
+ _args = arguments;
137
+
138
+ return _regenerator.default.wrap(function _callee$(_context) {
139
+ while (1) {
140
+ switch (_context.prev = _context.next) {
141
+ case 0:
142
+ _ref5 = _args.length > 2 && _args[2] !== undefined ? _args[2] : {}, partialFilter = _ref5.partialFilter, indexName = _ref5.indexName, doctype = _ref5.doctype;
143
+ absName = "".concat(doctype, "/").concat(indexName);
144
+ _context.next = 4;
145
+ return db.createIndex({
146
+ index: {
147
+ fields: fields,
148
+ ddoc: indexName,
149
+ indexName: indexName,
150
+ partial_filter_selector: partialFilter
151
+ }
152
+ });
153
+
154
+ case 4:
155
+ index = _context.sent;
156
+ indexes[absName] = index;
157
+ return _context.abrupt("return", index);
158
+
159
+ case 7:
160
+ case "end":
161
+ return _context.stop();
162
+ }
163
+ }
164
+ }, _callee);
165
+ }));
166
+
167
+ return function createIndex(_x, _x2) {
168
+ return _ref4.apply(this, arguments);
169
+ };
170
+ }();
171
+ /**
172
+ * Retrieve the PouchDB index if exist, undefined otherwise
173
+ *
174
+ * @param {string} doctype - The query's doctype
175
+ * @param {import('./types').MangoQueryOptions} options - The find options
176
+ * @param {string} indexName - The index name
177
+ * @returns {import('./types').PouchDbIndex | undefined}
178
+ */
179
+
180
+
181
+ exports.createIndex = createIndex;
182
+
183
+ var findExistingIndex = function findExistingIndex(doctype, options, indexName) {
184
+ var absName = "".concat(doctype, "/").concat(indexName);
185
+ return indexes[absName];
186
+ };
187
+
188
+ exports.findExistingIndex = findExistingIndex;
189
+
190
+ var getIndexFields = function getIndexFields(_ref6) {
191
+ var selector = _ref6.selector,
192
+ sort = _ref6.sort,
193
+ partialFilter = _ref6.partialFilter,
194
+ indexedFields = _ref6.indexedFields;
195
+ var fieldsToIndex = indexedFields;
196
+
197
+ if (!indexedFields) {
198
+ fieldsToIndex = getIndexFieldsFromFind({
199
+ selector: selector,
200
+ sort: sort,
201
+ partialFilter: partialFilter
202
+ });
203
+ } else if (partialFilter) {
204
+ // Some pouch adapters does not support partialIndex, e.g. with websql in react-native
205
+ // Therefore, we need to force the indexing the partialIndex fields to ensure they will be
206
+ // included in the actual index. Thanks to this, docs with missing fields will be excluded
207
+ // from the index.
208
+ // Note the $exists: false case should be handled in-memory.
209
+ fieldsToIndex = Array.from(new Set([].concat((0, _toConsumableArray2.default)(indexedFields), (0, _toConsumableArray2.default)(Object.keys(partialFilter))))); // FIXME: should properly handle n-level attributes
210
+
211
+ fieldsToIndex = indexedFields.filter(function (field) {
212
+ return field !== '$and' && field !== '$or';
213
+ });
214
+ }
215
+
216
+ return fieldsToIndex;
217
+ };
218
+
219
+ exports.getIndexFields = getIndexFields;
220
+
221
+ var getIndexName = function getIndexName(_ref7) {
222
+ var selector = _ref7.selector,
223
+ sort = _ref7.sort,
224
+ indexedFields = _ref7.indexedFields,
225
+ partialFilter = _ref7.partialFilter;
226
+ var fieldsToIndex = indexedFields;
227
+
228
+ if (!indexedFields) {
229
+ fieldsToIndex = getIndexFieldsFromFind({
230
+ selector: selector,
231
+ sort: sort,
232
+ partialFilter: partialFilter
233
+ });
234
+ } else if (partialFilter) {
235
+ // Some pouch adapters does not support partialIndex, e.g. with websql in react-native
236
+ // Therefore, we need to force the indexing the partialIndex fields to ensure they will be
237
+ // included in the actual index. Thanks to this, docs with missing fields will be excluded
238
+ // from the index.
239
+ // Note the $exists: false case should be handled in-memory.
240
+ fieldsToIndex = Array.from(new Set([].concat((0, _toConsumableArray2.default)(indexedFields), (0, _toConsumableArray2.default)(Object.keys(partialFilter))))); // FIXME: should properly handle n-level attributes
241
+
242
+ fieldsToIndex = indexedFields.filter(function (field) {
243
+ return field !== '$and' && field !== '$or';
244
+ });
245
+ }
246
+
247
+ var indexName = getIndexNameFromFields(fieldsToIndex, partialFilter);
248
+ return indexName;
249
+ };
107
250
 
108
- exports.getIndexFields = getIndexFields;
251
+ exports.getIndexName = getIndexName;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.destroyOldDatabases = void 0;
7
+
8
+ var _utils = require("../utils");
9
+
10
+ var destroyOldDatabases = function destroyOldDatabases(PouchDB, doctypes, prefix) {
11
+ if (doctypes) {
12
+ doctypes.forEach(function (doctype) {
13
+ // Check an old version of the db exist for this doctype
14
+ var db = new PouchDB((0, _utils.getOldDatabaseName)(prefix, doctype), {
15
+ skip_setup: true // Skip the db creation
16
+
17
+ });
18
+
19
+ if (db && db.info) {
20
+ db.info().then(function () {
21
+ // The database exists: destroy it
22
+ db.destroy();
23
+ }).catch(function (err) {
24
+ // No database found, nothing to do
25
+ return;
26
+ });
27
+ }
28
+ });
29
+ }
30
+ };
31
+
32
+ exports.destroyOldDatabases = destroyOldDatabases;
@@ -43,7 +43,7 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
43
43
  */
44
44
  var replicateOnce = /*#__PURE__*/function () {
45
45
  var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(pouchManager) {
46
- var doctypes, promises, res, zippedDoctypes, successZippedDoctypes, failedZippedDoctypes, blockingErrors, unblockingErrors, _iterator, _step, unblockingError, doctype, errors, reasons, doctypeUpdated, doctypeFailed;
46
+ var doctypes, promises, res, zippedDoctypes, successZippedDoctypes, failedZippedDoctypes, blockingErrors, unblockingErrors, _iterator, _step, unblockingError, _doctype, errors, reasons, doctypeUpdated, doctypeFailed;
47
47
 
48
48
  return _regenerator.default.wrap(function _callee2$(_context2) {
49
49
  while (1) {
@@ -67,12 +67,14 @@ var replicateOnce = /*#__PURE__*/function () {
67
67
 
68
68
 
69
69
  pouchManager.replications = (0, _map.default)(pouchManager.pouches, /*#__PURE__*/function () {
70
- var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(pouch, doctype) {
71
- var getReplicationURL, initialReplication, replicationFilter, seq, lastSeq, replicationOptions, res;
70
+ var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(pouch, dbName) {
71
+ var doctype, getReplicationURL, initialReplication, replicationFilter, seq, lastSeq, replicationOptions, res;
72
72
  return _regenerator.default.wrap(function _callee$(_context) {
73
73
  while (1) {
74
74
  switch (_context.prev = _context.next) {
75
75
  case 0:
76
+ doctype = (0, _utils.getDoctypeFromDatabaseName)(dbName);
77
+
76
78
  _logger.default.info('PouchManager: Starting replication for ' + doctype);
77
79
 
78
80
  getReplicationURL = function getReplicationURL() {
@@ -88,30 +90,30 @@ var replicateOnce = /*#__PURE__*/function () {
88
90
  seq = '';
89
91
 
90
92
  if (!initialReplication) {
91
- _context.next = 13;
93
+ _context.next = 14;
92
94
  break;
93
95
  }
94
96
 
95
- _context.next = 8;
97
+ _context.next = 9;
96
98
  return (0, _remote.fetchRemoteLastSequence)(getReplicationURL());
97
99
 
98
- case 8:
100
+ case 9:
99
101
  lastSeq = _context.sent;
100
- _context.next = 11;
102
+ _context.next = 12;
101
103
  return pouchManager.storage.persistDoctypeLastSequence(doctype, lastSeq);
102
104
 
103
- case 11:
104
- _context.next = 16;
105
+ case 12:
106
+ _context.next = 17;
105
107
  break;
106
108
 
107
- case 13:
108
- _context.next = 15;
109
+ case 14:
110
+ _context.next = 16;
109
111
  return pouchManager.storage.getDoctypeLastSequence(doctype);
110
112
 
111
- case 15:
113
+ case 16:
112
114
  seq = _context.sent;
113
115
 
114
- case 16:
116
+ case 17:
115
117
  replicationOptions = (0, _get.default)(pouchManager.doctypesReplicationOptions, doctype, {});
116
118
  replicationOptions.initialReplication = initialReplication;
117
119
  replicationOptions.filter = replicationFilter;
@@ -122,25 +124,25 @@ var replicateOnce = /*#__PURE__*/function () {
122
124
  pouchManager.options.onDoctypeSyncStart(doctype);
123
125
  }
124
126
 
125
- _context.next = 24;
127
+ _context.next = 25;
126
128
  return (0, _startReplication.startReplication)(pouch, replicationOptions, getReplicationURL, pouchManager.storage);
127
129
 
128
- case 24:
130
+ case 25:
129
131
  res = _context.sent;
130
132
 
131
133
  if (!seq) {
132
- _context.next = 28;
134
+ _context.next = 29;
133
135
  break;
134
136
  }
135
137
 
136
- _context.next = 28;
138
+ _context.next = 29;
137
139
  return pouchManager.storage.destroyDoctypeLastSequence(doctype);
138
140
 
139
- case 28:
140
- _context.next = 30;
141
+ case 29:
142
+ _context.next = 31;
141
143
  return pouchManager.updateSyncInfo(doctype);
142
144
 
143
- case 30:
145
+ case 31:
144
146
  pouchManager.checkToWarmupDoctype(doctype, replicationOptions);
145
147
 
146
148
  if (pouchManager.options.onDoctypeSyncEnd) {
@@ -149,7 +151,7 @@ var replicateOnce = /*#__PURE__*/function () {
149
151
 
150
152
  return _context.abrupt("return", res);
151
153
 
152
- case 33:
154
+ case 34:
153
155
  case "end":
154
156
  return _context.stop();
155
157
  }
@@ -209,10 +211,10 @@ var replicateOnce = /*#__PURE__*/function () {
209
211
  }
210
212
 
211
213
  unblockingError = _step.value;
212
- doctype = unblockingError[0]; // @ts-ignore
214
+ _doctype = unblockingError[0]; // @ts-ignore
213
215
 
214
216
  _context2.next = 28;
215
- return pouchManager.updateSyncInfo(doctype, 'not_complete');
217
+ return pouchManager.updateSyncInfo(_doctype, 'not_complete');
216
218
 
217
219
  case 28:
218
220
  _context2.next = 23;
package/dist/types.js CHANGED
@@ -1,10 +1,14 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
3
5
  Object.defineProperty(exports, "__esModule", {
4
6
  value: true
5
7
  });
6
8
  exports.default = void 0;
7
9
 
10
+ var _dbInterface = _interopRequireDefault(require("./db/dbInterface"));
11
+
8
12
  /**
9
13
  * @typedef {Object} Cancelable
10
14
  * @property {Function} [cancel] - Cancel the promise
@@ -39,6 +43,7 @@ exports.default = void 0;
39
43
  * @typedef {object} LinkPlatform
40
44
  * @property {LocalStorage} storage Methods to access local storage
41
45
  * @property {any} pouchAdapter PouchDB class (can be pouchdb-core or pouchdb-browser)
46
+ * @property { DatabaseQueryEngine } queryEngine
42
47
  * @property {function(): Promise<boolean>} isOnline Method that check if the app is connected to internet
43
48
  */
44
49