gpt4 book ai didi

node.js - 从文件中序列化 6 个导入模型

转载 作者:行者123 更新时间:2023-12-03 12:16:07 25 4
gpt4 key购买 nike

我想知道如何使用 Sequelize 6 从文件中导入模型?
它适用于 "sequelize": "^5.22.0",
"sequelize-cli": "^5.5.1",但我在使用 Sequelize 6 时出错。
目前,我有这个:

database/setup/databaseConnection.js

// Imports
import { Sequelize } from "sequelize"

const connection = new Sequelize(
process.env.DATABASE_NAME,
process.env.DATABASE_USER,
process.env.DATABASE_PASSWORD,
{
host: process.env.DATABASE_URL,
port: process.env.DATABASE_PORT,
dialect: "mysql",
logging: false,
define: {
// prevent sequelize from pluralizing table names
freezeTableName: true,
},
}
)

// Test connection
console.info("SETUP - Connecting database...")

connection
.authenticate()
.then(() => {
console.info("INFO - Database connected.")
})
.catch((err) => {
console.error("ERROR - Unable to connect to the database:", err)
})

export { connection as default }

database/models/index.js

// Imports
import Sequelize from "sequelize"

// App Imports
import connection from "../setup/databaseConnection"

const models = {
Language: connection.import("./language"),
}

Object.keys(models).forEach((modelName) => {
if ("associate" in models[modelName]) {
models[modelName].associate(models)
}
})

models.sequelize = connection
models.Sequelize = Sequelize

export { models as default }

database/models/language.js

module.exports = (sequelize, DataTypes) => {
const Language = sequelize.define(
"language",
{
/* id : {
primaryKey: true,
type : DataTypes.INTEGER
}, */
name: {
type: DataTypes.STRING,
},
code: {
type: DataTypes.STRING,
},
is_active: {
type: DataTypes.BOOLEAN,
},
},
{}
)
Language.associate = function (models) {
// Language has Many Bucket
models.Language.hasMany(models.Bucket, {
foreignKey: "id",
})
}
return Language
}
但我有这个错误:
formation-api/database/models/index.js:17
Language: _databaseConnection["default"]["import"]("./language")
^

TypeError: _databaseConnection.default.import is not a function
at Object.<anonymous> (/Users/jeremiechazelle/Sites/api/database/models/index.js:8:15)
at Module._compile (internal/modules/cjs/loader.js:1147:30)
at Module._compile (/Users/jeremiechazelle/Sites/api/node_modules/pirates/lib/index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
at Object.newLoader [as .js] (/Users/jeremiechazelle/Sites/api/node_modules/pirates/lib/index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:996:32)
at Function.Module._load (internal/modules/cjs/loader.js:896:14)
at Module.require (internal/modules/cjs/loader.js:1036:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/Users/jeremiechazelle/Sites/api/resolvers/Queries/User.js:2:1)
[nodemon] app crashed - waiting for file changes before starting...
我用 :
"Sequelize ": "^6.1.0",
"sequelize-cli": "^6.0.0"

最佳答案

像这样:

import Sequelize from 'sequelize'

import userModel from './user'
import messageModel from './message'

const sequelize = new Sequelize(process.env.DATABASE, process.env.DATABASE_USER, process.env.DATABASE_PASSWORD, {
dialect: 'postgres'
})

const models = {
User: userModel(sequelize, Sequelize.DataTypes),
Message: messageModel(sequelize, Sequelize.DataTypes)
}

关于node.js - 从文件中序列化 6 个导入模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62556633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com