npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sqlite-docstore

v1.0.3

Published

A MongoDB-like JSON abstraction with SQLite backend

Downloads

218

Readme

SQLite Docstore

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.

License: MIT Node Version GitHub Issues NPM Version

Features

  • 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.

Why Choose SQLite Docstore?

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.

Installation

To install via npm:

npm install sqlite-docstore

To install via yarn:

yarn add sqlite-docstore

The library requires Node.js v22 or higher.


Getting Started

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);

Core API

Initialization

sqliteDocstore.init(fileName?: string | null): SqliteDocstoreFunctions
  • fileName: Path to the SQLite file for persistent data. When null, 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.


CRUD Operations

createCollection(collectionName: string)

Creates a new collection. Each collection corresponds to an SQLite table.

insertOne(collectionName: string, document: object)

Inserts a single document into a specified collection.

Returns:

{ acknowledged: boolean, insertedId: string }

insertMany(collectionName: string, documents: object[])

Inserts multiple documents in a single transaction.

Returns:

{ acknowledged: boolean, insertedCount: number }

find(collectionName: string, query?: object)

Finds all documents matching a query. If no query is provided, all documents are returned.

Example:

db.find("users", { name: "Alice" });

findOne(collectionName: string, query?: object)

Finds the first document matching a query.


findById(collectionName: string, id: string)

Finds a document by its unique _id.


updateOne(collectionName: string, query: object, update: {$set: object})

Updates a single document that matches the query using the $set operator.

Returns:

{ acknowledged: boolean, modifiedCount: number }

deleteOne(collectionName: string, query: object)

Deletes the first document that matches the query.

Returns:

{ acknowledged: boolean, deletedCount: number }

countDocuments(collectionName: string, query?: object)

Counts the number of documents matching a query. Counts all documents if no query is provided.


Advanced Operations

findWithIn(collectionName: string, query: object)

Finds documents that match an $in condition.

Example:

db.findWithIn("users", { name: { $in: ["Alice", "Bob"] } });

findWithRegex(collectionName: string, query: object)

Finds documents that match a $regex condition.

Example:

db.findWithRegex("users", { name: { $regex: "^A" } });

aggregate(collectionName: string, pipeline: object[])

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);

distinct(collectionName: string, field: string)

Returns all distinct values for a specific field.

Example:

db.distinct("users", "age");

renameCollection(oldName: string, newName: string)

Renames an existing collection.


dropCollection(collectionName: string)

Drops (deletes) a collection and all associated documents.


Running Tests

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

Contributing

Contributions, issues, and feature requests are welcome! Please check the issues page before opening new requests.

If you'd like to contribute:

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature-name).
  3. Commit your changes (git commit -m 'Add feature').
  4. Push to your fork (git push origin feature-name).
  5. Open a Pull Request.

License

This project is licensed under the MIT License.


Author

Developed and maintained by jmcudd. Feel free to reach out for support or collaboration!

GitHub: @jmcudd
X: @jmcudd


Links