Skip to content

Commit

Permalink
Merge pull request #57 from oliver-oloughlin/feature/minor-update-README
Browse files Browse the repository at this point in the history
Feature/minor update readme
  • Loading branch information
oliver-oloughlin authored Aug 10, 2023
2 parents 9ee28d9 + 527a22c commit 89d6444
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ create your Zod object schema and use its inferred type as your model.
**_NOTE_:** When using interfaces instead of types, sub-interfaces must also extend the Model type.

```ts
import type { Model } from "https://deno.land/x/kvdex@v0.9.4/mod.ts"
import type { Model } from "https://deno.land/x/kvdex@v0.9.5/mod.ts"

interface User extends Model {
username: string
Expand All @@ -81,7 +81,7 @@ interface User extends Model {
Deno KV instance and a schema definition as arguments.

```ts
import { kvdex } from "https://deno.land/x/kvdex@v0.9.4/mod.ts"
import { kvdex } from "https://deno.land/x/kvdex@v0.9.5/mod.ts"

const kv = await Deno.openKv()

Expand Down Expand Up @@ -585,14 +585,14 @@ index entries.
### Without checking

```ts
// Deletes and adds an entry to the bigints collection
// Deletes and adds an entry to the numbers collection
const result1 = await db
.atomic((schema) => schema.numbers)
.delete("id_1")
.set("id_2", 100)
.commit()

// Adds 2 new entries to the strings collection and 1 new entry to the users collection
// Adds 2 new entries to the numbers collection and 1 new entry to the users collection
const result2 = await db
.atomic((schema) => schema.numbers)
.add(1)
Expand Down Expand Up @@ -633,7 +633,7 @@ const result3 = await db
### With checking

```ts
// Only adds 10 to the value when it has not been changed after being read
// Only adds 10 to the value when it has not been changed since being read
let result = null
while (!result || !result.ok) {
const { id, versionstamp, value } = await db.numbers.find("id")
Expand All @@ -660,7 +660,7 @@ type Model. Only flattens the first layer of the document, meaning the result wi
document value.

```ts
import { flatten } from "https://deno.land/x/kvdex@v0.9.4/mod.ts"
import { flatten } from "https://deno.land/x/kvdex@v0.9.5/mod.ts"

// We assume the document exists in the KV store
const doc = await db.users.find(123n)
Expand Down
10 changes: 10 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
// Expose kvdex and utils functions
export { kvdex } from "./src/db.ts"
export * from "./src/utils.ts"

// Expose classes
export { Collection } from "./src/collection.ts"
export { IndexableCollection } from "./src/indexable_collection.ts"
export { LargeCollection } from "./src/large_collection.ts"
export { AtomicBuilder } from "./src/atomic_builder.ts"
export { CollectionBuilderContext } from "./src/collection_builder.ts"

// Expose all types
export type * from "./src/db.ts"
export type * from "./src/collection.ts"
export type * from "./src/indexable_collection.ts"
export type * from "./src/large_collection.ts"
export type * from "./src/collection_builder.ts"
export type * from "./src/atomic_builder.ts"
export type * from "./src/utils.internal.ts"
Expand Down
4 changes: 2 additions & 2 deletions src/large_collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class LargeCollection<

// This should never happen
if (!json) {
this.delete(id)
await this.delete(id)
return null
}

Expand Down Expand Up @@ -221,7 +221,7 @@ export class LargeCollection<

// This should never happen
if (!json) {
this.delete(id)
await this.delete(id)
return
}

Expand Down
43 changes: 43 additions & 0 deletions test/tests/large_collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assert } from "../deps.ts"
import {
db,
generateLargeDatas,
generateNumbers,
kv,
LargeData,
reset,
Expand Down Expand Up @@ -38,6 +39,48 @@ Deno.test("large_collection", async (t) => {
})
})

// Test validity of values in large colelctions
await t.step("values", async (t) => {
await t.step("Should allow empty string as value", async () => {
await useTemporaryKv(async (kv) => {
const db = kvdex(kv, {
strings: (ctx) => ctx.largeCollection<string>().build(),
})

const cr = await db.strings.add("")
assert(cr.ok)

const count = await db.strings.count()
assert(count === 1)

const doc = await db.strings.find(cr.id)
assert(doc !== null)
assert(typeof doc.value === "string")
assert(doc.value === "")
})
})

await t.step("Should allow array of numbers as value", async () => {
await useTemporaryKv(async (kv) => {
const db = kvdex(kv, {
arrs: (ctx) => ctx.largeCollection<number[]>().build(),
})

const numbers = generateNumbers(100_000)
const cr = await db.arrs.add(numbers)
assert(cr.ok)

const count = await db.arrs.count()
assert(count === 1)

const doc = await db.arrs.find(cr.id)
assert(doc !== null)
assert(doc.value instanceof Array)
assert(typeof doc.value[0] === "number")
})
})
})

// Test "add" method
await t.step("add", async (t) => {
await t.step(
Expand Down

0 comments on commit 89d6444

Please sign in to comment.