Sharding Low-Level API
import pg from "pg";
export const cluster = new Cluster({
islands: async () => [ // sync or async
{
no: 0,
nodes: [
{ name: "abc-writer-1", config: { host: "...", ... } },
{ name: "abc-reader-2", config: { host: "...", ... } },
],
},
{
no: 1,
nodes: [
{ name: "abc-writer-3", config: { host: "...", ... } },
{ name: "abc-reader-4", config: { host: "...", ... } },
],
},
],
createClient: (node) => new PgClient({
...node,
// you can use your own Pool class here instead of node-postgres
createPool: (config) => new pg.Pool(config),
} satisfies PgClientOptions),
shardNamer: new ShardNamer({
nameFormat: "sh%04d",
discoverQuery:
"SELECT unnest FROM unnest(microsharding.microsharding_list_active_shards())",
}),
...,
});Substituting the Default node-postgres Pool
cluster.shardByNo(): Get a Shard by its Number
Sending SQL Queries via a Shard Client
pgClient.query(): Send a Single SQL Query
pgClient.acquireConn(): Get a Low-Level node-postgres Client
Other Ways of Accessing Shards
cluster.globalShard(): Access a Global Shard
cluster.nonGlobalShards(): Get the List of All Shards
cluster.randomShard(): Get a Random Shard in the Cluster
cluster.shard(id): Get a Shard from the ID prefix

Working with Islands
cluster.islands(): Get All Islands of the Cluster
cluster.island(no): Get One Island by its Number
island.master(): Get a Client for Island Master Node
island.shards(): Get the Currently Known Shards on an Island
Last updated