Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add secondary index to store table #121

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions upload-api/tables/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export const storeTableProps = {
},
// space + link must be unique to satisfy index constraint
primaryIndex: { partitionKey: 'space', sortKey: 'link' },
globalIndexes: {
// @ts-expect-error sst got an typo in its types for TableGlobalIndexProps.partitionKey
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, github actions do not agree

linkSpace: { partitionKey: 'link', sortKey: 'space' }
}
}

/** @type TableProps */
Expand Down
40 changes: 31 additions & 9 deletions upload-api/test/helpers/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,51 @@ export async function createDynamodDb(opts = {}) {
* @typedef {import('@serverless-stack/resources').TableProps} TableProps
*
* @param {TableProps} props
* @returns {Pick<CreateTableCommandInput, 'AttributeDefinitions' | 'KeySchema'>}
* @returns {Pick<CreateTableCommandInput, 'AttributeDefinitions' | 'KeySchema' | 'GlobalSecondaryIndexes'>}
*/
export function dynamoDBTableConfig ({ fields, primaryIndex }) {
export function dynamoDBTableConfig ({ fields, primaryIndex, globalIndexes = {} }) {
if (!primaryIndex || !fields) throw new Error('Expected primaryIndex and fields on TableProps')
const attributes = Object.values(primaryIndex)

const AttributeDefinitions = Object.entries(fields)
.filter(([k]) => attributes.includes(k)) // 'The number of attributes in key schema must match the number of attributes defined in attribute definitions'
.map(([k, v]) => ({
AttributeName: k,
AttributeType: v[0].toUpperCase()
}))

const KeySchema = toKeySchema(primaryIndex)

const GlobalSecondaryIndexes = Object.entries(globalIndexes)
.map(([IndexName, val]) => ({
IndexName,
// @ts-expect-error sst got an typo in its types for TableGlobalIndexProps.partitionKey
KeySchema: toKeySchema(val),
Projection: { ProjectionType: 'ALL' }
}))

return {
AttributeDefinitions,
KeySchema,
GlobalSecondaryIndexes
}
}

/**
* @param {object} index
* @param {string} index.partitionKey
* @param {string} [index.sortKey]
*/
function toKeySchema ({partitionKey, sortKey}) {
const KeySchema = [
{ AttributeName: primaryIndex.partitionKey, KeyType: 'HASH' }
{ AttributeName: partitionKey, KeyType: 'HASH' }
]
if (primaryIndex.sortKey) {
if (sortKey) {
KeySchema.push(
{ AttributeName: primaryIndex.sortKey, KeyType: 'RANGE' }
{ AttributeName: sortKey, KeyType: 'RANGE' }
)
}
return {
AttributeDefinitions,
KeySchema
}
return KeySchema
}

/**
Expand Down