Skip to content

Commit

Permalink
Fix write server
Browse files Browse the repository at this point in the history
  • Loading branch information
samwillis committed Dec 5, 2024
1 parent a88cd82 commit 64d7ea0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
21 changes: 9 additions & 12 deletions demos/linearlite/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,26 @@ async function applyTableChange(
change: IssueChange | CommentChange,
sql: postgres.TransactionSql
): Promise<void> {
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}
`
}
Expand Down
23 changes: 10 additions & 13 deletions demos/linearlite/supabase/functions/write-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -102,29 +102,26 @@ async function applyTableChange(
change: IssueChange | CommentChange,
sql: postgres.TransactionSql
): Promise<void> {
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}
`
}
Expand Down

0 comments on commit 64d7ea0

Please sign in to comment.