Skip to content

Add initial Data preparing for SSR support #127

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
88 changes: 81 additions & 7 deletions packages/db-collections/src/electric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,27 @@ import type { CollectionConfig, SyncConfig } from "@tanstack/db"
import type {
ControlMessage,
Message,
Offset,
Row,
ShapeStreamOptions,
} from "@electric-sql/client"

/**
* Initial data structure for server-side rendering
*/
export interface ElectricInitialData<T extends Row<unknown>> {
data: Array<{ key: string; value: T; metadata?: Record<string, unknown> }>
txids: Array<number>
schema?: string
lastOffset?: string
shapeHandle?: string
}

/**
* Configuration interface for ElectricCollection
*/
export interface ElectricCollectionConfig<T extends Row<unknown>>
extends Omit<CollectionConfig<T>, `sync`> {
extends Omit<CollectionConfig<T>, `sync` | `initialData`> {
/**
* Configuration options for the ElectricSQL ShapeStream
*/
Expand All @@ -27,6 +39,12 @@ export interface ElectricCollectionConfig<T extends Row<unknown>>
* Array of column names that form the primary key of the shape
*/
primaryKey: Array<string>

/**
* Initial data from server-side rendering
* Allows hydration from server-loaded Electric data
*/
initialData?: ElectricInitialData<T>
}

/**
Expand All @@ -38,15 +56,53 @@ export class ElectricCollection<
private seenTxids: Store<Set<number>>

constructor(config: ElectricCollectionConfig<T>) {
const seenTxids = new Store<Set<number>>(new Set([Math.random()]))
const initialTxids = config.initialData?.txids || [Math.random()]
const seenTxids = new Store<Set<number>>(new Set(initialTxids))

const sync = createElectricSync<T>(config.streamOptions, {
primaryKey: config.primaryKey,
seenTxids,
initialData: config.initialData,
})

super({ ...config, sync })
super({
...config,
sync,
initialData: undefined,
})

this.seenTxids = seenTxids

if (config.initialData?.data && config.initialData.data.length > 0) {
this.seedFromElectricInitialData(config.initialData.data)
}
}

private seedFromElectricInitialData(
items: Array<{ key: string; value: T; metadata?: Record<string, unknown> }>
): void {
const keys = new Set<string>()

this.syncedData.setState((prevData) => {
const newData = new Map(prevData)
items.forEach(({ key, value }) => {
keys.add(key)
newData.set(key, value)
this.objectKeyMap.set(value, key)
})
return newData
})

this.syncedMetadata.setState((prevMetadata) => {
const newMetadata = new Map(prevMetadata)
items.forEach(({ key, metadata }) => {
const syncMetadata = this.config.sync.getSyncMetadata?.() || {}
newMetadata.set(key, { ...syncMetadata, ...metadata })
})
return newMetadata
})

this.onFirstCommit(() => {})
}

/**
Expand Down Expand Up @@ -114,12 +170,18 @@ export function createElectricCollection<T extends Row<unknown>>(
*/
function createElectricSync<T extends Row<unknown>>(
streamOptions: ShapeStreamOptions,
options: { primaryKey: Array<string>; seenTxids: Store<Set<number>> }
options: {
primaryKey: Array<string>
seenTxids: Store<Set<number>>
initialData?: ElectricInitialData<T>
}
): SyncConfig<T> {
const { primaryKey, seenTxids } = options
const { primaryKey, seenTxids, initialData } = options

// Store for the relation schema information
const relationSchema = new Store<string | undefined>(undefined)
const relationSchema = new Store<string | undefined>(
initialData?.schema || undefined
)

/**
* Get the sync metadata for insert operations
Expand All @@ -140,7 +202,19 @@ function createElectricSync<T extends Row<unknown>>(
return {
sync: (params: Parameters<SyncConfig<T>[`sync`]>[0]) => {
const { begin, write, commit } = params
const stream = new ShapeStream(streamOptions)

// Resume from where server left off if we have initial data
const resumeOptions: ShapeStreamOptions = {
...streamOptions,
...(initialData?.lastOffset && {
offset: initialData.lastOffset as Offset,
}),
...(initialData?.shapeHandle && {
shapeHandle: initialData.shapeHandle,
}),
}

const stream = new ShapeStream(resumeOptions)
let transactionStarted = false
let newTxids = new Set<number>()

Expand Down
1 change: 1 addition & 0 deletions packages/db-collections/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export {
ElectricCollection,
createElectricCollection,
type ElectricCollectionConfig,
type ElectricInitialData,
} from "./electric"
export {
QueryCollection,
Expand Down
Loading