Skip to content

Instantly share code, notes, and snippets.

@diegomichell
Created February 8, 2024 22:00
Show Gist options
  • Save diegomichell/9ef2e4b44cde7c9b9838e5b9a243fc01 to your computer and use it in GitHub Desktop.
Save diegomichell/9ef2e4b44cde7c9b9838e5b9a243fc01 to your computer and use it in GitHub Desktop.
Implementing Photo Sharing Schema in CDK Typescript
import * as cdk from '@aws-cdk/core';
import * as ddb from '@aws-cdk/aws-dynamodb';
const TABLE_NAME = 'PhotoSharing';
export class PhotoSharingStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const table = new ddb.Table(this, TABLE_NAME, {
partitionKey: {
name: 'userId',
type: ddb.AttributeType.STRING,
},
// Optional sort key for improved querying
sortKey: {
name: 'timestamp',
type: ddb.AttributeType.NUMBER,
},
});
// Define attributes for each entity
table.addGlobalSecondaryIndex({
indexName: 'PhotoIndex',
partitionKey: {
name: 'userId',
type: ddb.AttributeType.STRING,
},
sortKey: {
name: 'photoId',
type: ddb.AttributeType.STRING,
},
});
// Example item creation functions (modify based on your needs)
function createUser(userId: string, username: string, email: string) {
const item = {
userId,
username,
email,
// ...other user attributes
};
table.putItem(item);
}
function createPhoto(userId: string, photoId: string, imageURL: string, caption: string) {
const item = {
userId,
photoId,
imageURL,
caption,
// ...other photo attributes
};
table.putItem(item);
}
// ... similar functions for comments, likes, and follows
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment