Skip to content

Commit

Permalink
chore: update models
Browse files Browse the repository at this point in the history
  • Loading branch information
sobird committed Nov 20, 2024
1 parent e6e2a1d commit 1febf5c
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 190 deletions.
27 changes: 4 additions & 23 deletions lib/sequelize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -130,15 +130,13 @@ export const sequelize = new Sequelize({
// console.log('attributes', attributes);
// });

type Initattributes<MS extends ModelStatic<Model>, M extends InstanceType<MS>> = Omit<Parameters<typeof Model.init<MS, M>>[0], 'updatedAt' | 'createdAt'>;

/**
* 模型基类
*
* sobird<i@sobird.me> at 2023/12/05 21:08:43 created.
*/
export class BaseModel<T extends {} = any, P extends {} = T> extends Model<T, P> {
// declare id: CreationOptional<string>;
export abstract class BaseModel<T extends {} = any, P extends {} = T> extends Model<Omit<T, ''>, Omit<P, ''>> {
declare id: CreationOptional<number>;

declare createdAt: CreationOptional<Date>;

Expand All @@ -149,7 +147,7 @@ export class BaseModel<T extends {} = any, P extends {} = T> extends Model<T, P>
* 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;

Expand Down Expand Up @@ -196,21 +194,4 @@ export class BaseModel<T extends {} = any, P extends {} = T> extends Model<T, P>
pn, ps, count: 0, rows: [],
};
}

public static define<MS extends ModelStatic<BaseModel>, M extends InstanceType<MS>>(
this: MS,
attributes: Initattributes<MS, M>,
options: ModelOptions<M>,
): MS {
return super.init({
...attributes,

createdAt: DataTypes.DATE,
updatedAt: DataTypes.DATE,
}, {
sequelize,
deletedAt: true,
...options,
}) as unknown as MS;
}
}
25 changes: 13 additions & 12 deletions models/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ 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<Account>;
/** Some attributes are optional in `Account.build` and `Account.create` calls */
export type AccountCreationAttributes = InferCreationAttributes<Account>;

class Account extends BaseModel<AccountAttributes, AccountCreationAttributes> {
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.
Expand All @@ -40,33 +40,33 @@ class Account extends BaseModel<AccountAttributes, AccountCreationAttributes> {
* 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<string>;
declare session_state: CreationOptional<string>;

static associate({ User }) {
this.belongsTo(User, { onDelete: 'cascade' });
}
}

Account.define(
Account.init(
{
type: {
type: DataTypes.STRING,
Expand Down Expand Up @@ -117,6 +117,7 @@ Account.define(
},
},
{
sequelize,
modelName: 'Account',
},
);
Expand Down
15 changes: 8 additions & 7 deletions models/dictionaryItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DictionaryItem>;
Expand All @@ -18,18 +18,18 @@ export type DictionaryItemAttributes = InferAttributes<DictionaryItem>;
export type DictionaryItemCreationAttributes = InferCreationAttributes<DictionaryItem>;

class DictionaryItem extends BaseModel<DictionaryItemAttributes, DictionaryItemCreationAttributes> {
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),
Expand All @@ -56,6 +56,7 @@ DictionaryItem.define(
},
},
{
sequelize,
modelName: 'DictionaryItem',
},
);
Expand Down
11 changes: 6 additions & 5 deletions models/dictionaryType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -17,11 +18,11 @@ export type DictionaryTypeAttributes = InferAttributes<DictionaryType>;
export type DictionaryTypeCreationAttributes = InferCreationAttributes<DictionaryType>;

class DictionaryType extends BaseModel<DictionaryTypeAttributes, DictionaryTypeCreationAttributes> {
name: string;
declare name: string;

code: string;
declare code: string;

status: string;
declare status: string;
}

DictionaryType.init(
Expand All @@ -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),
Expand Down
24 changes: 12 additions & 12 deletions models/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Group>;
/** Some attributes are optional in `Group.build` and `Group.create` calls */
export type GroupCreationAttributes = Optional<GroupAttributes, 'id'>;
export type GroupCreationAttributes = InferCreationAttributes<Group>;

class Group extends BaseModel<GroupAttributes, GroupCreationAttributes> {
declare name: string;

declare description: string;

class Group extends Model<GroupAttributes, GroupCreationAttributes> {
declare id: number;
declare parentId: number;
}

Group.init(
Expand Down
2 changes: 1 addition & 1 deletion models/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* models
* Models Index
*
* import { models } from '@auth/sequelize-adapter';
*
Expand Down
28 changes: 19 additions & 9 deletions models/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@
* sobird<i@sobird.me> 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<Log>;
/** Some attributes are optional in `Log.build` and `Log.create` calls */
export type LogCreationAttributes = InferCreationAttributes<Log>;

class Log extends BaseModel<LogAttributes, LogCreationAttributes> {
// 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<LogAttributes, 'agent'>;

class Log extends Model<LogAttributes, LogCreationAttributes> {
declare id: number;
// test: number;
}

Log.init(
Expand Down
40 changes: 28 additions & 12 deletions models/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,45 @@
* sobird<i@sobird.me> 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<Menu>;
/** Some attributes are optional in `Menu.build` and `Menu.create` calls */
export type MenuCreationAttributes = Optional<MenuAttributes, 'description'>;
export type MenuCreationAttributes = InferCreationAttributes<Menu>;

class Menu extends Model<MenuAttributes, MenuCreationAttributes> {
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,
Expand Down
20 changes: 12 additions & 8 deletions models/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@
* sobird<i@sobird.me> 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<Organization>;
/** Some attributes are optional in `Organization.build` and `Organization.create` calls */
export type OrganizationCreationAttributes = Optional<OrganizationAttributes, 'description'>;
export type OrganizationCreationAttributes = InferCreationAttributes<Organization>;

class Organization extends Model<OrganizationAttributes, OrganizationCreationAttributes> {
declare id: number;
name: string;

description: string;

public userId: number;
// public userId: number;
}

Organization.init(
Expand Down
Loading

0 comments on commit 1febf5c

Please sign in to comment.