{"id":13598800,"url":"https://github.com/iamolegga/nestjs-session","last_synced_at":"2025-05-15T11:06:34.285Z","repository":{"id":35156309,"uuid":"214189761","full_name":"iamolegga/nestjs-session","owner":"iamolegga","description":"Idiomatic Session Module for NestJS. Built on top of `express-session` 😎","archived":false,"fork":false,"pushed_at":"2025-05-12T02:08:40.000Z","size":7833,"stargazers_count":236,"open_issues_count":3,"forks_count":15,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-12T03:25:29.475Z","etag":null,"topics":["express","express-session","expressjs","nest","nestjs","session"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/iamolegga.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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,"zenodo":null}},"created_at":"2019-10-10T13:25:58.000Z","updated_at":"2025-05-12T02:08:43.000Z","dependencies_parsed_at":"2023-12-22T04:20:11.662Z","dependency_job_id":"a91438e6-34b2-4555-8a7c-28b3cc340ead","html_url":"https://github.com/iamolegga/nestjs-session","commit_stats":{"total_commits":1043,"total_committers":9,"mean_commits":"115.88888888888889","dds":"0.28283796740172584","last_synced_commit":"12b23c11d39b017666a7abdec56e011016590812"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamolegga%2Fnestjs-session","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamolegga%2Fnestjs-session/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamolegga%2Fnestjs-session/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamolegga%2Fnestjs-session/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iamolegga","download_url":"https://codeload.github.com/iamolegga/nestjs-session/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254328385,"owners_count":22052632,"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":["express","express-session","expressjs","nest","nestjs","session"],"created_at":"2024-08-01T17:00:56.637Z","updated_at":"2025-05-15T11:06:34.259Z","avatar_url":"https://github.com/iamolegga.png","language":"TypeScript","funding_links":[],"categories":["资源","Components \u0026 Libraries"],"sub_categories":["组件和库"],"readme":"\u003ch1 align=\"center\"\u003eNestJS-Session\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.npmjs.com/package/nestjs-session\"\u003e\n    \u003cimg alt=\"npm\" src=\"https://img.shields.io/npm/v/nestjs-session\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/nestjs-session\"\u003e\n    \u003cimg alt=\"npm\" src=\"https://img.shields.io/npm/dm/nestjs-session\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/iamolegga/nestjs-session/actions\"\u003e\n    \u003cimg alt=\"GitHub branch checks state\" src=\"https://badgen.net/github/checks/iamolegga/nestjs-session\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://codeclimate.com/github/iamolegga/nestjs-session/test_coverage\"\u003e\n    \u003cimg src=\"https://api.codeclimate.com/v1/badges/08bcbca7b2da14b3bbfd/test_coverage\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://snyk.io/test/github/iamolegga/nestjs-session\"\u003e\n    \u003cimg alt=\"Known Vulnerabilities\" src=\"https://snyk.io/test/github/iamolegga/nestjs-session/badge.svg\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://libraries.io/npm/nestjs-session\"\u003e\n    \u003cimg alt=\"Libraries.io\" src=\"https://img.shields.io/librariesio/release/npm/nestjs-session\" /\u003e\n  \u003c/a\u003e\n  \u003cimg alt=\"Dependabot\" src=\"https://badgen.net/github/dependabot/iamolegga/nestjs-session\" /\u003e\n  \u003cimg alt=\"Supported platforms: Express\" src=\"https://img.shields.io/badge/platforms-Express-green\" /\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003eIdiomatic Session Module for NestJS. Built on top of \u003ca href=\"https://npm.im/express-session\"\u003eexpress-session\u003c/a\u003e 😎\u003c/p\u003e\n\nThis module implements a session with storing data in one of [external stores](https://github.com/expressjs/session#compatible-session-stores) and passing ID of session to client via `Cookie`/`Set-Cookie` headers.\n\nIf you want to store data directly in `Cookie`, you can look at [nestjs-cookie-session](https://github.com/iamolegga/nestjs-cookie-session).\n\n## Example\n\nRegister module:\n\n```ts\n// app.module.ts\nimport { Module } from '@nestjs/common';\nimport { NestSessionOptions, SessionModule } from 'nestjs-session';\nimport { ViewsController } from './views.controller';\n\n@Module({\n  imports: [\n    // sync params:\n\n    SessionModule.forRoot({\n      session: { secret: 'keyboard cat' },\n    }),\n\n    // or async:\n\n    SessionModule.forRootAsync({\n      imports: [ConfigModule],\n      inject: [Config],\n      //              TIP: to get autocomplete in return object\n      //                  add `NestSessionOptions` here ↓↓↓\n      useFactory: async (config: Config): Promise\u003cNestSessionOptions\u003e =\u003e {\n        return {\n          session: { secret: config.secret },\n        };\n      },\n    }),\n  ],\n  controllers: [ViewsController],\n})\nexport class AppModule {}\n```\n\nIn controllers use NestJS built-in `Session` decorator:\n\n```ts\n// views.controller.ts\nimport { Controller, Get, Session } from '@nestjs/common';\n\n@Controller('views')\nexport class ViewsController {\n  @Get()\n  getViews(@Session() session: { views?: number }) {\n    session.views = (session.views || 0) + 1;\n    return session.views;\n  }\n}\n```\n\n---\n\n**BE AWARE THAT THIS EXAMPLE IS NOT FOR PRODUCTION! IT USES IN-MEMORY STORE, SO YOUR DATA WILL BE LOST ON RESTART. USE OTHER [STORES](https://github.com/expressjs/session#compatible-session-stores)**\n\n---\n\nSee [redis-store](https://github.com/tj/connect-redis) example in `examples` folder.\n\nTo run examples:\n\n```sh\ngit clone https://github.com/iamolegga/nestjs-session.git\ncd nestjs-session\nnpm i\nnpm run build\ncd examples/in-memory # or `cd examples/redis-store`\nnpm i\nnpm start\n```\n\nFor Redis example, you should start Redis on localhost:6379.\nIf you have Docker installed you can start Redis image by `npm run redis` from `redis-store` directory.\n\n## Install\n\n```sh\nnpm i nestjs-session express-session @types/express-session\n```\n\n## API\n\n### SessionModule\n\n`SessionModule` class has two static methods, that returns `DynamicModule`, that you need to import:\n\n- `SessionModule.forRoot` for sync configuration without dependencies\n- `SessionModule.forRootAsync` for sync/async configuration with dependencies\n\n### SessionModule.forRoot\n\nAccept `NestSessionOptions`. Returns NestJS `DynamicModule` for import.\n\n### SessionModule.forRootAsync\n\nAccept `NestSessionAsyncOptions`. Returns NestJS `DynamicModule` for import.\n\n### NestSessionOptions\n\n`NestSessionOptions` is the interface of all options. It has next properties:\n\n- `session` - **required** - [express-session options](https://github.com/expressjs/session#options).\n- `forRoutes` - **optional** - same as NestJS buil-in `MiddlewareConfigProxy['forRoutes']` [See examples in official docs](https://docs.nestjs.com/middleware#applying-middleware). Specify routes, that should have access to session. If `forRoutes` and `exclude` will not be set, then sessions will be set to all routes.\n- `exclude` - **optional** - same as NestJS buil-in `MiddlewareConfigProxy['exclude']` [See examples in official docs](https://docs.nestjs.com/middleware#applying-middleware). Specify routes, that should not have access to session. If `forRoutes` and `exclude` will not be set, then sessions will be set to all routes.\n- `retries` - **optional** - `number` - by default if your session store lost connection to database it will return session as `undefined`, and no errors will be thrown, and then you need to check session in controller. But you can set this property how many times it should retry to get session, and on fail `InternalServerErrorException` will be thrown. If you don't want retries, but just want to `InternalServerErrorException` to be throw, then set to `0`. Set this option, if you dont't want manualy check session inside controllers.\n- `retriesStrategy` - **optional** - `(attempt: number) =\u003e number` - function that returns number of ms to wait between next attempt. Not calls on first attempt.\n\n### NestSessionAsyncOptions\n\n`NestSessionAsyncOptions` is the interface of options to create session module, that depends on other modules. It has next properties:\n\n- `imports` - **optional** - modules, that session module depends on. See [official docs](https://docs.nestjs.com/modules).\n- `inject` - **optional** - providers from `imports`-property modules, that will be passed as arguments to `useFactory` method.\n- `useFactory` - **required** - method, that returns `NestSessionOptions`.\n\n## Migration\n\n### v2\n\n`express-session` and `@types/express-session` are moved to peer dependencies, so you can update them independently.\n\n\u003ch2 align=\"center\"\u003eDo you use this library?\u003cbr/\u003eDon't be shy to give it a star! ★\u003c/h2\u003e\n\n\u003ch3 align=\"center\"\u003eAlso if you are into NestJS you might be interested in one of my \u003ca href=\"https://github.com/iamolegga#nestjs\"\u003eother NestJS libs\u003c/a\u003e.\u003c/h3\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamolegga%2Fnestjs-session","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiamolegga%2Fnestjs-session","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamolegga%2Fnestjs-session/lists"}