How do you trigger population of db on startup of app in SolidStart?

How do you trigger population of db on startup of app in SolidStart?
6 Replies
Joe Pea
Joe Pea3w ago
That's more like a "how do I use a database" question, not Solid specific. How you do that is really up to you. You can write code in your server entry point that will check if the DB has content, and if not the populate it. Start here where it describes the default files, including the server entry: https://docs.solidjs.com/solid-start/getting-started#project-files and see a description of the server entry: https://docs.solidjs.com/solid-start/reference/entrypoints/entry-server Or you can do a check for existing data in your server endpoints, if you prefer: https://docs.solidjs.com/solid-start/building-your-application/api-routes It's really up to you. It could look like this:
// entry-server.tsx

import { createHandler, StartServer } from "@solidjs/start/server";
import {getDatabaseValue, setDatabaseValue} from "some-database-lib"

const value = await getDatabaseValue('foo')

if (!value) await setDatabaseValue('foo', 'bar')

// continue as before:
export default createHandler(() => (
// ...Same as before...
))
// entry-server.tsx

import { createHandler, StartServer } from "@solidjs/start/server";
import {getDatabaseValue, setDatabaseValue} from "some-database-lib"

const value = await getDatabaseValue('foo')

if (!value) await setDatabaseValue('foo', 'bar')

// continue as before:
export default createHandler(() => (
// ...Same as before...
))
entry-server.tsx - Solid Docs
Documentation for SolidJS, the signals-powered UI framework
API routes - Solid Docs
Documentation for SolidJS, the signals-powered UI framework
Getting started - Solid Docs
Documentation for SolidJS, the signals-powered UI framework
exercise
exercise3w ago
yeah that's how i'd do it
ctrl-z
ctrl-z3w ago
I currently take the local scripts approach to handle 1-off scripts like db-seeding. Basically just create a scripts dir with nodejs scripts in it and run via tsx scripts/script-name. Then it's like the above comments: just an issue of using the database.
Garrett Floyd
Garrett FloydOP2w ago
Thanks! Seems to work well. I would prefer to set up the db only when I run the startup command and not waste resources checking if the db is initialized every API call. I assume this is what entry-server does. From the docs I couldn't discern how entry-server does its magic. I noticed this approach throws an error when used with server functions. I'm pretty sure entry-server only runs on the server, but could someone confirm that I didn't just leak my db logic to the front end by removing "use server" and calling it in entry-server?
Joe Pea
Joe Pea2w ago
Where do you see an error? In the browser? Or server output? Server functions are on-demand, so they are little bit similar to running logic in an API call, but potentially shared with multiple API calls. Server functions typically do not run a DB (they could, but then it would be temporary) and connnect to an external DB. In a tradition single-instance long-running server, then yeah entry-server would run once at startup. With server functions it will run once per server function start.
Garrett Floyd
Garrett FloydOP2w ago
Both. Here is the error: [vite] (ssr) Error when evaluating SSR module $vinxi/handler/ssr: Cannot call server function outside of a request. I'm not sure what you mean by "Server functions typically do not run a db". I essentially have some files that are structured in the following way:

"use server";
imports go here...
export async function doSomeDbOp(params: Types){
await db.doSomeThing(moreParams);
}

"use server";
imports go here...
export async function doSomeDbOp(params: Types){
await db.doSomeThing(moreParams);
}

Did you find this page helpful?