Record mapping shard names to database instances
Schema SQL, or a function returning it for a given shard
Promise that resolves when schema is created on all shards
const shards = {
'db-east': env.DB_EAST,
'db-west': env.DB_WEST,
'db-central': env.DB_CENTRAL
};
try {
await createSchemaAcrossShards(shards, userSchema);
console.log('Schema created on all shards successfully');
} catch (error) {
console.error('Schema creation failed:', error.message);
// Error will specify which shard failed
}
Applies the schema to all provided database instances in parallel. This is useful for initializing a complete sharded database system where all shards need the same table structure.
The function executes schema creation on all shards concurrently for performance, but provides detailed error reporting that identifies which specific shard failed if any errors occur.
A cluster that mixes backends cannot share one DDL string:
INTEGER PRIMARY KEY AUTOINCREMENTis a syntax error on Postgres, andSERIALis one on SQLite. Pass a function instead of a string to build the statement per shard from its dialect.