Mongoose service that implements @rxstack/platform adapter API and querying syntax.
This adapter also requires a running MongoDB database server.
npm install @rxstack/mongoose-service --save
MongooseServiceModule needs to be registered in the application. Let's create the application:
import {Application, ApplicationOptions} from '@rxstack/core';
import {MongooseServiceModule} from '@rxstack/mongoose-service';
export const APP_OPTIONS: ApplicationOptions = {
imports: [
MongooseServiceModule.configure({
connection: {
uri: process.env.MONGO_HOST, // mongodb://localhost:27017/test
// mongoose options
options: { }
},
})
],
providers: [
// ...
]
};
new Application(APP_OPTIONS).start();connection.url: mongodb server uriconnection.options: mongoose options (optional)logger.enabled: enable query logging (defaults to false)logger.level: logging level (defaults to debug)
In addition to service base options we need to set the following options:
model: mongoose model
First we need to create model interface and InjectionToken:
import {InjectionToken} from 'injection-js';
import {MongooseService} from '@rxstack/mongoose-service';
export interface Product {
id: string;
name: string;
}
export const PRODUCT_SERVICE = new InjectionToken<MongooseService<Product>>('PRODUCT_SERVICE');import { Schema } from 'mongoose';
const { v4: uuid } = require('uuid');
export const productMongooseSchema = new Schema({
_id: {
type: String,
default: uuid
},
name: {
type: String,
unique: true,
required: true,
}
}, {_id: false, versionKey: false });then register the service in the application provides:
import {ApplicationOptions} from '@rxstack/core';
import {MongooseService} from '@rxstack/mongoose-service';
import {Connection} from 'mongoose';
export const APP_OPTIONS: ApplicationOptions = {
// ...
providers: [
{
provide: PRODUCT_SERVICE,
useFactory: (conn: Connection) => {
return new MongooseService({
idField: '_id', defaultLimit: 25, model: conn.model('Product', productMongooseSchema)
});
},
deps: [Connection],
},
]
};import {Connection} from 'mongoose';
import {Injectable} from 'injection-js';
import {Http, Request, Response, WebSocket, InjectorAwareInterface} from '@rxstack/core';
@Injectable()
export class ProductController implements InjectorAwareInterface {
@Http('POST', '/product', 'app_product_create')
@WebSocket('app_product_create')
async createAction(request: Request): Promise<Response> {
// getting connection
const connection = injector.get(Connection);
// standard use
const service = this.injector.get(PRODUCT_SERVICE);
await service.insertOne(request.body);
}
}Read more about platform services
Helpful commands managing your mongoose database
Makes the indexes in MongoDB match the indexes defined in this model's schema
npm run cli mongoose:ensure-indexesDrop databases, collections and indexes for your documents.
npm run cli mongoose:dropValidationObserver converts mongoose errors to BadRequestException.
In order to return proper validation errors and status code 400 we catch the exception and throw BadRequestException.
The error messages can be accessed exception.data['errors'] and implement ValidationError[].
Licensed under the MIT license.