0.0.5 • Published 2 years ago

@mattellis91/mockdb v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Mockdb

Create and manage mock document orientated databases using mongodb like filters.

Install

npm i @mattellis91/mockdb

Database

Create a new database , connect to an existing database or remove an existing database with the MockDb static methods

Databases are represented as subdirectories of the root MockDb directory that is created when creating the 1st database for the project.

project
│       
└─── mockdb
│    └─── testdb
│    └─── testdb2
import { MockDb } from '@mattellis91/mockdb';

//connect to an existing database. If the database does not exist. 
//It will be created and a connection to it will be returned
const connection = MockDb.connect('testDb');

//create new db without connecting
//const connection = MockDb.createDb('testDb');

//remove an existing db
//await MockDb.removeDb('existingDb')

Collections

Manage collections of documents. Collections are represented as json files inside the relevant database subdirectory.

project
│       
└─── mockdb
│    └─── testdb
│    │    └─── testCollection.json
│    └─── testdb2
//get reference to an existing collection or create a new one if the collection doesn't exist
const collection = connection.collection('testCollection');
    
//use collection the reference to peform operations on the collection

//insert documents into the collection. If an id isn't provided a random one will be generated.
collection.insertOne({ _id: cjld2cjxh0000qzrmn831i7rn, foo: 1, bar: false });
collection.inserMany([
    {
        foo: 2,
        bar: true
    },
    {
        foo: 3,
        bar: false
    },
    {
        foo: 4,
        bar: true
    },
]);

//retrieve all documents in the collection
collection.find();

//retrieve a single document by Id
collection.findById('cjld2cjxh0000qzrmn831i7rn');

//remove a single document by Id
collection.removeById('cjld2cjxh0000qzrmn831i7rn');

//remove all documents that match filter
collection.remove({bar: false});

//get number of documents in the collection
collection.count();

//remove collection from database
connection.dropCollection('testCollection');

Filter

Filter documents using mongodb like query operators

//find all documents that match filter
collection.find({ foo: {$gt: 2} });

//find first document that matches filter
collection.findOne({ foo: {$gt: 2} })

//filter documents using logical operators
collection.find(
    {
        $or: [
            { foo: {$lt: 2} },
            { bar: true }
        ]
    }
)

const collection.find({
            $and: [
                { bar: true },
                { $or: [{ foo: 1}, {foo: {$ne: 4}, bar: {$exists: true}} ]}
            ],
        });
        
        
//remove all documents that match filter
collection.remove({bar: true});

//remove first document that matches filter
collection.removeOne({bar: false});

Current supported filter operators

OperatorDescription
$eqMatches values that are equal to a specified value.
$neMatches all values that are not equal to a specified value.
$gtMatches values that are greater than a specified value.
$gteMatches values that are greater than or equal to a specified value.
$ltMatches values that are less than a specified value.
$lteMatches values that are less than or equal to a specified value.
$lteMatches values that are less than or equal to a specified value.
$inMatches any of the values specified in an array.
$ninMatches none of the values specified in an array.
$existsMatches documents that have the specified field.
$textPerforms text search.
$andJoins query clauses with a logical AND returns all documents that match the conditions of both clauses.
$orJoins query clauses with a logical OR returns all documents that match the conditions of either clause.

Update

Update documents using mongodb like update operators

//update all documents that matches filter
collection.update({foo: {$gt: 2} }, {$set : {bar : false } });

//update first document that matches filter
collection.updateOne({foo: {$lt: 2} }, {$set : {bar : true } });

//update document by Id
collection.updateById('cjld2cjxh0000qzrmn831i7rn', {$set : {foo : 100 } });

//upsert document by adding setting upsert property to true on update filter
collection.updateOne({foo: {$gt: 1000} }, {$set : {bar : true }, upsert: true });

Current supported update operators

OperatorDescription
$setSets the value of a field in a document.
$incIncrements the value of the field by the specified amount.
$mulMultiplies the value of the field by the specified amount.
$minOnly updates the field if the specified value is less than the existing field value.
$maxOnly updates the field if the specified value is greater than the existing field value.
$unsetRemoves the specified field from a document.
$renameRenames a field.
$setOnInsertSets the value of a field if an update results in an insert of a document. Has no effect on update operations that modify existing documents.
$addTosetAdds elements to an array only if they do not already exist in the set.
$popRemoves the first or last item of an array.
$pushAdds an item to an array.
$pullAllRemoves all matching values from an array.

Roadmap

  • Add exporting / importing data from databases
  • Add additional filter operators
  • Add add update operators
  • Add random data generation

Tests

npm run test

Contact

Created by Matt Ellis. Feel free to contact me

License

This project is open source and available under the MIT License.