Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wterberg/mongooseloader
Allows you to easily load all your schemas and/or models into mongoose.
https://github.com/wterberg/mongooseloader
model mongo mongodb mongoose nodejs schema
Last synced: about 1 month ago
JSON representation
Allows you to easily load all your schemas and/or models into mongoose.
- Host: GitHub
- URL: https://github.com/wterberg/mongooseloader
- Owner: WterBerg
- License: mit
- Created: 2017-10-04T12:08:35.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-14T17:10:41.000Z (about 7 years ago)
- Last Synced: 2024-12-19T15:42:30.697Z (about 1 month ago)
- Topics: model, mongo, mongodb, mongoose, nodejs, schema
- Language: JavaScript
- Homepage:
- Size: 85.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @wrpterberg/mongooseloader
[![Build Status](https://travis-ci.org/WterBerg/mongooseloader.svg?branch=master)](https://travis-ci.org/WterBerg/mongooseloader)Allows you to easily load all your schemas and/or models into mongoose.
## Example usage
### Load all schemas and models
```javascript
const mongoose = require('mongoose'),
loader = require('@wrpterberg/mongooseloader');async function prepareMongoose() {
loader.enableLogging();
await loader.loadSchemas('./schemas');
await loader.loadModels('./models');
}prepareMongoose()
.then(() => {
let myModel = mongoose.model('MyModel');
})
.catch((err) => {
console.log('Aaah!! My database is on fire!');
console.log(err);
});
```All schemas and models found in their source directories are now part of your mongoose object.
### Load specific schemas and models
```javascript
const mongoose = require('mongoose'),
loader = require('@wrpterberg/mongooseloader');async function prepareSomeSchemasAndModels() {
loader.enableLogging();
loader.setSchemaSource('./schemas');
loader.setModelSource('./models');
await loader.loadSchema('MyModel.js');
await loader.loadSchema('MyOtherModel.js');
await loader.loadModel('MyModel.js');
await loader.loadModel('MyOtherModel.js');
}prepareSomeSchemasAndModels()
.then(() => {
let myModel = mongoose.model('MyModel');
})
.catch((err) => {
console.log('Aaah!! My database is on fire!');
console.log(err);
});
```The specific schemas and models are now part of your mongoose object.
### Logging
use `loader.enableLogging()` to allow MongooseLoader to log its actions and errors into your `console.log`. This can
also be disabled again by calling `loader.disableLogging()`. Logging is disabled by default.## Schema
The name of a schema is resolved to its filename without the file's extension.A valid schema must follow the rules and template stated below:
**Rules:**
- file extension must be '.js'
- A schema can't already be defined with the same name
- A schema must export the functions `getRequiredSchemas()` and `getSchema()`
- If a schema uses another schema as part of its schema, it must be defined in `getRequiredSchemas()`### Template
```javascript
let schema = {};
schema.getRequiredSchemas = () => {
return ['Item'];
};
schema.getSchema = (mongoose, schemas) => {
let mySchema = mongoose.Schema({
items: [schemas['Item']]
});
mySchema.methods.myMethod = (myArgs) => {
// my method
};
mySchema.statics.myStaticsMethod = (myArgs) => {
// myStaticsMethod
};
return mySchema;
};module.exports = schema;
```## Model
The name of a model is resolved to its filename without the file's extension.A valid model must follow the rules and template stated below:
**Rules:**
- file extension must be '.js'
- The modelfile must export a function
- A model can't already be defined with the same name
- There must already be defined a schema with a matching name of the model### Template
```javascript
module.exports = (mongoose, schema) => {
let model = mongoose.model('myModel', schema);model.on('myEvent', (myArgs) => {
// my event handler
});return model;
};
```## Installation
```node
npm install @wrpterberg/mongooseloader --save
```## Test
```node
npm test
```