Skip to content

Commit

Permalink
All-pages versions of multiple-entity operations
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebroberts committed Sep 26, 2023
1 parent 3b04083 commit acd99fc
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 26 deletions.
20 changes: 12 additions & 8 deletions src/lib/internal/multipleEntities/multipleEntityQueryOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export async function queryMultipleByPk<TKeyItem extends TPKSource & TSKSource,
contexts: EntityContextsByEntityType,
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TPKSource,
options: AdvancedQueryOnePageOptions
allPages: boolean,
options: AdvancedQueryOnePageOptions = {}
): Promise<MultipleEntityCollectionResponse> {
const keyEntityContext = findKeyEntityContext(contexts, keyEntity)
return await queryMultipleWithCriteria(
Expand All @@ -29,7 +30,7 @@ export async function queryMultipleByPk<TKeyItem extends TPKSource & TSKSource,
options,
`${keyEntityContext.metaAttributeNames.pk} = :pk`,
expressionAttributeParams({ ':pk': keyEntityContext.entity.pk(pkSource) }),
false
allPages
)
}

Expand All @@ -38,7 +39,8 @@ export async function queryMultipleBySkRange<TKeyItem extends TPKSource & TSKSou
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TPKSource,
queryRange: SkQueryRange,
options: AdvancedQueryOnePageOptions
allPages: boolean,
options: AdvancedQueryOnePageOptions = {}
) {
const keyEntityContext = findKeyEntityContext(contexts, keyEntity)
if (!keyEntityContext.metaAttributeNames.sk)
Expand All @@ -57,7 +59,7 @@ export async function queryMultipleBySkRange<TKeyItem extends TPKSource & TSKSou
'#sk': keyEntityContext.metaAttributeNames.sk
}
),
false
allPages
)
}

Expand All @@ -70,7 +72,8 @@ export async function queryMultipleByGsiPk<
contexts: EntityContextsByEntityType,
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TGSIPKSource,
options: AdvancedGsiQueryOnePageOptions
allPages: boolean,
options: AdvancedGsiQueryOnePageOptions = {}
): Promise<MultipleEntityCollectionResponse> {
const keyEntityContext = findKeyEntityContext(contexts, keyEntity)
const gsiDetails = findGsiDetails(keyEntityContext, options)
Expand All @@ -84,7 +87,7 @@ export async function queryMultipleByGsiPk<
IndexName: gsiDetails.tableIndexName,
...expressionAttributeParams({ ':pk': gsiDetails.generators.pk(pkSource) })
},
false
allPages
)
}

Expand All @@ -98,7 +101,8 @@ export async function queryMultipleByGsiSkRange<
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TGSIPKSource,
queryRange: SkQueryRange,
options: AdvancedGsiQueryOnePageOptions
allPages: boolean,
options: AdvancedGsiQueryOnePageOptions = {}
) {
const keyEntityContext = findKeyEntityContext(contexts, keyEntity)
const gsiDetails = findGsiDetails(keyEntityContext, options)
Expand All @@ -123,7 +127,7 @@ export async function queryMultipleByGsiSkRange<
}
)
},
false
allPages
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { AdvancedScanOnePageOptions } from '../../singleEntityAdvancedOperations

export async function scanMultiple(
contextsByEntityType: EntityContextsByEntityType,
options: AdvancedScanOnePageOptions
allPages: boolean,
options: AdvancedScanOnePageOptions = {}
): Promise<MultipleEntityCollectionResponse> {
const defaultEntityContext = Object.values(contextsByEntityType)[0]

Expand All @@ -19,7 +20,7 @@ export async function scanMultiple(

return await performMultipleEntityOperationAndParse(
contextsByEntityType,
configureScanOperation(defaultEntityContext, options, false),
configureScanOperation(defaultEntityContext, options, allPages),
defaultEntityContext
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { MultipleEntityCollectionResponse, MultipleEntityOperations } from '../.
import { createEntityContext } from '../entityContext'
import { scanMultiple } from './multipleEntityScanOperation'
import {
AdvancedGsiQueryAllOptions,
AdvancedGsiQueryOnePageOptions,
AdvancedQueryAllOptions,
AdvancedQueryOnePageOptions,
AdvancedScanAllOptions,
AdvancedScanOnePageOptions
} from '../../singleEntityAdvancedOperations'
import {
Expand All @@ -30,27 +33,62 @@ export function tableBackedMultipleEntityOperations(
)

return {
async queryAllByPk<TKeyItem extends TPKSource & TSKSource, TPKSource, TSKSource>(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TPKSource,
options?: AdvancedQueryAllOptions
) {
return queryMultipleByPk(contextsByEntityType, keyEntity, pkSource, true, options)
},
async queryOnePageByPk<TKeyItem extends TPKSource & TSKSource, TPKSource, TSKSource>(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TPKSource,
options: AdvancedQueryOnePageOptions = {}
): Promise<MultipleEntityCollectionResponse> {
return await queryMultipleByPk(contextsByEntityType, keyEntity, pkSource, options)
options?: AdvancedQueryOnePageOptions
) {
return queryMultipleByPk(contextsByEntityType, keyEntity, pkSource, false, options)
},
async queryAllByPkAndSk<TKeyItem extends TPKSource & TSKSource, TPKSource, TSKSource>(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TPKSource,
queryRange: SkQueryRange,
options?: AdvancedQueryAllOptions
) {
return queryMultipleBySkRange(contextsByEntityType, keyEntity, pkSource, queryRange, true, options)
},
async queryOnePageByPkAndSk<TKeyItem extends TPKSource & TSKSource, TPKSource, TSKSource>(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TPKSource,
queryRange: SkQueryRange,
options: AdvancedQueryOnePageOptions = {}
): Promise<MultipleEntityCollectionResponse> {
return await queryMultipleBySkRange(contextsByEntityType, keyEntity, pkSource, queryRange, options)
options?: AdvancedQueryOnePageOptions
) {
return queryMultipleBySkRange(contextsByEntityType, keyEntity, pkSource, queryRange, false, options)
},
async queryAllWithGsiByPk<TKeyItem extends TPKSource & TSKSource, TPKSource, TSKSource, TGSIPKSource>(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TGSIPKSource,
options?: AdvancedGsiQueryAllOptions
) {
return queryMultipleByGsiPk(contextsByEntityType, keyEntity, pkSource, true, options)
},
async queryOnePageWithGsiByPk<TKeyItem extends TPKSource & TSKSource, TPKSource, TSKSource, TGSIPKSource>(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TGSIPKSource,
options: AdvancedGsiQueryOnePageOptions = {}
options?: AdvancedQueryOnePageOptions
) {
return queryMultipleByGsiPk(contextsByEntityType, keyEntity, pkSource, false, options)
},
async queryAllWithGsiByPkAndSk<
TKeyItem extends TPKSource & TSKSource,
TPKSource,
TSKSource,
TGSIPKSource
>(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TGSIPKSource,
queryRange: SkQueryRange,
options?: AdvancedGsiQueryAllOptions
): Promise<MultipleEntityCollectionResponse> {
return await queryMultipleByGsiPk(contextsByEntityType, keyEntity, pkSource, options)
return queryMultipleByGsiSkRange(contextsByEntityType, keyEntity, pkSource, queryRange, true, options)
},
async queryOnePageWithGsiByPkAndSk<
TKeyItem extends TPKSource & TSKSource,
Expand All @@ -61,12 +99,15 @@ export function tableBackedMultipleEntityOperations(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TGSIPKSource,
queryRange: SkQueryRange,
options: AdvancedGsiQueryOnePageOptions = {}
options?: AdvancedGsiQueryOnePageOptions
): Promise<MultipleEntityCollectionResponse> {
return await queryMultipleByGsiSkRange(contextsByEntityType, keyEntity, pkSource, queryRange, options)
return queryMultipleByGsiSkRange(contextsByEntityType, keyEntity, pkSource, queryRange, false, options)
},
async scanAll(options?: AdvancedScanAllOptions): Promise<MultipleEntityCollectionResponse> {
return await scanMultiple(contextsByEntityType, true, options)
},
async scanOnePage(options: AdvancedScanOnePageOptions = {}) {
return await scanMultiple(contextsByEntityType, options)
async scanOnePage(options?: AdvancedScanOnePageOptions) {
return await scanMultiple(contextsByEntityType, false, options)
}
}
}
32 changes: 31 additions & 1 deletion src/lib/multipleEntityOperations.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,70 @@
import { DynamoDBValues, Entity } from './entities'
import {
AdvancedGsiQueryAllOptions,
AdvancedGsiQueryOnePageOptions,
AdvancedQueryAllOptions,
AdvancedQueryOnePageOptions,
AdvancedScanAllOptions,
AdvancedScanOnePageOptions,
ConsumedCapacitiesMetadata
} from './singleEntityAdvancedOperations'
import { SkQueryRange } from './singleEntityOperations'

// TODOEventually - all pages versions
export interface MultipleEntityOperations {
queryAllByPk<TKeyItem extends TPKSource & TSKSource, TPKSource, TSKSource>(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TPKSource,
options?: AdvancedQueryAllOptions
): Promise<MultipleEntityCollectionResponse>

queryOnePageByPk<TKeyItem extends TPKSource & TSKSource, TPKSource, TSKSource>(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TPKSource,
options?: AdvancedQueryOnePageOptions
): Promise<MultipleEntityCollectionResponse>

queryAllByPkAndSk<TKeyItem extends TPKSource & TSKSource, TPKSource, TSKSource>(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TPKSource,
queryRange: SkQueryRange,
options?: AdvancedQueryAllOptions
): Promise<MultipleEntityCollectionResponse>

queryOnePageByPkAndSk<TKeyItem extends TPKSource & TSKSource, TPKSource, TSKSource>(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TPKSource,
queryRange: SkQueryRange,
options?: AdvancedQueryOnePageOptions
): Promise<MultipleEntityCollectionResponse>

queryAllWithGsiByPk<TKeyItem extends TPKSource & TSKSource, TPKSource, TSKSource, TGSIPKSource>(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TGSIPKSource,
options?: AdvancedGsiQueryAllOptions
): Promise<MultipleEntityCollectionResponse>

queryOnePageWithGsiByPk<TKeyItem extends TPKSource & TSKSource, TPKSource, TSKSource, TGSIPKSource>(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TGSIPKSource,
options?: AdvancedGsiQueryOnePageOptions
): Promise<MultipleEntityCollectionResponse>

queryAllWithGsiByPkAndSk<TKeyItem extends TPKSource & TSKSource, TPKSource, TSKSource, TGSIPKSource>(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TGSIPKSource,
queryRange: SkQueryRange,
options?: AdvancedGsiQueryAllOptions
): Promise<MultipleEntityCollectionResponse>

queryOnePageWithGsiByPkAndSk<TKeyItem extends TPKSource & TSKSource, TPKSource, TSKSource, TGSIPKSource>(
keyEntity: Entity<TKeyItem, TPKSource, TSKSource>,
pkSource: TGSIPKSource,
queryRange: SkQueryRange,
options?: AdvancedGsiQueryOnePageOptions
): Promise<MultipleEntityCollectionResponse>

scanAll(options?: AdvancedScanAllOptions): Promise<MultipleEntityCollectionResponse>

scanOnePage(options?: AdvancedScanOnePageOptions): Promise<MultipleEntityCollectionResponse>
}

Expand Down
12 changes: 9 additions & 3 deletions test/integration/integrationTests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ describe('standard single table', () => {
expect(
await store
.forMultiple([DUCK_ENTITY, CHICKEN_ENTITY])
.queryOnePageWithGsiByPk(DUCK_ENTITY, { coop: 'bristol' })
.queryAllWithGsiByPk(DUCK_ENTITY, { coop: 'bristol' })
).toEqual({
itemsByEntityType: {
chicken: [ginger],
Expand Down Expand Up @@ -929,7 +929,7 @@ describe('standard single table', () => {

// Multiple Entity API
expect(
await store.forMultiple([DOG_ENTITY]).queryOnePageByPk(DOG_ENTITY, { farm: 'Sunflower Farm' })
await store.forMultiple([DOG_ENTITY]).queryAllByPk(DOG_ENTITY, { farm: 'Sunflower Farm' })
).toEqual({
itemsByEntityType: {
dog: [chesterDog]
Expand Down Expand Up @@ -969,7 +969,7 @@ describe('standard single table', () => {

// query GSI single entity with multiple entity api where multiple returned from table
expect(
await store.forMultiple([DUCK_ENTITY]).queryOnePageWithGsiByPk(DUCK_ENTITY, { coop: 'bristol' })
await store.forMultiple([DUCK_ENTITY]).queryAllWithGsiByPk(DUCK_ENTITY, { coop: 'bristol' })
).toEqual({
itemsByEntityType: {
duck: [waddles]
Expand Down Expand Up @@ -1021,6 +1021,12 @@ describe('standard single table', () => {
chicken: [ginger]
}
})
expect(await storeWithScans.forMultiple([SHEEP_ENTITY, CHICKEN_ENTITY]).scanAll()).toEqual({
itemsByEntityType: {
sheep: [shaunTheSheep],
chicken: [ginger]
}
})

// Scan multiple entity API when some entities not specified
expect(await storeWithScans.forMultiple([SHEEP_ENTITY]).scanOnePage()).toEqual({
Expand Down

0 comments on commit acd99fc

Please sign in to comment.