simov / express-admin

MySQL, MariaDB, PostgreSQL, SQLite admin for Node.js
MIT License
1.18k stars 223 forks source link

Allow the use of JS files to configure things #134

Closed yihangho closed 1 year ago

yihangho commented 7 years ago

I'm not sure if this has been discussed/considered before. Currently, the configuration files (config.json, settings.json, custom.json, and users.json) have to be JSON files since they are hardcoded as such in the codebase.

This has some limitations - the most important one being they are static. For example, it is quite common for us to configure the database credentials using environment variables. This will not be possible with JSON files. My current solution to this is to generate these JSON files right before starting express admin. (Think of this as running generateJsonFile && admin config.)

I think this is suboptimal. My proposal here is to accept JS files in addition to JSON files.

Pros:

  1. Allows various configuration options to be set dynamically (e.g., the database credential issue I mention here).
  2. Opens up new possibilities for the kind of things that we can do with express-admin. For example, we can add functions to settings.js to allow arbitrary formatting of data or even dynamically generated data. For example:

    • If we store an enum as an integer column (such as 0 = male, 1 = female), we can have a, say, formatter function that maps a raw value into a human-friendly string.

Cons:

  1. There might be some backward-incompatibility issue. We can fix this by making the JS config files strictly opt-in. (For e.g., enabled by a command line flag or always read JSON files if they are present.)

If this is something that the maintainers are willing to consider, I'm happy to come up with a prototype to get things started. 😃

Hillshum commented 6 years ago

Just doing require(path.join(args.dpath, 'config'), leaving off the extension, would allow JS files that export a config object. Literally no other changes needed.

tilomitra commented 6 years ago

Any update on this?

simov commented 6 years ago

Check out the embedding section of the docs.

simov commented 1 year ago

Hi, check out v2 of the admin. In v2 the admin is simply an Express.js middleware. Also, a little bit down below I am explaining exactly about that case and the fact that only the settings.json is required to be a file stored on your server, but then even that can now be manipulated before passing as an object to the middleware, even though I don't know how useful that would be. But for the rest of the config, in particular the config and the users key it is actually recommended to load that from an external storage since that contains your password for the database connection and for your admin users respectively.

Also have a look at the changelog for any potential breaking changes, all are pretty minor.