Skip to content
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

feat: rows and rowsSync API on Shape #1897

Merged
merged 6 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/friendly-toes-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@electric-sql/client": patch
"@electric-sql/react": patch
---

Implement `rows` and `rowsSync` getters on `Shape` interface for easier data access.
2 changes: 1 addition & 1 deletion packages/react-hooks/src/react-hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function parseShapeData<T extends Row<unknown>>(
shape: Shape<T>
): UseShapeResult<T> {
return {
data: [...shape.valueSync.values()],
data: shape.rowsSync,
isLoading: shape.isLoading(),
lastSyncedAt: shape.lastSyncedAt(),
isError: shape.error !== false,
Expand Down
8 changes: 8 additions & 0 deletions packages/typescript-client/src/shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ export class Shape<T extends Row<unknown> = Row> {
return this.#stream.isUpToDate
}

get rows(): Promise<T[]> {
return this.value.then((v) => Array.from(v.values()))
}

get rowsSync(): T[] {
return Array.from(this.valueSync.values())
}

get value(): Promise<ShapeData<T>> {
return new Promise((resolve, reject) => {
if (this.#stream.isUpToDate) {
Expand Down
5 changes: 5 additions & 0 deletions packages/typescript-client/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ describe(`Shape`, () => {
})
const shape = new Shape(shapeStream)
const map = await shape.value
const rows = await shape.rows

expect(map).toEqual(new Map())
expect(rows).toEqual([])
expect(shape.lastSyncedAt()).toBeGreaterThanOrEqual(start)
expect(shape.lastSyncedAt()).toBeLessThanOrEqual(Date.now())
expect(shape.lastSynced()).toBeLessThanOrEqual(Date.now() - start)
Expand Down Expand Up @@ -48,6 +50,9 @@ describe(`Shape`, () => {
})

expect(map).toEqual(expectedValue)
expect(shape.rowsSync).toEqual([
{ id: id, title: `test title`, priority: 10 },
])
expect(shape.lastSyncedAt()).toBeGreaterThanOrEqual(start)
expect(shape.lastSyncedAt()).toBeLessThanOrEqual(Date.now())
expect(shape.lastSynced()).toBeLessThanOrEqual(Date.now() - start)
Expand Down
Loading