Skip to content

Commit 6caaee8

Browse files
Merge pull request #94 from oliver-oloughlin/feature/optional-indices
feat: optional indices + tests
2 parents 1a40f85 + bb04e61 commit 6caaee8

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export type IndexableCollectionKeys = CollectionKeys & {
178178
export type IndexType = "primary" | "secondary"
179179

180180
export type IndexRecord<T extends KvObject> = {
181-
[key in KeysOfThatExtend<T, KvId>]?: IndexType
181+
[key in KeysOfThatExtend<T, KvId | undefined>]?: IndexType
182182
}
183183

184184
export type PrimaryIndexKeys<T1 extends KvObject, T2 extends IndexRecord<T1>> =

tests/indexable_collection/properties.test.ts

+84
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,88 @@ Deno.test("indexable_collection - properties", async (t) => {
199199
)
200200
})
201201
})
202+
203+
await t.step("Should allow optional indices", async () => {
204+
await useKv(async (kv) => {
205+
const db = kvdex(kv, {
206+
i: indexableCollection(
207+
model<{
208+
oblPrimary: string
209+
oblSecondary: number
210+
optPrimary?: string
211+
optSecondary?: number
212+
check?: Date
213+
}>(),
214+
{
215+
indices: {
216+
oblPrimary: "primary",
217+
oblSecondary: "secondary",
218+
optPrimary: "primary",
219+
optSecondary: "secondary",
220+
},
221+
},
222+
),
223+
})
224+
225+
const cr1 = await db.i.add({
226+
oblPrimary: "oblPrimary1",
227+
oblSecondary: 10,
228+
})
229+
230+
const cr2 = await db.i.add({
231+
oblPrimary: "oblPrimary2",
232+
oblSecondary: 10,
233+
optPrimary: "optPrimary2",
234+
optSecondary: 20,
235+
})
236+
237+
assert(cr1.ok)
238+
assert(cr2.ok)
239+
240+
const byOptPrimary2 = await db.i.findByPrimaryIndex(
241+
"optPrimary",
242+
"optPrimary2",
243+
)
244+
const byOptSecondary2 = await db.i.findBySecondaryIndex(
245+
"optSecondary",
246+
20,
247+
)
248+
249+
assert(byOptPrimary2?.id === cr2.id)
250+
assert(byOptSecondary2.result.length === 1)
251+
assert(byOptSecondary2.result.some((i) => i.id === cr2.id))
252+
253+
const cr3 = await db.i.add({
254+
oblPrimary: "oblPrimary3",
255+
oblSecondary: 10,
256+
optPrimary: "optPrimary2",
257+
optSecondary: 20,
258+
})
259+
260+
assert(!cr3.ok)
261+
262+
const cr4 = await db.i.add({
263+
oblPrimary: "oblPrimary4",
264+
oblSecondary: 10,
265+
optPrimary: "optPrimary4",
266+
optSecondary: 20,
267+
})
268+
269+
assert(cr4.ok)
270+
271+
const byOptPrimary4 = await db.i.findByPrimaryIndex(
272+
"optPrimary",
273+
"optPrimary4",
274+
)
275+
const byOptSecondary4 = await db.i.findBySecondaryIndex(
276+
"optSecondary",
277+
20,
278+
)
279+
280+
assert(byOptPrimary4?.id === cr4.id)
281+
assert(byOptSecondary4.result.length === 2)
282+
assert(byOptSecondary4.result.some((i) => i.id === cr2.id))
283+
assert(byOptSecondary4.result.some((i) => i.id === cr4.id))
284+
})
285+
})
202286
})

0 commit comments

Comments
 (0)