首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >dynamoose如何让多个模型共享一个表?

dynamoose如何让多个模型共享一个表?
EN

Stack Overflow用户
提问于 2018-04-25 23:29:36
回答 2查看 1K关注 0票数 1

你能使用dynamoose让多个模型共享一个表吗?这里有两个模型,我希望它们保存在同一个表中,并通过它们的type散列键和id范围键来区分它们。

代码语言:javascript
复制
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 Coursenew Teachers函数的返回语句包含我添加的所有参数,但它们实际上并不在数据库中。为什么会这样呢?

代码语言:javascript
复制
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();
EN

回答 2

Stack Overflow用户

发布于 2018-04-26 02:19:50

我知道是怎么回事了。Dynamoose doesn't support having multiple models for the same table。不过,这可能是未来的一个特性。我认为解决这个问题的最好方法是将模型合并在一起,或者使用不同的ORM。

票数 1
EN

Stack Overflow用户

发布于 2021-09-30 21:25:18

随着Dynamoose 2.0版的重写,这是可能的。https://github.com/dynamoose/dynamoose/issues/709

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50026115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档