Skip to content

Discussion: Documentation around singletons/runtime setup (db connections seem most common) #1753

@elliott-with-the-longest-name-on-github

Description

Is your feature request related to a problem? Please describe.
This is a request for discussion.

One problem brought up in the Discord server pretty often revolves around one-time setup that should happen at startup, where "startup" is either defined as "whenever the server starts" or, for adapter-static, "whenever the app runs on the user's browser".

In the FAQs, we have a "How do I setup a database?" section. This section documents functionality that is otherwise not documented anywhere, specifically around hooks.js. It says "You can execute any one-time setup code in hooks.js and import your database helpers into any endpoint that needs them." I imagine that would look something like this:

let ready = false;
const executeSetupActions = async (): void => {
  const t1 = setupDb1();
  const t2 = setupDb2();
  const t3 = setupTelemetry();
  const t4 = setupAuth();
  await Promise.all([t1, t2, t3, t4]);
  ready = true;
}();

const handle: Handle = () => {
  // For server-side code, this shouldn't REALLY matter unless you receive a request in the short
  // amount of time AFTER the server has started but BEFORE the connections are initialized
  while (!ready) {
    await new Promise(resolve => setTimeout(resolve, 2000));
  }
}

Describe the solution you'd like

  1. In the Hooks section of the documentation, document that setup code can be run in hooks outside of the two exported functions getSession and handle. (It might be nice to also document what this does if you're using adapter-static.) If the SvelteKit-approved place to run setup code is in hooks.js, it should be documented with the documentation about hooks.js.
  2. Include something like the code examples above in the "setting up a database" FAQ.'
  3. It might also be a good idea to just document how to set up singletons. Yes, you should know this as a JavaScript developer, but it's such a common use case that it might fit in nicely at the bottom of the FAQs.

Describe alternatives you've considered

  1. Just leave it alone and continue answering this question in the Discord... often.

How important is this feature to you?

Important enough that I'm willing to make said adjustments in a pull request if I get the go-ahead from a maintainer :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions