FeedHenry Database Library

3.3.2 · maintenance · verified Wed Apr 22

The `fh-db` library, currently at version `3.3.2`, provides a robust database abstraction layer for Node.js applications, specifically designed to interface with MongoDB. It offers a "Ditch-like" API, aiming for a consistent set of actions and parameters that simplify common database operations such as create, read, update, and delete. This library functions as a wrapper around the native `mongodb` driver, abstracting some of its complexities and providing a higher-level interface suitable for FeedHenry cloud application development. While its original target Node.js engine was `4.4`, it has been updated to its current major version `3`, indicating that it received active development and maintenance at some point. Its key differentiator is providing a simplified, consistent API tailored for the FeedHenry ecosystem, reducing boilerplate when interacting with MongoDB. The latest publish date for version `3.3.2` was over 7 years ago, suggesting it is now in maintenance mode, though still widely used within the FeedHenry ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `fh-db` with a MongoDB connection URI and perform basic CRUD (Create, Read, Update, Delete) and list operations on a collection. It uses Promises to wrap the callback-based API for easier asynchronous handling. Ensure a MongoDB instance is running and accessible via the provided `MONGO_URI`.

const fhdb = require('fh-db');
const assert = require('assert');

const MONGO_URI = process.env.MONGO_URI || 'mongodb://localhost:27017/testdb_fhdb';

async function runExample() {
  let dbInstance;
  try {
    dbInstance = fhdb.db(MONGO_URI);
    console.log('Database connected to:', MONGO_URI);

    const collectionName = 'mydata';
    const documentToInsert = { id: 'item1', value: 'hello world', timestamp: new Date() };

    // Create a document
    console.log('Creating document...');
    const createResult = await new Promise((resolve, reject) => {
      dbInstance.create(collectionName, documentToInsert, (err, data) => {
        if (err) return reject(err); resolve(data);
      });
    });
    console.log('Created:', createResult);
    assert.strictEqual(createResult.value, documentToInsert.value, 'Document created successfully');

    // Read documents
    console.log('Reading documents...');
    const readResult = await new Promise((resolve, reject) => {
      dbInstance.read(collectionName, { id: 'item1' }, (err, data) => {
        if (err) return reject(err); resolve(data);
      });
    });
    console.log('Read:', readResult);
    assert(Array.isArray(readResult) && readResult.length > 0, 'Document should be found');

    // Update a document
    console.log('Updating document...');
    const updatedValue = 'updated value';
    const updateResult = await new Promise((resolve, reject) => {
      dbInstance.update(collectionName, { id: 'item1' }, { value: updatedValue }, (err, data) => {
        if (err) return reject(err); resolve(data);
      });
    });
    console.log('Updated:', updateResult);
    assert.strictEqual(updateResult.value, updatedValue, 'Document updated successfully');

    // List all documents (simple read without query)
    console.log('Listing all documents...');
    const listResult = await new Promise((resolve, reject) => {
      dbInstance.list(collectionName, (err, data) => {
        if (err) return reject(err); resolve(data);
      });
    });
    console.log('Listed:', listResult);
    assert(Array.isArray(listResult), 'List should return an array');

    // Delete a document
    console.log('Deleting document...');
    const deleteResult = await new Promise((resolve, reject) => {
      dbInstance.delete(collectionName, { id: 'item1' }, (err, data) => {
        if (err) return reject(err); resolve(data);
      });
    });
    console.log('Deleted:', deleteResult);
    assert.strictEqual(deleteResult.id, 'item1', 'Document deleted successfully');

    // Verify deletion
    const verifyDeletion = await new Promise((resolve, reject) => {
      dbInstance.read(collectionName, { id: 'item1' }, (err, data) => {
        if (err) return reject(err); resolve(data);
      });
    });
    assert.strictEqual(verifyDeletion.length, 0, 'Document should no longer exist');

  } catch (error) {
    console.error('An error occurred:', error.message);
  } finally {
    // fh-db itself doesn't expose a direct close method; it relies on the underlying MongoDB driver's connection pooling.
    console.log('Example finished. Ensure your MongoDB connection is managed appropriately by the underlying driver.');
  }
}

runExample();

view raw JSON →