Giter Club home page Giter Club logo

mongtype's Introduction

MongType

Codacy Badge Codacy Badge Build Status npm version

MongoDB Repository pattern for NodeJS written in TypeScript.

Install

npm i mongtype --S

Building

npm run build

Usage

Migrating from 2.X to 3.X Migrating from 1.X to 2.X

DatabaseClient

The DatabaseClient class provides a wrapper around a mongodb connection It has a single method called connect that allows you to provide a uri to a MongoDB instance and optionally include an already established MongoClient.

import { DatabaseClient } from 'mongtype';

const uri = 'mongodb://localhost/Foo';
const dbc = new DatabaseClient();

// DatabaseClient manages the connection
dbc.connect(uri);

// DatabaseClient reuses an existing connection
dbc.connect(uri, mongoClient);

Using DI

import { MongoRepository, Collection, Pre, Post } from 'mongtype';
import { Injectable } from 'injection-js';

interface User {
  name: string;
}

@Injectable()
@Collection({
  name: 'user',
  capped: true,
  size: 10000
})
export class UserRepository extends MongoRepository<User> {
  @Before('save')
  doSomethingBeforeSave() {}

  @After('save')
  doSomethingAfterSave() { }
}

@Injectable()
export class App {
  constructor(private userRepo: UserRepository) { }

  async findUsers() {
    const one = await this.userRepo.findById('3434-34-34343-3434');
    const many = await this.userRepo.find({ conditions: { name: 'foo' } });
    const newOne = await this.userRepo.create({ foo: true });
    const updated = await this.userRepo.save(newOne);
  }
}

Without DI

const dbc = new DatabaseClient();
dbc.connect('mongodb://your.mongo.url'); // optional existing connection as second arg
const svc = new UserRepository(dbc);

const one = await svc.findById('3434-34-34343-3434');
const many = await svc.find({ conditions: { name: 'foo' } });
const newOne = await svc.create({ foo: true });
const updated = await svc.save(newOne);

Similar

Credits

MongType is a Swimlane open-source project; we believe in giving back to the open-source community by sharing some of the projects we build for our application. Swimlane is an automated cyber security operations and incident response platform that enables cyber security teams to leverage threat intelligence, speed up incident response and automate security operations.

mongtype's People

Contributors

shaunburdick avatar amcdnl avatar krozner avatar nick-allen avatar

Watchers

 avatar

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.