CORS setup and FirstRow method added to postgres object
This commit is contained in:
		
							parent
							
								
									21847e9a6d
								
							
						
					
					
						commit
						ebc296366e
					
				| @ -5,7 +5,7 @@ | |||||||
|   "main": "index.js", |   "main": "index.js", | ||||||
|   "scripts": { |   "scripts": { | ||||||
|     "test": "node_modules/mocha/bin/mocha", |     "test": "node_modules/mocha/bin/mocha", | ||||||
|     "start": "node index.js" |     "start": "nodemon index.js" | ||||||
|   }, |   }, | ||||||
|   "author": "", |   "author": "", | ||||||
|   "license": "ISC", |   "license": "ISC", | ||||||
|  | |||||||
							
								
								
									
										183
									
								
								server.js
									
									
									
									
									
								
							
							
						
						
									
										183
									
								
								server.js
									
									
									
									
									
								
							| @ -11,12 +11,16 @@ var pg = require('pg'); | |||||||
| var server = express(); | var server = express(); | ||||||
| server.engine('handlebars', handlebars()); | server.engine('handlebars', handlebars()); | ||||||
| server.set('view engine', 'handlebars'); | server.set('view engine', 'handlebars'); | ||||||
| server.use(function(req, res, next) { | 
 | ||||||
|     res.header("Access-Control-Allow-Origin", "*"); | server.use(function(inReq, inRes, inNext) | ||||||
|     res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | { | ||||||
|     next(); |     inRes.header("Access-Control-Allow-Origin", "*"); | ||||||
|  |     inRes.header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS"); | ||||||
|  |     inRes.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | ||||||
|  |     inNext(); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| var Postgres = new pg.Client({ | var Postgres = new pg.Client({ | ||||||
|     user: process.env.user, |     user: process.env.user, | ||||||
|     password: process.env.password, |     password: process.env.password, | ||||||
| @ -26,133 +30,59 @@ var Postgres = new pg.Client({ | |||||||
|     application_name: "tps_etl_api", |     application_name: "tps_etl_api", | ||||||
|     ssl: true |     ssl: true | ||||||
| }); | }); | ||||||
| 
 | Postgres.FirstRow = function(inSQL, inResponse) | ||||||
|  | { | ||||||
|  |     Postgres.query(inSQL, (err, res) => { | ||||||
|  |         if (err === null) | ||||||
|  |         { | ||||||
|  |             inResponse.json(res.rows[0]); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |         inResponse.json(err.message); | ||||||
|  |     }); | ||||||
|  | }; | ||||||
| Postgres.connect(); | Postgres.connect(); | ||||||
| 
 | 
 | ||||||
| //-------------------------------------------------------------list source--------------------------------------------------------------------------
 |  | ||||||
| 
 | 
 | ||||||
| server.use("/srce_list", function (inReq, inRes) { | server.get("/source", function (inReq, inRes) | ||||||
| 
 | { | ||||||
|     var sql = "SELECT jsonb_agg(defn) source_list FROM tps.srce" |     var sql = "SELECT jsonb_agg(defn) source_list FROM tps.srce"; | ||||||
|     console.log(sql); |     Postgres.FirstRow(sql, inRes); | ||||||
| 
 | }); | ||||||
|     Postgres.query(sql, (err, res) => { | server.post("/source", bodyParser.json(), function (inReq, inRes)// remove body parsing, just pass post body to the sql string build
 | ||||||
|         inRes.json(res.rows[0]); | { | ||||||
|         console.log("source list request complete"); |     var sql = "SELECT x.message FROM tps.srce_set($$" + JSON.stringify(inReq.body) + "$$::jsonb) as x(message)"; | ||||||
|     }); |     Postgres.FirstRow(sql, inRes); | ||||||
| } | }); | ||||||
| ); |  | ||||||
| 
 | 
 | ||||||
| //-------------------------------------------------------------list maps--------------------------------------------------------------------------
 | //-------------------------------------------------------------list maps--------------------------------------------------------------------------
 | ||||||
|  | server.get("/map_list", function (inReq, inRes) | ||||||
|  | { | ||||||
|  |     var sql = "SELECT jsonb_agg(regex) regex FROM tps.map_rm"; | ||||||
|  |     Postgres.FirstRow(sql, inRes); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| server.use("/map_list", function (inReq, inRes) { | //list unmapped items flagged to be mapped   ?srce=
 | ||||||
|  | server.get("/unmapped", function (inReq, inRes) | ||||||
|  | { | ||||||
|  |     var sql = "SELECT jsonb_agg(row_to_json(x)::jsonb) regex FROM tps.report_unmapped_recs('"+ inReq.query.srce + "') x"; | ||||||
|  |     Postgres.FirstRow(sql, inRes); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
|     var sql = "SELECT jsonb_agg(regex) regex FROM tps.map_rm" |  | ||||||
|     console.log(sql); |  | ||||||
| 
 | 
 | ||||||
|     Postgres.query(sql, (err, res) => { | //set one or more map definitions
 | ||||||
|  | server.post("/mapdef_set", bodyParser.json(), function (inReq, inRes) | ||||||
|  | { | ||||||
|  |     var sql = "SELECT x.message FROM tps.srce_map_def_set($$" + JSON.stringify(inReq.body) + "$$::jsonb) as x(message)"; | ||||||
|  |     Postgres.FirstRow(sql, inRes); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
|         if (err === null) { | //add entries to lookup table
 | ||||||
|             inRes.json(res.rows[0]); | server.post("/mapval_set", bodyParser.json(), function (inReq, inRes) | ||||||
|             return; | { | ||||||
|         } |     var sql = "SELECT x.message FROM tps.map_rv_set($$" + JSON.stringify( inReq.body) + "$$::jsonb) as x(message)"; | ||||||
|         inRes.json(err.message); |     Postgres.FirstRow(sql, inRes); | ||||||
|     }); | }); | ||||||
| } |  | ||||||
| ); |  | ||||||
| 
 |  | ||||||
| //--------------------------------------------------------list unmapped items flagged to be mapped---------------------------------------------------
 |  | ||||||
| 
 |  | ||||||
| server.use("/unmapped", function (inReq, inRes) { |  | ||||||
| 
 |  | ||||||
|     var sql = "SELECT jsonb_agg(row_to_json(x)::jsonb) regex FROM tps.report_unmapped_recs('"; |  | ||||||
|     sql += inReq.query.srce + "') x" |  | ||||||
|     console.log(sql); |  | ||||||
| 
 |  | ||||||
|     Postgres.query(sql, (err, res) => { |  | ||||||
| 
 |  | ||||||
|         if (err === null) { |  | ||||||
|             inRes.json(res.rows[0]); |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|         inRes.json(err.message); |  | ||||||
|     }); |  | ||||||
| } |  | ||||||
| ); |  | ||||||
| 
 |  | ||||||
| //-------------------------------------------------------------set source via json in body--------------------------------------------------------------------------
 |  | ||||||
| 
 |  | ||||||
| server.use("/srce_set", bodyParser.json(), function (inReq, inRes) { |  | ||||||
|      |  | ||||||
|     //validate the body contents before pushing to sql?
 |  | ||||||
|     var sql = "SELECT x.message FROM tps.srce_set($$"; |  | ||||||
|     sql += JSON.stringify( inReq.body); |  | ||||||
|     sql += "$$::jsonb) as x(message)"; |  | ||||||
|     console.log(sql); |  | ||||||
| 
 |  | ||||||
|     Postgres.query(sql, (err, res) => { |  | ||||||
| 
 |  | ||||||
|         //Postgres.end();
 |  | ||||||
| 
 |  | ||||||
|         if (err === null) { |  | ||||||
|             inRes.json(res.rows[0]); |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|         inRes.json(err.message); |  | ||||||
|         //handle error
 |  | ||||||
|     }); |  | ||||||
| } |  | ||||||
| ); |  | ||||||
| 
 |  | ||||||
| //-------------------------------------------------------------set one or more map definitions--------------------------------------------------------------------------
 |  | ||||||
| 
 |  | ||||||
| server.use("/mapdef_set", bodyParser.json(), function (inReq, inRes) { |  | ||||||
| 
 |  | ||||||
|     //validate the body contents before pushing to sql?
 |  | ||||||
|     var sql = "SELECT x.message FROM tps.srce_map_def_set($$"; |  | ||||||
|     sql += JSON.stringify( inReq.body); |  | ||||||
|     sql += "$$::jsonb) as x(message)"; |  | ||||||
|     console.log(sql); |  | ||||||
| 
 |  | ||||||
|     Postgres.query(sql, (err, res) => { |  | ||||||
| 
 |  | ||||||
|         //Postgres.end();
 |  | ||||||
| 
 |  | ||||||
|         if (err === null) { |  | ||||||
|             inRes.json(res.rows[0]); |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|         inRes.json(err.message); |  | ||||||
|         //handle error
 |  | ||||||
|     }); |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| ); |  | ||||||
| 
 |  | ||||||
| //-------------------------------------------------------------add entries to lookup table--------------------------------------------------------------------------
 |  | ||||||
| 
 |  | ||||||
| server.use("/mapval_set", bodyParser.json(), function (inReq, inRes) { |  | ||||||
| 
 |  | ||||||
|     //validate the body contents before pushing to sql?
 |  | ||||||
|     var sql = "SELECT x.message FROM tps.map_rv_set($$"; |  | ||||||
|     sql += JSON.stringify( inReq.body); |  | ||||||
|     sql += "$$::jsonb) as x(message)"; |  | ||||||
|     console.log(sql); |  | ||||||
| 
 |  | ||||||
|     Postgres.query(sql, (err, res) => { |  | ||||||
| 
 |  | ||||||
|         //Postgres.end();
 |  | ||||||
| 
 |  | ||||||
|         if (err === null) { |  | ||||||
|             inRes.json(res.rows[0]); |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|         inRes.json(err.message); |  | ||||||
|         //handle error
 |  | ||||||
|     }); |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| ); |  | ||||||
| 
 | 
 | ||||||
| /* | /* | ||||||
| send a csv with powershell: | send a csv with powershell: | ||||||
| @ -252,8 +182,9 @@ server.use("/csv_suggest", upload.single('upload'), function (inReq, inRes) { | |||||||
| ); | ); | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     server.get("/", function (inReq, inRes) { | server.get("/", function (inReq, inRes) | ||||||
|         inRes.render("definition", { title: "definition", layout: "main" }); | { | ||||||
|     }) |     inRes.render("definition", { title: "definition", layout: "main" }); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
|     module.exports = server; | module.exports = server; | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user