I'm having trouble wrapping my head around this context. Here's my first take at this using some of the built in functions:
What I've tried so far
SELECT json_build_object('id', MD5(c.id), 'firstName',
c.first_name, 'lastName', c.last_name, 'location',
json_build_object('city', cl.city, 'state', cl.state))
FROM person p
LEFT JOIN (SELECT id, city, state FROM
person_location) pl ON pl.id = p.id
LIMIT 10
Works well enough for this small example, and also returns 10 rows. If I take off the limit, I'll get all of my rows. Perfect; however, this is meant to power a view that will be feeding data directly into Logstash, so there's some custom business logic that cleans up the fields and performs some other light tasks. To remedy this, it made sense to try plv8
plv8 method:
CREATE OR REPLACE FUNCTION generate_search_documents()
RETURNS SETOF person_test AS
$$
const _ = require('lodash'),
candidates = plv8.execute('select * FROM candidate LIMIT 10');
plv8.return_next(people);
$$
LANGUAGE plv8;
And I'm already immediately lost. What I'm trying to do is select the tables that I need with the proper joins, clean up the data, and build up my JSON object and return one per row. In the first method, it's clear that in the query I'm working within the context of a single person. In the method below, it appears that I'm executing a query that's selecting all 80M people in our table. That's not easy.
Any idea how I can use this method to replicate what I'm attempting to do in the first example?