index.d.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import * as EggApplication from 'egg';
  2. import * as mongoose from 'mongoose';
  3. declare module 'egg' {
  4. type MongooseModels = {
  5. [key: string]: mongoose.Model<any>
  6. };
  7. type MongooseSingleton = {
  8. clients: Map<string, mongoose.Connection>,
  9. get (id: string) : mongoose.Connection
  10. };
  11. type MongooseConfig = {
  12. url: string,
  13. options?: mongoose.ConnectionOptions
  14. };
  15. // extend app
  16. interface Application {
  17. mongooseDB: mongoose.Connection | MongooseSingleton;
  18. mongoose: typeof mongoose;
  19. model: MongooseModels;
  20. }
  21. // extend context
  22. interface Context {
  23. model: MongooseModels;
  24. /**
  25. * compose from ctx.query and ctx.request.body
  26. */
  27. requestparam: any;
  28. /**
  29. * 租户ID,用于多租户系统
  30. */
  31. tenant: string;
  32. /**
  33. * 当前用户ID
  34. */
  35. userid: string;
  36. /**
  37. * 当前用户角色
  38. */
  39. role: string;
  40. /**
  41. * 当前用户标签
  42. */
  43. tags: string;
  44. // 返回JSON结果
  45. json(errcode: number, errmsg: string, data: object);
  46. success(message: string, data: object);
  47. fail(errcode: number, errmsg: string, details: any);
  48. /**
  49. * same to success(message: string, data: object);
  50. */
  51. ok(message: string, data: object);
  52. }
  53. // extend your config
  54. interface EggAppConfig {
  55. mongoose: {
  56. url?: string,
  57. options?: mongoose.ConnectionOptions,
  58. client?: MongooseConfig,
  59. clients?: {
  60. [key: string]: MongooseConfig
  61. }
  62. };
  63. }
  64. }