From 1febf5cbf1bb38c9a42b9a82b52066a92ab477a7 Mon Sep 17 00:00:00 2001 From: sobird Date: Wed, 20 Nov 2024 15:18:27 +0800 Subject: [PATCH] chore: update models --- lib/sequelize.ts | 27 +++-------------- models/account.ts | 25 ++++++++-------- models/dictionaryItem.ts | 15 +++++----- models/dictionaryType.ts | 11 +++---- models/group.ts | 24 +++++++-------- models/index.ts | 2 +- models/log.ts | 28 +++++++++++------ models/menu.ts | 40 +++++++++++++++++-------- models/organization.ts | 20 ++++++++----- models/permission.ts | 53 ++++++++++++++++++-------------- models/role-permission.ts | 28 ++++++++--------- models/role.ts | 28 ++++++++--------- models/session.ts | 11 ++++++- models/user-role.ts | 28 ++++++++--------- models/user.ts | 60 +++++++++++++++++-------------------- models/verificationToken.ts | 9 +++--- 16 files changed, 219 insertions(+), 190 deletions(-) diff --git a/lib/sequelize.ts b/lib/sequelize.ts index 987e93f..01ab18a 100644 --- a/lib/sequelize.ts +++ b/lib/sequelize.ts @@ -13,7 +13,7 @@ import { AbilityTuple, MongoAbility } from '@casl/ability'; import log4js from 'log4js'; import { - Sequelize, Model, CreationOptional, ModelStatic, InferAttributes, ModelOptions, DataTypes, + Sequelize, Model, CreationOptional, ModelStatic, InferAttributes, } from 'sequelize'; import sqlite3 from 'sqlite3'; @@ -130,15 +130,13 @@ export const sequelize = new Sequelize({ // console.log('attributes', attributes); // }); -type Initattributes, M extends InstanceType> = Omit>[0], 'updatedAt' | 'createdAt'>; - /** * 模型基类 * * sobird at 2023/12/05 21:08:43 created. */ -export class BaseModel extends Model { - // declare id: CreationOptional; +export abstract class BaseModel extends Model, Omit> { + declare id: CreationOptional; declare createdAt: CreationOptional; @@ -149,7 +147,7 @@ export class BaseModel extends Model * This method is not a part of Sequelize lifecycle. * The `models/index` file will call this method automatically. */ - declare static associate: (models: Models) => void; + public static associate: (models: Models) => void; static accessibleBy = accessibleBy; @@ -196,21 +194,4 @@ export class BaseModel extends Model pn, ps, count: 0, rows: [], }; } - - public static define, M extends InstanceType>( - this: MS, - attributes: Initattributes, - options: ModelOptions, - ): MS { - return super.init({ - ...attributes, - - createdAt: DataTypes.DATE, - updatedAt: DataTypes.DATE, - }, { - sequelize, - deletedAt: true, - ...options, - }) as unknown as MS; - } } diff --git a/models/account.ts b/models/account.ts index ce7b9a9..d273c76 100644 --- a/models/account.ts +++ b/models/account.ts @@ -16,7 +16,7 @@ import { type InferAttributes, InferCreationAttributes, CreationOptional, } from 'sequelize'; -import { BaseModel } from '@/lib/sequelize'; +import { sequelize, BaseModel } from '@/lib/sequelize'; /** These are all the attributes in the Account model */ export type AccountAttributes = InferAttributes; @@ -24,14 +24,14 @@ export type AccountAttributes = InferAttributes; export type AccountCreationAttributes = InferCreationAttributes; class Account extends BaseModel { - public userId: number; + declare userId: number; declare type: 'oauth' | 'oidc' | 'email'; /** * Provider's id for this account. Eg.: 'google' */ - provider: string; + declare provider: string; /** * This value depends on the type of the provider being used to create the account. @@ -40,33 +40,33 @@ class Account extends BaseModel { * email: The user's email address. * credentials: id returned from the authorize() callback */ - providerAccountId: string; + declare providerAccountId: string; - refresh_token?: string; + declare refresh_token?: string; - access_token?: string | undefined; + declare access_token?: string | undefined; /** * Calculated value based on [OAuth2TokenEndpointResponse.expires_in]([object Object]). * It is the absolute timestamp (in seconds) when the [OAuth2TokenEndpointResponse.access_token]([object Object]) expires. * This value can be used for implementing token rotation together with [OAuth2TokenEndpointResponse.refresh_token]([object Object]). */ - expires_at?: number; + declare expires_at?: number; - token_type?: string; + declare token_type?: string; - scope?: string; + declare scope?: string; - id_token?: string; + declare id_token?: string; - session_state: CreationOptional; + declare session_state: CreationOptional; static associate({ User }) { this.belongsTo(User, { onDelete: 'cascade' }); } } -Account.define( +Account.init( { type: { type: DataTypes.STRING, @@ -117,6 +117,7 @@ Account.define( }, }, { + sequelize, modelName: 'Account', }, ); diff --git a/models/dictionaryItem.ts b/models/dictionaryItem.ts index 4007c53..f0db3c1 100644 --- a/models/dictionaryItem.ts +++ b/models/dictionaryItem.ts @@ -9,7 +9,7 @@ import { type InferAttributes, InferCreationAttributes, } from 'sequelize'; -import { BaseModel } from '@/lib/sequelize'; +import { sequelize, BaseModel } from '@/lib/sequelize'; /** These are all the attributes in the DictionaryItem model */ export type DictionaryItemAttributes = InferAttributes; @@ -18,18 +18,18 @@ export type DictionaryItemAttributes = InferAttributes; export type DictionaryItemCreationAttributes = InferCreationAttributes; class DictionaryItem extends BaseModel { - label: string; + declare label: string; - value: string; + declare value: string; - sort: number; + declare sort: number; - status: string; + declare status: string; - typeId: number; + declare typeId: number; } -DictionaryItem.define( +DictionaryItem.init( { label: { type: DataTypes.STRING(120), @@ -56,6 +56,7 @@ DictionaryItem.define( }, }, { + sequelize, modelName: 'DictionaryItem', }, ); diff --git a/models/dictionaryType.ts b/models/dictionaryType.ts index 26efe10..718d955 100644 --- a/models/dictionaryType.ts +++ b/models/dictionaryType.ts @@ -8,6 +8,7 @@ import { DataTypes, type InferAttributes, InferCreationAttributes, } from 'sequelize'; + import { sequelize, BaseModel } from '@/lib/sequelize'; /** These are all the attributes in the DictionaryType model */ @@ -17,11 +18,11 @@ export type DictionaryTypeAttributes = InferAttributes; export type DictionaryTypeCreationAttributes = InferCreationAttributes; class DictionaryType extends BaseModel { - name: string; + declare name: string; - code: string; + declare code: string; - status: string; + declare status: string; } DictionaryType.init( @@ -30,12 +31,12 @@ DictionaryType.init( type: DataTypes.STRING(120), unique: true, allowNull: false, - comment: '字典名称', + comment: 'Dictionary Name', }, code: { type: DataTypes.STRING(120), allowNull: false, - comment: '字典类型', + comment: 'Dictionary Type', }, status: { type: DataTypes.CHAR(1), diff --git a/models/group.ts b/models/group.ts index 8700b32..f286f00 100644 --- a/models/group.ts +++ b/models/group.ts @@ -5,24 +5,24 @@ */ import { - Model, DataTypes, - type Optional, + type InferAttributes, + type InferCreationAttributes, } from 'sequelize'; -import { sequelize } from '@/lib/sequelize'; + +import { sequelize, BaseModel } from '@/lib/sequelize'; /** These are all the attributes in the Group model */ -export interface GroupAttributes { - id?: number; - parentId: number, - name: string; - description: string; -} +export type GroupAttributes = InferAttributes; /** Some attributes are optional in `Group.build` and `Group.create` calls */ -export type GroupCreationAttributes = Optional; +export type GroupCreationAttributes = InferCreationAttributes; + +class Group extends BaseModel { + declare name: string; + + declare description: string; -class Group extends Model { - declare id: number; + declare parentId: number; } Group.init( diff --git a/models/index.ts b/models/index.ts index 351a868..9fe2494 100644 --- a/models/index.ts +++ b/models/index.ts @@ -1,5 +1,5 @@ /** - * models + * Models Index * * import { models } from '@auth/sequelize-adapter'; * diff --git a/models/log.ts b/models/log.ts index c1b827d..d704d40 100644 --- a/models/log.ts +++ b/models/log.ts @@ -4,21 +4,31 @@ * sobird at 2023/12/03 22:01:07 created. */ -import { Model, DataTypes, type Optional } from 'sequelize'; -import { sequelize } from '@/lib/sequelize'; +import { + DataTypes, + type InferAttributes, + type InferCreationAttributes, +} from 'sequelize'; + +import { sequelize, BaseModel } from '@/lib/sequelize'; /** These are all the attributes in the Log model */ -export interface LogAttributes { +export type LogAttributes = InferAttributes; +/** Some attributes are optional in `Log.build` and `Log.create` calls */ +export type LogCreationAttributes = InferCreationAttributes; + +class Log extends BaseModel { + // declare id: never; + type: string; + content: string; - operator: number; + + operator: string; + agent: string; -} -/** Some attributes are optional in `Log.build` and `Log.create` calls */ -export type LogCreationAttributes = Optional; -class Log extends Model { - declare id: number; + // test: number; } Log.init( diff --git a/models/menu.ts b/models/menu.ts index ec5f0ab..8b75ecf 100644 --- a/models/menu.ts +++ b/models/menu.ts @@ -4,29 +4,45 @@ * sobird at 2023/12/05 10:56:40 created. */ -import { Model, DataTypes, type Optional } from 'sequelize'; +import { + Model, DataTypes, + type InferAttributes, + type InferCreationAttributes, +} from 'sequelize'; + import { sequelize } from '@/lib/sequelize'; /** These are all the attributes in the Menu model */ -export interface MenuAttributes { - name: string; - icon: string; - href: string; - description: string; - parentId: number; - sort: number; -} +export type MenuAttributes = InferAttributes; /** Some attributes are optional in `Menu.build` and `Menu.create` calls */ -export type MenuCreationAttributes = Optional; +export type MenuCreationAttributes = InferCreationAttributes; class Menu extends Model { - declare id: number; + declare userId: number; + + declare name: string; + + declare icon: string; + + declare href: string; - public userId: number; + declare description: string; + + declare parentId: number; + + declare sort: number; + + static associate({ User }) { + this.belongsTo(User, { onDelete: 'cascade' }); + } } Menu.init( { + userId: { + type: DataTypes.INTEGER, + comment: 'user id', + }, name: { type: DataTypes.STRING, allowNull: false, diff --git a/models/organization.ts b/models/organization.ts index 5722b28..10a43c9 100644 --- a/models/organization.ts +++ b/models/organization.ts @@ -4,21 +4,25 @@ * sobird at 2023/11/29 10:37:29 created. */ -import { Model, DataTypes, type Optional } from 'sequelize'; +import { + Model, DataTypes, + type InferAttributes, + type InferCreationAttributes, +} from 'sequelize'; + import { sequelize } from '@/lib/sequelize'; /** These are all the attributes in the Organization model */ -export interface OrganizationAttributes { - name: string; - description: string; -} +export type OrganizationAttributes = InferAttributes; /** Some attributes are optional in `Organization.build` and `Organization.create` calls */ -export type OrganizationCreationAttributes = Optional; +export type OrganizationCreationAttributes = InferCreationAttributes; class Organization extends Model { - declare id: number; + name: string; + + description: string; - public userId: number; + // public userId: number; } Organization.init( diff --git a/models/permission.ts b/models/permission.ts index 4ee4685..71eee1c 100644 --- a/models/permission.ts +++ b/models/permission.ts @@ -6,35 +6,40 @@ import { DataTypes, - type Optional, - BelongsToManyGetAssociationsMixin, - BelongsToManySetAssociationsMixin, - BelongsToManyAddAssociationMixin, - BelongsToManyAddAssociationsMixin, - BelongsToManyRemoveAssociationMixin, - BelongsToManyRemoveAssociationsMixin, - BelongsToManyHasAssociationMixin, - BelongsToManyHasAssociationsMixin, - BelongsToManyCreateAssociationMixin, - BelongsToManyCountAssociationsMixin, + type InferAttributes, + type InferCreationAttributes, + type Association, + type BelongsToManyGetAssociationsMixin, + type BelongsToManySetAssociationsMixin, + type BelongsToManyAddAssociationMixin, + type BelongsToManyAddAssociationsMixin, + type BelongsToManyRemoveAssociationMixin, + type BelongsToManyRemoveAssociationsMixin, + type BelongsToManyHasAssociationMixin, + type BelongsToManyHasAssociationsMixin, + type BelongsToManyCreateAssociationMixin, + type BelongsToManyCountAssociationsMixin, } from 'sequelize'; + import { sequelize, BaseModel } from '@/lib/sequelize'; -import type Role from './user'; + +import type Role from './role'; /** These are all the attributes in the Permission model */ -export interface PermissionAttributes { - id?: number; - name: string; - description: string; - operator: string; - target: string; - rules: object; -} +export type PermissionAttributes = InferAttributes; /** Some attributes are optional in `Permission.build` and `Permission.create` calls */ -export type PermissionCreationAttributes = Optional; +export type PermissionCreationAttributes = InferCreationAttributes; class Permission extends BaseModel { - declare id: number; + declare name: string; + + declare description: string; + + declare operator: string; + + declare target: string; + + declare rules: object; declare getRoles: BelongsToManyGetAssociationsMixin; @@ -60,6 +65,10 @@ class Permission extends BaseModel; + }; } Permission.init( diff --git a/models/role-permission.ts b/models/role-permission.ts index 9bfaddd..4d9e9b0 100644 --- a/models/role-permission.ts +++ b/models/role-permission.ts @@ -4,23 +4,23 @@ * sobird at 2023/11/30 19:51:05 created. */ -import { Model, DataTypes, type Optional } from 'sequelize'; +import { + Model, DataTypes, + type InferAttributes, + type InferCreationAttributes, +} from 'sequelize'; + import { sequelize } from '@/lib/sequelize'; -import Role from './role'; + import Permission from './permission'; +import Role from './role'; /** These are all the attributes in the RolePermission model */ -export interface RolePermissionAttributes { - id?: number; - PermissionId: number; - RoleId: number; -} +export type RolePermissionAttributes = InferAttributes; /** Some attributes are optional in `RolePermission.build` and `RolePermission.create` calls */ -export type RolePermissionCreationAttributes = Optional; +export type RolePermissionCreationAttributes = InferCreationAttributes; class RolePermission extends Model { - declare id: number; - declare roleId: number; declare permissionId: number; @@ -28,17 +28,17 @@ class RolePermission extends Model; export type RoleCreationAttributes = InferCreationAttributes; class Role extends BaseModel { - declare id: CreationOptional; - declare parentId?: CreationOptional; declare name: string; diff --git a/models/session.ts b/models/session.ts index 76d7445..6fca866 100644 --- a/models/session.ts +++ b/models/session.ts @@ -7,11 +7,16 @@ */ import { - DataTypes, type InferAttributes, InferCreationAttributes, + DataTypes, + type InferAttributes, + type InferCreationAttributes, + type Association, } from 'sequelize'; import { sequelize, BaseModel } from '@/lib/sequelize'; +import type User from './user'; + /** These are all the attributes in the Session model */ export type SessionAttributes = InferAttributes; /** Some attributes are optional in `Session.build` and `Session.create` calls */ @@ -44,6 +49,10 @@ class Session extends BaseModel { static associate({ User }) { this.belongsTo(User, { onDelete: 'cascade' }); } + + declare static associations: { + Roles: Association; + }; } Session.init( diff --git a/models/user-role.ts b/models/user-role.ts index 53894cc..a27b4d6 100644 --- a/models/user-role.ts +++ b/models/user-role.ts @@ -4,23 +4,23 @@ * sobird at 2023/11/30 19:51:05 created. */ -import { Model, DataTypes, type Optional } from 'sequelize'; +import { + Model, DataTypes, + type InferAttributes, + type InferCreationAttributes, +} from 'sequelize'; + import { sequelize } from '@/lib/sequelize'; -import User from './user'; + import Role from './role'; +import User from './user'; /** These are all the attributes in the UserRole model */ -export interface UserRoleAttributes { - id?: number; - UserId: number; - RoleId: number; -} +export type UserRoleAttributes = InferAttributes; /** Some attributes are optional in `UserRole.build` and `UserRole.create` calls */ -export type UserRoleCreationAttributes = Optional; +export type UserRoleCreationAttributes = InferCreationAttributes; class UserRole extends Model { - declare id: number; - public userId!: number; public roleId!: number; @@ -28,17 +28,17 @@ class UserRole extends Model { UserRole.init( { - UserId: { + userId: { type: DataTypes.INTEGER, references: { - model: User, // 'Movies' 也可以使用 + model: User, key: 'id', }, }, - RoleId: { + roleId: { type: DataTypes.INTEGER, references: { - model: Role, // 'Actors' 也可以使用 + model: Role, key: 'id', }, }, diff --git a/models/user.ts b/models/user.ts index 4afc0ae..130e220 100644 --- a/models/user.ts +++ b/models/user.ts @@ -15,24 +15,25 @@ import { randomBytes, createHmac } from 'crypto'; import { DataTypes, Op, - InferAttributes, - InferCreationAttributes, - CreationOptional, - Association, - BelongsToManyGetAssociationsMixin, - BelongsToManySetAssociationsMixin, - BelongsToManyAddAssociationMixin, - BelongsToManyAddAssociationsMixin, - BelongsToManyRemoveAssociationMixin, - BelongsToManyRemoveAssociationsMixin, - BelongsToManyHasAssociationMixin, - BelongsToManyHasAssociationsMixin, - BelongsToManyCreateAssociationMixin, - BelongsToManyCountAssociationsMixin, - NonAttribute, + type InferAttributes, + type InferCreationAttributes, + type CreationOptional, + type NonAttribute, + type Attributes, + type Association, + type BelongsToManyGetAssociationsMixin, + type BelongsToManySetAssociationsMixin, + type BelongsToManyAddAssociationMixin, + type BelongsToManyAddAssociationsMixin, + type BelongsToManyRemoveAssociationMixin, + type BelongsToManyRemoveAssociationsMixin, + type BelongsToManyHasAssociationMixin, + type BelongsToManyHasAssociationsMixin, + type BelongsToManyCreateAssociationMixin, + type BelongsToManyCountAssociationsMixin, } from 'sequelize'; -import { BaseModel } from '@/lib/sequelize'; +import { sequelize, BaseModel } from '@/lib/sequelize'; import type Role from './role'; @@ -41,6 +42,7 @@ export const UserExcludeAttributes = ['salt', 'password', 'emailVerified']; // These are all the attributes in the User model export type UserAttributes = InferAttributes; +export type UU = Attributes; // Some attributes are optional in `User.build` and `User.create` calls export type UserCreationAttributes = InferCreationAttributes; // 用户登录属性 @@ -49,8 +51,6 @@ export type UserSigninAttributes = Pick export type UserSignupAttributes = Pick; class User extends BaseModel { - declare id: CreationOptional; - declare username: CreationOptional; declare name: CreationOptional; @@ -90,19 +90,19 @@ class User extends BaseModel { declare getRoles: BelongsToManyGetAssociationsMixin; /** Remove all previous associations and set the new ones */ - declare setRoles: BelongsToManySetAssociationsMixin; + declare setRoles: BelongsToManySetAssociationsMixin; - declare addRole: BelongsToManyAddAssociationMixin; + declare addRole: BelongsToManyAddAssociationMixin; - declare addRoles: BelongsToManyAddAssociationsMixin; + declare addRoles: BelongsToManyAddAssociationsMixin; - declare removeRole: BelongsToManyRemoveAssociationMixin; + declare removeRole: BelongsToManyRemoveAssociationMixin; - declare removeRoles: BelongsToManyRemoveAssociationsMixin; + declare removeRoles: BelongsToManyRemoveAssociationsMixin; - declare hasRole: BelongsToManyHasAssociationMixin; + declare hasRole: BelongsToManyHasAssociationMixin; - declare hasRoles: BelongsToManyHasAssociationsMixin; + declare hasRoles: BelongsToManyHasAssociationsMixin; declare createRole: BelongsToManyCreateAssociationMixin; @@ -125,7 +125,7 @@ class User extends BaseModel { }; /** 用户注册 */ - public static async signUp(attributes: UserSignupAttributes, fields?: (keyof UserAttributes)[]) { + public static async signUp(attributes: UserSignupAttributes, fields?: (keyof Attributes)[]) { const [user, created] = await this.findOrCreate({ defaults: { ...attributes, @@ -185,13 +185,8 @@ class User extends BaseModel { } } -User.define( +User.init( { - id: { - type: DataTypes.INTEGER.UNSIGNED, - autoIncrement: true, - primaryKey: true, - }, username: { type: DataTypes.STRING(32), unique: true, @@ -264,6 +259,7 @@ User.define( // updatedAt: DataTypes.DATE, }, { + sequelize, modelName: 'User', }, ); diff --git a/models/verificationToken.ts b/models/verificationToken.ts index 0195690..433126e 100644 --- a/models/verificationToken.ts +++ b/models/verificationToken.ts @@ -12,7 +12,8 @@ import { DataTypes, - type InferAttributes, InferCreationAttributes, + type InferAttributes, + type InferCreationAttributes, } from 'sequelize'; import { sequelize, BaseModel } from '@/lib/sequelize'; @@ -27,17 +28,17 @@ class VerificationToken extends BaseModel