Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rxstack/mongoose-service
MongooseService for @rxstack/platform
https://github.com/rxstack/mongoose-service
mongodb mongoose nodejs platform rxstack typescript
Last synced: about 1 month ago
JSON representation
MongooseService for @rxstack/platform
- Host: GitHub
- URL: https://github.com/rxstack/mongoose-service
- Owner: rxstack
- License: mit
- Created: 2019-01-25T14:42:29.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T03:29:52.000Z (over 1 year ago)
- Last Synced: 2024-09-30T11:02:53.919Z (about 2 months ago)
- Topics: mongodb, mongoose, nodejs, platform, rxstack, typescript
- Language: TypeScript
- Homepage:
- Size: 572 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# The RxStack Mongoose Service
[![Node.js CI](https://github.com/rxstack/mongoose-service/actions/workflows/node.js.yml/badge.svg?branch=master)](https://github.com/rxstack/mongoose-service/actions/workflows/node.js.yml)
[![Maintainability](https://api.codeclimate.com/v1/badges/f4b78bc8f5a0dc0d9915/maintainability)](https://codeclimate.com/github/rxstack/mongoose-service/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/f4b78bc8f5a0dc0d9915/test_coverage)](https://codeclimate.com/github/rxstack/mongoose-service/test_coverage)> Mongoose service that implements [@rxstack/platform adapter API and querying syntax](https://github.com/rxstack/rxstack/tree/master/packages/platform#services).
> This adapter also requires a running [MongoDB database server](https://docs.mongodb.com/manual/tutorial/getting-started/#).
## Table of content
- [Installation](#installation)
- [Setup](#setup)
- [Module Options](#module-options)
- [Service Options](#service-options)
- [Usage](#usage)
- [Create interfaces](#usage-create-interfaces)
- [Create mongoose schemas](#usage-schemas)
- [How to use in controller](#usage-controller)
- [Commands](#commands)
- [Ensure Endexes](#commands-ensure-indexes)
- [Drop Database](#commands-drop-database)
- [Validation Observer](#validation-observer)```
npm install @rxstack/mongoose-service --save
```## Setup
`MongooseServiceModule` needs to be registered in the `application`. Let's create the application:```typescript
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 uri
- `connection.options`: mongoose options (optional)
- `logger.enabled`: enable query logging (defaults to false)
- `logger.level`: logging level (defaults to debug)## Service Options
In addition to [service base options](https://github.com/rxstack/rxstack/tree/master/packages/platform#services)
we need to set the following options:- `model`: [mongoose model](https://mongoosejs.com/docs/models.html)
### Create interfaces
First we need to create `model interface` and `InjectionToken`:```typescript
import {InjectionToken} from 'injection-js';
import {MongooseService} from '@rxstack/mongoose-service';export interface Product {
id: string;
name: string;
}export const PRODUCT_SERVICE = new InjectionToken>('PRODUCT_SERVICE');
``````typescript
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:
```typescript
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],
},
]
};
``````typescript
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 {
// 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](https://github.com/rxstack/rxstack/tree/master/packages/platform#services)
## Commands
Helpful commands managing your mongoose database### Ensure Indexes
Makes the indexes in MongoDB match the indexes defined in this model's schema```bash
npm run cli mongoose:ensure-indexes
```### Drop databases
Drop databases, collections and indexes for your documents.```bash
npm run cli mongoose:drop
```## Validation Observer
`ValidationObserver` 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[]`](https://github.com/rxstack/rxstack/tree/master/packages/platform/src).## License
Licensed under the [MIT license](LICENSE).