From 5b7862fe7b601e61dca51f7e11147726a37e8dac Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sat, 2 Mar 2024 23:51:18 +0100 Subject: [PATCH] extend database by saving guilds --- database.js | 12 +++++++++++- models/guilds.js | 23 +++++++++++++++++++++++ models/messages.js | 9 +++++++++ models/roleEmojiPairs.js | 7 ++++++- models/roles.js | 32 ++++++++++++++++++++++++++++++++ models/voiceChannels.js | 11 ++++++++++- 6 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 models/guilds.js create mode 100644 models/roles.js diff --git a/database.js b/database.js index cce1763..7c43f0b 100644 --- a/database.js +++ b/database.js @@ -1,6 +1,8 @@ import defineRoleEmojiPair from './models/roleEmojiPairs.js'; import defineVoiceChannel from './models/voiceChannels.js'; import defineMessage from './models/messages.js'; +import defineGuild from './models/guilds.js'; +import defineRole from './models/roles.js'; import { Sequelize } from 'sequelize'; import { config } from 'dotenv'; @@ -14,6 +16,14 @@ const sequelize = new Sequelize({ logging: false }); +const Guild = defineGuild(sequelize); +Guild.hasMany(VoiceChannel, { foreignKey: 'guild', onDelete: 'CASCADE' }); +Guild.hasMany(Message, { foreignKey: 'guild', onDelete: 'CASCADE' }); +Guild.hasMany(Role, { foreignKey: 'guild', onDelete: 'CASCADE' }); + +const Role = defineRole(sequelize); +Role.hasMany(RoleEmojiPair, { foreignKey: 'role', onDelete: 'CASCADE' }); + const RoleEmojiPair = defineRoleEmojiPair(sequelize); const VoiceChannel = defineVoiceChannel(sequelize); @@ -22,4 +32,4 @@ const Message = defineMessage(sequelize); Message.hasMany(RoleEmojiPair, { foreignKey: 'message', onDelete: 'CASCADE' }); sequelize.sync(); -export { sequelize, RoleEmojiPair, VoiceChannel, Message }; +export { sequelize, Guild, Role, RoleEmojiPair, VoiceChannel, Message }; diff --git a/models/guilds.js b/models/guilds.js new file mode 100644 index 0000000..8460b8f --- /dev/null +++ b/models/guilds.js @@ -0,0 +1,23 @@ +import { DataTypes, Sequelize } from 'sequelize'; + +/** + * @typedef {Object} Guild + * @property {string} id A Discord guild ID. + * @property {(model: Object) => void} hasMany Defines an One-To-Many relationship. + * @property {(conditions: Object) => Promise} findOne Finds one instance in the database matching the provided condition(-s). + * @property {(conditions: Object) => Promise>} findAll Finds all instances in the database matching the provided condition(-s). + */ + +/** + * The definition of the `Guild` table in the database. + * @param {Sequelize} sequelize + * @returns {Guild} + */ +export default function (sequelize) { + return sequelize.define('Guilds', { + id: { + type: DataTypes.STRING, + primaryKey: true + } + }); +} diff --git a/models/messages.js b/models/messages.js index ee23e27..c9175ad 100644 --- a/models/messages.js +++ b/models/messages.js @@ -3,6 +3,7 @@ import { DataTypes, Sequelize } from 'sequelize'; /** * @typedef {Object} Message * @property {string} id A Discord message ID. + * @property {string} guild A Discord guild ID as a foreign key reference. * @property {(model: Object) => void} hasMany Defines an One-To-Many relationship. * @property {(conditions: Object) => Promise} findOne Finds one instance in the database matching the provided condition(-s). * @property {(conditions: Object) => Promise>} findAll Finds all instances in the database matching the provided condition(-s). @@ -18,6 +19,14 @@ export default function (sequelize) { id: { type: DataTypes.STRING, primaryKey: true + }, + guild: { + type: DataTypes.STRING, + references: { + deferrable: Deferrable.INITIALLY_IMMEDIATE, + model: 'Guilds', + key: 'id' + } } }); } diff --git a/models/roleEmojiPairs.js b/models/roleEmojiPairs.js index be5b04f..11a5946 100644 --- a/models/roleEmojiPairs.js +++ b/models/roleEmojiPairs.js @@ -32,7 +32,12 @@ export default function (sequelize) { } }, role: { - type: DataTypes.STRING + type: DataTypes.STRING, + references: { + deferrable: Deferrable.INITIALLY_IMMEDIATE, + model: 'Roles', + key: 'id' + } }, emoji: { type: DataTypes.STRING diff --git a/models/roles.js b/models/roles.js new file mode 100644 index 0000000..315d1bb --- /dev/null +++ b/models/roles.js @@ -0,0 +1,32 @@ +import { DataTypes, Sequelize } from 'sequelize'; + +/** + * @typedef {Object} Role + * @property {string} id A Discord role ID. + * @property {string} guild A Discord guild ID as a foreign key reference. + * @property {(model: Object) => void} hasMany Defines an One-To-Many relationship. + * @property {(conditions: Object) => Promise} findOne Finds one instance in the database matching the provided condition(-s). + * @property {(conditions: Object) => Promise>} findAll Finds all instances in the database matching the provided condition(-s). + */ + +/** + * The definition of the `Role` table in the database. + * @param {Sequelize} sequelize + * @returns {Role} + */ +export default function (sequelize) { + return sequelize.define('Roles', { + id: { + type: DataTypes.STRING, + primaryKey: true + }, + guild: { + type: DataTypes.STRING, + references: { + deferrable: Deferrable.INITIALLY_IMMEDIATE, + model: 'Guilds', + key: 'id' + } + } + }); +} diff --git a/models/voiceChannels.js b/models/voiceChannels.js index 4bf9f20..f18edfe 100644 --- a/models/voiceChannels.js +++ b/models/voiceChannels.js @@ -5,6 +5,7 @@ import { DataTypes, Sequelize } from 'sequelize'; * @property {string} id A Discord channel ID. * @property {boolean} create Whether or not this channel is registered to create customs when joined. * @property {(string|null)} owner The owner of this channel, if not registered for customs. + * @property {string} guild A Discord guild ID as a foreign key reference. * @property {(model: Object) => void} hasMany Defines an One-To-Many relationship. * @property {(conditions: Object) => Promise} findOne Finds one instance in the database matching the provided condition(-s). * @property {(conditions: Object) => Promise>} findAll Finds all instances in the database matching the provided condition(-s). @@ -16,7 +17,7 @@ import { DataTypes, Sequelize } from 'sequelize'; * @returns {VoiceChannel} */ export default function (sequelize) { - return sequelize.define('VoiceChannel', { + return sequelize.define('VoiceChannels', { id: { type: DataTypes.STRING, primaryKey: true @@ -28,6 +29,14 @@ export default function (sequelize) { owner: { type: DataTypes.STRING, allowNull: true + }, + guild: { + type: DataTypes.STRING, + references: { + deferrable: Deferrable.INITIALLY_IMMEDIATE, + model: 'Guilds', + key: 'id' + } } }); }