From 75f337a96a7f93f4c4c8ff67d02fdfcb5cd96c02 Mon Sep 17 00:00:00 2001 From: Miguel Romero Karam Date: Thu, 30 May 2024 12:06:17 +0200 Subject: [PATCH] 0.5.50 --- lib/cli/src/version.ts | 2 +- lib/plugins/database/plugin.ts | 45 ++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/lib/cli/src/version.ts b/lib/cli/src/version.ts index 2b54a763..ce350fff 100644 --- a/lib/cli/src/version.ts +++ b/lib/cli/src/version.ts @@ -1,5 +1,5 @@ // latest version of netzo/cli (see https://github.com/netzo/netzo/releases) -export const VERSION = "0.5.49"; +export const VERSION = "0.5.50"; // minimum version of Deno required to run this CLI // (see https://github.com/denoland/deployctl/blob/main/src/version.ts) diff --git a/lib/plugins/database/plugin.ts b/lib/plugins/database/plugin.ts index 408d65fa..15c6b96b 100644 --- a/lib/plugins/database/plugin.ts +++ b/lib/plugins/database/plugin.ts @@ -95,26 +95,33 @@ export const database = (config?: DatabaseConfig): Plugin => { return Response.json(rows); }, POST: async (req, ctx) => { + const pk = ctx.url.searchParams.get("pk") || "id"; const { tableName } = ctx.params; const table = config.schema![tableName] as any; - const pk = Object.entries(table).find((e) => !!e[1]?.primary)?.[0]!; const data = await parseRequestBody(req); - const { [pk]: id, ...rest } = data; - const result = await db - .insert(table) - .values({ id, ...rest }) - // there is no option to exclude column in sql, that's why we have to specify the - // columns we want to update by using a custom function buildConflictUpdateColumns() - // see https://discord.com/channels/1043890932593987624/1243571991945019402/1243571991945019402 - // and also https://orm.drizzle.team/learn/guides/upsert - .onConflictDoUpdate({ - target: table[pk], - set: buildConflictUpdateColumns(table[pk], Object.keys(rest)), - }) - .returning(); - return Response.json( - Array.isArray(result) ? result[0] : result.rows, - ); + try { + const result = await db + .insert(table) + .values(data) + // NOTE: there is no option to exclude column in sql, that's why we + // have to specify the columns we want to update by using a custom + // function buildConflictUpdateColumns() to allow for bulk upserts + // see https://discord.com/channels/1043890932593987624/1243571991945019402/1243571991945019402 + // and also https://orm.drizzle.team/learn/guides/upsert + .onConflictDoUpdate({ + target: table[pk], + set: { ...data, id: sql`${table[pk]}` }, // leave "id" as it was + // set: buildConflictUpdateColumns(table[pk], columns), + }) + .returning(); + console.log(result); + return Response.json( + Array.isArray(result) ? result[0] : result.rows, + ); + } catch (error) { + console.error(error); + return Response.json({ error: error.message }, { status: 400 }); + } }, }, } satisfies PluginRoute, @@ -173,8 +180,8 @@ function buildConflictUpdateColumns< ) { const cls = getTableColumns(table); return columns.reduce((acc, column) => { - const colName = cls[column].name; - acc[column] = sql.raw(`excluded.${colName}`); + const colName = cls?.[column]?.name; + if (colName) acc[column] = sql.raw(`excluded.${colName}`); return acc; }, {} as Record); }