Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
samwillis committed Dec 10, 2024
1 parent 541437a commit ccf4700
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
35 changes: 18 additions & 17 deletions examples/linearlite/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions examples/linearlite/src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
37 changes: 19 additions & 18 deletions examples/linearlite/supabase/functions/write-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down

0 comments on commit ccf4700

Please sign in to comment.