Einkaufslisten anzeigen, Datenbankeinbindung
This commit is contained in:
		
							
								
								
									
										9
									
								
								express-server/db-config/keys.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								express-server/db-config/keys.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
module.exports ={
 | 
			
		||||
    postgresSQL:{
 | 
			
		||||
        user:"postgres",
 | 
			
		||||
        host:"localhost",
 | 
			
		||||
        database:"SmartShopperDB",
 | 
			
		||||
        password:"postgres",
 | 
			
		||||
        port:"5432"
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										45
									
								
								express-server/db-config/postgresql-common.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								express-server/db-config/postgresql-common.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
			
		||||
const { Client } = require ("pg");
 | 
			
		||||
const {postgresSQL} = require("./keys")
 | 
			
		||||
 | 
			
		||||
const client = new Client(postgresSQL);
 | 
			
		||||
 | 
			
		||||
async function connect() {
 | 
			
		||||
    try{
 | 
			
		||||
    await client.connect();
 | 
			
		||||
    console.log("connected!");
 | 
			
		||||
    }
 | 
			
		||||
    catch(error)
 | 
			
		||||
    {
 | 
			
		||||
        console.error(error)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
connect();
 | 
			
		||||
 | 
			
		||||
async function query(queryString,param)
 | 
			
		||||
{
 | 
			
		||||
    try {
 | 
			
		||||
    let result = await client.query(queryString,param);
 | 
			
		||||
    let resultarray = [];
 | 
			
		||||
    for(let row of result.rows)
 | 
			
		||||
    {
 | 
			
		||||
        resultarray.push(row.obj);
 | 
			
		||||
    }
 | 
			
		||||
    return resultarray;
 | 
			
		||||
    }
 | 
			
		||||
    catch(error)
 | 
			
		||||
    {
 | 
			
		||||
        console.log(error)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function nonQuery(queryString,param) {
 | 
			
		||||
    try {
 | 
			
		||||
        await client.query(queryString,param);
 | 
			
		||||
    } catch (error) {
 | 
			
		||||
        console.error(error)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
    query, nonQuery
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user