Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongot

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongot

A lightweight typed MongoDb library for TypeScript.

  • 0.2.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9
decreased by-18.18%
Maintainers
1
Weekly downloads
 
Created
Source

MongoT

A lightweight typed MongoDb library for TypeScript.

Install

npm i -S mongot

Usage

Collections

A collection is a class which support CRUD operations for own documents.

Create a collection example
# UserCollection.ts
import {Collection, collection} from 'mongot'; 
import {UserDocument} from './UserDocument';

@collection('users', UserDocument) // bind to a document schema
class UserCollection extends Collection<UserDocument> {
    findByEmail(email: string) {
        return this.findOne({email});
    }
}

Document Schema

Document schemas support following types: string, boolean, number, date, Object (any), SchemaFragment (also known as subdocument), array type may contain any of these type. Buffer didn't tested at this time.

Any type can be defined via a function decorator @prop:

    @prop fieldName: string

For arrays you need pass a proto to the function decorator @prop like as:

    @prop(Date) loginDates: SchemaArray<Date> = new SchemaArray();
Create a document example
# UserDocument.ts
import {hook, prop, SchemaDocument} from 'mongot';

@unique({email: -1})
class UserDocument extends SchemaDocument {
    @prop firstName: string;
    @prop lastName: string;
    
    @prop @req email: string;
    
    @prop registered: Date = new Date(); // default value
    @prop updated: Date;
    
    @hook
    beforeUpdate() {
        this.updated = new Date();
    }
    
    get displayName() {
        return this.firstName + 
            (this.lastName ? ' ' + this.lastName : '');
    }
}

Create a repository

To connect your collections to MongoDb you should create a repository:

# index.ts
import {Repository} from 'mongot';

const options = {};
const repository = new Repository('mongodb://localhost/test', options);

The Repository class constructor has same arguments that MongoClient.

Querying

Before querying you should get a collection instance via Repository.get.

# index.ts

async function main(): void {
    const users: UserCollection = repository.get(UserCollection);
    const user = await users.findByEmail('username@example.com');
    
    // do something
    ...
}

Documentation

Coming soon... maybe

Vanilla

You can if you know how it cook.

License

MIT

Keywords

FAQs

Package last updated on 18 Oct 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc