Skip to content

skiv71/mongodb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@skivy71/mongodb

Description

This is a small library inspired by TypeORM.

Like TypeORM, exports a base "Entity" class, however this library does so using the Active Record format (which TypeORM does not).

Usage

import Entity from ".."

class MyClass extends Entity {

    // (optional) - omit to use *.save directly
    public async save(): Promise<this> {   
        await validateOrReject(this)
        return super.save()
    }

    // (optional) - omit to use *.remove directly
    public async remove(): Promise<void> { // omit to use entity.remove directly
        await super.remove()
    }

    // implement for MyClass.find({})
    public static find = Entity.findMany<MyClass>     

    // implement for MyClass.findById(<string | ObjectId>)
    public static findById = Entity.findOne<MyClass>  

}

How it works at database level

Under the hood, the library creates mongodb ObjectId's automatically and applies timestamps automatically.

Any values which are non-enumerable will not be added the database.

In the case of "relations", the field e.g. group (Group Entity) will be replaced an and should you wish to query an entity by relation, the relation (as an entity can be used in the query.)

e.g

class User extends Entity {

    public group: Group

}

const [group] = await Group.find({ name })

if (!group)
    throw new Error("Oops, wrong name!")

const users = await User.find({ group })

Loading relations

In the case of relations, these can be loaded automatically via the findOne / findMany.

The base Entity class looks for the *.load() function and calls it automatically, if present.

There is a special method available to quickly load any relation(s).

e.g.

class User extends Entity {

    public group: Group
    
    public async load(): Promise<this> {
        this.group = await this.relation(Group)
        return this
    }

}

Indexes

The base Entity class exposes the following static methods, which you can use to control any indexes

e.g.

await Entity.createIndex(User, {
    fields: {
        email: 1,
        mobile: 1
    },
    options: {
        unique: true
    }
}, {
    fields: {
        role: 1
    },
    options: {
        unique: true,
        partialFilterExpression: {
            role: `admin`
        }
    }
})

Validation

At this time, I'm currently using class-validator

Please see "./src/app/*"

Notes

This snippet is a WIP, but I use the same code for my personal projects.

Build

You can run

npm run build

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors