Skip to content

Commit

Permalink
Merge pull request #27 from oliver-oloughlin/patch/fix-atomic-generics
Browse files Browse the repository at this point in the history
Patch/fix atomic generics
  • Loading branch information
oliver-oloughlin authored May 30, 2023
2 parents 84fce12 + 0941afc commit 2ab2f9a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Optional and nullable properties are allowed. If you wish to use Zod, you can
create your Zod object schema and use its type as your model.

```ts
import type { Model } from "https://deno.land/x/[email protected].0/mod.ts"
import type { Model } from "https://deno.land/x/[email protected].1/mod.ts"

interface User extends Model {
username: string
Expand All @@ -35,7 +35,7 @@ The "createDb" function is used for creating a new database instance. It takes a
Deno KV instance and a schema builder function as arguments.

```ts
import { createDb } from "https://deno.land/x/[email protected].0/mod.ts"
import { createDb } from "https://deno.land/x/[email protected].1/mod.ts"

const kv = await Deno.openKv()

Expand Down Expand Up @@ -425,7 +425,7 @@ result will be an object containing: id, versionstamp and all the entries in the
document value.

```ts
import { flatten } from "https://deno.land/x/[email protected].0/mod.ts"
import { flatten } from "https://deno.land/x/[email protected].1/mod.ts"

// We assume the document exists in the KV store
const doc = await db.users.find(123n)
Expand Down
7 changes: 3 additions & 4 deletions src/atomic_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ import {
export class AtomicBuilder<
const TSchema extends Schema,
const TValue extends KvValue,
const TCollection extends Collection<TValue>,
> {
private kv: Deno.Kv
private schema: TSchema
private operations: Operations
private collection: TCollection
private collection: Collection<TValue>

/**
* Create a new AtomicBuilder for building and executing atomic operations in the KV store.
Expand All @@ -45,7 +44,7 @@ export class AtomicBuilder<
constructor(
kv: Deno.Kv,
schema: TSchema,
collection: TCollection,
collection: Collection<TValue>,
operations?: Operations,
) {
this.kv = kv
Expand Down Expand Up @@ -73,7 +72,7 @@ export class AtomicBuilder<
* @returns A new AtomicBuilder instance.
*/
select<const TValue extends KvValue>(
selector: CollectionSelector<TSchema, TValue, Collection<TValue>>,
selector: CollectionSelector<TSchema, TValue>,
) {
return new AtomicBuilder(
this.kv,
Expand Down
14 changes: 6 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import type { AtomicBuilder } from "./atomic_builder.ts"
export type CollectionSelector<
T1 extends Schema,
T2 extends KvValue,
T3 extends Collection<T2>,
> = (schema: T1) => T3
> = (schema: T1) => Collection<T2>

export type AtomicOperationFn = (
op: Deno.AtomicOperation,
Expand Down Expand Up @@ -55,15 +54,15 @@ export type AtomicMutation<T extends KvValue> =
}
| {
type: "sum"
value: Deno.KvU64
value: T extends Deno.KvU64 ? T : never
}
| {
type: "min"
value: Deno.KvU64
value: T extends Deno.KvU64 ? T : never
}
| {
type: "max"
value: Deno.KvU64
value: T extends Deno.KvU64 ? T : never
}
| {
type: "delete"
Expand Down Expand Up @@ -154,10 +153,9 @@ export type DB<TSchema extends Schema> = TSchema & {
*/
atomic: <
const TValue extends KvValue,
const TCollection extends Collection<TValue>,
>(
selector: CollectionSelector<TSchema, TValue, TCollection>,
) => AtomicBuilder<TSchema, TValue, TCollection>
selector: CollectionSelector<TSchema, TValue>,
) => AtomicBuilder<TSchema, TValue>
}

// KV Types
Expand Down

0 comments on commit 2ab2f9a

Please sign in to comment.