1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- const { Sequelize } = require('sequelize');
- const { mariadb: config } = require('../../../config');
- const { dateFormat } = require('../../../utils');
- function formatTime(m) {
- delete m.deletedAt;
- m.createdAt && (m.createdAt = dateFormat(m.createdAt));
- m.updatedAt && (m.updatedAt = dateFormat(m.updatedAt));
- }
- const connect = new Sequelize({
- encrypt: true,
- ...config,
- dialect: 'mysql',
- pool: {
- max: 5,
- min: 0,
- acquire: 30000,
- idle: 10000,
- },
- define: {
- // prevent sequelize from pluralizing table names
- charset: 'utf8',
- collate: 'utf8_bin',
- paranoid: true,
- underscored: true,
- timestamps: true,
- freezeTableName: true,
- initialAutoIncrement: 10000,
- },
- timezone: '+08:00',
- logging: console.log,
- benchmark: true,
- logQueryParameters: true,
- query: {
- raw: true,
- },
- hooks: {
- afterFind(m) {
- m && (Array.isArray(m) ? m.forEach(item => formatTime(item)) : formatTime(m));
- },
- beforeFind(options) {
- if (options.attributes) {
- if (options.attributes.exclude) {
- options.attributes.exclude.push('deletedAt');
- } else {
- options.attributes = {
- exclude: ['deletedAt'],
- include: options.attributes,
- };
- }
- } else {
- options.attributes = {
- exclude: ['deletedAt'],
- };
- }
- },
- },
- });
- module.exports = connect;
|