Created
July 5, 2018 15:00
-
-
Save mikhail-angelov/b902eefd9b82fbf1dda8deeba9d923f5 to your computer and use it in GitHub Desktop.
This file contains 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
var commentSchema = new Schema({ | |
postId: { | |
type: String, | |
hashKey: true | |
}, | |
id: { | |
type: String, | |
rangeKey: true, | |
default: shortId.generate | |
}, | |
timestamp: Date | |
commenterId: { | |
type: String, | |
required: true, | |
index: { | |
global: true, | |
rangeKey: 'timestamp', | |
} | |
}, | |
text: String, | |
likes: Number | |
}); | |
var Comment = dynamoose.model('Comment', commentSchema); | |
// Get a single comment using the table key | |
Comment.get( | |
{postId: postId, id: commentId}, // this is the table key (hash and range) | |
function (err, comment) {...} | |
); | |
// Get all comments for a post | |
Comment | |
.query('postId').eq(postId) // only table hash key is used | |
.exec(function (err, allCommentsForPost) {...}); | |
// Get all comments for a commenter | |
Comment | |
.query('commenterId').eq(commenterId) // hash key for index | |
.exec(function (err, allCommentsForCommenter) {...}); | |
// Get all comments for a commenter within last 10 days (uses range key) | |
Comment | |
.query('commenterId').eq(commenterId) | |
.where('timestamp').ge(Date.now() - 8.64e+8) // use .where for range keys | |
.exec(function (err, commentsForCommenterWithin10Days) {...}); | |
// Get all comments for a commenter with likes (uses filters - slower than range) | |
Comment | |
.query('commenterId').eq(commenterId) | |
.filter('likes').gt(0) // use filter when not a range key | |
.exec(function (err, commentsForCommenterWithLikes) {...}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment