@@ -6,7 +6,6 @@ const fs = require("fs");
66const pg = require ( "pg" ) ;
77const prettier = require ( "prettier" ) ;
88const { graphql } = require ( "graphql" ) ;
9- const child_process = require ( "child_process" ) ;
109const {
1110 createPostGraphileSchema,
1211 withPostGraphileContext,
@@ -30,68 +29,6 @@ async function prettify(filepath, content) {
3029 } ) ;
3130}
3231
33- async function spawn ( command , args , options = { } , stdin = null ) {
34- return new Promise ( ( resolve , reject ) => {
35- console . log ( command , ...args ) ;
36- const cp = child_process . spawn ( command , args , {
37- stdio : "pipe" ,
38- ...options ,
39- } ) ;
40- let stdout = "" ;
41- let stderr = "" ;
42- cp . stdout . on ( "data" , data => {
43- stdout += data ;
44- } ) ;
45- cp . stderr . on ( "data" , data => {
46- stderr += data ;
47- } ) ;
48- cp . on ( "error" , reject ) ;
49- function fail ( e ) {
50- e . stdout = stdout ;
51- e . stderr = stderr ;
52- console . error ( stderr ) ;
53- reject ( e ) ;
54- }
55- cp . on ( "close" , code => {
56- if ( code === 0 ) {
57- resolve ( {
58- stdout,
59- stderr,
60- code,
61- } ) ;
62- } else {
63- const e = new Error ( `Failed with status code ${ code } ` ) ;
64- e . code = code ;
65- fail ( e ) ;
66- }
67- } ) ;
68- if ( stdin ) {
69- cp . stdin . on ( "error" , fail ) ;
70- cp . stdin . write ( stdin ) ;
71- cp . stdin . end ( ) ;
72- } else {
73- cp . stdin . end ( ) ;
74- }
75- } ) ;
76- }
77-
78- async function psql ( flags , sql ) {
79- return spawn (
80- "psql" ,
81- [ "-X" , "-1" , "-v" , "ON_ERROR_STOP=1" , ...flags ] ,
82- { } ,
83- sql
84- ) ;
85- }
86-
87- async function tryPsql ( flags , sql ) {
88- try {
89- return await psql ( flags , sql ) ;
90- } catch ( e ) {
91- // NOOP
92- }
93- }
94-
9532async function main ( ) {
9633 const schemaSql = fs . readFileSync (
9734 `${ __dirname } /../examples/db/schema.sql` ,
@@ -102,33 +39,67 @@ async function main() {
10239 "utf8"
10340 ) ;
10441
105- try {
106- await spawn ( "dropdb" , [ "graphile_org_demo" ] ) ;
107- } catch ( e ) {
108- /* non-existence is fine */
42+ const password = String ( Math . random ( ) ) ;
43+
44+ {
45+ const rootPool = new pg . Pool ( {
46+ host : process . env . PGHOST ,
47+ port : process . env . PGPORT ,
48+ user : process . env . PGUSER ,
49+ password : process . env . PGPASSWORD ,
50+ database : "postgres" ,
51+ } ) ;
52+
53+ try {
54+ await rootPool . query ( "drop database graphile_org_demo" ) ;
55+ } catch ( e ) {
56+ /* non-existence is fine */
57+ }
58+ try {
59+ await rootPool . query ( "drop role graphiledemo" ) ;
60+ } catch ( e ) {
61+ /* non-existence is fine */
62+ }
63+ try {
64+ await rootPool . query ( "drop role graphiledemo_authenticator" ) ;
65+ } catch ( e ) {
66+ /* non-existence is fine */
67+ }
68+ try {
69+ await rootPool . query ( "drop role graphiledemo_visitor" ) ;
70+ } catch ( e ) {
71+ /* non-existence is fine */
72+ }
73+
74+ await rootPool . query (
75+ `CREATE ROLE graphiledemo WITH LOGIN PASSWORD '${ password } ' SUPERUSER`
76+ ) ;
77+ await rootPool . query (
78+ `CREATE ROLE graphiledemo_authenticator WITH LOGIN PASSWORD '${ password } ' NOINHERIT;`
79+ ) ;
80+ await rootPool . query ( `CREATE ROLE graphiledemo_visitor;` ) ;
81+ await rootPool . query (
82+ `GRANT graphiledemo_visitor TO graphiledemo_authenticator;`
83+ ) ;
84+ await rootPool . query (
85+ `create database graphile_org_demo owner graphiledemo`
86+ ) ;
87+ rootPool . end ( ) ;
10988 }
11089
111- const password = String ( Math . random ( ) ) ;
112- await tryPsql (
113- [ "template1" ] ,
114- `CREATE ROLE graphiledemo WITH LOGIN PASSWORD '${ password } ' SUPERUSER`
115- ) ;
116- await tryPsql (
117- [ "template1" ] ,
118- `CREATE ROLE graphiledemo_authenticator WITH LOGIN PASSWORD '${ password } ' NOINHERIT;`
119- ) ;
120- await tryPsql ( [ "template1" ] , `CREATE ROLE graphiledemo_visitor;` ) ;
121- await tryPsql (
122- [ "template1" ] ,
123- `GRANT graphiledemo_visitor TO graphiledemo_authenticator;`
124- ) ;
90+ {
91+ const ownerPool = new pg . Pool ( {
92+ host : process . env . PGHOST ,
93+ port : process . env . PGPORT ,
94+ user : process . env . PGUSER ,
95+ password : process . env . PGPASSWORD ,
96+ database : "graphile_org_demo" ,
97+ } ) ;
12598
126- await spawn ( "createdb" , [ "-O" , "graphiledemo" , "graphile_org_demo" ] ) ;
127- await psql ( [ "graphile_org_demo" ] , schemaSql ) ;
128- await psql ( [ "graphile_org_demo" ] , dataSql ) ;
129- await psql (
130- [ "graphile_org_demo" ] ,
131- `\
99+ await ownerPool . query ( schemaSql ) ;
100+ await ownerPool . query ( dataSql ) ;
101+ await ownerPool . query (
102+ `\
132103set search_path to app_public, app_private, public;
133104/**
134105 * These are the tables we're using in the insert-multiple-records example
@@ -171,9 +142,15 @@ insert into quiz_entry (user_id, quiz_id) values
171142 (4, 2),
172143 (5, 3);
173144`
174- ) ;
145+ ) ;
146+ }
147+
175148 const pgPool = new pg . Pool ( {
176- connectionString : "postgres:///graphile_org_demo" ,
149+ host : process . env . PGHOST ,
150+ port : process . env . PGPORT ,
151+ user : process . env . PGUSER ,
152+ password : process . env . PGPASSWORD ,
153+ database : "graphile_org_demo" ,
177154 } ) ;
178155 const getPostGraphileSchemaWithOptions = ( options = { } , client = pgPool ) =>
179156 createPostGraphileSchema ( client , [ "app_public" ] , {
0 commit comments