Created
February 8, 2024 22:00
Implementing Photo Sharing Schema in CDK Typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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