https://github.com/tswayne/mongoose-active-record
Activerecord-like bindings for mongoose
https://github.com/tswayne/mongoose-active-record
Last synced: about 1 month ago
JSON representation
Activerecord-like bindings for mongoose
- Host: GitHub
- URL: https://github.com/tswayne/mongoose-active-record
- Owner: tswayne
- Created: 2019-11-14T03:55:11.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-02-01T15:01:06.000Z (over 2 years ago)
- Last Synced: 2024-04-24T21:02:05.720Z (about 1 year ago)
- Language: JavaScript
- Size: 12.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/mongoose-active-record)
# Mongoose Activerecord
A wrapper library to provide an elegant activerecord-like approach to creating mongoose models.Traditional mongoose model
```javascript
const animalSchema = new Schema({ name: String, type: String });
// Creating an instance method
animalSchema.methods.findSimilarTypes = function() {
return this.model('Animal').find({ type: this.type });
};// Creating a static method
animalSchema.statics.findByType = function(type) {
return this.find({ type });
};
const Animal = mongoose.model('Animal', schema);const dog = new Animal({ type: 'dog' });
const dogs = await dog.findSimilarTypes()
const otherDogs = await Animal.findByType('dog');
```Mongoose ActiveRecord model
```javascript
class Animal {
//Schema definition
static get name() { return String }
static get type() { return { type: String, index: true } }//Create a static method
static findByType(type) {
return this.find({ type })
}//Create an instance method
findSimilarTypes() {
return this.model('Animal').find({ type: this.type });
}
}const { Animal } = await mongooseActiveRecord(options)
const dog = new Animal({ type: 'dog' });
const dogs = await dog.findSimilarTypes()
const otherDogs = await Animal.findByType('dog');
```## Usage
```javascript
const mongooseActiveRecord = require('mongoose-active-record')const options = {
modelPath: path.join(__dirname, './models'),
connectionUrl: 'mongodb://localhost/test',
connectionOptions: {
useNewUrlParser: true
},
schemaOptions: {
timestamps: true
}
}const models = await mongooseActiveRecord(options)
const { Pet } = models
await Pet.find({})
```#### Model definition
```javascript
// ./models/Pet.js
class Pet{
static get someField() { return String }
static staticMethod() {
console.log('do something')
}instanceMethod() {
console.log('do something else')
}
}
module.exports = Pet
```Specifics:
* Static getters are converted into the schema. The getter can return any [schema type](https://mongoosejs.com/docs/schematypes.html)
* Static methods are converted into [static schema methods](https://mongoosejs.com/docs/guide.html#statics)
* Standard class methods are converted into [schema instance methods](https://mongoosejs.com/docs/guide.html#methods)
* The models returned from mongooseActiveRecord are just [mongoose models](https://mongoosejs.com/docs/models.html)## Options
* modelPath: Path to the directory where your models are defined.
* connectionUrl: Any valid [mongoose connection string](https://mongoosejs.com/docs/connections.html#connection-string-options)
* connectionOptions: Any valid [mongoose connection option](https://mongoosejs.com/docs/connections.html#options)
* schemaOptions: any valid [mongoose schema option](https://mongoosejs.com/docs/guide.html#options)## Contributing
Pull requests or issues welcome!