{"id":16518,"library":"redux-database","title":"Redux Database","description":"Redux Database is a JavaScript library providing an in-memory, reducer-based relational database for client-side state management. It allows developers to organize application state in a structured, relational manner, similar to an SQL database, complete with queries and joins. The library ships with strong TypeScript typings, ensuring type safety for schema definitions, queries, and data manipulation. It supports immutable documents and offers plugins for seamless integration with Redux stores or can be used standalone, notably having no direct dependencies itself. Currently at version 0.0.20, its release cadence appears to be incremental, focusing on feature development and refinement rather than rapid major version changes. A key differentiator is its focus on client-side data normalization, offering a chainable query syntax, embedded relations for joins, and transaction support to optimize state updates and prevent performance issues from excessive dispatches. It aims to simplify complex client-side data challenges by providing a structured, queryable layer over the Redux state.","status":"active","version":"0.0.20","language":"javascript","source_language":"en","source_url":"https://github.com/nerdgeschoss/redux-database","tags":["javascript","typescript"],"install":[{"cmd":"npm install redux-database","lang":"bash","label":"npm"},{"cmd":"yarn add redux-database","lang":"bash","label":"yarn"},{"cmd":"pnpm add redux-database","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary class for interacting with the database state.","wrong":"const { DB } = require('redux-database');","symbol":"DB","correct":"import { DB } from 'redux-database';"},{"note":"Utility constant for initializing an empty data table in your schema.","wrong":"const emptyTable = require('redux-database').emptyTable;","symbol":"emptyTable","correct":"import { emptyTable } from 'redux-database';"},{"note":"The function to create a Redux reducer that handles database actions. It's a named export, not a default.","wrong":"import { databaseReducer } from 'redux-database';","symbol":"createDatabaseReducer","correct":"import { createDatabaseReducer } from 'redux-database';"},{"note":"This is a TypeScript type definition, imported using 'import type'.","symbol":"DataTable","correct":"import type { DataTable } from 'redux-database';"}],"quickstart":{"code":"import { createStore, combineReducers } from 'redux';\nimport { DB, emptyTable, createDatabaseReducer } from 'redux-database';\n\ninterface Item { id: string; name: string; categoryId?: string; }\ninterface Category { id: string; name: string; }\n\ninterface AppState {\n  settings: { enableFeatureX: boolean; };\n  data: {\n    items: DataTable<Item>;\n    categories: DataTable<Category>;\n  };\n}\n\nconst initialState: AppState = {\n  settings: {\n    enableFeatureX: true\n  },\n  data: {\n    items: emptyTable,\n    categories: emptyTable\n  }\n};\n\n// Create the database reducer\nconst databaseReducer = createDatabaseReducer(initialState);\n\n// Combine with other Redux reducers if any\nconst rootReducer = combineReducers({\n  db: databaseReducer\n});\n\nconst store = createStore(rootReducer);\n\n// Get a DB instance from the current state\nlet db = new DB(store.getState().db);\n\n// Example: Writing data using a transaction\nstore.dispatch(\n  db.transaction((dispatch) => {\n    dispatch(db.table('categories').insert({ id: 'cat1', name: 'Electronics' }));\n    dispatch(db.table('items').insert({ id: 'item1', name: 'Laptop', categoryId: 'cat1' }));\n    dispatch(db.table('items').insert({ id: 'item2', name: 'Mouse', categoryId: 'cat1' }));\n    dispatch(db.set('enableFeatureX', false));\n  })\n);\n\n// Refresh DB instance after state update\ndb = new DB(store.getState().db);\n\n// Example: Reading data\nconsole.log('Feature X enabled:', db.get('enableFeatureX'));\n\nconst allItems = db.table('items').all;\nconsole.log('All Items:', allItems);\n\nconst electronicsItems = db\n  .query('items')\n  .where({ categoryId: 'cat1' })\n  .embed('category', 'categories', 'categoryId')\n  .all;\n\nconsole.log('Electronics Items with Category:', electronicsItems);\n\n// Update an item\nstore.dispatch(db.table('items').update('item1', { name: 'Gaming Laptop' }));\n\ndb = new DB(store.getState().db);\nconsole.log('Updated Laptop:', db.table('items').find('item1'));\n","lang":"typescript","description":"Demonstrates defining a database schema, initializing a Redux store with the database reducer, performing transactional writes, and executing chainable queries including embedded relations."},"warnings":[{"fix":"Always wrap multiple write operations within a transaction using `db.transaction((dispatch) => { ... })` to ensure a single Redux action is dispatched for all changes, optimizing performance.","message":"Directly dispatching many individual write actions (insert, update, delete) can lead to significant performance degradation in UIs that observe state mutations frequently, as each dispatch triggers listeners.","severity":"gotcha","affected_versions":">=0.0.1"},{"fix":"Always use the provided write methods (e.g., `db.table('items').insert()`, `update()`, `delete()`, `db.set()`) on `DB` or table instances. These methods return Redux actions that must be dispatched to correctly update the immutable state.","message":"Although `redux-database` integrates with Redux, it operates with immutable state internally. Direct mutations to objects retrieved from a `DB` instance (e.g., `db.table('items').find('id')`) will not be reflected in the Redux store and can lead to desynchronization.","severity":"gotcha","affected_versions":">=0.0.1"},{"fix":"After any state update that affects the database slice of your Redux store, create a new `DB` instance from the updated store state: `db = new DB(store.getState().db);`.","message":"The `DB` instance represents a snapshot of the database state at the time of its creation. If the underlying Redux store's database state changes (e.g., after a dispatch), the existing `DB` instance will be stale and will not reflect the latest data.","severity":"gotcha","affected_versions":">=0.0.1"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure your database schema (`interface State`) correctly defines all tables under the `data` key and settings under the `settings` key before attempting to access them via `db.table('xyz')` or `db.get('xyz')`.","cause":"Attempting to access a table or setting that is not defined in the database schema (`interface State`). TypeScript correctly identifies this as a type error.","error":"Property 'xyz' does not exist on type 'DataTables<State>'"},{"fix":"Ensure that any object passed to `insert()` or `update()` methods strictly conforms to the TypeScript interface defined for that table's model, including all required properties like `id`.","cause":"Attempting to insert a new record into a table without providing the required `id` property, assuming the model interface requires it.","error":"Argument of type '{ name: string; }' is not assignable to parameter of type 'Item'. Property 'id' is missing in type '{ name: string; }' but required in type 'Item'."}],"ecosystem":"npm"}