-
Notifications
You must be signed in to change notification settings - Fork 8
add updates sync #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add updates sync #49
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
apps/server/prisma/migrations/20241122132737_add_update/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-- CreateTable | ||
CREATE TABLE "Update" ( | ||
"spaceId" TEXT NOT NULL, | ||
"clock" INTEGER NOT NULL, | ||
"content" BLOB NOT NULL, | ||
|
||
PRIMARY KEY ("spaceId", "clock"), | ||
CONSTRAINT "Update_spaceId_fkey" FOREIGN KEY ("spaceId") REFERENCES "Space" ("id") ON DELETE RESTRICT ON UPDATE CASCADE | ||
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { prisma } from '../prisma.js'; | ||
|
||
type Params = { | ||
accountId: string; | ||
update: string; | ||
spaceId: string; | ||
}; | ||
|
||
export const createUpdate = async ({ accountId, update, spaceId }: Params) => { | ||
// throw error if account is not a member of the space | ||
await prisma.space.findUniqueOrThrow({ | ||
where: { id: spaceId, members: { some: { id: accountId } } }, | ||
}); | ||
|
||
let success = false; | ||
let retries = 0; | ||
const maxRetries = 5; | ||
const retryDelay = 100; // milliseconds | ||
let result: | ||
| { | ||
spaceId: string; | ||
clock: number; | ||
content: Buffer; | ||
} | ||
| undefined = undefined; | ||
|
||
while (!success && retries < maxRetries) { | ||
try { | ||
result = await prisma.$transaction(async (prisma) => { | ||
const lastUpdate = await prisma.update.findFirst({ | ||
where: { spaceId }, | ||
orderBy: { clock: 'desc' }, | ||
}); | ||
|
||
const clock = lastUpdate ? lastUpdate.clock + 1 : 0; | ||
|
||
return await prisma.update.create({ | ||
data: { | ||
spaceId, | ||
clock, | ||
content: Buffer.from(update), | ||
}, | ||
}); | ||
}); | ||
success = true; | ||
return result; | ||
} catch (error) { | ||
const dbError = error as { code?: string; message?: string }; | ||
if (dbError.code === 'P2034' || dbError.code === 'P1008' || dbError.message?.includes('database is locked')) { | ||
retries += 1; | ||
console.warn(`Database is busy, retrying (${retries}/${maxRetries})...`); | ||
await new Promise((resolve) => setTimeout(resolve, retryDelay)); | ||
} else { | ||
console.error('Database error:', error); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (!result) { | ||
throw new Error('Failed to create update'); | ||
} | ||
|
||
return result; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { prisma } from '../prisma.js'; | ||
|
||
type Params = { | ||
accountId: string; | ||
spaceId: string; | ||
after?: number; | ||
}; | ||
|
||
export const listUpdates = async ({ spaceId, accountId, after }: Params) => { | ||
// throw error if account is not a member of the space | ||
await prisma.space.findUniqueOrThrow({ | ||
where: { id: spaceId, members: { some: { id: accountId } } }, | ||
}); | ||
|
||
return await prisma.update.findMany({ | ||
where: after | ||
? { | ||
spaceId, | ||
clock: { gt: after }, | ||
} | ||
: { spaceId }, | ||
orderBy: { | ||
clock: 'desc', | ||
}, | ||
}); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, have we considered using protobuf here? May not be worth it, but curious on your thoughts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, makes a lot of sense here
the main reason why I didn't so far: I yet haven't worked with it and didn't know which library to use or if there are best practices to consider. I created an issue and we can flesh out the details there #53