Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

Commit

Permalink
0.5.50
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelrk committed May 30, 2024
1 parent 4fe3a67 commit 75f337a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/cli/src/version.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
45 changes: 26 additions & 19 deletions lib/plugins/database/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Q, SQL>);
}

0 comments on commit 75f337a

Please sign in to comment.