/* * @Description: 框架服务postgresql模块 * @version: 1.0.0 * @Company: saganlab * @Author: vcbear * @Date: 2022-08-31 12:37:51 * @LastEditors: vcbear * @LastEditTime: 2022-09-02 09:47:42 */ import glob from 'glob'; import path from 'path'; import compose from 'koa-compose'; import { Sequelize} from 'sequelize'; import { YiProcess } from '../types'; const dprocess = process as YiProcess; export default async (app) => { const pgConfig = app.config?.postgresql || {}; if (pgConfig?.host && pgConfig?.port && pgConfig?.user && pgConfig?.password && pgConfig?.database) { try { const connection = new Sequelize ( pgConfig.database, pgConfig.user, pgConfig.password, { host: pgConfig.host, port: pgConfig.port, dialect: "postgres", timezone: "+08:00", define:{ timestamps: false }, pool:{ max: pgConfig.poolSize? pgConfig.poolSize : 50, min: 0, idle: pgConfig.poolIdleTimeout? pgConfig.poolIdleTimeout : 30000 }, logging:false } ); const c = { cyan: '\x1b[36m', red: '\x1b[31m', end: '\x1b[39m' } app.postgresqlConMsg = `postgresql connect success. host: ${c.cyan}${pgConfig.host} port: ${c.cyan}${pgConfig.port}${c.end}` const filesList = glob.sync(`**/*${app.extName}`, {cwd: path.resolve(app.appPath, './API/models')}) for (let file of filesList) { console.log(file); const model = await import(path.resolve(app.appPath, './API/models',file)); //console.log(model.default.registModel) await model.registModel(connection); } app.use((ctx, next) => { ctx.postgresql = connection; return next() }) } catch (error) { console.log(error); dprocess.emit('error', error); } } }