-
-
Notifications
You must be signed in to change notification settings - Fork 361
/
Document.ts
537 lines (495 loc) · 24.2 KB
/
Document.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
import aws = require("./aws");
import ddb = require("./aws/ddb/internal");
import utils = require("./utils");
import Error = require("./Error");
import Internal = require("./Internal");
import {Model, ModelExpiresSettings} from "./Model";
import {DynamoDBTypeResult, Schema, DynamoDBSetTypeResult, TimestampObject} from "./Schema";
const {internalProperties} = Internal.General;
const dynamooseUndefined = Internal.Public.undefined;
import {DynamoDB, AWSError} from "aws-sdk";
import {ValueType} from "./Schema";
import {CallbackType, ObjectType} from "./General";
import {SerializerOptions} from "./Serializer";
import {PopulateDocument, PopulateSettings} from "./Populate";
import {Condition} from "./Condition";
export interface DocumentSaveSettings {
overwrite?: boolean;
return?: "request" | "document";
condition?: Condition;
}
export interface DocumentSettings {
type?: "fromDynamo" | "toDynamo";
}
// Document represents an item in a Model that is either pending (not saved) or saved
export class Document {
constructor (model: Model<Document>, object?: DynamoDB.AttributeMap | ObjectType, settings?: DocumentSettings) {
const documentObject = Document.isDynamoObject(object) ? aws.converter().unmarshall(object) : object;
Object.keys(documentObject).forEach((key) => this[key] = documentObject[key]);
Object.defineProperty(this, internalProperties, {
"configurable": false,
"value": {}
});
this[internalProperties].originalObject = JSON.parse(JSON.stringify(documentObject));
this[internalProperties].originalSettings = {...settings};
Object.defineProperty(this, "model", {
"configurable": false,
"value": model
});
if (settings.type === "fromDynamo") {
this[internalProperties].storedInDynamo = true;
}
}
// Internal
model?: Model<Document>;
static objectToDynamo (object: ObjectType): DynamoDB.AttributeMap;
static objectToDynamo (object: any, settings: {type: "value"}): DynamoDB.AttributeValue;
static objectToDynamo (object: ObjectType, settings: {type: "object"}): DynamoDB.AttributeMap;
static objectToDynamo (object: any, settings: {type: "object" | "value"} = {"type": "object"}): DynamoDB.AttributeValue | DynamoDB.AttributeMap {
return (settings.type === "value" ? aws.converter().input : aws.converter().marshall)(object);
}
static fromDynamo (object: DynamoDB.AttributeMap): ObjectType {
return aws.converter().unmarshall(object);
}
// This function will return null if it's unknown if it is a Dynamo object (ex. empty object). It will return true if it is a Dynamo object and false if it's not.
static isDynamoObject (object: ObjectType, recurrsive?: boolean): boolean | null {
function isValid (value): boolean {
if (typeof value === "undefined" || value === null) {
return false;
}
const keys = Object.keys(value);
const key = keys[0];
const nestedResult = typeof value[key] === "object" && !(value[key] instanceof Buffer) ? Array.isArray(value[key]) ? value[key].every((value) => Document.isDynamoObject(value, true)) : Document.isDynamoObject(value[key]) : true;
const {Schema} = require("./Schema");
const attributeType = Schema.attributeTypes.findDynamoDBType(key);
return typeof value === "object" && keys.length === 1 && attributeType && (nestedResult || Object.keys(value[key]).length === 0 || attributeType.isSet);
}
const keys = Object.keys(object);
const values = Object.values(object);
if (keys.length === 0) {
return null;
} else {
return recurrsive ? isValid(object) : values.every((value) => isValid(value));
}
}
static attributesWithSchema: (document: Document, model: Model<Document>) => Promise<string[]>;
static objectFromSchema: (object: any, model: Model<Document>, settings?: DocumentObjectFromSchemaSettings) => Promise<any>;
static prepareForObjectFromSchema: (object: any, model: Model<Document>, settings: DocumentObjectFromSchemaSettings) => any;
conformToSchema: (this: Document, settings?: DocumentObjectFromSchemaSettings) => Promise<Document>;
toDynamo: (this: Document, settings?: Partial<DocumentObjectFromSchemaSettings>) => Promise<any>;
// This function handles actions that should take place before every response (get, scan, query, batchGet, etc.)
async prepareForResponse (): Promise<Document> {
if (this.model.options.populate) {
return this.populate({"properties": this.model.options.populate});
}
return this;
}
// Original
original (): ObjectType | null {
return this[internalProperties].originalSettings.type === "fromDynamo" ? this[internalProperties].originalObject : null;
// toJSON
}
toJSON (): ObjectType {
return utils.dynamoose.documentToJSON.bind(this)();
}
// Serializer
serialize (nameOrOptions?: SerializerOptions | string): ObjectType {
return this.model.serializer._serialize(this, nameOrOptions);
}
// Delete
delete (this: Document): Promise<void>;
delete (this: Document, callback: CallbackType<void, AWSError>): void;
delete (this: Document, callback?: CallbackType<void, AWSError>): Promise<void> | void {
const hashKey = this.model.getHashKey();
const rangeKey = this.model.getRangeKey();
const key = {[hashKey]: this[hashKey]};
if (rangeKey) {
key[rangeKey] = this[rangeKey];
}
return this.model.delete(key, callback);
}
// Save
save (this: Document): Promise<Document>;
save (this: Document, callback: CallbackType<Document, AWSError>): void;
save (this: Document, settings: DocumentSaveSettings & {return: "request"}): Promise<DynamoDB.PutItemInput>;
save (this: Document, settings: DocumentSaveSettings & {return: "request"}, callback: CallbackType<DynamoDB.PutItemInput, AWSError>): void;
save (this: Document, settings: DocumentSaveSettings & {return: "document"}): Promise<Document>;
save (this: Document, settings: DocumentSaveSettings & {return: "document"}, callback: CallbackType<Document, AWSError>): void;
save (this: Document, settings?: DocumentSaveSettings | CallbackType<Document, AWSError> | CallbackType<DynamoDB.PutItemInput, AWSError>, callback?: CallbackType<Document, AWSError> | CallbackType<DynamoDB.PutItemInput, AWSError>): void | Promise<Document | DynamoDB.PutItemInput> {
if (typeof settings !== "object" && typeof settings !== "undefined") {
callback = settings;
settings = {};
}
if (typeof settings === "undefined") {
settings = {};
}
let savedItem;
const localSettings: DocumentSaveSettings = settings;
const paramsPromise = this.toDynamo({"defaults": true, "validate": true, "required": true, "enum": true, "forceDefault": true, "combine": true, "saveUnknown": true, "customTypesDynamo": true, "updateTimestamps": true, "modifiers": ["set"]}).then((item) => {
savedItem = item;
let putItemObj: DynamoDB.PutItemInput = {
"Item": item,
"TableName": this.model.name
};
if (localSettings.condition) {
putItemObj = {
...putItemObj,
...localSettings.condition.requestObject()
};
}
if (localSettings.overwrite === false) {
const conditionExpression = "attribute_not_exists(#__hash_key)";
putItemObj.ConditionExpression = putItemObj.ConditionExpression ? `(${putItemObj.ConditionExpression}) AND (${conditionExpression})` : conditionExpression;
putItemObj.ExpressionAttributeNames = {
...putItemObj.ExpressionAttributeNames || {},
"#__hash_key": this.model.getHashKey()
};
}
return putItemObj;
});
if (settings.return === "request") {
if (callback) {
const localCallback: CallbackType<DynamoDB.PutItemInput, AWSError> = callback as CallbackType<DynamoDB.PutItemInput, AWSError>;
paramsPromise.then((result) => localCallback(null, result));
return;
} else {
return paramsPromise;
}
}
const promise: Promise<DynamoDB.PutItemOutput> = Promise.all([paramsPromise, this.model.pendingTaskPromise()]).then((promises) => {
const [putItemObj] = promises;
return ddb("putItem", putItemObj);
});
if (callback) {
const localCallback: CallbackType<Document, AWSError> = callback as CallbackType<Document, AWSError>;
promise.then(() => {
this[internalProperties].storedInDynamo = true;
const returnDocument = new this.model.Document(savedItem as any);
returnDocument[internalProperties].storedInDynamo = true;
localCallback(null, returnDocument);
}).catch((error) => callback(error));
} else {
return (async (): Promise<Document> => {
await promise;
this[internalProperties].storedInDynamo = true;
const returnDocument = new this.model.Document(savedItem as any);
returnDocument[internalProperties].storedInDynamo = true;
return returnDocument;
})();
}
}
// Populate
populate (): Promise<Document>;
populate (callback: CallbackType<Document, AWSError>): void;
populate (settings: PopulateSettings): Promise<Document>;
populate (settings: PopulateSettings, callback: CallbackType<Document, AWSError>): void;
populate (...args): Promise<Document> | void {
return PopulateDocument.bind(this)(...args);
}
}
export class AnyDocument extends Document {
[key: string]: any;
}
// This function will mutate the object passed in to run any actions to conform to the schema that cannot be achieved through non mutating methods in Document.objectFromSchema (setting timestamps, etc.)
Document.prepareForObjectFromSchema = async function<T>(object: T, model: Model<Document>, settings: DocumentObjectFromSchemaSettings): Promise<T> {
if (settings.updateTimestamps) {
const schema: Schema = await model.schemaForObject(object);
if (schema.settings.timestamps && settings.type === "toDynamo") {
const date = new Date();
const createdAtProperties: string[] = ((Array.isArray((schema.settings.timestamps as TimestampObject).createdAt) ? (schema.settings.timestamps as TimestampObject).createdAt : [(schema.settings.timestamps as TimestampObject).createdAt]) as any).filter((a) => Boolean(a));
const updatedAtProperties: string[] = ((Array.isArray((schema.settings.timestamps as TimestampObject).updatedAt) ? (schema.settings.timestamps as TimestampObject).updatedAt : [(schema.settings.timestamps as TimestampObject).updatedAt]) as any).filter((a) => Boolean(a));
if (object[internalProperties] && !object[internalProperties].storedInDynamo && (typeof settings.updateTimestamps === "boolean" || settings.updateTimestamps.createdAt)) {
createdAtProperties.forEach((prop) => {
utils.object.set(object as any, prop, date);
});
}
if (typeof settings.updateTimestamps === "boolean" || settings.updateTimestamps.updatedAt) {
updatedAtProperties.forEach((prop) => {
utils.object.set(object as any, prop, date);
});
}
}
}
return object;
};
// This function will return a list of attributes combining both the schema attributes with the document attributes. This also takes into account all attributes that could exist (ex. properties in sets that don't exist in document), adding the indexes for each item in the document set.
// https://stackoverflow.com/a/59928314/894067
Document.attributesWithSchema = async function (document: Document, model: Model<Document>): Promise<string[]> {
const schema: Schema = await model.schemaForObject(document);
const attributes = schema.attributes();
// build a tree out of schema attributes
const root = {};
attributes.forEach((attribute) => {
let node = root;
attribute.split(".").forEach((part) => {
node[part] = node[part] || {};
node = node[part];
});
});
// explore the tree
function traverse (node, treeNode, outPath, callback): void {
callback(outPath);
if (Object.keys(treeNode).length === 0) { // a leaf
return;
}
Object.keys(treeNode).forEach((attr) => {
if (attr === "0") {
// We check for empty objects here (added `typeof node === "object" && Object.keys(node).length == 0`, see PR https://github.com/dynamoose/dynamoose/pull/1034) to handle the use case of 2d arrays, or arrays within arrays. `node` in that case will be an empty object.
if (!node || node.length == 0 || typeof node === "object" && Object.keys(node).length == 0) {
node = [{}]; // fake the path for arrays
}
node.forEach((a, index) => {
outPath.push(index);
traverse(node[index], treeNode[attr], outPath, callback);
outPath.pop();
});
} else {
if (!node) {
node = {}; // fake the path for properties
}
outPath.push(attr);
traverse(node[attr], treeNode[attr], outPath, callback);
outPath.pop();
}
});
}
const out = [];
traverse(document, root, [], (val) => out.push(val.join(".")));
const result = out.slice(1);
return result;
};
export interface DocumentObjectFromSchemaSettings {
type: "toDynamo" | "fromDynamo";
schema?: Schema;
checkExpiredItem?: boolean;
saveUnknown?: boolean;
defaults?: boolean;
forceDefault?: boolean;
customTypesDynamo?: boolean;
validate?: boolean;
required?: boolean | "nested";
enum?: boolean;
populate?: boolean;
combine?: boolean;
modifiers?: ("set" | "get")[];
updateTimestamps?: boolean | {updatedAt?: boolean; createdAt?: boolean};
}
// This function will return an object that conforms to the schema (removing any properties that don't exist, using default values, etc.) & throws an error if there is a typemismatch.
Document.objectFromSchema = async function (object: any, model: Model<Document>, settings: DocumentObjectFromSchemaSettings = {"type": "toDynamo"}): Promise<ObjectType> {
if (settings.checkExpiredItem && model.options.expires && ((model.options.expires as ModelExpiresSettings).items || {}).returnExpired === false && object[(model.options.expires as ModelExpiresSettings).attribute] && object[(model.options.expires as ModelExpiresSettings).attribute] * 1000 < Date.now()) {
return undefined;
}
const returnObject = {...object};
const schema: Schema = settings.schema || await model.schemaForObject(returnObject);
const schemaAttributes = schema.attributes(returnObject);
// Type check
const validParents = []; // This array is used to allow for set contents to not be type checked
const keysToDelete = [];
const typeIndexOptionMap = schema.getTypePaths(returnObject, settings);
const checkTypeFunction = (item): void => {
const [key, value] = item;
if (validParents.find((parent) => key.startsWith(parent.key) && (parent.infinite || key.split(".").length === parent.key.split(".").length + 1))) {
return;
}
const genericKey = key.replace(/\.\d+/gu, ".0"); // This is a key replacing all list numbers with 0 to standardize things like checking if it exists in the schema
const existsInSchema = schemaAttributes.includes(genericKey);
if (existsInSchema) {
const {isValidType, matchedTypeDetails, typeDetailsArray} = utils.dynamoose.getValueTypeCheckResult(schema, value, genericKey, settings, {"standardKey": true, typeIndexOptionMap});
if (!isValidType) {
throw new Error.TypeMismatch(`Expected ${key} to be of type ${typeDetailsArray.map((detail) => detail.dynamicName ? detail.dynamicName() : detail.name.toLowerCase()).join(", ")}, instead found type ${utils.type_name(value, typeDetailsArray)}.`);
} else if (matchedTypeDetails.isSet || matchedTypeDetails.name.toLowerCase() === "model") {
validParents.push({key, "infinite": true});
} else if (/*typeDetails.dynamodbType === "M" || */matchedTypeDetails.dynamodbType === "L") {
// The code below is an optimization for large array types to speed up the process of not having to check the type for every element but only the ones that are different
value.forEach((subValue, index: number, array: any[]) => {
if (index === 0 || typeof subValue !== typeof array[0]) {
checkTypeFunction([`${key}.${index}`, subValue]);
} else if (keysToDelete.includes(`${key}.0`) && typeof subValue === typeof array[0]) {
keysToDelete.push(`${key}.${index}`);
}
});
validParents.push({key});
}
} else {
// Check saveUnknown
if (!settings.saveUnknown || !utils.dynamoose.wildcard_allowed_check(schema.getSettingValue("saveUnknown"), key)) {
keysToDelete.push(key);
}
}
};
utils.object.entries(returnObject).filter((item) => item[1] !== undefined && item[1] !== dynamooseUndefined).map(checkTypeFunction);
keysToDelete.reverse().forEach((key) => utils.object.delete(returnObject, key));
if (settings.defaults || settings.forceDefault) {
await Promise.all((await Document.attributesWithSchema(returnObject, model)).map(async (key) => {
const value = utils.object.get(returnObject, key);
if (value === dynamooseUndefined) {
utils.object.set(returnObject, key, undefined);
} else {
const defaultValue = await schema.defaultCheck(key, value as ValueType, settings);
const isDefaultValueUndefined = Array.isArray(defaultValue) ? defaultValue.some((defaultValue) => typeof defaultValue === "undefined" || defaultValue === null) : typeof defaultValue === "undefined" || defaultValue === null;
if (!isDefaultValueUndefined) {
const {isValidType, typeDetailsArray} = utils.dynamoose.getValueTypeCheckResult(schema, defaultValue, key, settings, {typeIndexOptionMap});
if (!isValidType) {
throw new Error.TypeMismatch(`Expected ${key} to be of type ${typeDetailsArray.map((detail) => detail.dynamicName ? detail.dynamicName() : detail.name.toLowerCase()).join(", ")}, instead found type ${typeof defaultValue}.`);
} else {
utils.object.set(returnObject, key, defaultValue);
}
}
}
}));
}
// Custom Types
if (settings.customTypesDynamo) {
(await Document.attributesWithSchema(returnObject, model)).map((key) => {
const value = utils.object.get(returnObject, key);
const isValueUndefined = typeof value === "undefined" || value === null;
if (!isValueUndefined) {
const typeDetails = utils.dynamoose.getValueTypeCheckResult(schema, value, key, settings, {typeIndexOptionMap}).matchedTypeDetails as DynamoDBTypeResult;
const {customType} = typeDetails;
const {"type": typeInfo} = typeDetails.isOfType(value as ValueType);
const isCorrectTypeAlready = typeInfo === (settings.type === "toDynamo" ? "underlying" : "main");
if (customType && customType.functions[settings.type] && !isCorrectTypeAlready) {
const customValue = customType.functions[settings.type](value);
utils.object.set(returnObject, key, customValue);
}
}
});
}
// DynamoDB Type Handler (ex. converting sets to correct value for toDynamo & fromDynamo)
utils.object.entries(returnObject).filter((item) => typeof item[1] === "object").forEach((item) => {
const [key, value] = item;
let typeDetails;
try {
typeDetails = utils.dynamoose.getValueTypeCheckResult(schema, value, key, settings, {typeIndexOptionMap}).matchedTypeDetails;
} catch (e) {
const {Schema} = require("./Schema");
typeDetails = Schema.attributeTypes.findTypeForValue(value, settings.type, settings);
}
if (typeDetails && typeDetails[settings.type]) {
utils.object.set(returnObject, key, typeDetails[settings.type](value));
}
});
if (settings.combine) {
schemaAttributes.map((key) => {
try {
const typeDetails = schema.getAttributeTypeDetails(key);
return {
key,
"type": typeDetails
};
} catch (e) {} // eslint-disable-line no-empty
}).filter((item: any) => {
return Array.isArray(item.type) ? item.type.some((type) => type.name === "Combine") : item.type.name === "Combine";
}).map((obj: {"key": string; "type": DynamoDBTypeResult | DynamoDBSetTypeResult | DynamoDBTypeResult[] | DynamoDBSetTypeResult[]} | undefined): {"key": string; "type": DynamoDBTypeResult | DynamoDBSetTypeResult} => {
if (obj && Array.isArray(obj.type)) {
throw new Error.InvalidParameter("Combine type is not allowed to be used with multiple types.");
}
return obj as any;
}).forEach((item) => {
const {key, type} = item;
const value = type.typeSettings.attributes.map((attribute) => utils.object.get(returnObject, attribute)).filter((value) => typeof value !== "undefined" && value !== null).join(type.typeSettings.seperator);
utils.object.set(returnObject, key, value);
});
}
if (settings.modifiers) {
await Promise.all(settings.modifiers.map(async (modifier) => {
return Promise.all((await Document.attributesWithSchema(returnObject, model)).map(async (key) => {
const value = utils.object.get(returnObject, key);
const modifierFunction = await schema.getAttributeSettingValue(modifier, key, {"returnFunction": true, typeIndexOptionMap});
const modifierFunctionExists: boolean = Array.isArray(modifierFunction) ? modifierFunction.some((val) => Boolean(val)) : Boolean(modifierFunction);
const isValueUndefined = typeof value === "undefined" || value === null;
if (modifierFunctionExists && !isValueUndefined) {
const oldValue = object.original ? utils.object.get(object.original(), key) : undefined;
utils.object.set(returnObject, key, await modifierFunction(value, oldValue));
}
}));
}));
}
if (settings.validate) {
await Promise.all((await Document.attributesWithSchema(returnObject, model)).map(async (key) => {
const value = utils.object.get(returnObject, key);
const isValueUndefined = typeof value === "undefined" || value === null;
if (!isValueUndefined) {
const validator = await schema.getAttributeSettingValue("validate", key, {"returnFunction": true, typeIndexOptionMap});
if (validator) {
let result;
if (validator instanceof RegExp) {
// TODO: fix the line below to not use `as`. This will cause a weird issue even in vanilla JS, where if your validator is a Regular Expression but the type isn't a string, it will throw a super random error.
result = validator.test(value as string);
} else {
result = typeof validator === "function" ? await validator(value) : validator === value;
}
if (!result) {
throw new Error.ValidationError(`${key} with a value of ${value} had a validation error when trying to save the document`);
}
}
}
}));
}
if (settings.required) {
let attributesToCheck = await Document.attributesWithSchema(returnObject, model);
if (settings.required === "nested") {
attributesToCheck = attributesToCheck.filter((attribute) => utils.object.keys(returnObject).find((key) => attribute === key || attribute.startsWith(key + ".")));
}
await Promise.all(attributesToCheck.map(async (key) => {
const check = async (): Promise<void> => {
const value = utils.object.get(returnObject, key);
await schema.requiredCheck(key, value as ValueType);
};
const keyParts = key.split(".");
const parentKey = keyParts.slice(0, -1).join(".");
if (parentKey) {
const parentValue = utils.object.get(returnObject, parentKey);
const isParentValueUndefined = typeof parentValue === "undefined" || parentValue === null;
if (!isParentValueUndefined) {
await check();
}
} else {
await check();
}
}));
}
if (settings.enum) {
await Promise.all((await Document.attributesWithSchema(returnObject, model)).map(async (key) => {
const value = utils.object.get(returnObject, key);
const isValueUndefined = typeof value === "undefined" || value === null;
if (!isValueUndefined) {
const enumArray = await schema.getAttributeSettingValue("enum", key, {"returnFunction": false, typeIndexOptionMap});
if (enumArray && !enumArray.includes(value)) {
throw new Error.ValidationError(`${key} must equal ${JSON.stringify(enumArray)}, but is set to ${value}`);
}
}
}));
}
return returnObject;
};
Document.prototype.toDynamo = async function (this: Document, settings: Partial<DocumentObjectFromSchemaSettings> = {}): Promise<any> {
const newSettings: DocumentObjectFromSchemaSettings = {
...settings,
"type": "toDynamo"
};
await Document.prepareForObjectFromSchema(this, this.model, newSettings);
const object = await Document.objectFromSchema(this, this.model, newSettings);
return Document.objectToDynamo(object);
};
// This function will modify the document to conform to the Schema
Document.prototype.conformToSchema = async function (this: Document, settings: DocumentObjectFromSchemaSettings = {"type": "fromDynamo"}): Promise<Document> {
let document = this;
if (settings.type === "fromDynamo") {
document = await this.prepareForResponse();
}
await Document.prepareForObjectFromSchema(document, document.model, settings);
const expectedObject = await Document.objectFromSchema(document, document.model, settings);
if (!expectedObject) {
return expectedObject;
}
const expectedKeys = Object.keys(expectedObject);
Object.keys(document).forEach((key) => {
if (!expectedKeys.includes(key)) {
delete this[key];
} else if (this[key] !== expectedObject[key]) {
this[key] = expectedObject[key];
}
});
return this;
};