From 597038082404e80cbd6eaef12a47497049ffc4ba Mon Sep 17 00:00:00 2001 From: b1ek Date: Thu, 23 Feb 2023 00:21:29 +1000 Subject: [PATCH] synchronize migrations & models data schemes --- migrations/20230219070939-create-guestbook.js | 35 +------- migrations/20230222114001-create-articles.js | 34 +------- migrations/20230222134124-create-users.js | 18 ++-- models/article.js | 83 +++++++++--------- models/guestbook.js | 84 +++++++++---------- models/index.js | 30 +------ models/user.js | 71 ++++++++-------- 7 files changed, 137 insertions(+), 218 deletions(-) diff --git a/migrations/20230219070939-create-guestbook.js b/migrations/20230219070939-create-guestbook.js index 7a99296..c82949b 100644 --- a/migrations/20230219070939-create-guestbook.js +++ b/migrations/20230219070939-create-guestbook.js @@ -2,39 +2,8 @@ /** @type {import('sequelize-cli').Migration} */ module.exports = { async up(queryInterface, DataTypes) { - await queryInterface.createTable('guestbook', { - 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, - allowNull: false - } - }); + const struct = require('../models').Guestbook.structure; + await queryInterface.createTable('guestbook', struct); }, async down(queryInterface, Sequelize) { if (await queryInterface.tableExists('guestbook')) { diff --git a/migrations/20230222114001-create-articles.js b/migrations/20230222114001-create-articles.js index 157161f..490bd1f 100644 --- a/migrations/20230222114001-create-articles.js +++ b/migrations/20230222114001-create-articles.js @@ -4,38 +4,8 @@ module.exports = { /** @param {import('sequelize').QueryInterface} queryInterface */ async up (queryInterface, DataTypes) { - queryInterface.createTable('articles', { - id: { - type: DataTypes.BIGINT(11), - primaryKey: true, - autoIncrement: true, - allowNull: false - }, - title: { - type: DataTypes.TEXT, - allowNull: false - }, - shortText: { - type: DataTypes.TEXT, - allowNull: false - }, - body: { - type: DataTypes.TEXT, - allowNull: false - }, - submitted: { - type: DataTypes.BIGINT, - allowNull: false - }, - edited: { - type: DataTypes.BIGINT, - allowNull: false - }, - submitter: { - type: DataTypes.BIGINT, - allowNull: false - } - }); + const struct = require('../models').Article.structure; + queryInterface.createTable('articles', struct); }, /** @param {import('sequelize').QueryInterface} queryInterface */ diff --git a/migrations/20230222134124-create-users.js b/migrations/20230222134124-create-users.js index b6e01de..73ade60 100644 --- a/migrations/20230222134124-create-users.js +++ b/migrations/20230222134124-create-users.js @@ -1,22 +1,14 @@ -'use strict'; /** @type {import('sequelize-cli').Migration} */ module.exports = { + /** @param {import('sequelize').QueryInterface} queryInterface */ async up (queryInterface, Sequelize) { - /** - * Add altering commands here. - * - * Example: - * await queryInterface.createTable('users', { id: Sequelize.INTEGER }); - */ + const struct = require('../models').User.structure; + await queryInterface.createTable('users', struct); }, + /** @param {import('sequelize').QueryInterface} queryInterface */ async down (queryInterface, Sequelize) { - /** - * Add reverting commands here. - * - * Example: - * await queryInterface.dropTable('users'); - */ + await queryInterface.dropTable('users'); } }; diff --git a/models/article.js b/models/article.js index 70dd9af..98eb933 100644 --- a/models/article.js +++ b/models/article.js @@ -1,46 +1,53 @@ -const { Model } = require('sequelize'); +const { Model, DataTypes } = require('sequelize'); class Article extends Model { - + } +Article.structure = { + id: { + type: DataTypes.BIGINT(11), + primaryKey: true, + autoIncrement: true, + allowNull: false + }, + title: { + type: DataTypes.TEXT, + allowNull: false + }, + shortText: { + type: DataTypes.TEXT, + allowNull: false + }, + body: { + type: DataTypes.TEXT, + allowNull: false + }, + submitted: { + type: DataTypes.BIGINT, + allowNull: false + }, + edited: { + type: DataTypes.BIGINT, + allowNull: false + }, + submitter: { + type: DataTypes.BIGINT, + allowNull: false + }, + gpgsign: { + type: DataTypes.TEXT, + allowNull: true + } +}; + +/** + * @param {import('sequelize').Sequelize} sequelize + * @param {import('sequelize').DataTypes} DataTypes + * @returns Article + */ let init = (sequelize, DataTypes) => { - let article = Article.init({ - id: { - type: DataTypes.BIGINT(11), - primaryKey: true, - autoIncrement: true, - allowNull: false - }, - title: { - type: DataTypes.TEXT, - allowNull: false - }, - shortText: { - type: DataTypes.TEXT, - allowNull: false - }, - body: { - type: DataTypes.TEXT, - allowNull: false - }, - submitted: { - type: DataTypes.BIGINT, - allowNull: false - }, - edited: { - type: DataTypes.BIGINT, - allowNull: false - }, - submitter: { - type: DataTypes.BIGINT, - allowNull: false - }, - gpgsign: { - type: DataTypes.TEXT, - allowNull: true - } - }, { + let article = Article.init(Article.structure, { sequelize, modelName: 'Article', tableName: 'articles' diff --git a/models/guestbook.js b/models/guestbook.js index ecb1260..4e0c8dc 100644 --- a/models/guestbook.js +++ b/models/guestbook.js @@ -1,49 +1,49 @@ -const { Model } = require('sequelize'); +const { Model, DataTypes } = require('sequelize'); 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.structure = { + 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 + } +}; + +/** + * @param {import('sequelize').Sequelize} sequelize + * @param {import('sequelize').DataTypes} DataTypes + * @returns Guestbook + */ const init = (sequelize, DataTypes) => { - let model = 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 - } - }, { + let model = Guestbook.init(Guestbook.structure, { sequelize, modelName: 'Guestbook', tableName: 'guestbook' @@ -52,4 +52,4 @@ const init = (sequelize, DataTypes) => { }; init.class = Guestbook; -return init; \ No newline at end of file +module.exports = init; \ No newline at end of file diff --git a/models/index.js b/models/index.js index 7ede145..03ce21e 100644 --- a/models/index.js +++ b/models/index.js @@ -10,18 +10,6 @@ const basename = path.basename(__filename); const env = process.env.NODE_ENV || 'production'; let config = require(__dirname + '/../config/config.json')[env]; -class Models { - sequelize = Sequelize - Sequelize = Sequelize - - User = require('./user').class - Article = require('./article').class - Guestbook = require('./guestbook').class -} - -/** - * @type Models - */ const db = {}; const { @@ -41,6 +29,7 @@ config = { } }; +/** @type Sequelize */ let sequelize; if (config.use_env_variable) { sequelize = new Sequelize(process.env[config.use_env_variable], config); @@ -48,20 +37,9 @@ if (config.use_env_variable) { 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; - }); +db.User = require('./user')(sequelize, sequelize.DataTypes); +db.Guestbook = require('./guestbook')(sequelize, sequelize.DataTypes); +db.Article = require('./article')(sequelize, sequelize.DataTypes); Object.keys(db).forEach(modelName => { if (db[modelName].associate) { diff --git a/models/user.js b/models/user.js index c338cc5..2e7d58e 100644 --- a/models/user.js +++ b/models/user.js @@ -1,47 +1,50 @@ -const { Model } = require('sequelize'); +const { Model, DataTypes } = require('sequelize'); class User extends Model { } +const structure = { + id: { + type: DataTypes.BIGINT, + primaryKey: true, + autoIncrement: true, + allowNull: false + }, + login: { + type: DataTypes.TEXT, + allowNull: false + }, + pass: { + type: DataTypes.TEXT, + allowNull: false + }, + totp: { + type: DataTypes.TEXT, + allowNull: true + }, + totpRec: { + type: DataTypes.TEXT, + allowNull: true + }, + email: { + type: DataTypes.TEXT, + allowNull: false + }, + gpgkey: { + type: DataTypes.TEXT, + allowNull: false + } +} +User.structure = structure + /** - * * @param {import('sequelize').Sequelize} sequelize * @param {import('sequelize').DataTypes} DataTypes + * @returns User */ const init = (sequelize, DataTypes) => { - let user = User.init({ - id: { - type: DataTypes.BIGINT, - primaryKey: true, - autoIncrement: true, - allowNull: false - }, - login: { - type: DataTypes.TEXT, - allowNull: false - }, - pass: { - type: DataTypes.TEXT, - allowNull: false - }, - totp: { - type: DataTypes.TEXT, - allowNull: true - }, - totpRec: { - type: DataTypes.TEXT, - allowNull: true - }, - email: { - type: DataTypes.TEXT, - allowNull: false - }, - gpgkey: { - type: DataTypes.TEXT, - allowNull: false - } - }, { + let user = User.init(structure, { sequelize, tableName: 'users', tableName: 'users'