feat(database): Improve database definition to add sizes

This commit is contained in:
Michel Fedde 2025-06-22 16:43:30 +02:00
parent ec0aa5654c
commit a3c1bae6db
6 changed files with 38 additions and 30 deletions

View file

@ -5,6 +5,7 @@ export type DatabaseColumnDefinition = {
autoIncrement?: boolean;
notNull?: boolean;
options?: string;
size?: number;
}
export type DatabaseDefinition = {

View file

@ -48,33 +48,21 @@ export class DatabaseUpdater {
const columnsSQL = missingColumns.map((column: DatabaseColumnDefinition) => {
const values = [
"ADD",
column.name,
column.type,
column.primaryKey ? `PRIMARY KEY` : '',
column.notNull ? 'NOT NULL' : '',
column.autoIncrement ? 'AUTOINCREMENT' : '',
this.getSQLColumnDefinition(column)
]
return values.join(' ');
}).join(', ');
const sql = `ALTER TABLE ${definition.name} ${columnsSQL}`;
this.database.execute(sql);
console.log(sql);
}
private createTable(definition: DatabaseDefinition) {
const columnsSQL = definition.columns.map((column: DatabaseColumnDefinition) => {
const values = [
column.name,
column.type,
column.primaryKey ? `PRIMARY KEY` : '',
column.notNull ? 'NOT NULL' : '',
column.autoIncrement ? 'AUTOINCREMENT' : '',
]
return values.join(' ');
}).join(', ');
const columnsSQL = definition.columns.map((column: DatabaseColumnDefinition) =>
this.getSQLColumnDefinition(column)
).join(', ');
const sql = `CREATE TABLE IF NOT EXISTS ${definition.name}
(
@ -82,4 +70,16 @@ export class DatabaseUpdater {
)`;
this.database.execute(sql);
}
private getSQLColumnDefinition(column: DatabaseColumnDefinition) {
const values = [
column.name,
`${column.type}${column.size ? `(${column.size})` : ''}`,
column.primaryKey ? `PRIMARY KEY` : '',
column.notNull ? 'NOT NULL' : '',
column.autoIncrement ? 'AUTOINCREMENT' : '',
];
return values.join(' ')
}
}

View file

@ -18,15 +18,17 @@ const dbDefinition: DatabaseDefinition = {
},
{
name: "groupid",
type: "VARCHAR(32)",
type: "INTEGER",
},
{
name: "key",
type: "VARCHAR(32)",
type: "VARCHAR",
size: 32
},
{
name: "value",
type: "VARCHAR(128)",
type: "VARCHAR",
size: 2 ^ 11
}
]
}

View file

@ -19,19 +19,23 @@ const dbDefinition: DatabaseDefinition = {
},
{
name: "server",
type: "VARCHAR(32)"
type: "VARCHAR",
size: 32
},
{
name: "name",
type: "VARCHAR(32)",
type: "VARCHAR",
size: 32
},
{
name: "leader",
type: "VARCHAR(32)",
type: "VARCHAR",
size: 32
},
{
name: "role",
type: "VARCHAR(32)",
type: "VARCHAR",
size: 32
}
]
}

View file

@ -128,10 +128,9 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
);
}
const validName = this.validateGroupName(name);
// @ts-expect-error Is correct, since the valid name can return either true or a string and the error should only be thrown if it's a string.
if (name !== true) {
throw new UserError(`Your group name contains one or more invalid character sequences: ${validName}`)
const invalidName = this.validateGroupName(name);
if (invalidName) {
throw new UserError(`Your group name contains one or more invalid character sequences: ${invalidName}`)
}
const group: GroupModel = {
@ -152,7 +151,7 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
interaction.reply({content: `:white_check_mark: Created group \`${name}\``, flags: MessageFlags.Ephemeral})
}
private validateGroupName(name: string): true | string {
private validateGroupName(name: string): string | null {
const lowercaseName = name.toLowerCase();
for (const invalidcharactersequence of GroupCommand.INVALID_CHARACTER_SEQUENCES) {
if (!lowercaseName.includes(invalidcharactersequence)) {
@ -161,7 +160,7 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
return invalidcharactersequence
}
return true;
return null;
}
private list(interaction: ChatInputCommandInteraction) {

View file

@ -209,10 +209,12 @@ export class ConfigurationMenuHandler {
private getStringBuilder(context: FieldMenuItemContext): TextInputBuilder {
return new TextInputBuilder()
.setStyle(TextInputStyle.Short)
.setMaxLength(100)
}
private getTextareaBuilder(context: FieldMenuItemContext): TextInputBuilder {
return new TextInputBuilder()
.setStyle(TextInputStyle.Paragraph)
.setMaxLength(2048)
}