-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.ts
27 lines (24 loc) · 907 Bytes
/
index.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
PersistentModel,
PersistentModelConstructor,
} from '@aws-amplify/datastore';
// Helper for converting JSON back into DataStore models (while respecting IDs)
export function deserializeModel<T extends PersistentModel>(
Model: PersistentModelConstructor<T>,
init: T | T[]
) {
if (Array.isArray(init)) {
return init.map(init => deserializeModel(Model, init));
}
// `fromJSON` is intentionally hidden from types as a "private" method (though it exists on the instance)
// @ts-ignore Property 'fromJSON' does not exist on type 'PersistentModelConstructor<T>'.ts(2339)
return Model.fromJSON(init);
}
// Helper for converting DataStore models to JSON
export function serializeModel<T extends PersistentModel>(
model: T | T[]
): JSON {
return JSON.parse(JSON.stringify(model));
}