Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/midgarjs/mongo
https://github.com/midgarjs/mongo
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/midgarjs/mongo
- Owner: midgarjs
- License: mit
- Created: 2020-01-02T15:58:21.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T05:21:00.000Z (almost 2 years ago)
- Last Synced: 2024-11-18T02:41:05.778Z (about 2 months ago)
- Language: JavaScript
- Size: 2.49 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://drone.midgar.io/api/badges/Midgar/mongo/status.svg)](https://drone.midgar.io/Midgar/mongo)
[![Coverage](https://sonar.midgar.io/api/project_badges/measure?project=midgar-mongo&metric=coverage)](https://sonar.midgar.io/dashboard?id=midgar-mongo)## @midgar/mongo
Intégration de [Moogoose](https://mongoosejs.com) pour [Midgar](https://github.com/midgarjs/midgar)
## Installation
```sh
$ npm i @midgar/mongo
```
Si tout s'est bien passé, un message de confirmation s'affiche:```sh
#midgar-cli
@midgar/mongo added to plugins.json !
```## Configuration
Vous devez ajouter la confuguration suviante a votre projet avec un uri valide:
```js
mongo: {
default: {
uri: 'mongodb://localhost:27017/midgar?connectTimeoutMS=1000',
}
}
```Vous devez ajouter plusieur connection à la configuration:
```js
mongo: {
default: {
uri: 'mongodb://localhost:27017/midgar?connectTimeoutMS=1000',
},
otherConnexion: {
uri: 'mongodb://localhost:27017/otherDb?connectTimeoutMS=1000',
}
}
```## Fonctionnement
Ce plugin ajoute un type de module **midgar-mongo-model** dans le dossier ./mongo-models/.## Model
Voici un exemple de model:
```js
const name = 'namespace:monmodel'export default {
name,
model: (mongoose) => {
const schema = mongoose.Schema({
name: {
type: String,
required: true
},
description: {
type: String,
required: true
},
type: {
type: String,
required: true
}
})return mongoose.model(name, schema)
}
}
```Vous trouverez plus d'informations sur la structure des model mongoose dans la [documentation](https://mongoosejs.com/docs/guide.html).
## Migration
Ce plugin ajoute un storage pour [@midgar/migrate](https://github.com/midgarjs/migrate)
### Exemple de fichier
```js
export default {
up: async (mid, mongoService) => {
const MonModel = mongoService.getModel('namespace:monmodel')
const pulpFiction = new MonModel({
name: 'Pulp Fiction',
description: 'L\'odyssée sanglante et burlesque de petits malfrats dans la jungle de Hollywoo.'
type: 'film'
})await pulpFiction.save()
},
down: async (mid, mongoService) => {
const pulpFiction = mongoService.getModel('namespace:monmodel')
pulpFiction.deleteOne({ name: 'Pulp Fiction' })
}
}```