Open charlieforward9 opened 1 year ago
Hi @charlieforward9 👋 thanks for raising this issue.
We have some utility functions for serializing and deserializing datastore models.
Example:
import { serializeModel } from '@aws-amplify/datastore/ssr';
//...
interface AuthState {
//...
account: Account | null;
}
const initialState: AuthState = {
//...
account: null,
};
const authSlice = createSlice({
//....
const account = new Account({
type: action.payload.accountType,
creationDate: new Date().toISOString().slice(0, 10),
});
state.account = serializeModel(account);
});
}
These were intended for serializing models so that they may be passed from server to client and vice versa so perhaps this fits your use case?
Thank you for the quick response. That seems like it should fit my needs as it is converted to JSON!
Hello Amplify team,
As my repository grows in complexity, I would like to know if there is a solution for this growing issue:
I need serializable models for state management purposes. However, if the data model changes, I have to propagate these changes manually over to the serializable model instances, this can lead to bugs that can take some time to catch.
The best solution in mind is to have the serializable versions generated in the models/index.d.ts
file, where they can be used without any extra manually written code, and update to data model changes automatically.
However, I may be wrong. What does the Amplify team think? Is there already a solution out there for this use case that extends the functionality of serializeModel()
into a easy-to-use, easy-to-update solution?
Hi @charlieforward9 I will mark this as a feature request for now and discuss it with the team
@chrisbonifacio I am upgrading to v6 and the serializeModel
utility is no longer available in the @aws-amplify/datastore/ssr
import path. Has it been moved? Has it been deprecated?
Edit: I found the function in the v5 source code, which was honestly simpler than I expected.
// Helper for converting DataStore models to JSON
export function serializeModel<T extends PersistentModel>(
model: T | T[]
): JSON {
return JSON.parse(JSON.stringify(model));
}
I will just add this to my codebase until this feature request is considered.
Hi @charlieforward9 👋
Unfortunately, with Amplify v6, DataStore is not supported in an SSR context. We recommend using the Amplify API library on the server side.
For anyone who lands on here looking for a solution, I ended up removing DataStore from my project and interact exclusively with the GraphQL API to get serializable objects from the get go.
Is this related to a new or existing framework?
React
Is this related to a new or existing API?
DataStore
Is this related to another service?
No response
Describe the feature you'd like to request
I would like to be able to store DataStore instances in my Redux state variables. For example:
With this Account data model:
I have this snippet
That returns this error
A non-serializable value was detected in the state, in the path: auth.account
Describe the solution you'd like
I would like to be able to call something like
account.serialize()
, which would return a serialized version of Account.Describe alternatives you've considered
I have created Account instance and use the helper function to convert it into a serializable object, but this adds more complexity to my codebase, which will need to be manually changed whenever the data model changes through
amplify pull
. It seems more suitable to have this functionality within the generatedmodels/
folder.Additional context
No response
Is this something that you'd be interested in working on?