{"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install redux-database"],"cli":null},"imports":["import { DB } from 'redux-database';","import { emptyTable } from 'redux-database';","import { createDatabaseReducer } from 'redux-database';","import type { DataTable } from 'redux-database';"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}