-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Is 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.