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

Hstore data type #970

Closed
dbrgn opened this Issue Jun 27, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@dbrgn
Contributor

dbrgn commented Jun 27, 2017

Since there wasn't a tracking issue yet, I'll create one.

While the jsonb data type is partially supported, hstore isn't at all.

https://www.postgresql.org/docs/current/static/hstore.html

This module implements the hstore data type for storing sets of key/value pairs within a single PostgreSQL value. This can be useful in various scenarios, such as rows with many attributes that are rarely examined, or semi-structured data. Keys and values are simply text strings.

I'm dealing with string-typed key-value data. In theory I could just store my data into a jsonb field, but then I always have to explicitly validate whether the value is a Value::Object, then iterate over all the values of the map and ensure that the object only has values of type String.

For basic support:

  • Read and write hstore data as HashMap<String, String>

Operators that would have to be added for full support:

  • hstore -> text operator (returns Option<String>)
  • hstore -> text[] operator (returns Option<Vec<String>>)
  • hstore || hstore operator (returns HashMap<String, String>)
  • hstore ? text operator (returns bool)
  • hstore ?& text[] operator (returns bool)
  • hstore ?| text[] operator (returns bool)
  • hstore @> hstore operator (returns bool)
  • hstore <@ hstore operator (returns bool)
  • hstore - text operator (returns HashMap<String, String>)
  • hstore - text[] operator (returns HashMap<String, String>)
  • hstore - hstore operator (returns HashMap<String, String>)

These could probably be implemented as FromSql:

  • %% hstore operator (returns Vec<String>)
  • %# hstore operator (returns Vec<(String, String)>)

These are probably tricky:

  • record #= hstore

In all cases, if one of the operands is nullable, then the result is nullable too.

Basic support would probably be pretty straightforward, right? If someone could tell me what would need to be done, I might be able to provide a PR.

@sgrif

This comment has been minimized.

Member

sgrif commented Jul 4, 2017

While I'm happy to provide assistance for anyone looking to implement this, it's not a type that I want to support in the main Diesel crate. Diesel's design is meant to allow for third party crates to add extension types such as this one. Feel free to comment on this issue with questions or discussion of implementation, but I'm going to close it as it's not a feature that I'd like to add to the main crate.

@sgrif sgrif closed this Jul 4, 2017

@dbrgn

This comment has been minimized.

Contributor

dbrgn commented Jul 4, 2017

I started. It's not in any working state yet though. https://github.com/dbrgn/diesel-hstore-rs

@lholden

This comment has been minimized.

lholden commented Oct 13, 2017

Figured I'd leave a comment here in case people go looking to use hstore with diesel. I've created a create that successfully serializes to/from hstore. Right now it's using a wrapper type due to not being able to impl AsExpression for HashMap. Sgrif had an idea for how this could be gotten around... so once I've explored that idea I'll get the crate published and share a link here so that others can use it.

It does not currently support any of the hstore operators however. Would love to end up doing this eventually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment