Dexie.use()

Syntax

db.use({stack, name?, create})

Properties of the parameter

stack : String Stack type. Currently, only "dbcore" is supported.
name : String Optional name of your middleware
create : Function The middlware itself. It takes a DBCore instance and should return a modified DBCore instance.

Remarks

Your provided create function takes a DBCore and should return another plain JS object confirming to the DBCore interface.

Example

import Dexie from 'dexie';

const db = new Dexie('dbname');

db.use({
  stack: "dbcore", // The only stack supported so far.
  name: "MyMiddleware", // Optional name of your middleware
  create (downlevelDatabase) {
    // Return your own implementation of DBCore:
    return {
      // Copy default implementation.
      ...downlevelDatabase, 
      // Override table method
      table (tableName) {
        // Call default table method
        const downlevelTable = downlevelDatabase.table(tableName);
        // Derive your own table from it:
        return {
          // Copy default table implementation:
          ...downlevelTable,
          // Override the mutate method:
          mutate: req => {
            // Copy the request object
            const myRequest = {...req};
            // Do things before mutate, then
            // call downlevel mutate:
            return downlevelTable.mutate(myRequest).then(res => {
              // Do things after mutate
              const myResponse = {...res};
              // Then return your response:
              return myResponse;
            });
          }
        }
      }
    };
  }
});

In essence, all mutating operations are bulk-oriented. Theres only bulkPut(), bulkAdd(), bulkDelete() and deleteRange(). Currently all of these four are reached through a single method mutate().

Interface definitions for DBCore is found here

In this version, the CRUD hooks are called from a built-in middleware (hooksMiddleware): A middleware that makes sure to call CRUD hooks in a backward compatible manner.

In Dexie 3, compound indexes can be used partially as if the first part was ha plain index. This is also accomplished via a DBCore middleware VirtualIndex.

Table of Contents