src/app/model 里面定义数据库模型
sequelize.define 定义模型

  1. sequelize.define('model_name',{filed:value})

创建表

  1. 首先定义模型: model
  2. 然后同步:model.sync()
  3. 创建表会自动创建主键,默认为 id,自增长

Object.keys(app)

  1. [
  2. '_events',
  3. '_eventsCount',
  4. '_maxListeners',
  5. 'subdomainOffset',
  6. 'proxyIpHeader',
  7. 'maxIpsCount',
  8. 'middleware',
  9. 'context',
  10. 'request',
  11. 'response',
  12. 'timing',
  13. 'options',
  14. '_options',
  15. 'console',
  16. 'BaseContextClass',
  17. 'Controller',
  18. 'Service',
  19. 'lifecycle',
  20. 'loader',
  21. 'ContextCookies',
  22. 'ContextLogger',
  23. 'ContextHttpClient',
  24. 'HttpClient',
  25. 'messenger',
  26. '_unhandledRejectionHandler',
  27. 'cluster',
  28. 'Subscription',
  29. 'BaseHookClass',
  30. 'Boot',
  31. 'safeCurl',
  32. 'ScheduleStrategy',
  33. 'TimerScheduleStrategy',
  34. 'schedule',
  35. 'LogRotator',
  36. 'watcher',
  37. 'Sequelize'
  38. ]

Model

image.png

  1. import { Application } from 'egg';
  2. import * as uuidv4 from 'uuid/v4';
  3. /** 用户资料表,修改邮箱和手机号时,需要同步到userAuth表 */
  4. export default function(app: Application) {
  5. const { STRING, BOOLEAN, DATE, UUID, ARRAY } = app.Sequelize;
  6. const tableName = 'user';
  7. const User = app.model.define(
  8. tableName,
  9. {
  10. id: {
  11. type: UUID,
  12. unique: true,
  13. primaryKey: true,
  14. allowNull: false,
  15. defaultValue: () => {
  16. return uuidv4().replace(/-/g, '');
  17. },
  18. },
  19. username: {
  20. type: STRING(32),
  21. unique: true,
  22. allowNull: false,
  23. comment: '用户名',
  24. },
  25. password: {
  26. type: STRING(255),
  27. allowNull: false,
  28. comment: '密码',
  29. },
  30. email: {
  31. type: STRING(64),
  32. unique: true,
  33. allowNull: true,
  34. comment: '邮箱地址',
  35. },
  36. phone: {
  37. type: STRING(20),
  38. unique: true,
  39. allowNull: true,
  40. comment: '手机号码',
  41. },
  42. avatar: {
  43. type: STRING(150),
  44. allowNull: true,
  45. comment: '头像',
  46. },
  47. alias: {
  48. type: STRING(30),
  49. comment: '别名',
  50. },
  51. realName: {
  52. type: STRING(30),
  53. allowNull: true,
  54. comment: '真实姓名',
  55. },
  56. signature: {
  57. type: STRING(255),
  58. allowNull: true,
  59. comment: '签名',
  60. },
  61. status: {
  62. type: BOOLEAN,
  63. allowNull: false,
  64. defaultValue: 1,
  65. comment: '用户状态: 1 正常; 0 禁用',
  66. },
  67. lastActivedAt: DATE,
  68. createdAt: DATE,
  69. updatedAt: DATE,
  70. },
  71. {
  72. tableName,
  73. comment: '用户表',
  74. timestamps: true,
  75. underscored: false,
  76. }
  77. );
  78. class UserModal extends User {
  79. id: string;
  80. username: string;
  81. email: string;
  82. phone: string;
  83. avatar: string;
  84. alias: string;
  85. realName: string;
  86. signature: string;
  87. status: boolean;
  88. roles: any[];
  89. lastActivedAt: number;
  90. static associate() {
  91. app.model.User.hasMany(app.model.UserAuth, { as: 'userAuths', sourceKey: 'id', foreignKey: 'userId' });
  92. app.model.User.belongsToMany(app.model.Role, { as: 'roles', through: app.model.UserRoleRelation, foreignKey: 'userId' });
  93. }
  94. }
  95. return UserModal;
  96. }

Model属性和方法

https://sequelize.org/master/class/lib/model.js~Model.html

  1. instance.field
  2. // is the same as
  3. instance.get('field')
  4. // is the same as
  5. instance.getDataValue('field')