forked from atapas/nodeX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupConnection.js
32 lines (24 loc) · 1016 Bytes
/
setupConnection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
* @description : The Sequelize function in sequelize module is used to create an instance to connect with database.
* authenticate function authenticates the database with provided value, if not returns an error.
* sync function when called, authenticates and create connection first by calling the createConnection(),
* then if successfull, runs the callback function
*/
const { Sequelize } = require("sequelize");
function createConnection() {
let sequelize = new Sequelize('<Database name>', '<user>', '<password>', {
host: 'localhost',
dialect: 'mysql',
});
sequelize.authenticate().then(() => {
console.log('connected successfully');
}).catch((error) => {
console.log("error occurred");
});
return { sequelize }
}
let { sequelize } = createConnection();
function executeWithSync(promiseCallback) {
sequelize.sync().then(() => promiseCallback);
}
module.exports = { sequelize, executeWithSync };