How to programmatically disconnect a model from a loopback application?

I use loopback without a strongloop frame, that is, I do not have access to any of the cli tools. I can successfully create and start the loopback server and define / load some models this way:

var loopback = require('loopback');
var app = loopback();

var dataSource = app.dataSource
(
    'db',
    {
        adapter : 'memory'
    });
);

var UserModel = app.loopback.findModel('User');
UserModel.attachTo(dataSource);
app.model(UserModel);

/* ... other models loading / definitions */

// Expose API
app.use('/api', app.loopback.rest());

What I would like to achieve is the ability to disconnect the model from the loopback application at runtime, so it is not available from the rest of the API or loopback object (without having to restart the node script).

I know that you can remove the model definition made earlier from cli: Destroy the model in loopback.io , but this is not valid in my case, since it does this to remove the json objects loaded when strongloop loads, and this does not apply here.

, API-.

+4
1

: LoopBack.

, , issue # 1590.

API loopback

REST API. REST API, " ", , .

delete app.remotes()._classes[modelName];
delete app.remotes()._typeRegistry._types[modelName];
delete app._handlers.rest;

, LoopBack REST .

, , .

JavaScript API LoopBack, , :

delete app.models[modelName];
delete app.models[classify(modelName)];
delete app.models[camelize(modelName)];
app.models.models.splice(app.models.indexOf(ModelCtor), 1);

( , ).

loopback-datasource-juggler:

delete app.registry.modelBuilder.models[modelName];

:

  • / , .
  • , .
  • loopback-- API
+1

Source: https://habr.com/ru/post/1617450/


All Articles