export function keysFromSchema()

in packages/dynamodb-data-marshaller/src/keysFromSchema.ts [16:59]


export function keysFromSchema(schema: Schema): KeySchema {
    const attributes: AttributeTypeMap = {};
    const tableKeys: KeyTypeMap = {};
    const indexKeys: {[key: string]: KeyTypeMap} = {};

    for (const propertyName of Object.keys(schema)) {
        const fieldSchema = schema[propertyName];
        if (
            fieldSchema.type === 'Binary' ||
            fieldSchema.type === 'Custom' ||
            fieldSchema.type === 'Date' ||
            fieldSchema.type === 'Number' ||
            fieldSchema.type === 'String'
        ) {
            const {
                attributeName = propertyName
            } = fieldSchema;

            if (fieldSchema.keyType) {
                attributes[attributeName] = attributeType(fieldSchema);
                tableKeys[attributeName] = fieldSchema.keyType;
            }

            if (
                fieldSchema.indexKeyConfigurations &&
                Object.keys(fieldSchema.indexKeyConfigurations).length > 0
            ) {
                attributes[attributeName] = attributeType(fieldSchema);

                for (const indexName of Object.keys(
                    fieldSchema.indexKeyConfigurations
                )) {
                    if (!(indexName in indexKeys)) {
                        indexKeys[indexName] = {};
                    }
                    indexKeys[indexName][attributeName]
                        = fieldSchema.indexKeyConfigurations[indexName];
                }
            }
        }
    }

    return {attributes, tableKeys, indexKeys};
}