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.
- package/dist/CozyPouchLink.js +234 -470
- package/dist/CozyPouchLink.spec.js +6 -147
- package/dist/PouchManager.js +43 -8
- package/dist/PouchManager.spec.js +21 -12
- package/dist/__mocks__/@op-engineering/op-sqlite.js +11 -0
- package/dist/db/dbInterface.js +190 -0
- package/dist/db/helpers.js +128 -0
- package/dist/db/pouchdb/getDocs.js +157 -0
- package/dist/db/pouchdb/getDocs.spec.js +63 -0
- package/dist/db/pouchdb/pouchdb.js +264 -0
- package/dist/db/pouchdb/pouchdb.spec.js +151 -0
- package/dist/db/sqlite/sql.js +422 -0
- package/dist/db/sqlite/sql.spec.js +419 -0
- package/dist/db/sqlite/sqliteDb.js +41 -0
- package/dist/db/sqlite/sqliteDb.native.js +317 -0
- package/dist/errors.js +17 -2
- package/dist/helpers.js +21 -147
- package/dist/helpers.spec.js +1 -98
- package/dist/index.js +9 -1
- package/dist/jsonapi.js +57 -21
- package/dist/jsonapi.spec.js +105 -32
- package/dist/mango.js +146 -3
- package/dist/migrations/pouchdb.js +32 -0
- package/dist/replicateOnce.js +25 -23
- package/dist/types.js +5 -0
- package/dist/utils.js +33 -3
- package/package.json +5 -3
- package/types/CozyPouchLink.d.ts +6 -63
- package/types/PouchManager.d.ts +6 -1
- package/types/__mocks__/@op-engineering/op-sqlite.d.ts +1 -0
- package/types/db/dbInterface.d.ts +117 -0
- package/types/db/helpers.d.ts +4 -0
- package/types/db/pouchdb/getDocs.d.ts +18 -0
- package/types/db/pouchdb/pouchdb.d.ts +8 -0
- package/types/db/sqlite/sql.d.ts +45 -0
- package/types/db/sqlite/sqliteDb.d.ts +4 -0
- package/types/db/sqlite/sqliteDb.native.d.ts +7 -0
- package/types/errors.d.ts +2 -0
- package/types/helpers.d.ts +1 -4
- package/types/index.d.ts +1 -0
- package/types/jsonapi.d.ts +4 -2
- package/types/mango.d.ts +19 -1
- package/types/migrations/pouchdb.d.ts +1 -0
- package/types/types.d.ts +2 -0
- package/types/utils.d.ts +3 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.getCozyPouchData = exports.areDocsEqual = exports.getExistingDocument = void 0;
|
|
9
|
+
|
|
10
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
11
|
+
|
|
12
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
13
|
+
|
|
14
|
+
var _fastDeepEqual = _interopRequireDefault(require("fast-deep-equal"));
|
|
15
|
+
|
|
16
|
+
var _dbInterface = _interopRequireDefault(require("./dbInterface"));
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Retrieve the existing document from Pouch
|
|
20
|
+
*
|
|
21
|
+
* @param {DatabaseQueryEngine} queryEngine - The query engine
|
|
22
|
+
* @param {string} id - ID of the document to retrieve
|
|
23
|
+
* @param {boolean} throwIfNotFound - If true the method will throw when the document is not found. Otherwise it will return null
|
|
24
|
+
* @returns {Promise<import('./dbInterface').QueryResponseSingleDoc>}
|
|
25
|
+
*/
|
|
26
|
+
var getExistingDocument = /*#__PURE__*/function () {
|
|
27
|
+
var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(queryEngine, id) {
|
|
28
|
+
var throwIfNotFound,
|
|
29
|
+
existingDoc,
|
|
30
|
+
_args = arguments;
|
|
31
|
+
return _regenerator.default.wrap(function _callee$(_context) {
|
|
32
|
+
while (1) {
|
|
33
|
+
switch (_context.prev = _context.next) {
|
|
34
|
+
case 0:
|
|
35
|
+
throwIfNotFound = _args.length > 2 && _args[2] !== undefined ? _args[2] : false;
|
|
36
|
+
_context.prev = 1;
|
|
37
|
+
_context.next = 4;
|
|
38
|
+
return queryEngine.getById(id);
|
|
39
|
+
|
|
40
|
+
case 4:
|
|
41
|
+
existingDoc = _context.sent;
|
|
42
|
+
return _context.abrupt("return", existingDoc);
|
|
43
|
+
|
|
44
|
+
case 8:
|
|
45
|
+
_context.prev = 8;
|
|
46
|
+
_context.t0 = _context["catch"](1);
|
|
47
|
+
|
|
48
|
+
if (!(_context.t0.name === 'not_found' && !throwIfNotFound)) {
|
|
49
|
+
_context.next = 14;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return _context.abrupt("return", null);
|
|
54
|
+
|
|
55
|
+
case 14:
|
|
56
|
+
throw _context.t0;
|
|
57
|
+
|
|
58
|
+
case 15:
|
|
59
|
+
case "end":
|
|
60
|
+
return _context.stop();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}, _callee, null, [[1, 8]]);
|
|
64
|
+
}));
|
|
65
|
+
|
|
66
|
+
return function getExistingDocument(_x, _x2) {
|
|
67
|
+
return _ref.apply(this, arguments);
|
|
68
|
+
};
|
|
69
|
+
}();
|
|
70
|
+
|
|
71
|
+
exports.getExistingDocument = getExistingDocument;
|
|
72
|
+
|
|
73
|
+
var areDocsEqual = /*#__PURE__*/function () {
|
|
74
|
+
var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(oldDoc, newDoc) {
|
|
75
|
+
return _regenerator.default.wrap(function _callee2$(_context2) {
|
|
76
|
+
while (1) {
|
|
77
|
+
switch (_context2.prev = _context2.next) {
|
|
78
|
+
case 0:
|
|
79
|
+
// Do not rely on revisions as they can be missing or wrong
|
|
80
|
+
newDoc._rev = undefined;
|
|
81
|
+
oldDoc._rev = undefined;
|
|
82
|
+
|
|
83
|
+
if (!(0, _fastDeepEqual.default)(oldDoc, newDoc)) {
|
|
84
|
+
_context2.next = 4;
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return _context2.abrupt("return", true);
|
|
89
|
+
|
|
90
|
+
case 4:
|
|
91
|
+
return _context2.abrupt("return", false);
|
|
92
|
+
|
|
93
|
+
case 5:
|
|
94
|
+
case "end":
|
|
95
|
+
return _context2.stop();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}, _callee2);
|
|
99
|
+
}));
|
|
100
|
+
|
|
101
|
+
return function areDocsEqual(_x3, _x4) {
|
|
102
|
+
return _ref2.apply(this, arguments);
|
|
103
|
+
};
|
|
104
|
+
}();
|
|
105
|
+
/**
|
|
106
|
+
* Get cozyPouchData from a document
|
|
107
|
+
*
|
|
108
|
+
* Sometimes, queries are transformed by Collections and they call a dedicated
|
|
109
|
+
* cozy-stack route. When this is the case, we want to be able to replicate the same
|
|
110
|
+
* query from cozy-pouch-link. It is not possible as-is because the received data
|
|
111
|
+
* is not the same as the one stored in the Couch database
|
|
112
|
+
* To handle this, we store the received data in the Pouch with a dedicated id and
|
|
113
|
+
* we store the query result in a `cozyPouchData` attribute
|
|
114
|
+
* So when `cozyPouchData` attribute exists, we know that we want to return its content
|
|
115
|
+
* as the result of the query
|
|
116
|
+
*
|
|
117
|
+
* @param {import('../CozyPouchLink').CozyPouchDocument} doc - The PouchDB doc
|
|
118
|
+
* @returns { Array<import('cozy-client/types/types').CozyClientDocument>} The formatted docs
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
exports.areDocsEqual = areDocsEqual;
|
|
123
|
+
|
|
124
|
+
var getCozyPouchData = function getCozyPouchData(doc) {
|
|
125
|
+
return doc.cozyPouchData;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
exports.getCozyPouchData = getCozyPouchData;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.getDocs = exports.getDocsAndNormalize = void 0;
|
|
9
|
+
|
|
10
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
11
|
+
|
|
12
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
13
|
+
|
|
14
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
15
|
+
|
|
16
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
17
|
+
|
|
18
|
+
var _helpers = _interopRequireDefault(require("../../helpers"));
|
|
19
|
+
|
|
20
|
+
var _jsonapi = require("../../jsonapi");
|
|
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
|
+
|
|
26
|
+
var isAdapterBugged = _helpers.default.isAdapterBugged,
|
|
27
|
+
LIMIT_BUG = _helpers.default.LIMIT_BUG;
|
|
28
|
+
|
|
29
|
+
var getDocsAndNormalize = /*#__PURE__*/function () {
|
|
30
|
+
var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(_ref) {
|
|
31
|
+
var client, doctype, db, queryFunc, _ref$queryParams, queryParams, _ref$withRows, withRows, results, jsonResult;
|
|
32
|
+
|
|
33
|
+
return _regenerator.default.wrap(function _callee$(_context) {
|
|
34
|
+
while (1) {
|
|
35
|
+
switch (_context.prev = _context.next) {
|
|
36
|
+
case 0:
|
|
37
|
+
client = _ref.client, doctype = _ref.doctype, db = _ref.db, queryFunc = _ref.queryFunc, _ref$queryParams = _ref.queryParams, queryParams = _ref$queryParams === void 0 ? {} : _ref$queryParams, _ref$withRows = _ref.withRows, withRows = _ref$withRows === void 0 ? true : _ref$withRows;
|
|
38
|
+
_context.next = 3;
|
|
39
|
+
return getDocs(db, queryFunc, queryParams);
|
|
40
|
+
|
|
41
|
+
case 3:
|
|
42
|
+
results = _context.sent;
|
|
43
|
+
jsonResult = (0, _jsonapi.fromPouchResult)({
|
|
44
|
+
res: results,
|
|
45
|
+
withRows: withRows,
|
|
46
|
+
doctype: doctype,
|
|
47
|
+
client: client
|
|
48
|
+
});
|
|
49
|
+
return _context.abrupt("return", jsonResult);
|
|
50
|
+
|
|
51
|
+
case 6:
|
|
52
|
+
case "end":
|
|
53
|
+
return _context.stop();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}, _callee);
|
|
57
|
+
}));
|
|
58
|
+
|
|
59
|
+
return function getDocsAndNormalize(_x) {
|
|
60
|
+
return _ref2.apply(this, arguments);
|
|
61
|
+
};
|
|
62
|
+
}();
|
|
63
|
+
|
|
64
|
+
exports.getDocsAndNormalize = getDocsAndNormalize;
|
|
65
|
+
|
|
66
|
+
var getDocs = /*#__PURE__*/function () {
|
|
67
|
+
var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(db, fct) {
|
|
68
|
+
var queryParams,
|
|
69
|
+
limit,
|
|
70
|
+
field,
|
|
71
|
+
data,
|
|
72
|
+
next,
|
|
73
|
+
_args2 = arguments;
|
|
74
|
+
return _regenerator.default.wrap(function _callee2$(_context2) {
|
|
75
|
+
while (1) {
|
|
76
|
+
switch (_context2.prev = _context2.next) {
|
|
77
|
+
case 0:
|
|
78
|
+
queryParams = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : {};
|
|
79
|
+
// allDocs return an error when limit is null
|
|
80
|
+
if (queryParams.limit === null) delete queryParams.limit;
|
|
81
|
+
limit = queryParams.limit;
|
|
82
|
+
field = fct === 'allDocs' ? 'rows' : 'docs';
|
|
83
|
+
|
|
84
|
+
if (isAdapterBugged(db.adapter)) {
|
|
85
|
+
// FIXME: to remove? This was used to deal with an old adapter that is probably no longer used
|
|
86
|
+
// But it might be worth checking the issue does not exist anymore
|
|
87
|
+
if (limit === undefined || limit > LIMIT_BUG) {
|
|
88
|
+
queryParams.limit = LIMIT_BUG;
|
|
89
|
+
queryParams.skip = queryParams.skip || 0;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (!(fct === 'get')) {
|
|
94
|
+
_context2.next = 9;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (queryParams.id) {
|
|
99
|
+
_context2.next = 8;
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return _context2.abrupt("return", null);
|
|
104
|
+
|
|
105
|
+
case 8:
|
|
106
|
+
return _context2.abrupt("return", db.get(queryParams.id));
|
|
107
|
+
|
|
108
|
+
case 9:
|
|
109
|
+
_context2.next = 11;
|
|
110
|
+
return db[fct](queryParams);
|
|
111
|
+
|
|
112
|
+
case 11:
|
|
113
|
+
data = _context2.sent;
|
|
114
|
+
|
|
115
|
+
if (!(data[field].length === queryParams.limit)) {
|
|
116
|
+
_context2.next = 20;
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
queryParams.skip = (queryParams.skip ? queryParams.skip : 0) + queryParams.limit;
|
|
121
|
+
queryParams.limit = limit ? limit - queryParams.limit : undefined;
|
|
122
|
+
|
|
123
|
+
if (!(queryParams.limit > 0 || queryParams.limit === undefined)) {
|
|
124
|
+
_context2.next = 20;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
_context2.next = 18;
|
|
129
|
+
return getDocs(db, fct, queryParams);
|
|
130
|
+
|
|
131
|
+
case 18:
|
|
132
|
+
next = _context2.sent;
|
|
133
|
+
return _context2.abrupt("return", _objectSpread(_objectSpread({}, data), {}, (0, _defineProperty2.default)({}, field, [].concat((0, _toConsumableArray2.default)(data[field]), (0, _toConsumableArray2.default)(next[field])))));
|
|
134
|
+
|
|
135
|
+
case 20:
|
|
136
|
+
if (queryParams.keys) {
|
|
137
|
+
// Special case for getByIds queries: Pouch returns the total number of rows in the database,
|
|
138
|
+
// which will make cozy-client believes there are more results to fetch
|
|
139
|
+
data.total_rows = data.rows.length;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return _context2.abrupt("return", data);
|
|
143
|
+
|
|
144
|
+
case 22:
|
|
145
|
+
case "end":
|
|
146
|
+
return _context2.stop();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}, _callee2);
|
|
150
|
+
}));
|
|
151
|
+
|
|
152
|
+
return function getDocs(_x2, _x3) {
|
|
153
|
+
return _ref3.apply(this, arguments);
|
|
154
|
+
};
|
|
155
|
+
}();
|
|
156
|
+
|
|
157
|
+
exports.getDocs = getDocs;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { getDocs } from './getDocs'
|
|
2
|
+
|
|
3
|
+
import PouchDB from 'pouchdb-browser'
|
|
4
|
+
import PouchDBFind from 'pouchdb-find'
|
|
5
|
+
import adapter from 'pouchdb-adapter-memory'
|
|
6
|
+
PouchDB.plugin(PouchDBFind)
|
|
7
|
+
PouchDB.plugin(adapter)
|
|
8
|
+
|
|
9
|
+
const insertData = async (db, number) => {
|
|
10
|
+
const docs = []
|
|
11
|
+
for (let i = 0; i < number; i++) {
|
|
12
|
+
docs.push({ _id: `doc${i}`, status: true })
|
|
13
|
+
}
|
|
14
|
+
await db.bulkDocs(docs)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe('getDocs', () => {
|
|
18
|
+
let db, options
|
|
19
|
+
|
|
20
|
+
beforeEach(async () => {
|
|
21
|
+
db = new PouchDB('test', { adapter: 'memory' })
|
|
22
|
+
options = { selector: { status: { $eq: true } } }
|
|
23
|
+
await db.createIndex({ index: { fields: ['status'] } })
|
|
24
|
+
await insertData(db, 105)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
afterEach(async () => {
|
|
28
|
+
await db.destroy()
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('should get all docs', async () => {
|
|
32
|
+
const resp = await getDocs(db, 'allDocs', options)
|
|
33
|
+
expect(resp.rows).toHaveLength(106) // include design doc
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('should get 20 docs with allDocs', async () => {
|
|
37
|
+
const resp = await getDocs(db, 'allDocs', { limit: 20 })
|
|
38
|
+
expect(resp.rows).toHaveLength(20)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('should find docs with default limit', async () => {
|
|
42
|
+
const resp = await getDocs(db, 'find', options)
|
|
43
|
+
expect(resp.docs).toHaveLength(105) // does not include design doc
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('should get 20 docs with find', async () => {
|
|
47
|
+
const resp = await getDocs(db, 'find', { ...options, limit: 20 })
|
|
48
|
+
expect(resp.docs).toHaveLength(20)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('should get a single doc by id', async () => {
|
|
52
|
+
const resp = await getDocs(db, 'get', { ...options, id: 'doc0' })
|
|
53
|
+
expect(resp._id).toEqual('doc0')
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('should get a multiple docs by ids', async () => {
|
|
57
|
+
const resp = await getDocs(db, 'allDocs', {
|
|
58
|
+
...options,
|
|
59
|
+
keys: ['doc0', 'doc1', 'doc2']
|
|
60
|
+
})
|
|
61
|
+
expect(resp.rows).toHaveLength(3)
|
|
62
|
+
})
|
|
63
|
+
})
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = void 0;
|
|
9
|
+
|
|
10
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
11
|
+
|
|
12
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
13
|
+
|
|
14
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
15
|
+
|
|
16
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
17
|
+
|
|
18
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
19
|
+
|
|
20
|
+
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
|
21
|
+
|
|
22
|
+
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
|
|
23
|
+
|
|
24
|
+
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
|
|
25
|
+
|
|
26
|
+
var _mango = require("../../mango");
|
|
27
|
+
|
|
28
|
+
var _dbInterface = _interopRequireDefault(require("../dbInterface"));
|
|
29
|
+
|
|
30
|
+
var _errors = require("../../errors");
|
|
31
|
+
|
|
32
|
+
var _getDocs = require("./getDocs");
|
|
33
|
+
|
|
34
|
+
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; }
|
|
35
|
+
|
|
36
|
+
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; }
|
|
37
|
+
|
|
38
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
|
|
39
|
+
|
|
40
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
|
|
41
|
+
|
|
42
|
+
var PouchDBQueryEngine = /*#__PURE__*/function (_DatabaseQueryEngine) {
|
|
43
|
+
(0, _inherits2.default)(PouchDBQueryEngine, _DatabaseQueryEngine);
|
|
44
|
+
|
|
45
|
+
var _super = _createSuper(PouchDBQueryEngine);
|
|
46
|
+
|
|
47
|
+
function PouchDBQueryEngine(pouchManager, doctype) {
|
|
48
|
+
var _this;
|
|
49
|
+
|
|
50
|
+
(0, _classCallCheck2.default)(this, PouchDBQueryEngine);
|
|
51
|
+
_this = _super.call(this);
|
|
52
|
+
_this.pouchManager = pouchManager;
|
|
53
|
+
_this.client = pouchManager === null || pouchManager === void 0 ? void 0 : pouchManager.client;
|
|
54
|
+
_this.doctype = doctype;
|
|
55
|
+
_this.db = null;
|
|
56
|
+
return _this;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
(0, _createClass2.default)(PouchDBQueryEngine, [{
|
|
60
|
+
key: "openDB",
|
|
61
|
+
value: function openDB(dbName) {
|
|
62
|
+
this.db = this.pouchManager.getPouch(dbName);
|
|
63
|
+
return this.db;
|
|
64
|
+
}
|
|
65
|
+
}, {
|
|
66
|
+
key: "allDocs",
|
|
67
|
+
value: function () {
|
|
68
|
+
var _allDocs = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(options) {
|
|
69
|
+
return _regenerator.default.wrap(function _callee$(_context) {
|
|
70
|
+
while (1) {
|
|
71
|
+
switch (_context.prev = _context.next) {
|
|
72
|
+
case 0:
|
|
73
|
+
return _context.abrupt("return", (0, _getDocs.getDocsAndNormalize)({
|
|
74
|
+
client: this.client,
|
|
75
|
+
db: this.db,
|
|
76
|
+
doctype: this.doctype,
|
|
77
|
+
queryFunc: 'allDocs',
|
|
78
|
+
queryParams: _objectSpread({
|
|
79
|
+
include_docs: true
|
|
80
|
+
}, options)
|
|
81
|
+
}));
|
|
82
|
+
|
|
83
|
+
case 1:
|
|
84
|
+
case "end":
|
|
85
|
+
return _context.stop();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}, _callee, this);
|
|
89
|
+
}));
|
|
90
|
+
|
|
91
|
+
function allDocs(_x) {
|
|
92
|
+
return _allDocs.apply(this, arguments);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return allDocs;
|
|
96
|
+
}()
|
|
97
|
+
}, {
|
|
98
|
+
key: "getById",
|
|
99
|
+
value: function () {
|
|
100
|
+
var _getById = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(id) {
|
|
101
|
+
return _regenerator.default.wrap(function _callee2$(_context2) {
|
|
102
|
+
while (1) {
|
|
103
|
+
switch (_context2.prev = _context2.next) {
|
|
104
|
+
case 0:
|
|
105
|
+
return _context2.abrupt("return", (0, _getDocs.getDocsAndNormalize)({
|
|
106
|
+
client: this.client,
|
|
107
|
+
db: this.db,
|
|
108
|
+
doctype: this.doctype,
|
|
109
|
+
queryFunc: 'get',
|
|
110
|
+
queryParams: {
|
|
111
|
+
id: id
|
|
112
|
+
},
|
|
113
|
+
withRows: false
|
|
114
|
+
}));
|
|
115
|
+
|
|
116
|
+
case 1:
|
|
117
|
+
case "end":
|
|
118
|
+
return _context2.stop();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}, _callee2, this);
|
|
122
|
+
}));
|
|
123
|
+
|
|
124
|
+
function getById(_x2) {
|
|
125
|
+
return _getById.apply(this, arguments);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return getById;
|
|
129
|
+
}()
|
|
130
|
+
}, {
|
|
131
|
+
key: "getByIds",
|
|
132
|
+
value: function () {
|
|
133
|
+
var _getByIds = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(ids) {
|
|
134
|
+
return _regenerator.default.wrap(function _callee3$(_context3) {
|
|
135
|
+
while (1) {
|
|
136
|
+
switch (_context3.prev = _context3.next) {
|
|
137
|
+
case 0:
|
|
138
|
+
return _context3.abrupt("return", (0, _getDocs.getDocsAndNormalize)({
|
|
139
|
+
client: this.client,
|
|
140
|
+
db: this.db,
|
|
141
|
+
doctype: this.doctype,
|
|
142
|
+
queryFunc: 'allDocs',
|
|
143
|
+
queryParams: {
|
|
144
|
+
include_docs: true,
|
|
145
|
+
keys: ids
|
|
146
|
+
}
|
|
147
|
+
}));
|
|
148
|
+
|
|
149
|
+
case 1:
|
|
150
|
+
case "end":
|
|
151
|
+
return _context3.stop();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}, _callee3, this);
|
|
155
|
+
}));
|
|
156
|
+
|
|
157
|
+
function getByIds(_x3) {
|
|
158
|
+
return _getByIds.apply(this, arguments);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return getByIds;
|
|
162
|
+
}()
|
|
163
|
+
}, {
|
|
164
|
+
key: "find",
|
|
165
|
+
value: function () {
|
|
166
|
+
var _find = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(options) {
|
|
167
|
+
var selector, sort, partialFilter, doctype, indexedFields, indexName, findOpts, res;
|
|
168
|
+
return _regenerator.default.wrap(function _callee4$(_context4) {
|
|
169
|
+
while (1) {
|
|
170
|
+
switch (_context4.prev = _context4.next) {
|
|
171
|
+
case 0:
|
|
172
|
+
selector = options.selector, sort = options.sort, partialFilter = options.partialFilter, doctype = options.doctype;
|
|
173
|
+
indexedFields = options.indexedFields;
|
|
174
|
+
indexedFields = (0, _mango.getIndexFields)({
|
|
175
|
+
indexedFields: indexedFields,
|
|
176
|
+
selector: selector,
|
|
177
|
+
sort: sort,
|
|
178
|
+
partialFilter: partialFilter
|
|
179
|
+
});
|
|
180
|
+
indexName = (0, _mango.getIndexName)({
|
|
181
|
+
selector: selector,
|
|
182
|
+
sort: sort,
|
|
183
|
+
partialFilter: partialFilter,
|
|
184
|
+
indexedFields: indexedFields
|
|
185
|
+
});
|
|
186
|
+
findOpts = _objectSpread({
|
|
187
|
+
selector: selector,
|
|
188
|
+
sort: sort,
|
|
189
|
+
partialFilter: partialFilter,
|
|
190
|
+
doctype: doctype,
|
|
191
|
+
use_index: indexName
|
|
192
|
+
}, options);
|
|
193
|
+
findOpts.indexedFields = indexedFields;
|
|
194
|
+
_context4.prev = 6;
|
|
195
|
+
_context4.next = 9;
|
|
196
|
+
return (0, _getDocs.getDocsAndNormalize)({
|
|
197
|
+
client: this.client,
|
|
198
|
+
db: this.db,
|
|
199
|
+
doctype: this.doctype,
|
|
200
|
+
queryFunc: 'find',
|
|
201
|
+
queryParams: findOpts
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
case 9:
|
|
205
|
+
res = _context4.sent;
|
|
206
|
+
_context4.next = 23;
|
|
207
|
+
break;
|
|
208
|
+
|
|
209
|
+
case 12:
|
|
210
|
+
_context4.prev = 12;
|
|
211
|
+
_context4.t0 = _context4["catch"](6);
|
|
212
|
+
|
|
213
|
+
if (!(0, _errors.isMissingPouchDBIndexError)(_context4.t0)) {
|
|
214
|
+
_context4.next = 22;
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
_context4.next = 17;
|
|
219
|
+
return (0, _mango.createIndex)(this.db, indexedFields, {
|
|
220
|
+
indexName: indexName,
|
|
221
|
+
doctype: doctype,
|
|
222
|
+
partialFilter: partialFilter
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
case 17:
|
|
226
|
+
_context4.next = 19;
|
|
227
|
+
return (0, _getDocs.getDocsAndNormalize)({
|
|
228
|
+
client: this.client,
|
|
229
|
+
db: this.db,
|
|
230
|
+
doctype: this.doctype,
|
|
231
|
+
queryFunc: 'find',
|
|
232
|
+
queryParams: findOpts
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
case 19:
|
|
236
|
+
res = _context4.sent;
|
|
237
|
+
_context4.next = 23;
|
|
238
|
+
break;
|
|
239
|
+
|
|
240
|
+
case 22:
|
|
241
|
+
throw _context4.t0;
|
|
242
|
+
|
|
243
|
+
case 23:
|
|
244
|
+
return _context4.abrupt("return", res);
|
|
245
|
+
|
|
246
|
+
case 24:
|
|
247
|
+
case "end":
|
|
248
|
+
return _context4.stop();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}, _callee4, this, [[6, 12]]);
|
|
252
|
+
}));
|
|
253
|
+
|
|
254
|
+
function find(_x4) {
|
|
255
|
+
return _find.apply(this, arguments);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return find;
|
|
259
|
+
}()
|
|
260
|
+
}]);
|
|
261
|
+
return PouchDBQueryEngine;
|
|
262
|
+
}(_dbInterface.default);
|
|
263
|
+
|
|
264
|
+
exports.default = PouchDBQueryEngine;
|