Here, I am trying to create a database in RDS in AWS (Amazon Web Services). Below are the steps that should be followed:
- Create a free account in AWS
- Go to RDS and create a DB as shown in below video
- Utilize the DB URL in API to create database connection.
NOTE: I have described all the steps in my Youtube video. You can follow it on Youtube for better understanding. The URL of this video: https://youtu.be/fj3gCZ9bTOo
Below is the code where I am creating a connection with AWS RDS DB in the file model/index.js. This index.js file is being used in schema file model/user.js. This schema file is being used in service/user.js.
const { Sequelize, DataTypes } = require('sequelize');
// Initialize Sequelize connection
const sequelize = new Sequelize('testdb', 'username', 'password', {
host: 'testdb.sdjkweurowdsj.ap-southeast-2.rds.amazonaws.com', //It is Dummy hostname
dialect: 'mysql', // Specify the dialect (in this case, MySQL)
operationsAliases: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
});
const db = {};
db.sequelize = sequelize;
db.models = {};
db.models.User = require('./user')(sequelize, Sequelize.DataTypes);
module.exports = db;
// Close Sequelize connection when the Node.js process exits
process.on('exit', () => {
sequelize.close()
.then(() => {
console.log('Sequelize connection closed');
})
.catch((error) => {
console.error('Error closing Sequelize connection:', error);
});
});
// Handle Ctrl+C or SIGINT signal to gracefully close the connection
process.on('SIGINT', () => {
sequelize.close()
.then(() => {
console.log('Sequelize connection closed');
process.exit(0);
})
.catch((error) => {
console.error('Error closing Sequelize connection:', error);
process.exit(1);
});
});
// Handle uncaught exceptions and promise rejections
process.on('uncaughtException', (error) => {
console.log(error);
sequelize.close()
.then(() => {
console.log('Sequelize connection closed');
process.exit(1);
})
.catch((closeError) => {
console.error('Error closing Sequelize connection:', closeError);
process.exit(1);
});
});
process.on('unhandledRejection', (reason, promise) => {
sequelize.close()
.then(() => {
console.log('Sequelize connection closed');
process.exit(1);
})
.catch((closeError) => {
console.error('Error closing Sequelize connection:', closeError);
process.exit(1);
});
});
Sequelize Schema file : model/user.js
module.exports = (sequelize, DataTypes) => {
const UserModel = sequelize.define('User', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
email: {
type: DataTypes.STRING,
allowNull: false
}
}, {
tableName: 'users',
timestamps: false // If your table doesn't have timestamp fields (createdAt, updatedAt)
});
return UserModel;
}
Service file: service/user.js
const {models: {User}} = require('../model');
const getUsers = async function () {
try {
const users = await User.findAll();
return users;
} catch (error) {
throw error;
}
}
const addUsers = async function (userObj) {
try {
const insertResult = await User.create(userObj);
return insertResult;
} catch (error) {
throw error;
}
}
const deleteUser = async function (id) {
try {
const deleteResult = await User.destroy({"where": {"id": id}});
return deleteResult;
} catch (error) {
throw error;
}
}
module.exports = {
getUsers, addUsers, deleteUser
}
No comments:
Post a Comment
Please provide your precious comments and suggestion