@routier/pouchdb-plugin 0.2.0 → 0.4.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.
@@ -1,723 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.PouchDbPlugin = void 0;
7
- const pouchdb_1 = __importDefault(require("pouchdb"));
8
- const PouchDbTranslator_1 = require("./PouchDbTranslator");
9
- const pipeline_1 = require("@routier/core/pipeline");
10
- const results_1 = require("@routier/core/results");
11
- const assertions_1 = require("@routier/core/assertions");
12
- const expressions_1 = require("@routier/core/expressions");
13
- const queue = new pipeline_1.SyncronousQueue();
14
- const INDEX_NAME = "routier_pdb_indexes";
15
- const cache = {};
16
- class PouchDbPlugin {
17
- _name;
18
- _options;
19
- constructor(name, options) {
20
- this._name = name;
21
- this._options = options;
22
- }
23
- sync(schemas) {
24
- (0, assertions_1.assertIsNotNull)(this._options?.sync, "Cannot start sync process without sync options. Provide sync options in PouchDbPlugin constructor");
25
- if (cache["sync"] == null) {
26
- cache["sync"] = {}; // placeholder
27
- const localDb = new pouchdb_1.default(this._name);
28
- const remoteDb = new pouchdb_1.default(this._options.sync.remoteDb);
29
- // Set up sync
30
- const sync = localDb.sync(remoteDb, {
31
- ...this._options.sync
32
- });
33
- if (this._options.sync.onChange) {
34
- sync.on('change', (e) => this._options.sync.onChange(schemas, e));
35
- }
36
- if (this._options.sync.onActive) {
37
- sync.on('active', () => this._options.sync.onActive(schemas));
38
- }
39
- if (this._options.sync.onComplete) {
40
- sync.on('complete', (e) => this._options.sync.onComplete(schemas, e));
41
- }
42
- if (this._options.sync.onDenied) {
43
- sync.on('denied', (e) => this._options.sync.onDenied(schemas, e));
44
- }
45
- if (this._options.sync.onError) {
46
- sync.on('error', (e) => this._options.sync.onError(schemas, e));
47
- }
48
- if (this._options.sync.onPaused) {
49
- sync.on('paused', (e) => this._options.sync.onPaused(schemas, e));
50
- }
51
- cache["sync"] = sync;
52
- }
53
- return cache["sync"];
54
- }
55
- _identityBulkOperations(identitySchemaIds, changes, result, done) {
56
- if (changes.aggregate.size === 0) {
57
- done(results_1.Result.success());
58
- return;
59
- }
60
- // Link each entity to a schema id below so we can easily look up the schema id for an entity
61
- const updates = [];
62
- const adds = [];
63
- const removes = [];
64
- for (const [schemaId, schemaChanges] of changes) {
65
- if (identitySchemaIds.includes(schemaId) === false || schemaChanges.hasItems === false) {
66
- continue;
67
- }
68
- updates.push(...schemaChanges.updates.map(x => ({ change: x, schemaId })));
69
- adds.push(...schemaChanges.adds.map(x => ({ entity: x, schemaId })));
70
- removes.push(...schemaChanges.removes.map(x => ({ entity: x, schemaId })));
71
- }
72
- // Do adds separate. Updates/Removes already have an id that we can reference
73
- // Default can do the same as updates/removes below since it has an id
74
- // Check again now to see if we have any identity schema changes
75
- if (updates.length === 0 && removes.length === 0 && adds.length === 0) {
76
- done(results_1.Result.success());
77
- return;
78
- }
79
- this._doWork((db, d) => {
80
- try {
81
- const updatedDocuments = [...updates].map(w => w.change.entity);
82
- const errors = [];
83
- db.bulkDocs([...removes.map(w => ({ _id: w.entity._id, _rev: w.entity._rev, _deleted: true })), ...updatedDocuments], null, (error, response) => {
84
- if (error) {
85
- d(results_1.Result.error(error));
86
- return;
87
- }
88
- for (let i = 0, length = response.length; i < length; i++) {
89
- const doc = response[i];
90
- if ("error" in doc && doc.error === true) {
91
- errors.push(doc); // send back the entire error
92
- continue;
93
- }
94
- if ("id" in doc && "ok" in doc) {
95
- const updatesIndex = updates.findIndex(x => x.change.entity._id === doc.id);
96
- if (updatesIndex !== -1) {
97
- // Optimistically assume that the update worked as expected
98
- const update = updates[updatesIndex];
99
- const changesResult = result.resolve(update.schemaId);
100
- // Set the new rev
101
- update.change.entity._rev = doc.rev;
102
- changesResult.updates.push(update.change.entity);
103
- continue;
104
- }
105
- const removesIndex = removes.findIndex(x => x.entity._id === doc.id);
106
- if (removesIndex !== -1) {
107
- const remove = removes[removesIndex];
108
- const changesResult = result.resolve(remove.schemaId);
109
- changesResult.removes.push(remove.entity);
110
- }
111
- }
112
- }
113
- if (errors.length > 0) {
114
- d(results_1.Result.error(errors));
115
- return;
116
- }
117
- const pipeline = new pipeline_1.WorkPipeline();
118
- for (let i = 0, length = identitySchemaIds.length; i < length; i++) {
119
- const schemaId = identitySchemaIds[i];
120
- const schemaAdds = adds.filter(x => x.schemaId === schemaId).map(x => x.entity);
121
- // Skip schemas with no adds
122
- if (schemaAdds.length === 0) {
123
- continue;
124
- }
125
- pipeline.pipe((d) => {
126
- const schemaResult = result.resolve(schemaId);
127
- (0, assertions_1.assertIsNotNull)(schemaResult);
128
- (0, assertions_1.assertIsNotNull)(schemaAdds);
129
- db.bulkDocs([...schemaAdds], null, (error, response) => {
130
- if (error) {
131
- d(results_1.Result.error(error));
132
- return;
133
- }
134
- const ids = response.map(x => x.id);
135
- for (let i = 0, length = response.length; i < length; i++) {
136
- const doc = response[i];
137
- if ("error" in doc) {
138
- const reason = doc.reason ?? doc.error;
139
- d(results_1.Result.error(reason));
140
- return;
141
- }
142
- if ("id" in doc && "ok" in doc) {
143
- ids.push(doc.id);
144
- }
145
- else {
146
- d(results_1.Result.error(doc.error));
147
- return;
148
- }
149
- }
150
- this._bulkGetAdditions(ids, (bulkGetResponse) => {
151
- if (bulkGetResponse.ok !== results_1.Result.SUCCESS) {
152
- d(results_1.Result.error(bulkGetResponse));
153
- return;
154
- }
155
- for (let i = 0, length = bulkGetResponse.data.results.length; i < length; i++) {
156
- const docs = bulkGetResponse.data.results[i].docs;
157
- if (docs.length === 1) {
158
- const doc = docs[0];
159
- if ("ok" in doc) {
160
- schemaResult.adds.push(doc.ok);
161
- }
162
- }
163
- }
164
- d(results_1.Result.success());
165
- });
166
- });
167
- });
168
- }
169
- pipeline.filter((r) => {
170
- if (r.ok !== results_1.Result.SUCCESS) {
171
- d(r);
172
- return;
173
- }
174
- d(results_1.Result.success());
175
- });
176
- });
177
- }
178
- catch (e) {
179
- d(results_1.Result.error(e));
180
- }
181
- }, done);
182
- }
183
- _defaultBulkOperations(nonIdentitySchemaIds, changes, result, done) {
184
- if (changes.aggregate.size === 0) {
185
- done(results_1.Result.success());
186
- return;
187
- }
188
- // Link each entity to a schema id below so we can easily look up the schema id for an entity
189
- const updates = [];
190
- const adds = [];
191
- const removes = [];
192
- for (const [schemaId, schemaChanges] of changes) {
193
- if (nonIdentitySchemaIds.includes(schemaId) === false || schemaChanges.hasItems === false) {
194
- continue;
195
- }
196
- updates.push(...schemaChanges.updates.map(x => ({ change: x, schemaId })));
197
- adds.push(...schemaChanges.adds.map(x => ({ entity: x, schemaId })));
198
- removes.push(...schemaChanges.removes.map(x => ({ entity: x, schemaId })));
199
- }
200
- if (updates.length === 0 && removes.length === 0 && adds.length === 0) {
201
- done(results_1.Result.success());
202
- return;
203
- }
204
- this._doWork((db, d) => {
205
- try {
206
- db.bulkDocs([
207
- ...removes.map(x => ({ _id: x.entity._id, _rev: x.entity._rev, _deleted: true })),
208
- ...updates.map(x => x.change.entity)
209
- ], null, (error, response) => {
210
- if (error != null) {
211
- d(results_1.Result.error(error));
212
- return;
213
- }
214
- for (let i = 0, length = response.length; i < length; i++) {
215
- const doc = response[i];
216
- if ("error" in doc) {
217
- const reason = doc.reason ?? doc.error;
218
- d(results_1.Result.error(reason));
219
- return;
220
- }
221
- const removesIndex = removes.findIndex(x => x.entity._id === doc.id);
222
- if (removesIndex !== -1) {
223
- const { schemaId } = removes[removesIndex];
224
- const schemaResult = result.resolve(schemaId);
225
- (0, assertions_1.assertIsNotNull)(schemaResult);
226
- schemaResult.removes.push(doc);
227
- continue;
228
- }
229
- const updatesIndex = updates.findIndex(x => x.change.entity._id === doc.id);
230
- if (updatesIndex !== -1) {
231
- // Optimistically assume that the update worked as expected
232
- const update = updates[updatesIndex];
233
- const changesResult = result.resolve(update.schemaId);
234
- // Set the new rev
235
- update.change.entity._rev = doc.rev;
236
- changesResult.updates.push(update.change.entity);
237
- continue;
238
- }
239
- d(results_1.Result.error(new Error("Cannot classify resulting doc")));
240
- return;
241
- }
242
- const pipeline = new pipeline_1.WorkPipeline();
243
- for (let i = 0, length = nonIdentitySchemaIds.length; i < length; i++) {
244
- const schemaId = nonIdentitySchemaIds[i];
245
- const schemaAdds = adds.filter(x => x.schemaId === schemaId).map(x => x.entity);
246
- // Skip schemas with no adds
247
- if (schemaAdds.length === 0) {
248
- continue;
249
- }
250
- pipeline.pipe((d) => {
251
- const schemaResult = result.resolve(schemaId);
252
- (0, assertions_1.assertIsNotNull)(schemaResult);
253
- (0, assertions_1.assertIsNotNull)(schemaAdds);
254
- db.bulkDocs([...schemaAdds], null, (error, response) => {
255
- if (error) {
256
- d(results_1.Result.error(error));
257
- return;
258
- }
259
- const ids = response.map(x => x.id);
260
- for (let i = 0, length = response.length; i < length; i++) {
261
- const doc = response[i];
262
- if ("error" in doc) {
263
- const reason = doc.reason ?? doc.error;
264
- d(results_1.Result.error(reason));
265
- return;
266
- }
267
- if ("id" in doc && "ok" in doc) {
268
- ids.push(doc.id);
269
- }
270
- else {
271
- d(results_1.Result.error(doc.error));
272
- return;
273
- }
274
- }
275
- this._bulkGetAdditions(ids, (bulkGetResponse) => {
276
- if (bulkGetResponse.ok !== results_1.Result.SUCCESS) {
277
- d(results_1.Result.error(bulkGetResponse));
278
- return;
279
- }
280
- for (let i = 0, length = bulkGetResponse.data.results.length; i < length; i++) {
281
- const docs = bulkGetResponse.data.results[i].docs;
282
- if (docs.length === 1) {
283
- const doc = docs[0];
284
- if ("ok" in doc) {
285
- schemaResult.adds.push(doc.ok);
286
- }
287
- }
288
- }
289
- d(results_1.Result.success());
290
- });
291
- });
292
- });
293
- }
294
- pipeline.filter((r) => {
295
- if (r.ok !== results_1.Result.SUCCESS) {
296
- d(r);
297
- return;
298
- }
299
- d(results_1.Result.success());
300
- });
301
- });
302
- }
303
- catch (e) {
304
- d(results_1.Result.error(e));
305
- }
306
- }, done);
307
- }
308
- _prepareProperties(...properties) {
309
- properties.sort((a, b) => a.name.localeCompare(b.name, undefined, { sensitivity: 'base' }));
310
- const paths = properties.map(w => w.getAssignmentPath({ parent: "doc" }));
311
- const viewName = `by_${paths.join("_")}`;
312
- return {
313
- properties,
314
- paths,
315
- viewName
316
- };
317
- }
318
- resolveIndexes(event, done) {
319
- const indexes = event.operation.schema.getIndexes();
320
- if (indexes.length === 0) {
321
- done(results_1.Result.success(null));
322
- return;
323
- }
324
- if (cache["indexes"]) {
325
- done(results_1.Result.success(cache["indexes"]));
326
- return; // Already built
327
- }
328
- const ddoc = {
329
- _id: `_design/${INDEX_NAME}`,
330
- views: {}
331
- };
332
- for (let i = 0, length = indexes.length; i < length; i++) {
333
- const index = indexes[i];
334
- const { viewName, paths } = this._prepareProperties(...index.properties);
335
- if (index.properties.length === 1) {
336
- const property = index.properties[0];
337
- const path = property.getAssignmentPath({ parent: "doc" });
338
- ddoc.views[viewName] = {
339
- map: `function(doc) {
340
- if (${path}) {
341
- emit(${path});
342
- }
343
- }`
344
- };
345
- continue;
346
- }
347
- ddoc.views[viewName] = {
348
- map: `function(doc) {
349
- if (${paths.join(" && ")}) {
350
- emit([${paths.join(",")}]);
351
- }
352
- }`
353
- };
354
- }
355
- this._doWork((db, d) => {
356
- // change this out in the future? or make a new index, this does not return the rev which makes us need to call get again below
357
- db.allDocs({
358
- startkey: '_design/',
359
- endkey: '_design0',
360
- include_docs: true
361
- }).then(response => {
362
- // if we have no indexes, let's create it
363
- if (response.rows.length === 0) {
364
- db.put(ddoc, {}, (e) => {
365
- if (e) {
366
- d(results_1.Result.error(e));
367
- return;
368
- }
369
- cache["indexes"] = ddoc;
370
- d(results_1.Result.success(ddoc));
371
- });
372
- return;
373
- }
374
- const matchingIndex = response.rows.find(x => x.id === `_design/${INDEX_NAME}`);
375
- // make sure we have the correct index created
376
- if (matchingIndex == null) {
377
- db.put(ddoc, {}, (e) => {
378
- if (e) {
379
- d(results_1.Result.error(e));
380
- return;
381
- }
382
- cache["indexes"] = ddoc;
383
- d(results_1.Result.success(ddoc));
384
- });
385
- return;
386
- }
387
- (0, assertions_1.assertIsNotNull)(ddoc.views);
388
- (0, assertions_1.assertIsNotNull)(matchingIndex.doc);
389
- const views = Object.keys(ddoc.views);
390
- // make sure all of the views are in the document, if not update it
391
- if (views.some(x => matchingIndex.doc.views[x] == null)) {
392
- // need to get the rev
393
- db.get(ddoc._id, {}, (e, result) => {
394
- if (e != null) {
395
- d(results_1.Result.error(e));
396
- return;
397
- }
398
- result.views = ddoc.views;
399
- db.put(result, {}, (e) => {
400
- if (e) {
401
- d(results_1.Result.error(e));
402
- return;
403
- }
404
- cache["indexes"] = ddoc;
405
- d(results_1.Result.success(ddoc));
406
- });
407
- });
408
- return;
409
- }
410
- d(results_1.Result.success(ddoc));
411
- }).catch(e => {
412
- d(results_1.Result.error(e));
413
- });
414
- }, done);
415
- }
416
- // id should be a string here because this is only run on identity inserts
417
- _bulkGetAdditions(ids, done) {
418
- if (ids.length === 0) {
419
- done(results_1.Result.success({ results: [] }));
420
- return;
421
- }
422
- this._doWork((db, d) => {
423
- db.bulkGet({
424
- docs: ids.map(w => ({ id: w }))
425
- }, (error, response) => {
426
- if (error) {
427
- d(results_1.Result.error(error));
428
- return;
429
- }
430
- d(results_1.Result.success(response));
431
- });
432
- }, done);
433
- }
434
- _validateSchemas(event) {
435
- for (const [, schema] of event.schemas) {
436
- if (schema.idProperties.length > 1) {
437
- throw new Error("PouchDB cannot have more than one key per document. Only '_id' is allowed to be the key");
438
- }
439
- }
440
- }
441
- _bulkPersist(event, done) {
442
- this._validateSchemas(event);
443
- const result = event.operation.toResult();
444
- const identitySchemaIds = [];
445
- const defaultSchemaIds = [];
446
- for (const [schemaId, schema] of event.schemas) {
447
- if (schema.hasIdentityKeys) {
448
- identitySchemaIds.push(schemaId);
449
- continue;
450
- }
451
- defaultSchemaIds.push(schemaId);
452
- }
453
- this._identityBulkOperations(identitySchemaIds, event.operation, result, (identityResult) => {
454
- if (identityResult.ok !== results_1.Result.SUCCESS) {
455
- done(results_1.PluginEventResult.error(event.id, identityResult.error));
456
- return;
457
- }
458
- this._defaultBulkOperations(defaultSchemaIds, event.operation, result, (defaultResult) => {
459
- if (defaultResult.ok !== results_1.Result.SUCCESS) {
460
- done(results_1.PluginEventResult.error(event.id, defaultResult.error));
461
- return;
462
- }
463
- done(results_1.PluginEventResult.success(event.id, result));
464
- });
465
- });
466
- }
467
- _doWork(work, done, shouldClose = false) {
468
- const { sync, queryType, ...rest } = this._options ?? {};
469
- const db = new pouchdb_1.default(this._name, rest);
470
- work(db, (result) => {
471
- if (shouldClose) {
472
- db.close(() => done(result));
473
- }
474
- done(result);
475
- });
476
- }
477
- destroy(_event, done) {
478
- // this needs to be queued too
479
- this._doWork((w, d) => {
480
- w.destroy(null, (e) => {
481
- if (e) {
482
- d(results_1.Result.error(e));
483
- return;
484
- }
485
- d(results_1.Result.success());
486
- });
487
- }, done, false);
488
- }
489
- bulkPersist(event, done) {
490
- const unitOfWork = (d) => this._bulkPersist(event, (r) => {
491
- d();
492
- done(r);
493
- });
494
- queue.enqueue(unitOfWork.bind(this));
495
- }
496
- query(event, done) {
497
- const unitOfWork = (d) => this._query(event, (r) => {
498
- d();
499
- done(r);
500
- });
501
- queue.enqueue(unitOfWork.bind(this));
502
- }
503
- onGetIndex(_, __, done) {
504
- done(null);
505
- }
506
- extractEqualityValueForProperty(expression, prop) {
507
- // Recursively search for a comparator expression that matches the property
508
- if (expression.type === "comparator" && expression.comparator === "equals") {
509
- // Check left side is property, right side is value
510
- if (expression.left && expression.left.type === "property" && expression.left.property.name === prop.name && expression.right && expression.right.type === "value") {
511
- return expression.right.value;
512
- }
513
- // Or right side is property, left side is value
514
- if (expression.right && expression.right.type === "property" && expression.right.property.name === prop.name && expression.left && expression.left.type === "value") {
515
- return expression.left.value;
516
- }
517
- }
518
- // Recursively check left and right
519
- if (expression.left) {
520
- const left = this.extractEqualityValueForProperty(expression.left, prop);
521
- if (left !== undefined)
522
- return left;
523
- }
524
- if (expression.right) {
525
- const right = this.extractEqualityValueForProperty(expression.right, prop);
526
- if (right !== undefined)
527
- return right;
528
- }
529
- return undefined;
530
- }
531
- _findMatchingIndex(ddoc, expression) {
532
- const queryProperties = (0, expressions_1.getProperties)(expression);
533
- const { viewName } = this._prepareProperties(...queryProperties);
534
- if (ddoc.views == null) {
535
- return null;
536
- }
537
- const index = ddoc.views[viewName];
538
- if (index == null) {
539
- return null;
540
- }
541
- const properties = [];
542
- for (const property of queryProperties) {
543
- const value = this.extractEqualityValueForProperty(expression, property);
544
- if (value === undefined) {
545
- return null;
546
- }
547
- properties.push({
548
- property,
549
- value
550
- });
551
- }
552
- return {
553
- viewName,
554
- properties
555
- };
556
- }
557
- _queryIndex(event, matchingIndex, done) {
558
- const translator = new PouchDbTranslator_1.PouchDbTranslator(event.operation);
559
- this._doWork((w, d) => {
560
- const options = {
561
- include_docs: true,
562
- };
563
- const values = matchingIndex.properties.map(x => x.value);
564
- if (values.some(x => x == null) == false) {
565
- if (values.length === 1) {
566
- options.key = values[0];
567
- }
568
- else {
569
- options.keys = values;
570
- }
571
- }
572
- w.query(`${INDEX_NAME}/${matchingIndex.viewName}`, options, (error, response) => {
573
- if (error != null) {
574
- d(results_1.Result.error(error));
575
- return;
576
- }
577
- try {
578
- const translated = translator.translate(response.rows.map(w => w.doc));
579
- d(results_1.Result.success(translated));
580
- }
581
- catch (e) {
582
- d(results_1.Result.error(e));
583
- }
584
- });
585
- }, done);
586
- }
587
- _queryNoIndex(event, done) {
588
- const filters = event.operation.options.get("filter");
589
- if (filters.length === 0) {
590
- this._queryWithNoFilters(event, done);
591
- return;
592
- }
593
- this._queryWithFilters(event, done);
594
- }
595
- _queryWithNoFilters(event, done) {
596
- const translator = new PouchDbTranslator_1.PouchDbTranslator(event.operation);
597
- this._doWork((w, d) => {
598
- w.allDocs({
599
- include_docs: true
600
- }).then(response => {
601
- try {
602
- const data = response.rows.map(w => w.doc);
603
- const translated = translator.translate(data);
604
- d(results_1.Result.success(translated));
605
- }
606
- catch (e) {
607
- d(results_1.Result.error(e));
608
- }
609
- }).catch(error => {
610
- d(results_1.Result.error(error));
611
- });
612
- }, done);
613
- }
614
- _queryWithFilters(event, done) {
615
- const translator = new PouchDbTranslator_1.PouchDbTranslator(event.operation);
616
- this._doWork((w, d) => {
617
- if (this._name.startsWith("http")) {
618
- // Cannot query remote databases using translator.matches, fallback to alldocs
619
- // Does not throw, will fail on the remote server
620
- w.allDocs({
621
- include_docs: true
622
- }).then(response => {
623
- const data = response.rows.map(w => w.doc);
624
- try {
625
- const translated = translator.translate(data);
626
- d(results_1.Result.success(translated));
627
- }
628
- catch (e) {
629
- d(results_1.Result.error(e));
630
- }
631
- }).catch(error => {
632
- d(results_1.Result.error(error));
633
- });
634
- return;
635
- }
636
- w.query((doc, emit) => {
637
- if (typeof doc === "object" && "_id" in doc && translator.matches(doc)) {
638
- emit(doc._id, doc);
639
- }
640
- }, (error, response) => {
641
- if (error != null) {
642
- d(results_1.Result.error(error));
643
- return;
644
- }
645
- const data = response.rows.map(w => w.value);
646
- try {
647
- const translated = translator.translate(data);
648
- d(results_1.Result.success(translated));
649
- }
650
- catch (e) {
651
- d(results_1.Result.error(e));
652
- }
653
- });
654
- }, done);
655
- }
656
- _query(event, done) {
657
- this.resolveIndexes(event, (r) => {
658
- if (r.ok !== results_1.Result.SUCCESS) {
659
- done(results_1.PluginEventResult.error(event.id, r.error));
660
- return;
661
- }
662
- const ddoc = r.data;
663
- if (ddoc == null) {
664
- this._queryNoIndex(event, done);
665
- return;
666
- }
667
- const mapMatchingIndex = this.findMatchingIndexForMap(ddoc, event);
668
- if (mapMatchingIndex != null) {
669
- this._queryIndex(event, mapMatchingIndex, done);
670
- return;
671
- }
672
- const expression = this._getExpressionFromQuery(event);
673
- if (expressions_1.Expression.isEmpty(expression)) {
674
- this._queryNoIndex(event, done);
675
- return;
676
- }
677
- const matchingIndex = this._findMatchingIndex(ddoc, expression);
678
- if (matchingIndex == null) {
679
- this._queryNoIndex(event, done);
680
- return;
681
- }
682
- this._queryIndex(event, matchingIndex, done);
683
- });
684
- }
685
- _getExpressionFromQuery(event) {
686
- const filters = event.operation.options.get("filter");
687
- const databaseFilters = filters.filter(x => x.option.target === "database");
688
- if (databaseFilters.length === 0) {
689
- return expressions_1.Expression.EMPTY;
690
- }
691
- const expressions = databaseFilters.map(x => x.option.value.expression);
692
- return (0, expressions_1.combineExpressions)(...expressions);
693
- }
694
- findMatchingIndexForMap(ddoc, event) {
695
- const map = event.operation.options.getLast("map");
696
- const filters = event.operation.options.get("filter");
697
- // we only want to look at the index if there are no filters
698
- if (filters.length > 0) {
699
- return null;
700
- }
701
- if (map == null || map.target !== "database") {
702
- return null;
703
- }
704
- if (map.value.fields.length !== 1 || map.value.fields[0].property == null) {
705
- return null;
706
- }
707
- const property = map.value.fields[0].property;
708
- const { viewName } = this._prepareProperties(property);
709
- if (ddoc.views == null) {
710
- return null;
711
- }
712
- const index = ddoc.views[viewName];
713
- if (index == null) {
714
- return null;
715
- }
716
- return {
717
- viewName,
718
- properties: [{ property, value: null }]
719
- };
720
- }
721
- }
722
- exports.PouchDbPlugin = PouchDbPlugin;
723
- //# sourceMappingURL=PouchDbPlugin.js.map