Node.js requirement
NyaDB supports Node.js >=12.x. It is published as a CommonJS package and can be required directly from Node projects.
Node.js JSON storage
A tiny file-backed database for projects that need readable JSON files, simple methods, and safer defaults without adding a database server.
npm install @decaded/nyadb
{
"ada": {
"role": "admin",
"active": true
},
"linus": {
"role": "maintainer",
"active": true
}
}
Install
NyaDB supports Node.js >=12.x. It is published as a CommonJS package and can be required directly from Node projects.
Every database is stored as a separate JSON file inside a NyaDB folder in your project's root directory.
Creating a new instance applies configuration, prepares the storage folder, and loads the available database files.
npm install @decaded/nyadb
const NyaDB = require('@decaded/nyadb');
const nyadb = new NyaDB();
Use cases
Quick start
const NyaDB = require('@decaded/nyadb');
const nyadb = new NyaDB();
nyadb.create('users');
nyadb.set('users', {
ada: { role: 'admin', active: true },
linus: { role: 'maintainer', active: true },
});
console.log(nyadb.get('users'));
{
"ada": {
"role": "admin",
"active": true
},
"linus": {
"role": "maintainer",
"active": true
}
}
Tutorial
create returns true when the database is created and false if it already exists or fails validation.
nyadb.create('test');
Use plain serializable objects. With validation enabled, circular data is rejected before it can be written.
const mockDatabase = {
yellow: ['banana', 'citrus'],
red: ['apple', 'paprika'],
};
nyadb.set('test', mockDatabase);
get returns the object for an existing database, while getList returns every database name.
nyadb.get('test');
// {
// yellow: ['banana', 'citrus'],
// red: ['apple', 'paprika'],
// }
nyadb.getList();
// ['test']
exists returns true when the database file is available and false otherwise.
if (nyadb.exists('test')) {
console.log('Database exists!');
}
clear keeps the file and resets it to {}. rename returns false if the source is missing or the destination exists.
nyadb.clear('test');
nyadb.rename('test', 'newName');
delete removes the database file and returns whether the operation succeeded.
nyadb.delete('test');
Recipes
nyadb.create('settings');
nyadb.set('settings', {
theme: 'dark',
notifications: true,
});
const settings = nyadb.get('settings');
nyadb.create('features');
nyadb.set('features', {
newDashboard: false,
betaTools: true,
});
const flags = nyadb.get('features');
nyadb.create('cache');
nyadb.set('cache', {
usersUpdatedAt: Date.now(),
users: [],
});
const status = nyadb.sizeStatus('cache');
Playground
API
Creates a database file when it does not already exist.
Writes JSON data and batches rapid updates with the debounce setting.
Returns the database object, or false when the name is missing.
Lists available database names from the project-local NyaDB folder.
Reports bytes and formatted file sizes for one, many, or all databases.
Returns status labels such as ok, warning, and critical.
Preserves the file while resetting the database contents to {}.
Moves a database to a new valid name when that destination is free.
Deletes an existing database file and returns false when it cannot be removed.
Checks whether a database is currently loaded by name.
Sizes
const size = nyadb.size('test');
console.log(size);
// { name: 'test', bytes: 1234, formatted: '1.21 KB' }
const sizes = nyadb.size(['test', 'users']);
console.log(sizes.total.formatted);
console.log(sizes.databases.test.formatted);
const allSizes = nyadb.size();
console.log(allSizes.databases);
const status = nyadb.sizeStatus('test');
console.log(status);
// {
// name: 'test',
// bytes: 1234,
// formatted: '1.21 KB',
// percentOfLimit: 0.01,
// status: 'ok'
// }
const allStatuses = nyadb.sizeStatus();
console.log(allStatuses.total.status);
// 'ok', 'warning', 'grace', 'critical', or 'unknown'
Configuration
| Setting | Default | Options | Purpose |
|---|---|---|---|
formattingEnabled |
true |
false |
Enables or disables formatting of stored JSON output. |
formattingStyle |
tab |
space |
Chooses tab or space indentation for stored JSON. |
indentSize |
4 |
Any non-negative integer | Sets space indentation size when formattingStyle is space. |
encoding |
utf8 |
Any valid Node.js encoding | Controls file input and output encoding. |
enableConsoleLogs |
false |
true |
Enables console logging. Errors are logged regardless of this setting. |
logLevel |
warn |
error, warn, info, debug |
Filters console output when logs are enabled. Priority is error, warn, info, debug. |
validateInput |
true |
false |
Validates names and data before file operations. |
useAtomicWrites |
true |
false |
Writes through a temporary file before rename. |
maxFileSize |
100 |
Any non-negative integer | Tracks file usage against a megabyte limit. |
writeDebounce |
10 |
Any non-negative integer | Batches rapid write operations in milliseconds. |
const nyadb = new NyaDB({
formattingStyle: 'space',
indentSize: 5,
enableConsoleLogs: true,
logLevel: 'info',
});
Changes apply when the instance initializes. To return to the default values, remove the setting from your config object.
useAtomicWrites requires validateInput: true. Setting atomic writes to true while validation is false throws a configuration error.
Security
const nyadb = new NyaDB();
// validateInput: true
// useAtomicWrites: true
const nyadb = new NyaDB({
validateInput: false,
useAtomicWrites: false,
});
Disabling validateInput may expose your application to path traversal vulnerabilities. Only disable it when you fully control every database name.
Migration
Version 5.0.0 is a major release. validateInput and useAtomicWrites now default to true.
Valid names using letters, numbers, dots, dashes, or underscores continue to work without changes.
Names with path separators or parent directory references should be renamed. For example, replace ../data/users with users.
If those old JSON files already exist and you want to keep using them, move them manually into the project NyaDB folder with the new valid file names.
// Before
nyadb.create('../data/users');
// After
nyadb.create('users');
NyaDB version 4+ stores each database as its own JSON file. Older database.json storage is automatically split into multiple files.
This migration is one-way. The original file is backed up as database_backup.json inside the NyaDB folder, but creating your own backup before upgrading is recommended.