-
Notifications
You must be signed in to change notification settings - Fork 1
/
serializer.ts
159 lines (142 loc) · 4.08 KB
/
serializer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import * as AWSDDB from "@aws-sdk/client-dynamodb";
import Joi from "joi";
import type { PlainObject } from "./types";
import { getItemSchemaFromObjectSchema } from "./joiReflection";
class Serializer {
constructor(private schema: Joi.ObjectSchema, private version: number) {}
private string(value: string): AWSDDB.AttributeValue {
return { S: value };
}
private number(value: number): AWSDDB.AttributeValue {
return { N: value.toString() };
}
private boolean(value: boolean): AWSDDB.AttributeValue {
return { BOOL: value };
}
private date(value: Date): AWSDDB.AttributeValue {
return this.string(value.toISOString());
}
private object(
o: PlainObject,
schema: Joi.ObjectSchema
): AWSDDB.AttributeValue {
if (o === null) {
return { NULL: true };
}
if (o instanceof Buffer) {
throw new Error("Buffers not yet supported yet");
}
return {
M: this.toDynamoMap(o, schema),
};
}
private array(
a: any[],
schema: Joi.ArraySchema
):
| {
SS: string[];
}
| {
L: AWSDDB.AttributeValue[];
}
| undefined {
if (a.length === 0) {
return undefined;
}
const meta = schema.describe().metas?.[0]?.type;
switch (meta) {
case "SS":
return { SS: a };
default:
return {
L: a
.map((item) => this.any(item))
.filter(Boolean) as AWSDDB.AttributeValue[],
};
}
}
private any(value: any): AWSDDB.AttributeValue | undefined {
const type = typeof value;
if (type === "boolean") {
return this.boolean(value);
}
if (type === "number") {
return this.number(value);
}
if (type === "string") {
return this.string(value);
}
if (type === "object") {
if (Array.isArray(value)) {
return this.array(value, Joi.array().items(Joi.any()));
}
if (value === null) {
return this.object(value, Joi.object());
}
// Build an object schema to look through made from the known keys
const objectSchema = Object.keys(value).reduce(
(structure, key) => ({
...structure,
[key]: Joi.any(),
}),
{}
);
return this.object(value, Joi.object(objectSchema));
}
throw new Error(`Cannot serialize "${type}" to DynamoDB`);
}
toDynamoValue(
userValue: any,
schemaKey: string | Joi.AnySchema
): AWSDDB.AttributeValue | undefined {
const schema =
typeof schemaKey === "string"
? getItemSchemaFromObjectSchema(this.schema, schemaKey)
: schemaKey;
if (userValue === undefined) {
return undefined;
}
if (schema.type === "object") {
return this.object(userValue, schema as Joi.ObjectSchema);
} else if (schema.type === "array") {
return this.array(userValue, schema as Joi.ArraySchema);
} else if (schema.type === "number") {
return this.number(userValue);
} else if (schema.type === "string") {
return this.string(userValue);
} else if (schema.type === "boolean") {
return this.boolean(userValue);
} else if (schema.type === "any") {
return this.any(userValue);
} else if (schema.type === "date") {
return this.date(userValue);
} else {
throw new Error(`Unable to handle Joi schema type: "${schema.type}"`);
}
}
private toDynamoMap(
userObj: PlainObject,
objectSchema: Joi.ObjectSchema = this.schema
): Record<string, AWSDDB.AttributeValue> {
return Object.entries(userObj).reduce((dynamoObj, [key, userValue]) => {
const itemSchema = getItemSchemaFromObjectSchema(objectSchema, key);
const dynamoValue = this.toDynamoValue(userValue, itemSchema);
if (dynamoValue === undefined) {
return dynamoObj;
}
return {
...dynamoObj,
[key]: dynamoValue,
};
}, {});
}
serialize(userObj: PlainObject, objectSchema?: Joi.ObjectSchema) {
const versionedObject = {
...userObj,
_v: this.version,
};
return this.toDynamoMap(versionedObject, objectSchema);
}
}
export default Serializer;