Dexie Logger Middleware
raw JSON →Dexie Logger is a middleware for Dexie.js, a wrapper for IndexedDB, designed to provide enhanced debugging capabilities by logging database operations. Currently at version 1.2.6, it offers detailed insights into database interactions such as `mutate`, `get`, `query`, and `openCursor`. While a specific release cadence isn't published, the project includes future development ideas, indicating an active maintenance state. Its key differentiator lies in its deep integration with Dexie, allowing developers to filter logs based on specific tables and operation types using either a whitelist or a blacklist approach. This makes it a valuable tool for isolating and debugging complex data flow issues within Dexie-backed applications, improving development efficiency and understanding of IndexedDB transactions.
Common errors
error TypeError: db.use is not a function ↓
const db = new Dexie("MyDatabase"); and that you are calling db.use() on this instance. error Logs not appearing in console (but operations are succeeding) ↓
db.use(logger({ ... })). Temporarily remove all filters or broaden them to confirm the logger is working, then progressively re-apply filters to identify the specific rule preventing logs. error ReferenceError: logger is not defined ↓
dexie-logger is primarily ESM-first). Warnings
gotcha When configuring the logger, ensure you use either a whitelist OR a blacklist for tables and operations, but not both simultaneously for the same type. For example, using both `tableWhiteList` and `tablesBlackList` can lead to unexpected logging behavior as the options are mutually exclusive. ↓
gotcha Applying the logger middleware can introduce a slight performance overhead, especially in applications with very high-frequency database operations or large data volumes. While generally negligible for typical debugging, be mindful of its impact in performance-critical sections. ↓
Install
npm install dexie-logger yarn add dexie-logger pnpm add dexie-logger Imports
- logger wrong
const logger = require('dexie-logger');correctimport logger from 'dexie-logger';
Quickstart
import Dexie, { Table } from 'dexie';
import logger from 'dexie-logger';
// Define your database schema
interface Event {
id?: number;
name: string;
timestamp: Date;
}
interface User {
id?: number;
name: string;
email: string;
}
class MyDatabase extends Dexie {
events!: Table<Event>;
users!: Table<User>;
constructor() {
super("MyDebuggerDatabase");
this.version(1).stores({
events: "++id, name, timestamp",
users: "++id, name, email"
});
}
}
const db = new MyDatabase();
// Apply the logger middleware with specific filters
db.use(
logger({
tableWhiteList: ["events"], // Only log operations on the 'events' table
operationsWhiteList: ["mutate", "get"], // Only log 'mutate' (add/update/delete) and 'get' operations
})
);
async function runExample() {
try {
console.log("Applying Dexie Logger middleware...");
// Perform some operations to trigger logs
await db.events.add({ name: "App Started", timestamp: new Date() });
await db.events.add({ name: "User Session Begin", timestamp: new Date() });
await db.users.add({ name: "John Doe", email: "john@example.com" }); // This operation on 'users' table will NOT be logged due to tableWhiteList
const firstEvent = await db.events.get(1); // This 'get' operation will be logged
console.log("Retrieved event:", firstEvent);
await db.events.put({ id: firstEvent?.id, name: "App Updated", timestamp: new Date() }); // This 'mutate' operation will be logged
console.log("Check your browser's developer console for Dexie Logger output.");
} catch (error) {
console.error("Database operation failed:", error);
}
}
runExample();