@routier/pouchdb-plugin 0.0.1-alpha.1 → 0.0.1-alpha.3
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/PouchDbPlugin.d.ts +68 -0
- package/{src/PouchDbPlugin.ts → dist/PouchDbPlugin.js} +185 -435
- package/dist/PouchDbPlugin.js.map +1 -0
- package/dist/PouchDbTranslator.d.ts +11 -0
- package/{src/PouchDbTranslator.ts → dist/PouchDbTranslator.js} +29 -47
- package/dist/PouchDbTranslator.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/package.json +9 -4
- package/CHANGELOG.md +0 -8
- package/rspack.config.mjs +0 -63
- package/src/index.ts +0 -1
- package/src/tests/comments.test.ts +0 -46
- package/src/tests/datastore/PouchDbDatastore.ts +0 -26
- package/src/tests/events.test.ts +0 -65
- package/src/tests/player.test.ts +0 -59
- package/src/tests/products.test.ts +0 -1520
- package/src/tests/schemas/blogPost.ts +0 -13
- package/src/tests/schemas/comments.ts +0 -12
- package/src/tests/schemas/event.ts +0 -14
- package/src/tests/schemas/inventoryItem.ts +0 -18
- package/src/tests/schemas/player.ts +0 -9
- package/src/tests/schemas/playerMatch.ts +0 -11
- package/src/tests/schemas/product.ts +0 -14
- package/src/tests/schemas/user.ts +0 -17
- package/src/tests/schemas/userProfile.ts +0 -69
- package/src/tests/userProfiles.test.ts +0 -63
- package/src/utilities.ts +0 -12
- package/tsconfig.json +0 -29
- package/vitest.config.ts +0 -11
|
@@ -1,457 +1,312 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
onError?: (error: any) => void;
|
|
21
|
-
onComplete?: (info: PouchDB.Replication.SyncResult<{}>) => void;
|
|
22
|
-
onPaused?: (info: PouchDB.Replication.SyncResult<{}>) => void;
|
|
23
|
-
onActive?: (info: PouchDB.Replication.SyncResult<{}>) => void;
|
|
24
|
-
onDenied?: (info: PouchDB.Replication.SyncResult<{}>) => void;
|
|
25
|
-
auth?: {
|
|
26
|
-
username: string;
|
|
27
|
-
password: string;
|
|
28
|
-
};
|
|
29
|
-
headers?: { [key: string]: string };
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export type PouchDbDesignDoc = {
|
|
34
|
-
_id: string;
|
|
35
|
-
language?: string;
|
|
36
|
-
views?: {
|
|
37
|
-
[viewName: string]: {
|
|
38
|
-
map: string | Function;
|
|
39
|
-
reduce?: string | Function;
|
|
40
|
-
};
|
|
41
|
-
};
|
|
42
|
-
filters?: {
|
|
43
|
-
[filterName: string]: string | Function;
|
|
44
|
-
};
|
|
45
|
-
validate_doc_update?: string | Function;
|
|
46
|
-
options?: Record<string, any>;
|
|
47
|
-
[key: string]: any; // allow other CouchDB features
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
type MatchingIndex = {
|
|
51
|
-
viewName: string,
|
|
52
|
-
properties: {
|
|
53
|
-
property: PropertyInfo<any>,
|
|
54
|
-
value: unknown
|
|
55
|
-
}[]
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export class PouchDbPlugin implements IDbPlugin {
|
|
59
|
-
|
|
60
|
-
private readonly _name: string;
|
|
61
|
-
private readonly _options?: PouchDBPluginOptions;
|
|
62
|
-
|
|
63
|
-
constructor(name: string, options?: PouchDBPluginOptions) {
|
|
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) {
|
|
64
20
|
this._name = name;
|
|
65
21
|
this._options = options;
|
|
66
22
|
}
|
|
67
|
-
|
|
68
|
-
private _tryStartSync(schemas: SchemaCollection) {
|
|
23
|
+
_tryStartSync(schemas) {
|
|
69
24
|
if (this._options?.sync != null) {
|
|
70
|
-
|
|
71
25
|
if (cache["sync"] == null) {
|
|
72
26
|
cache["sync"] = {}; // placeholder
|
|
73
|
-
const localDb = new
|
|
74
|
-
const remoteDb = new
|
|
75
|
-
|
|
27
|
+
const localDb = new pouchdb_1.default(this._name);
|
|
28
|
+
const remoteDb = new pouchdb_1.default(this._options.sync.remoteDb);
|
|
76
29
|
// Set up sync
|
|
77
30
|
const sync = localDb.sync(remoteDb, {
|
|
78
31
|
...this._options.sync
|
|
79
32
|
});
|
|
80
|
-
|
|
81
33
|
sync.on('change', (change) => this._options.sync.onChange(schemas, change));
|
|
82
|
-
|
|
83
34
|
cache["sync"] = sync;
|
|
84
35
|
}
|
|
85
36
|
}
|
|
86
37
|
}
|
|
87
|
-
|
|
88
|
-
private _identityBulkOperations(identitySchemaIds: SchemaId[], changes: BulkPersistChanges, result: BulkPersistResult, done: CallbackResult<never>): void {
|
|
89
|
-
|
|
38
|
+
_identityBulkOperations(identitySchemaIds, changes, result, done) {
|
|
90
39
|
if (changes.aggregate.size === 0) {
|
|
91
|
-
done(Result.success())
|
|
40
|
+
done(results_1.Result.success());
|
|
92
41
|
return;
|
|
93
42
|
}
|
|
94
|
-
|
|
95
43
|
// Link each entity to a schema id below so we can easily look up the schema id for an entity
|
|
96
|
-
const updates
|
|
97
|
-
const adds
|
|
98
|
-
const removes
|
|
99
|
-
|
|
44
|
+
const updates = [];
|
|
45
|
+
const adds = [];
|
|
46
|
+
const removes = [];
|
|
100
47
|
for (const [schemaId, schemaChanges] of changes) {
|
|
101
|
-
|
|
102
48
|
if (identitySchemaIds.includes(schemaId) === false || schemaChanges.hasItems === false) {
|
|
103
49
|
continue;
|
|
104
50
|
}
|
|
105
|
-
|
|
106
51
|
updates.push(...schemaChanges.updates.map(x => ({ change: x, schemaId })));
|
|
107
52
|
adds.push(...schemaChanges.adds.map(x => ({ entity: x, schemaId })));
|
|
108
53
|
removes.push(...schemaChanges.removes.map(x => ({ entity: x, schemaId })));
|
|
109
54
|
}
|
|
110
|
-
|
|
111
55
|
// Do adds separate. Updates/Removes already have an id that we can reference
|
|
112
56
|
// Default can do the same as updates/removes below since it has an id
|
|
113
57
|
// Check again now to see if we have any identity schema changes
|
|
114
58
|
if (updates.length === 0 && removes.length === 0 && adds.length === 0) {
|
|
115
|
-
done(Result.success())
|
|
59
|
+
done(results_1.Result.success());
|
|
116
60
|
return;
|
|
117
61
|
}
|
|
118
|
-
|
|
119
62
|
this._doWork((db, d) => {
|
|
120
63
|
try {
|
|
121
|
-
|
|
122
64
|
const updatedDocuments = [...updates].map(w => w.change.entity);
|
|
123
|
-
const errors
|
|
124
|
-
|
|
65
|
+
const errors = [];
|
|
125
66
|
db.bulkDocs([...removes.map(w => ({ _id: w.entity._id, _rev: w.entity._rev, _deleted: true })), ...updatedDocuments], null, (error, response) => {
|
|
126
|
-
|
|
127
67
|
if (error) {
|
|
128
|
-
d(Result.error(error));
|
|
68
|
+
d(results_1.Result.error(error));
|
|
129
69
|
return;
|
|
130
70
|
}
|
|
131
|
-
|
|
132
71
|
for (let i = 0, length = response.length; i < length; i++) {
|
|
133
72
|
const doc = response[i];
|
|
134
|
-
|
|
135
73
|
if ("error" in doc && doc.error === true) {
|
|
136
74
|
errors.push(doc); // send back the entire error
|
|
137
75
|
continue;
|
|
138
76
|
}
|
|
139
|
-
|
|
140
77
|
if ("id" in doc && "ok" in doc) {
|
|
141
78
|
const updatesIndex = updates.findIndex(x => x.change.entity._id === doc.id);
|
|
142
|
-
|
|
143
79
|
if (updatesIndex !== -1) {
|
|
144
80
|
// Optimistically assume that the update worked as expected
|
|
145
81
|
const update = updates[updatesIndex];
|
|
146
|
-
|
|
147
82
|
const changesResult = result.get(update.schemaId);
|
|
148
|
-
|
|
149
83
|
// Set the new rev
|
|
150
|
-
|
|
151
|
-
|
|
84
|
+
update.change.entity._rev = doc.rev;
|
|
152
85
|
changesResult.updates.push(update.change.entity);
|
|
153
86
|
continue;
|
|
154
87
|
}
|
|
155
|
-
|
|
156
88
|
const removesIndex = removes.findIndex(x => x.entity._id === doc.id);
|
|
157
|
-
|
|
158
89
|
if (removesIndex !== -1) {
|
|
159
|
-
|
|
160
90
|
const remove = removes[removesIndex];
|
|
161
|
-
|
|
162
91
|
const changesResult = result.get(remove.schemaId);
|
|
163
|
-
|
|
164
92
|
changesResult.removes.push(remove.entity);
|
|
165
93
|
}
|
|
166
94
|
}
|
|
167
95
|
}
|
|
168
|
-
|
|
169
96
|
if (errors.length > 0) {
|
|
170
|
-
d(Result.error(errors));
|
|
97
|
+
d(results_1.Result.error(errors));
|
|
171
98
|
return;
|
|
172
99
|
}
|
|
173
|
-
|
|
174
|
-
const pipeline = new WorkPipeline();
|
|
175
|
-
|
|
100
|
+
const pipeline = new pipeline_1.WorkPipeline();
|
|
176
101
|
for (let i = 0, length = identitySchemaIds.length; i < length; i++) {
|
|
177
102
|
const schemaId = identitySchemaIds[i];
|
|
178
103
|
pipeline.pipe((d) => {
|
|
179
104
|
const schemaAdds = adds.filter(x => x.schemaId === schemaId).map(x => x.entity);
|
|
180
105
|
const schemaResult = result.get(schemaId);
|
|
181
|
-
|
|
182
|
-
assertIsNotNull(
|
|
183
|
-
assertIsNotNull(schemaAdds);
|
|
184
|
-
|
|
106
|
+
(0, assertions_1.assertIsNotNull)(schemaResult);
|
|
107
|
+
(0, assertions_1.assertIsNotNull)(schemaAdds);
|
|
185
108
|
db.bulkDocs([...schemaAdds], null, (error, response) => {
|
|
186
|
-
|
|
187
109
|
if (error) {
|
|
188
|
-
d(Result.error(error));
|
|
110
|
+
d(results_1.Result.error(error));
|
|
189
111
|
return;
|
|
190
112
|
}
|
|
191
|
-
|
|
192
113
|
const ids = response.map(x => x.id);
|
|
193
|
-
|
|
194
114
|
for (let i = 0, length = response.length; i < length; i++) {
|
|
195
|
-
|
|
196
115
|
const doc = response[i];
|
|
197
|
-
|
|
198
116
|
if ("error" in doc) {
|
|
199
|
-
|
|
200
117
|
const reason = doc.reason ?? doc.error;
|
|
201
|
-
|
|
202
|
-
d(Result.error(reason));
|
|
118
|
+
d(results_1.Result.error(reason));
|
|
203
119
|
return;
|
|
204
120
|
}
|
|
205
|
-
|
|
206
121
|
if ("id" in doc && "ok" in doc) {
|
|
207
122
|
ids.push(doc.id);
|
|
208
|
-
}
|
|
209
|
-
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
d(results_1.Result.error(doc.error));
|
|
210
126
|
return;
|
|
211
127
|
}
|
|
212
128
|
}
|
|
213
|
-
|
|
214
129
|
this._bulkGetAdditions(ids, (bulkGetResponse) => {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
d(Result.error(bulkGetResponse));
|
|
130
|
+
if (bulkGetResponse.ok !== results_1.Result.SUCCESS) {
|
|
131
|
+
d(results_1.Result.error(bulkGetResponse));
|
|
218
132
|
return;
|
|
219
133
|
}
|
|
220
|
-
|
|
221
134
|
for (let i = 0, length = bulkGetResponse.data.results.length; i < length; i++) {
|
|
222
135
|
const docs = bulkGetResponse.data.results[i].docs;
|
|
223
|
-
|
|
224
136
|
if (docs.length === 1) {
|
|
225
137
|
const doc = docs[0];
|
|
226
|
-
|
|
227
138
|
if ("ok" in doc) {
|
|
228
|
-
schemaResult.adds.push(doc.ok
|
|
139
|
+
schemaResult.adds.push(doc.ok);
|
|
229
140
|
}
|
|
230
141
|
}
|
|
231
142
|
}
|
|
232
|
-
|
|
233
|
-
d(Result.success());
|
|
143
|
+
d(results_1.Result.success());
|
|
234
144
|
});
|
|
235
145
|
});
|
|
236
|
-
|
|
237
146
|
});
|
|
238
147
|
}
|
|
239
|
-
|
|
240
148
|
pipeline.filter((r) => {
|
|
241
|
-
if (r.ok !== Result.SUCCESS) {
|
|
149
|
+
if (r.ok !== results_1.Result.SUCCESS) {
|
|
242
150
|
d(r);
|
|
243
151
|
return;
|
|
244
152
|
}
|
|
245
|
-
|
|
246
|
-
d(Result.success());
|
|
153
|
+
d(results_1.Result.success());
|
|
247
154
|
});
|
|
248
155
|
});
|
|
249
|
-
}
|
|
250
|
-
|
|
156
|
+
}
|
|
157
|
+
catch (e) {
|
|
158
|
+
d(results_1.Result.error(e));
|
|
251
159
|
}
|
|
252
160
|
}, done);
|
|
253
161
|
}
|
|
254
|
-
|
|
255
|
-
private _defaultBulkOperations(nonIdentitySchemaIds: SchemaId[], changes: BulkPersistChanges, result: BulkPersistResult, done: CallbackResult<never>): void {
|
|
256
|
-
|
|
162
|
+
_defaultBulkOperations(nonIdentitySchemaIds, changes, result, done) {
|
|
257
163
|
if (changes.aggregate.size === 0) {
|
|
258
|
-
done(Result.success())
|
|
164
|
+
done(results_1.Result.success());
|
|
259
165
|
return;
|
|
260
166
|
}
|
|
261
|
-
|
|
262
167
|
// Link each entity to a schema id below so we can easily look up the schema id for an entity
|
|
263
|
-
const updates
|
|
264
|
-
const adds
|
|
265
|
-
const removes
|
|
266
|
-
|
|
168
|
+
const updates = [];
|
|
169
|
+
const adds = [];
|
|
170
|
+
const removes = [];
|
|
267
171
|
for (const [schemaId, schemaChanges] of changes) {
|
|
268
|
-
|
|
269
172
|
if (nonIdentitySchemaIds.includes(schemaId) === false || schemaChanges.hasItems === false) {
|
|
270
173
|
continue;
|
|
271
174
|
}
|
|
272
|
-
|
|
273
175
|
updates.push(...schemaChanges.updates.map(x => ({ change: x, schemaId })));
|
|
274
176
|
adds.push(...schemaChanges.adds.map(x => ({ entity: x, schemaId })));
|
|
275
177
|
removes.push(...schemaChanges.removes.map(x => ({ entity: x, schemaId })));
|
|
276
178
|
}
|
|
277
|
-
|
|
278
179
|
if (updates.length === 0 && removes.length === 0 && adds.length === 0) {
|
|
279
|
-
done(Result.success())
|
|
180
|
+
done(results_1.Result.success());
|
|
280
181
|
return;
|
|
281
182
|
}
|
|
282
|
-
|
|
283
183
|
this._doWork((db, d) => {
|
|
284
184
|
try {
|
|
285
|
-
|
|
286
185
|
db.bulkDocs([
|
|
287
186
|
...removes.map(x => ({ _id: x.entity._id, _rev: x.entity._rev, _deleted: true })),
|
|
288
187
|
...updates.map(x => x.change.entity)
|
|
289
188
|
], null, (error, response) => {
|
|
290
|
-
|
|
291
189
|
if (error != null) {
|
|
292
|
-
d(Result.error(error));
|
|
190
|
+
d(results_1.Result.error(error));
|
|
293
191
|
return;
|
|
294
192
|
}
|
|
295
|
-
|
|
296
193
|
for (let i = 0, length = response.length; i < length; i++) {
|
|
297
194
|
const doc = response[i];
|
|
298
|
-
|
|
299
195
|
if ("error" in doc) {
|
|
300
|
-
|
|
301
196
|
const reason = doc.reason ?? doc.error;
|
|
302
|
-
|
|
303
|
-
d(Result.error(reason));
|
|
197
|
+
d(results_1.Result.error(reason));
|
|
304
198
|
return;
|
|
305
199
|
}
|
|
306
|
-
|
|
307
200
|
const removesIndex = removes.findIndex(x => x.entity._id === doc.id);
|
|
308
|
-
|
|
309
201
|
if (removesIndex !== -1) {
|
|
310
|
-
const { schemaId } = removes[removesIndex]
|
|
202
|
+
const { schemaId } = removes[removesIndex];
|
|
311
203
|
const schemaResult = result.get(schemaId);
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
schemaResult.removes.push(doc as any);
|
|
204
|
+
(0, assertions_1.assertIsNotNull)(schemaResult);
|
|
205
|
+
schemaResult.removes.push(doc);
|
|
316
206
|
continue;
|
|
317
207
|
}
|
|
318
|
-
|
|
319
208
|
const updatesIndex = updates.findIndex(x => x.change.entity._id === doc.id);
|
|
320
|
-
|
|
321
209
|
if (updatesIndex !== -1) {
|
|
322
210
|
// Optimistically assume that the update worked as expected
|
|
323
211
|
const update = updates[updatesIndex];
|
|
324
|
-
|
|
325
212
|
const changesResult = result.get(update.schemaId);
|
|
326
|
-
|
|
327
213
|
// Set the new rev
|
|
328
|
-
|
|
329
|
-
|
|
214
|
+
update.change.entity._rev = doc.rev;
|
|
330
215
|
changesResult.updates.push(update.change.entity);
|
|
331
216
|
continue;
|
|
332
217
|
}
|
|
333
|
-
|
|
334
|
-
d(Result.error(new Error("Cannot classify resulting doc")));
|
|
218
|
+
d(results_1.Result.error(new Error("Cannot classify resulting doc")));
|
|
335
219
|
return;
|
|
336
220
|
}
|
|
337
|
-
|
|
338
|
-
const pipeline = new WorkPipeline();
|
|
339
|
-
|
|
221
|
+
const pipeline = new pipeline_1.WorkPipeline();
|
|
340
222
|
for (let i = 0, length = nonIdentitySchemaIds.length; i < length; i++) {
|
|
341
223
|
const schemaId = nonIdentitySchemaIds[i];
|
|
342
224
|
pipeline.pipe((d) => {
|
|
343
225
|
const schemaAdds = adds.filter(x => x.schemaId === schemaId).map(x => x.entity);
|
|
344
226
|
const schemaResult = result.get(schemaId);
|
|
345
|
-
|
|
346
|
-
assertIsNotNull(
|
|
347
|
-
assertIsNotNull(schemaAdds);
|
|
348
|
-
|
|
227
|
+
(0, assertions_1.assertIsNotNull)(schemaResult);
|
|
228
|
+
(0, assertions_1.assertIsNotNull)(schemaAdds);
|
|
349
229
|
db.bulkDocs([...schemaAdds], null, (error, response) => {
|
|
350
|
-
|
|
351
230
|
if (error) {
|
|
352
|
-
d(Result.error(error));
|
|
231
|
+
d(results_1.Result.error(error));
|
|
353
232
|
return;
|
|
354
233
|
}
|
|
355
|
-
|
|
356
234
|
const ids = response.map(x => x.id);
|
|
357
|
-
|
|
358
235
|
for (let i = 0, length = response.length; i < length; i++) {
|
|
359
|
-
|
|
360
236
|
const doc = response[i];
|
|
361
|
-
|
|
362
237
|
if ("error" in doc) {
|
|
363
|
-
|
|
364
238
|
const reason = doc.reason ?? doc.error;
|
|
365
|
-
|
|
366
|
-
d(Result.error(reason));
|
|
239
|
+
d(results_1.Result.error(reason));
|
|
367
240
|
return;
|
|
368
241
|
}
|
|
369
|
-
|
|
370
242
|
if ("id" in doc && "ok" in doc) {
|
|
371
243
|
ids.push(doc.id);
|
|
372
|
-
}
|
|
373
|
-
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
d(results_1.Result.error(doc.error));
|
|
374
247
|
return;
|
|
375
248
|
}
|
|
376
249
|
}
|
|
377
|
-
|
|
378
250
|
this._bulkGetAdditions(ids, (bulkGetResponse) => {
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
d(Result.error(bulkGetResponse));
|
|
251
|
+
if (bulkGetResponse.ok !== results_1.Result.SUCCESS) {
|
|
252
|
+
d(results_1.Result.error(bulkGetResponse));
|
|
382
253
|
return;
|
|
383
254
|
}
|
|
384
|
-
|
|
385
255
|
for (let i = 0, length = bulkGetResponse.data.results.length; i < length; i++) {
|
|
386
256
|
const docs = bulkGetResponse.data.results[i].docs;
|
|
387
|
-
|
|
388
257
|
if (docs.length === 1) {
|
|
389
258
|
const doc = docs[0];
|
|
390
|
-
|
|
391
259
|
if ("ok" in doc) {
|
|
392
|
-
schemaResult.adds.push(doc.ok
|
|
260
|
+
schemaResult.adds.push(doc.ok);
|
|
393
261
|
}
|
|
394
262
|
}
|
|
395
263
|
}
|
|
396
|
-
|
|
397
|
-
d(Result.success());
|
|
264
|
+
d(results_1.Result.success());
|
|
398
265
|
});
|
|
399
266
|
});
|
|
400
|
-
|
|
401
267
|
});
|
|
402
268
|
}
|
|
403
|
-
|
|
404
269
|
pipeline.filter((r) => {
|
|
405
|
-
if (r.ok !== Result.SUCCESS) {
|
|
270
|
+
if (r.ok !== results_1.Result.SUCCESS) {
|
|
406
271
|
d(r);
|
|
407
272
|
return;
|
|
408
273
|
}
|
|
409
|
-
|
|
410
|
-
d(Result.success());
|
|
274
|
+
d(results_1.Result.success());
|
|
411
275
|
});
|
|
412
276
|
});
|
|
413
|
-
}
|
|
414
|
-
|
|
277
|
+
}
|
|
278
|
+
catch (e) {
|
|
279
|
+
d(results_1.Result.error(e));
|
|
415
280
|
}
|
|
416
281
|
}, done);
|
|
417
282
|
}
|
|
418
|
-
|
|
419
|
-
private _prepareProperties(...properties: PropertyInfo<any>[]) {
|
|
420
|
-
|
|
283
|
+
_prepareProperties(...properties) {
|
|
421
284
|
properties.sort((a, b) => a.name.localeCompare(b.name, undefined, { sensitivity: 'base' }));
|
|
422
285
|
const paths = properties.map(w => w.getAssignmentPath({ parent: "doc" }));
|
|
423
286
|
const viewName = `by_${paths.join("_")}`;
|
|
424
|
-
|
|
425
287
|
return {
|
|
426
288
|
properties,
|
|
427
289
|
paths,
|
|
428
290
|
viewName
|
|
429
|
-
}
|
|
291
|
+
};
|
|
430
292
|
}
|
|
431
|
-
|
|
432
|
-
private resolveIndexes<TEntity extends {}, TShape extends unknown = TEntity>(event: DbPluginQueryEvent<TEntity, TShape>, done: CallbackResult<PouchDbDesignDoc | null>) {
|
|
433
|
-
|
|
293
|
+
resolveIndexes(event, done) {
|
|
434
294
|
const indexes = event.operation.schema.getIndexes();
|
|
435
|
-
|
|
436
295
|
if (indexes.length === 0) {
|
|
437
|
-
done(Result.success(null));
|
|
296
|
+
done(results_1.Result.success(null));
|
|
438
297
|
return;
|
|
439
298
|
}
|
|
440
|
-
|
|
441
299
|
if (cache["indexes"]) {
|
|
442
|
-
done(Result.success(cache["indexes"]
|
|
300
|
+
done(results_1.Result.success(cache["indexes"]));
|
|
443
301
|
return; // Already built
|
|
444
302
|
}
|
|
445
|
-
|
|
446
|
-
const ddoc: PouchDbDesignDoc = {
|
|
303
|
+
const ddoc = {
|
|
447
304
|
_id: `_design/${INDEX_NAME}`,
|
|
448
305
|
views: {}
|
|
449
|
-
}
|
|
450
|
-
|
|
306
|
+
};
|
|
451
307
|
for (let i = 0, length = indexes.length; i < length; i++) {
|
|
452
308
|
const index = indexes[i];
|
|
453
309
|
const { viewName, paths } = this._prepareProperties(...index.properties);
|
|
454
|
-
|
|
455
310
|
if (index.properties.length === 1) {
|
|
456
311
|
const property = index.properties[0];
|
|
457
312
|
const path = property.getAssignmentPath({ parent: "doc" });
|
|
@@ -461,19 +316,17 @@ export class PouchDbPlugin implements IDbPlugin {
|
|
|
461
316
|
emit(${path});
|
|
462
317
|
}
|
|
463
318
|
}`
|
|
464
|
-
}
|
|
319
|
+
};
|
|
465
320
|
continue;
|
|
466
321
|
}
|
|
467
|
-
|
|
468
322
|
ddoc.views[viewName] = {
|
|
469
323
|
map: `function(doc) {
|
|
470
324
|
if (${paths.join(" && ")}) {
|
|
471
325
|
emit([${paths.join(",")}]);
|
|
472
326
|
}
|
|
473
327
|
}`
|
|
474
|
-
}
|
|
328
|
+
};
|
|
475
329
|
}
|
|
476
|
-
|
|
477
330
|
this._doWork((db, d) => {
|
|
478
331
|
// 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
|
|
479
332
|
db.allDocs({
|
|
@@ -481,329 +334,253 @@ export class PouchDbPlugin implements IDbPlugin {
|
|
|
481
334
|
endkey: '_design0',
|
|
482
335
|
include_docs: true
|
|
483
336
|
}).then(response => {
|
|
484
|
-
|
|
485
337
|
// if we have no indexes, let's create it
|
|
486
338
|
if (response.rows.length === 0) {
|
|
487
|
-
db.put(ddoc
|
|
339
|
+
db.put(ddoc, {}, (e) => {
|
|
488
340
|
if (e) {
|
|
489
|
-
d(Result.error(e));
|
|
341
|
+
d(results_1.Result.error(e));
|
|
490
342
|
return;
|
|
491
343
|
}
|
|
492
|
-
|
|
493
344
|
cache["indexes"] = ddoc;
|
|
494
|
-
|
|
495
|
-
d(Result.success(ddoc));
|
|
345
|
+
d(results_1.Result.success(ddoc));
|
|
496
346
|
});
|
|
497
347
|
return;
|
|
498
348
|
}
|
|
499
|
-
|
|
500
349
|
const matchingIndex = response.rows.find(x => x.id === `_design/${INDEX_NAME}`);
|
|
501
|
-
|
|
502
350
|
// make sure we have the correct index created
|
|
503
351
|
if (matchingIndex == null) {
|
|
504
|
-
db.put(ddoc
|
|
352
|
+
db.put(ddoc, {}, (e) => {
|
|
505
353
|
if (e) {
|
|
506
|
-
d(Result.error(e));
|
|
354
|
+
d(results_1.Result.error(e));
|
|
507
355
|
return;
|
|
508
356
|
}
|
|
509
|
-
|
|
510
357
|
cache["indexes"] = ddoc;
|
|
511
|
-
|
|
512
|
-
d(Result.success(ddoc));
|
|
358
|
+
d(results_1.Result.success(ddoc));
|
|
513
359
|
});
|
|
514
360
|
return;
|
|
515
361
|
}
|
|
516
|
-
|
|
517
|
-
assertIsNotNull(
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
const views = Object.keys(ddoc.views)
|
|
521
|
-
|
|
362
|
+
(0, assertions_1.assertIsNotNull)(ddoc.views);
|
|
363
|
+
(0, assertions_1.assertIsNotNull)(matchingIndex.doc);
|
|
364
|
+
const views = Object.keys(ddoc.views);
|
|
522
365
|
// make sure all of the views are in the document, if not update it
|
|
523
|
-
if (views.some(x =>
|
|
524
|
-
|
|
366
|
+
if (views.some(x => matchingIndex.doc.views[x] == null)) {
|
|
525
367
|
// need to get the rev
|
|
526
368
|
db.get(ddoc._id, {}, (e, result) => {
|
|
527
|
-
|
|
528
369
|
if (e != null) {
|
|
529
|
-
d(Result.error(e));
|
|
370
|
+
d(results_1.Result.error(e));
|
|
530
371
|
return;
|
|
531
372
|
}
|
|
532
|
-
|
|
533
|
-
(result
|
|
534
|
-
|
|
535
|
-
db.put(result as any, {}, (e) => {
|
|
536
|
-
|
|
373
|
+
result.views = ddoc.views;
|
|
374
|
+
db.put(result, {}, (e) => {
|
|
537
375
|
if (e) {
|
|
538
|
-
d(Result.error(e));
|
|
376
|
+
d(results_1.Result.error(e));
|
|
539
377
|
return;
|
|
540
378
|
}
|
|
541
|
-
|
|
542
379
|
cache["indexes"] = ddoc;
|
|
543
|
-
|
|
544
|
-
d(Result.success(ddoc));
|
|
380
|
+
d(results_1.Result.success(ddoc));
|
|
545
381
|
});
|
|
546
|
-
})
|
|
382
|
+
});
|
|
547
383
|
return;
|
|
548
384
|
}
|
|
549
|
-
|
|
550
|
-
d(Result.success(ddoc));
|
|
385
|
+
d(results_1.Result.success(ddoc));
|
|
551
386
|
}).catch(e => {
|
|
552
|
-
d(Result.error(e));
|
|
387
|
+
d(results_1.Result.error(e));
|
|
553
388
|
});
|
|
554
|
-
}, done)
|
|
389
|
+
}, done);
|
|
555
390
|
}
|
|
556
|
-
|
|
557
391
|
// id should be a string here because this is only run on identity inserts
|
|
558
|
-
|
|
559
|
-
|
|
392
|
+
_bulkGetAdditions(ids, done) {
|
|
560
393
|
if (ids.length === 0) {
|
|
561
|
-
done(Result.success({ results: [] }));
|
|
394
|
+
done(results_1.Result.success({ results: [] }));
|
|
562
395
|
return;
|
|
563
396
|
}
|
|
564
|
-
|
|
565
397
|
this._doWork((db, d) => {
|
|
566
|
-
db.bulkGet
|
|
567
|
-
docs: ids.map(w => ({ id: w
|
|
398
|
+
db.bulkGet({
|
|
399
|
+
docs: ids.map(w => ({ id: w }))
|
|
568
400
|
}, (error, response) => {
|
|
569
|
-
|
|
570
401
|
if (error) {
|
|
571
|
-
d(Result.error(error));
|
|
572
|
-
return
|
|
402
|
+
d(results_1.Result.error(error));
|
|
403
|
+
return;
|
|
573
404
|
}
|
|
574
|
-
|
|
575
|
-
d(Result.success(response));
|
|
405
|
+
d(results_1.Result.success(response));
|
|
576
406
|
});
|
|
577
407
|
}, done);
|
|
578
408
|
}
|
|
579
|
-
|
|
580
|
-
private _validateSchemas(event: DbPluginBulkPersistEvent) {
|
|
409
|
+
_validateSchemas(event) {
|
|
581
410
|
for (const [, schema] of event.schemas) {
|
|
582
411
|
if (schema.idProperties.length > 1) {
|
|
583
|
-
throw new Error("PouchDB cannot have more than one key per document. Only '_id' is allowed to be the key")
|
|
412
|
+
throw new Error("PouchDB cannot have more than one key per document. Only '_id' is allowed to be the key");
|
|
584
413
|
}
|
|
585
414
|
}
|
|
586
415
|
}
|
|
587
|
-
|
|
588
|
-
private _bulkPersist(
|
|
589
|
-
event: DbPluginBulkPersistEvent,
|
|
590
|
-
done: PluginEventCallbackPartialResult<BulkPersistResult>) {
|
|
591
|
-
|
|
416
|
+
_bulkPersist(event, done) {
|
|
592
417
|
this._validateSchemas(event);
|
|
593
418
|
this._tryStartSync(event.schemas);
|
|
594
|
-
|
|
595
419
|
const result = event.operation.toResult();
|
|
596
|
-
const identitySchemaIds
|
|
597
|
-
const defaultSchemaIds
|
|
598
|
-
|
|
420
|
+
const identitySchemaIds = [];
|
|
421
|
+
const defaultSchemaIds = [];
|
|
599
422
|
for (const [schemaId, schema] of event.schemas) {
|
|
600
|
-
|
|
601
423
|
if (schema.hasIdentityKeys) {
|
|
602
424
|
identitySchemaIds.push(schemaId);
|
|
603
425
|
continue;
|
|
604
426
|
}
|
|
605
|
-
|
|
606
427
|
defaultSchemaIds.push(schemaId);
|
|
607
428
|
}
|
|
608
|
-
|
|
609
429
|
this._identityBulkOperations(identitySchemaIds, event.operation, result, (identityResult) => {
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
done(PluginEventResult.error(event.id, identityResult.error))
|
|
430
|
+
if (identityResult.ok !== results_1.Result.SUCCESS) {
|
|
431
|
+
done(results_1.PluginEventResult.error(event.id, identityResult.error));
|
|
613
432
|
return;
|
|
614
433
|
}
|
|
615
|
-
|
|
616
434
|
this._defaultBulkOperations(defaultSchemaIds, event.operation, result, (defaultResult) => {
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
done(PluginEventResult.error(event.id, defaultResult.error))
|
|
435
|
+
if (defaultResult.ok !== results_1.Result.SUCCESS) {
|
|
436
|
+
done(results_1.PluginEventResult.error(event.id, defaultResult.error));
|
|
620
437
|
return;
|
|
621
438
|
}
|
|
622
|
-
|
|
623
|
-
done(PluginEventResult.success(event.id, result))
|
|
439
|
+
done(results_1.PluginEventResult.success(event.id, result));
|
|
624
440
|
});
|
|
625
|
-
|
|
626
441
|
});
|
|
627
442
|
}
|
|
628
|
-
|
|
629
|
-
private _doWork<TResult, TEntity>(work: (db: PouchDB.Database<TEntity>, done: CallbackResult<TResult>) => void, done: CallbackResult<TResult>, shouldClose: boolean = false) {
|
|
443
|
+
_doWork(work, done, shouldClose = false) {
|
|
630
444
|
const { sync, queryType, ...rest } = this._options ?? {};
|
|
631
|
-
const db = new
|
|
632
|
-
|
|
445
|
+
const db = new pouchdb_1.default(this._name, rest);
|
|
633
446
|
work(db, (result) => {
|
|
634
|
-
|
|
635
447
|
if (shouldClose) {
|
|
636
448
|
db.close(() => done(result));
|
|
637
449
|
}
|
|
638
|
-
|
|
639
450
|
done(result);
|
|
640
|
-
})
|
|
451
|
+
});
|
|
641
452
|
}
|
|
642
|
-
|
|
643
|
-
destroy(_event: DbPluginEvent, done: (error?: any) => void): void {
|
|
453
|
+
destroy(_event, done) {
|
|
644
454
|
// this needs to be queued too
|
|
645
455
|
this._doWork((w, d) => {
|
|
646
456
|
w.destroy(null, (e) => {
|
|
647
457
|
if (e) {
|
|
648
|
-
d(Result.error(e))
|
|
458
|
+
d(results_1.Result.error(e));
|
|
649
459
|
return;
|
|
650
460
|
}
|
|
651
|
-
|
|
652
|
-
d(Result.success());
|
|
461
|
+
d(results_1.Result.success());
|
|
653
462
|
});
|
|
654
463
|
}, done, false);
|
|
655
464
|
}
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
bulkPersist(
|
|
659
|
-
event: DbPluginBulkPersistEvent,
|
|
660
|
-
done: PluginEventCallbackPartialResult<BulkPersistResult>) {
|
|
661
|
-
|
|
662
|
-
const unitOfWork: SyncronousUnitOfWork = (d) => this._bulkPersist(event, (r) => {
|
|
465
|
+
bulkPersist(event, done) {
|
|
466
|
+
const unitOfWork = (d) => this._bulkPersist(event, (r) => {
|
|
663
467
|
d();
|
|
664
|
-
done(r)
|
|
665
|
-
})
|
|
666
|
-
|
|
468
|
+
done(r);
|
|
469
|
+
});
|
|
667
470
|
queue.enqueue(unitOfWork.bind(this));
|
|
668
471
|
}
|
|
669
|
-
|
|
670
|
-
query<TRoot extends {}, TShape extends any = TRoot>(event: DbPluginQueryEvent<TRoot, TShape>, done: PluginEventCallbackResult<TShape>): void {
|
|
671
|
-
|
|
472
|
+
query(event, done) {
|
|
672
473
|
this._tryStartSync(event.schemas);
|
|
673
|
-
|
|
674
|
-
const unitOfWork: SyncronousUnitOfWork = (d) => this._query<TRoot, TShape>(event, (r) => {
|
|
474
|
+
const unitOfWork = (d) => this._query(event, (r) => {
|
|
675
475
|
d();
|
|
676
|
-
done(r)
|
|
677
|
-
})
|
|
678
|
-
|
|
476
|
+
done(r);
|
|
477
|
+
});
|
|
679
478
|
queue.enqueue(unitOfWork.bind(this));
|
|
680
479
|
}
|
|
681
|
-
|
|
682
|
-
protected onGetIndex<TEntity extends {}, TShape extends unknown = TEntity>(_: IQuery<TEntity, TShape>, __: PouchDB.Find.FindRequest<unknown>, done: (result: null | string | [string, string]) => void) {
|
|
480
|
+
onGetIndex(_, __, done) {
|
|
683
481
|
done(null);
|
|
684
482
|
}
|
|
685
|
-
|
|
686
|
-
extractEqualityValueForProperty(expression: Expression, prop: PropertyInfo<any>): any | undefined {
|
|
483
|
+
extractEqualityValueForProperty(expression, prop) {
|
|
687
484
|
// Recursively search for a comparator expression that matches the property
|
|
688
|
-
if (expression.type === "comparator" &&
|
|
485
|
+
if (expression.type === "comparator" && expression.comparator === "equals") {
|
|
689
486
|
// Check left side is property, right side is value
|
|
690
|
-
if (expression.left && expression.left.type === "property" &&
|
|
691
|
-
return
|
|
487
|
+
if (expression.left && expression.left.type === "property" && expression.left.property.name === prop.name && expression.right && expression.right.type === "value") {
|
|
488
|
+
return expression.right.value;
|
|
692
489
|
}
|
|
693
490
|
// Or right side is property, left side is value
|
|
694
|
-
if (expression.right && expression.right.type === "property" &&
|
|
695
|
-
return
|
|
491
|
+
if (expression.right && expression.right.type === "property" && expression.right.property.name === prop.name && expression.left && expression.left.type === "value") {
|
|
492
|
+
return expression.left.value;
|
|
696
493
|
}
|
|
697
494
|
}
|
|
698
495
|
// Recursively check left and right
|
|
699
496
|
if (expression.left) {
|
|
700
497
|
const left = this.extractEqualityValueForProperty(expression.left, prop);
|
|
701
|
-
if (left !== undefined)
|
|
498
|
+
if (left !== undefined)
|
|
499
|
+
return left;
|
|
702
500
|
}
|
|
703
501
|
if (expression.right) {
|
|
704
502
|
const right = this.extractEqualityValueForProperty(expression.right, prop);
|
|
705
|
-
if (right !== undefined)
|
|
503
|
+
if (right !== undefined)
|
|
504
|
+
return right;
|
|
706
505
|
}
|
|
707
506
|
return undefined;
|
|
708
507
|
}
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
const queryProperties = getProperties(expression);
|
|
712
|
-
|
|
508
|
+
_findMatchingIndex(ddoc, expression) {
|
|
509
|
+
const queryProperties = (0, expressions_1.getProperties)(expression);
|
|
713
510
|
const { viewName } = this._prepareProperties(...queryProperties);
|
|
714
|
-
|
|
715
511
|
if (ddoc.views == null) {
|
|
716
512
|
return null;
|
|
717
513
|
}
|
|
718
|
-
|
|
719
514
|
const index = ddoc.views[viewName];
|
|
720
|
-
|
|
721
515
|
if (index == null) {
|
|
722
516
|
return null;
|
|
723
517
|
}
|
|
724
|
-
|
|
725
|
-
const properties: MatchingIndex["properties"] = [];
|
|
726
|
-
|
|
518
|
+
const properties = [];
|
|
727
519
|
for (const property of queryProperties) {
|
|
728
520
|
const value = this.extractEqualityValueForProperty(expression, property);
|
|
729
|
-
|
|
730
521
|
if (value === undefined) {
|
|
731
|
-
return null
|
|
522
|
+
return null;
|
|
732
523
|
}
|
|
733
|
-
|
|
734
524
|
properties.push({
|
|
735
525
|
property,
|
|
736
526
|
value
|
|
737
527
|
});
|
|
738
528
|
}
|
|
739
|
-
|
|
740
529
|
return {
|
|
741
530
|
viewName,
|
|
742
531
|
properties
|
|
743
532
|
};
|
|
744
533
|
}
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
const translator = new PouchDbTranslator<TEntity, TShape>(event.operation);
|
|
534
|
+
_queryIndex(event, matchingIndex, done) {
|
|
535
|
+
const translator = new PouchDbTranslator_1.PouchDbTranslator(event.operation);
|
|
748
536
|
this._doWork((w, d) => {
|
|
749
|
-
|
|
750
|
-
const options: PouchDB.Query.Options<any, any> = {
|
|
537
|
+
const options = {
|
|
751
538
|
include_docs: true,
|
|
752
539
|
};
|
|
753
|
-
|
|
754
540
|
const values = matchingIndex.properties.map(x => x.value);
|
|
755
|
-
|
|
756
541
|
if (values.some(x => x == null) == false) {
|
|
757
|
-
|
|
758
542
|
if (values.length === 1) {
|
|
759
543
|
options.key = values[0];
|
|
760
|
-
}
|
|
544
|
+
}
|
|
545
|
+
else {
|
|
761
546
|
options.keys = values;
|
|
762
547
|
}
|
|
763
548
|
}
|
|
764
|
-
|
|
765
549
|
w.query(`${INDEX_NAME}/${matchingIndex.viewName}`, options, (error, response) => {
|
|
766
|
-
|
|
767
550
|
if (error != null) {
|
|
768
|
-
d(Result.error(error));
|
|
551
|
+
d(results_1.Result.error(error));
|
|
769
552
|
return;
|
|
770
553
|
}
|
|
771
|
-
|
|
772
554
|
const translated = translator.translate(response.rows.map(w => w.doc));
|
|
773
|
-
d(Result.success(translated));
|
|
555
|
+
d(results_1.Result.success(translated));
|
|
774
556
|
});
|
|
775
557
|
}, done);
|
|
776
558
|
}
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
const filters = event.operation.options.get("filter")
|
|
559
|
+
_queryNoIndex(event, done) {
|
|
560
|
+
const filters = event.operation.options.get("filter");
|
|
780
561
|
if (filters.length === 0) {
|
|
781
562
|
this._queryWithNoFilters(event, done);
|
|
782
563
|
return;
|
|
783
564
|
}
|
|
784
|
-
|
|
785
565
|
this._queryWithFilters(event, done);
|
|
786
566
|
}
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
const translator = new PouchDbTranslator<TEntity, TShape>(event.operation);
|
|
567
|
+
_queryWithNoFilters(event, done) {
|
|
568
|
+
const translator = new PouchDbTranslator_1.PouchDbTranslator(event.operation);
|
|
790
569
|
this._doWork((w, d) => {
|
|
791
570
|
w.allDocs({
|
|
792
571
|
include_docs: true
|
|
793
572
|
}).then(response => {
|
|
794
573
|
const data = response.rows.map(w => w.doc);
|
|
795
574
|
const translated = translator.translate(data);
|
|
796
|
-
d(Result.success(translated));
|
|
575
|
+
d(results_1.Result.success(translated));
|
|
797
576
|
}).catch(error => {
|
|
798
|
-
d(Result.error(error));
|
|
577
|
+
d(results_1.Result.error(error));
|
|
799
578
|
});
|
|
800
579
|
}, done);
|
|
801
580
|
}
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
const translator = new PouchDbTranslator<TEntity, TShape>(event.operation);
|
|
581
|
+
_queryWithFilters(event, done) {
|
|
582
|
+
const translator = new PouchDbTranslator_1.PouchDbTranslator(event.operation);
|
|
805
583
|
this._doWork((w, d) => {
|
|
806
|
-
|
|
807
584
|
if (this._name.startsWith("http")) {
|
|
808
585
|
// Cannot query remote databases using translator.matches, fallback to alldocs
|
|
809
586
|
// Does not throw, will fail on the remote server
|
|
@@ -812,119 +589,92 @@ export class PouchDbPlugin implements IDbPlugin {
|
|
|
812
589
|
}).then(response => {
|
|
813
590
|
const data = response.rows.map(w => w.doc);
|
|
814
591
|
const translated = translator.translate(data);
|
|
815
|
-
d(Result.success(translated));
|
|
592
|
+
d(results_1.Result.success(translated));
|
|
816
593
|
}).catch(error => {
|
|
817
|
-
d(Result.error(error));
|
|
594
|
+
d(results_1.Result.error(error));
|
|
818
595
|
});
|
|
819
596
|
return;
|
|
820
597
|
}
|
|
821
|
-
|
|
822
|
-
w.query<{}, any>((doc, emit) => {
|
|
598
|
+
w.query((doc, emit) => {
|
|
823
599
|
if (typeof doc === "object" && "_id" in doc && translator.matches(doc)) {
|
|
824
600
|
emit(doc._id, doc);
|
|
825
601
|
}
|
|
826
602
|
}, (error, response) => {
|
|
827
|
-
|
|
828
603
|
if (error != null) {
|
|
829
|
-
d(Result.error(error));
|
|
604
|
+
d(results_1.Result.error(error));
|
|
830
605
|
return;
|
|
831
606
|
}
|
|
832
|
-
|
|
833
607
|
const data = response.rows.map(w => w.value);
|
|
834
608
|
const translated = translator.translate(data);
|
|
835
|
-
d(Result.success(translated));
|
|
609
|
+
d(results_1.Result.success(translated));
|
|
836
610
|
});
|
|
837
|
-
|
|
838
611
|
}, done);
|
|
839
612
|
}
|
|
840
|
-
|
|
841
|
-
private _query<TEntity extends {}, TShape extends unknown = TEntity>(event: DbPluginQueryEvent<TEntity, TShape>, done: PluginEventCallbackResult<TShape>): void {
|
|
613
|
+
_query(event, done) {
|
|
842
614
|
this.resolveIndexes(event, (r) => {
|
|
843
|
-
if (r.ok !== Result.SUCCESS) {
|
|
844
|
-
done(PluginEventResult.error(event.id, r.error))
|
|
615
|
+
if (r.ok !== results_1.Result.SUCCESS) {
|
|
616
|
+
done(results_1.PluginEventResult.error(event.id, r.error));
|
|
845
617
|
return;
|
|
846
618
|
}
|
|
847
|
-
|
|
848
619
|
const ddoc = r.data;
|
|
849
|
-
|
|
850
620
|
if (ddoc == null) {
|
|
851
621
|
this._queryNoIndex(event, done);
|
|
852
622
|
return;
|
|
853
623
|
}
|
|
854
|
-
|
|
855
624
|
const mapMatchingIndex = this.findMatchingIndexForMap(ddoc, event);
|
|
856
|
-
|
|
857
625
|
if (mapMatchingIndex != null) {
|
|
858
626
|
this._queryIndex(event, mapMatchingIndex, done);
|
|
859
627
|
return;
|
|
860
628
|
}
|
|
861
|
-
|
|
862
629
|
const expression = this._getExpressionFromQuery(event);
|
|
863
|
-
|
|
864
|
-
if (Expression.isEmpty(expression)) {
|
|
630
|
+
if (expressions_1.Expression.isEmpty(expression)) {
|
|
865
631
|
this._queryNoIndex(event, done);
|
|
866
632
|
return;
|
|
867
633
|
}
|
|
868
|
-
|
|
869
634
|
const matchingIndex = this._findMatchingIndex(ddoc, expression);
|
|
870
|
-
|
|
871
635
|
if (matchingIndex == null) {
|
|
872
636
|
this._queryNoIndex(event, done);
|
|
873
637
|
return;
|
|
874
638
|
}
|
|
875
|
-
|
|
876
639
|
this._queryIndex(event, matchingIndex, done);
|
|
877
640
|
});
|
|
878
641
|
}
|
|
879
|
-
|
|
880
|
-
private _getExpressionFromQuery<TEntity extends {}, TShape>(event: DbPluginQueryEvent<TEntity, TShape>) {
|
|
642
|
+
_getExpressionFromQuery(event) {
|
|
881
643
|
const filters = event.operation.options.get("filter");
|
|
882
|
-
|
|
883
644
|
const databaseFilters = filters.filter(x => x.option.target === "database");
|
|
884
|
-
|
|
885
645
|
if (databaseFilters.length === 0) {
|
|
886
|
-
return Expression.EMPTY;
|
|
646
|
+
return expressions_1.Expression.EMPTY;
|
|
887
647
|
}
|
|
888
|
-
|
|
889
648
|
const expressions = databaseFilters.map(x => x.option.value.expression);
|
|
890
|
-
|
|
891
|
-
return combineExpressions(...expressions);
|
|
649
|
+
return (0, expressions_1.combineExpressions)(...expressions);
|
|
892
650
|
}
|
|
893
|
-
|
|
894
|
-
private findMatchingIndexForMap<TEntity extends {}, TShape>(ddoc: PouchDbDesignDoc, event: DbPluginQueryEvent<TEntity, TShape>): MatchingIndex | null {
|
|
651
|
+
findMatchingIndexForMap(ddoc, event) {
|
|
895
652
|
const map = event.operation.options.getLast("map");
|
|
896
653
|
const filters = event.operation.options.get("filter");
|
|
897
|
-
|
|
898
654
|
// we only want to look at the index if there are no filters
|
|
899
655
|
if (filters.length > 0) {
|
|
900
656
|
return null;
|
|
901
657
|
}
|
|
902
|
-
|
|
903
658
|
if (map == null || map.target !== "database") {
|
|
904
659
|
return null;
|
|
905
660
|
}
|
|
906
|
-
|
|
907
661
|
if (map.value.fields.length !== 1 || map.value.fields[0].property == null) {
|
|
908
662
|
return null;
|
|
909
663
|
}
|
|
910
|
-
|
|
911
664
|
const property = map.value.fields[0].property;
|
|
912
|
-
|
|
913
665
|
const { viewName } = this._prepareProperties(property);
|
|
914
|
-
|
|
915
666
|
if (ddoc.views == null) {
|
|
916
667
|
return null;
|
|
917
668
|
}
|
|
918
|
-
|
|
919
669
|
const index = ddoc.views[viewName];
|
|
920
|
-
|
|
921
670
|
if (index == null) {
|
|
922
671
|
return null;
|
|
923
672
|
}
|
|
924
|
-
|
|
925
673
|
return {
|
|
926
674
|
viewName,
|
|
927
675
|
properties: [{ property, value: null }]
|
|
928
|
-
}
|
|
676
|
+
};
|
|
929
677
|
}
|
|
930
|
-
}
|
|
678
|
+
}
|
|
679
|
+
exports.PouchDbPlugin = PouchDbPlugin;
|
|
680
|
+
//# sourceMappingURL=PouchDbPlugin.js.map
|