Do I need Dynamic Imports to do this?

I’m trying to figure out if what I’d like to do is possible, or if it will require dynamic imports.

I have a lot of code inside a module that just performs “data extraction”. Meaning it just queries mongo and loads it into an object for consumption by other modules. The twist is that of course I’ll need an ID to query on and the ID is only known once the client makes a server side call to a Meteor Method.

For example:

// imports/server/data-extraction.js

import Test from '../collections/test';
import { THE_USER } from '../server/globals';

let query = { _personId: THE_USER };


let {
	FirstName,
	MiddleName,
	LastName,
} = Test.findOne(query);

Docs = {};

Docs.Test = {
    FirstName,
    MiddleName,
    LastName,
}

export default Docs;

So when the application starts, the module is loaded, and there’s no data. It’s only after the Meteor Method is called that THE_USER is known and I can query for it in this module. Yet, by this time it’s too late, the module is already loaded and never gets loaded again. So the way I “fixed” this is to wrap this all in a function that is called “on-demaind”.

// imports/server/data-extraction.js

import Test from '../collections/test';
import { THE_USER } from '../server/globals';

const CallMeWhenYouHaveTheData = () => {
  let query = { _personId: THE_USER };

  let {
	FirstName,
	MiddleName,
	LastName,
  } = Test.findOne(query);

  Docs = {};

  Docs.Test = {
    FirstName,
    MiddleName,
    LastName,
  }

  return Docs;
}


export { CallMeWhenYouHaveTheData };

Now in another module that imports the above module, I can simply call the method when the ID is known and all works out okay. But the thing is, I’d like to not have to wrap this code in a function, and only have the module loaded/called when I have the ID from the Meteor Method. Is what I’m looking for Dynamic Imports or is there another way?

This seems like a weird thing to do. Why would you not want this as a function? And why import UserId from server/globals, instead of giving it as an argument to the function, or calling Meteor.userId()?

I would just like the option to load a module at the right time, I shouldn’t have to wrap the data in a function to do that – right?. Without dynamic imports, I’m forced to wrap things in functions, simply for the sake of controlling timing. I should be able to control timing via the import statement. Also, If I have, say, 100 variables inside a module, it’s easier to control what gets exported/imported if I don’t have to wrap things within functions IMO.

Because it’s not the UserId/Meteor.userId() that I’m querying for! It’s an ID that an Admin selects for a particular user.