function indexDefinitions()

in packages/dynamodb-data-mapper/src/DataMapper.ts [1271:1308]


function indexDefinitions(
    keys: PerIndexKeys,
    options: PerIndexOptions,
    schema: Schema
): {
    GlobalSecondaryIndexes?: GlobalSecondaryIndexList;
    LocalSecondaryIndexes?: LocalSecondaryIndexList;
} {
    const globalIndices: GlobalSecondaryIndexList = [];
    const localIndices: LocalSecondaryIndexList = [];

    for (const IndexName of Object.keys(keys)) {
        const KeySchema = keyTypesToElementList(keys[IndexName]);
        const indexOptions = options[IndexName];
        if (!indexOptions) {
            throw new Error(`No options provided for ${IndexName} index`);
        }

        const indexInfo = {
            IndexName,
            KeySchema,
            Projection: indexProjection(schema, indexOptions.projection),
        };
        if (indexOptions.type === 'local') {
            localIndices.push(indexInfo);
        } else {
            globalIndices.push({
                ...indexInfo,
                ...provisionedThroughput(indexOptions.readCapacityUnits, indexOptions.writeCapacityUnits),
            });
        }
    }

    return {
        GlobalSecondaryIndexes: globalIndices.length ? globalIndices : void 0,
        LocalSecondaryIndexes: localIndices.length ? localIndices : void 0,
    };
}