SolidJSS
SolidJS14mo ago
2 replies
Cone

Run server code only once

I'm trying to run an embedded db in SolidStart, but every time a new route is visited the db initialization is re-run and the db data gets corrupted. Is there a way to only run the db code once and reuse it for all routes? I saw this issue in nitro which seems like it would solve it, but the code still runs for every route https://github.com/nitrojs/nitro/issues/711#issuecomment-1415819402

My db initialization in utils/db.ts looks like:
import { Database } from "db"

const DATA_DIR = "./.data"

let _db: Database
export const useDB = () => {
    if (!_db) {
        _db = new Database(DATA_DIR)
    }
    return _db
}
GitHub
Describe the feature It would be nice if you could provide objects from Nitro plugins, like you can with Nuxt plugins. In my opinion, this would make the usage of something like Prisma ORM with Nit...
Add 'provide' option to Nitro Plugins. · Issue #711 · nitrojs/nitro
Was this page helpful?