Skip to content

How to load the collections statically #905

Description

@ferjul17

Expected behavior

I would like to build my forest admin project with esbuild into a single js file. That's almost working perfectly.
However, I have defined actions in some collections. I would like to import statically these collections otherwise esbuild won't bundle them.

Actual behavior

Currently, the only way to import collections is by providing a configDir in LianaOptions. This will load dynamically (through a call to requireAll()) all the files in the provided directory.

Failure Logs

N/A

Context

I'm using forest-express-sequelize@8.5.7 which requires forest-express@9.5.1.

Here is my attempt:

const lianaOptions: LianaOptions = {
  authSecret: readFromEnv('FOREST_AUTH_SECRET'),
  // configDir will load dynamically js files. It is replaced by the explicit import of ../forest
  configDir: `pathWhichMustNotExist${Math.random()}`,
  connections,
  envSecret: readFromEnv('FOREST_ENV_SECRET'),
  objectMapping,
  schemaDir: join(__dirname, '../..'),
};

require('../forest');
app.use(await init(lianaOptions));

require('../forest') imports a call to the function collection() like this:

collection('contracts', {
  actions: [
    // ...
  ],

It does not work because Schemas.schemas was not yet initialized and the collection is not loaded (see https://github.com/ForestAdmin/forest-express/blob/main/src/index.js#L402).

If I require the collection after the initialization, the schema .forestadmin-schema.json has been written and does not include the actions.

If we could have a (asynchronous) function to load the collections in addition to the configDir that would solve my problem.

Maybe something like:

if (configStore.doesConfigDirExist()) {
  loadCollections(configStore.configDir);
}

if (typeof configStore.loadCollections === 'function') {
  await configStore.loadCollections();
}

I would appreciate your help or feedbacks. If you like the idea, I can create a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions