kuzzle 2.60.1 → 2.61.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/index.d.ts +2 -0
  2. package/dist/index.js +2 -0
  3. package/dist/lib/cluster/idCardHandler.d.ts +11 -1
  4. package/dist/lib/cluster/idCardHandler.js +10 -2
  5. package/dist/lib/cluster/node.d.ts +14 -0
  6. package/dist/lib/cluster/node.js +19 -0
  7. package/dist/lib/core/backend/backendStorage.js +4 -6
  8. package/dist/lib/core/plugin/pluginContext.js +2 -2
  9. package/dist/lib/core/storage/clientAdapter.d.ts +13 -10
  10. package/dist/lib/core/storage/clientAdapter.js +112 -60
  11. package/dist/lib/core/storage/conformance/capabilityCases.d.ts +3 -0
  12. package/dist/lib/core/storage/conformance/capabilityCases.js +121 -0
  13. package/dist/lib/core/storage/conformance/conformanceContext.d.ts +33 -0
  14. package/dist/lib/core/storage/conformance/conformanceContext.js +97 -0
  15. package/dist/lib/core/storage/conformance/conformanceEnvironment.d.ts +7 -0
  16. package/dist/lib/core/storage/conformance/conformanceEnvironment.js +136 -0
  17. package/dist/lib/core/storage/conformance/coreCases.d.ts +3 -0
  18. package/dist/lib/core/storage/conformance/coreCases.js +381 -0
  19. package/dist/lib/core/storage/conformance/storageDriverConformance.d.ts +53 -0
  20. package/dist/lib/core/storage/conformance/storageDriverConformance.js +89 -0
  21. package/dist/lib/core/storage/indexCache.d.ts +26 -2
  22. package/dist/lib/core/storage/indexCache.js +100 -2
  23. package/dist/lib/core/storage/storageDriverLoader.d.ts +41 -0
  24. package/dist/lib/core/storage/storageDriverLoader.js +276 -0
  25. package/dist/lib/core/storage/storageEngine.js +5 -1
  26. package/dist/lib/core/storage/storageError.d.ts +15 -0
  27. package/dist/lib/core/storage/storageError.js +36 -0
  28. package/dist/lib/kerror/codes/1-services.json +35 -0
  29. package/dist/lib/kerror/codes/8-cluster.json +6 -0
  30. package/dist/lib/service/storage/7/elasticsearch.d.ts +5 -1
  31. package/dist/lib/service/storage/builtInDrivers.d.ts +17 -0
  32. package/dist/lib/service/storage/builtInDrivers.js +61 -0
  33. package/dist/lib/service/storage/storageService.d.ts +13 -0
  34. package/dist/lib/service/storage/storageService.js +44 -0
  35. package/dist/lib/types/config/storageEngine/StorageEngineElasticsearchConfiguration.d.ts +8 -1
  36. package/dist/lib/types/index.d.ts +1 -0
  37. package/dist/lib/types/index.js +1 -0
  38. package/dist/lib/types/storage/StorageDriver.d.ts +306 -0
  39. package/dist/lib/types/storage/StorageDriver.js +41 -0
  40. package/dist/package.json +2 -1
  41. package/package.json +2 -1
  42. package/dist/lib/service/storage/Elasticsearch.d.ts +0 -16
  43. package/dist/lib/service/storage/Elasticsearch.js +0 -48
@@ -52,7 +52,9 @@ var __importStar = (this && this.__importStar) || (function () {
52
52
  return result;
53
53
  };
54
54
  })();
55
- const Elasticsearch_1 = require("../../service/storage/Elasticsearch");
55
+ const storageService_1 = require("../../service/storage/storageService");
56
+ const StorageDriver_1 = require("../../types/storage/StorageDriver");
57
+ const storageDriverLoader_1 = require("./storageDriverLoader");
56
58
  const indexCache_1 = require("./indexCache");
57
59
  const safeObject_1 = require("../../util/safeObject");
58
60
  const kerror = __importStar(require("../../kerror"));
@@ -74,19 +76,49 @@ const DEFAULT_LOAD_MAPPINGS_OPTIONS = Object.freeze({
74
76
  rawMappings: false,
75
77
  refresh: false,
76
78
  });
79
+ const driverError = kerror.wrap("services", "storage_driver");
80
+ /**
81
+ * @throws {services.storage_driver.unsupported_capability} if the driver does
82
+ * not declare the capability of `method`
83
+ */
84
+ function assertCapability(capabilities, driver, method) {
85
+ const capability = StorageDriver_1.STORAGE_CAPABILITIES.find((name) => storageDriverLoader_1.CAPABILITY_METHODS[name].includes(method));
86
+ // Every capability method has its capability, and the loader checked that
87
+ // a declared capability has its methods: the first and last tests are what
88
+ // narrow the types.
89
+ if (capability === undefined ||
90
+ !capabilities.has(capability) ||
91
+ typeof driver[method] !== "function") {
92
+ throw driverError.get("unsupported_capability", capability);
93
+ }
94
+ }
95
+ /**
96
+ * @throws {services.storage_driver.unsupported_capability} if the search body
97
+ * has aggregations the driver does not declare
98
+ */
99
+ function assertSearchCapabilities(capabilities, searchBody) {
100
+ if (!capabilities.has("aggregations") &&
101
+ (searchBody?.aggregations !== undefined || searchBody?.aggs !== undefined)) {
102
+ throw driverError.get("unsupported_capability", "aggregations");
103
+ }
104
+ }
77
105
  /**
78
106
  * Storage client adapter to perform validation on index/collection existence
79
107
  * and to maintain the index/collection cache.
80
108
  */
81
109
  class ClientAdapter {
82
110
  constructor(scope) {
83
- this.es = new Elasticsearch_1.Elasticsearch(global.kuzzle.config.services.storageEngine, scope);
84
- this.client = this.es.client;
111
+ const config = global.kuzzle.config.services.storageEngine;
112
+ const selected = (0, storageDriverLoader_1.selectStorageDriver)(config);
113
+ this.service = new storageService_1.StorageService(selected.Driver, config, scope);
114
+ this.driver = this.service.driver;
115
+ this.capabilities = selected.capabilities;
116
+ (0, storageDriverLoader_1.assertDeclaredCapabilities)(this.driver, selected);
85
117
  this.scope = scope;
86
118
  this.cache = new indexCache_1.IndexCache();
87
119
  }
88
120
  async init() {
89
- await this.es.init();
121
+ await this.service.init();
90
122
  await this.populateCache();
91
123
  this.registerCollectionEvents();
92
124
  this.registerIndexEvents();
@@ -113,14 +145,14 @@ class ClientAdapter {
113
145
  * Return information about the instantiated ES service
114
146
  * @returns {Promise.<Object>}
115
147
  */
116
- global.kuzzle.onAsk(`core:storage:${this.scope}:info:get`, () => this.client.info());
148
+ global.kuzzle.onAsk(`core:storage:${this.scope}:info:get`, () => this.driver.info());
117
149
  /**
118
150
  * Translate Koncorde filters to Elasticsearch query
119
151
  *
120
152
  * @param {Object} koncordeFilters - Set of valid Koncorde filters
121
153
  * @returns {Object} Equivalent Elasticsearch query
122
154
  */
123
- global.kuzzle.onAsk(`core:storage:${this.scope}:translate`, (filters) => this.client.translateKoncordeFilters(filters));
155
+ global.kuzzle.onAsk(`core:storage:${this.scope}:translate`, (filters) => this.driver.translateKoncordeFilters(filters));
124
156
  }
125
157
  async createIndex(index, { indexCacheOnly = false, propagate = true } = {}) {
126
158
  if (this.cache.hasIndex(index)) {
@@ -128,7 +160,7 @@ class ClientAdapter {
128
160
  }
129
161
  if (!indexCacheOnly) {
130
162
  try {
131
- await this.client.createIndex(index);
163
+ await this.driver.createIndex(index);
132
164
  }
133
165
  catch (error) {
134
166
  // The index exists in storage but not in this node's cache: a peer
@@ -154,7 +186,7 @@ class ClientAdapter {
154
186
  if (!indexCacheOnly) {
155
187
  // Creating an existing collection updates its mappings and settings
156
188
  this.cache.assertCollectionWritable(index, collection);
157
- await this.client.createCollection(index, collection, opts);
189
+ await this.driver.createCollection(index, collection, opts);
158
190
  }
159
191
  this.cache.addCollection(index, collection);
160
192
  if (propagate) {
@@ -168,7 +200,7 @@ class ClientAdapter {
168
200
  async deleteIndex(index) {
169
201
  this.cache.assertIndexExists(index);
170
202
  this.cache.assertIndexWritable(index);
171
- await this.client.deleteIndex(index);
203
+ await this.driver.deleteIndex(index);
172
204
  this.cache.removeIndex(index);
173
205
  global.kuzzle.emit("core:storage:index:delete:after", {
174
206
  index,
@@ -180,7 +212,7 @@ class ClientAdapter {
180
212
  this.cache.assertIndexExists(index);
181
213
  this.cache.assertIndexWritable(index);
182
214
  }
183
- const deleted = await this.client.deleteIndexes(indexes);
215
+ const deleted = await this.driver.deleteIndexes(indexes);
184
216
  if (deleted.length > 0) {
185
217
  for (const index of deleted) {
186
218
  this.cache.removeIndex(index);
@@ -195,7 +227,7 @@ class ClientAdapter {
195
227
  async deleteCollection(index, collection) {
196
228
  this.cache.assertCollectionExists(index, collection);
197
229
  this.cache.assertCollectionWritable(index, collection);
198
- await this.client.deleteCollection(index, collection);
230
+ await this.driver.deleteCollection(index, collection);
199
231
  this.cache.removeCollection(index, collection);
200
232
  global.kuzzle.emit("core:storage:collection:delete:after", {
201
233
  collection,
@@ -209,25 +241,24 @@ class ClientAdapter {
209
241
  */
210
242
  async populateCache() {
211
243
  if (global.kuzzle.config.services.storageEngine.generateMissingAliases) {
212
- await this.client.generateMissingAliases();
244
+ await this.driver.generateMissingAliases?.();
213
245
  }
214
246
  await this.reloadCache();
215
247
  }
216
248
  /**
217
- * Adds the indexes and collections found in storage to the index cache,
218
- * then reloads the collection write locks. Unlike `populateCache`, it never
219
- * writes to storage.
249
+ * Replaces the index cache with the indexes and collections found in
250
+ * storage, then reloads the collection write locks. Unlike `populateCache`,
251
+ * it never writes to storage.
252
+ *
253
+ * Replaced, not merged: an index or a collection a peer deleted before this
254
+ * node joined the cluster must go too (kuzzle#3040).
220
255
  */
221
256
  async reloadCache() {
257
+ const snapshot = this.cache.snapshotIndexes();
222
258
  // `client` is untyped (see the field's comment), so the schema shape is
223
259
  // stated here: index name -> its collection names.
224
- const schema = await this.client.getSchema();
225
- for (const [index, collections] of Object.entries(schema)) {
226
- this.cache.addIndex(index);
227
- for (const collection of collections) {
228
- this.cache.addCollection(index, collection);
229
- }
230
- }
260
+ const schema = await this.driver.getSchema();
261
+ this.cache.replaceIndexes(schema, snapshot);
231
262
  await this.loadLocks();
232
263
  }
233
264
  /**
@@ -235,12 +266,19 @@ class ClientAdapter {
235
266
  * stored in Redis.
236
267
  */
237
268
  async loadLocks() {
269
+ const snapshot = this.cache.snapshotLocks();
238
270
  const fields = await global.kuzzle.ask("core:cache:internal:execute", "hgetall", this.locksKey);
239
- this.cache.clearLocks();
271
+ const stored = new Map();
240
272
  for (const [field, value] of Object.entries(fields ?? {})) {
241
273
  const [index, collection] = JSON.parse(field);
242
- this.cache.setLock(index, collection, JSON.parse(value));
274
+ let collections = stored.get(index);
275
+ if (collections === undefined) {
276
+ collections = new Map();
277
+ stored.set(index, collections);
278
+ }
279
+ collections.set(collection, JSON.parse(value));
243
280
  }
281
+ this.cache.replaceLocks(stored, snapshot);
244
282
  }
245
283
  /**
246
284
  * Write-locks a collection cluster-wide: until it is unlocked, every action
@@ -337,7 +375,7 @@ class ClientAdapter {
337
375
  */
338
376
  global.kuzzle.onAsk(`core:storage:${this.scope}:collection:settings:get`, (index, collection) => {
339
377
  this.cache.assertCollectionExists(index, collection);
340
- return this.client.getSettings(index, collection);
378
+ return this.driver.getSettings(index, collection);
341
379
  });
342
380
  /**
343
381
  * Wait, for a bounded time, until the primary shards of a collection
@@ -345,7 +383,7 @@ class ClientAdapter {
345
383
  * @param {string} index
346
384
  * @param {string} collection
347
385
  */
348
- global.kuzzle.onAsk(`core:storage:${this.scope}:collection:shards:wait`, (index, collection) => this.client.waitForCollectionShards(index, collection));
386
+ global.kuzzle.onAsk(`core:storage:${this.scope}:collection:shards:wait`, (index, collection) => this.driver.waitForCollectionShards?.(index, collection));
349
387
  /**
350
388
  * Check a collection existence
351
389
  * @param {string} index
@@ -366,7 +404,7 @@ class ClientAdapter {
366
404
  */
367
405
  global.kuzzle.onAsk(`core:storage:${this.scope}:collection:refresh`, (index, collection) => {
368
406
  this.cache.assertCollectionExists(index, collection);
369
- return this.client.refreshCollection(index, collection);
407
+ return this.driver.refreshCollection(index, collection);
370
408
  });
371
409
  /**
372
410
  * Remove all documents from an existing collection
@@ -377,7 +415,7 @@ class ClientAdapter {
377
415
  global.kuzzle.onAsk(`core:storage:${this.scope}:collection:truncate`, (index, collection) => {
378
416
  this.cache.assertCollectionExists(index, collection);
379
417
  this.cache.assertCollectionWritable(index, collection);
380
- return this.client.truncateCollection(index, collection);
418
+ return this.driver.truncateCollection(index, collection);
381
419
  });
382
420
  /**
383
421
  * Update a collection settings and mappings
@@ -389,7 +427,8 @@ class ClientAdapter {
389
427
  global.kuzzle.onAsk(`core:storage:${this.scope}:collection:update`, (index, collection, changes) => {
390
428
  this.cache.assertCollectionExists(index, collection);
391
429
  this.cache.assertCollectionWritable(index, collection);
392
- return this.client.updateCollection(index, collection, changes);
430
+ assertCapability(this.capabilities, this.driver, "updateCollection");
431
+ return this.driver.updateCollection(index, collection, changes);
393
432
  });
394
433
  }
395
434
  registerIndexEvents() {
@@ -430,7 +469,10 @@ class ClientAdapter {
430
469
  * Return detailed storage stats within this adapter's scope
431
470
  * @returns {Promise.<Object>}
432
471
  */
433
- global.kuzzle.onAsk(`core:storage:${this.scope}:index:stats`, () => this.client.stats());
472
+ global.kuzzle.onAsk(`core:storage:${this.scope}:index:stats`, () => {
473
+ assertCapability(this.capabilities, this.driver, "stats");
474
+ return this.driver.stats();
475
+ });
434
476
  }
435
477
  registerDocumentEvents() {
436
478
  /**
@@ -445,7 +487,7 @@ class ClientAdapter {
445
487
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:bulk`, (index, collection, bulk, opts) => {
446
488
  this.cache.assertCollectionExists(index, collection);
447
489
  this.cache.assertCollectionWritable(index, collection);
448
- return this.client.import(index, collection, bulk, opts);
490
+ return this.driver.import(index, collection, bulk, opts);
449
491
  });
450
492
  /**
451
493
  * Count how many documents match the provided query
@@ -457,7 +499,7 @@ class ClientAdapter {
457
499
  */
458
500
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:count`, (index, collection, query) => {
459
501
  this.cache.assertCollectionExists(index, collection);
460
- return this.client.count(index, collection, query);
502
+ return this.driver.count(index, collection, query);
461
503
  });
462
504
  /**
463
505
  * Create a document
@@ -471,7 +513,7 @@ class ClientAdapter {
471
513
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:create`, (index, collection, content, opts) => {
472
514
  this.cache.assertCollectionExists(index, collection);
473
515
  this.cache.assertCollectionWritable(index, collection);
474
- return this.client.create(index, collection, content, opts);
516
+ return this.driver.create(index, collection, content, opts);
475
517
  });
476
518
  /**
477
519
  * Create or replace a document
@@ -486,7 +528,7 @@ class ClientAdapter {
486
528
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:createOrReplace`, (index, collection, id, content, opts) => {
487
529
  this.cache.assertCollectionExists(index, collection);
488
530
  this.cache.assertCollectionWritable(index, collection);
489
- return this.client.createOrReplace(index, collection, id, content, opts);
531
+ return this.driver.createOrReplace(index, collection, id, content, opts);
490
532
  });
491
533
  /**
492
534
  * Delete a document
@@ -500,7 +542,7 @@ class ClientAdapter {
500
542
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:delete`, (index, collection, id, opts) => {
501
543
  this.cache.assertCollectionExists(index, collection);
502
544
  this.cache.assertCollectionWritable(index, collection);
503
- return this.client.delete(index, collection, id, opts);
545
+ return this.driver.delete(index, collection, id, opts);
504
546
  });
505
547
  /**
506
548
  * Delete all documents matching the provided search query
@@ -514,7 +556,7 @@ class ClientAdapter {
514
556
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:deleteByQuery`, (index, collection, query, opts) => {
515
557
  this.cache.assertCollectionExists(index, collection);
516
558
  this.cache.assertCollectionWritable(index, collection);
517
- return this.client.deleteByQuery(index, collection, query, opts);
559
+ return this.driver.deleteByQuery(index, collection, query, opts);
518
560
  });
519
561
  /**
520
562
  * Delete fields of a document
@@ -529,7 +571,7 @@ class ClientAdapter {
529
571
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:deleteFields`, (index, collection, id, fields, opts) => {
530
572
  this.cache.assertCollectionExists(index, collection);
531
573
  this.cache.assertCollectionWritable(index, collection);
532
- return this.client.deleteFields(index, collection, id, fields, opts);
574
+ return this.driver.deleteFields(index, collection, id, fields, opts);
533
575
  });
534
576
  /**
535
577
  * Check if a document exists
@@ -541,7 +583,7 @@ class ClientAdapter {
541
583
  */
542
584
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:exist`, (index, collection, id) => {
543
585
  this.cache.assertCollectionExists(index, collection);
544
- return this.client.exists(index, collection, id);
586
+ return this.driver.exists(index, collection, id);
545
587
  });
546
588
  /**
547
589
  * Check if a document multiple document Exists
@@ -553,7 +595,7 @@ class ClientAdapter {
553
595
  */
554
596
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:mExists`, (index, collection, ids) => {
555
597
  this.cache.assertCollectionExists(index, collection);
556
- return this.client.mExists(index, collection, ids);
598
+ return this.driver.mExists(index, collection, ids);
557
599
  });
558
600
  /**
559
601
  * Get a document using its unique id
@@ -565,7 +607,7 @@ class ClientAdapter {
565
607
  */
566
608
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:get`, (index, collection, id) => {
567
609
  this.cache.assertCollectionExists(index, collection);
568
- return this.client.get(index, collection, id);
610
+ return this.driver.get(index, collection, id);
569
611
  });
570
612
  /**
571
613
  * Import documents as fixtures
@@ -585,7 +627,7 @@ class ClientAdapter {
585
627
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:mCreate`, (index, collection, documents, opts) => {
586
628
  this.cache.assertCollectionExists(index, collection);
587
629
  this.cache.assertCollectionWritable(index, collection);
588
- return this.client.mCreate(index, collection, documents, opts);
630
+ return this.driver.mCreate(index, collection, documents, opts);
589
631
  });
590
632
  /**
591
633
  * Create or replace multiple documents
@@ -599,7 +641,7 @@ class ClientAdapter {
599
641
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:mCreateOrReplace`, (index, collection, documents, opts) => {
600
642
  this.cache.assertCollectionExists(index, collection);
601
643
  this.cache.assertCollectionWritable(index, collection);
602
- return this.client.mCreateOrReplace(index, collection, documents, opts);
644
+ return this.driver.mCreateOrReplace(index, collection, documents, opts);
603
645
  });
604
646
  /**
605
647
  * Delete multiple documents
@@ -613,7 +655,7 @@ class ClientAdapter {
613
655
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:mDelete`, (index, collection, ids, opts) => {
614
656
  this.cache.assertCollectionExists(index, collection);
615
657
  this.cache.assertCollectionWritable(index, collection);
616
- return this.client.mDelete(index, collection, ids, opts);
658
+ return this.driver.mDelete(index, collection, ids, opts);
617
659
  });
618
660
  /**
619
661
  * Replace multiple documents
@@ -627,7 +669,7 @@ class ClientAdapter {
627
669
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:mReplace`, (index, collection, documents, opts) => {
628
670
  this.cache.assertCollectionExists(index, collection);
629
671
  this.cache.assertCollectionWritable(index, collection);
630
- return this.client.mReplace(index, collection, documents, opts);
672
+ return this.driver.mReplace(index, collection, documents, opts);
631
673
  });
632
674
  /**
633
675
  * Update multiple documents
@@ -641,7 +683,7 @@ class ClientAdapter {
641
683
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:mUpdate`, (index, collection, documents, opts) => {
642
684
  this.cache.assertCollectionExists(index, collection);
643
685
  this.cache.assertCollectionWritable(index, collection);
644
- return this.client.mUpdate(index, collection, documents, opts);
686
+ return this.driver.mUpdate(index, collection, documents, opts);
645
687
  });
646
688
  /**
647
689
  * Applies a partial update to documents provided in the body.
@@ -656,7 +698,7 @@ class ClientAdapter {
656
698
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:mUpsert`, (index, collection, documents, opts) => {
657
699
  this.cache.assertCollectionExists(index, collection);
658
700
  this.cache.assertCollectionWritable(index, collection);
659
- return this.client.mUpsert(index, collection, documents, opts);
701
+ return this.driver.mUpsert(index, collection, documents, opts);
660
702
  });
661
703
  /**
662
704
  * Apply the provided callback to all documents matching a search query
@@ -670,7 +712,7 @@ class ClientAdapter {
670
712
  */
671
713
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:mExecute`, (index, collection, query, callback, opts) => {
672
714
  this.cache.assertCollectionExists(index, collection);
673
- return this.client.mExecute(index, collection, query, callback, opts);
715
+ return this.driver.mExecute(index, collection, query, callback, opts);
674
716
  });
675
717
  /**
676
718
  * Get multiple documents using their ids
@@ -682,7 +724,7 @@ class ClientAdapter {
682
724
  */
683
725
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:mGet`, (index, collection, ids) => {
684
726
  this.cache.assertCollectionExists(index, collection);
685
- return this.client.mGet(index, collection, ids);
727
+ return this.driver.mGet(index, collection, ids);
686
728
  });
687
729
  /**
688
730
  * Replace the content of a document
@@ -697,7 +739,7 @@ class ClientAdapter {
697
739
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:replace`, (index, collection, id, content, opts) => {
698
740
  this.cache.assertCollectionExists(index, collection);
699
741
  this.cache.assertCollectionWritable(index, collection);
700
- return this.client.replace(index, collection, id, content, opts);
742
+ return this.driver.replace(index, collection, id, content, opts);
701
743
  });
702
744
  /**
703
745
  * Fetch the next page of results of a search query
@@ -706,7 +748,7 @@ class ClientAdapter {
706
748
  * @param {Object} [opts] -- see Elasticsearch "scroll" options
707
749
  * @returns {Promise.<{ scrollId, hits, aggregations, total }>}
708
750
  */
709
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:scroll`, (scrollId, opts) => this.client.scroll(scrollId, opts));
751
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:scroll`, (scrollId, opts) => this.driver.scroll(scrollId, opts));
710
752
  /**
711
753
  * Search for documents
712
754
  *
@@ -718,7 +760,8 @@ class ClientAdapter {
718
760
  */
719
761
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:search`, (index, collection, searchBody, opts) => {
720
762
  this.cache.assertCollectionExists(index, collection);
721
- return this.client.search({ collection, index, searchBody }, opts);
763
+ assertSearchCapabilities(this.capabilities, searchBody);
764
+ return this.driver.search({ collection, index, searchBody }, opts);
722
765
  });
723
766
  /**
724
767
  * Search for multiples documents
@@ -734,7 +777,8 @@ class ClientAdapter {
734
777
  this.cache.assertCollectionExists(target.index, collection);
735
778
  }
736
779
  }
737
- return this.client.search({ searchBody, targets }, opts);
780
+ assertSearchCapabilities(this.capabilities, searchBody);
781
+ return this.driver.search({ searchBody, targets }, opts);
738
782
  });
739
783
  /**
740
784
  * Update a document
@@ -749,7 +793,7 @@ class ClientAdapter {
749
793
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:update`, (index, collection, id, content, opts) => {
750
794
  this.cache.assertCollectionExists(index, collection);
751
795
  this.cache.assertCollectionWritable(index, collection);
752
- return this.client.update(index, collection, id, content, opts);
796
+ return this.driver.update(index, collection, id, content, opts);
753
797
  });
754
798
  /**
755
799
  * Update all documents matching the search query, by applying the same
@@ -765,7 +809,8 @@ class ClientAdapter {
765
809
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:updateByQuery`, (index, collection, query, changes, opts) => {
766
810
  this.cache.assertCollectionExists(index, collection);
767
811
  this.cache.assertCollectionWritable(index, collection);
768
- return this.client.updateByQuery(index, collection, query, changes, opts);
812
+ assertCapability(this.capabilities, this.driver, "updateByQuery");
813
+ return this.driver.updateByQuery(index, collection, query, changes, opts);
769
814
  });
770
815
  /**
771
816
  * Directly Update all documents matching the search query (without regards
@@ -781,7 +826,8 @@ class ClientAdapter {
781
826
  global.kuzzle.onAsk(`core:storage:${this.scope}:bulk:updateByQuery`, (index, collection, query, changes, opts) => {
782
827
  this.cache.assertCollectionExists(index, collection);
783
828
  this.cache.assertCollectionWritable(index, collection);
784
- return this.client.bulkUpdateByQuery(index, collection, query, changes, opts);
829
+ assertCapability(this.capabilities, this.driver, "bulkUpdateByQuery");
830
+ return this.driver.bulkUpdateByQuery(index, collection, query, changes, opts);
785
831
  });
786
832
  /**
787
833
  * Applies a partial update to an existing document.
@@ -797,7 +843,7 @@ class ClientAdapter {
797
843
  global.kuzzle.onAsk(`core:storage:${this.scope}:document:upsert`, (index, collection, id, content, opts) => {
798
844
  this.cache.assertCollectionExists(index, collection);
799
845
  this.cache.assertCollectionWritable(index, collection);
800
- return this.client.upsert(index, collection, id, content, opts);
846
+ return this.driver.upsert(index, collection, id, content, opts);
801
847
  });
802
848
  }
803
849
  registerMappingEvents() {
@@ -812,7 +858,7 @@ class ClientAdapter {
812
858
  */
813
859
  global.kuzzle.onAsk(`core:storage:${this.scope}:mappings:get`, (index, collection, opts) => {
814
860
  this.cache.assertCollectionExists(index, collection);
815
- return this.client.getMapping(index, collection, opts);
861
+ return this.driver.getMapping(index, collection, opts);
816
862
  });
817
863
  /**
818
864
  * Import mappings as fixtures. Create non-existing indexes and collections
@@ -834,7 +880,8 @@ class ClientAdapter {
834
880
  global.kuzzle.onAsk(`core:storage:${this.scope}:mappings:update`, (index, collection, mappings) => {
835
881
  this.cache.assertCollectionExists(index, collection);
836
882
  this.cache.assertCollectionWritable(index, collection);
837
- return this.client.updateMapping(index, collection, mappings);
883
+ assertCapability(this.capabilities, this.driver, "updateMapping");
884
+ return this.driver.updateMapping(index, collection, mappings);
838
885
  });
839
886
  }
840
887
  registerLockEvents() {
@@ -929,7 +976,12 @@ class ClientAdapter {
929
976
  for (const [collection, payload] of Object.entries(collections)) {
930
977
  this.cache.assertCollectionExists(index, collection);
931
978
  this.cache.assertCollectionWritable(index, collection);
932
- const { errors } = await this.client.import(index, collection, payload, { refresh });
979
+ const imported = this.driver.import(index, collection, payload, {
980
+ refresh,
981
+ });
982
+ // One collection at a time: the first failure stops the load before
983
+ // the next collection is written.
984
+ const { errors } = await imported; // NOSONAR
933
985
  if (errors.length > 0) {
934
986
  throw servicesError.get("import_failed", errors);
935
987
  }
@@ -988,7 +1040,7 @@ class ClientAdapter {
988
1040
  propagate: options.propagate,
989
1041
  });
990
1042
  if (options.refresh && !options.indexCacheOnly) {
991
- await this.client.refreshCollection(index, collection);
1043
+ await this.driver.refreshCollection(index, collection);
992
1044
  }
993
1045
  }
994
1046
  }
@@ -0,0 +1,3 @@
1
+ import type { ConformanceContext } from "./conformanceContext";
2
+ import type { ConformanceTestApi } from "./storageDriverConformance";
3
+ export declare function capabilityCases(test: ConformanceTestApi, context: ConformanceContext): void;
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+ /*
3
+ * Kuzzle, a backend software, self-hostable and ready to use
4
+ * to power modern apps
5
+ *
6
+ * Copyright 2015-2026 Kuzzle
7
+ * mailto: support AT kuzzle.io
8
+ * website: http://kuzzle.io
9
+ *
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * https://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ var __importDefault = (this && this.__importDefault) || function (mod) {
23
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.capabilityCases = capabilityCases;
27
+ // The cases of the optional capability groups: a group's cases are registered
28
+ // only when the driver declares it. The test runners list a suite before any
29
+ // hook runs, so the capabilities are read at registration time.
30
+ const strict_1 = __importDefault(require("node:assert/strict"));
31
+ const conformanceContext_1 = require("./conformanceContext");
32
+ const StorageDriver_1 = require("../../../types/storage/StorageDriver");
33
+ const WAIT = { refresh: "wait_for" };
34
+ const IMPLEMENTED = "a driver declaring a capability implements its methods (Kuzzle checks it when it loads the driver)";
35
+ const CASES = {
36
+ aggregations(test, context) {
37
+ test.it("answers the aggregations of a search", async () => {
38
+ const collection = await context.collection("aggregations");
39
+ await context.driver.mCreate(context.index, collection, [
40
+ { _id: "a1", body: { city: "Lyon" } },
41
+ { _id: "a2", body: { city: "Lyon" } },
42
+ { _id: "a3", body: { city: "Paris" } },
43
+ ], WAIT);
44
+ const { aggregations } = await context.driver.search({
45
+ collection,
46
+ index: context.index,
47
+ searchBody: { aggregations: { cities: { terms: { field: "city" } } } },
48
+ });
49
+ strict_1.default.deepEqual((0, conformanceContext_1.field)(aggregations, "cities.buckets"), [
50
+ { doc_count: 2, key: "Lyon" },
51
+ { doc_count: 1, key: "Paris" },
52
+ ]);
53
+ });
54
+ },
55
+ mappingUpdates(test, context) {
56
+ test.it("adds fields to the mappings of a collection", async () => {
57
+ const collection = await context.collection("mappings");
58
+ const { driver } = context;
59
+ strict_1.default.ok(driver.updateMapping && driver.updateCollection, IMPLEMENTED);
60
+ await driver.updateMapping(context.index, collection, {
61
+ properties: { country: { type: "keyword" } },
62
+ });
63
+ await driver.updateCollection(context.index, collection, {
64
+ mappings: { properties: { zip: { type: "keyword" } } },
65
+ });
66
+ const mapping = await context.driver.getMapping(context.index, collection);
67
+ strict_1.default.deepEqual((0, conformanceContext_1.field)(mapping, "properties.country"), {
68
+ type: "keyword",
69
+ });
70
+ strict_1.default.deepEqual((0, conformanceContext_1.field)(mapping, "properties.zip"), { type: "keyword" });
71
+ strict_1.default.deepEqual((0, conformanceContext_1.field)(mapping, "properties.city"), { type: "keyword" });
72
+ });
73
+ },
74
+ stats(test, context) {
75
+ test.it("answers the document count of each collection", async () => {
76
+ const collection = await context.collection("stats");
77
+ await context.driver.mCreate(context.index, collection, [{ body: {} }, { body: {} }], WAIT);
78
+ const { driver } = context;
79
+ strict_1.default.ok(driver.stats, IMPLEMENTED);
80
+ const { indexes } = await driver.stats();
81
+ const index = indexes.find(({ name }) => name === context.index);
82
+ strict_1.default.ok(index, `the stats list the index "${context.index}"`);
83
+ strict_1.default.equal(index.collections.find(({ name }) => name === collection)
84
+ ?.documentCount, 2);
85
+ });
86
+ },
87
+ updateByQuery(test, context) {
88
+ test.it("updates the documents matching a query", async () => {
89
+ const collection = await context.collection("update-by-query");
90
+ await context.driver.mCreate(context.index, collection, [
91
+ { _id: "u1", body: { city: "Lyon" } },
92
+ { _id: "u2", body: { city: "Lyon" } },
93
+ { _id: "u3", body: { city: "Paris" } },
94
+ ], WAIT);
95
+ const { driver } = context;
96
+ strict_1.default.ok(driver.updateByQuery && driver.bulkUpdateByQuery, IMPLEMENTED);
97
+ const { errors, successes } = await driver.updateByQuery(context.index, collection, { term: { city: "Lyon" } }, { name: "updated" }, WAIT);
98
+ strict_1.default.deepEqual(errors, []);
99
+ strict_1.default.deepEqual(successes
100
+ .map(({ _id }) => String(_id))
101
+ .sort((a, b) => a.localeCompare(b)), ["u1", "u2"]);
102
+ const { updated } = await driver.bulkUpdateByQuery(context.index, collection, { term: { city: "Paris" } }, { name: "bulk-updated" }, { refresh: true });
103
+ strict_1.default.equal(updated, 1);
104
+ const { items } = await context.driver.mGet(context.index, collection, [
105
+ "u1",
106
+ "u3",
107
+ ]);
108
+ strict_1.default.deepEqual(items.map(({ _source }) => (0, conformanceContext_1.field)(_source, "name")), ["updated", "bulk-updated"]);
109
+ });
110
+ },
111
+ };
112
+ function capabilityCases(test, context) {
113
+ for (const capability of StorageDriver_1.STORAGE_CAPABILITIES) {
114
+ if (context.capabilities.has(capability)) {
115
+ test.describe(`capability: ${capability}`, () => {
116
+ CASES[capability](test, context);
117
+ });
118
+ }
119
+ }
120
+ }
121
+ //# sourceMappingURL=capabilityCases.js.map
@@ -0,0 +1,33 @@
1
+ import type { JSONObject } from "../../../types/JSONObject";
2
+ import type { StorageCapability, StorageDriver } from "../../../types/storage/StorageDriver";
3
+ /** What the conformance cases share: the drivers under test and their index */
4
+ export declare class ConformanceContext {
5
+ /** The index the kit creates in the public scope, then deletes */
6
+ readonly index: string;
7
+ readonly capabilities: ReadonlySet<StorageCapability>;
8
+ private publicInstance?;
9
+ private privateInstance?;
10
+ private collectionCount;
11
+ constructor(
12
+ /** The index the kit creates in the public scope, then deletes */
13
+ index: string, capabilities: ReadonlySet<StorageCapability>);
14
+ /** The public scope's driver instance, set before any case runs */
15
+ get driver(): StorageDriver;
16
+ /** The private scope's driver instance */
17
+ get privateDriver(): StorageDriver;
18
+ setDrivers(publicDriver: StorageDriver, privateDriver: StorageDriver): void;
19
+ /** A name no other case of this run uses */
20
+ uniqueName(prefix: string): string;
21
+ /** Creates a collection of the kit's index, with the kit's mappings */
22
+ collection(prefix: string): Promise<string>;
23
+ }
24
+ /** The mappings of the collections the kit creates */
25
+ export declare const CONFORMANCE_MAPPINGS: JSONObject;
26
+ /**
27
+ * Asserts that `promise` rejects with the Kuzzle error `expected`: its id, or
28
+ * its HTTP status
29
+ */
30
+ export declare function rejectsWith(promise: Promise<unknown>, expected: string | number): Promise<void>;
31
+ /** A field of a document or an answer, by its dotted path */
32
+ export declare function field(object: unknown, path: string): unknown;
33
+ export declare function hasField(object: unknown, path: string): boolean;