Git Product home page Git Product logo

ezmongodb's Introduction

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.

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();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.