2021-02-16 16:48:04 +02:00
|
|
|
const {
|
|
|
|
Sequelize,
|
|
|
|
DataTypes
|
|
|
|
} = require('sequelize');
|
|
|
|
const { path } = require('../utils/fs');
|
|
|
|
const config = require('../../user/config');
|
|
|
|
const types = require('./dialects');
|
|
|
|
|
|
|
|
module.exports = async (log) => {
|
|
|
|
|
|
|
|
const {
|
|
|
|
DB_TYPE,
|
|
|
|
DB_HOST,
|
2021-02-26 17:34:06 +02:00
|
|
|
DB_PORT,
|
2021-02-16 16:48:04 +02:00
|
|
|
DB_USER,
|
|
|
|
DB_PASS,
|
2021-02-17 00:34:20 +02:00
|
|
|
DB_NAME,
|
|
|
|
DB_TABLE_PREFIX
|
2021-02-16 16:48:04 +02:00
|
|
|
} = process.env;
|
|
|
|
|
|
|
|
let type = (DB_TYPE || 'sqlite').toLowerCase();
|
|
|
|
|
2021-03-24 01:32:24 +02:00
|
|
|
const supported = Object.keys(types);
|
2021-02-16 16:48:04 +02:00
|
|
|
if (!supported.includes(type)) {
|
2021-02-17 00:34:20 +02:00
|
|
|
log.error(new Error(`DB_TYPE (${type}) is not a valid type`));
|
|
|
|
return process.exit();
|
2021-02-16 16:48:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
types[type].packages.forEach(pkg => require(pkg));
|
|
|
|
} catch {
|
|
|
|
let required = types[type].packages.map(i => `"${i}"`).join(' and ');
|
2021-02-17 00:34:20 +02:00
|
|
|
log.error(new Error(`Please install the package(s) for your selected database type: ${required}`));
|
|
|
|
return process.exit();
|
2021-02-16 16:48:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let sequelize;
|
|
|
|
|
|
|
|
if (type === 'sqlite') {
|
|
|
|
log.info('Using SQLite storage');
|
|
|
|
sequelize = new Sequelize({
|
|
|
|
dialect: types[type].dialect,
|
|
|
|
storage: path('./user/database.sqlite'),
|
2021-03-03 22:21:27 +02:00
|
|
|
logging: text => log.debug(text)
|
2021-02-16 16:48:04 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
log.info(`Connecting to ${types[type].name} database...`);
|
|
|
|
sequelize = new Sequelize(DB_NAME, DB_USER, DB_PASS, {
|
|
|
|
dialect: types[type].dialect,
|
|
|
|
host: DB_HOST,
|
2021-02-26 17:34:06 +02:00
|
|
|
port: DB_PORT,
|
2021-03-03 22:25:54 +02:00
|
|
|
logging: text => log.debug(text)
|
2021-02-16 16:48:04 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await sequelize.authenticate();
|
|
|
|
log.success('Connected to database successfully');
|
|
|
|
} catch (error) {
|
2021-03-16 13:16:08 +02:00
|
|
|
log.warn('Failed to connect to database');
|
2021-02-16 16:48:04 +02:00
|
|
|
log.error(error);
|
2021-02-17 00:34:20 +02:00
|
|
|
return process.exit();
|
2021-02-16 16:48:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Guild = sequelize.define('Guild', {
|
|
|
|
id: {
|
|
|
|
type: DataTypes.CHAR(18),
|
2021-02-17 00:34:20 +02:00
|
|
|
primaryKey: true,
|
|
|
|
allowNull: false,
|
2021-02-16 16:48:04 +02:00
|
|
|
},
|
2021-03-24 01:32:24 +02:00
|
|
|
locale: {
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
defaultValue: config.locale
|
|
|
|
},
|
2021-02-20 19:09:08 +02:00
|
|
|
colour: {
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
defaultValue: config.defaults.colour
|
|
|
|
},
|
2021-03-24 01:32:24 +02:00
|
|
|
success_colour: {
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
defaultValue: 'GREEN'
|
|
|
|
},
|
|
|
|
error_colour: {
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
defaultValue: 'RED'
|
|
|
|
},
|
2021-02-21 17:58:06 +02:00
|
|
|
log_messages: {
|
|
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
defaultValue: config.defaults.log_messages
|
|
|
|
},
|
2021-02-16 16:48:04 +02:00
|
|
|
}, {
|
2021-02-17 00:34:20 +02:00
|
|
|
tableName: DB_TABLE_PREFIX + 'guilds'
|
2021-02-16 16:48:04 +02:00
|
|
|
});
|
|
|
|
|
2021-02-25 15:12:15 +02:00
|
|
|
const Category = sequelize.define('Category', {
|
|
|
|
id: {
|
|
|
|
type: DataTypes.CHAR(18),
|
|
|
|
primaryKey: true,
|
|
|
|
allowNull: false,
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false,
|
2021-03-29 01:49:36 +03:00
|
|
|
unique: 'name-guild'
|
2021-02-25 15:12:15 +02:00
|
|
|
},
|
|
|
|
guild: {
|
2021-03-28 20:38:17 +03:00
|
|
|
type: DataTypes.CHAR(18),
|
2021-02-25 15:12:15 +02:00
|
|
|
allowNull: false,
|
|
|
|
references: {
|
|
|
|
model: Guild,
|
|
|
|
key: 'id'
|
|
|
|
},
|
2021-03-29 01:49:36 +03:00
|
|
|
unique: 'name-guild'
|
2021-02-25 15:12:15 +02:00
|
|
|
},
|
2021-03-24 01:32:24 +02:00
|
|
|
roles: {
|
|
|
|
type: DataTypes.JSON
|
2021-03-29 01:49:36 +03:00
|
|
|
},
|
|
|
|
max_per_member: {
|
|
|
|
type: DataTypes.INTEGER,
|
|
|
|
defaultValue: 1
|
|
|
|
},
|
|
|
|
name_format: {
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: config.defaults.name_format
|
2021-03-24 01:32:24 +02:00
|
|
|
}
|
2021-02-25 15:12:15 +02:00
|
|
|
}, {
|
|
|
|
tableName: DB_TABLE_PREFIX + 'categories'
|
|
|
|
});
|
2021-02-16 16:48:04 +02:00
|
|
|
|
|
|
|
const Ticket = sequelize.define('Ticket', {
|
|
|
|
id: {
|
|
|
|
type: DataTypes.CHAR(18),
|
2021-02-17 00:34:20 +02:00
|
|
|
primaryKey: true,
|
|
|
|
allowNull: false,
|
2021-02-16 16:48:04 +02:00
|
|
|
},
|
2021-02-20 23:27:49 +02:00
|
|
|
number: {
|
|
|
|
type: DataTypes.INTEGER,
|
|
|
|
allowNull: false,
|
2021-03-29 01:49:36 +03:00
|
|
|
unique: 'number-guild'
|
2021-02-20 23:27:49 +02:00
|
|
|
},
|
2021-02-16 16:48:04 +02:00
|
|
|
guild: {
|
2021-03-28 20:38:17 +03:00
|
|
|
type: DataTypes.CHAR(18),
|
2021-02-16 16:48:04 +02:00
|
|
|
allowNull: false,
|
|
|
|
references: {
|
|
|
|
model: Guild,
|
|
|
|
key: 'id'
|
2021-03-29 01:49:36 +03:00
|
|
|
},
|
|
|
|
unique: 'number-guild'
|
2021-02-20 23:27:49 +02:00
|
|
|
},
|
2021-02-25 15:12:15 +02:00
|
|
|
category: {
|
2021-03-28 20:38:17 +03:00
|
|
|
type: DataTypes.CHAR(18),
|
2021-02-25 15:12:15 +02:00
|
|
|
allowNull: false,
|
|
|
|
references: {
|
|
|
|
model: Category,
|
|
|
|
key: 'id'
|
|
|
|
},
|
|
|
|
},
|
2021-03-28 20:38:17 +03:00
|
|
|
creator: {
|
|
|
|
type: DataTypes.CHAR(18),
|
|
|
|
allowNull: false,
|
|
|
|
},
|
2021-03-29 01:49:36 +03:00
|
|
|
open: {
|
|
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
defaultValue: true
|
|
|
|
}
|
2021-02-16 16:48:04 +02:00
|
|
|
}, {
|
2021-02-17 00:34:20 +02:00
|
|
|
tableName: DB_TABLE_PREFIX + 'tickets'
|
2021-02-16 16:48:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const Message = sequelize.define('Message', {
|
|
|
|
id: {
|
|
|
|
type: DataTypes.CHAR(18),
|
2021-02-17 00:34:20 +02:00
|
|
|
primaryKey: true,
|
|
|
|
allowNull: false,
|
2021-02-16 16:48:04 +02:00
|
|
|
},
|
|
|
|
ticket: {
|
2021-03-28 20:38:17 +03:00
|
|
|
type: DataTypes.CHAR(18),
|
2021-02-16 16:48:04 +02:00
|
|
|
allowNull: false,
|
|
|
|
references: {
|
|
|
|
model: Ticket,
|
|
|
|
key: 'id'
|
|
|
|
},
|
2021-02-21 17:58:06 +02:00
|
|
|
},
|
|
|
|
author: {
|
|
|
|
type: DataTypes.CHAR(18),
|
|
|
|
allowNull: false,
|
|
|
|
},
|
2021-02-22 02:05:37 +02:00
|
|
|
edited: {
|
|
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
defaultValue: false,
|
|
|
|
},
|
|
|
|
deleted: {
|
|
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
defaultValue: false,
|
|
|
|
},
|
2021-02-21 17:58:06 +02:00
|
|
|
updates: {
|
|
|
|
type: DataTypes.JSON
|
|
|
|
},
|
2021-02-16 16:48:04 +02:00
|
|
|
}, {
|
2021-02-17 00:34:20 +02:00
|
|
|
tableName: DB_TABLE_PREFIX + 'messages'
|
2021-02-16 16:48:04 +02:00
|
|
|
});
|
|
|
|
|
2021-03-15 23:12:58 +02:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const UserEntity = sequelize.define('UserEntity', {
|
|
|
|
user: {
|
|
|
|
type: DataTypes.CHAR(18),
|
|
|
|
allowNull: false,
|
2021-03-29 01:49:36 +03:00
|
|
|
unique: 'id-ticket'
|
2021-03-15 23:12:58 +02:00
|
|
|
},
|
|
|
|
ticket: {
|
2021-03-28 20:38:17 +03:00
|
|
|
type: DataTypes.CHAR(18),
|
2021-03-15 23:12:58 +02:00
|
|
|
allowNull: false,
|
2021-03-29 01:49:36 +03:00
|
|
|
unique: 'id-ticket',
|
2021-03-15 23:12:58 +02:00
|
|
|
references: {
|
|
|
|
model: Ticket,
|
|
|
|
key: 'id'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
avatar: DataTypes.STRING,
|
|
|
|
username: DataTypes.STRING,
|
|
|
|
discriminator: DataTypes.STRING,
|
|
|
|
display_name: DataTypes.STRING,
|
|
|
|
colour: DataTypes.INTEGER,
|
|
|
|
bot: DataTypes.BOOLEAN
|
|
|
|
}, {
|
|
|
|
tableName: DB_TABLE_PREFIX + 'user_entities'
|
|
|
|
});
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const ChannelEntity = sequelize.define('ChannelEntity', {
|
|
|
|
channel: {
|
|
|
|
type: DataTypes.CHAR(18),
|
|
|
|
allowNull: false,
|
2021-03-29 01:49:36 +03:00
|
|
|
unique: 'id-ticket'
|
2021-03-15 23:12:58 +02:00
|
|
|
},
|
|
|
|
ticket: {
|
2021-03-28 20:38:17 +03:00
|
|
|
type: DataTypes.CHAR(18),
|
2021-03-15 23:12:58 +02:00
|
|
|
allowNull: false,
|
2021-03-29 01:49:36 +03:00
|
|
|
unique: 'id-ticket',
|
2021-03-15 23:12:58 +02:00
|
|
|
references: {
|
|
|
|
model: Ticket,
|
|
|
|
key: 'id'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
name: DataTypes.STRING,
|
|
|
|
}, {
|
|
|
|
tableName: DB_TABLE_PREFIX + 'channel_entities'
|
|
|
|
});
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const RoleEntity = sequelize.define('RoleEntity', {
|
|
|
|
role: {
|
|
|
|
type: DataTypes.CHAR(18),
|
|
|
|
allowNull: false,
|
2021-03-29 01:49:36 +03:00
|
|
|
unique: 'id-ticket'
|
2021-03-15 23:12:58 +02:00
|
|
|
},
|
|
|
|
ticket: {
|
2021-03-28 20:38:17 +03:00
|
|
|
type: DataTypes.CHAR(18),
|
2021-03-15 23:12:58 +02:00
|
|
|
allowNull: false,
|
2021-03-29 01:49:36 +03:00
|
|
|
unique: 'id-ticket',
|
2021-03-15 23:12:58 +02:00
|
|
|
references: {
|
|
|
|
model: Ticket,
|
|
|
|
key: 'id'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
name: DataTypes.STRING,
|
|
|
|
colour: DataTypes.INTEGER,
|
|
|
|
}, {
|
|
|
|
tableName: DB_TABLE_PREFIX + 'role_entities'
|
|
|
|
});
|
|
|
|
|
2021-02-16 16:48:04 +02:00
|
|
|
sequelize.sync();
|
|
|
|
|
2021-02-17 15:24:33 +02:00
|
|
|
return sequelize;
|
2021-02-16 16:48:04 +02:00
|
|
|
};
|