Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kisiwu/storehouse-mongoose
Mongoose (MongoDB ODM) manager for @storehouse/core.
https://github.com/kisiwu/storehouse-mongoose
database mongoose nodejs
Last synced: 13 days ago
JSON representation
Mongoose (MongoDB ODM) manager for @storehouse/core.
- Host: GitHub
- URL: https://github.com/kisiwu/storehouse-mongoose
- Owner: kisiwu
- License: mit
- Created: 2021-07-26T22:43:23.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-07-16T21:09:50.000Z (4 months ago)
- Last Synced: 2024-09-17T19:57:06.175Z (2 months ago)
- Topics: database, mongoose, nodejs
- Language: TypeScript
- Homepage: https://novice1.000webhostapp.com/storehouse/mongoose/modules/
- Size: 608 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @storehouse/mongoose
[Documentation](https://kisiwu.github.io/storehouse/mongoose/latest/).
## Installation
Make sure you have [@storehouse/core](https://www.npmjs.com/package/@storehouse/core) and [mongoose](https://www.npmjs.com/package/mongoose) installed.
```bash
npm install @storehouse/mongoose
```## Usage
### Basic
movies.ts
```ts
import { Model, Require_id, Schema } from 'mongoose';
import { ModelSettings } from '@storehouse/mongoose';export interface IMovie {
title: string;
rate?: number;
}export type MovieWithId = Require_id
export interface MovieModel extends Model {
}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
```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.
```ts
import Storehouse from '@storehouse/core';
import { MongooseManager } from '@storehouse/mongoose';
import { Connection } from 'mongoose';
import { IMovie, MovieModel } from './movies';// connection
const conn = await Storehouse.getConnection().asPromise();
if (conn) {
console.log('retrieved connection for database', conn.name);
}// manager
const localManager = Storehouse.getManager('local');
if (localManager) {
// model
const moviesModel = localManager.getModel('movies');
if (moviesModel) {
console.log('nb movies', await moviesModel.countDocuments());
}
}// model
const Movies = Storehouse.getModel('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.```ts
import Storehouse from '@storehouse/core';
import { getConnection, getManager, getModel } from '@storehouse/mongoose';
import { IMovie, MovieModel } 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('movies');// model
const Movies = getModel(Storehouse, 'local', 'movies');
console.log('nb movies', await Movies.countDocuments());
```### Aggregation
A method from `@storehouse/mongoose`'s model.
```ts
import Storehouse from '@storehouse/core';
import { getModel } from '@storehouse/mongoose';
import { IMovie, MovieModel, MovieWithId } from './movies';const Movies = getModel(Storehouse, 'local', 'movies');
const movies = await Movies.aggregation().match({});
```## References
- [Documentation](https://kisiwu.github.io/storehouse/mongoose/latest/)
- [@storehouse/core](https://www.npmjs.com/package/@storehouse/core)
- [mongoose](https://mongoosejs.com/)