{"id":18801840,"url":"https://github.com/mitscherlich/koa2-session-mongo","last_synced_at":"2025-04-13T17:32:26.499Z","repository":{"id":33894493,"uuid":"163393610","full_name":"Mitscherlich/koa2-session-mongo","owner":"Mitscherlich","description":"🌿 MongoDB storage layer for Koa session middleware","archived":false,"fork":false,"pushed_at":"2024-10-29T22:31:36.000Z","size":792,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"dev","last_synced_at":"2024-10-30T00:44:59.598Z","etag":null,"topics":[],"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/Mitscherlich.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2018-12-28T09:41:07.000Z","updated_at":"2024-10-26T03:10:38.000Z","dependencies_parsed_at":"2024-01-10T07:34:55.439Z","dependency_job_id":"3cbee335-f29b-46d2-8e84-1a70697b1b0b","html_url":"https://github.com/Mitscherlich/koa2-session-mongo","commit_stats":{"total_commits":121,"total_committers":4,"mean_commits":30.25,"dds":0.4628099173553719,"last_synced_commit":"0781009499d48eb38a571d66f54f26eff3420c2a"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mitscherlich%2Fkoa2-session-mongo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mitscherlich%2Fkoa2-session-mongo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mitscherlich%2Fkoa2-session-mongo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mitscherlich%2Fkoa2-session-mongo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Mitscherlich","download_url":"https://codeload.github.com/Mitscherlich/koa2-session-mongo/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223598723,"owners_count":17171320,"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-11-07T22:25:29.082Z","updated_at":"2024-11-07T22:25:29.543Z","avatar_url":"https://github.com/Mitscherlich.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# koa2-session-mongo\n\n[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][codecov-image]][codecov-url]\n[![David deps][david-image]][david-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/koa2-session-mongo.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/koa2-session-mongo\n[travis-image]: https://img.shields.io/travis/Mitscherlich/koa2-session-mongo.svg?style=flat-square\n[travis-url]: https://travis-ci.org/Mitscherlich/koa2-session-mongo\n[codecov-image]: https://img.shields.io/codecov/c/github/Mitscherlich/koa2-session-mongo.svg?style=flat-square\n[codecov-url]: https://codecov.io/github/Mitscherlich/koa2-session-mongo?branch=dev\n[david-image]: https://img.shields.io/david/Mitscherlich/koa2-session-mongo.svg?style=flat-square\n[david-url]: https://david-dm.org/Mitscherlich/koa2-session-mongo\n[snyk-image]: https://snyk.io/test/npm/koa2-session-mongo/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/koa2-session-mongo\n[download-image]: https://img.shields.io/npm/dm/koa2-session-mongo.svg?style=flat-square\n[download-url]: https://npmjs.org/package/koa2-session-mongo\n\nMongoDB session store for [Koa session middleware](https://github.com/hiddentao/koa-session-store). Based on [connect-mongo](https://github.com/jdesboeufs/connect-mongo).\n\nThis repo is forked from [hiddentao/Koa session middleware](https://github.com/hiddentao/koa-session-store). Go origin repository see preview version.\n\nSee Chinses version: [中文](docs/README.zh-CN.md)\n\n## Compatibility\n\n- Support Koa up to `2.x`\n- Support [Mongoose](http://mongoosejs.com/index.html) `\u003e= 4.1.2+`\n- Support [native MongoDB driver](http://mongodb.github.io/node-mongodb-native/) `\u003e= 2.0.36`\n- Support Node.js 8 and 10 (**`async`** keyword required)\n- Support [MongoDB](https://www.mongodb.com/) `\u003e= 3.0`\n\nFor extended compatibility, see previous versions.\n\n## Installation\n\n```bash\n# for npm\nnpm install --save koa2-session-mongo\n# for yarn\nyarn add koa2-session-mongo\n```\n\n## Usage\n\n### Koa integration\n\nKoa `2.x`:\n\n```js\nconst session = require(\"koa-session-store\");\n\nconst MongoStore = require(\"koa2-session-mongo\");\nconst Koa = require(\"koa\");\n\nconst app = new Koa();\n\napp.keys = [\"some secret key\"]; // needed for cookie-signing\n\napp.use(\n  session({\n    store: new MongoStore({\n      url: \"mongodb://127.0.0.1\", // requierd\n      db: \"database_name\", // required\n    }),\n  })\n);\n\napp.use(async (ctx) =\u003e {\n  let n = ctx.session.views || 0;\n  ctx.session.views = ++n;\n  ctx.body = n + \" views\";\n});\n\napp.listen(3000);\nconsole.log(\"listening on port 3000\");\n```\n\n### Connection to MongoDB\n\nIn many circumstances, `koa2-session-mongo` will not be the only part of your application which need a connection to a MongoDB database. It could be interesting to re-use an existing connection.\n\nAlternatively, you can configure `koa2-session-mongo` to establish a new connection.\n\n#### Re-use a Mongoose connection\n\n```js\nconst mongoose = require(\"mongoose\");\n\n// Basic usage\nmongoose.connect(connectionOptions);\n\napp.use(\n  session({\n    store: new MongoStore({ mongooseConnection: mongoose.connection }),\n  })\n);\n\n// Advanced usage\nconst connection = mongoose.createConnection(connectionOptions);\n\napp.use(\n  session({\n    store: new MongoStore({ mongooseConnection: connection }),\n  })\n);\n```\n\n#### Re-use a native MongoDB driver connection (or a promise)\n\nIn this case, you just have to give your `db` instance to `koa2-session-mongo`.\nIf the connection is not opened, `koa2-session-mongo` will do it for you.\n\n```js\n/*\n ** There are many ways to create dbInstance.\n ** You should refer to the driver documentation.\n */\n\napp.use(\n  session({\n    store: new MongoStore({ db: dbInstance }),\n  })\n);\n```\n\nOr just give a promise...\n\n```js\napp.use(\n  session({\n    store: new MongoStore({ dbPromise: dbInstancePromise }),\n  })\n);\n```\n\n#### Create a new connection from a MongoDB connection string\n\n[MongoDB connection strings](http://docs.mongodb.org/manual/reference/connection-string/) are **the best way** to configure a new connection. For advanced usage, [more options](http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html#mongoclient-connect-options) can be configured with `mongoOptions` property.\n\n```js\n// Basic usage\napp.use(\n  session({\n    store: new MongoStore({ url: \"mongodb://localhost/test-app\" }),\n  })\n);\n\n// Advanced usage\napp.use(\n  session({\n    store: new MongoStore({\n      url:\n        \"mongodb://user12345:foobar@localhost/test-app?authSource=admins\u0026w=1\",\n      mongoOptions: advancedOptions, // See below for details\n    }),\n  })\n);\n```\n\n## Options\n\nThe following configuration options are available for the `new` call:\n\n- **db** `String` or `Object` - db name or instantiated [node-mongo-native](https://github.com/mongodb/node-mongodb-native) db object.\n- **collection** `String` - collection name. Default is _sessions_.\n- **auto_reconnect** `Boolean` - gets passed to the [node-mongo-native](https://github.com/mongodb/node-mongodb-native) constructor as the same option. Default is _false_.\n- **ssl** `Boolean` - use ssl to connect to the server. Default is _false_.\n- **expirationTime** `Number` - time-to-live (TTL) in seconds for any given session data - MongoDB will auto-delete data which hasn't been updated for this amount of time. Default is 2 weeks.\n- **url** `String` - connection URL of the form `mongodb://user:pass@host:port/database/collection`. **If provided then this will take precedence over other options except `mongoose`**.\n- **mongoose** `Object` - a [Mongoose](https://github.com/LearnBoost/mongoose) connection, use\\*\\* `mongoose.connection` to get the connection out of an existing Mongoose object. If provided then this will take precedence over other options.\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitscherlich%2Fkoa2-session-mongo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmitscherlich%2Fkoa2-session-mongo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitscherlich%2Fkoa2-session-mongo/lists"}