Wanting to refactor how we call our DB
Unanswered
Japanese jack mackerel posted this in #help-forum
Japanese jack mackerelOP
the way i am currently calling my db is i have a db_access file under my api folder in pages within that file i set up my connection and then when i get a request i check what the request is and if its say GET i run
tbh im not a huge fan of this layout, im wondering if instead in this file i could simply define various functions as i need to make specific calls, and then when i call the api i pass to it the name of the function i want to call so it would look something like this:
slightly slimpified and pseudo but this was my rough thinking of the refactor, is this a good idea or is the current method better?
select * from ${query}tbh im not a huge fan of this layout, im wondering if instead in this file i could simply define various functions as i need to make specific calls, and then when i call the api i pass to it the name of the function i want to call so it would look something like this:
function handler(req, res) {
const connection = mysql.createConnection
connection.connect();
let data
if(req.query.function === "select2"){
data = select2(data)
}
res.json(data)
}
function select2(data){
return query
}slightly slimpified and pseudo but this was my rough thinking of the refactor, is this a good idea or is the current method better?
14 Replies
Tonkinese
Why not use server actions?
(im googling it)
@Tonkinese Why not use server actions?
Japanese jack mackerelOP
wait so with server actions i would create a function within say page.ts, put 'use server' at the top of said function, create/run my sql connection right there.
and then in page.ts i could make say an onclick button that would call said function and that would just..work?
and then in page.ts i could make say an onclick button that would call said function and that would just..work?
Tonkinese
Yes fetch the data on the server. You won’t be able to have an onclick in the page. As you are consuming a web api (on click) you will need to create a button component with the use client directive.
You can trigger a server action from a client component
There are lots of ways to achieve this
Japanese jack mackerelOP
function Home(){
const [data, setData] = useState()
function getData() {
'use server'
connection = mysql.createconnecction({})
connection.connect()
connection.query('select * from table', (error, results) => {
setData(results)
})
}
return (div.onClick={getData} CLICK ME
<div>{data}</div>)
}@Tonkinese Yes fetch the data on the server. You won’t be able to have an onclick in the page. As you are consuming a web api (on click) you will need to create a button component with the use client directive.
Japanese jack mackerelOP
this is sort of what my understanding of how this would work is
Tonkinese
Yeah something like tbat.
@Tonkinese Yeah something like tbat.
Japanese jack mackerelOP
and that works and its safe, maybe cause its in the same file as the html thats returned just slightly worried itll somehow expose the mysql.createconnection info
i could create another normal js file with helpers still, like createConnection thatlly return the connection that i call .connect to
Tonkinese
If you think this helps you please mark solution 🙂
@Japanese jack mackerel and that works and its safe, maybe cause its in the same file as the html thats returned just slightly worried itll somehow expose the mysql.createconnection info
Tonkinese
You won’t expose any secrets as your connection is server side. Only client side values can be accessed/leaked/