mirror of
https://github.com/Hessenuk/DiscordTickets.git
synced 2025-09-02 00:31:27 +03:00
Database
This commit is contained in:
42
src/database/dialects.js
Normal file
42
src/database/dialects.js
Normal file
@@ -0,0 +1,42 @@
|
||||
module.exports = {
|
||||
sqlite: {
|
||||
name: 'SQLite',
|
||||
dialect: 'sqlite',
|
||||
packages: ['sqlite3'],
|
||||
},
|
||||
mysql: {
|
||||
name: 'MySQL',
|
||||
dialect: 'mysql',
|
||||
packages: ['mysql2']
|
||||
},
|
||||
maria: {
|
||||
name: 'MariaDB',
|
||||
dialect: 'mariadb',
|
||||
package: ['mariadb']
|
||||
},
|
||||
mariadb: {
|
||||
name: 'MariaDB',
|
||||
dialect: 'mariadb',
|
||||
package: ['mariadb']
|
||||
},
|
||||
postgre: {
|
||||
name: 'PostgreSQL',
|
||||
dialect: 'postgres',
|
||||
packages: ['pg', 'pg-hstore']
|
||||
},
|
||||
postgres: {
|
||||
name: 'PostgreSQL',
|
||||
dialect: 'postgres',
|
||||
packages: ['pg', 'pg-hstore']
|
||||
},
|
||||
postgresql: {
|
||||
name: 'PostgreSQL',
|
||||
dialect: 'postgres',
|
||||
packages: ['pg', 'pg-hstore']
|
||||
},
|
||||
microsoft: {
|
||||
name: 'Microsoft SQL',
|
||||
dialect: 'mssql',
|
||||
packages: ['tedious']
|
||||
},
|
||||
};
|
126
src/database/index.js
Normal file
126
src/database/index.js
Normal file
@@ -0,0 +1,126 @@
|
||||
const {
|
||||
Sequelize,
|
||||
DataTypes
|
||||
} = require('sequelize');
|
||||
const { path } = require('../utils/fs');
|
||||
const config = require('../../user/config');
|
||||
const types = require('./dialects');
|
||||
const supported = Object.keys(types);
|
||||
|
||||
module.exports = async (log) => {
|
||||
|
||||
const {
|
||||
DB_TYPE,
|
||||
DB_HOST,
|
||||
DB_USER,
|
||||
DB_PASS,
|
||||
DB_NAME
|
||||
} = process.env;
|
||||
|
||||
let type = (DB_TYPE || 'sqlite').toLowerCase();
|
||||
|
||||
if (!supported.includes(type)) {
|
||||
log.report('Invalid database type');
|
||||
throw new Error(`DB_TYPE (${type}) is not a valid type`);
|
||||
}
|
||||
|
||||
try {
|
||||
types[type].packages.forEach(pkg => require(pkg));
|
||||
} catch {
|
||||
log.report('Specified database type is not installed');
|
||||
let required = types[type].packages.map(i => `"${i}"`).join(' and ');
|
||||
throw new Error(`Please install the package(s) for your selected database type: ${required}`);
|
||||
}
|
||||
|
||||
let sequelize;
|
||||
|
||||
if (type === 'sqlite') {
|
||||
log.info('Using SQLite storage');
|
||||
sequelize = new Sequelize({
|
||||
dialect: types[type].dialect,
|
||||
storage: path('./user/database.sqlite'),
|
||||
logging: log.debug
|
||||
});
|
||||
} 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,
|
||||
logging: log.debug
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
log.success('Connected to database successfully');
|
||||
} catch (error) {
|
||||
log.warn('Unable to connect to database');
|
||||
log.error(error);
|
||||
}
|
||||
|
||||
/* let models = {};
|
||||
let files = fs.readdirSync(path('src/database/models/')).filter(file => file.endsWith('.js'));
|
||||
|
||||
for (let file of files) {
|
||||
let table = require(`./models/${file}`);
|
||||
let model = sequelize.define(table.name, table.model);
|
||||
models[table.name] = model;
|
||||
} */
|
||||
|
||||
const Guild = sequelize.define('Guild', {
|
||||
id: {
|
||||
type: DataTypes.CHAR(18),
|
||||
primaryKey: true
|
||||
},
|
||||
prefix: {
|
||||
type: DataTypes.STRING, // STRING(255) = VARCHAR(255)
|
||||
defaultValue: config.defaults.prefix
|
||||
},
|
||||
locale: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: config.defaults.locale
|
||||
}
|
||||
}, {
|
||||
tableName: 'guilds'
|
||||
});
|
||||
|
||||
|
||||
const Ticket = sequelize.define('Ticket', {
|
||||
id: {
|
||||
type: DataTypes.CHAR(18),
|
||||
primaryKey: true
|
||||
},
|
||||
guild: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: Guild,
|
||||
key: 'id'
|
||||
},
|
||||
}
|
||||
}, {
|
||||
tableName: 'tickets'
|
||||
});
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const Message = sequelize.define('Message', {
|
||||
id: {
|
||||
type: DataTypes.CHAR(18),
|
||||
primaryKey: true
|
||||
},
|
||||
ticket: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: Ticket,
|
||||
key: 'id'
|
||||
},
|
||||
}
|
||||
}, {
|
||||
tableName: 'messages'
|
||||
});
|
||||
|
||||
sequelize.sync();
|
||||
|
||||
return sequelize.models;
|
||||
};
|
13
src/index.js
13
src/index.js
@@ -28,9 +28,11 @@ const { version } = require('../package.json');
|
||||
const fs = require('fs');
|
||||
const { path } = require('./utils/fs');
|
||||
if (!fs.existsSync(path('./.env'))) return console.log('Please make a copy of \'example.env\' called \'.env\'');
|
||||
if (!fs.existsSync(path('./user/config.js'))) return console.log('Please make a copy of \'example.config.js\' called \'config.js\'');
|
||||
if (!fs.existsSync(path('./user/config.js'))) return console.log('Please make a copy of \'user/example.config.js\' called \'user/config.js\'');
|
||||
|
||||
require('dotenv').config();
|
||||
require('dotenv').config({
|
||||
path: path('./.env')
|
||||
});
|
||||
|
||||
const config = require('../user/config');
|
||||
|
||||
@@ -43,7 +45,6 @@ const log = new Logger({
|
||||
});
|
||||
|
||||
|
||||
const terminalLink = require('terminal-link');
|
||||
log.report = error => {
|
||||
let report = [
|
||||
'<< Issue report >>',
|
||||
@@ -57,6 +58,9 @@ log.report = error => {
|
||||
if (error) log.error(error);
|
||||
};
|
||||
|
||||
const terminalLink = require('terminal-link');
|
||||
const I18n = require('@eartharoid/i18n');
|
||||
|
||||
const { Client } = require('discord.js');
|
||||
class Bot extends Client {
|
||||
constructor() {
|
||||
@@ -66,8 +70,9 @@ class Bot extends Client {
|
||||
|
||||
Object.assign(this, {
|
||||
config,
|
||||
db: require('./modules/database')(log),
|
||||
db: require('./database')(log),
|
||||
log,
|
||||
i18n: new I18n(path('./src/locales'), 'en-GB')
|
||||
});
|
||||
|
||||
this.log.info('Connecting to Discord API');
|
||||
|
1
src/locales/en-GB.json
Normal file
1
src/locales/en-GB.json
Normal file
@@ -0,0 +1 @@
|
||||
{}
|
@@ -1,75 +0,0 @@
|
||||
const Keyv = require('keyv');
|
||||
|
||||
const { homepage } = require('../../package.json');
|
||||
const { path } = require('../utils/fs');
|
||||
|
||||
const types = {
|
||||
sqlite: 'sqlite',
|
||||
mysql: 'mysql',
|
||||
mongo: 'mongo',
|
||||
mongodb: 'mongo',
|
||||
postgre: 'postgres',
|
||||
postgres: 'postgres',
|
||||
postgresql: 'postgres',
|
||||
};
|
||||
|
||||
const supported = Object.keys(types);
|
||||
|
||||
module.exports = (log) => {
|
||||
|
||||
let type = (process.env.DB_TYPE || 'sqlite').toLowerCase();
|
||||
|
||||
if (!supported.includes(type)) {
|
||||
log.report('Invalid database type');
|
||||
throw new Error(`DB_TYPE (${type}) is not a valid type`);
|
||||
}
|
||||
|
||||
try {
|
||||
require(`@keyv/${types[type]}`);
|
||||
} catch {
|
||||
log.report('Specified database type is not installed');
|
||||
throw new Error(`"@keyv/${types[type]}" is not installed, please install is manually`);
|
||||
}
|
||||
|
||||
const {
|
||||
DB_HOST,
|
||||
DB_USER,
|
||||
DB_PASS,
|
||||
DB_NAME
|
||||
} = process.env;
|
||||
let database;
|
||||
|
||||
switch (type) {
|
||||
case 'mysql':
|
||||
database = `mysql://${DB_USER}:${DB_PASS}@${DB_HOST}/${DB_NAME}`;
|
||||
log.info('Using MySQL as database backend');
|
||||
break;
|
||||
case 'mongo':
|
||||
case 'mongodb':
|
||||
database = `mongodb://${DB_USER}:${DB_PASS}@${DB_HOST}/${DB_NAME}`;
|
||||
log.info('Using MongoDB as database backend');
|
||||
break;
|
||||
case 'postgre':
|
||||
case 'postgres':
|
||||
case 'postgresql':
|
||||
database = `postgresql://${DB_USER}:${DB_PASS}@${DB_HOST}/${DB_NAME}`;
|
||||
log.info('Using Postgres as database backend');
|
||||
break;
|
||||
default: // sqlite
|
||||
database = `sqlite://${path('./user/database.sqlite')}`;
|
||||
log.info('Using SQLite as database backend');
|
||||
break;
|
||||
}
|
||||
|
||||
return {
|
||||
servers: new Keyv(database, {
|
||||
namespace: 'servers'
|
||||
}),
|
||||
tickets: new Keyv(database, {
|
||||
namespace: 'tickets'
|
||||
}),
|
||||
messages: new Keyv(database, {
|
||||
namespace: 'messages'
|
||||
})
|
||||
};
|
||||
};
|
Reference in New Issue
Block a user