I am trying to map values from one database to another database in Postgres using Sequelize. I encounter this error when running index.js
file. I do not have another JS file other than this. Here is the code:
// Import Sequelize
const { Sequelize, DataTypes } = require('sequelize');
async function migrateData() {
// Define the connection to Database A
const dbA = new Sequelize('postgres', 'newuser', 'pass', {
host: 'localhost',
dialect: 'postgres'
});
// Define the connection to Database B
const dbB = new Sequelize('postgres2', 'newuser', 'pass', {
host: 'localhost',
dialect: 'postgres'
});
// Define the models for Database A and B
const ModelA = dbA.define('modelA', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
age: {
type: DataTypes.INTEGER,
allowNull: false
}
});
const ModelB = dbB.define('modelB', {
student_id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
student_name: {
type: DataTypes.STRING,
allowNull: false
},
student_age: {
type: DataTypes.INTEGER,
allowNull: false
}
});
// Sync the models to their respective databases
dbA.sync();
dbB.sync();
// Map data from Database A to Database B
const dataFromA = await ModelA.findAll();
const mappedData = dataFromA.map(item => ({
student_name: item.name,
student_age: item.age
}))
ModelA.hasOne(ModelB);
ModelB.belongsTo(ModelA);
// Bulk insert the mapped data into Database B
await ModelB.bulkCreate(mappedData);
// Close the database connections
dbA.close();
dbB.close();
}
migrateData();
Before this, it gave me a async-await error which I resolved. But now this error. I can be completely wrong with my code. Error:
Error: ConnectionManager.getConnection was called after the connection manager was closed!
at ConnectionManager.getConnection. (file path blah blah)