ShareDB Mingo Memory Adapter

4.1.0 · active · verified Wed Apr 22

sharedb-mingo-memory is an in-memory database adapter for ShareDB, providing a local, fast, and ephemeral data store that supports a subset of MongoDB's query API through the Mingo library. Its primary use cases include accelerating application testing by eliminating external database dependencies and facilitating local development environments. The current stable version is 4.1.0, with a release cadence tied to Node.js LTS versions and new major releases of ShareDB itself, ensuring broad compatibility. Key differentiators include its lightweight, in-memory nature, making it ideal for CI/CD pipelines and quick prototyping, while still offering a familiar MongoDB-like query interface. It integrates seamlessly with ShareDB applications for a mock database experience.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize sharedb-mingo-memory, integrate it with ShareDB, create a document, and perform a basic query against the in-memory database.

import ShareDBMingo from 'sharedb-mingo-memory';
import ShareDB from 'sharedb';

// Initialize the in-memory database adapter
const db = new ShareDBMingo();

// Initialize ShareDB with the in-memory adapter
const backend = new ShareDB({ db });

// Example usage: Create a connection and a document
const connection = backend.connect();
const doc = connection.get('myCollection', 'myDocument');

doc.fetch((err) => {
  if (err) throw err;

  if (doc.type === null) {
    // Create the document if it doesn't exist
    doc.create({ title: 'Hello World', content: 'This is an in-memory document.' }, (err) => {
      if (err) throw err;
      console.log('Document created:', doc.data);
      queryDocuments();
    });
  } else {
    console.log('Document already exists:', doc.data);
    queryDocuments();
  }
});

function queryDocuments() {
  const query = connection.query('myCollection', { title: 'Hello World' });
  query.subscribe((err) => {
    if (err) throw err;
    console.log('Query results:', query.results.map(d => d.data));
    // Remember to unsubscribe or close connection in a real app
    query.destroy();
    connection.close();
  });
}

view raw JSON →