Files
blogger-nodejs/src/data.js
max e6476dcd4e Everything uses data.getdata()
data.getdata() excepts two parameters, if desired, the second will be the index, however if you don't specify anything, the whole array will be returned
Also, comments now have a composite key of postID-commentID, with each post's comments having their own set starting at 0, this makes it easier to index and find a specific comment, and making the getcomment() function unessesary
2025-09-24 17:06:13 +01:00

57 lines
1.4 KiB
JavaScript

import { createRequire } from "module";
const require = createRequire(import.meta.url);
const config = require("../config.json")
const fs = require("fs")
export function getdata(data, index=-1) {
if (config["data_storage"] == "json") {
if (data == "posts" || data == 'users' || data == 'comments') {
let result = require(`../data/${data}.json`)
if (index != -1) {
return result[index]
}
return result
}
else if (data == "other_data") {
let result = require('../data/data.json') // This file is actually called data.json
return result
}
else {
console.log("Error, invalid requested")
return 1
}
}
if (config["data_storage"] == "mysql") {
const mysql = require('mysql');
let con = mysql.createConnection({
host: config.database.host,
user: config.database.user,
password: config.database.password,
database: config.database.database,
});
con.connect(function(err) {
if (err) throw err;
if (data == "posts" || data == 'users' || data == 'comments') {
con.query(`SELECT * FROM ${data}`, function (err, result, fields) {
if (err) throw err;
console.log(result)
return result;
});
}
else if (data == 'hitcount') {
con.query(`SELECT paramValue FROM params WHERE paramName = '${data}'`, function (err, result, fields) {
if (err) throw err;
console.log(result)
return result;
});
}
});
}
}