index.d.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. tenantModel(tenant: string): MongooseModels;
  21. }
  22. // extend context
  23. interface Context {
  24. model: MongooseModels;
  25. /**
  26. * compose from ctx.query and ctx.request.body
  27. */
  28. requestparam: any;
  29. /**
  30. * 租户ID,用于多租户系统
  31. */
  32. tenant: string;
  33. // 返回JSON结果
  34. json(errcode: number, errmsg: string, data: object);
  35. success(message: string, data: object);
  36. fail(errcode: number, errmsg: string, details: any);
  37. /**
  38. * same to success(message: string, data: object);
  39. */
  40. ok(message: string, data: object);
  41. }
  42. // extend your config
  43. interface EggAppConfig {
  44. mongoose: {
  45. url?: string,
  46. options?: mongoose.ConnectionOptions,
  47. client?: MongooseConfig,
  48. clients?: {
  49. [key: string]: MongooseConfig
  50. }
  51. };
  52. }
  53. }
  54. /**
  55. * wrapper crud metods for Controller class
  56. */
  57. export function CrudController(cls: EggApplication.Controller, meta: any): EggApplication.Controller;