基于Dynamose Schema在NoSQL Workbench获取DynamoDB数据模型的问题
问题与解决方案
问题概述
使用Dynamoose库自动生成对应Schema的DynamoDB表时,遇到以下问题:
- 无法在NoSQL Workbench可视化工具中找到该表,也无法导出指定格式的DynamoDB数据模型
- 执行
aws dynamodb describe-table命令返回的表元数据中,缺失Schema里定义的content属性
用到的Schema代码
import { Schema } from 'dynamoose'; export const NotificationSchema = new Schema({ "id": { "type": "String", "hashKey": true, }, "targetId": { "type": "String", "index": { "type": "global", "rangeKey": "status", }, }, "userId": { "type": "String", "index": { "type": "global", "rangeKey": "status", }, }, "content": { "type": "String", }, "status": { "type": "String", }, "createAt": { "type": "String", }, });
执行的命令
aws dynamodb describe-table --table-name aws-nestjs-starter-dev-notification-table --endpoint-url http://localhost:8000
返回的缺失content的元数据
{ "AttributeDefinitions": [ { "AttributeName": "id", "AttributeType": "S" }, { "AttributeName": "targetId", "AttributeType": "S" }, { "AttributeName": "userId", "AttributeType": "S" }, { "AttributeName": "status", "AttributeType": "S" } ], "TableName": "aws-nestjs-starter-dev-notification-table", "KeySchema": [ { "AttributeName": "id", "KeyType": "HASH" } ], "TableStatus": "ACTIVE", "CreationDateTime": {}, "ProvisionedThroughput": { "LastIncreaseDateTime": {}, "LastDecreaseDateTime": {}, "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 1, "WriteCapacityUnits": 1 }, "TableSizeBytes": 381, "ItemCount": 4, "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/aws-nestjs-starter-dev-notification-table", "GlobalSecondaryIndexes": [ { "IndexName": "targetIdGlobalIndex", "KeySchema": [ { "AttributeName": "targetId", "KeyType": "HASH" }, { "AttributeName": "status", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "ALL" }, "IndexStatus": "ACTIVE", "ProvisionedThroughput": { "ReadCapacityUnits": 1, "WriteCapacityUnits": 1 }, "IndexSizeBytes": 378, "ItemCount": 3, "IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/aws-nestjs-starter-dev-notification-table/index/targetIdGlobalIndex" }, { "IndexName": "userIdGlobalIndex", "KeySchema": [ { "AttributeName": "userId", "KeyType": "HASH" }, { "AttributeName": "status", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "ALL" }, "IndexStatus": "ACTIVE", "ProvisionedThroughput": { "ReadCapacityUnits": 1, "WriteCapacityUnits": 1 }, "IndexSizeBytes": 378, "ItemCount": 3, "IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/aws-nestjs-starter-dev-notification-table/index/userIdGlobalIndex" } ] }
属性缺失的原因
DynamoDB是无Schema数据库,describe-table返回的AttributeDefinitions仅包含表键(主键/排序键)和索引键的属性信息。content属于普通非键属性,不会被包含在这个结果里,这是DynamoDB的正常行为,并非Dynamoose的问题。
解决属性显示与Workbench识别问题
1. 让NoSQL Workbench识别所有属性
NoSQL Workbench通过扫描表中现有数据来推断完整的属性列表,所以需要:
- 向表中插入至少一条包含
content、createAt等非键属性的记录 - 在NoSQL Workbench中刷新表的元数据,或重新导入该表
- 此时Workbench就能识别到所有定义的属性,也能正常在可视化工具中显示该表
2. 获取指定格式的数据模型
通过NoSQL Workbench导出
- 确保表中有包含所有属性的样本数据,让Workbench识别完整结构
- 打开NoSQL Workbench,进入「Data Modeler」模块
- 将目标表导入到数据模型中,补充模型元数据(如作者、版本、描述等)
- 导出时选择对应的JSON格式,即可得到类似示例的模型文件
通过代码手动生成
如果需要自动化生成,可以基于Dynamoose Schema和DynamoDB表元数据拼接:
- 从Dynamoose Schema中提取所有属性(键属性+非键属性)的名称和类型
- 调用
describe-table获取表的结构、索引、吞吐量等信息 - 按照目标格式的结构组合这些数据,添加自定义元数据和样本数据即可
内容的提问来源于stack exchange,提问作者Tabares
相关产品推荐
相关产品推荐

