ð meg.db is a lightweight, fast, and efficient BSON (Binary JSON), JSON, and BIN database module for JavaScript and Typescript. It provides a simple interface to store and retrieve data using files. The module is designed to be efficient, ensuring optimal performance for your database operations.
meg.db is designed to work seamlessly with Node.js v14 and above. While it is compatible with Node.js v14 and newer versions, we strongly recommend using the latest available version of Node.js to ensure you benefit from the latest features, performance enhancements, and security updates.
You can easily install meg.db using npm:
npm install meg.db
To incorporate meg.db into your JavaScript/Typescript project, import the required classes and create an instance of the desired driver, such as BSONDriver
, BINDriver
or JSONDriver
Here are a few examples:
// For ESM/Typescript
import { Megdb, BSONDriver, JSONDriver, BINDriver } from "meg.db";
// or CJS
const { Megdb, BSONDriver, JSONDriver, BINDriver } = require("meg.db");
const dbbson = new MegDB({ driver: new BSONDriver({ filePath: "./megdb.bson" }) });
const dbjson = new MegDB({ driver: new JSONDriver({ filePath: "./megdb.json" }) });
const dbbin = new Megdb({ driver: new BINDriver({ filePath: "./megdb.bin" }) });
Key-value pairs can be easily set and retrieved using the set
and get
methods:
dbbson.set('key1', 'value1');
console.log(dbbson.get('key1')); // Output: value1
dbjson.push('array1', ["hi", "hello", 1, null, true ]);
console.log(dbjson.get('array1')); // Output: ["hi", "hello", 1, null, true]
dbjson.pull('array1', "hello");
console.log(dbjson.get('array1')); // Output: ["hi", 1, null, true]
// And more...
With meg.db, you can effortlessly create backups with a variety of options. Using the built-in cronJob pattern, you can set the time, timezone, and backup folder path.
const dbjson = new Megdb({
driver: new JSONDriver({
filePath: "./megdb.json",
backupOptions: {
enabled: true,
timezone: "Europe/Istanbul",
CronJobPattern: "0 00 20 * * *",
folderPath: "./backups"
}
})
});
// Your code ...
Utilize Time-To-Live (TTL) for auto-deletion of temporary data:
// Set data to auto-delete in 10 seconds
dbjson.set("hello", "Hi! this will be deleted in 10 seconds!", 10 * 1000);
// Use Callbacks
dbjson.set("hello", "Hi! this will be deleted in 30 seconds!", 30 * 1000, (key, value) => {
console.log(key + " deleted.");
});
or Implement Garbage Collector to manage expired data:
const dbjson = new Megdb({
driver: new JSONDriver({
filePath: "./megdb.json",
garbageCollection: {
enabled: true,
interval: 1000
},
})
});
dbjson.set("Hello", "This won't be deleted");
dbjson.set("Hello2", "This will be deleted in 10 seconds", 10 * 1000); // 10 seconds
// Manually trigger Garbage Collection
dbjson.runGarbageCollection();
These methods enable automatic data deletion after a specified time and efficient management of expired data through the Garbage Collector. ðâĻ
meg.db allows you to seamlessly move data from other databases. Here's an example using the quick.db library:
import { Megdb, BSONDriver, DatabaseMigration } from "meg.db"; // Version: 3.0.0
import { QuickDB } from "quick.db"; // Version: 9.1.7
const megdb = new MegDB({ driver: new BSONDriver({ filePath: "./megdb.bson" }) });
const migration = new DatabaseMigration(megdb);
const quickdb = new QuickDB();
quickdb.set(`hi`, `Hello, world!`);
quickdb.set(`array`, [1, undefined, "hi", true]);
migration.move(await quickdb.all());
console.log(megdb.all()) // Map(2) { 'hi' => 'Hello, world!', 'array' => [1, undefined, "hi", true] }
This module is open source and available under the Apache 2.0 License.
Contributions are welcome! If you encounter any issues or have suggestions for improvement, please don't hesitate to open an issue or submit a pull request on the GitHub repository.
Generated using TypeDoc