@@ -199,4 +199,88 @@ Deno.test("indexable_collection - properties", async (t) => {
199
199
)
200
200
} )
201
201
} )
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
+ } )
202
286
} )
0 commit comments