feat: complete day 4

This commit is contained in:
Ayobami
2025-07-11 20:19:55 +01:00
parent 2b58c1d8b0
commit 95edc56088
10 changed files with 509 additions and 49 deletions
+38
View File
@@ -0,0 +1,38 @@
module.exports = (sequelize, DataTypes) => {
const email = sequelize.define(
"email",
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
slug: {
type: DataTypes.STRING,
unique: true,
allowNull: false,
},
subject: {
type: DataTypes.STRING,
allowNull: false,
},
body: {
type: DataTypes.TEXT,
allowNull: false,
},
status: {
type: DataTypes.INTEGER, // 0: inactive, 1: active
allowNull: false,
defaultValue: 1,
},
},
{
timestamps: true,
freezeTableName: true,
tableName: "email",
createdAt: "created_at",
updatedAt: "updated_at",
}
);
return email;
};
+45
View File
@@ -0,0 +1,45 @@
module.exports = (sequelize, DataTypes) => {
const email_queue = sequelize.define(
"email_queue",
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
email_id: {
type: DataTypes.INTEGER,
allowNull: false,
},
user_id: {
type: DataTypes.INTEGER,
allowNull: false,
},
status: {
type: DataTypes.INTEGER, // 0: not sent, 1: sent
allowNull: false,
defaultValue: 0,
},
created_at: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
send_at: {
type: DataTypes.DATE,
allowNull: false,
},
updated_at: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
},
{
timestamps: false,
freezeTableName: true,
tableName: "email_queue",
}
);
return email_queue;
};
+48 -34
View File
@@ -1,4 +1,4 @@
'use strict';
"use strict";
/*Powered By: Manaknightdigital Inc. https://manaknightdigital.com/ Year: 2020*/
/**
* Sequelize File
@@ -8,49 +8,63 @@
* @author Ryan Wong
*
*/
const fs = require('fs');
const path = require('path');
let Sequelize = require('sequelize');
const fs = require("fs");
const path = require("path");
let Sequelize = require("sequelize");
const basename = path.basename(__filename);
const { DataTypes } = require('sequelize');
const { DataTypes } = require("sequelize");
const config = {
DB_DATABASE: 'mysql',
DB_USERNAME: 'root',
DB_PASSWORD: 'root',
DB_ADAPTER: 'mysql',
DB_NAME: 'day_1',
DB_HOSTNAME: 'localhost',
DB_DATABASE: "mysql",
DB_USERNAME: "root",
DB_PASSWORD: process.env.DB_PASSWORD || "root",
DB_ADAPTER: "mysql",
DB_NAME: "day_4",
DB_HOSTNAME: "localhost",
DB_PORT: 3306,
};
let db = {};
let sequelize = new Sequelize(config.DB_DATABASE, config.DB_USERNAME, config.DB_PASSWORD, {
dialect: config.DB_ADAPTER,
username: config.DB_USERNAME,
password: config.DB_PASSWORD,
database: config.DB_NAME,
host: config.DB_HOSTNAME,
port: config.DB_PORT,
logging: console.log,
timezone: '-04:00',
pool: {
maxConnections: 1,
minConnections: 0,
maxIdleTime: 100,
},
define: {
timestamps: false,
underscoredAll: true,
underscored: true,
},
});
let sequelize = new Sequelize(
config.DB_NAME,
config.DB_USERNAME,
config.DB_PASSWORD,
{
dialect: config.DB_ADAPTER,
username: config.DB_USERNAME,
password: config.DB_PASSWORD,
database: config.DB_NAME,
host: config.DB_HOSTNAME,
port: config.DB_PORT,
logging: console.log,
timezone: "-04:00",
pool: {
maxConnections: 1,
minConnections: 0,
maxIdleTime: 100,
},
define: {
timestamps: false,
underscoredAll: true,
underscored: true,
},
}
);
// sequelize.sync({ force: true });
sequelize
.sync()
.then(() => {
console.log("All tables synced successfully.");
})
.catch((err) => {
console.error("Failed to sync tables:", err);
});
fs.readdirSync(__dirname)
.filter((file) => {
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
return (
file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
);
})
.forEach((file) => {
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
@@ -66,4 +80,4 @@ Object.keys(db).forEach((modelName) => {
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
module.exports = db;
+34
View File
@@ -0,0 +1,34 @@
module.exports = (sequelize, DataTypes) => {
const user = sequelize.define(
"user",
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
status: {
type: DataTypes.INTEGER, // 0: inactive, 1: active
allowNull: false,
defaultValue: 1,
},
},
{
timestamps: true,
freezeTableName: true,
tableName: "user",
createdAt: "created_at",
updatedAt: "updated_at",
}
);
return user;
};