application.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. const assert = require('assert');
  3. module.exports = {
  4. /**
  5. * set session external store
  6. *
  7. * ```js
  8. * app.sessionStore = {
  9. * * get() {},
  10. * * set() {},
  11. * * destory() {},
  12. * };
  13. *
  14. * app.sessionStore = class SessionStore {
  15. * constructor(app) {
  16. * }
  17. * * get() {},
  18. * * set() {},
  19. * * destroy() {},
  20. * }
  21. * ```
  22. * @param {Class|Object} store session store class or instance
  23. */
  24. set sessionStore(store) {
  25. if (this.config.session.store && this.config.env !== 'unittest') {
  26. this.logger.warn('[egg-session] sessionStore already exists and will be overwrite');
  27. }
  28. // supoprt this.sesionStore = null to disable external store
  29. if (!store) {
  30. this.config.session.store = undefined;
  31. return;
  32. }
  33. if (typeof store === 'function') store = new store(this);
  34. assert(typeof store.get === 'function', 'store.get must be function');
  35. assert(typeof store.set === 'function', 'store.set must be function');
  36. assert(typeof store.destroy === 'function', 'store.destroy must be function');
  37. store.get = this.toAsyncFunction(store.get);
  38. store.set = this.toAsyncFunction(store.set);
  39. store.destroy = this.toAsyncFunction(store.destroy);
  40. this.config.session.store = store;
  41. },
  42. /**
  43. * get sessionStore
  44. *
  45. * @return {Object} session store instance
  46. */
  47. get sessionStore() {
  48. return this.config.session.store;
  49. },
  50. };