jgaskins / perpetuity-postgres

Postgres adapter for Perpetuity
MIT License
10 stars 7 forks source link

Inferred joins #7

Open jgaskins opened 10 years ago

jgaskins commented 10 years ago

Given the following query:

postgres.query { |book| book.author.name == 'Foo' }

If the author attribute is stored in JSON (anything not serializable by the database is stored that way), it will be serialized as the following hash:

{
  '__metadata__' => {
    'class' => 'Person',
    'id' => 'insert some kind of UUID here'
  }
}

Using Postgres' JSON operators, we can join this Book table with the Person table on that id:

SELECT *
FROM "Book"
INNER JOIN "Person"
ON ("Book"."author"->'__metadata__'->>'id')::uuid = "Person"."id"
WHERE "Person"."name" = 'Foo'

The downside is that we can't use the fact that the Person class is specified in the serialized form. We need to know the type (or types) of the author attribute before we send off the query, which isn't yet possible because we need the core perpetuity gem to pass in the attributes to the query. There isn't anything we can do on this end until that happens, but I wanted to open this issue to remind myself about it.