{"id":13779809,"url":"https://github.com/koajs/redis-session-sets","last_synced_at":"2025-10-19T13:31:42.835Z","repository":{"id":3729310,"uuid":"50717884","full_name":"koajs/redis-session-sets","owner":"koajs","description":"Koa Redis sessions with field-referencing cross sets","archived":false,"fork":false,"pushed_at":"2023-03-06T18:58:03.000Z","size":1083,"stargazers_count":20,"open_issues_count":7,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-13T21:49:10.478Z","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/koajs.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}},"created_at":"2016-01-30T09:16:39.000Z","updated_at":"2024-01-12T18:07:09.000Z","dependencies_parsed_at":"2023-02-19T20:30:30.664Z","dependency_job_id":"f9df96fd-4a6f-4422-87e7-e31a9a9db17b","html_url":"https://github.com/koajs/redis-session-sets","commit_stats":{"total_commits":132,"total_committers":8,"mean_commits":16.5,"dds":"0.34090909090909094","last_synced_commit":"bca3707d7c0b8f66183f3da5668f48bf2f91c2f9"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fredis-session-sets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fredis-session-sets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fredis-session-sets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fredis-session-sets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/koajs","download_url":"https://codeload.github.com/koajs/redis-session-sets/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219866485,"owners_count":16554238,"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-08-03T18:01:09.508Z","updated_at":"2025-10-19T13:31:42.829Z","avatar_url":"https://github.com/koajs.png","language":"JavaScript","readme":"\n# redis-session-sets\n\n[![NPM version][npm-image]][npm-url]\n[![Node.js CI](https://github.com/koajs/redis-session-sets/workflows/Node.js%20CI/badge.svg?branch=master)](https://github.com/koajs/redis-session-sets/actions?query=workflow%3A%22Node.js+CI%22)\n[![Test coverage][codecov-image]][codecov-url]\n[![License][license-image]][license-url]\n[![Downloads][downloads-image]][downloads-url]\n\n\u003e NOTE: MAINTAINER WANTED\n\u003e \nA redis session for Koa that creates sets for specific values.\n\nUse-case: you want to know all the sessions related to a user so that if the user resets his/her password, you destroy all the sessions.\nNOTE: for expiring sessions, this is not optimal. However, you may still use this library as a redis hash-based solution without cross references.\n\nSpecifics:\n\n- Stores sessions as hash sets\n- Stores cross references as sets\n- Functional API\n\n## Example\n\n```js\nconst Koa = require('koa')\nconst client = require('ioredis').createClient()\n\nconst app = new Koa()\nconst Session = require('koa-redis-session-sets')(app, {\n  client,\n  references: {\n    user_id: {}\n  }\n})\n\napp.use(Session)\n\napp.use(async (ctx, next) =\u003e {\n  // get the session\n  let session = await ctx.session.get()\n\n  // update the session\n  await ctx.session.set({\n    user_id: 1\n  })\n\n  // get the session object with latest keys\n  session = await ctx.session.get()\n\n  ctx.status = 204\n})\n```\n\nHere's an example of deleting all the sessions associated with `user_id: 1`.\nYou have to do it yourself because handling it would be too opinionated.\nSpecifically, if this set is possibly large, you'd want to use `SSCAN`.\n\n```js\nconst key = Session.getReferenceKey('user_id', 1)\n\ntry {\n  const session_ids = await client.smembers(key)\n  await Promise.all(session_ids.map(session_id =\u003e {\n      // deletes the session and removes the session from all the referenced sets\n      return Session.store.delete(session_id)\n  }))\n} catch (err) {\n  console.error(err.stack)\n  process.exit(1)\n}\n```\n\n## Maintainers\n\n- Lead: @jonathanong [@jongleberry](https://twitter.com/jongleberry)\n\n## API\n\n### const SessionMiddleware = KoaRedisSessionSets(app, options)\n\nCreates a new session middleware instance.\n\nOptions:\n\n- `client` - `ioredis` client\n- `references` - fields to reference\n- `maxAge` - max age of sessions, defaulting to `28 days`\n- `prefix` - optional key prefix\n- `byteLength` - optional byte length for CSRF tokens\n\n### app.use(SessionMiddleware)\n\nUse the session middleware in your app.\nNote that this is a very simple function and middleware is not required.\nLook at the source code to understand how simple it is.\n\n### const Session = SessionMiddleware.createSession(context)\n\nCreate your own session object from a context.\n\n### const key = SessionMiddleware.getReferenceKey(field, value)\n\nGet the `key` for a redis `set` that contains all the session ids related to a `field:value` pair.\nUse `client.smembers(key)` to get all the session ids.\n\n### const key = Session.getKey()\n\nSession is `ctx.session`.\nGet the key for the redis `hash` for use with `client.hgetall(key)`.\n\n### Session.get([fields]).then(session =\u003e {})\n\nGet the session, optionally with select fields.\n\n### Session.set(values, [maxAge]).then(values =\u003e {})\n\nSet specific fields in the session.\nDoes not return the new session.\n\n### Session.unset(fields, [maxAge]).then(() =\u003e {})\n\nRemove specific fields in the session.\nDoes not return the new session.\n\n### Session.touch([maxAge]).then(() =\u003e {})\n\nUpdate the session, updating the cookies and the session expire time.\n\n### Session.delete().then(() =\u003e {})\n\nDeletes the session.\nDoes not create a new one.\nExecute `const session = await ctx.session.get()` to create a new one\n\n### Session.createCSRFToken([session]).then(token =\u003e {})\n\nCreate a CSRF token.\n\n### Session.verifyCSRFToken([session], token).then(valid =\u003e {})\n\nReturns a boolean of whether a CSRF token is valid.\n\n### const Store = SessionMiddleware.store\n\nThe `Store` is the underlying redis logic of the session.\n\n### const key = Store.getSessionKey(session_id)\n\n### const key = Store.getReferenceKey(field, value)\n\n### Store.get(session_id, [fields]).then(session =\u003e {})\n\n### Store.set(session_id, values, [maxAge]).then(values =\u003e {})\n\n### Store.unset(session_id, fields, [maxAge]).then(() =\u003e {})\n\n### Store.touch(session_id, [maxAge]).then(() =\u003e {})\n\n### Store.delete(session_id).then(() =\u003e {})\n\n[npm-image]: https://img.shields.io/npm/v/koa-redis-session-sets.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/koa-redis-session-sets\n[codecov-image]: https://img.shields.io/codecov/c/github/koajs/redis-session-sets/master.svg?style=flat-square\n[codecov-url]: https://codecov.io/github/koajs/redis-session-sets\n[license-image]: http://img.shields.io/npm/l/koa-redis-session-sets.svg?style=flat-square\n[license-url]: LICENSE\n[downloads-image]: http://img.shields.io/npm/dm/koa-redis-session-sets.svg?style=flat-square\n[downloads-url]: https://npmjs.org/package/koa-redis-session-sets\n","funding_links":[],"categories":["Middleware","仓库"],"sub_categories":["中间件"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fredis-session-sets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkoajs%2Fredis-session-sets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fredis-session-sets/lists"}