node.jsexpressamazon-dynamodbdynamoose

Equivalent of Schema.Types.ObjectId in Dynamoose


I'm creating a snippet manager using Node and React just as a bit of a learning project that we might be able to use where I work.

I had it set up previously using Express, Mongoose and mLab. With the mindset of learning and challenging myself I wanted to move to using AWS' DynamoDB.

I found Dynaamoose which is going to be a big help as the API is almost identical to Mongoose. However, I can't seem to figure out how to recreate what Schema.Types.ObjectId does.

const SnippetSchema = new Schema({
id: {
    type: String,
    default: shortid.generate(),
    hashKey: true
},
user: {
    type: Schema.Types.ObjectId,
    ref: 'users'
},
category: {
    type: Schema.Types.ObjectId,
    ref: 'categories'
},
code: {
    type: String,
    required: true
},
title: {
    type: String,
    required: true
}});

It's not formatted perfectly but essentially how do I get that to reference my user and my category in the same way?


Solution

  • Unlike Mongoose and MongoDB, Dynamoose and DynamoDB doesn't have a default _id property. Therefor there is no such thing as Schema.Types.ObjectId in DynamoDB/Dynamoose.

    It looks like what you are wanting to do is something like .populate.

    Personally for my projects, I just handle all associations manually, using .get.

    In your schema example above, the id property will be your primary key. In DynamoDB primary keys must be unique or else they will override the existing item (unless you put some checks in place).

    From here I think you can build a system to generate id's for each one of your documents, and then just associate it via that id. I normally use String's for this property, but I think it could technically be any supported DynamoDB property type.

    Hopefully this helps get you started. Feel free to comment if it didn't make sense or if you need more help. I'm pretty active as a member on the Dynamoose GitHub Project, so feel free to create issues or stuff on their as well. I try to check the Dynamoose tag on Stack Overflow once per day. My contact information is also pretty easy to find.

    Good luck in your move to Dynamoose and let me know if you run into any other problems or have any other questions!!