1

您可以使用 dynamoose 让多个模型共享一个表吗?这是我希望将它们保存在同一个表中的两个模型,并通过它们的type哈希键和id范围键来区分它们。

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 }
);

这些是我使用的方法。我可以在 console.logging 中告诉 args 参数有我期望进入new .... new Courseor函数的 return 语句new Teachers包含我添加的所有参数,但它们实际上并没有在数据库中结束。这是为什么?

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();
4

2 回答 2

1

我弄清楚发生了什么事。Dynamoose不支持同一张表有多个模型。不过,这可能是未来的一个功能。我认为解决它的最佳方法是将模型合并在一起或使用不同的 ORM。

于 2018-04-25T18:19:49.580 回答
0

现在可以使用 Dynamoose 的 2.0 版重写。 https://github.com/dynamoose/dynamoose/issues/709

模型文档还提到我们可以提供模式数组来支持单表设计 https://dynamoose.vercel.app/guide/Model

于 2021-09-30T21:25:17.803 回答