简体   繁体   中英

How can dynamoose have multiple models share a single table?

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 the new 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();

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM