你能使用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 Course或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();发布于 2018-04-26 02:19:50
我知道是怎么回事了。Dynamoose doesn't support having multiple models for the same table。不过,这可能是未来的一个特性。我认为解决这个问题的最好方法是将模型合并在一起,或者使用不同的ORM。
发布于 2021-09-30 21:25:18
随着Dynamoose 2.0版的重写,这是可能的。https://github.com/dynamoose/dynamoose/issues/709
模型文档还提到,我们可以提供模式数组来支持单表设计https://dynamoose.vercel.app/guide/Model
https://stackoverflow.com/questions/50026115
复制相似问题