kysely-replication
Advanced tools
Comparing version 0.2.0 to 0.2.1
{ | ||
"name": "kysely-replication", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Replication-aware Kysely query execution", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -146,1 +146,40 @@ # kysely-replication | ||
``` | ||
## Wait for Propagation (PostgreSQL-Only) | ||
When writing to the primary, you may want to ensure that replicas have caught up before considering a mutation as complete. This can be critical for consistency in systems that rely on replicated reads immediately following a write. | ||
Replication is usually near-instantaneous but can occasionally take longer (e.g., 30+ seconds). Therefore, waiting for replication increases mutation execution time, so use this approach selectively where required. | ||
### All Mutations | ||
To wait for replication on every mutation, configure the primary database connection: | ||
```diff | ||
-import { Kysely, PostgresDialect } from 'kysely' | ||
+import { CompiledQuery, Kysely, PostgresDialect } from 'kysely' | ||
import { KyselyReplicationDialect } from 'kysely-replication' | ||
import { RoundRobinReplicaStrategy } from 'kysely-replication/strategy/round-robin' | ||
import { Pool } from 'pg' | ||
const primaryDialect = new PostgresDialect({ | ||
pool: new Pool({ connectionString: process.env.DATABASE_URL_PRIMARY }), | ||
+ onCreateConnection: async (connection) => { | ||
+ await connection.executeQuery(CompiledQuery.raw(`set synchronous_commit = 'remote_apply'`)) | ||
+ }, | ||
}) | ||
``` | ||
### Ad-Hoc Mutations | ||
If waiting for replication is only required for specific operations, use SET LOCAL within a transaction. This scopes the setting to the transaction without affecting the connection’s global state. | ||
```ts | ||
import { sql } from 'kysely' | ||
await db.transaction().execute(async (trx) => { | ||
await sql`set local synchronous_commit = 'remote_apply'`.execute(trx) | ||
// continue with the transaction as normal | ||
}) | ||
``` |
99846
184