Skip to content

JexiDB is a pure JS NPM library for managing data on disk efficiently, without the need for a server.

License

Notifications You must be signed in to change notification settings

EdenwareApps/jexidb

Repository files navigation

JexiDB logo

Overview

JexiDB is a lightweight, standalone JavaScript database manager that stores data on disk using either JSON format or V8 serialization. It supports indexing and querying capabilities for efficient data operations. Ideal for local Node.js projects as well as apps built with Electron or NW.js. Written in pure JavaScript, it requires no compilation and is compatible with both CommonJS and ESM modules.

Installation

To install JexiDB, you can use npm:

npm install EdenwareApps/jexidb

Usage

Creating a Database Instance

To create a new instance of the database, you need to provide a file path where the database will be stored and an optional configuration object for indexes.

// const { Database } = require('jexidb'); // commonjs

import { Database } from 'jexidb'; // ESM

const db = new Database('path/to/database.jdb', { // file will be created if it does not already exist
  v8: false, // false by default, set to true to use V8 serialization instead of JSON.
  compress: false, // set to true to compress each entry
  compressIndex: false, // set to true to compress the index only
  indexes: { // keys to use in queries, only those key values ​​are kept in memory, so fewer specified keys lead to improved performance
    id: 'number',
    name: 'string'
  }
});

You can learn a bit more about these options at this link.

Initializing the Database

Before using the database, you need to initialize it. This will load the existing data and indexes from the file.

await db.init();

Only the values ​​specified as indexes are kept in memory for faster queries. JexiDB will never load the entire file into memory.

Inserting Data

You can insert data into the database by using the insert method. The data should be an object that contains the defined indexes. All object values will be saved into database.

await db.insert({ id: 1, name: 'John Doe' });
await db.insert({ id: 2, name: 'Jane Doe', anyArbitraryField: '1' });

Querying Data

The query method allows you to retrieve data based on specific criteria. You can specify criteria for multiple fields.

const results = await db.query({ name: 'John Doe' });
console.log(results); // [{ id: 1, name: 'John Doe' }]

Note: For now the query should be limited to using the fields specified as 'indexes' when instantiating the class.

Querying with Conditions

You can use conditions to perform more complex queries:

const results = await db.query({ id: { '>': 1 } });
console.log(results); // [{ id: 2, name: 'Jane Doe' }]

Updating Data

To update existing records, use the update method with the criteria to find the records and the new data.

await db.update({ id: 1 }, { name: 'John Smith' });

Deleting Data

You can delete records that match certain criteria using the delete method.

const deletedCount = await db.delete({ name: 'Jane Doe' });
console.log(`Deleted ${deletedCount} record(s).`);

Iterating Through Records

You can iterate through records in the database using the walk method, which returns an async generator.

for await (const record of db.walk()) {
  console.log(record);
}

Saving Changes

After making any changes to the database, you need to save them using the save method. This will persist the changes to disk.

await db.save();

Conclusion

JexiDB provides a simple yet powerful way to store and manage data in JavaScript applications. With its indexing and querying features, you can build efficient data-driven applications.

JexiDB mascot

Contributing

Please, feel free to contribute to the project by opening a discussion under Issues section or sending your PR.

If you find this library useful, please consider making a donation of any amount via PayPal by clicking here to help the developer continue to dedicate himself to the project. ❤