4

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?

Luke Todd
  • 123
  • 1
  • 6

2 Answers2

2

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!!

Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
  • Thank you for the speedy response that was super helpful! I went with using a `userId` and a `categoryId` and then using `.get` for what I needed :) – Luke Todd Oct 07 '18 at 15:18
  • @LukeTodd Awesome! Glad to hear it worked for you. I’m pretty sure with that setup you can also use the populate method. But I actually haven’t worked with it much. Again feel free to contact me with any questions. Good luck in your future Dynamoose adventures! – Charlie Fish Oct 07 '18 at 15:47
  • Those links are dead (and on your site too) – Developer Dave Jul 01 '20 at 00:56
  • @DeveloperDave Just updated those links. Keep in mind, this answer was written in 2018. So it might be slightly outdated. I linked the links back to v1 of the documentation. However since then v2 has been released. The answer was completely valid at time of writing it. And all of the content in the answer is self contained (not unnecessarily to outside links). So it doesn't violate any Stack Overflow guidelines about only using links in answers. – Charlie Fish Jul 01 '20 at 01:40
  • Gotcha. Don't overreact or read too much into that comment though. I'm not attacking your work nor implying you're violating any stackoverflow guidelines (no idea where that came from). I've simply noticed in the docs you published at dynamoosejs.com there are several dead links to sister pages (ie, when you click on "read more about this here" on the Configure menu). Since you have another one here, figured you'd want to know about it. – Developer Dave Jul 07 '20 at 22:30
  • @DeveloperDave Totally get it. It's really difficult to maintain old documentation/sites haha. Hopefully that updated link works well for you! – Charlie Fish Jul 08 '20 at 04:29
  • Yup. Great job on it btw. Definitely helps those of us that don't want to be too tightly coupled to the AWS SDK. Coding against an ORM makes it easier to reuse code when needing to point it at a different database (Mongo, MySQL, etc) – Developer Dave Jul 09 '20 at 07:34
0

I guess you want like this:

const SnippetSchema = new Schema({
   id: {
     type: String,
     default: shortid.generate(),
     hashKey: true
   },
   parent: User,
   category:{
     type: String,
     required: true,
     index: {
       global: true
     }
   },
   code: {
     type: String,
     required: true
   },
   title: {
     type: String,
     required: true
   }
});

So when .get(id)

will return all data with the object called "parent", which contains the User data

Luciano
  • 358
  • 1
  • 5
  • 17