diff --git a/README.md b/README.md new file mode 100644 index 0000000..5846b2d --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# homepage.js +This is a rewrite of my current website to Express.JS. + +# This was aborted. +With focusing on code structure & performance, I decided +to rewrite the website into JavaScript, but as it turned out, +i can't provide enough for my satisfaction with ExpressJS. + +I am re writing this to Go & atreugo diff --git a/config/config.json b/config/config.json new file mode 100644 index 0000000..0f858c6 --- /dev/null +++ b/config/config.json @@ -0,0 +1,23 @@ +{ + "development": { + "username": "root", + "password": null, + "database": "database_development", + "host": "127.0.0.1", + "dialect": "mysql" + }, + "test": { + "username": "root", + "password": null, + "database": "database_test", + "host": "127.0.0.1", + "dialect": "mysql" + }, + "production": { + "username": "root", + "password": null, + "database": "database_production", + "host": "127.0.0.1", + "dialect": "mysql" + } +} diff --git a/migrations/20230219070939-create-guestbook.js b/migrations/20230219070939-create-guestbook.js new file mode 100644 index 0000000..2a85f53 --- /dev/null +++ b/migrations/20230219070939-create-guestbook.js @@ -0,0 +1,31 @@ +'use strict'; +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.createTable('Guestbooks', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + id: { + type: Sequelize.BIGINT + }, + name: { + type: Sequelize.TEXT + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + async down(queryInterface, Sequelize) { + await queryInterface.dropTable('Guestbooks'); + } +}; \ No newline at end of file diff --git a/models/guestbook.js b/models/guestbook.js new file mode 100644 index 0000000..2f93f05 --- /dev/null +++ b/models/guestbook.js @@ -0,0 +1,52 @@ +'use strict'; +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class Guestbook extends Model { + /** + * Helper method for defining associations. + * This method is not a part of Sequelize lifecycle. + * The `models/index` file will call this method automatically. + */ + static associate(models) { + // define association here + } + } + Guestbook.init({ + id: { + type: DataTypes.BIGINT(11), + primaryKey: true, + autoIncrement: true, + allowNull: false + }, + name: { + type: DataTypes.TEXT, + allowNull: false + }, + email: DataTypes.TEXT, + text: { + type: DataTypes.TEXT, + allowNull: false + }, + hidemail: { + type: DataTypes.BOOLEAN, + allowNull: false + }, + ip: { + type: DataTypes.TEXT, + allowNull: false + }, + hidden: { + type: DataTypes.BOOLEAN, + allowNull: false + }, + time: { + type: DataTypes.BIGINT + } + }, { + sequelize, + modelName: 'Guestbook', + }); + return Guestbook; +}; \ No newline at end of file diff --git a/models/index.js b/models/index.js new file mode 100644 index 0000000..024200e --- /dev/null +++ b/models/index.js @@ -0,0 +1,43 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); +const Sequelize = require('sequelize'); +const process = require('process'); +const basename = path.basename(__filename); +const env = process.env.NODE_ENV || 'development'; +const config = require(__dirname + '/../config/config.json')[env]; +const db = {}; + +let sequelize; +if (config.use_env_variable) { + sequelize = new Sequelize(process.env[config.use_env_variable], config); +} else { + sequelize = new Sequelize(config.database, config.username, config.password, config); +} + +fs + .readdirSync(__dirname) + .filter(file => { + return ( + file.indexOf('.') !== 0 && + file !== basename && + file.slice(-3) === '.js' && + file.indexOf('.test.js') === -1 + ); + }) + .forEach(file => { + const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes); + db[model.name] = model; + }); + +Object.keys(db).forEach(modelName => { + if (db[modelName].associate) { + db[modelName].associate(db); + } +}); + +db.sequelize = sequelize; +db.Sequelize = Sequelize; + +module.exports = db;