From 64d7ea05854faa9c219a8d0d59b263709bb81e9f Mon Sep 17 00:00:00 2001 From: Sam Willis Date: Thu, 5 Dec 2024 11:08:58 +0000 Subject: [PATCH] Fix write server --- demos/linearlite/server.ts | 21 ++++++++--------- .../supabase/functions/write-server/index.ts | 23 ++++++++----------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/demos/linearlite/server.ts b/demos/linearlite/server.ts index 4cd2fc58..5fee4e2c 100644 --- a/demos/linearlite/server.ts +++ b/demos/linearlite/server.ts @@ -75,29 +75,26 @@ async function applyTableChange( change: IssueChange | CommentChange, sql: postgres.TransactionSql ): Promise { - const { id, modified_columns, new: isNew, deleted } = change + const { + id, + modified_columns: modified_columns_raw, + new: isNew, + deleted, + } = change + const modified_columns = modified_columns_raw as (keyof typeof change)[] if (deleted) { await sql` DELETE FROM ${sql(tableName)} WHERE id = ${id} ` } else if (isNew) { - const columns = modified_columns || [] - const values = columns.map((col) => change[col]) - await sql` - INSERT INTO ${sql(tableName)} (id, ${sql(columns)}) - VALUES (${id}, ${sql(values)}) + INSERT INTO ${sql(tableName)} ${sql(change, 'id', ...modified_columns)} ` } else { - const columns = modified_columns || [] - const updates = columns - .map((col) => ({ [col]: change[col] })) - .reduce((acc, curr) => ({ ...acc, ...curr }), {}) - await sql` UPDATE ${sql(tableName)} - SET ${sql(updates)} + SET ${sql(change, ...modified_columns)} WHERE id = ${id} ` } diff --git a/demos/linearlite/supabase/functions/write-server/index.ts b/demos/linearlite/supabase/functions/write-server/index.ts index 6668fca9..6554c873 100644 --- a/demos/linearlite/supabase/functions/write-server/index.ts +++ b/demos/linearlite/supabase/functions/write-server/index.ts @@ -7,7 +7,7 @@ import 'jsr:@supabase/functions-js/edge-runtime.d.ts' import { Hono } from 'jsr:@hono/hono' import { cors } from 'jsr:@hono/hono/cors' import postgres from 'https://deno.land/x/postgresjs/mod.js' -import { z } from "https://deno.land/x/zod/mod.ts"; +import { z } from 'https://deno.land/x/zod/mod.ts' const issueChangeSchema = z.object({ id: z.string(), @@ -102,29 +102,26 @@ async function applyTableChange( change: IssueChange | CommentChange, sql: postgres.TransactionSql ): Promise { - const { id, modified_columns, new: isNew, deleted } = change + const { + id, + modified_columns: modified_columns_raw, + new: isNew, + deleted, + } = change + const modified_columns = modified_columns_raw as (keyof typeof change)[] if (deleted) { await sql` DELETE FROM ${sql(tableName)} WHERE id = ${id} ` } else if (isNew) { - const columns = modified_columns || [] - const values = columns.map((col) => change[col]) - await sql` - INSERT INTO ${sql(tableName)} (id, ${sql(columns)}) - VALUES (${id}, ${sql(values)}) + INSERT INTO ${sql(tableName)} ${sql(change, 'id', ...modified_columns)} ` } else { - const columns = modified_columns || [] - const updates = columns - .map((col) => ({ [col]: change[col] })) - .reduce((acc, curr) => ({ ...acc, ...curr }), {}) - await sql` UPDATE ${sql(tableName)} - SET ${sql(updates)} + SET ${sql(change, ...modified_columns)} WHERE id = ${id} ` }