Skip to content

Instantly share code, notes, and snippets.

@diegomichell
Created February 8, 2024 22:01
Show Gist options
  • Save diegomichell/9b3453a9d0b776edd159a986c1a35c42 to your computer and use it in GitHub Desktop.
Save diegomichell/9b3453a9d0b776edd159a986c1a35c42 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 USER_TABLE_NAME = 'Users';
const PHOTO_TABLE_NAME = 'Photos';
const COMMENT_TABLE_NAME = 'Comments';
const LIKE_TABLE_NAME = 'Likes';
const FOLLOW_TABLE_NAME = 'Follows';
export class PhotoSharingStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// User table
const userTable = new ddb.Table(this, USER_TABLE_NAME, {
partitionKey: {
name: 'userId',
type: ddb.AttributeType.STRING,
},
});
// Photo table
const photoTable = new ddb.Table(this, PHOTO_TABLE_NAME, {
partitionKey: {
name: 'photoId',
type: ddb.AttributeType.STRING,
},
});
// Comment table
const commentTable = new ddb.Table(this, COMMENT_TABLE_NAME, {
partitionKey: {
name: 'photoId',
type: ddb.AttributeType.STRING,
},
sortKey: {
name: 'commentId',
type: ddb.AttributeType.STRING,
},
});
// Like table
const likeTable = new ddb.Table(this, LIKE_TABLE_NAME, {
partitionKey: {
name: 'photoId',
type: ddb.AttributeType.STRING,
},
sortKey: {
name: 'userId',
type: ddb.AttributeType.STRING,
},
});
// Follow table
const followTable = new ddb.Table(this, FOLLOW_TABLE_NAME, {
partitionKey: {
name: 'userId',
type: ddb.AttributeType.STRING,
},
sortKey: {
name: 'followingId',
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
};
userTable.putItem(item);
}
function createPhoto(userId: string, photoId: string, imageURL: string, caption: string) {
const item = {
photoId,
imageURL,
caption,
// ...other photo attributes
};
photoTable.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