diff --git a/apps/whale-api/src/module.database/database.spec/_model.ts b/apps/whale-api/src/module.database/database.spec/_model.ts index b0e5a92fa8..219e1b7c0a 100644 --- a/apps/whale-api/src/module.database/database.spec/_model.ts +++ b/apps/whale-api/src/module.database/database.spec/_model.ts @@ -32,6 +32,7 @@ export const PartitionMapping: ModelMapping = { } export interface Partition extends Model { + id: string a: string b: number @@ -77,6 +78,7 @@ export const PartitionSortMapping: ModelMapping = { } export interface PartitionSort extends Model { + id: string a_partition: string a_sort: string diff --git a/apps/whale-api/src/module.database/model.ts b/apps/whale-api/src/module.database/model.ts index 0e5120931c..369c4fda3f 100644 --- a/apps/whale-api/src/module.database/model.ts +++ b/apps/whale-api/src/module.database/model.ts @@ -4,6 +4,16 @@ export type ModelKey = string | number export type ModelKeyType = 'string' | 'number' +interface ModelIndexString { + type: 'string' + key: (model: M) => string +} + +interface ModelIndexNumber { + type: 'number' + key: (model: M) => number +} + /** * @see Model */ @@ -17,19 +27,14 @@ export interface ModelIndex { /** * Partition key of the model index. */ - partition: { - type: ModelKeyType - key: (model: M) => ModelKey - } + partition: ModelIndexString | ModelIndexNumber + /** * Sort key of the model index, where present indicates composite key. * This attribute must be sorted in lexicographically order, which is a * typical implementation of most key-value store. */ - sort?: { - type: ModelKeyType - key: (model: M) => ModelKey - } + sort?: ModelIndexString | ModelIndexNumber } export interface ModelMapping { diff --git a/apps/whale-api/src/module.database/provider.level/level.database.ts b/apps/whale-api/src/module.database/provider.level/level.database.ts index 4dd9cfcd87..ac6bc9ecff 100644 --- a/apps/whale-api/src/module.database/provider.level/level.database.ts +++ b/apps/whale-api/src/module.database/provider.level/level.database.ts @@ -50,6 +50,9 @@ export abstract class LevelUpDatabase extends Database { * Sub root space for type. */ protected subRoot (mapping: ModelMapping): LevelUp { + // TODO(fuxingloh): sub root indexing might dup too much indexed data as root indexes and sub indexes can be shared. + // We could allow a sub index to act as root. Need to revisit this in the future. + // Other providers like dynamodb where the indexes are manually setup it won't be such an issue. return sub(this.root, mapping.type, { valueEncoding: 'json', keyEncoding: 'binary'