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