[英]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 Course
或new 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.