Aller au contenu
Français

ShardKey

Ce contenu n’est pas encore disponible dans votre langue.

Defined in: src/cluster/sharding/ShardKey.ts:22

Named, type-tagged identity for a sharded entity type — the sharding counterpart to SingletonKey. Ties the typeName string and the message type together so start(), entityRefFor() and startProxy() hand back a correctly-typed ActorRef without the caller repeating either.

class UserActor extends PersistentActor<UserCommand, UserEvent, UserState> {
static readonly shard = ShardKey.of<UserCommand>('user', (command) => command.userId);
}
const users = cluster.sharding.start(UserActor);
const user = cluster.sharding.entityRefFor(UserActor, 'user-42');

The explicit type argument is load-bearing: without it the key infers ShardKey<unknown> and every ref taken from it degrades to ActorRef<unknown>.

TCommand = unknown

new ShardKey<TCommand>(typeName, extractEntityId?): ShardKey<TCommand>

Defined in: src/cluster/sharding/ShardKey.ts:26

string

(command) => string

How a command names its entity.

It rides along on the key so the declaring class is the single source of truth for both halves of “what this entity type is” — but it is NOT part of the identity, and equals ignores it. A node that only looks entities up (entityRefFor, startProxy) never routes by extraction, so it can name the same type with a key that omits the extractor entirely. An extractEntityId passed in options wins over this one.

ShardKey<TCommand>

readonly _command: TCommand

Defined in: src/cluster/sharding/ShardKey.ts:24

Phantom field — retains TCommand so inference round-trips through the key.


readonly optional extractEntityId?: (command) => string

Defined in: src/cluster/sharding/ShardKey.ts:38

How a command names its entity.

It rides along on the key so the declaring class is the single source of truth for both halves of “what this entity type is” — but it is NOT part of the identity, and equals ignores it. A node that only looks entities up (entityRefFor, startProxy) never routes by extraction, so it can name the same type with a key that omits the extractor entirely. An extractEntityId passed in options wins over this one.

TCommand

string


readonly typeName: string

Defined in: src/cluster/sharding/ShardKey.ts:27

equals(other): boolean

Defined in: src/cluster/sharding/ShardKey.ts:52

ShardKey<TCommand>

boolean


toString(): string

Defined in: src/cluster/sharding/ShardKey.ts:53

string


static of<TCommand>(typeName, extractEntityId?): ShardKey<TCommand>

Defined in: src/cluster/sharding/ShardKey.ts:41

TCommand

string

(command) => string

ShardKey<TCommand>