Skip to content

Commit

Permalink
Merge pull request #150 from oliver-oloughlin/feature/array-merge-update
Browse files Browse the repository at this point in the history
Feature/array merge update
  • Loading branch information
oliver-oloughlin authored Dec 20, 2023
2 parents 770c059 + b49ee23 commit 6c444b7
Show file tree
Hide file tree
Showing 39 changed files with 2,036 additions and 560 deletions.
63 changes: 31 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,25 +326,21 @@ if (result1.ok) {

### update()

Update the value of an exisiting document in the KV store. For primitive values,
arrays and built-in objects (Array, Date, RegExp, etc.), this method overwrites
the exisiting data with the new value. For plain object values, this method
performs a partial update, merging the new value with the existing data using
deep merge by default, or optionally using shallow merge. Upon completion, a
CommitResult object will be returned with the document id, versionstamp and ok
flag. If no document with a matching id exists in the collection, the operation
will fail.
Updates the value of an exisiting document in the KV store by id. By default,
the `merge` strategy is used when available, falling back to `replace` for
primitive types and built-in objects (Date, RegExp, etc.). For plain objects,
the `merge-shallow` strategy is also supported.

```ts
// Updates the document with a new value
const result = await db.numbers.update("num1", 42)

// Partial update using shallow merge, only updates the age field
const result = await db.users.update("user1", {
age: 67,
}, {
mergeType: "shallow",
})
// Partial update using merge, only updates the age field
const result = await db.users.update(
"oliver",
{ age: 30 },
{ strategy: "merge" },
)
```

### updateByPrimaryIndex()
Expand All @@ -353,36 +349,39 @@ Update a document by a primary index.

```ts
// Updates a user with username = "oliver" to have age = 56
const result = await db.users.updateByPrimaryIndex("username", "oliver", {
age: 56,
})
const result = await db.users.updateByPrimaryIndex(
"username",
"oliver",
{ age: 56 },
)

// Updates a user document using shallow merge
const result = await db.users.updateByPrimaryIndex("username", "anders", {
age: 89,
}, {
mergeType: "shallow",
})
const result = await db.users.updateByPrimaryIndex(
"username",
"anders",
{ age: 89 },
{ strategy: "merge-shallow" },
)
```

### updateBySecondaryIndex()

Update documents by a secondary index. It takes an optional options argument
that can be used for filtering of documents to be updated, and pagination. If no
Update documents by a secondary index. Takes an optional options argument that
can be used for filtering of documents to be updated, and pagination. If no
options are given, all documents by the given index value will we updated.

```ts
// Updates all user documents with age = 24 and sets age = 67
const { result } = await db.users.updateBySecondaryIndex("age", 24, { age: 67 })

// Updates all user documents where the user's age is 24 and username starts with "o" using shallow merge
// Updates all users where age = 24 and username starts with "o", using shallow merge
const { result } = await db.users.updateBySecondaryIndex(
"age",
24,
{ age: 67 },
{
filter: (doc) => doc.value.username.startsWith("o"),
mergeType: "shallow",
strategy: "merge-shallow",
},
)
```
Expand All @@ -398,14 +397,14 @@ documents in the collection.
// Updates all user documents and sets age = 67
const { result } = await db.users.updateMany({ age: 67 })

// Updates all user documents using deep shallow where the user's age is above 20
// Updates all users where age > 20, using shallow merge
const { result } = await db.users.updateMany({ age: 67 }, {
filter: (doc) => doc.value.age > 20,
mergeType: "shallow",
strategy: "merge-shallow",
})

// Only updates first user document, as username is a primary index
const { result } = await db.users.updateMany({ username: "XuserX" })
// Only updates first user document and fails the rest when username is a primary index
const { result } = await db.users.updateMany({ username: "oliver" })
```

### upsert()
Expand All @@ -417,7 +416,7 @@ document entry, otherwise an id will be generated.

```ts
// Upsert by id
const result1 = await db.users.upsert({
const result = await db.users.upsert({
id: "user_id",
update: { username: "Chris" },
set: {
Expand All @@ -434,7 +433,7 @@ const result1 = await db.users.upsert({
})

// Upsert by index
const result2 = await db.users.upsert({
const result = await db.users.upsert({
index: ["username", "Jack"],
update: { username: "Chris" },
set: {
Expand Down
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"lock": false,
"tasks": {
"check": "deno check --unstable mod.ts ext/zod.ts",
"test": "deno test -A --unstable --trace-ops",
"test": "deno test -A --unstable",
"bench": "deno bench -A --unstable",
"prep": "deno task check && deno fmt && deno lint && deno task test"
},
Expand Down
27 changes: 0 additions & 27 deletions ext/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,6 @@ export const KvObjectSchema: z.ZodType<KvObject> = z.record(
KvValueSchema,
)

export const QueueValueSchema = z.null()
.or(z.string())
.or(z.number())
.or(z.boolean())
.or(z.bigint())
.or(z.instanceof(Deno.KvU64))
.or(LazyKvObjectSchema)
.or(LazyKvArraySchema)
.or(z.instanceof(Int8Array))
.or(z.instanceof(Int16Array))
.or(z.instanceof(Int32Array))
.or(z.instanceof(BigInt64Array))
.or(z.instanceof(Uint8Array))
.or(z.instanceof(Uint16Array))
.or(z.instanceof(Uint32Array))
.or(z.instanceof(BigUint64Array))
.or(z.instanceof(Uint8ClampedArray))
.or(z.instanceof(Float32Array))
.or(z.instanceof(Float64Array))
.or(z.instanceof(ArrayBuffer))
.or(z.date())
.or(z.set(LazyKvValueSchema))
.or(z.map(LazyKvValueSchema, LazyKvValueSchema))
.or(z.instanceof(RegExp))
.or(z.instanceof(DataView))
.or(z.instanceof(Error))

/*****************/
/* */
/* FUNCTIONS */
Expand Down
7 changes: 2 additions & 5 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Expose kvdex
export { kvdex } from "./src/kvdex.ts"

// Expose classes, model and builders
// Expose builders and classes
export { model } from "./src/model.ts"
export { KvDex } from "./src/kvdex.ts"
export { KvDex, kvdex } from "./src/kvdex.ts"
export { Collection, collection } from "./src/collection.ts"
export { AtomicBuilder } from "./src/atomic_builder.ts"
export { Document } from "./src/document.ts"
Expand Down
3 changes: 1 addition & 2 deletions src/atomic_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type {
KvValue,
Operations,
ParseInputType,
QueueValue,
Schema,
SchemaDefinition,
} from "./types.ts"
Expand Down Expand Up @@ -383,7 +382,7 @@ export class AtomicBuilder<
* @param options - Enqueue options, optional.
* @returns A promise resolving to Deno.KvCommitResult.
*/
enqueue(data: QueueValue, options?: EnqueueOptions) {
enqueue(data: KvValue, options?: EnqueueOptions) {
// Prepare and add enqueue operation
const prep = prepareEnqueue(
this.collection._keys.base,
Expand Down
Loading

0 comments on commit 6c444b7

Please sign in to comment.