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 🙏

© 2026 – Pkg Stats / Ryan Hefner

memongo

v1.0.3

Published

A minimal MongoDB-like in-memory JSON database with pluggable persistence.

Readme

Memongo

English · 简体中文

license javascript repo size npm version stars

A minimal MongoDB-like in-memory JSON database with pluggable persistence.

Memongo is a lightweight JavaScript database that stores JSON data in memory and provides a MongoDB-style API for querying and manipulating data.

It is designed to be simple, embeddable, and environment-agnostic. Persistence is handled by user-provided read/write functions, allowing it to run in environments such as browsers, Node.js, or WeChat Mini Programs.


Features

  • MongoDB-like query API
  • In-memory JSON storage
  • Custom persistence layer
  • Dot-notation field access ("user.profile.age")
  • Functional updates
  • Query commands (gt, lt, in, exists, etc.)
  • Debounced persistence writes
  • No dependencies

Installation

Through npm if available:

npm install memongo

Or

simply copy the source file into your project.


Quick Example

Other example (in environment like browser or node.js or wechat-minipragram), refer to directory examples.

const { MemoryJSONDB } = require("memongo");

const _ = MemoryJSONDB.command;

const db = new MemoryJSONDB(
  async () => JSON.parse(localStorage.getItem("db") || "{}"),
  async (data, resolve, reject) => {
    try {
      localStorage.setItem("db", JSON.stringify(data));
      resolve();
    } catch (err) {
      reject(err);
    }
  },
);

await db.init();

const users = await db.createCollection("users");

await users.add({
  data: {
    name: "Tom",
    age: 20,
  },
});

const result = users
  .where({
    age: _.gt(18),
  })
  .get();

console.log(result.data);

Creating a Database

const db = new MemoryJSONDB(getJSONObj, writeJSONObj);
await db.init();

getJSONObj

A function that returns the persisted JSON object.

writeJSONObj

A function that writes the JSON object to persistent storage, using resolve and reject to handle success and failure cases respectively (resolve indicates the write operation completed successfully, while reject indicates it failed).


Collections

Create a collection:

const users = await db.createCollection("users");

Get an existing collection:

const users = db.collection("users");

Insert Documents

await users.add({
  data: {
    name: "Alice",
    age: 25,
  },
});

Custom _id:

await users.add({
  data: {
    _id: "user1",
    name: "Alice",
  },
});

Query Documents

users
  .where({
    age: 20,
  })
  .get();

Nested fields:

users.where({
  "profile.age": 20,
});

Query Commands

Available commands:

  • command.eq(value)
  • command.neq(value)
  • command.gt(value)
  • command.gte(value)
  • command.lt(value)
  • command.lte(value)
  • command.in(array)
  • command.nin(array)
  • command.exists(boolean)

Example:

users.where({
  age: command.gt(18),
});

Sorting

users.orderBy("age", "asc").get();

Multiple fields:

users.orderBy("age", "asc").orderBy("name", "desc").get();

Limit and Skip

users.skip(10).limit(5).get();

Update

await users
  .where({
    age: 20,
  })
  .update({
    data: {
      age: (age) => age + 1,
    },
  });

Nested update:

await users.doc("user1").update({
  data: {
    "profile.age": 30,
  },
});

Delete

Remove a document:

users.doc("user1").remove();

Clear a collection:

await users.remove();

Example: WeChat Mini Program Persistence

In this way, one can still use wx.getStorage and wx.setStorage as long as they avoid accessing the specific key "localDB". The maximum size of the database is then 1 MB, as WeChat Mini Program allows only 1 MB of storage per key (for details about this, refer to the official WeChat Mini Program documentation).

const db = new MemoryJSONDB(
  async () => {
    try {
      return (await wx.getStorage({ key: "localDB" })).data;
    } catch {
      return {};
    }
  },
  async (jsonObj, resolve, reject) => {
    wx.setStorage({
      key: "localDB",
      data: jsonObj,
      success: resolve,
      fail: reject,
    });
  },
);

The following is a solution to fully utilize the entire 10 MB of local storage. In this approach, access to local storage must be restricted solely to this database API; otherwise, unpredictable results may occur.

const db = new MemoryJSONDB(
  async () => {
    const memoryJSONDB = {};
    await Promise.all(
      (await wx.getStorageInfo()).keys.map(async (key) => {
        memoryJSONDB[key] = (await wx.getStorage({ key })).data;
      }),
    );
    return memoryJSONDB;
  },
  async (jsonObj, resolve, reject) => {
    Promise.all(
      Object.keys(jsonObj).map(async (key) => {
        await wx
          .setStorage({
            key: key,
            data: jsonObj[key],
          })
          .then(resolve)
          .catch(reject);
      }),
    );
  },
);

To implement either of the above approaches, it is best to do the following wrapping in a separate JS file (e.g., localDB.js) as demonstrated below, and always interact with the database through this file.

// In localDB.js file

const { MemoryJSONDB, LOCALDB_ERROR_TYPES } = require("../memongo"); // Or the right path to require

// Either of the above two way
// ...

module.exports = {
  db,
  command: MemoryJSONDB.command,
  LOCALDB_ERROR_TYPES,
};

In this way, one can ensure that there is only one instance of the database, thereby avoiding any inconsistency issues that might arise from running multiple instances without carefully handling data consistency.


Limitations

  • The entire database is rewritten during persistence
  • Not suitable for large datasets
  • No indexing support yet

Acknowledgements

Thanks to the following open-source projects:


License

Licensed under GNU GPL v3.