{"id":15573602,"url":"https://github.com/jedireza/mongo-models","last_synced_at":"2025-08-20T16:31:56.277Z","repository":{"id":11067480,"uuid":"68186618","full_name":"jedireza/mongo-models","owner":"jedireza","description":":package: Map JavaScript classes to MongoDB collections","archived":false,"fork":false,"pushed_at":"2023-03-04T02:32:32.000Z","size":456,"stargazers_count":67,"open_issues_count":11,"forks_count":28,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-12-10T02:11:01.662Z","etag":null,"topics":["hapi","javascript","mongodb","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jedireza.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-14T08:05:46.000Z","updated_at":"2023-11-03T17:12:32.000Z","dependencies_parsed_at":"2024-06-18T20:11:42.905Z","dependency_job_id":null,"html_url":"https://github.com/jedireza/mongo-models","commit_stats":{"total_commits":55,"total_committers":5,"mean_commits":11.0,"dds":0.07272727272727275,"last_synced_commit":"86528c0e0b50e63c7ce7caa2854f8f70a4aa8b7c"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedireza%2Fmongo-models","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedireza%2Fmongo-models/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedireza%2Fmongo-models/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedireza%2Fmongo-models/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jedireza","download_url":"https://codeload.github.com/jedireza/mongo-models/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230438185,"owners_count":18225870,"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":["hapi","javascript","mongodb","nodejs"],"created_at":"2024-10-02T18:13:41.872Z","updated_at":"2024-12-19T13:07:46.256Z","avatar_url":"https://github.com/jedireza.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongo-models\n\nJavaScript class interfaces to MongoDB collections.\n\n[![Build Status](https://img.shields.io/travis/jedireza/mongo-models.svg)](https://travis-ci.org/jedireza/mongo-models)\n[![Dependency Status](https://img.shields.io/david/jedireza/mongo-models.svg)](https://david-dm.org/jedireza/mongo-models)\n[![devDependency Status](https://img.shields.io/david/dev/jedireza/mongo-models.svg)](https://david-dm.org/jedireza/mongo-models#info=devDependencies)\n[![peerDependency Status](https://img.shields.io/david/peer/jedireza/mongo-models.svg)](https://david-dm.org/jedireza/mongo-models#info=peerDependencies)\n\n[MongoDB](https://github.com/mongodb/node-mongodb-native)'s native driver for\nNode.js is pretty good. We just want a little sugar on top.\n\n[Mongoose](http://mongoosejs.com/) is awesome, and big. It's built on top of\nMongoDB's native Node.js driver. It's a real deal ODM with tons of features.\nYou should check it out.\n\nWe wanted something in between the MongoDB driver and Mongoose. A light weight\nabstraction where we can interact with collections via JavaScript classes and\nget document results as instances of those classes.\n\nWe're also big fans of the object schema validation library\n[joi](https://github.com/hapijs/joi). Joi works well for defining a model's\ndata schema.\n\n\n## API reference\n\nhttps://github.com/jedireza/mongo-models/blob/master/API.md\n\nSee the old [v1.x API\nreference](https://github.com/jedireza/mongo-models/blob/996c21e0e18c4cf70e665d319c7cf5eaa9cff846/API.md).\n\n\n## Install\n\n```bash\n$ npm install mongo-models\n```\n\n\n## Usage\n\n### Creating models\n\nYou extend the `MongoModels` class to create new model classes that map to\nMongoDB collections.\n\nLet's create a `Customer` model.\n\n```js\n'use strict';\nconst Joi = require('joi');\nconst MongoModels = require('mongo-models');\n\nconst schema = Joi.object({\n    _id: Joi.object(),\n    name: Joi.string().required(),\n    email: Joi.string().email(),\n    phone: Joi.string()\n});\n\nclass Customer extends MongoModels {\n    static create(name, email, phone) {\n\n        const document = new Customer({\n            name,\n            email,\n            phone\n        });\n\n        return this.insertOne(document);\n    }\n\n    speak() {\n\n        console.log(`${this.name}: call me at ${this.phone}.`);\n    }\n}\n\nCustomer.collectionName = 'customers'; // the mongodb collection name\nCustomer.schema = schema;\n\nmodule.exports = Customer;\n```\n\n\n## Example\n\n```js\n'use strict';\nconst BodyParser = require('body-parser');\nconst Customer = require('./customer');\nconst Express = require('express');\nconst MongoModels = require('mongo-models');\n\nconst app = Express();\nconst connection = {\n    uri: process.env.MONGODB_URI,\n    db: process.env.MONGODB_NAME\n};\n\napp.use(BodyParser.json());\n\napp.post('/customers', async (req, res) =\u003e {\n\n    const name = req.body.name;\n    const email = req.body.email;\n    const phone = req.body.phone;\n    let customers;\n\n    try {\n        customers = await Customer.create(name, email, phone);\n    }\n    catch (err) {\n        res.status(500).json({ error: 'something blew up' });\n        return;\n    }\n\n    res.json(customers[0]);\n});\n\napp.get('/customers', async (req, res) =\u003e {\n\n    const name = req.query.name;\n    const filter = {};\n\n    if (name) {\n        filter.name = name;\n    }\n\n    let customers;\n\n    try {\n        customers = await Customer.find(filter);\n    }\n    catch (err) {\n        res.status(500).json({ error: 'something blew up' });\n        return;\n    }\n\n    res.json(customers);\n});\n\nconst main = async function () {\n\n    await MongoModels.connect(connection, {});\n\n    console.log('Models are now connected.');\n\n    await app.listen(process.env.PORT);\n\n    console.log(`Server is running on port ${process.env.PORT}`);\n};\n\nmain();\n```\n\n### Run the example\n\nTo run the example, first clone this repo and install the dependencies.\n\n```bash\n$ git clone https://github.com/jedireza/mongo-models.git\n$ cd mongo-models\n$ npm install\n```\n\nThe example is a simple Express API that uses the Customer model we created\nabove. [View the\ncode.](https://github.com/jedireza/mongo-models/tree/master/example)\n\n```bash\n$ npm run example\n```\n\n\n## Have a question?\n\nAny issues or questions (no matter how basic), open an issue. Please take the\ninitiative to read relevant documentation and be pro-active with debugging.\n\n\n## Want to contribute?\n\nContributions are welcome. If you're changing something non-trivial, you may\nwant to submit an issue before creating a large pull request.\n\n\n## License\n\nMIT\n\n\n## Don't forget\n\nWhat you create with `mongo-models` is more important than `mongo-models`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjedireza%2Fmongo-models","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjedireza%2Fmongo-models","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjedireza%2Fmongo-models/lists"}