@@ -292,7 +292,7 @@ export class RealtimeClient {
292292 ) : URL {
293293 const $url = new URL ( url . toString ( ) ) ;
294294
295- const electricOrigin = this . #resolveElectricOrigin( environment . id ) ;
295+ const electricOrigin = this . #resolveElectricOrigin( $url , whereClause , environment . id ) ;
296296 const electricUrl = new URL ( `${ electricOrigin } /v1/shape` ) ;
297297
298298 // Copy over all the url search params to the electric url
@@ -483,14 +483,30 @@ export class RealtimeClient {
483483 return `${ this . options . keyPrefix } :${ environmentId } ` ;
484484 }
485485
486- #resolveElectricOrigin( environmentId : string ) {
486+ #resolveElectricOrigin( url : URL , whereClause : string , environmentId : string ) {
487487 if ( typeof this . options . electricOrigin === "string" ) {
488488 return this . options . electricOrigin ;
489489 }
490490
491- const index = jumpHash ( environmentId , this . options . electricOrigin . length ) ;
491+ const shardKey = this . #getShardKey ( whereClause , environmentId ) ;
492492
493- return this . options . electricOrigin [ index ] ?? this . options . electricOrigin [ 0 ] ;
493+ const index = jumpHash ( shardKey , this . options . electricOrigin . length ) ;
494+
495+ const origin = this . options . electricOrigin [ index ] ?? this . options . electricOrigin [ 0 ] ;
496+
497+ logger . debug ( "[realtimeClient] resolveElectricOrigin" , {
498+ whereClause,
499+ environmentId,
500+ shardKey,
501+ index,
502+ electricOrigin : origin ,
503+ } ) ;
504+
505+ return origin ;
506+ }
507+
508+ #getShardKey( whereClause : string , environmentId : string ) {
509+ return [ environmentId , whereClause ] . join ( ":" ) ;
494510 }
495511
496512 #registerCommands( ) {
0 commit comments