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; autoIncrement?: boolean;
notNull?: boolean; notNull?: boolean;
options?: string; options?: string;
size?: number;
} }
export type DatabaseDefinition = { export type DatabaseDefinition = {

View file

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

View file

@ -19,19 +19,23 @@ const dbDefinition: DatabaseDefinition = {
}, },
{ {
name: "server", name: "server",
type: "VARCHAR(32)" type: "VARCHAR",
size: 32
}, },
{ {
name: "name", name: "name",
type: "VARCHAR(32)", type: "VARCHAR",
size: 32
}, },
{ {
name: "leader", name: "leader",
type: "VARCHAR(32)", type: "VARCHAR",
size: 32
}, },
{ {
name: "role", 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); const invalidName = 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 (invalidName) {
if (name !== true) { throw new UserError(`Your group name contains one or more invalid character sequences: ${invalidName}`)
throw new UserError(`Your group name contains one or more invalid character sequences: ${validName}`)
} }
const group: GroupModel = { 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}) 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(); const lowercaseName = name.toLowerCase();
for (const invalidcharactersequence of GroupCommand.INVALID_CHARACTER_SEQUENCES) { for (const invalidcharactersequence of GroupCommand.INVALID_CHARACTER_SEQUENCES) {
if (!lowercaseName.includes(invalidcharactersequence)) { if (!lowercaseName.includes(invalidcharactersequence)) {
@ -161,7 +160,7 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
return invalidcharactersequence return invalidcharactersequence
} }
return true; return null;
} }
private list(interaction: ChatInputCommandInteraction) { private list(interaction: ChatInputCommandInteraction) {

View file

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