cozy-pouch-link 57.5.0 → 57.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CozyPouchLink.js +221 -469
- 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 +106 -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 +418 -0
- package/dist/db/sqlite/sql.spec.js +363 -0
- package/dist/db/sqlite/sqliteDb.js +319 -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 +49 -10
- 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 +4 -3
- package/types/CozyPouchLink.d.ts +4 -60
- 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 +3 -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 +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 +2 -0
- 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,363 @@
|
|
|
1
|
+
import {
|
|
2
|
+
mangoSelectorToSQL,
|
|
3
|
+
makeWhereClause,
|
|
4
|
+
makeSortClause,
|
|
5
|
+
makeSQLQueryFromMango,
|
|
6
|
+
keepDocWitHighestRev,
|
|
7
|
+
makeSQLQueryAll,
|
|
8
|
+
parseResults
|
|
9
|
+
} from './sql'
|
|
10
|
+
|
|
11
|
+
describe('mangoSelectorToSQL', () => {
|
|
12
|
+
it('should return empty string for empty selector', () => {
|
|
13
|
+
const selector = {}
|
|
14
|
+
expect(mangoSelectorToSQL(selector)).toBe('')
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('should handle implicit equality selector', () => {
|
|
18
|
+
const selector = { status: 'active' }
|
|
19
|
+
expect(mangoSelectorToSQL(selector)).toBe(
|
|
20
|
+
"json_extract(data, '$.status') = 'active'"
|
|
21
|
+
)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should handle explicit $eq operator', () => {
|
|
25
|
+
const selector = { status: { $eq: 'active' } }
|
|
26
|
+
expect(mangoSelectorToSQL(selector)).toBe(
|
|
27
|
+
"json_extract(data, '$.status') = 'active'"
|
|
28
|
+
)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('should handle $neq operator', () => {
|
|
32
|
+
const selector = { status: { $ne: 'active' } }
|
|
33
|
+
expect(mangoSelectorToSQL(selector)).toBe(
|
|
34
|
+
"json_extract(data, '$.status') != 'active'"
|
|
35
|
+
)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('should handle $in and $nin operator', () => {
|
|
39
|
+
const selector = {
|
|
40
|
+
status: { $in: ['active', 'pending'] },
|
|
41
|
+
other_status: { $nin: ['maintenance', 'failing'] }
|
|
42
|
+
}
|
|
43
|
+
expect(mangoSelectorToSQL(selector)).toBe(
|
|
44
|
+
"json_extract(data, '$.status') IN ('active', 'pending') AND json_extract(data, '$.other_status') NOT IN ('maintenance', 'failing')"
|
|
45
|
+
)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('should handle $exists operator', () => {
|
|
49
|
+
const selector1 = { status: { $exists: true } }
|
|
50
|
+
expect(mangoSelectorToSQL(selector1)).toBe(
|
|
51
|
+
"json_extract(data, '$.status') IS NOT NULL"
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
const selector2 = { status: { $exists: false } }
|
|
55
|
+
expect(mangoSelectorToSQL(selector2)).toBe(
|
|
56
|
+
"json_extract(data, '$.status') IS NULL"
|
|
57
|
+
)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('should handle implicit $and operator', () => {
|
|
61
|
+
const selector = {
|
|
62
|
+
age: 18,
|
|
63
|
+
status: 'active',
|
|
64
|
+
date: '2025-01-01'
|
|
65
|
+
}
|
|
66
|
+
expect(mangoSelectorToSQL(selector)).toBe(
|
|
67
|
+
"json_extract(data, '$.age') = 18 AND json_extract(data, '$.status') = 'active' AND json_extract(data, '$.date') = '2025-01-01'"
|
|
68
|
+
)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('should handle range operators', () => {
|
|
72
|
+
const selector1 = { date: { $gt: '2025-01-01', $lt: '2026-01-01' } }
|
|
73
|
+
expect(mangoSelectorToSQL(selector1)).toBe(
|
|
74
|
+
"json_extract(data, '$.date') > '2025-01-01' AND json_extract(data, '$.date') < '2026-01-01'"
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
const selector2 = {
|
|
78
|
+
startDate: { $gte: '2025-01-01' },
|
|
79
|
+
endDate: { $lte: '2026-01-01' }
|
|
80
|
+
}
|
|
81
|
+
expect(mangoSelectorToSQL(selector2)).toBe(
|
|
82
|
+
"json_extract(data, '$.startDate') >= '2025-01-01' AND json_extract(data, '$.endDate') <= '2026-01-01'"
|
|
83
|
+
)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('should handle $gt: null cases', () => {
|
|
87
|
+
const selector = { date: { $gt: null } }
|
|
88
|
+
expect(mangoSelectorToSQL(selector)).toBe(
|
|
89
|
+
"json_extract(data, '$.date') IS NOT NULL"
|
|
90
|
+
)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('should handle explicit $and operator', () => {
|
|
94
|
+
const selector = { $and: [{ age: { $gte: 18 } }, { status: 'active' }] }
|
|
95
|
+
expect(mangoSelectorToSQL(selector)).toBe(
|
|
96
|
+
"(json_extract(data, '$.age') >= 18) AND (json_extract(data, '$.status') = 'active')"
|
|
97
|
+
)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it('should handle explicit $or operator', () => {
|
|
101
|
+
const selector = { $or: [{ status: 'active' }, { status: 'pending' }] }
|
|
102
|
+
expect(mangoSelectorToSQL(selector)).toBe(
|
|
103
|
+
"(json_extract(data, '$.status') = 'active') OR (json_extract(data, '$.status') = 'pending')"
|
|
104
|
+
)
|
|
105
|
+
})
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
describe('makeWhereClause', () => {
|
|
109
|
+
it('should return only deleted clause when no mango selector', () => {
|
|
110
|
+
expect(makeWhereClause(undefined)).toEqual('DELETED = 0')
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('should return deleted and mango clauses when there is a mango selector', () => {
|
|
114
|
+
const selector = { status: 'active' }
|
|
115
|
+
expect(makeWhereClause(selector)).toEqual(
|
|
116
|
+
"DELETED = 0 AND json_extract(data, '$.status') = 'active'"
|
|
117
|
+
)
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
describe('makeSortClause', () => {
|
|
122
|
+
it('should return null when no mango sort', () => {
|
|
123
|
+
const sortBy = undefined
|
|
124
|
+
expect(makeSortClause(sortBy)).toBe(null)
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
it('should return correct order by, with one sorting attribute', () => {
|
|
128
|
+
const sortBy = [{ date: 'asc' }]
|
|
129
|
+
expect(makeSortClause(sortBy)).toEqual("json_extract(data, '$.date') ASC")
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it('should return correct order by, with multiple sorting attribute', () => {
|
|
133
|
+
const sortBy = [{ date: 'asc' }, { name: 'asc' }, { type: 'asc' }]
|
|
134
|
+
expect(makeSortClause(sortBy)).toEqual(
|
|
135
|
+
"json_extract(data, '$.date'), json_extract(data, '$.name'), json_extract(data, '$.type') ASC"
|
|
136
|
+
)
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('should deal with ascending and descending order', () => {
|
|
140
|
+
const sortBy1 = [{ date: 'asc' }]
|
|
141
|
+
expect(makeSortClause(sortBy1)).toEqual("json_extract(data, '$.date') ASC")
|
|
142
|
+
const sortBy2 = [{ date: 'desc' }]
|
|
143
|
+
expect(makeSortClause(sortBy2)).toEqual("json_extract(data, '$.date') DESC")
|
|
144
|
+
})
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
describe('makeSQLQueryFromMango', () => {
|
|
148
|
+
it('should return a correct SQL query with no sort', () => {
|
|
149
|
+
const selector = { date: { $gt: '2025-01-01' } }
|
|
150
|
+
const indexName = 'by_name'
|
|
151
|
+
const limit = 100
|
|
152
|
+
const sql = makeSQLQueryFromMango({ selector, indexName, limit })
|
|
153
|
+
|
|
154
|
+
const expectedSql = [
|
|
155
|
+
`SELECT json AS data, doc_id, rev`,
|
|
156
|
+
`FROM 'by-sequence' INDEXED BY by_name`,
|
|
157
|
+
`WHERE DELETED = 0 AND json_extract(data, '$.date') > '2025-01-01'`,
|
|
158
|
+
`LIMIT 100;`
|
|
159
|
+
].join(' ')
|
|
160
|
+
expect(sql).toEqual(expectedSql)
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
it('should return a correct SQL query with sort', () => {
|
|
164
|
+
const selector = { date: { $gt: '2025-01-01' } }
|
|
165
|
+
const sort = [{ date: 'asc' }]
|
|
166
|
+
const indexName = 'by_name'
|
|
167
|
+
const limit = 100
|
|
168
|
+
const sql = makeSQLQueryFromMango({ selector, sort, indexName, limit })
|
|
169
|
+
|
|
170
|
+
const expectedSql = [
|
|
171
|
+
`SELECT json AS data, doc_id, rev`,
|
|
172
|
+
`FROM 'by-sequence' INDEXED BY by_name`,
|
|
173
|
+
`WHERE DELETED = 0 AND json_extract(data, '$.date') > '2025-01-01'`,
|
|
174
|
+
`LIMIT 100`,
|
|
175
|
+
`ORDER BY json_extract(data, '$.date') ASC;`
|
|
176
|
+
].join(' ')
|
|
177
|
+
expect(sql).toEqual(expectedSql)
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
it('should handle the skip and limit', () => {
|
|
181
|
+
const selector = { date: { $gt: '2025-01-01' } }
|
|
182
|
+
const indexName = 'by_name'
|
|
183
|
+
const limit = 200
|
|
184
|
+
const skip = 100
|
|
185
|
+
const sql = makeSQLQueryFromMango({
|
|
186
|
+
selector,
|
|
187
|
+
indexName,
|
|
188
|
+
limit,
|
|
189
|
+
skip
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
const expectedSql = [
|
|
193
|
+
`SELECT json AS data, doc_id, rev`,
|
|
194
|
+
`FROM 'by-sequence' INDEXED BY by_name`,
|
|
195
|
+
`WHERE DELETED = 0 AND json_extract(data, '$.date') > '2025-01-01'`,
|
|
196
|
+
`LIMIT 200`,
|
|
197
|
+
`OFFSET 100;`
|
|
198
|
+
].join(' ')
|
|
199
|
+
expect(sql).toEqual(expectedSql)
|
|
200
|
+
})
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
describe('makeSQLQueryAll', () => {
|
|
204
|
+
it('should return a correct sql query to get all docs', () => {
|
|
205
|
+
const sql = makeSQLQueryAll()
|
|
206
|
+
const expectedSql = [
|
|
207
|
+
`SELECT json AS data, doc_id, rev`,
|
|
208
|
+
`FROM 'by-sequence'`,
|
|
209
|
+
`WHERE deleted=0`,
|
|
210
|
+
`LIMIT -1;`
|
|
211
|
+
].join(' ')
|
|
212
|
+
expect(sql).toEqual(expectedSql)
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
it('should handle limit and skip', () => {
|
|
216
|
+
const sql = makeSQLQueryAll({ limit: 10, skip: 100 })
|
|
217
|
+
const expectedSql = [
|
|
218
|
+
`SELECT json AS data, doc_id, rev`,
|
|
219
|
+
`FROM 'by-sequence'`,
|
|
220
|
+
`WHERE deleted=0`,
|
|
221
|
+
`LIMIT 10`,
|
|
222
|
+
`OFFSET 100;`
|
|
223
|
+
].join(' ')
|
|
224
|
+
expect(sql).toEqual(expectedSql)
|
|
225
|
+
})
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
describe('keepDocWitHighestRev', () => {
|
|
229
|
+
it('should return null if no docs', () => {
|
|
230
|
+
expect(keepDocWitHighestRev([])).toBeNull()
|
|
231
|
+
expect(keepDocWitHighestRev(undefined)).toBeNull()
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
it('should return the single document when only one is provided', () => {
|
|
235
|
+
const doc = { _rev: '1-a', name: 'Single Doc' }
|
|
236
|
+
const docs = [doc]
|
|
237
|
+
expect(keepDocWitHighestRev(docs)).toBe(doc)
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
it('should return the document with the highest revision prefix', () => {
|
|
241
|
+
const docs = [
|
|
242
|
+
{ _rev: '1-a', name: 'Doc 1' },
|
|
243
|
+
{ _rev: '3-c', name: 'Doc 3' },
|
|
244
|
+
{ _rev: '2-b', name: 'Doc 2' }
|
|
245
|
+
]
|
|
246
|
+
expect(keepDocWitHighestRev(docs)).toEqual(docs[1])
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
it('should work correctly even if the documents are unsorted', () => {
|
|
250
|
+
const docs = [
|
|
251
|
+
{ _rev: '5-zzz', name: 'Doc 5' },
|
|
252
|
+
{ _rev: '2-aaa', name: 'Doc 2' },
|
|
253
|
+
{ _rev: '10-xxx', name: 'Doc 10' },
|
|
254
|
+
{ _rev: '7-bbb', name: 'Doc 7' }
|
|
255
|
+
]
|
|
256
|
+
expect(keepDocWitHighestRev(docs)).toEqual(docs[2])
|
|
257
|
+
})
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
describe('parseResults', () => {
|
|
261
|
+
const client = {}
|
|
262
|
+
const doctype = 'testdoctype'
|
|
263
|
+
|
|
264
|
+
it('should parse results correctly for multiple documents', () => {
|
|
265
|
+
const result = {
|
|
266
|
+
rows: {
|
|
267
|
+
length: 2,
|
|
268
|
+
item: jest.fn().mockImplementation(i => ({
|
|
269
|
+
data: JSON.stringify({ name: `doc${i}` }),
|
|
270
|
+
doc_id: `id${i}`,
|
|
271
|
+
rev: `rev${i}`
|
|
272
|
+
}))
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const parsed = parseResults(client, result, doctype)
|
|
277
|
+
|
|
278
|
+
expect(parsed.data.length).toBe(2)
|
|
279
|
+
expect(parsed.meta.count).toBe(2)
|
|
280
|
+
expect(parsed.skip).toBe(0)
|
|
281
|
+
expect(parsed.next).toBe(false)
|
|
282
|
+
|
|
283
|
+
expect(parsed.data[0]).toEqual({
|
|
284
|
+
_id: 'id0',
|
|
285
|
+
id: 'id0',
|
|
286
|
+
_rev: 'rev0',
|
|
287
|
+
_type: doctype,
|
|
288
|
+
name: 'doc0'
|
|
289
|
+
})
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
it('should handle isSingleDoc correctly with multiple docs', () => {
|
|
293
|
+
const result = {
|
|
294
|
+
rows: {
|
|
295
|
+
length: 2,
|
|
296
|
+
item: jest.fn().mockImplementation(i => ({
|
|
297
|
+
data: JSON.stringify({ name: `doc${i}` }),
|
|
298
|
+
doc_id: `id${i}`,
|
|
299
|
+
rev: `rev${i}`
|
|
300
|
+
}))
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const parsed = parseResults(client, result, doctype, { isSingleDoc: true })
|
|
305
|
+
|
|
306
|
+
expect(parsed.data).toEqual({
|
|
307
|
+
_id: 'id0',
|
|
308
|
+
id: 'id0',
|
|
309
|
+
_rev: 'rev0',
|
|
310
|
+
_type: doctype,
|
|
311
|
+
name: 'doc0'
|
|
312
|
+
})
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
it('should return empty data array when no rows are present', () => {
|
|
316
|
+
const result = { rows: { length: 0, item: jest.fn() } }
|
|
317
|
+
|
|
318
|
+
const parsed = parseResults(client, result, doctype)
|
|
319
|
+
|
|
320
|
+
expect(parsed).toEqual({ data: [] })
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
it('should set next=true when limit matches parsed length', () => {
|
|
324
|
+
const result = {
|
|
325
|
+
rows: {
|
|
326
|
+
length: 3,
|
|
327
|
+
item: jest.fn().mockImplementation(i => ({
|
|
328
|
+
data: JSON.stringify({ name: `doc${i}` }),
|
|
329
|
+
doc_id: `id${i}`,
|
|
330
|
+
rev: `rev${i}`
|
|
331
|
+
}))
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const parsed = parseResults(client, result, doctype, { limit: 3 })
|
|
336
|
+
|
|
337
|
+
expect(parsed.next).toBe(true)
|
|
338
|
+
expect(parsed.data.length).toBe(3)
|
|
339
|
+
})
|
|
340
|
+
|
|
341
|
+
it('should handle single document correctly', () => {
|
|
342
|
+
const result = {
|
|
343
|
+
rows: {
|
|
344
|
+
length: 1,
|
|
345
|
+
item: jest.fn().mockReturnValue({
|
|
346
|
+
data: JSON.stringify({ name: 'single_doc' }),
|
|
347
|
+
doc_id: 'single_id',
|
|
348
|
+
rev: 'single_rev'
|
|
349
|
+
})
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const parsed = parseResults(client, result, doctype, { isSingleDoc: true })
|
|
354
|
+
|
|
355
|
+
expect(parsed.data).toEqual({
|
|
356
|
+
_id: 'single_id',
|
|
357
|
+
id: 'single_id',
|
|
358
|
+
_rev: 'single_rev',
|
|
359
|
+
_type: doctype,
|
|
360
|
+
name: 'single_doc'
|
|
361
|
+
})
|
|
362
|
+
})
|
|
363
|
+
})
|
|
@@ -0,0 +1,319 @@
|
|
|
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 _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
13
|
+
|
|
14
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
15
|
+
|
|
16
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
17
|
+
|
|
18
|
+
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
|
19
|
+
|
|
20
|
+
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
|
|
21
|
+
|
|
22
|
+
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
|
|
23
|
+
|
|
24
|
+
var _dbInterface = _interopRequireDefault(require("../dbInterface"));
|
|
25
|
+
|
|
26
|
+
var _opSqlite = require("@op-engineering/op-sqlite");
|
|
27
|
+
|
|
28
|
+
var _sql = require("./sql");
|
|
29
|
+
|
|
30
|
+
var _mango = require("../../mango");
|
|
31
|
+
|
|
32
|
+
var _errors = require("../../errors");
|
|
33
|
+
|
|
34
|
+
var _logger = _interopRequireDefault(require("../../logger"));
|
|
35
|
+
|
|
36
|
+
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); }; }
|
|
37
|
+
|
|
38
|
+
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; } }
|
|
39
|
+
|
|
40
|
+
var SQLiteQueryEngine = /*#__PURE__*/function (_DatabaseQueryEngine) {
|
|
41
|
+
(0, _inherits2.default)(SQLiteQueryEngine, _DatabaseQueryEngine);
|
|
42
|
+
|
|
43
|
+
var _super = _createSuper(SQLiteQueryEngine);
|
|
44
|
+
|
|
45
|
+
function SQLiteQueryEngine(pouchManager, doctype) {
|
|
46
|
+
var _this;
|
|
47
|
+
|
|
48
|
+
(0, _classCallCheck2.default)(this, SQLiteQueryEngine);
|
|
49
|
+
_this = _super.call(this);
|
|
50
|
+
_this.db = null;
|
|
51
|
+
_this.client = pouchManager === null || pouchManager === void 0 ? void 0 : pouchManager.client;
|
|
52
|
+
_this.doctype = doctype;
|
|
53
|
+
return _this;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
(0, _createClass2.default)(SQLiteQueryEngine, [{
|
|
57
|
+
key: "openDB",
|
|
58
|
+
value: function openDB(dbName) {
|
|
59
|
+
var fileDbName = "".concat(dbName, ".sqlite");
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
this.db = (0, _opSqlite.open)({
|
|
63
|
+
name: fileDbName
|
|
64
|
+
}); // Create index at db opening if needed
|
|
65
|
+
|
|
66
|
+
var docIdIndexSql = (0, _sql.makeSQLCreateDocIDIndex)();
|
|
67
|
+
var deletedIndexSql = (0, _sql.makeSQLCreateDeletedIndex)();
|
|
68
|
+
(0, _sql.executeSQL)(this.db, docIdIndexSql);
|
|
69
|
+
(0, _sql.executeSQL)(this.db, deletedIndexSql);
|
|
70
|
+
} catch (err) {
|
|
71
|
+
_logger.default.error(err);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return this.db;
|
|
75
|
+
}
|
|
76
|
+
}, {
|
|
77
|
+
key: "allDocs",
|
|
78
|
+
value: function () {
|
|
79
|
+
var _allDocs = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
|
|
80
|
+
var _ref,
|
|
81
|
+
_ref$limit,
|
|
82
|
+
limit,
|
|
83
|
+
_ref$skip,
|
|
84
|
+
skip,
|
|
85
|
+
sql,
|
|
86
|
+
result,
|
|
87
|
+
docs,
|
|
88
|
+
_args = arguments;
|
|
89
|
+
|
|
90
|
+
return _regenerator.default.wrap(function _callee$(_context) {
|
|
91
|
+
while (1) {
|
|
92
|
+
switch (_context.prev = _context.next) {
|
|
93
|
+
case 0:
|
|
94
|
+
_ref = _args.length > 0 && _args[0] !== undefined ? _args[0] : {}, _ref$limit = _ref.limit, limit = _ref$limit === void 0 ? null : _ref$limit, _ref$skip = _ref.skip, skip = _ref$skip === void 0 ? 0 : _ref$skip;
|
|
95
|
+
_context.prev = 1;
|
|
96
|
+
sql = (0, _sql.makeSQLQueryAll)({
|
|
97
|
+
limit: limit,
|
|
98
|
+
skip: skip
|
|
99
|
+
});
|
|
100
|
+
_context.next = 5;
|
|
101
|
+
return (0, _sql.executeSQL)(this.db, sql);
|
|
102
|
+
|
|
103
|
+
case 5:
|
|
104
|
+
result = _context.sent;
|
|
105
|
+
docs = (0, _sql.parseResults)(this.client, result, this.doctype, {
|
|
106
|
+
limit: limit,
|
|
107
|
+
skip: skip
|
|
108
|
+
});
|
|
109
|
+
return _context.abrupt("return", docs);
|
|
110
|
+
|
|
111
|
+
case 10:
|
|
112
|
+
_context.prev = 10;
|
|
113
|
+
_context.t0 = _context["catch"](1);
|
|
114
|
+
|
|
115
|
+
_logger.default.error(_context.t0);
|
|
116
|
+
|
|
117
|
+
return _context.abrupt("return", null);
|
|
118
|
+
|
|
119
|
+
case 14:
|
|
120
|
+
case "end":
|
|
121
|
+
return _context.stop();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}, _callee, this, [[1, 10]]);
|
|
125
|
+
}));
|
|
126
|
+
|
|
127
|
+
function allDocs() {
|
|
128
|
+
return _allDocs.apply(this, arguments);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return allDocs;
|
|
132
|
+
}()
|
|
133
|
+
}, {
|
|
134
|
+
key: "getById",
|
|
135
|
+
value: function () {
|
|
136
|
+
var _getById = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(id) {
|
|
137
|
+
var sql, result, doc;
|
|
138
|
+
return _regenerator.default.wrap(function _callee2$(_context2) {
|
|
139
|
+
while (1) {
|
|
140
|
+
switch (_context2.prev = _context2.next) {
|
|
141
|
+
case 0:
|
|
142
|
+
_context2.prev = 0;
|
|
143
|
+
sql = (0, _sql.makeSQLQueryForId)(id);
|
|
144
|
+
_context2.next = 4;
|
|
145
|
+
return (0, _sql.executeSQL)(this.db, sql);
|
|
146
|
+
|
|
147
|
+
case 4:
|
|
148
|
+
result = _context2.sent;
|
|
149
|
+
doc = (0, _sql.parseResults)(this.client, result, this.doctype, {
|
|
150
|
+
isSingleDoc: true
|
|
151
|
+
});
|
|
152
|
+
return _context2.abrupt("return", doc);
|
|
153
|
+
|
|
154
|
+
case 9:
|
|
155
|
+
_context2.prev = 9;
|
|
156
|
+
_context2.t0 = _context2["catch"](0);
|
|
157
|
+
|
|
158
|
+
_logger.default.error(_context2.t0);
|
|
159
|
+
|
|
160
|
+
return _context2.abrupt("return", null);
|
|
161
|
+
|
|
162
|
+
case 13:
|
|
163
|
+
case "end":
|
|
164
|
+
return _context2.stop();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}, _callee2, this, [[0, 9]]);
|
|
168
|
+
}));
|
|
169
|
+
|
|
170
|
+
function getById(_x) {
|
|
171
|
+
return _getById.apply(this, arguments);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return getById;
|
|
175
|
+
}()
|
|
176
|
+
}, {
|
|
177
|
+
key: "getByIds",
|
|
178
|
+
value: function () {
|
|
179
|
+
var _getByIds = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(ids) {
|
|
180
|
+
var sql, result, docs;
|
|
181
|
+
return _regenerator.default.wrap(function _callee3$(_context3) {
|
|
182
|
+
while (1) {
|
|
183
|
+
switch (_context3.prev = _context3.next) {
|
|
184
|
+
case 0:
|
|
185
|
+
_context3.prev = 0;
|
|
186
|
+
sql = (0, _sql.makeSQLQueryForIds)(ids);
|
|
187
|
+
_context3.next = 4;
|
|
188
|
+
return (0, _sql.executeSQL)(this.db, sql);
|
|
189
|
+
|
|
190
|
+
case 4:
|
|
191
|
+
result = _context3.sent;
|
|
192
|
+
docs = (0, _sql.parseResults)(this.client, result, this.doctype);
|
|
193
|
+
return _context3.abrupt("return", docs);
|
|
194
|
+
|
|
195
|
+
case 9:
|
|
196
|
+
_context3.prev = 9;
|
|
197
|
+
_context3.t0 = _context3["catch"](0);
|
|
198
|
+
|
|
199
|
+
_logger.default.error(_context3.t0);
|
|
200
|
+
|
|
201
|
+
return _context3.abrupt("return", null);
|
|
202
|
+
|
|
203
|
+
case 13:
|
|
204
|
+
case "end":
|
|
205
|
+
return _context3.stop();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}, _callee3, this, [[0, 9]]);
|
|
209
|
+
}));
|
|
210
|
+
|
|
211
|
+
function getByIds(_x2) {
|
|
212
|
+
return _getByIds.apply(this, arguments);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return getByIds;
|
|
216
|
+
}()
|
|
217
|
+
}, {
|
|
218
|
+
key: "find",
|
|
219
|
+
value: function () {
|
|
220
|
+
var _find = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(options) {
|
|
221
|
+
var selector, sort, partialFilter, limit, recreateIndex, skip, indexedFields, indexName, sql, result, docs;
|
|
222
|
+
return _regenerator.default.wrap(function _callee4$(_context4) {
|
|
223
|
+
while (1) {
|
|
224
|
+
switch (_context4.prev = _context4.next) {
|
|
225
|
+
case 0:
|
|
226
|
+
selector = options.selector, sort = options.sort, partialFilter = options.partialFilter, limit = options.limit, recreateIndex = options.recreateIndex, skip = options.skip;
|
|
227
|
+
indexedFields = options.indexedFields;
|
|
228
|
+
indexedFields = (0, _mango.getIndexFields)({
|
|
229
|
+
indexedFields: indexedFields,
|
|
230
|
+
selector: selector,
|
|
231
|
+
sort: sort,
|
|
232
|
+
partialFilter: partialFilter
|
|
233
|
+
});
|
|
234
|
+
indexName = (0, _mango.getIndexName)({
|
|
235
|
+
selector: selector,
|
|
236
|
+
sort: sort,
|
|
237
|
+
partialFilter: partialFilter,
|
|
238
|
+
indexedFields: indexedFields
|
|
239
|
+
});
|
|
240
|
+
sql = (0, _sql.makeSQLQueryFromMango)({
|
|
241
|
+
selector: selector,
|
|
242
|
+
sort: sort,
|
|
243
|
+
indexName: indexName,
|
|
244
|
+
limit: limit,
|
|
245
|
+
skip: skip
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
if (!recreateIndex) {
|
|
249
|
+
_context4.next = 8;
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
_context4.next = 8;
|
|
254
|
+
return (0, _sql.deleteIndex)(this.db, indexName);
|
|
255
|
+
|
|
256
|
+
case 8:
|
|
257
|
+
_context4.prev = 8;
|
|
258
|
+
_context4.next = 11;
|
|
259
|
+
return (0, _sql.executeSQL)(this.db, sql);
|
|
260
|
+
|
|
261
|
+
case 11:
|
|
262
|
+
result = _context4.sent;
|
|
263
|
+
_context4.next = 26;
|
|
264
|
+
break;
|
|
265
|
+
|
|
266
|
+
case 14:
|
|
267
|
+
_context4.prev = 14;
|
|
268
|
+
_context4.t0 = _context4["catch"](8);
|
|
269
|
+
|
|
270
|
+
if (!(0, _errors.isMissingSQLiteIndexError)(_context4.t0)) {
|
|
271
|
+
_context4.next = 24;
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
_context4.next = 19;
|
|
276
|
+
return (0, _sql.createMangoIndex)(this.db, indexName, indexedFields, {
|
|
277
|
+
partialFilter: partialFilter
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
case 19:
|
|
281
|
+
_context4.next = 21;
|
|
282
|
+
return (0, _sql.executeSQL)(this.db, sql);
|
|
283
|
+
|
|
284
|
+
case 21:
|
|
285
|
+
result = _context4.sent;
|
|
286
|
+
_context4.next = 26;
|
|
287
|
+
break;
|
|
288
|
+
|
|
289
|
+
case 24:
|
|
290
|
+
_logger.default.error(_context4.t0);
|
|
291
|
+
|
|
292
|
+
return _context4.abrupt("return", null);
|
|
293
|
+
|
|
294
|
+
case 26:
|
|
295
|
+
docs = (0, _sql.parseResults)(this.client, result, this.doctype, {
|
|
296
|
+
skip: skip,
|
|
297
|
+
limit: limit
|
|
298
|
+
});
|
|
299
|
+
return _context4.abrupt("return", docs);
|
|
300
|
+
|
|
301
|
+
case 28:
|
|
302
|
+
case "end":
|
|
303
|
+
return _context4.stop();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}, _callee4, this, [[8, 14]]);
|
|
307
|
+
}));
|
|
308
|
+
|
|
309
|
+
function find(_x3) {
|
|
310
|
+
return _find.apply(this, arguments);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return find;
|
|
314
|
+
}()
|
|
315
|
+
}]);
|
|
316
|
+
return SQLiteQueryEngine;
|
|
317
|
+
}(_dbInterface.default);
|
|
318
|
+
|
|
319
|
+
exports.default = SQLiteQueryEngine;
|