index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const { Sequelize } = require('sequelize');
  2. const { mariadb: config } = require('../../../config');
  3. const { dateFormat } = require('../../../utils');
  4. function formatTime(m) {
  5. delete m.deletedAt;
  6. m.createdAt && (m.createdAt = dateFormat(m.createdAt));
  7. m.updatedAt && (m.updatedAt = dateFormat(m.updatedAt));
  8. }
  9. const connect = new Sequelize({
  10. encrypt: true,
  11. ...config,
  12. dialect: 'mysql',
  13. pool: {
  14. max: 5,
  15. min: 0,
  16. acquire: 30000,
  17. idle: 10000,
  18. },
  19. define: {
  20. // prevent sequelize from pluralizing table names
  21. charset: 'utf8',
  22. collate: 'utf8_bin',
  23. paranoid: true,
  24. underscored: true,
  25. timestamps: true,
  26. freezeTableName: true,
  27. initialAutoIncrement: 10000,
  28. },
  29. timezone: '+08:00',
  30. logging: console.log,
  31. benchmark: true,
  32. logQueryParameters: true,
  33. query: {
  34. raw: true,
  35. },
  36. hooks: {
  37. afterFind(m) {
  38. m && (Array.isArray(m) ? m.forEach(item => formatTime(item)) : formatTime(m));
  39. },
  40. beforeFind(options) {
  41. if (options.attributes) {
  42. if (options.attributes.exclude) {
  43. options.attributes.exclude.push('deletedAt');
  44. } else {
  45. options.attributes = {
  46. exclude: ['deletedAt'],
  47. include: options.attributes,
  48. };
  49. }
  50. } else {
  51. options.attributes = {
  52. exclude: ['deletedAt'],
  53. };
  54. }
  55. },
  56. },
  57. });
  58. module.exports = connect;