@storehouse/mongoose
TypeScript icon, indicating that this package has built-in type declarations

2.1.1 • Public • Published

@storehouse/mongoose

Documentation.

Installation

Make sure you have @storehouse/core and mongoose installed.

$ npm install @storehouse/mongoose

Usage

Basic

movies.ts

import { Document, Schema } from 'mongoose';
import { ModelSettings } from '@storehouse/mongoose';

export interface MovieJson {
  title: string;
  rate?: number;
}

export interface Movie extends Document, MovieJson {
}

export const movieSettings: ModelSettings = {
    name: 'movies',
    schema: new Schema({
      title: {
        type: String,
        trim: true,
        required: [true, '\'title\' is required.'],
        unique: true,
        index: { unique: true },
      },
      rate: {
        type: Number
      },
    }),
    collection: 'movies'
};

index.ts

import Storehouse from '@storehouse/core';
import { MongooseManager, CustomModel } from '@storehouse/mongoose';
import { movieSettings } from './movies';

// register
Storehouse.add({
  local: {
    // type: '@storehouse/mongoose' if you called Storehouse.setManagerType(MongooseManager)
    type: MongooseManager, 
    config: {
      // string
      database: 'mongodb://localhost:27017/database',
      
      // ConnectOptions
      options: {
        keepAlive: true,
        maxPoolSize: 24
      },
      
      // ModelSettings[]
      models: [
        movieSettings
      ]
    }
  }
});

Once the manager registered, you can access it or directly get the connection or models.

import Storehouse from '@storehouse/core';
import { MongooseManager, CustomModel } from '@storehouse/mongoose';
import { Connection } from 'mongoose';
import { Movie } from './movies';

// connection
const conn = await Storehouse.getConnection<Connection>().asPromise();
if (conn) {
  console.log('retrieved connection for database', conn.name);
}

// manager
const localManager = Storehouse.getManager<MongooseManager>('local');
if (localManager) {
  // model
  const moviesModel = manager.getModel<CustomModel<Movie>>('movies');
  if (moviesModel) {
    console.log('nb movies', await moviesModel.countDocuments());
  }
}

// model
const Movies = Storehouse.getModel<CustomModel<Movie>>('movies');
if(Movies) {
  console.log('nb movies', await Movies.countDocuments());
}

Helpers

There are methods to help you retrieve the connection, manager and models so you don't have to check if they are undefined. Those methods throw an error when they fail.

import Storehouse from '@storehouse/core';
import { CustomModel, getConnection, getManager, getModel } from '@storehouse/mongoose';
import { Movie } from './movies';

// connection
const conn = await getConnection(Storehouse, 'local').asPromise();
console.log('retrieved connection for database', conn.name);

// manager
const manager = getManager(Storehouse, 'local');
manager.getModel<CustomModel<Movie>>('movies');

// model
const Movies = getModel<Movie>(Storehouse, 'local', 'movies');
console.log('nb movies', await Movies.countDocuments());

Aggregation

A method from CustomModel of @storehouse/mongoose.

import Storehouse from '@storehouse/core';
import { getModel } from '@storehouse/mongoose';
import { Movie, MovieJson } from './movies';

const Movies = getModel<Movie>(Storehouse, 'local', 'movies');
const movies = await Movies.aggregation<MovieJson>().match({});

References

Readme

Keywords

Package Sidebar

Install

npm i @storehouse/mongoose

Weekly Downloads

0

Version

2.1.1

License

MIT

Unpacked Size

33.8 kB

Total Files

11

Last publish

Collaborators

  • demingongo