Reputation: 251
I'm new to Javascript (and web development in general) and want to display data from a PostgreSQL server on a web page using ag-Grid. I've followed some tutorials and I have successfully pulled data from postgreSQL, and successfully displayed some sample data using ag-Grid. Now I'm trying to combine the two, and I can't figure it out.
I've created a 'database.js' which pulls the data from PostgreSQL - it's modified from this tutorial (LINK):
database.js:
const {Client} = require("pg");
const client = new Client ({
host: "<IPaddress>",
user: "<username>",
port: 5432,
password: "<password>",
database: "postgres"
})
client.connect();
var Query = function(){
client.query( `select * from test1`, (err, res) => {
callback (err, res)
client.end;
})
return {
Query1: Query
}
}();
module.exports = Query;
Then in main.js I have the following which has come from ag-grid tutorials:
main.js:
var QueryResults = require('database');
var columnDefs = [
{headerName: 'Make', field: 'make', sortable: true, filter: true},
{headerName: 'Model', field: 'model', sortable: true, filter: true},
{headerName: 'Price', field: 'price', sortable: true, filter: true}
];
var gridOptions = {
columnDefs: columnDefs,
rowModelType: 'infinite',
datasource: datasource,
};
var eGridDiv = document.querySelector('#myGrid');
new agGrid.Grid(eGridDiv, gridOptions);
There are a few concerns that I have:
Any advice would be greatly appreciated.
Upvotes: 0
Views: 668
Reputation: 30088
You need to understand that in this sort of application, there are two separate places where code is executed.
On the "server", e.g. the computer where the database is running, or another computer that connects to it, there is an application that that is doing operations including querying the database, and providing the web application code to the user's web browser. This is also commonly referred to as "the backend".
On the "client" (the user's web browser), commonly referred to as "the frontend", the web application will get data from the backend via HTTP/S ("API calls").
In the sample code that you provided,the functionality in database.js belongs on the backend (on the server), and the code that you have in main.js belongs on the frontend (in the user's web browser).
Upvotes: 1