Skip to content

Commit

Permalink
packages/modeldb-idb: prep for composite indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
joeltg committed Jan 15, 2025
1 parent ce4b2bf commit b537df1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
9 changes: 7 additions & 2 deletions packages/modeldb-idb/src/ModelDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ export class ModelDB extends AbstractModelDB {

for (const index of model.indexes) {
if (index.length > 1) {
// TODO: we can support these by adding synthetic array values to every object
throw new Error("multi-property indexes not supported yet")
}

recordObjectStore.createIndex(getIndexName(index), index)
const [property] = index
recordObjectStore.createIndex(getIndexName(index), property)
}
}
},
Expand All @@ -61,7 +63,10 @@ export class ModelDB extends AbstractModelDB {
return "idb"
}

private constructor(public readonly db: IDBPDatabase, config: Config) {
private constructor(
public readonly db: IDBPDatabase,
config: Config,
) {
super(config)

for (const model of config.models) {
Expand Down
21 changes: 3 additions & 18 deletions packages/modeldb-idb/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
WhereCondition,
ModelValueWithIncludes,
IncludeExpression,
PrimitiveValue,
} from "@canvas-js/modeldb"

import { getIndexName } from "./utils.js"
Expand All @@ -30,19 +29,6 @@ type ObjectPropertyValue = PropertyValue | PropertyValue[]

type ObjectValue = Record<string, ObjectPropertyValue>

type IndexExpression = PropertyValue | NotExpression | RangeExpression

type CompositePropertyValue = PropertyValue[]
type CompositeNotExpression = { neq: PropertyValue[] | undefined }
type CompositeRangeExpression = {
gt?: PrimitiveValue[]
gte?: PrimitiveValue[]
lt?: PrimitiveValue[]
lte?: PrimitiveValue[]
}

type CompositeIndexExpression = CompositePropertyValue | CompositeNotExpression | CompositeRangeExpression

export class ModelAPI {
public readonly storeName: string
private readonly log: ReturnType<typeof logger>
Expand Down Expand Up @@ -335,12 +321,12 @@ export class ModelAPI {
}

private async *queryIndex(
index: string | string[],
propertyName: string,
storeIndex: IDBPObjectStore<any, any, string, "readonly"> | IDBPIndex<any, any, string, string, "readonly">,
expression: IndexExpression,
expression: PropertyValue | NotExpression | RangeExpression | null,
direction: IDBCursorDirection = "next",
): AsyncIterable<ModelValue> {
const property = this.model.properties.find((property) => property.name === index)
const property = this.model.properties.find((property) => property.name === propertyName)
assert(property !== undefined, "property not found")

if (expression === null) {
Expand Down Expand Up @@ -445,7 +431,6 @@ export class ModelAPI {

return
}

// try to find an index over one of the properties in the where clause
// we can use this to "pre-filter" the results before performing a "table scan"
// choose the index that has the fewest matching entries
Expand Down

0 comments on commit b537df1

Please sign in to comment.