I’m trying to get data from two different databases, I can not modify the data, I can only query them to fetch data.
I’d like match them by the primaryKey (uuid in this case) and join them together. I think I’m close, I’m just not there yet.
Here’s how I’m defining my two databases and models:
// database instances
exports.townyDB = new Sequelize(env.DB_TOWNY, env.DB_USER, env.DB_PASS, {
host: env.DB_HOST,
port: env.DB_PORT,
dialect: 'mariadb',
schemas: true
});
exports.townyDB = new Sequelize(env.DB_TOWNY, env.DB_USER, env.DB_PASS, {
host: env.DB_HOST,
port: env.DB_PORT,
dialect: 'mariadb',
schemas: true
});
// models
const townyResident = townyDB.define("towny_resident", {
name: {
type: DataTypes.STRING
},
player_uuid: {
type: DataTypes.STRING,
primaryKey: true,
field: 'uuid'
},
}, {
tableName: 'TOWNY_RESIDENTS',
schema: "s10_towny",
timestamps: false,
createdAt: false,
updatedAt: false
});
const CMIUser = cmiDB.define("cmi_user", {
player_uuid: {
type: DataTypes.STRING
},
username: {
type: DataTypes.STRING
},
}, {
tableName: 'CMI_users',
schema: "s3_cmi",
timestamps: false,
createdAt: false,
updatedAt: false
});
export default CMIUser;
// Calling both
CMIUser.hasOne(townyResident, { foreignKey: 'uuid' });
townyResident.belongsTo(CMIUser, { foreignKey: 'player_uuid' })
const test = await CMIUser.findOne({
raw: true, attributes: ['player_uuid', 'username', 'nickname'], order: cmiDB.literal('TotalPlayTime ASC'), where: { player_uuid: userUUID },
include: [{ model: townyResident, player_uuid: userUUID }]
});
Which seems like I’m close, the test returns:
{
player_uuid: '313f0b76-20de-473a-82fb-45684ea4431d',
username: 'mzya',
'towny_resident.name': null,
'towny_resident.player_uuid': null,
'towny_resident.uuid': null
}
I don’t think towny_resident. should be in the result, and it shouldn’t be null. What am I doing wrong?