test: verify atomic share creation
This commit is contained in:
+42
-56
@@ -2,63 +2,49 @@ import "server-only";
|
||||
|
||||
import { db } from "@/db";
|
||||
import { cdn, shareTexts } from "@/db/schema";
|
||||
import type { ShareSource } from "@/db/schema/share";
|
||||
import type { ValidatedImage } from "@/lib/share/validation";
|
||||
import {
|
||||
createShareAtomically,
|
||||
type CreateShareInput,
|
||||
} from "@/lib/share/create-core";
|
||||
|
||||
export type ShareAuthor = {
|
||||
userId: string;
|
||||
discordId: string;
|
||||
displayName: string;
|
||||
avatarUrl: string | null;
|
||||
};
|
||||
export type { ShareAuthor } from "@/lib/share/create-core";
|
||||
|
||||
export async function createShare({
|
||||
content,
|
||||
description,
|
||||
image,
|
||||
source,
|
||||
author,
|
||||
}: {
|
||||
content: string;
|
||||
description: string | null;
|
||||
image: ValidatedImage | null;
|
||||
source: ShareSource;
|
||||
author: ShareAuthor;
|
||||
}) {
|
||||
return db.transaction(async (tx) => {
|
||||
let imageCdnId: string | null = null;
|
||||
if (image) {
|
||||
const [record] = await tx
|
||||
.insert(cdn)
|
||||
.values({
|
||||
data: image.bytes,
|
||||
name: image.name,
|
||||
type: image.type,
|
||||
size: image.size,
|
||||
ownerDiscordId: author.discordId,
|
||||
})
|
||||
.returning({ id: cdn.id });
|
||||
imageCdnId = record.id;
|
||||
}
|
||||
|
||||
const [share] = await tx
|
||||
.insert(shareTexts)
|
||||
.values({
|
||||
content,
|
||||
description,
|
||||
imageCdnId,
|
||||
source,
|
||||
authorId: author.userId,
|
||||
authorDiscordId: author.discordId,
|
||||
authorName: author.displayName,
|
||||
authorAvatarUrl: author.avatarUrl,
|
||||
})
|
||||
.returning({
|
||||
id: shareTexts.id,
|
||||
imageCdnId: shareTexts.imageCdnId,
|
||||
createdAt: shareTexts.createdAt,
|
||||
});
|
||||
|
||||
return share;
|
||||
export async function createShare(input: CreateShareInput) {
|
||||
return createShareAtomically(input, {
|
||||
transaction: (callback) => db.transaction(async (tx) => callback({
|
||||
async insertImage(image, ownerDiscordId) {
|
||||
const [record] = await tx
|
||||
.insert(cdn)
|
||||
.values({
|
||||
data: image.bytes,
|
||||
name: image.name,
|
||||
type: image.type,
|
||||
size: image.size,
|
||||
ownerDiscordId,
|
||||
})
|
||||
.returning({ id: cdn.id });
|
||||
return record.id;
|
||||
},
|
||||
async insertShare(share, imageCdnId) {
|
||||
const [record] = await tx
|
||||
.insert(shareTexts)
|
||||
.values({
|
||||
content: share.content,
|
||||
description: share.description,
|
||||
imageCdnId,
|
||||
source: share.source,
|
||||
authorId: share.author.userId,
|
||||
authorDiscordId: share.author.discordId,
|
||||
authorName: share.author.displayName,
|
||||
authorAvatarUrl: share.author.avatarUrl,
|
||||
})
|
||||
.returning({
|
||||
id: shareTexts.id,
|
||||
imageCdnId: shareTexts.imageCdnId,
|
||||
createdAt: shareTexts.createdAt,
|
||||
});
|
||||
return record;
|
||||
},
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user