Skip to content

Commit

Permalink
Changed sidecar in order not to require changes to the ts-client.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-dp committed Dec 27, 2023
1 parent 130fc97 commit 8638018
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 22 deletions.
3 changes: 1 addition & 2 deletions clients/typescript/src/client/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ export type { ClientTables } from './client'
export type { TableSchema } from './schema'
export { DbSchema, Relation } from './schema'
export { Table } from './table'
export type { HKT } from '../util/hkt'
export { ShapeManager } from './shapes'
export type { HKT } from '../util/hkt'
1 change: 0 additions & 1 deletion clients/typescript/src/satellite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export interface Satellite {
shapeDefinitions: ClientShapeDefinition[]
): Promise<ShapeSubscription>
unsubscribe(shapeUuid: string): Promise<void>
mutexSnapshot(): Promise<Date>
}

export interface Client {
Expand Down
4 changes: 0 additions & 4 deletions clients/typescript/src/satellite/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ export class MockSatelliteProcess implements Satellite {
async stop(): Promise<void> {
await sleepAsync(50)
}

async mutexSnapshot(): Promise<Date> {
return new Date()
}
}

export class MockRegistry extends BaseRegistry {
Expand Down
2 changes: 1 addition & 1 deletion clients/typescript/src/satellite/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export class SatelliteProcess implements Satellite {
/**
* Perform a snapshot while taking out a mutex to avoid concurrent calls.
*/
async mutexSnapshot(): Promise<Date> {
private async mutexSnapshot() {
const release = await this.snapshotMutex.acquire()
try {
return await this._performSnapshot()
Expand Down
2 changes: 1 addition & 1 deletion examples/node-sidecar/sidecar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"dependencies": {
"better-sqlite3": "^9.2.2",
"electric-sql": "^0.8.3"
"electric-sql": "^0.8.2"
},
"devDependencies": {
"@electric-sql/prisma-generator": "^1.1.2",
Expand Down
25 changes: 12 additions & 13 deletions examples/node-sidecar/sidecar/src/sidecar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { HydratedConfig, Shape } from './util/config.js'

export class SideCar {
private electric?: ElectricClient<any>
private shapeManager?: ShapeManager

constructor(private config: HydratedConfig, private ipc: Server) {}

Expand All @@ -23,9 +22,6 @@ export class SideCar {
const schema = new DbSchema({}, []) // empty DB schema, we won't use the client anyway
this.electric = await electrify(conn, schema, config)

const shapeManager = new ShapeManager(this.electric.satellite)
this.shapeManager = shapeManager

// Sync shapes
await this.syncShapes()

Expand All @@ -34,7 +30,7 @@ export class SideCar {

// Perform snapshot on potential data change
await this.ipc.onPotentialDataChange(
this.performSnapshot.bind(this)
this.potentiallyChanged.bind(this)
)

// Notify clients of actual data changes
Expand All @@ -52,20 +48,23 @@ export class SideCar {
)
}

async performSnapshot(): Promise<void> {
await this.electric?.satellite.mutexSnapshot()
private async potentiallyChanged(): Promise<void> {
await this.electric?.notifier.potentiallyChanged()
}

async syncShapes(): Promise<void> {
if (!this.shapeManager) {
throw new Error("Shape manager not initialized")
}

private async syncShapes(): Promise<void> {
// Convert the shape to the format expected by the Satellite process
const { sync: tables } = this.config
const shapeDef = {
selects: tables.map((tbl) => ({ tablename: tbl })),
}

const joinedNames = tables.join(', ')
console.log(`Syncing tables ${joinedNames}...`)
const { synced } = await this.shapeManager.sync({ tables })

const { synced } = await this.electric!.satellite.subscribe([shapeDef])
await synced

console.log(`Synced tables ${joinedNames}`)
}
}

0 comments on commit 8638018

Please sign in to comment.