Skip to content

Commit babf494

Browse files
chore: updated readme and jsdoc for upsert
1 parent 41ed3f9 commit babf494

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

README.md

+45-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ _Supported Deno verisons:_ **^1.38.5**
4141
- [updateByPrimaryIndex()](#updatebyprimaryindex)
4242
- [updateBySecondaryIndex()](#updatebysecondaryindex)
4343
- [updateMany()](#updatemany)
44+
- [upsert()](#upsert)
4445
- [delete()](#delete)
4546
- [deleteByPrimaryIndex()](#deletebyprimaryindex)
4647
- [deleteBySecondaryIndex()](#deletebysecondaryindex)
@@ -245,7 +246,7 @@ timestamp, type of either "write" or "delete", and a copy of the document value
245246
if the type is "write".
246247

247248
```ts
248-
const history = await db.users.findHistory("user_id")
249+
const { result } = await db.users.findHistory("user_id")
249250
```
250251

251252
### findUndelivered()
@@ -406,6 +407,49 @@ const { result } = await db.users.updateMany({ age: 67 }, {
406407
const { result } = await db.users.updateMany({ username: "XuserX" })
407408
```
408409

410+
### upsert()
411+
412+
Update an existing document by either id or primary index, or set a new document
413+
entry if no document with matching id/index exists. When upserting by primary
414+
index, an id can be optionally specified which will be used when setting a new
415+
document entry, otherwise an id will be generated.
416+
417+
```ts
418+
// Upsert by id
419+
const result1 = await db.users.upsert({
420+
id: "user_id",
421+
update: { username: "Chris" },
422+
set: {
423+
username: "Chris",
424+
age: 54,
425+
activities: ["bowling"],
426+
address: {
427+
country: "USA",
428+
city: "Las Vegas"
429+
street: "St. Boulevard"
430+
houseNumber: 23
431+
}
432+
}
433+
})
434+
435+
// Upsert by index
436+
const result2 = await db.users.upsert({
437+
index: ["username", "Jack"],
438+
update: { username: "Chris" },
439+
set: {
440+
username: "Chris",
441+
age: 54,
442+
activities: ["bowling"],
443+
address: {
444+
country: "USA",
445+
city: "Las Vegas"
446+
street: "St. Boulevard"
447+
houseNumber: 23
448+
}
449+
}
450+
})
451+
```
452+
409453
### delete()
410454

411455
Delete one or more documents with the given ids from the KV store.

src/collection.ts

+49-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ export class Collection<
428428
*
429429
* @example
430430
* ```ts
431-
* const history = await db.users.findHistory("user_id")
431+
* const { result } = await db.users.findHistory("user_id")
432432
* ```
433433
*
434434
* @param id - Document id.
@@ -834,6 +834,54 @@ export class Collection<
834834
)
835835
}
836836

837+
/**
838+
* Update an existing document by either id or primary index, or set a new document
839+
* entry if no document with matching id/index exists.
840+
*
841+
* When upserting by primary index, an id can be optionally specified which
842+
* will be used when setting a new document entry, otherwise an id will be generated.
843+
*
844+
* @example
845+
* ```ts
846+
* // Upsert by id
847+
* const result1 = await db.users.upsert({
848+
* id: "user_id",
849+
* update: { username: "Chris" },
850+
* set: {
851+
* username: "Chris",
852+
* age: 54,
853+
* activities: ["bowling"],
854+
* address: {
855+
* country: "USA",
856+
* city: "Las Vegas"
857+
* street: "St. Boulevard"
858+
* houseNumber: 23
859+
* }
860+
* }
861+
* })
862+
*
863+
* // Upsert by index
864+
* const result2 = await db.users.upsert({
865+
* index: ["username", "Jack"],
866+
* update: { username: "Chris" },
867+
* set: {
868+
* username: "Chris",
869+
* age: 54,
870+
* activities: ["bowling"],
871+
* address: {
872+
* country: "USA",
873+
* city: "Las Vegas"
874+
* street: "St. Boulevard"
875+
* houseNumber: 23
876+
* }
877+
* }
878+
* })
879+
* ```
880+
*
881+
* @param input - Upsert input, including id or index, update data and set data.
882+
* @param options - Upsert options.
883+
* @returns A promise resolving to either CommitResult or CommitError.
884+
*/
837885
async upsert<
838886
const TIndex extends PrimaryIndexKeys<TOutput, TOptions>,
839887
>(

0 commit comments

Comments
 (0)