0.0.6 • Published 6 years ago

ovee-db v0.0.6

Weekly downloads
1
License
MIT
Repository
-
Last release
6 years ago

Ovee DB

A very simple wrapper around the Node MongoDB Driver to facilitate easy CRUD operations.

Installation

Just run a npm install --save ovee-db.

Usage

Here's a simple example of how to use Ovee DB:

const OveeDB = require('../dist').OveeDB;

(async () => {
    let db;
    try {
        db = new OveeDB('mongodb://localhost:27017/ovee', 'ovee');

        let car1 = await db.create('cars', {
            name: 'Ford Mustang'
        });

        const car2 = await db.create('cars', {
            name: 'Chevrolet Corvette'
        });

        let cars = await db.list('cars');
        console.log(cars);

        car1.name = 'New Model of Ford Mustang';
        car1 = await db.update('cars', car1);

        cars = await db.list('cars');
        console.log(cars);

        for (let car of cars) {
            await db.delete('cars', car);
        }

    } catch (err) {
        console.error(err);
    } finally {
        if (db != null) {
            await db.disconnect();
        }
    }

})();

Collections

Just like MongoDB, Ovee DB organizes objects in collections within a database. Every Ovee DB call therefore has a collection parameter.

IDs

All objects are identified by their Mongo IDs (field _id).

When creating a new object in the database, the object is automatically assigned an _id. For any further operations with this object (updating it, deleting it), make sure it still has its _id property set.

When retrieving an object using OveeDB.get use the _id to identify the given object.

Initializing Ovee DB

To initialize a new database connection, construct a new OveeDB object:

const db = new OveeDB(private mongoUri: string,
                private dbName: string,
                private mongoOptions: MongoClientOptions | null = null): OveeDB
                

On creation, no database connection will be opened yet. The database will be connected to once the first actual transaction happens.

Creating a new object

To create a new object in the DB, use OveeDB.create:

create(collection: string, object: any): Promise<any>;
  • collection: The collection name in which the object should be crated (e.g. 'cars').
  • object: The object to be created
  • Returns a Promise that resolves to the newly created object (already containing its _id property).

Updating an existing object

To update an existing object, pass it to OveeDB.update. Make sure it contains its _id property.

update(collection: string, object: any): Promise<any>
  • collection: The collection name in which the object should be updated.
  • object: The new version of the object, containing an _id property.
  • Returns a Promise that resolves to the updated object.

Retrieve an object

To retrieve an existing object, identified by its _id property, use OveeDB.get:

get(collection: string, _id: string): Promise<any>
  • collection: The collection name to retrieve the object from
  • _id: The identifier of the object to be retrieved
  • Returns a Promise that resolves to either the retrieve object (if one was found) or resolves without a result if no object was found.

List objects

To list all objects in a collection, or to list all objects matching certain criteria, use OveeDB.list:

list(collection: string, criteria: any = {}): Promise<any[]>
  • collection: The collection name to list objects from
  • criteria: If left empty, all objects in the collection will be retrieved. If set, should contain an object where each property is a filter for object properties. E.g. passing {name: 'Chevrolet Corvette'}', only returns objects whose name is 'Chevrolet Corvette'.

Disconnecting from the database

To disconnect from the database, use OveeDB.disconnect:

    disconnect(): Promise<any> {
  • Returns a Promise that resolves when the disconnect happened.