synchronize migrations & models data schemes

This commit is contained in:
b1ek 2023-02-23 00:21:29 +10:00
parent 7838c1abbe
commit 5970380824
Signed by: blek
GPG Key ID: 14546221E3595D0C
7 changed files with 137 additions and 218 deletions

View File

@ -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')) {

View File

@ -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 */

View File

@ -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');
}
};

View File

@ -1,11 +1,10 @@
const { Model } = require('sequelize');
const { Model, DataTypes } = require('sequelize');
class Article extends Model {
}
let init = (sequelize, DataTypes) => {
let article = Article.init({
Article.structure = {
id: {
type: DataTypes.BIGINT(11),
primaryKey: true,
@ -40,7 +39,15 @@ let init = (sequelize, DataTypes) => {
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(Article.structure, {
sequelize,
modelName: 'Article',
tableName: 'articles'

View File

@ -1,18 +1,10 @@
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
}
}
const init = (sequelize, DataTypes) => {
let model = Guestbook.init({
Guestbook.structure = {
id: {
type: DataTypes.BIGINT(11),
primaryKey: true,
@ -43,7 +35,15 @@ const init = (sequelize, DataTypes) => {
time: {
type: DataTypes.BIGINT
}
}, {
};
/**
* @param {import('sequelize').Sequelize} sequelize
* @param {import('sequelize').DataTypes} DataTypes
* @returns Guestbook
*/
const init = (sequelize, DataTypes) => {
let model = Guestbook.init(Guestbook.structure, {
sequelize,
modelName: 'Guestbook',
tableName: 'guestbook'
@ -52,4 +52,4 @@ const init = (sequelize, DataTypes) => {
};
init.class = Guestbook;
return init;
module.exports = init;

View File

@ -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) {

View File

@ -1,16 +1,10 @@
const { Model } = require('sequelize');
const { Model, DataTypes } = require('sequelize');
class User extends Model {
}
/**
*
* @param {import('sequelize').Sequelize} sequelize
* @param {import('sequelize').DataTypes} DataTypes
*/
const init = (sequelize, DataTypes) => {
let user = User.init({
const structure = {
id: {
type: DataTypes.BIGINT,
primaryKey: true,
@ -41,7 +35,16 @@ const init = (sequelize, DataTypes) => {
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(structure, {
sequelize,
tableName: 'users',
tableName: 'users'