Search code examples
restdynamicendpointloopbackjsstrongloop

LoopBack: How to Dynamically Create Custom REST Endpoints In Code (On The Fly)


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?


Solution

  • 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

    module.exports = function(MyModel){
    
      // This is the endpoint for creating endpoints...
      MyModel.addEndpoint = function(name, method, cb) {
        // audit name and method...
    
        MyModel[name] = function(options, newCb) {
          // do whatever this endpoint should do...
          newCb(null, 'New endpoint success!');
        };
    
        MyModel.remoteMethod(
          name, 
          {
            accepts: [{arg: 'options', type: 'object'}], // or whatever you need...
            returns: {arg: 'message', type: 'string'}, // whatever it returns...
            http: {verb: method}
          }
        );
    
        cb(null, 'Success creating new endpoint!');
      };
    
      MyModel.remoteMethod(
        'addEndpoint', 
        {
          accepts: [
            {arg: 'name', type: 'string', http: {source: 'body'}},
            {arg: 'method', type: 'string', http: {source: 'body'}}
          ],
          returns: {arg: 'message', type: 'string'},
          http: {verb: 'post'}
        }
      );
    };