DekGenius.com
JAVASCRIPT
sequelize init connection set up nodejs node
const Sequelize = require('sequelize');
const path = 'mysql://user12:12user@localhost:3306/testdb';
const sequelize = new Sequelize(path, { operatorsAliases: false });
sequelize.authenticate().then(() => {
console.log('Connection established successfully.');
}).catch(err => {
console.error('Unable to connect to the database:', err);
}).finally(() => {
sequelize.close();
});
How to create sequelize connection in javascript
// Include Sequelize module
const Sequelize = require('sequelize')
// Creating new Object of Sequelize
const sequelize = new Sequelize(
'DATABASE_NAME',
'DATABASE_USER_NAME',
'DATABASE_PASSWORD', {
// Explicitly specifying
// mysql database
dialect: 'mysql',
// By default host is 'localhost'
host: 'localhost'
}
);
// Exporting the sequelize object.
// We can use it in another file
// for creating models
module.exports = sequelize
Sequelize using javascript
// Include Sequelize module.
const Sequelize = require('sequelize')
// Import sequelize object,
// Database connection pool managed by Sequelize.
const sequelize = require('../utils/database')
// Define method takes two arguments
// 1st - name of table
// 2nd - columns inside the table
const User = sequelize.define('user', {
// Column-1, user_id is an object with
// properties like type, keys,
// validation of column.
user_id:{
// Sequelize module has INTEGER Data_Type.
type:Sequelize.INTEGER,
// To increment user_id automatically.
autoIncrement:true,
// user_id can not be null.
allowNull:false,
// For uniquely identify user.
primaryKey:true
},
// Column-2, name
name: { type: Sequelize.STRING, allowNull:false },
// Column-3, email
email: { type: Sequelize.STRING, allowNull:false },
// Column-4, default values for
// dates => current time
myDate: { type: Sequelize.DATE,
defaultValue: Sequelize.NOW },
// Timestamps
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
})
// Exporting User, using this constant
// we can perform CRUD operations on
// 'user' table.
module.exports = User
sequelize mysql node js tutorial
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
var corsOptions = {
origin: "http://localhost:8081"
};
app.use(cors(corsOptions));
// parse requests of content-type - application/json
app.use(bodyParser.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// simple route
app.get("/", (req, res) => {
res.json({ message: "Welcome to esparkinfo application." });
});
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
sequelize with mysql nodejs
const express = require(“express”);
const bodyParser = require(“body-parser”);
const cors = require(“cors”);
const app = express();
Var corsOptons = {
origin: “http://localhost:8080/”
};
app.use (cors(corsOptions));
// parse requests of content-type - application/json
app.use (bodyParser.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use (bodyParser.urlencoded({extended:true}));
//simple route
app.get (“/”, (req, res) => {
res.json({message: “Welcome to Turing.com”});
});
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log (‘Server is running on port $(PORT).’ );
});
© 2022 Copyright:
DekGenius.com