Conversation
7ae2f69 to
4693808
Compare
|
Note to self: still need to implement for Sequel. |
|
A query like SELECT "posts".* FROM "posts"
WHERE ("posts"."translations" -> 'en' @> '{"title":"foo"}'
AND "posts"."translations" -> 'en' @> '{"content":"bar"}')This could actually be done with one clause: SELECT "posts".* FROM "posts"
WHERE ("posts"."translations" -> 'en' @> '{"title":"foo","content":"bar"}')I'm not sure if there is any performance impact of querying on each key/value separately... |
0276c06 to
df9b3b0
Compare
|
And here's a query with a nil value, SELECT "posts".* FROM "posts"
WHERE "posts"."translations" -> 'en' @> '{"title":"foo"}'
AND NOT ("posts"."translations" ? 'en' AND "posts"."translations" -> 'en' ? 'content')And a SELECT "posts".* FROM "posts"
WHERE NOT (1=0)
AND "posts"."translations" ? 'en'
AND "posts"."translations" -> 'en' ? 'title' AND NOT ("posts"."translations" -> 'en' @> '{"title":"foo"}')
AND "posts"."translations" ? 'en' AND "posts"."translations" -> 'en' ? 'content'We are thus looking for posts that have a Like the jsonb backend, we depend here on the fact that when storing data, we strip any values which are not present. This allows us to use the existence operator ( We can also store/query on non-string values (again, like the jsonb backend). e.g. SELECT "posts".* FROM "posts"
WHERE "posts"."translations" -> 'en' @> '{"title":{"a":"b"}}'And this will correctly match posts which have a hash value of {"en"=>{"title"=>{"a"=>"b"}}} |
|
@pwim I'm curious is this is a backend you would consider using. It has a nice balance of ease-of-use (just one column per translated model), performance (jsonb so fast to query) and relatively low complexity (relative to, say, storing translations on a separate table). Implementation is actually quite simple, even querying. |
| end | ||
| include const_set(module_name, dupable) | ||
| end | ||
| end |
There was a problem hiding this comment.
This is a lot of code just to fix an issue which is arguably a bug in AR 4.2... debating maybe just skipping the dup specs for this backend in that version of Rails.
52d2a84 to
ed87e0f
Compare
ae33885 to
30e1732
Compare
8bcad15 to
625cffc
Compare
This is the first new backend to be added to Mobility since all the original ones were introduced in v0.1.x. I was inspired to add it looking at the Elixir hex package named Trans, which stores all translations for a model in a single depth-2 jsonb column (
translations). I've seen this also discussed elsewhere, and thought it would be interesting to implement.This implementation piggy-backs on parts of the existing Jsonb backend, but in other parts is entirely independent. Specs seem to be passing.
Regarding naming, I'm using the term "container" since this is what Trans uses. In the code I've called the backend "JsonbContainer", but this is quite a mouthful so I'm thinking of just renaming it "container", so you'd use it like this:
... or of course just set
config.default_backend = :container.Curious if anyone has any thoughts on this (backend itself, implementation, naming, etc.)