亚马逊迪纳摩DB 数据马歇尔
这个图书馆提供 marshallItem 财务报告和审定财务报表 unmarshallItem 函数
将本地 JavaScript 值转换为 DynamoDB 属性变量并再次返回,
(b) 分别根据一个定义的计划。 许多 JavaScript 值映射
干净到DynamoDB 数据类型,反之亦然,计划允许你无所不失
坚持任何 JavaScript 类型,包括日期、类例和空
字符串.
开始
要使用数据Marxler,首先定义一个描述 应用程序域对象与其序列化形式之间的关系 在迪纳摩DB表格中:
const schema = {
foo: {type: 'Binary'},
bar: {type: 'Boolean'},
baz: {type: 'String'},
quux: {
type: 'Document',
members: {
fizz: {type: 'Set', memberType: 'String'},
buzz: {
type: 'Tuple',
members: [
{
type: 'List',
memberType: {type: 'Set', memberType: 'Number'},
},
{
type: 'Map',
memberType: {type: 'Date'},
}
]
},
},
},
};
此方案可能用于将 JavaScript 值写入 DynamoDB 属性 值 :
import {marshallItem} from '@aws/dynamodb-data-marshaller';
const marshalled = marshallItem(schema, {
foo: Uint8Array.from([0xde, 0xad, 0xbe, 0xef]),
bar: false,
baz: '',
quux: {
fizz: new Set(['a', 'b', 'c']),
buzz: [
[
new Set([1, 2, 3]),
new Set([2, 3, 4]),
new Set([3, 4, 5]),
],
new Map([
['now', new Date()],
['then', new Date(0)],
]),
]
}
});
Schema 也可以用于解除 DynamoDB 属性值返回到 它们最初的 JavaScript 表示:
import {unmarshallItem} from '@aws/dynamodb-data-marshaller';
const unmarshalled = unmarshallItem(schema, {
foo: {B: Uint8Array.from([0xde, 0xad, 0xbe, 0xef])},
bar: {BOOL: false},
baz: {NULL: true},
quux: {
fizz: {SS: ['a', 'b', 'c']},
buzz: {
L: [
L: [
{NS: ['1', '2', '3']},
{NS: ['2', '3', '4']},
{NS: ['3', '4', '5']},
],
M: {
now: {N: '1507189047'},
then: {N: '0'}
},
],
},
},
});
指定密钥
DynamoDB表格必须定义散列键并可能可选择定义范围键. 单位
DynamoDB文档,这些密钥有时被称为 分区 财务报告和审定财务报表
排序 键。 宣布财产为密钥时,添加 keyType
财产的图案(例如: DynamoDB 开发者
指南:
// Table model taken from http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html
const gameScores = {
UserId: {
type: 'String',
keyType: 'HASH'
},
GameTitle: {
type: 'String',
keyType: 'RANGE'
},
TopScore: {type: 'Number'},
TopScoreDateTime: {type: 'Date'},
Wins: {type: 'Number'},
Losses: {type: 'Number'}
};
这个 keyType 属性只能用于序列化为
字符串、数字或二进制属性。 临Τ 'String', (中文(简体) ), 'Number', (中文(简体) ),
财务报告和审定财务报表 'Binary' 属性,可用于 'Date' 财务报告和审定财务报表 'Custom' 属性.
索引密钥使用对象映射索引名称指定为
该值用于给定索引。 继续 gameScores
的索引关键声明 页:1
DynamoDB 全球中学 索引开发者指南
内容如下:
const gameScores = {
UserId: {
type: 'String',
keyType: 'HASH'
},
GameTitle: {
type: 'String',
keyType: 'RANGE',
indexKeyConfigurations: {
GameTitleIndex: 'HASH'
}
},
TopScore: {
type: 'Number',
indexKeyConfigurations: {
GameTitleIndex: 'RANGE'
}
},
TopScoreDateTime: {type: 'Date'},
Wins: {type: 'Number'},
Losses: {type: 'Number'}
};
提供默认值
任何财产计划都可以定义 defaultProvider 函数在
字段为 undefined 输入 marshallItem。该函数必须
返回一个原始 JavaScript 值, 并且不应返回一个已标记的
DynamoDB 属性Value形状.
const uuidV4 = require('uuid/v4');
const schema = {
key: {
type: 'String',
defaultProvider: uuidV4,
keyType: 'HASH',
},
// ...
};
支持类型
任意
将使用 @aws/dynamodb-auto-marshaller
包,在运行时检测给定值的类型.
示例
const anyProperty = {
type: 'Any',
// optionally, you may specify configuration options for the
// @aws/dynamodb-auto-marshaller package's Marshaller class:
unwrapNumbers: false,
onInvalid: 'omit',
onEmpty: 'nullify',
};
二进制
用于 ArrayBuffer 财务报告和审定财务报表 ArrayBufferView 对象,以及节点。 联合来文
缓冲.
可用作表格或索引键.
示例
const binaryProperty = {type: 'Binary'};
布尔
用于 true编号 :false 数值.
示例
const booleanProperty = {type: 'Boolean'};
收藏
说明未类型项目列表。 将汇总组成项目
使用 @aws/dynamodb-auto-marshaller。 。 。 .
示例
const collectionProperty = {
type: 'Collection',
// optionally, you may specify configuration options for the
// @aws/dynamodb-auto-marshaller package's Marshaller class:
unwrapNumbers: false,
onInvalid: 'omit',
onEmpty: 'nullify',
};
自定义
允许使用讲解的集合功能和解析功能。 类型
定义 'Custom' 属性必须包括: marshall 函数
将类型中的 JavaScript 表达式转换为 DynamoDB 属性Value 和
a 一个 unmarshall 将属性Value 转换回 JavaScript 的函数
数值.
可用作表格或索引键.
示例
// This custom property handles strings
const customProperty = {
type: 'Custom',
marshall(input) {
return {S: input};
},
unmarshall(persistedValue) {
return persistedValue.S;
}
};
日期
用于时间数据. 日期将作为时间戳被序列化到 DynamoDB 与DynamoDB的时空功能进行简单整合 因此,时区 信息不会持续存在.
可用作表格或索引键.
示例
const dateProperty = {type: 'Date'};
文档
用于具有自身图案和(可选的)构造器的对象值.
示例
class MyCustomDocument {
method() {
// pass
}
get computedProperty() {
// pass
}
}
class documentSchema = {
fizz: {type: 'String'},
buzz: {type: 'Number'},
pop: {type: 'Date'}
}
const documentProperty = {
type: 'Document',
members: documentSchema,
// optionally, you may specify a constructor to use to create the object
// that will underlie unmarshalled instances. If not specified,
// Object.create(null) will be used.
valueConstructor: MyCustomDocument
};
散开
用于有字符串密钥和无类型值的对象.
示例
const collectionProperty = {
type: 'Hash',
// optionally, you may specify configuration options for the
// @aws/dynamodb-auto-marshaller package's Marshaller class:
unwrapNumbers: false,
onInvalid: 'omit',
onEmpty: 'nullify',
};
列表
用于数组或可移动对象,其元素均为同类型.
示例
const listOfStrings = {
type: 'List',
memberType: {type: 'String'}
};
地图
用于 Map 对象,其值都是同类型.
示例
const mapOfStrings = {
type: 'Map',
memberType: {type: 'String'}
};
无
用于序列化 null。常用作西格值.
示例
const nullProperty = {type: 'Null'};
数目
用来序列数字.
可用作表格或索引键.
示例
const numberProperty = {type: 'Number'};
设定
用来序列化其值都是同一类的集. DynamoDB 允许 组合数字、组合字符串和组合二进制值.
示例
const binarySetProperty = {type: 'Set', memberType: 'Binary'};
const numberSetProperty = {type: 'Set', memberType: 'Number'};
const stringSetProperty = {type: 'Set', memberType: 'String'};
字符串
用于串行.
可用作表格或索引键.
示例
const stringProperty = {type: 'String'};
调皮
用于存储具有特定长度和元素序列的数组.
示例
const tupleProperty = {
type: 'Tuple',
members: [
{type: 'Boolean'},
{type: 'String'}
]
};