Skip to content
This repository has been archived by the owner on Mar 4, 2019. It is now read-only.

Support for ON CONFLICT #425

Open
tamlyn opened this issue Jul 18, 2017 · 5 comments
Open

Support for ON CONFLICT #425

tamlyn opened this issue Jul 18, 2017 · 5 comments

Comments

@tamlyn
Copy link
Contributor

tamlyn commented Jul 18, 2017

Postgres 9.5 introduced the ON CONFLICT clause which enables upsert behavior.

Would you be open to a PR to add support for this to Massive as an option on insert? Simplest solution would be to add a second options parameter. e.g.

db.table.insert({id: 'might exist'}, {onConflict: 'do nothing'})
// or
db.table.insert({id: 'might exist'}, {onConflict: 'do update'})

It should be possible to generate the rest of the do update clause automatically.

Alternatively the options could be flags like {ignoreConflict: true} and {upsert: true} respectively.

As mentioned in #278 (comment) this this could also be used for save.

@vitaly-t
Copy link
Contributor

vitaly-t commented Jul 18, 2017

A typical ON CONFLICT clause is static, which means it can be simply appended to the generated query. And this is how it is done within pg-promise, b.t.w.

@dmfay
Copy link
Owner

dmfay commented Jul 18, 2017

Yes I would! Boolean options are preferable; onConflictIgnore and onConflictUpdate, maybe?

I am actually in the middle of some reorganizing around statement generation so you may want to hold off for a day or so to avoid a different kind of conflict :)

@dmfay
Copy link
Owner

dmfay commented Jul 20, 2017

@tamlyn I just merged the big statement refactor down, so you're good to go on this!

@Kikobeats
Copy link

Kikobeats commented Nov 18, 2018

That's an awesome feature, is it in the roadmap? 🙂

I made a upsert workaround:

const upsert = async (table, collection) =>
    aigle.eachSeries(collection, async data => {
      const id = data.id
      const instance = await table.findOne({ id })
      return isNil(instance) ? table.insert(data) : table.update({ id }, data)
    })

using it

await upsert(db.customers, customersPlans)
await upsert(db.quotas, quotas)
await upsert(db.plans, plans)

one thing, why is not possible use insert with custom id? :((

@dmfay
Copy link
Owner

dmfay commented Nov 20, 2018

Massive doesn't have a "roadmap" as such. The goal has always been to cover ~90% of day-to-day database usage and make doing the remaining 10% with SQL as easy as possible; there's not much to get all project management over since that goal's been comfortably met for some time, and everything else is, functionally speaking, gravy. If you'd like to take a crack at generating a proper ON CONFLICT DO UPDATE clause, feel free -- I'm happy to talk over ideas and pull requests! Otherwise it'll happen when it happens.

Can you open another issue for your custom id problem and clarify it a little more?

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

No branches or pull requests

4 participants