Registry / database / redux-database

redux-database

JSON →
library0.0.20jsnpmunverified

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.

npm install redux-database
INSTALL
IMPORT
SIG · REDUX-DATABASE
R
redux-database
databasejavascriptv0.0.20
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DB
import { DB } from 'redux-database';
const { DB } = require('redux-database');
The primary class for interacting with the database state.
emptyTable
import { emptyTable } from 'redux-database';
const emptyTable = require('redux-database').emptyTable;
Utility constant for initializing an empty data table in your schema.
createDatabaseReducer
import { createDatabaseReducer } from 'redux-database';
import { databaseReducer } from 'redux-database';
The function to create a Redux reducer that handles database actions. It's a named export, not a default.
DataTable
import type { DataTable } from 'redux-database';
This is a TypeScript type definition, imported using 'import type'.

Demonstrates defining a database schema, initializing a Redux store with the database reducer, performing transactional writes, and executing chainable queries including embedded relations.

import { createStore, combineReducers } from 'redux'; import { DB, emptyTable, createDatabaseReducer } from 'redux-database'; interface Item { id: string; name: string; categoryId?: string; } interface Category { id: string; name: string; } interface AppState { settings: { enableFeatureX: boolean; }; data: { items: DataTable<Item>; categories: DataTable<Category>; }; } const initialState: AppState = { settings: { enableFeatureX: true }, data: { items: emptyTable, categories: emptyTable } }; // Create the database reducer const databaseReducer = createDatabaseReducer(initialState); // Combine with other Redux reducers if any const rootReducer = combineReducers({ db: databaseReducer }); const store = createStore(rootReducer); // Get a DB instance from the current state let db = new DB(store.getState().db); // Example: Writing data using a transaction store.dispatch( db.transaction((dispatch) => { dispatch(db.table('categories').insert({ id: 'cat1', name: 'Electronics' })); dispatch(db.table('items').insert({ id: 'item1', name: 'Laptop', categoryId: 'cat1' })); dispatch(db.table('items').insert({ id: 'item2', name: 'Mouse', categoryId: 'cat1' })); dispatch(db.set('enableFeatureX', false)); }) ); // Refresh DB instance after state update db = new DB(store.getState().db); // Example: Reading data console.log('Feature X enabled:', db.get('enableFeatureX')); const allItems = db.table('items').all; console.log('All Items:', allItems); const electronicsItems = db .query('items') .where({ categoryId: 'cat1' }) .embed('category', 'categories', 'categoryId') .all; console.log('Electronics Items with Category:', electronicsItems); // Update an item store.dispatch(db.table('items').update('item1', { name: 'Gaming Laptop' })); db = new DB(store.getState().db); console.log('Updated Laptop:', db.table('items').find('item1'));
Debug
Known issues
gotchaDirectly 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.
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.
affects: >=0.0.1
gotchaAlthough `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.
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.
affects: >=0.0.1
gotchaThe `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.
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);`.
affects: >=0.0.1
Errors
Common errors & fixes
Property 'xyz' does not exist on type 'DataTables<State>'
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.
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')`.
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'.
Attempting to insert a new record into a table without providing the required `id` property, assuming the model interface requires it.
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`.
Upgrade
Version history
0.0.20latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
Meta
1
Resources
redux-database — npm install redux-database · libregistry