Skip to content

Commit b932dcb

Browse files
committed
Initial implementation
1 parent e047d73 commit b932dcb

12 files changed

+1966
-201
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.idea

Diff for: LICENSE

-201
This file was deleted.

Diff for: README.md

+25
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
11
# dynamodb-parser
22
A parser and encoder for DynamoDB records
3+
4+
This converts to and from the AWS DynamoDB record format:
5+
6+
### Usage:
7+
8+
### Installation
9+
```
10+
const {encodeRecord, parseRecord} = require('dynamodb-parser');
11+
```
12+
13+
#### Encoding a plain JavaScript object into a DynamoDB record:
14+
15+
```
16+
const ddbRecord = encodeRecord({foo: 'FOO', bar: {baz: 42}});
17+
18+
// { foo: { S: 'FOO' }, bar: { M: { baz: { N: '42' } } } }
19+
```
20+
21+
#### Parsing a DynamoDB record into a plain JavaScript object:
22+
23+
```
24+
const obj = parseRecord(ddbRecord);
25+
26+
// { foo: 'FOO', bar: { baz: 42 } }
27+
```

Diff for: dist/index.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"use strict";
2+
var __spreadArrays = (this && this.__spreadArrays) || function () {
3+
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
4+
for (var r = Array(s), k = 0, i = 0; i < il; i++)
5+
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
6+
r[k] = a[j];
7+
return r;
8+
};
9+
Object.defineProperty(exports, "__esModule", { value: true });
10+
exports.encodeRecord = exports.encodeAttributeValue = exports.parseRecord = exports.parseAttributeValue = void 0;
11+
var utils_1 = require("./utils");
12+
// Note that the order of 'L', 'NULL' and 'M' is important
13+
var ENCODERS = [
14+
encoder('S', utils_1.isString, function (s) { return s; }),
15+
encoder('SS', utils_1.every(utils_1.isString), function (ss) { return ss; }),
16+
encoder('N', utils_1.isNumber, String),
17+
encoder('NS', utils_1.every(utils_1.isNumber), utils_1.map(String)),
18+
encoder('B', Buffer.isBuffer, utils_1.toBinary),
19+
encoder('BS', utils_1.every(Buffer.isBuffer), utils_1.map(utils_1.toBinary)),
20+
encoder('BOOL', utils_1.isBoolean, function (b) { return b; }),
21+
encoder('L', Array.isArray, utils_1.map(encodeAttributeValue)),
22+
encoder('NULL', function (n) { return n === null; }, function (n) { return true; }),
23+
encoder('M', utils_1.isObject, encodeRecord)
24+
];
25+
var PARSERS = [
26+
parser('B', utils_1.fromBinary),
27+
parser('BS', utils_1.map(utils_1.fromBinary)),
28+
parser('BOOL', function (b) { return b; }),
29+
parser('N', Number),
30+
parser('NS', utils_1.map(Number)),
31+
parser('S', function (s) { return s; }),
32+
parser('SS', function (ss) { return ss; }),
33+
parser('NULL', function (b) { return null; }),
34+
parser('L', utils_1.map(parseAttributeValue)),
35+
parser('M', parseRecord)
36+
];
37+
function encoder(type, match, encode) {
38+
return {
39+
match: match,
40+
call: function (v) {
41+
var _a;
42+
return (_a = {}, _a[type] = encode(v), _a);
43+
}
44+
};
45+
}
46+
function parser(type, parse) {
47+
return {
48+
match: function (av) { return !!(av === null || av === void 0 ? void 0 : av[type]); },
49+
call: function (av) { return parse(av[type]); }
50+
};
51+
}
52+
function transformValue(transforms, t) {
53+
for (var _i = 0, transforms_1 = transforms; _i < transforms_1.length; _i++) {
54+
var _a = transforms_1[_i], match = _a.match, call = _a.call;
55+
if (match(t)) {
56+
return call(t);
57+
}
58+
}
59+
return undefined;
60+
}
61+
function transformRecord(transform, record) {
62+
return Object.assign.apply(Object, __spreadArrays([{}], Object.entries(record)
63+
.map(function (_a) {
64+
var _b;
65+
var k = _a[0], v = _a[1];
66+
return (_b = {}, _b[k] = transform(v), _b);
67+
})));
68+
}
69+
function parseAttributeValue(av) {
70+
return transformValue(PARSERS, av);
71+
}
72+
exports.parseAttributeValue = parseAttributeValue;
73+
function parseRecord(record) {
74+
return transformRecord(parseAttributeValue, record);
75+
}
76+
exports.parseRecord = parseRecord;
77+
function encodeAttributeValue(v) {
78+
return transformValue(ENCODERS, v);
79+
}
80+
exports.encodeAttributeValue = encodeAttributeValue;
81+
function encodeRecord(record) {
82+
return transformRecord(encodeAttributeValue, record);
83+
}
84+
exports.encodeRecord = encodeRecord;

Diff for: dist/utils.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.isObject = exports.isNumber = exports.isBoolean = exports.isString = exports.type = exports.map = exports.every = exports.fromBinary = exports.toBinary = void 0;
4+
function toBinary(buf) {
5+
return buf.toString('base64');
6+
}
7+
exports.toBinary = toBinary;
8+
function fromBinary(s) {
9+
return Buffer.from(s, 'base64');
10+
}
11+
exports.fromBinary = fromBinary;
12+
function every(condition) {
13+
return function (items) { return Array.isArray(items) && items.every(condition); };
14+
}
15+
exports.every = every;
16+
function map(transform) {
17+
return function (items) { return items.map(transform); };
18+
}
19+
exports.map = map;
20+
function type(type) {
21+
return function (t) { return typeof t === type; };
22+
}
23+
exports.type = type;
24+
exports.isString = type('string');
25+
exports.isBoolean = type('boolean');
26+
exports.isNumber = type('number');
27+
exports.isObject = type('object');

0 commit comments

Comments
 (0)