{"id":16693,"library":"tiny-memory-db","title":"Tiny Memory DB","description":"Tiny Memory DB is a zero-dependency, in-memory database library for JavaScript and TypeScript environments. Currently at version 0.4.2, it is in active development, with a release cadence that reflects its pre-1.0 status, meaning updates occur as features are implemented or bugs are addressed. Its primary differentiators are its minimal footprint and lack of external dependencies, making it ideal for lightweight data management. It provides a structured 'Table' abstraction for defining data schemas with attributes and primary keys, and supports a concise, string-based query language. Unlike persistent databases, Tiny Memory DB operates entirely in RAM, prioritizing speed and simplicity, though users can implement external mechanisms for data persistence if needed. It ships with comprehensive TypeScript types, ensuring type safety and an improved developer experience for TS projects.","status":"active","version":"0.4.2","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","typescript"],"install":[{"cmd":"npm install tiny-memory-db","lang":"bash","label":"npm"},{"cmd":"yarn add tiny-memory-db","lang":"bash","label":"yarn"},{"cmd":"pnpm add tiny-memory-db","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily uses ES Module syntax and ships TypeScript types. While CommonJS might work via transpilation, direct require is not the idiomatic approach.","wrong":"const { Table } = require('tiny-memory-db')","symbol":"Table","correct":"import { Table } from 'tiny-memory-db'"},{"note":"Leverage TypeScript generics for `Table` to ensure strong type inference and safety for your stored data objects.","wrong":"import { Table } from 'tiny-memory-db'; const table = new Table({ attrs: 'id name' }); // Lacks type safety","symbol":"Table<T>","correct":"import { Table } from 'tiny-memory-db'; type MyData = { id: number; name: string }; const table = new Table<MyData>({ attrs: 'id name', funcs: ['id ->'] });"}],"quickstart":{"code":"import { Table } from 'tiny-memory-db';\n\nasync function runExample() {\n    // Define the shape of our user data for type safety\n    type User = { id: number; name: string; email: string; createdAt: number };\n\n    // Create a new table for users, defining its attributes and a unique key\n    const usersTable = new Table<User>({\n        attrs: 'id name email createdAt',\n        funcs: ['id ->'] // 'id' is specified as a unique primary key\n    });\n\n    // Add some initial user data to the table\n    usersTable.put({ id: 1, name: 'Alice Wonderland', email: 'alice@example.com', createdAt: Date.now() - 100000 });\n    usersTable.put({ id: 2, name: 'Bob The Builder', email: 'bob@example.com', createdAt: Date.now() - 50000 });\n    usersTable.put({ id: 3, name: 'Charlie Chaplin', email: 'charlie@example.com', createdAt: Date.now() });\n    usersTable.put({ id: 4, name: 'David Jones', email: 'david@example.com', createdAt: Date.now() - 20000 });\n\n    console.log('--- All users initially stored ---');\n    // Retrieve and log all users in the table\n    for (const user of usersTable.scan()) {\n        console.log(user);\n    }\n\n    // Query for a specific user by their ID\n    console.log('\n--- User with ID 2 ---');\n    const bobResults = await usersTable.query('id=2');\n    for (const user of bobResults.scan()) {\n        console.log(user);\n    }\n\n    // Query for users whose name contains 'Alice' using a regular expression\n    console.log('\n--- Users with name containing \"Alice\" (using regex query) ---');\n    const usersContainingAlice = await usersTable.query('name~.*Alice.*');\n    for (const user of usersContainingAlice.scan()) {\n        console.log(user);\n    }\n\n    // Update an existing user's email address\n    const userToUpdate = (await usersTable.query('id=2')).scan()[0];\n    if (userToUpdate) {\n        usersTable.put({ ...userToUpdate, email: 'robert.builder@example.com' });\n    }\n    console.log('\\n--- Updated Bob\\'s email address ---');\n    const updatedBob = (await usersTable.query('id=2')).scan()[0];\n    console.log(updatedBob);\n\n    // Remove a user from the table based on their ID\n    usersTable.removeMatching('id=1');\n    console.log('\\n--- Users after deleting Alice Wonderland (ID 1) ---');\n    for (const user of usersTable.scan()) {\n        console.log(user);\n    }\n\n    // Add another new user to demonstrate continued operation\n    usersTable.put({ id: 5, name: 'Eve Adams', email: 'eve@example.com', createdAt: Date.now() + 10000 });\n    console.log('\\n--- All users after adding Eve ---');\n    for (const user of usersTable.scan()) {\n        console.log(user);\n    }\n}\n\nrunExample();","lang":"typescript","description":"This example demonstrates creating a typed in-memory table, adding data, querying by exact match and regular expression, updating records, and deleting records."},"warnings":[{"fix":"Pin the exact package version in package.json (e.g., `\"tiny-memory-db\": \"0.4.2\"`) and manually verify functionality after any update.","message":"As a pre-1.0 library (currently v0.4.2), `tiny-memory-db` may introduce breaking API changes in minor or patch releases. It is recommended to pin exact versions and review release notes carefully during upgrades.","severity":"breaking","affected_versions":"<1.0.0"},{"fix":"Monitor memory usage, paginate query results, and consider using it only for datasets that comfortably fit within available memory. For persistent storage or very large datasets, evaluate disk-backed or external database solutions.","message":"Since `tiny-memory-db` is an in-memory database, all data resides in the application's RAM. Large datasets can lead to 'Out of Memory' errors, especially in environments with limited resources (e.g., serverless functions, browser tabs).","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Implement external mechanisms for loading and saving data, such as writing table contents to a JSON file, `localStorage`, or an external database at appropriate lifecycle events (e.g., application shutdown, regular intervals).","message":"The library does not provide built-in data persistence. Any data stored in a `Table` instance is lost when the application terminates or the instance is garbage collected. This is by design for an in-memory database.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Familiarize yourself with the exact query syntax provided in the library's documentation. For highly complex filtering, consider retrieving a broader dataset using simpler queries and then applying additional filtering logic programmatically using standard JavaScript array methods.","message":"The string-based query language requires careful formatting. Complex queries or those relying on advanced operators might become cumbersome or less performant than dedicated query builders found in more feature-rich database solutions.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Reduce the amount of data stored, or increase the Node.js memory limit if applicable (`--max-old-space-size`). Re-evaluate if an in-memory solution is appropriate for your dataset size.","cause":"The in-memory database is holding too much data, exceeding the JavaScript engine's heap limit.","error":"RangeError: JavaScript heap out of memory"},{"fix":"Ensure you are using `import { Table } from 'tiny-memory-db';` with an environment configured for ESM (e.g., `\"type\": \"module\"` in package.json for Node.js, or a bundler like Webpack/Rollup).","cause":"Attempting to import `Table` using CommonJS `require()` syntax or incorrect named import when the package is primarily ESM, or module resolution issues.","error":"TypeError: Table is not a constructor"},{"fix":"After calling `table.query()`, you typically need to call `.scan()` on the returned `ResultSet` to get the iterable data. Correct usage is `(await table.query(...)).scan()`.","cause":"Incorrect usage of `Table` instance methods, often trying to iterate directly on a query result object without calling a method to get iterable data.","error":"Property 'scan' does not exist on type 'Table<T>'. Did you mean 'query'?"}],"ecosystem":"npm"}