estatesfunding/ef-api/controllers/mysqlproperty.js

63 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-09-17 14:36:35 +00:00
import mysql from "mysql2";
2024-09-19 11:35:14 +00:00
2024-09-17 14:36:35 +00:00
const db = mysql.createConnection({
host: "db-mysql-nyc1-99306-do-user-12431193-0.b.db.ondigitalocean.com",
user: "doadmin",
password: "AVNS_EPHqPilzmVjZfm8GH4G",
database: "defaultdb",
port: "25060",
2024-09-17 15:14:56 +00:00
connectTimeout: 20000, // 20 seconds
2024-09-17 14:36:35 +00:00
});
2024-09-19 11:35:14 +00:00
// const db = mysql.createConnection({
// host: "localhost",
// user: "root",
// password: "root",
// database: "estates",
// port: "3306",
// connectTimeout: 20000, // 20 seconds
// });
2024-09-17 14:36:35 +00:00
// Connect to MySQL database
db.connect((err) => {
if (err) {
console.error("Database connection failed: " + err.stack);
return;
}
console.log("Connected to the MYSQL database.");
});
// Controller function
2024-09-19 11:35:14 +00:00
// export const searchMySQL = (req, res) => {
// const q = "SELECT * FROM client_information";
// // const q = "SELECT * FROM home_information";
// db.query(q, (err, data) => {
// if (err) {
// console.log(err);
// return res.status(500).json({ error: "Database query failed" });
// }
// return res.json(data);
// });
// };
2024-09-17 14:36:35 +00:00
export const searchMySQL = (req, res) => {
2024-09-19 11:35:14 +00:00
const limit = parseInt(req.query.limit) || 10; // Default to 10 items per page
const offset = parseInt(req.query.offset) || 0; // Default to the first page
const q = `SELECT * FROM home_information LIMIT ${limit} OFFSET ${offset}`;
2024-09-17 14:36:35 +00:00
db.query(q, (err, data) => {
if (err) {
console.log(err);
return res.status(500).json({ error: "Database query failed" });
}
2024-09-19 11:35:14 +00:00
// Set cache-control headers to prevent caching
res.set('Cache-Control', 'no-store');
2024-09-17 14:36:35 +00:00
return res.json(data);
});
};
2024-09-19 11:35:14 +00:00