How to use SQLite as a Document Database like MongoDB

Editor Ratings:
User Ratings:
[Total: 0 Average: 0]

Doculite is a free and open-source library that allows you to use SQLite as a Document database. Basically, it helps you use SQLite as a NoSQL database just like MongoDB and CouchDB. It supports all the database operations of a NoSQL DB and more. This is a perfect choice for small projects where using existing NoSQL databases such as Mongo are overkill.

Right now, the library is available for NodeJS which is the most popular JS framework these days to build web applications. If you are thinking of trying a new NoSQL database then Doculite is one of the best options right there. However, for now, it doesn’t have proper specifications and documentation but examples on its GitHub repository will help you understand how it works.

What is a Document Database?

A document database is a type of NoSQL database that stores data in JSON-like documents with schema flexibility. Unlike relational databases which store data in tables with strict schemas and relationships, document databases allow for dynamic schemas for unstructured data.

Some key highlights of document databases:

  • Documents have a nested, hierarchical structure which allows them to model complex, real-world objects like profiles, orders or products very naturally.
  • They have “Collections” instead of “Tables” but with a free-form structure – each document can have different fields. This is very different from relational tables which enforce consistency.
  • Scaling is easy as documents can scale horizontally. You can share or partition collections across servers for unlimited storage capacity and high availability.
  • Popular NoSQL options include MongoDB, Couchbase, CouchDB – they’re often a better fit than SQL for certain applications that don’t require rigid schema.

So, in summary, document databases are a flexible way to store semi-structured or complex data without worrying too much about predefined schemas. They are easy to scale and a good fit for apps that prioritize flexibility over transactions.

How to use SQLite as a Document Database like MongoDB?

Here are the various code samples that you can use to initiate and retrieve data from an SQLite database by treating it as a NoSQL database using Doculite.

The very first thing you have to do is install it. To do that, just run this command.

npm install doculite

Install Doculite

Once installed, you can start using it in your projects. See the code samples below.

Initialize a DB:

Add the import statements and then create a new instance of Database object

import { Database } from "doculite"
const db = new Database();

Create Collections:

Create your first collection. Just name it anything you want and specific a record id like this.

const usersRef = db.collection("users").doc("123");
const refWithoutId = db.collection("users").doc();

Get a Specific Document:

Use the correct name of the collection to get a specific record from the database. The final output will be a JSON like object.

const usersRef = db.collection("users").doc("123");
console.log(user); // prints { username: "I Love Free Software" };

Update Documents in Collections:

Same for update code. Just specify the name of the collection and record correctly to fetch it and then update it with the new data by supplying a dictionary in the following format.

const usersRef = db.collection("users").doc("123");
await ref.set({ username: "I Love Free Software", updatedAt: "345" }, { merge: true });

Delete Documents in Collection:

When you want to delete a record or collection, just make sure to use the correct name of the database as you specified during the initialization.

const db = new Database();
const ref = db.collection("users").doc("deletable");
await ref.set({ username: "deletableUsername", updatedAt: 123123 });
await ref.delete();

There are more examples available in the GitHub repository of Doculite. You can analyze them and then implement this in your projects accordingly. Here I have used the code examples directly. But I hope the developer of this tool adds CLI mode in the coming updates. Also, bindings for other programming languages will be useful too.

Closing thoughts:

If you are planning to build a small project that requires a DocumentDB in the backend then Doculite is most suited in that case. The existing DynamoDB and MongoDB are good but they are also overkill for small projects. So, use Doculite now and run NoSQL type queries to retrieve, transform, and insert data in collections. If this is your first time trying Doculite then it will take some time for you to learn and understand the syntax. But if you have dealt with NoSQL databases before then you will understand it quite easily.

Editor Ratings:
User Ratings:
[Total: 0 Average: 0]
Free/Paid: Free

Leave A Reply


Get 100 GB FREE

Provide details to get this offer