zoukankan      html  css  js  c++  java
  • eggjs sequelize操作多个数据,表名不加s,不默认加创建和修改时间字段

    操作多个数据库:

    // 多个数据库操作,model名称不同
      // model目前项目的库,model2是之前的招聘库,model3是cd公共库
      config.sequelize = {
        datasources: [
          {
            dialect: 'mysql',
            host: '127.0.0.1',
            port: 3306,
            database: 'talent_management',
            username: "root",
            password: "root",
            charset: "utf8",
    
            timezone: "+08:00",
            dialectOptions: {
              dateStrings: true,
              typeCast(field, next) {
                if (field.type === "DATETIME") {
                  // 返回正确得时间
                  return field.string();
                }
                return next();
              }
            }
          },
          {
            dialect: 'mysql',
            // 通过egg的app或者ctx访问这个模型的字段
            // 默认是model,也就是app.model.xx
            // 这里是通过app.Model2来使用db1数据库的模型
            delegate: 'model2',
            // 指定模型文件在哪里,默认是model
            // 如果是在子目录如model/xx,要保证model没被单独使用
            baseDir: 'model2',
            dialectOptions: {
              charset: 'utf8mb4',
            },
            define: {
              freezeTableName: true,
              createdAt: false,
              updatedAt: false
            },
            database: '***',
            host: '***',
            port: '3306',
            username: "root",
            password: "***",
            // host: '***',
            // username: "***",
            // password: "***",
          
            timezone: "+08:00",
            dialectOptions: {
              dateStrings: true,
              typeCast(field, next) {
                if (field.type === "DATETIME") {
                  // 返回正确得时间
                  return field.string();
                }
                return next();
              }
            }
          },
          {
            dialect: 'mysql',
            // 通过egg的app或者ctx访问这个模型的字段
            // 默认是model,也就是app.model.xx
            // 这里是通过app.Model2来使用db1数据库的模型
            delegate: 'model3',
            // 指定模型文件在哪里,默认是model
            // 如果是在子目录如model/xx,要保证model没被单独使用
            baseDir: 'model3',
            dialectOptions: {
              charset: 'utf8mb4',
            },
            define: {
              freezeTableName: true,
              createdAt: false,
              updatedAt: false
            },
            database: '***',
            host: '192.168.0.10',
            port: '3306',
            username: "root",
            password: "***",
            // host: '***',
            // username: "***",
            // password: "**",
          
            timezone: "+08:00",
            dialectOptions: {
              dateStrings: true,
              typeCast(field, next) {
                if (field.type === "DATETIME") {
                  // 返回正确得时间
                  return field.string();
                }
                return next();
              }
            }
          },
        ]
      }

    创建:

     

     操作库:

    await ctx.model2.Category.findAndCountAll()

    表名创建不加s,不默认加创建和修改时间字段:

    define: {
        freezeTableName: true,
        createdAt: false,
        updatedAt: false
    },

    参考博客:

    https://blog.csdn.net/kyyius/article/details/116064987

     
  • 相关阅读:
    查看表中bytea类型的字段内容
    Js中生成32位随机id
    VS Code操作指南
    记录一次非常简单的Win10安装
    Typora中下载并安装主题
    Win 10下隐藏任务栏图标B & O Play Audio Control
    IDEA基本使用
    Eclipse中安装反编译器(中文版)
    Eclipse设置控制台不自动弹出
    解决Eclipse控制台中文乱码的情况
  • 原文地址:https://www.cnblogs.com/zezhou/p/15079428.html
Copyright © 2011-2022 走看看