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

@stuyk/ezmongodb

v3.0.1

Published

A very simple MongoDB driver wrapper for easy database usage.

Downloads

191

Readme

EzMongo is the simple way to use MongoDB without knowing how to use MongoDB.

Store your client data in collections and fetch and perfrom creation, reading, updating, and deleting functions in a very simple way.

It is a single database wrapper for MongoDB.

Installation

Prerequisites

Install

$ npm install @stuyk/ezmongodb

Starting Usage

Here are some generalized steps for getting started.

  1. Use the Database.init function.
  2. Wait for the connection to establish.
  3. Perform any operation for reading, writing, updating, etc.

Additional information can be found below. 👇🏻

🔽 Importing

Import the Database static class.

import Database from '@stuyk/ezmongodb';

🔗 Establish Connection

Establish a connection through the Database static class.

import Database from '@stuyk/ezmongodb';

const url = 'mongodb://localhost:27017';
const dbName = 'test';
const collections = ['accounts', 'characters', 'vehicles'];

async function connect() {
    const connected = await Database.init(url, dbName, collections);
    if (!connected) {
        throw new Error(`Did not connect to the database.`);
    }
}

📝 Regular Usage

These are just some general examples of creating, reading, updating, and deleting.

import Database from '@stuyk/ezmongodb';

const url = 'mongodb://localhost:27017';
const dbName = 'test';
const collections = ['accounts', 'characters', 'vehicles'];

interface IAccount {
    _id?: any;
    username?: string;
    age?: number;
    newData?: string;
}

async function connect() {
    // Establish a connection to your database.
    const connected = await Database.init(url, dbName, collections);
    if (!connected) {
        throw new Error(`Did not connect to the database.`);
    }

    const newDocument = {
        identifier: 'id-1-aaa-bbb-ccc', // Not required, just an example
        username: 'somePerson',
        someNumber: 1 // Not required, just an example
    }

    // Create a search index for 'string' fields
    await Database.createSearchIndex('identifier', 'accounts');
    await Database.createSearchIndex('username', 'accounts');

    // Create new data in a collection (table).
    // This is now a document with `_id` attached to it.
    const somePerson = await Database.insertData<IAccount>(newDocument, 'accounts', true);
    if (!somePerson) {
        throw new Error('Could not insert data');
    }

    // Fetch all entries that have an 'id-' in their properties
    const somePeople = await Database.fetchWithSearch('id-', 'accounts'); // Fetches anything with 'id-' in a field
    const someMorePeople = await Database.fetchWithSearch('1-aaa', 'accounts'); // Fetches anything with '1-aaa' in a field


    // Update all data for document based on 
    // ID in a collection.
    somePerson.age = 5;
    const didUpdate = await Database.updatePartialData(somePerson._id, { ...somePerson }, 'accounts');

    if (!didUpdate) {
        throw new Error(`Document for ${somePerson._id.toString()} did not update.`);
    }

    // Use the same function as above to
    // add a single field to an existing document.
    await Database.updatePartialData(somePerson._id, { newData: 'testing' }, 'accounts');

    // Fetch the new data from the database after updating it.
    // Not necessary if you keep a cache. 
    // Just use the cache as reference.
    const doc = await Database.fetchData<IAccount>('username', 'somePerson', 'accounts');
    if (!doc) {
        throw new Error(`Did not find a document with that data`);
    }


    // Delete a document entirely from a collection (table).
    const didDelete = await Database.deleteById(somePerson._id, 'accounts');
    if (!didDelete) {
        throw new Error(`Document did not exist.`);
    }

    // You can find some more examples in 'test/index.spec.ts'
}

connect();

Additional Functionality

If you need any additional syntax or custom queries, you can use the db directly by fetching its instance.

This lets you call all related MongoDB functionality.

const db = await Database.getDatabaseInstance();