diff --git a/database.js b/database.js index 3480ea5..77b1dc3 100644 --- a/database.js +++ b/database.js @@ -1,5 +1,6 @@ import defineRoleEmojiPair from './models/roleEmojiPairs.js'; import defineVoiceChannel from './models/voiceChannels.js'; +import defineResponse from './models/responses.js'; import defineMessage from './models/messages.js'; import defineKeyword from './models/keywords.js'; import defineGuild from './models/guilds.js'; @@ -17,7 +18,10 @@ const sequelize = new Sequelize({ logging: false }); +const Response = defineResponse(sequelize); + const Keyword = defineKeyword(sequelize); +Keyword.hasMany(Response, { foreignKey: 'keyword', onDelete: 'CASCADE' }); const RoleEmojiPair = defineRoleEmojiPair(sequelize); diff --git a/models/responses.js b/models/responses.js new file mode 100644 index 0000000..8c255e4 --- /dev/null +++ b/models/responses.js @@ -0,0 +1,37 @@ +import { DataTypes, Deferrable, Sequelize } from 'sequelize'; + +/** + * @typedef {Object} Response + * @property {string} id A universally unique id, generated by sequelize. + * @property {string} keyword A universally unique id referencing a `Keyword`. + * @property {string} name The name of the response. + * @property {string} response The response data itself. + */ + +/** + * The definition of the `Response` table in the database. + * @param {Sequelize} sequelize + */ +export default function (sequelize) { + return sequelize.define('Responses', { + id: { + defaultValue: DataTypes.UUIDV4, + type: DataTypes.UUID, + primaryKey: true + }, + keyword: { + type: DataTypes.UUID, + references: { + deferrable: Deferrable.INITIALLY_IMMEDIATE, + model: 'Keywords', + key: 'id' + } + }, + name: { + type: DataTypes.STRING + }, + response: { + type: DataTypes.STRING + } + }); +}