简体   繁体   English

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

[英]How can dynamoose have multiple models share a single table?

Can you use dynamoose to have multiple models share a single table?您可以使用 dynamoose 让多个模型共享一个表吗? 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.这是我希望将它们保存在同一个表中的两个模型,并通过它们的type hash 键和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 }
);

These are the methods that I use.这些是我使用的方法。 I can tell in console.logging the args parameters have what I expect going into the new... .我可以在 console.logging 中告诉 args 参数有我期望进入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. new Coursenew Teachers函数的返回语句具有我添加的所有参数,但它们实际上并没有出现在数据库中。 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 . Dynamoose 不支持同一张桌子具有多个模型 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. 我认为解决此问题的最佳方法是将模型合并在一起或使用其他ORM。

Its possible now with Version 2.0 rewrite of Dynamoose.现在可以使用 Dynamoose 的 2.0 版重写。 https://github.com/dynamoose/dynamoose/issues/709 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 Model 文档也提到我们可以给出模式数组来支持单表设计https://dynamoose.vercel.app/guide/Model

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 dynamoose 进行排序 - How to sort using dynamoose 从具有多个对象模型的单个表中批量获取 DynamdoDB - DynamdoDB batchget from single table with multiple object models 在 DymanoDB/Dynamoose 中,如何设置 map 字符串键和数值的模式? - In DymanoDB/Dynamoose, how can I set a schema of a map of string keys and number values? 如何将 Dynamoose 与 AWS SAM 结合使用? - How to use Dynamoose combined with AWS SAM? 如何与多个客户端共享 quicksight 嵌入式仪表板 - how to share quicksight embeded dashboard with multiple clients 如何在 kotlin 中为单个 recyclerview 适配器使用两个模型 - How to use two models for a single recyclerview adapter in kotlin 单个用户可以在 Firebase 身份验证中将多个电话号码链接到他的帐户吗? - Can a single user have multiple phone numbers linked to his account in Firebase auth? 如何将一个表中的多行插入到另一个表的单行的结构列中? - How do I insert multiple rows from one table into a struct column of a single row of another table? 单个 GKE ingress controller 是否可以拥有多个 ingress 资源 - Is it possible to have multiple ingress resources with a single GKE ingress controller 我如何在一个列表中显示多个 collections - How can i show multiple collections in a single list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM