TaffyDB In-Memory JavaScript Database
TaffyDB is an open-source JavaScript library that provides an in-memory database solution for both browser and Node.js environments. It allows developers to store and manipulate JSON data as 'tables' using a uniform, SQL-like interface. Currently at version 2.7.3, the library emphasizes stability and reliability, having been used in production for many years. It differentiates itself by offering a JavaScript-centric data selection engine with features like insert, update, unique, and count, all while being compatible with various DOM libraries and modern browsers (IE9+, FF3+, Safari 5+, Chrome 1.0+) and Node.js 0.10+. While stable, the project's focus appears to be on expanding regression test coverage rather than rapid feature development, suggesting a maintenance-oriented release cadence.
Common errors
-
TypeError: taffy is not a function
cause Attempting to call `require('taffy')` directly as a function in Node.js.fixYou need to access the `taffy` property from the required object: `const TAFFY = require('taffy').taffy;` -
SyntaxError: Cannot use import statement outside a module
cause Attempting to use `import TAFFY from 'taffy';` in a CommonJS-only or non-module context.fixTaffyDB does not provide an ESM export. Use `const TAFFY = require('taffy').taffy;` in Node.js or load it via a `<script>` tag in the browser and use the global `TAFFY` variable. -
TAFFY is not defined
cause Trying to use `TAFFY` in a browser environment without including the `taffy.js` script or before the script has loaded.fixEnsure you have `<script src="path/to/taffy.js"></script>` in your HTML before attempting to use the `TAFFY` global function.
Warnings
- gotcha When using TaffyDB in Node.js, you must specifically access the `taffy` function from the `require('taffy')` object (e.g., `require('taffy').taffy`). Requiring the package directly without `.taffy` will not return the database constructor.
- gotcha TaffyDB is a legacy library that does not provide native ES Module (ESM) exports. Attempting to use `import TAFFY from 'taffy';` or similar ESM syntax will result in errors in modern JavaScript environments that enforce ESM.
- gotcha TaffyDB is an in-memory database, meaning all data is stored in the application's RAM. It does not provide persistence out-of-the-box. If the application restarts or the browser tab is closed, all data will be lost unless manually saved and loaded.
- gotcha The documentation mentions compatibility with Node.js 0.10+. While the library is stable, its support for modern Node.js features (e.g., async/await, streams, newer event loop patterns) may be limited, and it might not be actively tested against the latest Node.js LTS versions.
Install
-
npm install taffydb -
yarn add taffydb -
pnpm add taffydb
Imports
- TAFFY
const TAFFY = require('taffy');const TAFFY = require('taffy').taffy; - TAFFY
import TAFFY from 'taffy';
<!-- Add <script src="taffy.js"></script> in HTML --> // TAFFY is now a global function
Quickstart
const TAFFY = require('taffy').taffy;
const product_db = TAFFY([
{
"item": 1,
"name": "Blue Ray Player",
"price": 99.99
},
{
"item": 2,
"name": "3D TV",
"price": 1799.99
},
{
"item": 3,
"name": "Soundbar",
"price": 149.99
}
]);
console.log('All products:', product_db().get());
// Query for items with price less than 150
const lowPricedItems = product_db({price: {lt: 150}}).get();
console.log('Low priced items (price < 150):', lowPricedItems);
// Update the price of an item
product_db({item: 1}).update({price: 89.99});
console.log('Updated Blue Ray Player:', product_db({item: 1}).first());
// Select only names
const productNames = product_db().select("name");
console.log('Product names:', productNames);