1

Can you use dynamoose to have multiple models share a single table? Here are two models that I have where I want them being saved in the same table and differentiate them by their type hash key and id range key.

const Courses = dynamoose.model(
  process.env.CONTENT_TABLE,
  new dynamoose.Schema(
    {
      id: { type: String, rangeKey: true },
      type: { type: String, hashKey: true },
      department: { type: String },
      description: { type: String },
      image: { type: String },
      name: { type: String },
    },
    {
      throughput: 1,
      timestamps: true
    }
  ),
  { update: true }
);

const Teachers = dynamoose.model(
  process.env.CONTENT_TABLE,
  new dynamoose.Schema(
    {
      id: { type: String, rangeKey: true },
      type: { type: String, hashKey: true },
      picture: { type: String },
      name: { type: String },
      bio: { type: String }
    },
    {
      throughput: 1,
      timestamps: true
    }
  ),
  { update: true }
);

These are the methods that I use. I can tell in console.logging the args parameters have what I expect going into the new .... The return statement from thenew Course or new Teachers functions have all the parameters that I added but they don't actually end up in the database. Why is that?

create: ({ args, context }) =>
  new Courses({
    id: shortid.generate(),
    type: course,
    ...args
  }).save()

create: ({ args, context }) => {
  console.log(args);
  return new Teachers({
    id: shortid.generate(),
    type: teacher,
    ...args
  }).save();
coherence
  • 93
  • 1
  • 11

2 Answers2

2

Its possible now with Version 2.0 rewrite of Dynamoose. https://github.com/dynamoose/dynamoose/issues/709

The Model document also mentions that we can give array of schemas to support single table design https://dynamoose.vercel.app/guide/Model

Neetu Das
  • 71
  • 8
0

I figured out what was going on. Dynamoose doesn't support having multiple models for the same table. This might be a feature in the future though. I think the best way to address it would be to merge the models together or use a different ORM.

coherence
  • 93
  • 1
  • 11