Straight to the point, here’s a database in 3 lines. Read on if you’re interested!

When you’re at the beginning of your journey to learn node and start fiddling with databases you soon realize that databases is quite advanced and require a lot of code to produce what looks like little result. I just want to save stuff in an array and keep it stored forever, why is this so confusing?

This is approximately what you’re faced with as a beginner node developer. You use mongoose to create a mongodb database. This code:

1. Creates a database

2. Adds fluffy the kitten

3. Finds fluffy the kitten

below you will see the same thing written with ninjadb with a fraction of the code

What is ninjadb?

ninjadb is a super simple json database which is essentially just a wrapper around lowdb. The reason I created ninjadb was that I thought lowdb could be made even simpler. To the left you see ninjadb and to the right we have lowdb. Both scripts producing the exact same result.

1. Creates a database

2. Adds fluffy the kitten

3. Finds fluffy the kitten

When to use and not use ninjadb

ninjadb is perfect for small projects that doesn’t require good optimization or involves a ton of data. I use it all the time for small projects such as scraping websites, twitter bots, electron torrent client, proof of concepts or even as a real server database for websites that doesn’t store a lot of information.

Do not use ninjadb for commercial websites or serious projects that process a lot of data. But if you do things like that, you probably already know what database to use and for what reason.

ninjadb is relatively slow. But when used in small projects you will probably never notice or care.

How to use ninjadb

We start off by installing ninjadb to your projects folder

npm install ninjadb

Create a new file, app.js. We then write some code to push a new object to the database, then run the script.

You will then notice that kittens.json is created and you will find fluffy inside. Add another kitten and watch kittens.json grow with more objects.

The update function takes two parameters. the first one is an object which finds the first matching object. The second param contains what you want to update or add

I did this for both and one of them is now a hungry kitten

Let’s find and filter the cats. We use find to fetch the first object that matches.

We use filter to fetch everything matching the object. If we leave filter empty it simply returns everything

Thank you for reading! 🎉

You can find more about ninjadb at github and learn about remove, upsert, options and electron support!

I created ninjadb 2 years ago and have been using it ever since. So I thought why not share it, perhaps someone could enjoy it. Feel free to send a pull request if you want something added or changed! Good luck!