A lightweight, MongoDB-like JSON document storage system powered by SQLite, designed for developers who need efficient document-based storage without the overhead of a dedicated NoSQL database like MongoDB. This library combines the simplicity of SQLite with robust JSON querying capabilities to provide a modern, performant, and flexible document store solution.
- Document Storage: Store and retrieve JSON documents in collections (SQLite tables).
-
Rich Query Capabilities: Support for equality checks,
$in
,$regex
, and logical operators. -
MongoDB-Like API: Familiar CRUD functions –
insertOne
,find
,updateOne
, and more! -
Advanced Queries: Count, distinct values, and
$match/$group
aggregations for advanced use cases. - In-Memory and File-Based DBs: Choice between ephemeral or persistent SQLite databases.
- Lightweight: Leverages the SQLite engine; no separate server or complex setup needed.
- Extensible: Access raw SQLite functions for full flexibility.
Sometimes, running a full-fledged MongoDB server is unnecessary, especially for smaller projects, edge deployments, or environments constrained by resources. SQLite Docstore gives you the simplicity of SQLite while letting you work with JSON documents in a MongoDB-like way.
With SQLite Docstore, you:
- Can get started right away without provisioning any external services.
- Have access to persistent, robust, and performant data storage through SQLite.
- Can take advantage of SQLite's native JSON functions to query and manipulate records.
To install via npm:
npm install sqlite-docstore
To install via yarn:
yarn add sqlite-docstore
The library requires Node.js v22 or higher.
Here's a quick example to help you get started:
import { sqliteDocstore } from "sqlite-docstore";
const db = sqliteDocstore.init(); // In-memory DB
const collectionName = "users";
// Create a collection
db.createCollection(collectionName);
// Insert a document
const result = db.insertOne(collectionName, { name: "Alice", age: 30 });
console.log("Inserted Document ID:", result.insertedId);
// Query documents
const users = db.find(collectionName, { name: "Alice" });
console.log("Found Users:", users);
// Count documents in a collection
const count = db.countDocuments(collectionName);
console.log("Total Users:", count);
// Drop the collection
db.dropCollection(collectionName);
sqliteDocstore.init(fileName?: string | null): SqliteDocstoreFunctions
-
fileName
: Path to the SQLite file for persistent data. Whennull
, creates an in-memory database (all data is lost when the process exits).
Returns: An object containing MongoDB-like methods to interact with your database.
Creates a new collection. Each collection corresponds to an SQLite table.
Inserts a single document into a specified collection.
Returns:
{ acknowledged: boolean, insertedId: string }
Inserts multiple documents in a single transaction.
Returns:
{ acknowledged: boolean, insertedCount: number }
Finds all documents matching a query. If no query is provided, all documents are returned.
Example:
db.find("users", { name: "Alice" });
Finds the first document matching a query.
Finds a document by its unique _id
.
Updates a single document that matches the query using the $set
operator.
Returns:
{ acknowledged: boolean, modifiedCount: number }
Deletes the first document that matches the query.
Returns:
{ acknowledged: boolean, deletedCount: number }
Counts the number of documents matching a query. Counts all documents if no query is provided.
Finds documents that match an $in
condition.
Example:
db.findWithIn("users", { name: { $in: ["Alice", "Bob"] } });
Finds documents that match a $regex
condition.
Example:
db.findWithRegex("users", { name: { $regex: "^A" } });
Performs advanced queries using a combination of $match
(filtering) and $group
(aggregation).
Example:
const pipeline = [
{ $match: { age: { $gt: 25 } } },
{
$group: {
_id: "country",
totalAge: { $sum: "age" },
count: { $count: 1 },
},
},
];
const results = db.aggregate("users", pipeline);
Returns all distinct values for a specific field.
Example:
db.distinct("users", "age");
Renames an existing collection.
Drops (deletes) a collection and all associated documents.
The library uses Mocha for testing. To run the tests:
npm test
To run tests in watch mode as you edit the code:
npm run test-watch
Contributions, issues, and feature requests are welcome! Please check the issues page before opening new requests.
If you'd like to contribute:
- Fork the repository.
- Create a feature branch (
git checkout -b feature-name
). - Commit your changes (
git commit -m 'Add feature'
). - Push to your fork (
git push origin feature-name
). - Open a Pull Request.
This project is licensed under the MIT License.
Developed and maintained by jmcudd. Feel free to reach out for support or collaboration!
- NPM: sqlite-docstore
- Repository: GitHub Repo
- Issues: Report Issues