We are using the LoopBack REST framework to expose our database (and business logic). We need to allow our customers to create custom tables in the database (single and multi-tenant) which can be accessed via a REST endpoint. All customers need to use the same common (production) REST endpoints which will be on exposed on multiple servers. However, custom tables and associated REST endpoints need to be accessible to only the customers that created them. Which means we cannot write the model for custom tables to disc. We need to be able to create an actual REST endpoint on the fly within the context of our production REST endpoints.
Question: How can we dynamically create custom REST endpoints in-code (on the fly) without writing the model to a JSON file on-disc?
You can create a "remote method" within a model's JS file, this adds the API hook "at runtime", although it is at startup. That said, I think you could use the same functions to add the endpoint any time, not just at startup (although I've never tried):
Inside
/common/models/MyModel.js
I have been facing a similar problem in loopback. The solution I came up with was:
Maintain a table [id INT, modelName VARCHAR, datasource VARCHAR, modelConfig TEXT] in my database which could hold the model definition of any other table.
Create a model (call it X) in loopback to provide CRUD operations on this table.
Create remoteMethod in X to update models in the app object and attach to any datasource. The implementation snippets are as follows: