npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nest-mongoose

v1.0.4

Published

NestJS Framework extension for easily injecting mongoose models into components

Downloads

13

Readme

Nest Mongoose

A simple helper library to inject mongoose models into components created with nest.

Written in Typescript.

Goals of this project

The primary goal is to inject mongoose models into components. In order to keep the code clean and simple this library offers wrapper classes around the mongoose model which can be configured on module level and be injected.

Relevant URLs

Requirments

  • Node >= 8.0.0
  • Typescript >= 2.4.0
  • Nest >= 4.0.0
  • mongoose >= 4.11.0

Setup

Just install the package via npm/yarn.

npm install --save nest-mongoose

Usage

In order to use a mongoose model we define some interfaces first. The first interfaces defines the used document structure of the mongodb (Figure 1.0).

import {Document} from 'mongoose';

export interface UserDocument extends Document {
  readonly name: string;
  readonly age: number;
}

Figure 1.0: Document interface (user-document.ts)

The next step is to define our schema and name our mongoose model by calling the super constructor.

nest-mongoose offers an abstract class which minifies the effort to create your model(Figure 1.1).

We override the defineSchema method which must return our mongodb schema.

import {Component} from '@nestjs/common';
import {Schema, Types} from 'mongoose';
import {NestMongooseModel} from 'nest-mongoose';

import {UserDocument} from './user-document';

@Component()
export class UserModel extends NestMongooseModel<UserDocument> {

  constructor() {
    super('user');
  }

  protected defineSchema(): Schema {
    const schema: Schema = new Schema({
      name: {
        type: String,
      },
      age: {
        type: Number,
      },
    });

    return schema;
  }

}

Figure 1.1: Model class (user-model.ts)

Afterwards we configure our model in the module file (Figure 1.2).

import {Module} from '@nestjs/common';

import {UserModel} from './user-model';

@Module({
  components: [
    {provide: 'UserModel', useClass: UserModel},
  ],
})
export class ExampleModule {}

Figure 1.2: Module configuration

Now we can inject the model into components and use the inner mongoose model to query and write data via mongoose (Figure 1.3).

import {Component, Inject} from '@nestjs/common';
import {Model} from 'mongoose';

import {User, UserRepository} from './interfaces';

import {UserDocument} from './example-document';
import {UserModel} from './example-model';

@Component()
export class UserMongoRepository implements UserRepository {

  private readonly user: Model<UserDocument>;

  constructor(
    @Inject('UserModel') nestUserModel: UserModel,
  ) {
    this.user = nestUserModel.mongooseModel;
  }

  public findAllUsers(): Promise<Array<User>> {
    const users: Array<User> = <Array<User>> this.user.find()
      .lean()
      .exec();
    
    return users;
  }
}

Figure 1.3: Repository component

At last we need to assign a mongoose connection to our model. This happens by assigning it to the static property connection. This step should be done when the mongoose connection has been established.

mongoose.connection.once('open', async() => {
  NestMongooseModel.connection = mongoose.connection;

  const listenPort: number = 4000;

  const app: INestApplication = await NestFactory.create(ApplicationModule);

  await app.listen(listenPort);
});

Related projects

License

MIT