I'm creating a chat application and using Dynamoose.js. I have a conversations table and a messages table. I want to query on the messages table to find all messages belonging to a conversation. When I make the query, I'm getting the following error:
message: 'Invalid operator used in KeyConditionExpression: IN',
code: 'ValidationException',
Here's what my schemas and query looks like:
conversation.model.js
let schema = {
conversation_id: {
hashKey: true,
type: String,
required: true
},
participants: {
type: Array,
schema: [String],
required: true
},
lastModified: {
type: Date,
required: true,
default: Date.now
}
};
message.model.js
let schema = {
message_id: {
hashKey: true,
type: String,
required: true,
index: {
name: 'conversationIndex',
global: true,
rangeKey: 'conversation_id',
throughput: {'read': 10, 'write': 10}
}
},
conversation_id: {
rangeKey: true,
type: String,
required: true
},
from: {
type: String,
required: true
},
message: {
type: String,
required: true
},
ts: {
type: Date,
required: true,
default: Date.now
}
query
Message.query('conversation_id').in(userConversations).using('conversationIndex').exec((err, messages) => {
// userConversations is an array of conversation_id's
if (err) {
console.error(err);
} else {
}
});
Any idea of where I'm going wrong here? I think it may be related to how I'm creating and using the secondary indexes, but I'm not quite sure. Any help is appreciated, thanks!
DynamoDB queries require you to query for where
hashKey = ______. You can't do a query where you query for a hashKey usingin.On top of that,
conversation_idisn't a hashKey in your index.Kinda hard to tell, but it looks like this is a symptom of DB design that doesn't fit with DynamoDB patterns. I'd suggest looking more into DynamoDB table design, and redesigning your table to better fit within DynamoDB patterns.