sequelize-使用es6和模块运行迁移

8zzbczxx  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(445)

我不确定我是否做错了什么。我觉得我在运行一个现代的、相当普通的堆栈。但是我无法让新的sequelize v6与我的设置很好地配合。我在节点v14.17,sequelize v6.6.2上,在我的package.json中有 "type": "module" . 我终于想出了如何通过大量的谷歌搜索和修补来自动导入我的模型。因此,现在我尝试使用迁移工具向模型添加一个字段。我在migrations文件夹中创建了迁移文件。
看起来是这样的:

'use strict';

module.exports = {
    up: async (queryInterface, Sequelize) => {
        /**
         * Add altering commands here.
         *
         * Example:
         * await queryInterface.createTable('users', { id: Sequelize.INTEGER });
         */
        return Promise.all([
            queryInterface.addColumn(
                'Customers', // table name
                'include_core_items', // new field name
                {
                    type: Sequelize.Boolean,
                    allowNull: false,
                    defaultValue: true,
                    after: 'customer_name',
                }
            ),
        ]);
    },

    down: async (queryInterface, Sequelize) => {
        /**
         * Add reverting commands here.
         *
         * Example:
         * await queryInterface.dropTable('users');
         */
        return Promise.all([
            queryInterface.removeColumn('Customers', 'include_core_items'),
        ]);
    },
};

然后我尝试使用以下工具运行迁移: npx sequelize-cli db:migrate 我得到以下错误: ERROR: Error reading "config\config.js". Error: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\...\config\config.js require() of ES modules is not supported. require() of C:\...\config\config.js from C:\Users\...\AppData\Roaming\npm-cache\_npx\12576\node_modules\sequelize-cli\lib\helpers\config-helper.js is an ES module file as it is a .js file whose nearest pare nt package.json contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename config.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\...\package.json. 我已尝试将配置重命名为.cjs,然后出现错误: ERROR: Dialect needs to be explicitly supplied as of v4.0.0 因此,我认为它不能正确地读取env变量或其他内容。
配置[c]js

// seed command: sequelize seed:create --name PermissionData
// import dotenv from 'dotenv';
// dotenv.config();

import 'dotenv/config.js';

const username = process.env.NAME;
const password = process.env.PASSWORD;
const database = process.env.DATABASE;
const host = process.env.HOST;
const port = process.env.DB_PORT;
const dialect = process.env.DIALECT;
const node_env = process.env.NODE_ENV;
const session_secret = process.env.SESSION_SECRET;
const base_url = process.env.BASE_URL;
const client_url = process.env.CLIENT_APP_LOC;
const secure_cookie = process.env.SECURE_COOKIE;

const config = {
    dev: {
        username,
        password,
        database,
        host,
        port,
        dialect,
        logging: true,
        session_secret,
        base_url,
        client_url,
        secure_cookie,
    },
    testing: {
        username,
        password,
        database,
        host,
        port,
        dialect,
        logging: true,
        session_secret,
        base_url,
        client_url,
        secure_cookie,
    },
    production: {
        username,
        password,
        database,
        host,
        port,
        dialect,
        logging: true,
        session_secret,
        base_url,
        client_url,
        secure_cookie,
    },
};

export default config[node_env];

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题