Skip to content

Commit

Permalink
Make payload a text
Browse files Browse the repository at this point in the history
  • Loading branch information
holzmaster committed Mar 26, 2024
1 parent d275b71 commit c58cc92
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/commands/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export const run: CommandFunction = async (_client, message, args, context) => {
await additionalMessageData.upsertForMessage(
pollMessage,
"DELAYED_POLL",
delayedPollData,
JSON.stringify(delayedPollData),
);
delayedPolls.push(delayedPollData);
}
Expand Down
15 changes: 5 additions & 10 deletions src/storage/additionalMessageData.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { Message, Snowflake } from "discord.js";
import { sql } from "kysely";

import type { AdditionalMessageData, DataUsage } from "./model.js";
import type { JsonObject } from "../types.js";
import db from "./db.js";

export async function getForMessage(
Expand All @@ -15,22 +13,19 @@ export async function getForMessage(
"Cannot associate data with message outside of a guild",
);
}
const res = await ctx

return await ctx
.selectFrom("additionalMessageData")
.where("messageId", "=", message.id)
.where("usage", "=", usage)
.selectAll()
.executeTakeFirst();

return res === undefined
? undefined
: { ...res, customData: JSON.parse(res.payload) as JsonObject };
}

export async function upsertForMessage(
message: Message,
usage: DataUsage,
payload: JsonObject,
payload: string,
ctx = db(),
) {
if (!message.guild) {
Expand All @@ -46,11 +41,11 @@ export async function upsertForMessage(
channelId: message.channelId,
messageId: message.id,
usage,
payload: JSON.stringify(payload),
payload,
})
.onConflict(oc =>
oc.columns(["guildId", "channelId", "messageId"]).doUpdateSet({
payload: JSON.stringify(payload),
payload,
}),
)
.execute();
Expand Down
2 changes: 1 addition & 1 deletion src/storage/migrations/01-message-data-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn("channelId", "text", c => c.notNull())
.addColumn("messageId", "text", c => c.notNull())
.addColumn("usage", "integer", c => c.notNull())
.addColumn("payload", "json", c => c.notNull())
.addColumn("payload", "text", c => c.notNull())
.addColumn("createdAt", "timestamp", c =>
c.notNull().defaultTo(sql`CURRENT_TIMESTAMP`),
)
Expand Down
5 changes: 3 additions & 2 deletions src/storage/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,9 @@ export interface AdditionalMessageDataTable {
channelId: Snowflake;
messageId: Snowflake;
usage: DataUsage;
// TODO: JSON types are currently not supported by the DB driver
payload: ColumnType<string, string, string>; // JsonObject;

/** Just a string, so the specific use-case can decide on how to save the data. */
payload: string;

createdAt: ColumnType<string, never, never>;
updatedAt: ColumnType<string, never, never>;
Expand Down

0 comments on commit c58cc92

Please sign in to comment.