Skip to content

Commit

Permalink
feat(@142vip/egg): 完善SequelizeORM基础类
Browse files Browse the repository at this point in the history
  • Loading branch information
mmdapl committed Jan 14, 2025
1 parent 2792fef commit 56b3795
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion packages/egg/src/orm/sequelize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DataTypes } from 'sequelize'
import type { Options } from 'sequelize'
import { DataTypes, Sequelize } from 'sequelize'

export const BaseSequelizeOptions = {
// 全局默认配置
Expand Down Expand Up @@ -58,3 +59,47 @@ export const VipSequelize = {
BaseEntity: BaseSequelizeEntity,
createEntity: createSequelizeEntity,
}

export interface SequelizeOptions extends Options {
connectUri?: string
Sequelize: typeof Sequelize
}

/**
* Sequelize基础类
*/
export class SequelizeORM {
private readonly options: SequelizeOptions
private sequelize: Sequelize
constructor(options: SequelizeOptions) {
this.options = options
this.sequelize = this.getConnect()
}

/**
* 连接
*/
getConnect() {
if (this.sequelize == null) {
const _Sequelize = this.options.Sequelize || Sequelize
this.sequelize = this.options.connectUri != null
? new _Sequelize(this.options.connectUri, this.options)
: new _Sequelize(this.options)
}
return this.sequelize
}

/**
* 连接重试
*/
public async retry() {
await this.sequelize.authenticate()
}

/**
* 关闭连接
*/
public async disconnect() {
await this.sequelize.close()
}
}

0 comments on commit 56b3795

Please sign in to comment.