{"id":15605516,"url":"https://github.com/tswayne/mongoose-active-record","last_synced_at":"2025-07-03T02:05:28.591Z","repository":{"id":57301961,"uuid":"221607468","full_name":"tswayne/mongoose-active-record","owner":"tswayne","description":"Activerecord-like bindings for mongoose","archived":false,"fork":false,"pushed_at":"2023-02-01T15:01:06.000Z","size":13,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-10T02:39:31.220Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tswayne.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-14T03:55:11.000Z","updated_at":"2019-11-30T18:36:37.000Z","dependencies_parsed_at":"2023-02-17T07:00:35.655Z","dependency_job_id":null,"html_url":"https://github.com/tswayne/mongoose-active-record","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tswayne/mongoose-active-record","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tswayne%2Fmongoose-active-record","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tswayne%2Fmongoose-active-record/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tswayne%2Fmongoose-active-record/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tswayne%2Fmongoose-active-record/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tswayne","download_url":"https://codeload.github.com/tswayne/mongoose-active-record/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tswayne%2Fmongoose-active-record/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263245254,"owners_count":23436511,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T04:07:48.410Z","updated_at":"2025-07-03T02:05:28.560Z","avatar_url":"https://github.com/tswayne.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm](https://img.shields.io/npm/v/mongoose-active-record.svg)](https://www.npmjs.com/package/mongoose-active-record)\n\n# Mongoose Activerecord\nA wrapper library to provide an elegant activerecord-like approach to creating mongoose models.\n\nTraditional mongoose model\n```javascript\nconst animalSchema = new Schema({ name: String, type: String });\n// Creating an instance method\nanimalSchema.methods.findSimilarTypes = function() {\n  return this.model('Animal').find({ type: this.type });\n};\n\n// Creating a static method\nanimalSchema.statics.findByType = function(type) {\n  return this.find({ type });\n};\nconst Animal = mongoose.model('Animal', schema);\n\nconst dog = new Animal({ type: 'dog' });\nconst dogs = await dog.findSimilarTypes()\nconst otherDogs = await Animal.findByType('dog');\n```\n\nMongoose ActiveRecord model\n```javascript\nclass Animal {\n  //Schema definition \n  static get name() { return String }\n  static get type() { return { type: String, index: true } }\n\n  //Create a static method\n  static findByType(type) {\n    return this.find({ type })\n  }\n\n  //Create an instance method\n  findSimilarTypes() {\n    return this.model('Animal').find({ type: this.type });\n  }\n}\n\nconst { Animal } = await mongooseActiveRecord(options)\n\nconst dog = new Animal({ type: 'dog' });\nconst dogs = await dog.findSimilarTypes()\nconst otherDogs = await Animal.findByType('dog');\n```\n\n## Usage \n```javascript\nconst mongooseActiveRecord = require('mongoose-active-record')\n\nconst options = {\n  modelPath: path.join(__dirname, './models'),\n  connectionUrl: 'mongodb://localhost/test',\n  connectionOptions: {\n    useNewUrlParser: true\n  },\n  schemaOptions: {\n    timestamps: true\n  }\n}\n\nconst models = await mongooseActiveRecord(options)\nconst { Pet } = models\nawait Pet.find({})\n```\n\n#### Model definition\n```javascript\n// ./models/Pet.js\nclass Pet{\n  static get someField() { return String }\n  static staticMethod() {\n   console.log('do something')\n  }\n\n  instanceMethod() {\n    console.log('do something else')\n  }\n}\nmodule.exports = Pet\n```\n\nSpecifics:\n* Static getters are converted into the schema.  The getter can return any [schema type](https://mongoosejs.com/docs/schematypes.html)\n* Static methods are converted into [static schema methods](https://mongoosejs.com/docs/guide.html#statics)\n* Standard class methods are converted into [schema instance methods](https://mongoosejs.com/docs/guide.html#methods)\n* The models returned from mongooseActiveRecord are just [mongoose models](https://mongoosejs.com/docs/models.html) \n\n## Options\n* modelPath: Path to the directory where your models are defined.\n* connectionUrl: Any valid [mongoose connection string](https://mongoosejs.com/docs/connections.html#connection-string-options) \n* connectionOptions: Any valid [mongoose connection option](https://mongoosejs.com/docs/connections.html#options)\n* schemaOptions: any valid [mongoose schema option](https://mongoosejs.com/docs/guide.html#options)\n\n## Contributing\nPull requests or issues welcome!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftswayne%2Fmongoose-active-record","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftswayne%2Fmongoose-active-record","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftswayne%2Fmongoose-active-record/lists"}