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 best to merge list of new/updated objects into existing list #15

Open
jfsiii opened this issue Jun 20, 2017 · 1 comment
Open

How best to merge list of new/updated objects into existing list #15

jfsiii opened this issue Jun 20, 2017 · 1 comment

Comments

@jfsiii
Copy link

jfsiii commented Jun 20, 2017

I receive an array of objects from an API and would like to store them. Sometimes the objects match those already in the db and other times they've been altered. This sounds like a great use of upsert.

Unfortunately, the only way I can get this to work is

users.forEach(user => db.get('users').upsert(user).write())

where users is the array of objects. Is there a better way to do this than calling write in a loop?

@typicode
Copy link
Owner

Hi @jfsiii,

Yes, you can do:

users.forEach(user => {
  // will change only in memory
  db.get('users').upsert(user).value()

  // you can also use `commit` if you don't need a returning value
  // db.get('users').upsert(user).commit()
})

// then write to file
db.write()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants