TingoDB
TingoDB is an embedded JavaScript database for Node.js, designed to be upwardly compatible with MongoDB's v1.4 API. It can operate either as an in-process filesystem database, storing data on disk, or entirely in memory. While the npm metadata shows version 0.6.1 and a README excerpt mentions an `apiLevel` parameter in 0.6.x for some 2.x specific behaviors, the project appears to be largely abandoned, with the last update noted in 2015/2018 and the author explicitly stating its abandonment in 2019. Its key differentiator was offering a local, file-based or in-memory data store with a familiar MongoDB-like query interface, rigorously tested using portions of the official MongoDB Node.js driver's test suite, allowing it to potentially serve as a drop-in replacement for applications built against older MongoDB driver versions.
Common errors
-
TypeError: require('tingodb').Db is not a constructorcause Attempting to access the `Db` constructor directly from the `tingodb` module without invoking the factory function.fixThe `tingodb` module exports a function that must be called to return the object containing the `Db` constructor. Use `const Db = require('tingodb')().Db;` -
MongoError: Unsupported operator: $someNewOperator
cause Using MongoDB query operators or commands that were introduced in versions of MongoDB beyond 1.4.x, which TingoDB does not support.fixConsult MongoDB 1.4.x documentation to ensure all queries and operations are compatible with that specific API level. Rewrite queries to use supported operators. -
Error: ENOENT: no such file or directory, open '/nonexistent/path/tingodb.db'
cause TingoDB is configured to use a filesystem path for storage, but the specified directory does not exist or the application lacks write permissions.fixEnsure the directory specified in `new Db('/some/local/path', {})` exists and is writable by the Node.js process, or handle directory creation before initializing the database.
Warnings
- breaking The `apiLevel` configuration parameter was introduced in version 0.6.x. Its default value of `140` (for MongoDB API 1.4.x compatibility) means behavior, such as `findAndModify` return values, might differ if not explicitly set, especially when aiming for experimental 2.x API behavior using `apiLevel: 200`.
- gotcha TingoDB is explicitly compatible with MongoDB's v1.4 API level. Many features, commands, or query operators introduced in later MongoDB versions (e.g., 2.x, 3.x, or modern versions) are not supported. Using TingoDB as a drop-in for newer MongoDB drivers will lead to unexpected errors or unsupported operations.
- gotcha The `require('tingodb')` call does not directly return the module object with classes. Instead, it returns a factory function that must be invoked (`require('tingodb')()`) to get the usable module object containing constructors like `Db`.
- gotcha As an embedded, in-process database, TingoDB lacks network capabilities, replication, sharding, or advanced security features inherent to a full MongoDB server. It's designed for local storage or in-memory operations, not as a networked database server.
Install
-
npm install tingodb -
yarn add tingodb -
pnpm add tingodb
Imports
- Db
const { Db } = require('tingodb'); const Db = require('tingodb').Db;const Db = require('tingodb')().Db; - tingodb (factory function)
import tingodbFactory from 'tingodb';
const tingodbFactory = require('tingodb'); // Usage: const { Db } = tingodbFactory({ apiLevel: 140 }); - Collection
// Collection instances are obtained from a Db object: // const collection = db.collection('mycollection');
Quickstart
const Db = require('tingodb')().Db,
assert = require('assert');
const dbPath = process.env.TINGODB_PATH || './data'; // Specify a path for persistent storage
// Initialize Db. The second argument is for options, e.g., { apiLevel: 200 }
const db = new Db(dbPath, {});
// Fetch a collection to insert documents into
const collection = db.collection("batch_document_insert_collection_safe");
// Insert multiple documents
collection.insert([{hello:'world_safe1'}, {hello:'world_safe2'}], {w:1}, function(err, result) {
assert.equal(null, err);
console.log('Inserted documents:', result);
// Fetch a document
collection.findOne({hello:'world_safe2'}, function(err, item) {
assert.equal(null, err);
console.log('Found item:', item);
assert.equal('world_safe2', item.hello);
// Example of finding all documents
collection.find({}).toArray(function(err, docs) {
assert.equal(null, err);
console.log('All documents:', docs);
// In a real app, you'd close the DB connection here if it's not reused
});
});
});