Expose a way to inject a start script into adapter-node
See original GitHub issueIs your feature request related to a problem? Please describe.
I’m tying to migrate from Sapper to SvelteKit. I’m aware of the discussion in #334 and hooks seem to solve that.
I don’t think there is currently a way to have code that runs on startup with the Node adapter. There are things that need to happen once and not for every request. In Sapper my server.js
looked like this:
import sirv from 'sirv';
import express from 'express';
import compression from 'compression';
import bodyParser from 'body-parser';
import * as sapper from '@sapper/server';
import { Model } from 'objection';
import knex from './lib/knex.js';
import env from './lib/env.js';
Model.knex(knex); // <==============================================
const app = express();
app.use(compression({ threshold: 0 }));
app.use(sirv('static', { dev: env.isDev }));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(sapper.middleware());
(async () => {
await knex.migrate.latest({ // <==============================================
directory: './src/migrations',
});
app.listen(env.PORT, (err) => {
if (err) {
console.error(err);
}
});
})();
The first line I’ve marked is something that would probably not hurt if done in every hook (setting up my Objection models with the knex instance).
The second line I’ve marked is running the database migration. In a serverfull environment this is a rather common occurrence. I want to run the migration right before starting the server so that I’m in a consistent state between database and application code. Even if this is scaled across multiple instances it works, since the second time the migrations run it’s a NOOP. I honestly have no idea how people do that in a serverless environment with a guarantee that no code is run that expects an outdated schema resulting in possibly undefined behavior.
Describe the solution you’d like
A way to run setup code before the server starts. Maybe an async function setup() {}
that is awaited before the server starts? But that can’t work with every adapter.
Describe alternatives you’ve considered
I guess some will argue I should have a second API server separated from SvelteKit and whatnot. But I want less complexity, not more.
How important is this feature to you?
I cannot migrate to SvelteKit, unless I’m missing something.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:10
- Comments:46 (5 by maintainers)
Top GitHub Comments
I’m confused why this isn’t a more important issue. There should be an easy way to call code on app startup both in dev mode and when built. Relying on a first request to do initialization doesn’t make any sense.
I use a hack-around for my SvelteKit TypeScript Node project:
Here’s how it works:
await setup
as it has already been resolvedOf course, having a
setup
hook would be cleaner and more straightforward than this. 👍