diff --git a/examples/linearlite/server.ts b/examples/linearlite/server.ts index 5fee4e2c57..b1fda875bb 100644 --- a/examples/linearlite/server.ts +++ b/examples/linearlite/server.ts @@ -39,8 +39,15 @@ app.post('/apply-changes', async (c) => { console.error(error) return c.json({ error: 'Invalid changes' }, 400) } - const changeResponse = await applyChanges(parsedChanges) - return c.json(changeResponse) + try { + await applyChanges(parsedChanges) + } catch (error) { + // In a real app you would want to check which changes have failed and save that + // and return that information to the client. + console.error(error) + return c.json({ error: 'Failed to apply changes' }, 500) + } + return c.json({ success: true }) }) // Start the server @@ -52,22 +59,16 @@ serve({ port, }) -async function applyChanges(changes: ChangeSet): Promise<{ success: boolean }> { +async function applyChanges(changes: ChangeSet) { const { issues, comments } = changes - - try { - await sql.begin(async (sql) => { - for (const issue of issues) { - await applyTableChange('issue', issue, sql) - } - for (const comment of comments) { - await applyTableChange('comment', comment, sql) - } - }) - return { success: true } - } catch (error) { - throw error - } + await sql.begin(async (sql) => { + for (const issue of issues) { + await applyTableChange('issue', issue, sql) + } + for (const comment of comments) { + await applyTableChange('comment', comment, sql) + } + }) } async function applyTableChange( diff --git a/examples/linearlite/src/sync.ts b/examples/linearlite/src/sync.ts index ea5a84f528..efdeb662e1 100644 --- a/examples/linearlite/src/sync.ts +++ b/examples/linearlite/src/sync.ts @@ -214,6 +214,8 @@ async function doSyncToServer(pg: PGliteWithExtensions) { body: JSON.stringify(changeSet), }) if (!response.ok) { + // In a real app you would want to check which changes have failed and save that + // information to the database, maybe in a `sync_errors` column on the row effected. throw new Error('Failed to apply changes') } await pg.transaction(async (tx) => { diff --git a/examples/linearlite/supabase/functions/write-server/index.ts b/examples/linearlite/supabase/functions/write-server/index.ts index 6554c8730f..05b547d46a 100644 --- a/examples/linearlite/supabase/functions/write-server/index.ts +++ b/examples/linearlite/supabase/functions/write-server/index.ts @@ -65,7 +65,7 @@ app.get('/write-server/', async (c) => { return c.json(result[0]) }) -app.post('/write-server/apply-changes', async (c) => { +app.post('/apply-changes', async (c) => { const content = await c.req.json() let parsedChanges: ChangeSet try { @@ -75,26 +75,27 @@ app.post('/write-server/apply-changes', async (c) => { console.error(error) return c.json({ error: 'Invalid changes' }, 400) } - const changeResponse = await applyChanges(parsedChanges) - return c.json(changeResponse) -}) - -async function applyChanges(changes: ChangeSet): Promise<{ success: boolean }> { - const { issues, comments } = changes - try { - await sql.begin(async (sql) => { - for (const issue of issues) { - await applyTableChange('issue', issue, sql) - } - for (const comment of comments) { - await applyTableChange('comment', comment, sql) - } - }) - return { success: true } + await applyChanges(parsedChanges) } catch (error) { - throw error + // In a real app you would want to check which changes have failed and save that + // and return that information to the client. + console.error(error) + return c.json({ error: 'Failed to apply changes' }, 500) } + return c.json({ success: true }) +}) + +async function applyChanges(changes: ChangeSet) { + const { issues, comments } = changes + await sql.begin(async (sql) => { + for (const issue of issues) { + await applyTableChange('issue', issue, sql) + } + for (const comment of comments) { + await applyTableChange('comment', comment, sql) + } + }) } async function applyTableChange(