Loading...
Loading...
Use Electric with Drizzle ORM or Prisma for the write path. Covers getting pg_current_xact_id() from ORM transactions using Drizzle tx.execute(sql) and Prisma $queryRaw, running migrations that preserve REPLICA IDENTITY FULL, and schema management patterns compatible with Electric shapes. Load when using Drizzle or Prisma alongside Electric for writes.
npx skill4agent add electric-sql/electric electric-ormimport { drizzle } from 'drizzle-orm/node-postgres'
import { sql } from 'drizzle-orm'
import { todos } from './schema'
const db = drizzle(pool)
// Write with txid for Electric reconciliation
async function createTodo(text: string, userId: string) {
return await db.transaction(async (tx) => {
const [row] = await tx
.insert(todos)
.values({
id: crypto.randomUUID(),
text,
userId,
})
.returning()
const [{ txid }] = await tx.execute<{ txid: string }>(
sql`SELECT pg_current_xact_id()::xid::text AS txid`
)
return { id: row.id, txid: parseInt(txid) }
})
}import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
async function createTodo(text: string, userId: string) {
return await prisma.$transaction(async (tx) => {
const todo = await tx.todo.create({
data: { id: crypto.randomUUID(), text, userId },
})
const [{ txid }] = await tx.$queryRaw<[{ txid: string }]>`
SELECT pg_current_xact_id()::xid::text AS txid
`
return { id: todo.id, txid: parseInt(txid) }
})
}// In migration file
import { sql } from 'drizzle-orm'
export async function up(db) {
await db.execute(sql`
CREATE TABLE todos (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
text TEXT NOT NULL,
completed BOOLEAN DEFAULT false
)
`)
await db.execute(sql`ALTER TABLE todos REPLICA IDENTITY FULL`)
}-- prisma/migrations/001_init/migration.sql
CREATE TABLE "todos" (
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"text" TEXT NOT NULL,
"completed" BOOLEAN DEFAULT false
);
ALTER TABLE "todos" REPLICA IDENTITY FULL;import { createCollection } from '@tanstack/react-db'
import { electricCollectionOptions } from '@tanstack/electric-db-collection'
export const todoCollection = createCollection(
electricCollectionOptions({
id: 'todos',
schema: todoSchema,
getKey: (row) => row.id,
shapeOptions: { url: '/api/todos' },
onInsert: async ({ transaction }) => {
const newTodo = transaction.mutations[0].modified
const { txid } = await createTodo(newTodo.text, newTodo.userId)
return { txid }
},
})
)// Drizzle — no txid returned
const [todo] = await db.insert(todos).values({ text: 'New' }).returning()
return { id: todo.id }// Drizzle — txid in same transaction
const result = await db.transaction(async (tx) => {
const [row] = await tx.insert(todos).values({ text: 'New' }).returning()
const [{ txid }] = await tx.execute<{ txid: string }>(
sql`SELECT pg_current_xact_id()::xid::text AS txid`
)
return { id: row.id, txid: parseInt(txid) }
})pg_current_xact_id()AGENTS.md:116-119// ORM migration recreates table without REPLICA IDENTITY
await db.execute(sql`DROP TABLE todos`)
await db.execute(sql`CREATE TABLE todos (...)`)
// Missing: ALTER TABLE todos REPLICA IDENTITY FULLawait db.execute(sql`DROP TABLE todos`)
await db.execute(sql`CREATE TABLE todos (...)`)
await db.execute(sql`ALTER TABLE todos REPLICA IDENTITY FULL`)REPLICA IDENTITY FULLwebsite/docs/guides/troubleshooting.md:373