ORM models added

This commit is contained in:
newtbot 2023-12-17 18:31:17 +08:00
parent ca0d24248c
commit 33adf3f1d9
4 changed files with 95 additions and 11 deletions

View File

@ -0,0 +1,45 @@
'use strict';
const { Sequelize, DataTypes } = require('sequelize');
const { sequelize } = require("../mySQL.js")
const adminUserModel = sequelize.define('adminusers', {
// Model attributes are defined here
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
username: {
type: DataTypes.STRING,
allowNull: true,
length: 50
},
email: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
password: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
lastLogin: {
type: DataTypes.timestamps,
allowNull: true,
},
jobTitle: {
type: DataTypes.STRING,
allowNull: true,
length: 255
}
},{
timestamps: false, // Disable automatic timestamps
});
module.exports = { adminUserModel }

View File

@ -0,0 +1,40 @@
'use strict';
const { Sequelize, DataTypes } = require('sequelize');
const { sequelize } = require("../mySQL.js")
const userModel = sequelize.define('users', {
// Model attributes are defined here
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
username: {
type: DataTypes.STRING,
allowNull: true,
length: 50
},
email: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
password: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
lastLogin: {
type: DataTypes.timestamps,
allowNull: true,
}
},{
timestamps: false, // Disable automatic timestamps
});
module.exports = { userModel }

View File

@ -1,17 +1,17 @@
require("dotenv").config({ path: "../../.env" });
require("dotenv").config({ path: "../.env" });
const Sequelize = require("sequelize");
const sequelize = new Sequelize(
"your_database_name",
process.env.
process.env.,
{
host: '',
"adminusers",
process.env.DB_USER,
process.env.DB_PASS,
{
host: "mpsqldatabasean.mysql.database.azure.com",
dialect: 'mysql'
}
);
sequelize.authenticate().then(() => {
console.log('Connection has been established successfully.');
}).catch((error) => {

View File

@ -7,7 +7,6 @@ const app = express();
const PORT = process.env.PORT || 3000;
require('dotenv').config()
// MySQL setup (replace with your MySQL connection details)
const mysqlConfig = {
host: process.env.host,
user: process.env.user,
@ -38,7 +37,7 @@ function isAuthenticated(req, res, next) {
app.post('/login', (req, res) => {
let { username, password } = req.body;
// Trim leading and trailing spaces from username
// Trim whitespace
username = username.trim();
// Validate username and password against MySQL