modokdb
v0.1.0-16
Published
In-memory Database
Downloads
5
Readme
modokDB
In-memory Database
modokDB is a small high performant in-memory database powered by JavaScript Maps (supports Node, Electron and the browser)
The Problem
You are looking for a database that is simple and without any dependencies to install.
The Solution
modokDB is a high performant and convenient method for storing data without setting up a database server. It is fast and excellent to be used as an embedded database.
Installation
This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies
:
npm install --save modokdb
Alternatively, if you're using yarn
yarn add modokdb
A UMD build is also available on unpkg for testing and quick prototyping:
<script src="https://unpkg.com/modokdb/dist/modokdb.min.js"></script>
<script>
var db = new modokdb('users')
</script>
Usage
Without file storage:
const Modok = require('modokdb');
const db = new Modok('users');
With file storage:
const Modok = require('modokdb');
const db = new Modok('users', { filepath: './data/users', filename: 'database' });
db.ready.then(() => {
console.log('Database file is ready');
});
db.has - Synchronous (returns true / false)
const isAvailable = db.has(<index>);
db.$has - Asynchronous (returns true / false)
db.$has(<index>).then((value) => {
console.log(value);
});
db.insert - Synchronous (returns database entry)
const user = db.insert({ _id: 0, first_name: 'John', last_name: 'Doe', age: 29 });
console.log('User', user)
db.insert - Synchronous (returns database entries)
const users = db.insert([{ _id: 0, first_name: 'John', last_name: 'Doe', age: 29 },
{ _id: 1, first_name: 'Jane', last_name: 'Doe', age: 27 }]);
console.log('Users', users)
db.$insert - Asynchronous (returns database entry)
db.$insert({ _id: 1, first_name: 'Jane', last_name: 'Doe', age: 27 }).then((user) => {
console.log('User', user);
});
db.$insert - Asynchronous (returns database entries)
db.$insert([{ _id: 0, first_name: 'John', last_name: 'Doe', age: 29 },
{ _id: 1, first_name: 'Jane', last_name: 'Doe', age: 27 }]).then((users) => {
console.log('Users', users);
});
db.insertOne - Synchronous (returns database entry / entries)
const user = db.insertOne({ _id: 0, first_name: 'John', last_name: 'Doe', age: 29 });
console.log('User', user)
db.$insertOne - Asynchronous (returns database entry / entries)
db.$insertOne({ _id: 1, first_name: 'Jane', last_name: 'Doe', age: 27 }).then((user) => {
console.log('User', user);
});
db.insertMany - Synchronous (returns database entry / entries)
const users = db.insertMany([{ _id: 0, first_name: 'John', last_name: 'Doe', age: 29 },
{ _id: 1, first_name: 'Jane', last_name: 'Doe', age: 27 }]);
console.log('Users', users)
db.$insertMany - Asynchronous (returns database entry / entries)
db.$insertMany([{ _id: 0, first_name: 'John', last_name: 'Doe', age: 29 },
{ _id: 1, first_name: 'Jane', last_name: 'Doe', age: 27 }]).then((users) => {
console.log('Users', users);
});
db.find - Synchronous (_returns database entry / null)
const users = db.find({ last_name: 'Doe' });
console.log('Users', users)
db.$find - Asynchronous (_returns database entry / null)
db.$find({ last_name: 'Doe' }).then((users) => {
console.log('Users', users);
});
db.findOne - Synchronous (_returns database entry / null)
const users = db.findOne({ last_name: 'Doe' });
console.log('User', users)
db.$findOne - Asynchronous (_returns database entry / null)
db.$findOne({ last_name: 'Doe' }).then((users) => {
console.log('User', users);
});
db.findOneAndDelete - Synchronous (_returns database entry / null)
const users = db.findOneAndDelete({ last_name: 'Doe' });
console.log('User', users)
db.$findOneAndDelete - Asynchronous (_returns database entry / null)
db.$findOneAndDelete({ last_name: 'Doe' }).then((users) => {
console.log('User', users);
});
db.update - Synchronous (_returns database entry / null)
const updatedUsers = db.update({ first_name: 'Jane' }, { age: 45, last_name: 'Mayer' });
console.log('updatedUsers', updatedUsers)
db.$update - Asynchronous (_returns database entry / null)
db.$update({ first_name: 'Jane' }, { age: 45, last_name: 'Mayer' }).then((users) => {
console.log('users', users);
});
db.update - upsert : true - Synchronous (_returns database entry / null)
const updatedUsers = db.update({ first_name: 'Jane' }, { age: 45, last_name: 'Mayer' }, { upsert: true });
console.log('updatedUsers', updatedUsers)
db.$update - upsert : true - Asynchronous (_returns database entry / null)
db.$update({ first_name: 'Jane' }, { age: 45, last_name: 'Mayer' }, { upsert: true }).then((users) => {
console.log('users', users);
});
db.updateOne - Synchronous (_returns database entry / null)
const updatedUser = db.updateOne({ first_name: 'Jane' }, { age: 45, last_name: 'Mayer' });
console.log('updatedUser', updatedUser)
db.$updateOne - Asynchronous (_returns database entry / null)
db.$updateOne({ first_name: 'Jane' }, { age: 45, last_name: 'Mayer' }).then((user) => {
console.log('user', user);
});
db.updateMany - Synchronous (_returns database entry / null)
const updatedUsers = db.updateMany({ first_name: 'Jane' }, { age: 45, last_name: 'Mayer' });
console.log('updatedUsers', updatedUsers)
db.$updateMany - Asynchronous (_returns database entry / null)
db.$updateMany({ first_name: 'Jane' }, { age: 45, last_name: 'Mayer' }).then((users) => {
console.log('users', users);
});
db.count - Synchronous (returns integer)
const count = db.count();
console.log('Count', count);
db.$count - Asynchronous (returns integer)
db.$count().then((value) => {
console.log('Count', value);
});
db.drop - Synchronous (returns empty object)
db.drop();
db.$drop - Asynchronous (returns empty object)
db.$drop().then(() => {
});
db.delete - Synchronous (returns true / false)
db.delete({ last_name: 'Doe' });
db.$delete - Asynchronous (returns true / false)
db.$delete({ last_name: 'Doe' }).then(() => {
});
db.deleteOne - Synchronous (returns true / false)
db.deleteOne({ last_name: 'Doe' });
db.$deleteOne - Asynchronous (returns true / false)
db.$deleteOne({ last_name: 'Doe' }).then(() => {
});
db.deleteOne - Synchronous (returns true / false)
db.deleteMany({ last_name: 'Doe' });
db.$deleteOne - Asynchronous (returns true / false)
db.$deleteMany({ last_name: 'Doe' }).then(() => {
});
db.deleteOneById - Synchronous (returns true / false)
db.deleteOneById(2);
db.$deleteOne - Asynchronous (returns true / false)
db.$deleteOneById(2).then(() => {
});
db.stats - Synchronous (returns object)
const db = new Modok('users', { filepath: './data/users', filename: 'database' });
const stats = db.stats();
console.log('Stats', stats);
db.$stats - Asynchronous (returns object)
const db = new Modok('users', { filepath: './data/users', filename: 'database' });
db.$stats().then((stats) => {
console.log('Stats', stats);
});
Collection Reference
# File: 1
const db = new Modok('users', { filepath: './data/users', filename: 'database' });
# File: 2
const usersRef = Modok('users');
const results = usersRef.find({ last_name: 'Doe' });
console.log('results', results);
db.renameCollection - Synchronous (returns collection reference)
const db = new Modok('users');
const newDB = db.renameCollection('accounts');
console.log('newDB', newDB);
db.$renameCollection - Asynchronous (returns collection reference)
const db = new Modok('users');
db.$renameCollection('accounts').then((newDB) => {
console.log('newDB', newDB);
});
Contributors
LICENSE
MIT