{"id":18441588,"url":"https://github.com/stefanwalther/mongoose-connection-promise","last_synced_at":"2025-08-03T17:34:29.715Z","repository":{"id":51811111,"uuid":"81891453","full_name":"stefanwalther/mongoose-connection-promise","owner":"stefanwalther","description":"Convenience library to connect Mongoose to a MongoDB instance using promises.","archived":false,"fork":false,"pushed_at":"2025-03-25T02:29:24.000Z","size":148,"stargazers_count":5,"open_issues_count":11,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-03T17:54:32.402Z","etag":null,"topics":["mongodb","mongoose","promise"],"latest_commit_sha":null,"homepage":"","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/stefanwalther.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.yml","contributing":null,"funding":null,"license":null,"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":"2017-02-14T01:40:55.000Z","updated_at":"2019-10-09T08:19:30.000Z","dependencies_parsed_at":"2024-02-16T11:23:14.138Z","dependency_job_id":"a5c3d288-57ee-439d-910f-f63e1fbfdb31","html_url":"https://github.com/stefanwalther/mongoose-connection-promise","commit_stats":{"total_commits":83,"total_committers":7,"mean_commits":"11.857142857142858","dds":0.6987951807228916,"last_synced_commit":"00addaab32510dc5c7c3cb8248c776d034885048"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefanwalther%2Fmongoose-connection-promise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefanwalther%2Fmongoose-connection-promise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefanwalther%2Fmongoose-connection-promise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefanwalther%2Fmongoose-connection-promise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stefanwalther","download_url":"https://codeload.github.com/stefanwalther/mongoose-connection-promise/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247741451,"owners_count":20988403,"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":["mongodb","mongoose","promise"],"created_at":"2024-11-06T06:38:40.975Z","updated_at":"2025-04-07T22:32:20.189Z","avatar_url":"https://github.com/stefanwalther.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## mongoose-connection-promise\n\n\u003e Convenience library to connect Mongoose to a MongoDB instance using promises.\n\n[![NPM version](https://img.shields.io/npm/v/mongoose-connection-promise.svg?style=flat)](https://www.npmjs.com/package/mongoose-connection-promise)\n[![David](https://img.shields.io/david/stefanwalther/mongoose-connection-promise.svg)](https://github.com/stefanwalther/mongoose-connection-promise)\n[![CircleCI](https://img.shields.io/circleci/project/github/stefanwalther/mongoose-connection-promise.svg)](https://circleci.com/gh/stefanwalther/mongoose-connection-promise/tree/master)\n[![codecov](https://codecov.io/gh/stefanwalther/mongoose-connection-promise/branch/master/graph/badge.svg)](https://codecov.io/gh/stefanwalther/mongoose-connection-promise)\n[![XO code style](https://img.shields.io/badge/code_style-XO--space-5ed9c7.svg)](https://github.com/sindresorhus/eslint-config-xo-space)\n\n***\n\n**Note:** With the introduction of mongoose \u003e 5.x this library becomes obsolete as the handling of connections has been greatly improved in mongoose \u003e 5.x.\n[Read here](http://thecodebarbarian.com/whats-new-in-mongoose-5-improved-connections.html) for a good summary of the changes/improvements introduced.\n\nTherefore this library will not be updated for mongoose \u003e 5.x.\n\n***\n\n## Table of Contents\n\n\u003cdetails\u003e\n\n- [Install](#install)\n- [Motivation](#motivation)\n- [Usage](#usage)\n- [API](#api)\n- [Test](#test)\n- [Author](#author)\n- [License](#license)\n\n_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_\n\n\u003c/details\u003e\n\n## Install\n\nInstall with [npm](http://npmjs.org/)\n```sh\n$ npm install mongoose-connection-promise\n```\n\nInstall with [yarn](https://yarnpkg.com)\n```sh\n$ yarn add mongoose-connection-promise\n```\n\n## Motivation\n\nAlthough mongoose does not force you to wait until a mongoose connection has been created, the author of this module prefers to not start with any application before we know that a connection has been established successfully.\n\n_mongoose-connection-promise_ helps connecting and disonnecting to MongoDB in mongoose reliably.\n\n## Usage\n\n### Use mongoose-connection-promise in express.js\n\nConnect mongoose to a MongoDB instance using the default settings:\n\n```js\n\nconst express = require('express');\nconst MongooseConnection = require('mongoose-connection-promise');\n\nconst app = express();\n\n// Initialize using the default settings, which is assuming MongoDB to run\n// at mongodb://localhost:27017\nconst mongooseConnection = new MongooseConnection();\n\nconsole.log(mongooseConnection.config.host); // Returns localhost\nconsole.log(mongooseConnection.config.port): // Returns 27017\n\nmongooseConnection.connect()\n  .then(connection =\u003e {\n    app.db = connection;\n    const port = 3003;\n    app.listen(port, err =\u003e {\n      if (err) {\n        console.log('Could not start express server');\n      } else {\n        console.log(`Express server started at port ${port}`);\n      }\n    });\n  })\n  .catch(err =\u003e {\n    console.log('Error creating a mongoose connection', err);\n  });\n\n```\n\nPass in options:\n\n```js\nconst MongooseConnection = require('mongoose-connection-promise');\n\nconst opts = {\n  username: 'foo',\n  password: 'bar',\n  host: 'mongo.local',\n  port: 27018,\n  debug: true\n};\n\nconst mongooseConnection = new MongooseConnection(opts);\n\nmongooseConnection.connect()\n  .then(connection =\u003e {\n    // successfully connected\n  })\n  .catch(err =\u003e {\n    // an error occurred\n  });\n\n```\n\n## API\n\n### [Configuration](lib/index.js#L81)\n\nDefine a configuration object to pass to the constructor.\n\nIf no options are defined, the default options will be used:\nSee [index.js =\u003e defaultOpts](index.js) for more information about the current default options.\n\n**Params**\n\n* `opts` **{Object}**: Options to pass in.\n* `opts.debug` **{Boolean}**: Whether MongoDB runs in debug mode or not.\n* `opts.host` **{String}**: The MongoDBhost, defaults to `localhost`.  See the mongodb [connection string spec](https://docs.mongodb.com/manual/reference/connection-string/) for more details.\n* `opts.port` **{Number}**: The MongoDB port, defaults to `27017`.  See the mongodb [connection string spec](https://docs.mongodb.com/manual/reference/connection-string/) for more details.\n* `opts.database` **{String}**: The MongoDB database, defaults to `admin`.  See the mongodb [connection string spec](https://docs.mongodb.com/manual/reference/connection-string/) for more details.\n* `opts.connectOptions` **{Object}**: The MongoDB connection properties, being passed through to the native MongoDB driver. See [mongoose' documentation](http://mongoosejs.com/docs/connections.html), resp. [MongoDB's native driver for node.js' documentation](https://github.com/mongodb/node-mongodb-native) for more details.\n\n**Example**\n\n```js\n// Default Options:\nconst defaultOpts = {\n   debug: false,\n   host: 'localhost',\n   port: 27017,\n   database: '',\n   connectOptions: {\n     db: {},\n     server: {\n       auto_reconnect: true\n     },\n     replset: {},\n     user: {},\n     pass: {},\n     auth: {},\n     mongos: {}\n   }\n};\n```\n\n### [.constructor()](lib/index.js#L90)\n\nInitialize a new MongooseConnection.\n\n**Params**\n\n* **{Configuration}**: opts - Options to initialize _MongooseConnection_.\n\n### [.DEFAULT_CONFIG](lib/index.js#L102)\n\nReturns the default options of mongoose-connection-promise\n\n### [.connect()](lib/index.js#L115)\n\nConnect mongoose to the given instance of MongoDB.\n\n* `returns` **{Promise}**\n\n### [.get()](lib/index.js#L137)\n\nGet an existing connection or create a new one.\n\nIn contrary to `.connect()` this method will not create a new connection if MongooseConnection is already connected,\nbut the existing connection will be re-used and returned.\n\n* `returns` **{Promise\u003cNavtiveConnection,Error\u003e}**: Returns the connection to MongoDB.\n\n### [.disconnect()](lib/index.js#L151)\n\nDisconnects all mongoose connections.\n\n* `returns` **{Promise\u003cvoid,Error\u003e}**\n\n### [.isConnected()](lib/index.js#L162)\n\nIndicates whether there is a current and ready-to-use mongoose connection.\n\n* `returns` **{boolean}**\n\n### [.defaultOptions()](lib/index.js#L174)\n\nReturn the default options (DEPRECATED).\n\n* `returns`: object\n\n## Test\n\nStart the MongoDB docker container:\n\n```sh\n$ npm run dc-dev-up\n```\n\nThen run the tests:\n\n```\n$ npm run test\n```\n\n## Author\n\n**Stefan Walther**\n\n* [github/stefanwalther](https://github.com/stefanwalther)\n* [twitter/waltherstefan](http://twitter.com/waltherstefan)\n\n## License\n\nMIT\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on February 22, 2018._","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstefanwalther%2Fmongoose-connection-promise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstefanwalther%2Fmongoose-connection-promise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstefanwalther%2Fmongoose-connection-promise/lists"}