Skip to content

Commit 8e6e3cc

Browse files
committed
docs: upsert, upsertBatch 문서화
1 parent e99d5c7 commit 8e6e3cc

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

docs/MUTATION_AND_TRANSACTION.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,23 @@ There are two ways to modify documents according to your needs.
8080
);
8181
```
8282

83+
- **Upsert Control (`upsert` & `upsertBatch`)**
84+
Inserts a document if it does not exist, or updates (full update) if it already exists based on `_id`.
85+
- Without `_id` or if `_id` is not present in the DB: Inserts as a new document with an auto-generated `_id`.
86+
- If `_id` exists in the DB: Performs a full update (`fullUpdate`) on the existing document.
87+
- Returns the count of updated documents (`0` if inserted, `1` if updated for single `upsert`).
88+
89+
```typescript
90+
// Single document upsert
91+
const updatedCount = await db.upsert({ _id: 1, name: 'Alice Updated', age: 26 });
92+
93+
// Batch document upsert
94+
const updatedBatchCount = await db.upsertBatch([
95+
{ name: 'Bob', age: 30 }, // Insert (no _id)
96+
{ _id: 1, name: 'Alice Re-updated', age: 27 } // Update (existing _id)
97+
]);
98+
```
99+
83100
- **Document Deletion (`delete`)**
84101
Permanently removes documents that fit specific search conditions.
85102
```typescript

readme.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ const activeSeniors = await db.select({
110110
}).drain();
111111
```
112112

113+
### Upsert Operations (Insert or Full Update)
114+
```typescript
115+
// Single Upsert: Inserts if _id is missing/not found, updates if _id exists
116+
await db.upsert({ _id: 1, name: 'John Doe', age: 31 });
117+
118+
// Batch Upsert: Process multiple inserts and updates in a single batch
119+
await db.upsertBatch([
120+
{ name: 'New User', age: 20 }, // Inserted with auto _id
121+
{ _id: 1, name: 'John Doe Updated', age: 32 } // Updated existing document
122+
]);
123+
```
124+
113125
---
114126

115127
## 📚 Detailed Manual

0 commit comments

Comments
 (0)