Skip to content

Commit 82d939e

Browse files
sorted types
1 parent f0d01cb commit 82d939e

File tree

1 file changed

+59
-56
lines changed

1 file changed

+59
-56
lines changed

src/types.ts

+59-56
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@ import type { Collection } from "./collection.ts"
22
import type { LargeCollection } from "./large_collection.ts"
33
import type { Document } from "./document.ts"
44

5+
// Utility Types
6+
export type CollectionBuilderFn = (
7+
kv: Deno.Kv,
8+
key: KvKey,
9+
) => Collection<KvValue, CollectionOptions<KvValue>>
10+
11+
export type CheckKeyOf<K, T> = K extends keyof T ? T[K] : never
12+
13+
export type KeysOfThatExtend<T1, T2> = keyof {
14+
[K in keyof T1 as T1[K] extends T2 ? K : never]: unknown
15+
}
16+
17+
export type KeysOfThatDontExtend<T1, T2> = keyof {
18+
[K in keyof T1 as T1[K] extends T2 ? never : K]: unknown
19+
}
20+
21+
export type CommitResult<T1 extends KvValue> = {
22+
ok: true
23+
versionstamp: Document<T1>["versionstamp"]
24+
id: KvId
25+
}
26+
27+
export type IdGenerator<T extends KvValue> = (data: T) => KvId
28+
529
// Atomic Builder Types
630
export type CollectionSelector<
731
T1 extends Schema<SchemaDefinition>,
@@ -68,8 +92,6 @@ export type AtomicSetOptions = NonNullable<
6892
>
6993

7094
// Collection Types
71-
export type IdGenerator<T extends KvValue> = (data: T) => KvId
72-
7395
export type CollectionOptions<T extends KvValue> = {
7496
/**
7597
* Set a custom function for automatic id generation.
@@ -82,40 +104,6 @@ export type CollectionKeys = {
82104
idKey: KvKey
83105
}
84106

85-
export type SetOptions = NonNullable<Parameters<Deno.Kv["set"]>["2"]> & {
86-
retry?: number
87-
}
88-
89-
export type ListOptions<T extends KvValue> = Deno.KvListOptions & {
90-
/**
91-
* Filter documents based on predicate.
92-
*
93-
* @param doc - Document
94-
* @returns true or false
95-
*/
96-
filter?: (doc: Document<T>) => boolean
97-
98-
startId?: KvId
99-
100-
endId?: KvId
101-
}
102-
103-
export type CountOptions<T extends KvValue> =
104-
& CountAllOptions
105-
& Pick<ListOptions<T>, "filter">
106-
107-
export type FindOptions = NonNullable<Parameters<Deno.Kv["get"]>[1]>
108-
109-
export type FindManyOptions = NonNullable<Parameters<Deno.Kv["getMany"]>[1]>
110-
111-
export type UpdateManyOptions<T extends KvValue> = ListOptions<T> & SetOptions
112-
113-
export type CommitResult<T1 extends KvValue> = {
114-
ok: true
115-
versionstamp: Document<T1>["versionstamp"]
116-
id: KvId
117-
}
118-
119107
// Indexable Collection Types
120108
export type IndexableCollectionOptions<T extends Model> =
121109
& CollectionOptions<T>
@@ -128,18 +116,8 @@ export type IndexableCollectionKeys = CollectionKeys & {
128116
secondaryIndexKey: KvKey
129117
}
130118

131-
export type CheckKeyOf<K, T> = K extends keyof T ? T[K] : never
132-
133119
export type IndexType = "primary" | "secondary"
134120

135-
export type KeysOfThatExtend<T1, T2> = keyof {
136-
[K in keyof T1 as T1[K] extends T2 ? K : never]: unknown
137-
}
138-
139-
export type KeysOfThatDontExtend<T1, T2> = keyof {
140-
[K in keyof T1 as T1[K] extends T2 ? never : K]: unknown
141-
}
142-
143121
export type IndexRecord<T extends Model> = {
144122
[key in KeysOfThatExtend<T, KvId>]?: IndexType
145123
}
@@ -167,12 +145,40 @@ export type LargeDocumentEntry = {
167145
ids: KvId[]
168146
}
169147

170-
// DB Types
171-
export type CollectionBuilderFn = (
172-
kv: Deno.Kv,
173-
key: KvKey,
174-
) => Collection<KvValue, CollectionOptions<KvValue>>
148+
// Method Option types
149+
export type SetOptions = NonNullable<Parameters<Deno.Kv["set"]>["2"]> & {
150+
retry?: number
151+
}
175152

153+
export type ListOptions<T extends KvValue> = Deno.KvListOptions & {
154+
/**
155+
* Filter documents based on predicate.
156+
*
157+
* @param doc - Document
158+
* @returns true or false
159+
*/
160+
filter?: (doc: Document<T>) => boolean
161+
162+
startId?: KvId
163+
164+
endId?: KvId
165+
}
166+
167+
export type CountOptions<T extends KvValue> =
168+
& CountAllOptions
169+
& Pick<ListOptions<T>, "filter">
170+
171+
export type FindOptions = NonNullable<Parameters<Deno.Kv["get"]>[1]>
172+
173+
export type FindManyOptions = NonNullable<Parameters<Deno.Kv["getMany"]>[1]>
174+
175+
export type UpdateManyOptions<T extends KvValue> = ListOptions<T> & SetOptions
176+
177+
export type CountAllOptions = Pick<Deno.KvListOptions, "consistency">
178+
179+
export type DeleteAllOptions = Pick<Deno.KvListOptions, "consistency">
180+
181+
// Schema Types
176182
export type SchemaDefinition = {
177183
[key: string]: SchemaDefinition | CollectionBuilderFn
178184
}
@@ -183,10 +189,6 @@ export type Schema<T extends SchemaDefinition> = {
183189
: never
184190
}
185191

186-
export type CountAllOptions = Pick<Deno.KvListOptions, "consistency">
187-
188-
export type DeleteAllOptions = Pick<Deno.KvListOptions, "consistency">
189-
190192
// Queue Types
191193
export type QueueMessage<T extends KvValue> = {
192194
collectionKey: KvKey | null
@@ -226,7 +228,7 @@ export type PreparedEnqueue<T extends KvValue> = {
226228
options: DenoKvEnqueueOptions
227229
}
228230

229-
// KV Types
231+
// Data Types
230232
export type UpdateData<T extends KvValue> = T extends KvObject ? Partial<T> : T
231233

232234
export type FlatDocumentData<T extends KvValue> =
@@ -243,6 +245,7 @@ export type DocumentData<T extends KvValue> = {
243245
readonly value: T
244246
}
245247

248+
// KV Types
246249
export type KvVersionstamp<T extends KvValue> = Deno.KvEntry<T>["versionstamp"]
247250

248251
export type KvKey = [Deno.KvKeyPart, ...Deno.KvKey]

0 commit comments

Comments
 (0)