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

Can some updates be made so all config elements can be modularized in all environments including development? #1083

Open
scottconso opened this issue Feb 23, 2023 · 0 comments

Comments

@scottconso
Copy link

This was originally posted as a question at the end of #677 Is there any way to modularize routes? But it has not gotten a response because that issue is closed. So opening this as a new issue.

I did the second way because I need to modularize all config elements, not just routes. And it does work fully in environment 'test' - server.config() does seem to be additive for all the elements I am using - models, factories, serializers and routes - so I can add them piece-by-piece. And then when I call my seed data function in each test, it correctly seeds data using the models, factories, etc., and everything works. My basic steps are:

  1. Construct the server object (with just environment in the initial config) with createServer
  2. Call all my add....(server) functions to add my config elements piece-by-piece (no seeds part in the config)
  3. Call my seed...Data(server) function in each test to seed the data

But I have not been able to quite get it to work in environment 'development'. And it has to do with the fact that 'development' environment seems to require that the seed data function(s) be called through the seeds part of the config. Rather than after the server object is constructed.

So there ends up being a timing problem. The seeds part of the config seems to execute while the server object is being constructed, but at that time I have not yet added my config pieces such as models and factories, so the seed data comes out wrong.

So, I need to manually call my seed...Data(server) function after the server object is constructed, same as I do with 'test' environment, but that does not seem to be working. My seed data still comes out wrong, as if it still does not have visibility/access to the factories, even though by then they are there.

So, does anyone know a way to get this to work? With 'development' environment, to manually call seed...Data(server) function(s) after the server object is constructed and not use the seeds part of the config?

If there is not a way, could a fix be provided so that it could work this way? It seems like a sound concept - to be able to manually seed data after the server object is constructed, with any environment including 'development'.

So, my start server function is like:

export function startMocksServer({ environment = 'test' } = {}) {
  const server = createServer({
    environment,
  });

  addModels(server);
  addFactories(server);
  addSerializers(server);
  addRoutes(server);

  return server;
}

And I am trying to call it for 'development' like:

if (process.env.NODE_ENV === 'development') {
  const server = startMocksServer({ environment: 'development' });
  seedMockData(server);
}

Instead of the usual:

if (process.env.NODE_ENV === 'development') {
  startMocksServer({ environment: 'development' });
}

And, if I simply change environment: from 'development' to 'test', it does work, I just lose the console logging in the browser, and my response delays are shorter.

So, it seems tantalizingly close and as if it should work. Can you investigate and see if this could be made to work? (Seeding the data after-the-fact with any environment including 'development'.)

I am using version 0.1.46, which is the latest at the time of writing this.

Originally posted by @scottconso in #677 (comment)

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

No branches or pull requests

1 participant