fix: batch saved keyword inserts for D1 (#85)

This commit is contained in:
Ben Senescu 2026-04-07 00:36:17 -04:00 committed by Ben Senescu
parent ad3b732f60
commit e5206ec07b
2 changed files with 11 additions and 9 deletions

View File

@ -66,18 +66,20 @@ async function saveKeywordsToProject(params: {
}) {
if (params.keywords.length === 0) return;
await db
const [first, ...rest] = params.keywords.map((keyword) =>
db
.insert(savedKeywords)
.values(
params.keywords.map((keyword) => ({
.values({
id: crypto.randomUUID(),
projectId: params.projectId,
keyword,
locationCode: params.locationCode,
languageCode: params.languageCode,
})),
)
.onConflictDoNothing();
})
.onConflictDoNothing(),
);
await db.batch([first, ...rest]);
}
async function listSavedKeywordsByProject(projectId: string) {

View File

@ -16,7 +16,7 @@ export const researchKeywordsSchema = z.object({
export const saveKeywordsSchema = z.object({
projectId: z.string().min(1),
keywords: z.array(z.string().min(1)).min(1).max(200),
keywords: z.array(z.string().min(1)).min(1).max(500),
locationCode: z.number().int().positive().default(2840),
languageCode: z.string().min(2).max(8).default("en"),
metrics: z
@ -54,7 +54,7 @@ export const saveKeywordsSchema = z.object({
.optional(),
}),
)
.max(200)
.max(500)
.optional(),
});