From c56c2f286402cd9a4d43e058e3ddaaabc995fcd8 Mon Sep 17 00:00:00 2001 From: Breno Corsi Date: Thu, 9 Oct 2025 14:58:47 -0300 Subject: [PATCH 1/4] feat(db.init.js): create database function --- app/config/db.config.js | 3 ++- app/config/db.init.js | 49 +++++++++++++++++++++++++++++++++++++++++ package.json | 4 +++- 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 app/config/db.init.js diff --git a/app/config/db.config.js b/app/config/db.config.js index 491ebb64..3ac774b8 100644 --- a/app/config/db.config.js +++ b/app/config/db.config.js @@ -2,5 +2,6 @@ module.exports = { HOST: "localhost", USER: "root", PASSWORD: "123456", - DB: "testdb" + DB: "testdb", + DIALECT: "mysql" }; \ No newline at end of file diff --git a/app/config/db.init.js b/app/config/db.init.js new file mode 100644 index 00000000..3e392d94 --- /dev/null +++ b/app/config/db.init.js @@ -0,0 +1,49 @@ +/** +Initialization script for database setup. + +Usage: +Run this file once to create the database (and optionally the tables): +$ node ./app/config/db.init.js + +By default, this script runs immediately when executed. +It does NOT export any functions since it’s meant to be a one-time setup. +If you prefer to import and call the functions manually elsewhere, +just uncomment the last line, and paste the following script in the desired file: + + const { createDatabase, createTables } = require('./app/config/db.init'); + createDatabase(); + createTables(); + +*/ + +const { Sequelize } = require("sequelize"); +const dbConfig = require("../config/db.config.js"); + +async function createDatabase() { + const sequelize = new Sequelize("", dbConfig.USER, dbConfig.PASSWORD, { + host: dbConfig.HOST, + dialect: dbConfig.DIALECT, + }); + + try { + await sequelize.query(`CREATE DATABASE IF NOT EXISTS \`${dbConfig.DB}\`;`); + console.log(`Database "${dbConfig.DB}" created successfully!`); + + } catch (err) { + console.error("Error creating database:", err); + + } finally { + await sequelize.close(); + + } + + console.log(`Database "${dbConfig.DB}" created successfully!`); +} + +async function createTables() { + console.log(`Tables "${dbConfig.DB}" created successfully!`) +} + +createDatabase(); + +// module.exports = { createDatabase, createTables }; // uncomment line to export function diff --git a/package.json b/package.json index d23c6b65..4c19f948 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,8 @@ "dependencies": { "cors": "^2.8.5", "express": "^4.18.2", - "mysql": "^2.18.1" + "mysql": "^2.18.1", + "mysql2": "^3.15.2", + "sequelize": "^6.37.7" } } From 428a21ef6b0270a75a8df6c82ab20521283b2e00 Mon Sep 17 00:00:00 2001 From: Breno Corsi Date: Thu, 9 Oct 2025 20:15:00 -0300 Subject: [PATCH 2/4] feat(db.init.js): create tables function --- app/config/db.init.js | 41 +++++++++++++++++++++++++++++++----- app/models/tutorial.model.js | 33 ++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/app/config/db.init.js b/app/config/db.init.js index 3e392d94..f8baf645 100644 --- a/app/config/db.init.js +++ b/app/config/db.init.js @@ -26,7 +26,7 @@ async function createDatabase() { }); try { - await sequelize.query(`CREATE DATABASE IF NOT EXISTS \`${dbConfig.DB}\`;`); + await sequelize.query(`CREATE DATABASE IF NOT EXISTS \`${dbConfig.DB}\`;`); console.log(`Database "${dbConfig.DB}" created successfully!`); } catch (err) { @@ -34,16 +34,47 @@ async function createDatabase() { } finally { await sequelize.close(); + } +} + +async function createTables() { + const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, { + host: dbConfig.HOST, + dialect: dbConfig.DIALECT, + }); + + // every table should be imported as it follows: + const { TutorialModel } = require("../models/tutorial.model.js"); + // the model should be added to "modelNameList": + const modelNameList = [TutorialModel]; + for (const modelDef of modelNameList) { + const attributes = modelDef.getAttributes(); + sequelize.define( + modelDef.name, + attributes, + { + ...(modelDef.options || {}), + tableName: modelDef.options?.tableName ?? modelDef.tableName + } + ); } - console.log(`Database "${dbConfig.DB}" created successfully!`); + try { + await sequelize.sync({ force: true }); + console.log(`All tables created/updated successfully in database "${dbConfig.DB}"!`); + } catch (err) { + console.error(`Failed to create tables:`, err); + } finally { + await sequelize.close(); + } } -async function createTables() { - console.log(`Tables "${dbConfig.DB}" created successfully!`) +async function init() { + await createDatabase(); + await createTables(); } -createDatabase(); +init(); // module.exports = { createDatabase, createTables }; // uncomment line to export function diff --git a/app/models/tutorial.model.js b/app/models/tutorial.model.js index 17dc453f..f6814468 100644 --- a/app/models/tutorial.model.js +++ b/app/models/tutorial.model.js @@ -1,4 +1,6 @@ const sql = require("./db.js"); +const dbConfig = require("../config/db.config.js"); +const { Sequelize, DataTypes } = require('sequelize'); // constructor const Tutorial = function(tutorial) { @@ -7,6 +9,35 @@ const Tutorial = function(tutorial) { this.published = tutorial.published; }; +// connecting to database +const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, { + host: dbConfig.HOST, + dialect: dbConfig.DIALECT, +}); + +// example table definition +const TutorialModel = sequelize.define('Tutorial', { + id: { + type: DataTypes.INTEGER(11), + primaryKey: true, + autoIncrement: true, + }, + title: { + type: DataTypes.TEXT, + allowNull: false, + }, + description: { + type: DataTypes.STRING, + }, + published: { + type: DataTypes.BOOLEAN, + defaultValue: false, + } +}, { + tableName: 'tutorials', + timestamps: true, +}); + Tutorial.create = (newTutorial, result) => { sql.query("INSERT INTO tutorials SET ?", newTutorial, (err, res) => { if (err) { @@ -126,4 +157,4 @@ Tutorial.removeAll = result => { }); }; -module.exports = Tutorial; +module.exports = { Tutorial, TutorialModel }; From 39141fbba2711c8c3a23c84b184b0a3683734792 Mon Sep 17 00:00:00 2001 From: Breno Corsi Date: Thu, 9 Oct 2025 20:38:22 -0300 Subject: [PATCH 3/4] chore(db.init.js): ident using 2 spaces --- app/config/db.init.js | 94 +++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/app/config/db.init.js b/app/config/db.init.js index f8baf645..d9e919d5 100644 --- a/app/config/db.init.js +++ b/app/config/db.init.js @@ -10,9 +10,9 @@ It does NOT export any functions since it’s meant to be a one-time setup. If you prefer to import and call the functions manually elsewhere, just uncomment the last line, and paste the following script in the desired file: - const { createDatabase, createTables } = require('./app/config/db.init'); - createDatabase(); - createTables(); + const { createDatabase, createTables } = require('./app/config/db.init'); + createDatabase(); + createTables(); */ @@ -20,59 +20,59 @@ const { Sequelize } = require("sequelize"); const dbConfig = require("../config/db.config.js"); async function createDatabase() { - const sequelize = new Sequelize("", dbConfig.USER, dbConfig.PASSWORD, { - host: dbConfig.HOST, - dialect: dbConfig.DIALECT, - }); + const sequelize = new Sequelize("", dbConfig.USER, dbConfig.PASSWORD, { + host: dbConfig.HOST, + dialect: dbConfig.DIALECT, + }); - try { - await sequelize.query(`CREATE DATABASE IF NOT EXISTS \`${dbConfig.DB}\`;`); - console.log(`Database "${dbConfig.DB}" created successfully!`); + try { + await sequelize.query(`CREATE DATABASE IF NOT EXISTS \`${dbConfig.DB}\`;`); + console.log(`Database "${dbConfig.DB}" created successfully!`); - } catch (err) { - console.error("Error creating database:", err); + } catch (err) { + console.error("Error creating database:", err); - } finally { - await sequelize.close(); - } + } finally { + await sequelize.close(); + } } async function createTables() { - const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, { - host: dbConfig.HOST, - dialect: dbConfig.DIALECT, - }); - - // every table should be imported as it follows: - const { TutorialModel } = require("../models/tutorial.model.js"); - // the model should be added to "modelNameList": - const modelNameList = [TutorialModel]; - - for (const modelDef of modelNameList) { - const attributes = modelDef.getAttributes(); - sequelize.define( - modelDef.name, - attributes, - { - ...(modelDef.options || {}), - tableName: modelDef.options?.tableName ?? modelDef.tableName - } - ); - } - - try { - await sequelize.sync({ force: true }); - console.log(`All tables created/updated successfully in database "${dbConfig.DB}"!`); - } catch (err) { - console.error(`Failed to create tables:`, err); - } finally { - await sequelize.close(); - } + const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, { + host: dbConfig.HOST, + dialect: dbConfig.DIALECT, + }); + + // every table should be imported as it follows: + const { TutorialModel } = require("../models/tutorial.model.js"); + // the model should be added to "modelNameList": + const modelNameList = [TutorialModel]; + + for (const modelDef of modelNameList) { + const attributes = modelDef.getAttributes(); + sequelize.define( + modelDef.name, + attributes, + { + ...(modelDef.options || {}), + tableName: modelDef.options?.tableName ?? modelDef.tableName + } + ); + } + + try { + await sequelize.sync({ force: true }); + console.log(`All tables created/updated successfully in database "${dbConfig.DB}"!`); + } catch (err) { + console.error(`Failed to create tables:`, err); + } finally { + await sequelize.close(); + } } async function init() { - await createDatabase(); - await createTables(); + await createDatabase(); + await createTables(); } init(); From 3232ba335105ea56290282d55ad32b481a8e99e6 Mon Sep 17 00:00:00 2001 From: Breno Corsi Date: Thu, 9 Oct 2025 21:36:21 -0300 Subject: [PATCH 4/4] chore(tutorial.model): instruction comments --- app/config/db.init.js | 1 + app/models/tutorial.model.js | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/config/db.init.js b/app/config/db.init.js index d9e919d5..f24c9fbc 100644 --- a/app/config/db.init.js +++ b/app/config/db.init.js @@ -45,6 +45,7 @@ async function createTables() { // every table should be imported as it follows: const { TutorialModel } = require("../models/tutorial.model.js"); + // the model should be added to "modelNameList": const modelNameList = [TutorialModel]; diff --git a/app/models/tutorial.model.js b/app/models/tutorial.model.js index f6814468..a61800c7 100644 --- a/app/models/tutorial.model.js +++ b/app/models/tutorial.model.js @@ -15,7 +15,7 @@ const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, { dialect: dbConfig.DIALECT, }); -// example table definition +// example table definition (every table model should be exported in the last line) const TutorialModel = sequelize.define('Tutorial', { id: { type: DataTypes.INTEGER(11), @@ -157,4 +157,5 @@ Tutorial.removeAll = result => { }); }; +// add here modules to export module.exports = { Tutorial, TutorialModel };