Skip to content

Commit

Permalink
Test migrate stempeln to kysely
Browse files Browse the repository at this point in the history
  • Loading branch information
holzmaster committed Mar 25, 2024
1 parent b8d4230 commit f5d9efa
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/commands/stempelkarte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
SlashCommandUserOption,
} from "discord.js";

import Stempel from "../storage/model/Stempel.js";
import * as stempel from "../storage/stempel.js";
import log from "../utils/logger.js";
import type { ApplicationCommand, CommandResult } from "./command.js";
import { chunkArray } from "../utils/arrayUtils.js";
Expand Down Expand Up @@ -140,7 +140,7 @@ export class StempelkarteCommand implements ApplicationCommand {
const getUserById = (id: Snowflake) =>
command.guild?.members.cache.find(member => member.id === id);

const allInvitees = await Stempel.getStempelByInvitator(ofMember.id);
const allInvitees = await stempel.getStempelByInvitator(ofMember);

if (allInvitees.length === 0) {
await command.reply({
Expand Down
12 changes: 2 additions & 10 deletions src/commands/stempeln.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import {
type CommandInteraction,
type Client,
type GuildMember,
SlashCommandBuilder,
SlashCommandUserOption,
} from "discord.js";

import type { ApplicationCommand, CommandResult } from "./command.js";
import Stempel from "../storage/model/Stempel.js";

const stempelUser = async (
invitator: GuildMember,
invitedMember: GuildMember,
): Promise<boolean> => {
return Stempel.insertStempel(invitator.id, invitedMember.id);
};
import * as stempel from "../storage/stempel.js";

const replies = [
"Der Bruder {0} hat den neuen Bruder {1} eingeladen und du hast dies so eben bestätigt!",
Expand Down Expand Up @@ -67,7 +59,7 @@ export class StempelCommand implements ApplicationCommand {
return;
}

const isNewInvite = await stempelUser(invitator, invitedUser);
const isNewInvite = await stempel.insertStempel(invitator, invitedUser);
if (isNewInvite) {
const reply = replies[Math.floor(Math.random() * replies.length)]
.replace("{0}", invitator.toString())
Expand Down
17 changes: 17 additions & 0 deletions src/storage/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { OneBasedMonth } from "./birthday.js";

export interface Database {
birthdays: BirthdayTable;
stempels: StempelTable;
}

export type Uuid = string;
Expand All @@ -25,3 +26,19 @@ export interface BirthdayTable {
createdAt: ColumnType<string, string, never>;
updatedAt: ColumnType<string, string, never>;
}

export type Stempel = Selectable<StempelTable>;

export interface StempelTable {
// Cannot use GeneratedAlways because sequelize generated the ID on the client side
// id: GeneratedAlways<Uuid>;
id: ColumnType<Uuid, Uuid, never>;

invitator: Snowflake;
invitedMember: Snowflake;

// TODO: These don't seem to be taken care of by the database, so we need to insert them manually
// Also, Date is not supported by the DB driver
createdAt: ColumnType<string, string, never>;
updatedAt: ColumnType<string, string, never>;
}
35 changes: 0 additions & 35 deletions src/storage/model/Stempel.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { type Sequelize, Model, DataTypes, type Optional } from "sequelize";
import type { Snowflake } from "discord.js";

import log from "../../utils/logger.js";

export interface StempelAttributes {
id: string;
invitator: Snowflake;
Expand All @@ -19,39 +17,6 @@ export default class Stempel
declare invitator: Snowflake;
declare invitedMember: Snowflake;

/**
* @returns true/false depending if the invitedMember is already in the database
*/
static async insertStempel(
invitator: Snowflake,
invitedMember: Snowflake,
): Promise<boolean> {
log.debug(
`Inserting Stempel into Database with ${invitator} invitator and ${invitedMember} as invited member`,
);
try {
await Stempel.create({
invitator,
invitedMember,
});
return true;
} catch {
log.debug("Stempel does already exist");
return false;
}
}

/**
* @param {Snowflake} invitator Snowflake ID of the inviter.
*/
static getStempelByInvitator(invitator: Snowflake) {
return Stempel.findAll({
where: {
invitator,
},
});
}

static initialize(sequelize: Sequelize) {
Stempel.init(
{
Expand Down
40 changes: 40 additions & 0 deletions src/storage/stempel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { GuildMember, Snowflake } from "discord.js";

import type { Stempel } from "./model.js";
import db from "./kysely.js";

/**
* @returns true/false depending if the invitedMember is already in the database
*/
export async function insertStempel(
invitator: GuildMember,
invitedMember: GuildMember,
ctx = db(),
): Promise<boolean> {
const now = new Date().toISOString();

const res = await ctx
.insertInto("stempels")
.values({
id: crypto.randomUUID(),
invitator: invitator.id,
invitedMember: invitedMember.id,
createdAt: now,
updatedAt: now,
})
.returning("id")
.executeTakeFirst();

return typeof res?.id === "string";
}

export function getStempelByInvitator(
invitator: GuildMember,
ctx = db(),
): Promise<Stempel[]> {
return ctx
.selectFrom("stempels")
.where("invitator", "=", invitator.id)
.selectAll()
.execute();
}

0 comments on commit f5d9efa

Please sign in to comment.