extend database by saving guilds

This commit is contained in:
Baipyrus 2024-03-02 23:51:18 +01:00
parent 7983eb60f9
commit 5b7862fe7b
6 changed files with 91 additions and 3 deletions

View File

@ -1,6 +1,8 @@
import defineRoleEmojiPair from './models/roleEmojiPairs.js'; import defineRoleEmojiPair from './models/roleEmojiPairs.js';
import defineVoiceChannel from './models/voiceChannels.js'; import defineVoiceChannel from './models/voiceChannels.js';
import defineMessage from './models/messages.js'; import defineMessage from './models/messages.js';
import defineGuild from './models/guilds.js';
import defineRole from './models/roles.js';
import { Sequelize } from 'sequelize'; import { Sequelize } from 'sequelize';
import { config } from 'dotenv'; import { config } from 'dotenv';
@ -14,6 +16,14 @@ const sequelize = new Sequelize({
logging: false 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 RoleEmojiPair = defineRoleEmojiPair(sequelize);
const VoiceChannel = defineVoiceChannel(sequelize); const VoiceChannel = defineVoiceChannel(sequelize);
@ -22,4 +32,4 @@ const Message = defineMessage(sequelize);
Message.hasMany(RoleEmojiPair, { foreignKey: 'message', onDelete: 'CASCADE' }); Message.hasMany(RoleEmojiPair, { foreignKey: 'message', onDelete: 'CASCADE' });
sequelize.sync(); sequelize.sync();
export { sequelize, RoleEmojiPair, VoiceChannel, Message }; export { sequelize, Guild, Role, RoleEmojiPair, VoiceChannel, Message };

23
models/guilds.js Normal file
View File

@ -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<Guild>} findOne Finds one instance in the database matching the provided condition(-s).
* @property {(conditions: Object) => Promise<Array<Guild>>} 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
}
});
}

View File

@ -3,6 +3,7 @@ import { DataTypes, Sequelize } from 'sequelize';
/** /**
* @typedef {Object} Message * @typedef {Object} Message
* @property {string} id A Discord message ID. * @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 {(model: Object) => void} hasMany Defines an One-To-Many relationship.
* @property {(conditions: Object) => Promise<Message>} findOne Finds one instance in the database matching the provided condition(-s). * @property {(conditions: Object) => Promise<Message>} findOne Finds one instance in the database matching the provided condition(-s).
* @property {(conditions: Object) => Promise<Array<Message>>} findAll Finds all instances in the database matching the provided condition(-s). * @property {(conditions: Object) => Promise<Array<Message>>} findAll Finds all instances in the database matching the provided condition(-s).
@ -18,6 +19,14 @@ export default function (sequelize) {
id: { id: {
type: DataTypes.STRING, type: DataTypes.STRING,
primaryKey: true primaryKey: true
},
guild: {
type: DataTypes.STRING,
references: {
deferrable: Deferrable.INITIALLY_IMMEDIATE,
model: 'Guilds',
key: 'id'
}
} }
}); });
} }

View File

@ -32,7 +32,12 @@ export default function (sequelize) {
} }
}, },
role: { role: {
type: DataTypes.STRING type: DataTypes.STRING,
references: {
deferrable: Deferrable.INITIALLY_IMMEDIATE,
model: 'Roles',
key: 'id'
}
}, },
emoji: { emoji: {
type: DataTypes.STRING type: DataTypes.STRING

32
models/roles.js Normal file
View File

@ -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<Role>} findOne Finds one instance in the database matching the provided condition(-s).
* @property {(conditions: Object) => Promise<Array<Role>>} 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'
}
}
});
}

View File

@ -5,6 +5,7 @@ import { DataTypes, Sequelize } from 'sequelize';
* @property {string} id A Discord channel ID. * @property {string} id A Discord channel ID.
* @property {boolean} create Whether or not this channel is registered to create customs when joined. * @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|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 {(model: Object) => void} hasMany Defines an One-To-Many relationship.
* @property {(conditions: Object) => Promise<VoiceChannel>} findOne Finds one instance in the database matching the provided condition(-s). * @property {(conditions: Object) => Promise<VoiceChannel>} findOne Finds one instance in the database matching the provided condition(-s).
* @property {(conditions: Object) => Promise<Array<VoiceChannel>>} findAll Finds all instances in the database matching the provided condition(-s). * @property {(conditions: Object) => Promise<Array<VoiceChannel>>} findAll Finds all instances in the database matching the provided condition(-s).
@ -16,7 +17,7 @@ import { DataTypes, Sequelize } from 'sequelize';
* @returns {VoiceChannel} * @returns {VoiceChannel}
*/ */
export default function (sequelize) { export default function (sequelize) {
return sequelize.define('VoiceChannel', { return sequelize.define('VoiceChannels', {
id: { id: {
type: DataTypes.STRING, type: DataTypes.STRING,
primaryKey: true primaryKey: true
@ -28,6 +29,14 @@ export default function (sequelize) {
owner: { owner: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: true allowNull: true
},
guild: {
type: DataTypes.STRING,
references: {
deferrable: Deferrable.INITIALLY_IMMEDIATE,
model: 'Guilds',
key: 'id'
}
} }
}); });
} }