Jsonjsdb - Client-side Relational Database
Jsonjsdb is a client-side relational database library specifically designed for static Single Page Applications. It enables offline data storage and querying capabilities, functioning effectively whether the application is run locally via the `file://` protocol or served over HTTP/HTTPS (e.g., localhost or production servers). The current stable JavaScript version is 0.8.10, with the project showing a somewhat active release cadence, although many recent updates have focused on its Python counterpart. A key differentiator is its unique approach to handling database file extensions (`.json.js` for `file://` and `.json` for `http(s)://`) based on the protocol, and its reliance on HTML-embedded configuration, which distinguishes it from more traditional pure JavaScript database solutions.
Common errors
-
Failed to load resource: net::ERR_FILE_NOT_FOUND (or similar HTTP 404 for JSON files)
cause The browser is attempting to load `.json` files over the `file://` protocol (e.g., when opening `index.html` directly), but the database expects `.json.js` files in this environment.fixEnsure your database files are named with a `.json.js` extension (e.g., `users.json.js`) when running locally via `file://`. For HTTP/HTTPS, they should be `.json`. -
TypeError: db.init is not a function (or: Cannot read properties of undefined (reading 'init'))
cause The `db` instance was not correctly created, or `init()` was called on an undefined variable. More commonly, if using ESM, `require('jsonjsdb')` was used instead of `import Jsonjsdb from 'jsonjsdb'`.fixVerify that `import Jsonjsdb from 'jsonjsdb'` is used for ES Modules, and that `const db = new Jsonjsdb()` successfully creates an instance before calling `db.init()`. -
Uncaught (in promise) Error: Table 'tableName' not found in database.
cause The specified table name does not exist, or the `__table__.json` (or `__table__.json.js`) file, which lists all tables, is missing or incorrectly formatted.fixCheck the `_db_` folder (or configured `data-path`) for the `__table__.json` (or `__table__.json.js`) file and ensure it correctly lists all available table names. Also, verify that the table data files themselves are present and correctly named.
Warnings
- gotcha When running your application locally via the `file://` protocol, database table files must have a `.json.js` extension (e.g., `users.json.js`). When served over HTTP/HTTPS, they must have a `.json` extension (e.g., `users.json`). Failure to use the correct extension for the protocol will result in database loading errors.
- gotcha The library primarily uses HTML-embedded configuration via a `div` element with `id="jsonjsdb-config"` and `data-` attributes. Developers accustomed to pure JavaScript configuration might overlook this, leading to unexpected default behavior or configuration issues.
- breaking While no specific breaking changes for the JavaScript core were detailed in the provided changelog, minor version increments in pre-1.0 software (like `0.x` to `0.8.x`) often introduce breaking changes in APIs, configuration, or expected behavior without extensive migration guides.
Install
-
npm install jsonjsdb -
yarn add jsonjsdb -
pnpm add jsonjsdb
Imports
- Jsonjsdb
const Jsonjsdb = require('jsonjsdb')import Jsonjsdb from 'jsonjsdb'
- JsonjsdbConfig
import type { JsonjsdbConfig } from 'jsonjsdb' - init
db.init()
await db.init()
Quickstart
import Jsonjsdb from 'jsonjsdb';
async function initializeAndQueryDb() {
const db = new Jsonjsdb();
try {
// Initialize the database. This reads the __table__.json(.js) and table data.
await db.init();
// Example: Get all entries from a 'user' table
const users = db.getAll('user');
console.log('All users:', users);
// Example: Get a specific user by ID (assuming 'user' table has IDs like 123)
const specificUser = db.get('user', 123);
console.log('Specific user (ID 123):', specificUser);
// You can also demonstrate iteration or checking existence
db.foreach('product', (product) => {
console.log('Product found:', product.name);
});
if (db.exists('settings', 'app_name')) {
console.log('App name setting exists.');
}
} catch (error) {
console.error('Failed to initialize or query database:', error);
// Depending on the error, provide user guidance. E.g., check 'db' folder, config.
}
}
initializeAndQueryDb();