Is there way to Create Database and Tables on startup? #2437
-
Maybe a better title for this question, "How to run a piece of code on startup only"
The above code is from using Fastapi But I want to do this in hono. Simply put, I want to run some code that should be called only once at startup, not before every request, maybe I can use singleton pattern but is there a built-in method like FastApi has? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
hey Mohsin, usually you don't need to perform a "create" every time, because if is already created why would you want to perform the same query again? One way of controlling that is by using migrations. For instance, using kysqly to connect to your DB:
This command is usually called on every deployment to apply the new required changes, then you can start your code as usual. If your question is not related to the DB, and you wish to perform a code once on startup, you can add a new line after the new hono instance const app = new Hono()
app.onError((error, c) => {
// ...
})
// ...
console.log('initial load') |
Beta Was this translation helpful? Give feedback.
-
Hono is only a router not the server, so it does not have control of those lifecycles methods like python frameworks do. This would depend on the runtime. For example in Bun you can import the Hono instance and put it in a fetch. From there you can access the startup and exit and run functions before each. You can even control if this would rerun every time the code is hot reloaded. |
Beta Was this translation helpful? Give feedback.
Hono is only a router not the server, so it does not have control of those lifecycles methods like python frameworks do. This would depend on the runtime. For example in Bun you can import the Hono instance and put it in a fetch. From there you can access the startup and exit and run functions before each. You can even control if this would rerun every time the code is hot reloaded.