Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to save array of objects in datastore #5099

Closed
apoorvmote opened this issue Mar 15, 2020 · 7 comments · Fixed by #5199
Closed

How to save array of objects in datastore #5099

apoorvmote opened this issue Mar 15, 2020 · 7 comments · Fixed by #5199
Assignees
Labels
bug Something isn't working DataStore Related to DataStore category
Milestone

Comments

@apoorvmote
Copy link

Which Category is your question related to?
Datastore

What AWS Services are you utilizing?
Cognito, AWS AppSync, Datastore

Provide additional details e.g. code snippets

This is really stupid

const results = await DataStore.save(new User({name: 'John'}))

this returns User[] So you would naturally assume that you can save multiple users at once.

But following creates an error.

const results = await DataStore.save(new User({name: 'John'}), new User({name: 'Jane'}))

or

const results = await DataStore.save([new User({name: 'John'}), new User({name: 'Jane'})])

or

const results = await DataStore.save(new User({name: 'John'}, {name: 'Jane'}))

why bother returning User[] if you are allowing to save only one item at a time?

Also I used wrong example but for real world use case think of array of invoice items to be added to single invoice.

@apoorvmote apoorvmote added the question General question label Mar 15, 2020
@ashika01 ashika01 added the DataStore Related to DataStore category label Mar 16, 2020
@manueliglesias manueliglesias added bug Something isn't working and removed question General question labels Mar 26, 2020
@manueliglesias
Copy link
Contributor

manueliglesias commented Mar 26, 2020

Hi @apoorvmote

Thanks for the feedback,

this returns User[] So you would naturally assume that you can save multiple users at once.

why bother returning User[] if you are allowing to save only one item at a time?

This seems to be an error in the typescript definition. I'll be labeling this issue as a bug.

Now, internally, the storage adapters return that array and we use it for book keeping purposes, it shouldn't be in the public facing API


Now, regarding your question:

How to save array of objects in datastore

The answer would be with a regular loop, e.g. for, there is no batch-save as of today

@armenr
Copy link

armenr commented Apr 15, 2021

Any chance that we CAN do batch saves now? Or still individual saves?

@Gab1988
Copy link

Gab1988 commented Jul 24, 2021

Same here , it would be great to do batch saves!

@apoorvmote
Copy link
Author

I got tired of waiting around for this feature and actually since launch of HTTP api its cheaper to use the HTTP API + Lambda for batch save. You will lose benefit of graphql mutation triggering subscription notification but batch save is possible with HTTP API.

@Gab1988
Copy link

Gab1988 commented Jul 25, 2021

Hi @apoorvmote , IMHO since you save first on local the performance is not too bad (maybe it's because the size of the DB is relatively small)
Anyhow, following your approach, how do you ensure that local and server data are in sync?
Thanks!

@apoorvmote
Copy link
Author

I personally use react-query and you can also try swr from vercel. Its not perfect solution but for now it works. In future I would create websocket connection how github does it when you write comment and if I am on same page then the page refreshes with your comment.

@github-actions
Copy link

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working DataStore Related to DataStore category
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants