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

Upsert for documents #278

Closed
JohannesRudolph opened this issue Jun 27, 2016 · 2 comments
Closed

Upsert for documents #278

JohannesRudolph opened this issue Jun 27, 2016 · 2 comments

Comments

@JohannesRudolph
Copy link

Many document DBs with an HTTP API (e.g. RavenDB) implement PUT semantics for saving a document. I like the way this is handled in massive for tables (save doing an upsert), but for documents the API looks at the existence of a primary key column to determine whether an insert or update should be performed.

Can't we have saveDoc perform an upsert as well? This would keep the API similar. We could have separate insert/update methods as well.

By the way:
https://github.com/robconery/massive-js/blob/master/lib/document_table.js#L71 declares an updateDocSync method, but never actually defines the updateDoc method that it should wrap.

@JohannesRudolph
Copy link
Author

Apparently I was wrong about the table vs. document table API, both just check for existence of an id column. Is there any way to force massive-js to do an upsert of a document with a primary key already specified?

@dmfay
Copy link
Owner

dmfay commented Jun 27, 2016

Not through the table API, no. With 9.5 it should be possible to have save and saveDoc generate INSERT... ON CONFLICT ($pk) DO UPDATE instead of testing the primary key up front, but since that's an existing API call we'd have to implement some sort of option to turn it on so as not to break those functions for previous versions of Postgres.

If you're on 9.5 and you need to upsert something with a (potential) primary key, you should be able to cobble something together using the ON CONFLICT clause in a script file. That may or may not be more trouble than it's worth, though.

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

2 participants