Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
elliott-with-the-longest-name-on-github opened this issue Jun 24, 2021 · 2 comments · Fixed by #6756
Labels
documentation Improvements or additions to documentation
Milestone

Comments

@elliott-with-the-longest-name-on-github
Copy link
Contributor

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 :)

@rmunn
Copy link
Contributor

rmunn commented Jul 16, 2021

I'd have one suggestion to make to improve that sample code. Instead of making ready a boolean, you could make it a Promise, and then await it in the handle hook or whatever else needs it. Awaiting a Promise that's already fulfilled returns immediately, although it does yield control to the next microtask so it's not instant. So there are times when you might not want that technique. But at other times, something like this might be useful:

let ready = new Promise(resolve => {
  const t1 = setupDb1();
  const t2 = setupDb2();
  const t3 = setupTelemetry();
  const t4 = setupAuth();
  Promise.all([t1, t2, t3, t4]).then(() => resolve(true));
};

const handle: Handle = async () => {
  // 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
  await ready;
  // Do something with DB or auth or whatever
}

@benmccann benmccann added the documentation Improvements or additions to documentation label Jul 23, 2021
@Rich-Harris Rich-Harris added this to the whenever milestone Apr 26, 2022
@PlopTheReal
Copy link

Any update on that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants