我正在使用 Amazon DynamoDB 作为数据库,并希望采用单表设计。
我的数据类型是用户、帖子、类别、评论等。我可以获取帖子列表,但我需要获取每个帖子的帖子和用户(作者)数据(例如名字、姓氏等)
访问模式预计如下:
通过 ID 获取用户(完成)
- 获取所有用户(完成
userStatusIndex
) - 按类别 ID 获取所有帖子(完成)
- 获取用户的所有帖子:我可以获取帖子列表,
publishStatusIndex
但我也需要作者信息和类别详细信息。(每个帖子的作者信息和类别详细信息) - 获取所有用户的所有帖子:(以上案例)
我正在使用 Dynamoose:
const dynamoose = require("dynamoose");
const { Schema } = dynamoose;
const ServiceSchema = new Schema({
pk: {
type: String,
hashKey: true,
},
sk: {
type: String,
rangeKey: true,
},
id: {
type: String,
},
createdAt: {
type: Date,
default: new Date(),
index: {
global: true,
name: "createdAtIndex",
},
},
updatedAt: {
type: Date,
default: new Date(),
index: {
global: true,
name: "updatedAtIndex",
},
},
deleted: {
type: Boolean,
default: false,
},
firstName: {
type: String,
},
lastName: {
type: String,
},
username: {
type: String,
index: {
global: true,
name: "usernameIndex",
},
},
userStatus: {
type: String,
enum: ["pending", "active"],
index: {
global: true,
name: "userStatusIndex",
},
},
categories: {
type: Array,
schema: [String],
},
title: String,
body: String,
publishStatus: {
type: String,
enum: ["pending", "published"],
index: {
global: true,
name: "publishStatusIndex",
},
},
});
module.exports = dynamoose.model("service", ServiceSchema, {
create: true,
});
数据(更新:我决定替换 pk 和 sk:所以现在 PKUSER#1234
和 SK 是POST#5678
):
我需要的回复帖子是这样的:
{
id: '1234',
title: 'Test',
body: 'This is body',
user: {
id: '5678',
firstName: 'David',
picture: 'myImage.jpg',
points: 12 // **This is very important. And it's the main issue. I need to change this value every day.**
},
categories: [
{ "id": "cat1", "name": "Sport" },
{ "id": "cat2", "name": "Cinema" }
]
}
更新 1: 这是一个社交网络的原型。
更新 2:
我正在使用 GraphQL。我还决定替换 pk 和 sk:所以现在 PK 是USER#1234
,SK 是POST#5678
。