Session.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import {
  2. DataTypes,
  3. Model,
  4. InferAttributes,
  5. InferCreationAttributes,
  6. CreationOptional,
  7. ForeignKey,
  8. NonAttribute,
  9. BelongsToCreateAssociationMixin,
  10. BelongsToGetAssociationMixin,
  11. BelongsToSetAssociationMixin,
  12. Association
  13. } from "sequelize";
  14. import { ObjectIdType } from "@/modules/DataModule";
  15. import User from "./User";
  16. export class Session extends Model<
  17. // eslint-disable-next-line no-use-before-define
  18. InferAttributes<Session>,
  19. // eslint-disable-next-line no-use-before-define
  20. InferCreationAttributes<Session>
  21. > {
  22. declare _id: ObjectIdType;
  23. declare userId: ForeignKey<User["_id"]>;
  24. declare createdAt: CreationOptional<Date>;
  25. declare updatedAt: CreationOptional<Date>;
  26. declare getUserModel: BelongsToGetAssociationMixin<User>;
  27. declare setUserModel: BelongsToSetAssociationMixin<User, number>;
  28. declare createUserModel: BelongsToCreateAssociationMixin<User>;
  29. declare userModel?: NonAttribute<User>;
  30. declare static associations: {
  31. // eslint-disable-next-line no-use-before-define
  32. userModel: Association<Session, User>;
  33. };
  34. }
  35. export const schema = {
  36. _id: {
  37. type: DataTypes.OBJECTID,
  38. allowNull: false,
  39. primaryKey: true
  40. },
  41. userId: {
  42. type: DataTypes.OBJECTID,
  43. allowNull: false
  44. },
  45. createdAt: DataTypes.DATE,
  46. updatedAt: DataTypes.DATE,
  47. _name: {
  48. type: DataTypes.VIRTUAL,
  49. get() {
  50. return `session`;
  51. }
  52. }
  53. };
  54. export const options = {};
  55. export const setup = async () => {
  56. Session.belongsTo(User, {
  57. as: "userModel",
  58. foreignKey: {
  59. name: "userId",
  60. type: DataTypes.OBJECTID,
  61. allowNull: false
  62. },
  63. onDelete: "RESTRICT",
  64. onUpdate: "RESTRICT"
  65. });
  66. // Session.afterSave(async record => {
  67. // });
  68. // Session.afterDestroy(async record => {
  69. // });
  70. };
  71. export default Session;