Skip to main content

Writing data

Insert, update and delete data using the create, createMany, update, updateMany, delete and deleteMany functions.

For example, to insert a new project:

const project = await db.projects.create({
data: {
name: 'ElectricSQL',
description: 'Instant local-first for your Postgres'
}
})

To update the same project:

await db.projects.update({
where: {
id: project.id
},
data: {
description: 'Local-first Postgres'
}
})

To delete the project:

await db.projects.delete({
where: {
id: project.id
}
})

Reacting to writes

Writes automatically cause any relevant live queries to update. For example, if you take the following component:

const MyComponent = () => {
const { db } = useElectric()!
const { results } = useLiveQuery(db.projects.liveMany())

const add = async () => (
await db.projects.create({
data: {
/* ... */
}
})
)

return <List items={results} add={add} />
}

Calling add will insert a new project into the local database. ElectricSQL will automatically detect and replicate the write. The replication process emits a notification, causing the live query to re-run. This updates the value of the results state variable, which triggers the component to re-render.

This automatic reactivity works no matter where the write is made locally, on another device, by another user, or directly into Postgres.