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} */ /** @type {import('sequelize-cli').Migration} */
module.exports = { module.exports = {
async up(queryInterface, DataTypes) { async up(queryInterface, DataTypes) {
await queryInterface.createTable('guestbook', { const struct = require('../models').Guestbook.structure;
id: { await queryInterface.createTable('guestbook', struct);
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
}
});
}, },
async down(queryInterface, Sequelize) { async down(queryInterface, Sequelize) {
if (await queryInterface.tableExists('guestbook')) { if (await queryInterface.tableExists('guestbook')) {

View File

@ -4,38 +4,8 @@
module.exports = { module.exports = {
/** @param {import('sequelize').QueryInterface} queryInterface */ /** @param {import('sequelize').QueryInterface} queryInterface */
async up (queryInterface, DataTypes) { async up (queryInterface, DataTypes) {
queryInterface.createTable('articles', { const struct = require('../models').Article.structure;
id: { queryInterface.createTable('articles', struct);
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
}
});
}, },
/** @param {import('sequelize').QueryInterface} queryInterface */ /** @param {import('sequelize').QueryInterface} queryInterface */

View File

@ -1,22 +1,14 @@
'use strict';
/** @type {import('sequelize-cli').Migration} */ /** @type {import('sequelize-cli').Migration} */
module.exports = { module.exports = {
/** @param {import('sequelize').QueryInterface} queryInterface */
async up (queryInterface, Sequelize) { async up (queryInterface, Sequelize) {
/** const struct = require('../models').User.structure;
* Add altering commands here. await queryInterface.createTable('users', struct);
*
* Example:
* await queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
}, },
/** @param {import('sequelize').QueryInterface} queryInterface */
async down (queryInterface, Sequelize) { async down (queryInterface, Sequelize) {
/** await queryInterface.dropTable('users');
* Add reverting commands here.
*
* Example:
* await queryInterface.dropTable('users');
*/
} }
}; };

View File

@ -1,46 +1,53 @@
const { Model } = require('sequelize'); const { Model, DataTypes } = require('sequelize');
class Article extends Model { 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 init = (sequelize, DataTypes) => {
let article = Article.init({ let article = Article.init(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
}
}, {
sequelize, sequelize,
modelName: 'Article', modelName: 'Article',
tableName: 'articles' tableName: 'articles'

View File

@ -1,49 +1,49 @@
const { Model } = require('sequelize'); const { Model, DataTypes } = require('sequelize');
class Guestbook extends Model { 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) => { const init = (sequelize, DataTypes) => {
let model = Guestbook.init({ let model = Guestbook.init(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
}
}, {
sequelize, sequelize,
modelName: 'Guestbook', modelName: 'Guestbook',
tableName: 'guestbook' tableName: 'guestbook'
@ -52,4 +52,4 @@ const init = (sequelize, DataTypes) => {
}; };
init.class = Guestbook; 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'; const env = process.env.NODE_ENV || 'production';
let config = require(__dirname + '/../config/config.json')[env]; 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 db = {};
const { const {
@ -41,6 +29,7 @@ config = {
} }
}; };
/** @type Sequelize */
let sequelize; let sequelize;
if (config.use_env_variable) { if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config); 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); sequelize = new Sequelize(config.database, config.username, config.password, config);
} }
fs db.User = require('./user')(sequelize, sequelize.DataTypes);
.readdirSync(__dirname) db.Guestbook = require('./guestbook')(sequelize, sequelize.DataTypes);
.filter(file => { db.Article = require('./article')(sequelize, sequelize.DataTypes);
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 => { Object.keys(db).forEach(modelName => {
if (db[modelName].associate) { if (db[modelName].associate) {

View File

@ -1,47 +1,50 @@
const { Model } = require('sequelize'); const { Model, DataTypes } = require('sequelize');
class User extends Model { 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').Sequelize} sequelize
* @param {import('sequelize').DataTypes} DataTypes * @param {import('sequelize').DataTypes} DataTypes
* @returns User
*/ */
const init = (sequelize, DataTypes) => { const init = (sequelize, DataTypes) => {
let user = User.init({ let user = User.init(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
}
}, {
sequelize, sequelize,
tableName: 'users', tableName: 'users',
tableName: 'users' tableName: 'users'