Wio.db
wio.db is a Turkish-origin, human-readable database module for Node.js, currently in version 4.0.22. It serves as a lightweight, file-based key-value store, offering an alternative to packages like quick.db, and is frequently used in discord.js bot development. The library provides distinct implementations for JSON and YAML file formats, allowing developers to choose based on their data persistence preferences. While a specific release cadence is not explicitly stated, the project appears actively maintained with recent updates and bug fixes, indicated by its 4.x versioning. Key features include intuitive data manipulation methods for setting, getting, deleting, and querying data, alongside array and mathematical operations directly on stored values. It differentiates itself by emphasizing a human-readable storage format and offering both JSON and YAML options, rather than relying solely on a single serialization method.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use CommonJS `require()` syntax in an ECMAScript Module (ESM) file (e.g., a `.mjs` file or a project with `"type": "module"` in `package.json`).fixChange your import statement to use ESM syntax: `import { JsonDatabase, YamlDatabase } from 'wio.db';` -
TypeError: JsonDatabase is not a constructor
cause You are calling `JsonDatabase()` or `YamlDatabase()` without the `new` keyword.fixAlways instantiate database classes with `new`: `const db = new JsonDatabase({ databasePath: './data.json' });` -
Error: Database path is required
cause The `databasePath` option was omitted or left undefined when initializing `JsonDatabase` or `YamlDatabase`.fixProvide a valid string path for your database file: `const db = new JsonDatabase({ databasePath: './my-database.json' });`
Warnings
- gotcha Wio.db requires Node.js version 12 or greater to function correctly. Running on older Node.js versions may lead to unexpected errors or stability issues.
- breaking The `<db>.arrayHasValue` method has been removed in Wio.db v4. This functionality is no longer available.
- breaking The static property `JsonDatabase.DBCollection` has been removed in Wio.db v4. This property is no longer accessible.
- breaking The `<db>.totalDBSize` property has been removed in Wio.db v4. You can no longer directly access the total database size via this property.
Install
-
npm install wio.db -
yarn add wio.db -
pnpm add wio.db
Imports
- JsonDatabase
import wiodb from 'wio.db'; // No default export
import { JsonDatabase } from 'wio.db'; - YamlDatabase
const YamlDatabase = require('wio.db').YamlDatabase; // Direct access is less idiomatic for multiple exportsconst { YamlDatabase } = require('wio.db'); - All exports
import { Database } from 'wio.db'; // 'Database' is not an exported symbolimport { JsonDatabase, YamlDatabase } from 'wio.db';
Quickstart
import { JsonDatabase, YamlDatabase } from 'wio.db';
// Initialize a JSON database instance
const db = new JsonDatabase({
databasePath: './databases/myJsonDatabase.json'
});
// Initialize a YAML database instance
const yamldb = new YamlDatabase({
databasePath: './databases/myYamlDatabase.yml'
});
// --- JSON Database Operations ---
// Set a value
db.set('user_settings.theme', 'dark');
console.log("Set 'user_settings.theme' to 'dark'");
// Get a value
const theme = db.get('user_settings.theme');
console.log(`Retrieved theme: ${theme}`); // Expected: dark
// Check if a key exists
const hasSetting = db.has('user_settings.theme');
console.log(`Does 'user_settings.theme' exist? ${hasSetting}`); // Expected: true
// Push to an array
db.push('recent_logins', 'user123');
db.push('recent_logins', 'admin456');
console.log(`Recent logins: ${db.get('recent_logins')}`); // Expected: [ 'user123', 'admin456' ]
// Add a number
db.add('user_points', 100);
db.add('user_points', 50);
console.log(`User points: ${db.get('user_points')}`); // Expected: 150
// Get all data (top 5 entries)
const allData = db.all(5);
console.log('All JSON DB data (top 5):', allData);
// Delete a key
db.delete('user_settings.theme');
console.log("Deleted 'user_settings.theme'");
console.log(`Does 'user_settings.theme' exist after delete? ${db.has('user_settings.theme')}`); // Expected: false
// Get DB info
console.log('JSON DB Info:', db.info);
// --- YAML Database Operations (similar) ---
yamldb.set('config.port', 3000);
console.log(`YAML config port: ${yamldb.get('config.port')}`);
// To destroy databases and clean up files (uncomment to run)
// db.destroy();
// yamldb.destroy();