Skip to content

Pre-processor in Go for easier Json handling in postgres

License

Notifications You must be signed in to change notification settings

StephanSchmidt/EasyPgJson

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A small pre-parser for Postgres SQL for Go.

It turns

WITH tasks AS $(
 SELECT
   t.id id,
   t.title title,
   u.name name,
   s.status status
   FROM tasks t, users u, status s
   WHERE t.user_id=u.id AND t.status_id=s.id
   AND t.user_id=$1
)
$$(
  'tasks',  (SELECT * from tasks)
)

into

WITH tasks AS ( 
  select json_agg(row_to_json(t)) from (                          
    SELECT
        t.id id,
        t.title title,
        u.name name,
        s.status status
        FROM tasks t, users u, status s
        WHERE t.user_id=u.id AND t.status_id=s.id
        AND t.user_id=$1
    ) t )
select json_build_object(
         'tasks',  (SELECT * from tasks)
)

and makes creating SQL that returns JSON more readable.

The API is

	sql := easypgjson.ToSql(....)

About

Pre-processor in Go for easier Json handling in postgres

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages