> ## Documentation Index
> Fetch the complete documentation index at: https://confect.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Writing

> Insert, modify, and delete documents in the database.

The `DatabaseWriter` service is used to insert, modify, and delete documents in the database.

```ts theme={null}
import * as Effect from "effect/Effect";
import { DatabaseWriter } from "./confect/_generated/services";

Effect.gen(function* () {
  const writer = yield* DatabaseWriter;

  return yield* writer.table("notes").insert({
    text: "This is my first note",
  });
});
```

## Methods

### Insert

Insert a new document.

```ts theme={null}
writer.table("notes").insert({
  text: "This is my first note",
  tag: "drafts",
});
```

### Patch

Shallow merge the provided object with the existing document. New fields are added. Existing fields are overwritten. Optional fields set to `undefined` are unset.

```ts theme={null}
writer.table("notes").patch(noteId, {
  text: "Patched text",
  author: { role: "user", name: "John Doe" },
  tag: undefined,
});
```

### Replace

Replace the entire document.

```ts theme={null}
writer.table("notes").replace(noteId, {
  text: "I'm replacing the whole document",
});
```

### Delete

Delete the document with the given ID.

```ts theme={null}
writer.table("notes").delete(noteId);
```
