meg.db is a lightweight, fast, and efficient BSON (Binary JSON) and JSON database module for JavaScript. It provides a simple interface to store and retrieve data using BSON files. The module is designed to be efficient, ensuring optimal performance for your database operations.
You can install meg.db using npm:
npm install meg.db
To use meg.db in your JavaScript project, import the necessary classes and instantiate a BSONProvider
object with the path to your BSON file:
import { BSONProvider } from "meg.db";
const db = new BSONProvider('./megdb.bson');
or you can use JSONProvider
to use JSON based database.
import { JSONProvider } from "meg.db";
const db = new JSONProvider('./megdb.json');
or you can use YAMLProvider
to use YAML based database.
import { YAMLProvider } from "meg.db";
const db = new YAMLProvider('./megdb.yaml');
or you can use NBTProvider
to use NBT based database.
import { NBTProvider } from "meg.db";
const db = new YAMLProvider('./megdb.nbt');
You can set and retrieve key-value pairs using the set
and get
methods:
db.set('key1', 'value1');
console.log(db.get('key1')); // Output: value1
db.push('array1', ["hi", "hello", 1, null, true ])
console.log(db.get('array1')); // Output: ["hi", "hello", 1, null, true]
db.pull('array1', "hello");
console.log(db.get('array1')); // Output: ["hi", 1, null, true]
// And more...
- Lightweight: Designed to have a minimal footprint and low resource usage.
- Fast: Optimized for efficient data storage and retrieval operations.
- Efficient: Utilizes efficient algorithms and data structures for optimal performance.
- Easy to use: Provides a simple and intuitive interface for working with the database.
- ESM support: Supports ES modules for modern JavaScript development.
Module also supports optional schema validation using the BSONSchema
class. You can define a schema to enforce a specific structure and data types for a collection, but it is not mandatory.
import { BSONprovider, BSONSchema } from "meg.db";
const db = new BSONProvider('./data.bson');
const userSchema = new BSONSchema('./data.bson', {
name: { type: 'string', required: true },
age: { type: 'number', required: true },
email: { type: 'string', required: false },
});
db.setSchema('user', userSchema);
db.set('user', { name: 'John Doe', age: 30, email: 'johndoe@example.com' });
console.log(db.get('user'));
If a schema is defined, meg.db will validate the data against the schema when setting values. If the data does not conform to the schema, an error will be thrown. However, if no schema is defined, no validation will be performed.
This module is open source and available under the MIT License.
Contributions are welcome! If you find any issues or have suggestions for improvement, please open an issue or submit a pull request on the GitHub repository.