How to find loopback auto generated rest api names? - node.js

I was generated loopback.js framework auto generated rest api, I was trying to write acls for the rest api, that table contains multiple relations with other tables. I want to restrict every rest api call by using their names how do I find rest api names to write acls in loopback.js,
I mean if any rest api like "/users/{id}/requests" how to find this kind of rest api name. I am looking for any source or any suggestions.

From LoopBack documentation:
When two models have a relationship between them (see Creating model relations), LoopBack automatically creates a set of related model methods corresponding to the API routes defined for the relationship.
In the following list, modelName is the name of the related model and modelNamePlural is the plural form of the related model name.
belongsTo:
__get__relatedModelName
hasOne:
__create__relatedModelName
__get__relatedModelName
__update__relatedModelName
__destroy__relatedModelName
hasMany:
__count__relatedModelNamePlural
__create__relatedModelNamePlural
__delete__relatedModelNamePlural
__destroyById__relatedModelNamePlural
__findById__relatedModelNamePlural
__get__relatedModelNamePlural
__updateById__relatedModelNamePlural
hasManyThrough:
__count__relatedModelNamePlural
__create__relatedModelNamePlural
__delete__relatedModelNamePlural
__destroyById__relatedModelNamePlural
__exists__relatedModelNamePlural (through only)
__findById__relatedModelNamePlural
__get__relatedModelNamePlural
__link__relatedModelNamePlural (through only)
__updateById__relatedModelNamePlural
__unlink__relatedModelNamePlural (through only)
hasAndBelongsToMany:
__link__relatedModelNamePlural
__unlink__relatedModelNamePlural

Related

OpenAPI Generator issue with Destination service API specification

I want to get all destinations on subaccount and instance level. In SAP API business Hub, I found the API information and "SAP Cloud SDK" tab to generate code by OpenAPI generator.
https://api.sap.com/api/SAP_CP_CF_Connectivity_Destination/overview
I downloaded the API specification and added dependencies into Cloud SDK for Java project. The code is generated successfully with some errors (unknown models)in generated api classes.
For example in DestinationsOnSubaccountLevelApi.class, model OneOfDestinationNameOnly is imported and used in method but it is not generated in model package.
I looked into API specification and found that there were two types of response entity. That is the reason why the code could not be generated properly. I can modify the API specification to make it work but it should not be the long term solution. Is there any other way to fix this issue?
Unfortunately the SAP Cloud SDK Generator for Open API services is not yet able to understand oneOf relationship that is modeled in the specification.
As an alternative, would you consider using the DestinationAccessor API for resolving single destinations?
You can also directly instantiate an ScpCfDestinationLoader, which allows for querying all destinations:
ScpCfDestinationLoader loader = new ScpCfDestinationLoader();
DestinationOptions options = DestinationOptions
.builder()
.augmentBuilder(ScpCfDestinationOptionsAugmenter.augmenter().retrievalStrategy(ScpCfDestinationRetrievalStrategy.ALWAYS_SUBSCRIBER))
.build();
Try<Iterable<ScpCfDestination>> destinations = loader.tryGetAllDestinations(options);
Similar to the default behavior of DestinationAccessor API, in the code above only the subscriber account will be considered. Other options are:
ScpCfDestinationRetrievalStrategy.ALWAYS_SUBSCRIBER
ScpCfDestinationRetrievalStrategy.ALWAYS_PROVIDER
ScpCfDestinationRetrievalStrategy.SUBSCRIBER_THEN_PROVIDER

How to load a child component of a relation in Strapi

In my Strapi application, I have the following structure:
Footer(single type) > Section(component) > Menu(collection type) > link(component)
When performing a query Strapi only returns the id of the link and I needed its LABEL and URL, does anyone know how I can bring this in a query only?
Strapi section
Link Component
Footer API return
I used this topic on the Strapi forum, but without success yet
https://forum.strapi.io/t/how-do-i-return-only-selected-fields-from-the-model-and-its-relation/1115/2
Disclaimer; im new to Strapi.
The Strapi get requests only populates with relational data in a single layer of nesting. Your links looks to be on the second layer.
You have two options:
Query for the links by ID from your client after receiving the IDs. This can be done efficiently using Promise.all.
Modify the default GET handler for the ressource in the Strapi code. Extend the query to join on your links, and return the response in its entirity.
Update on option 2;
You can customize the logic behind any single endpoint handler in the API in your Strapi backend. The process is best descriped in their documentation on Custom Data Response's. Note that the syntax on how to query the database is based on a popular ORM depending on your database of choice. The ORMs are:
MongoDB (through Mongoose)
Postgres, MySQL, SQLite3 and more (through Bookshelf)
Redis (through ioredis).

Cosmos Db: SQL API + Graph API -- Really multi-model?

This is my scenario:
I want to have one single collection where I can insert and query documents using SqlApi and create vertices and edges using Graph Api, all in the same collection.
I believed that that was possible taken into account what I've read on the documentations.
My first try was using Microsoft.Azure.Graphs.dll
With this approach I was able to do CRUD operations with the Sql Api and execute gremlin scripts against the collection.
It is important to note that the documents created with the Sql api Insert command where used as vertices. Then I created edges using the graph api connecting the documents created with the Sql Api. This works as expected.
The only problem I had was that if the document contains an array property the graph Api returned and error : Invalid cast from 'System.String' to 'Newtonsoft.Json.Linq.JObject'.
After investigating I was told that the Microsoft.Azure.Graphs.dll was deprecated and I should use Gremlin.Net instead.
I have not read in any place that this assembly is deprecated and even in the official documentation and examples I can see that this assembly is being used.
Is it really deprecated? Should I not use it?
Then this was my second try:
I moved to Gremlin.net.
First issue: I was connecting to a collection created originally for Sql Api. When I tried to connect with Gremlin.Net Client it tolds me that can not connect to the server.
When I created another database and collection for graph Api I was able to connect.
Conclusion: It's not possible to use Gremlin.net with a collection created with Sql Api.
Or is it possible to activate the gremlin endpoint in a database with Sql Api?
Using the new collection for Graph Api I tried again to create documents with the Sql Api and then connect using Graph Api. I see that in this case both endpoints are created: SqlApi+Gremlin.
I had a few problems making this works. For example I had to set the GraphSon writer and reader to version 2, if not I received a null reference exception.
Second issue: I was able to create documents with Sql Api but I had the same problem with the array property (Example Document { "id":"aaa" "Prop": [ "1", "2"] } )
I get the same error when I query with gremlin: g.V('aaa') => Invalid cast from 'System.String' to 'Newtonsoft.Json.Linq.JObject'.
Conclusion: My first issue with previous library was not solved with the new one.
Third issue: The json returned when I query with SqlApi the edges or vertices are different from received when I used Microsoft.Azure.Graphs.dll. It looks like the cosmos db engine handles differently the gremlin scripts depending on the assembly. Which the json format I should expect?
Notes:
-Why I need to create vertices using SqlApi?
Because I want to create properties with custom objects and I'm not able to do it with graphApi:
Example: { "Id": "aaa", "Custom" : { "Id": 456, "Code" : { "Id": 555, "Name": "Hi"} }
-Why I want to query graph using SqlApi?
Because I want to access my custom properties.
Because I want to paginate using tokens. (Range gremlin function it is not performant. It traverse all the vertex/edges from 0 to the last page I want)
Has someone some information about this situations?
Help will be appreciated.
I know this post is a bit older, but it looks like it is possible to use graph and sql api. The key is in adopting the gremlin's underlying storage format when using the sql api for querying/manipulating. Please check this post out:
https://vincentlauzon.com/2017/09/05/hacking-accessing-a-graph-in-cosmos-db-with-sql-documentdb-api/

How can i access different models with same plural in loopback?

I want to access different models with same plurals. Is there any way in loopback to do so, e.g.:
get-account.json
{
"name": "getAccount",
"plural": "account"
}
to access get-account.js and inside it remoteMethod with http path /
get-smtp-account.json
{
"name": "getSmtpAccount",
"plural": "account"
}
to access get-smtp-account.js and inside it remoteMethod with http path /smtp
I think you are missing the point of Restful resources.
The restful specification is all about using the http verbs, representing CRUD actions like this:
GET: READ -
POST: CREATE -
PUT: UPDATE -
DELETE: DELETE
Each model should represent a resource, that would be the name of a single entry in the persistance, for example, account.
Because we are storing a collection of models, we use a "plural" to refer to this resources, thats why all endpoints should be named in plural, in this example that would be accounts.
So, in any restful resource, if you wanna read data from the persistance, you should use
GET http://0.0.0.0/api/accounts
In loopback, you can create relations between models. This relations, are representations of the relations in the persistance (db). So you can create a model called "smtp".
After the relation is made, you can query the account and "include" the smtp relation, bringing together all users with their data.
Check the documentation for more information https://docs.strongloop.com/display/public/LB/Creating+model+relations
If you have more questions, like, how to create relations between models, i'm really happy to help you in another question.

How to programmatically detach a model from a loopback application?

I am using loopback without the strongloop framework itself, meaning I have no access to any of the cli tools. I am able to succesfully create and launch a loopback server and define/load some models in this fashion:
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 to be able to detach a model from the loopback application at runtime, so it is not available from the rest API nor the loopback object anymore (without the need to restart the node script).
I know it is possible to remove a model definition made previously from the cli:
Destroy a model in loopback.io, but this is not valid in my case since what it does is to remove the json objects that are loaded at strongloop boot, which is not applicable here.
I would appreciate very much any help regarding this, I have found nothing in the strongloop API documentation.
Disclaimer: I am a core developer of LoopBack.
I am afraid there is no easy way for deleting models at runtime, we are tracking this request in issue #1590.
so it is not available from the rest API nor the loopback object anymore
Let's take a look at the REST API first. In order to remove your model from the REST API, you need to remove it from the list of "shared classes" maintained by strong-remoting and then clean the cached handler middleware.
delete app.remotes()._classes[modelName];
delete app.remotes()._typeRegistry._types[modelName];
delete app._handlers.rest;
When the next request comes in, LoopBack will create a new REST handler middleware and rebuild the routing table.
In essence, you need to undo the work done by this code.
In order to remove the model from LoopBack JavaScript APIs, you need to remove it from the list of models maintained by application's registry:
delete app.models[modelName];
delete app.models[classify(modelName)];
delete app.models[camelize(modelName)];
app.models.models.splice(app.models.indexOf(ModelCtor), 1);
(This is undoing the work done by this code).
Next, you need to remove it from loopback-datasource-juggler registries:
delete app.registry.modelBuilder.models[modelName];
Caveats:
I haven't run/tested this code, it may not work out of the box.
It does not handle the case where the removed model has relations with other models.
It does not notify loopback-component-explorer about the change in the API
Update
There's now a function called deleteModelByName that does exactly that.
https://apidocs.strongloop.com/loopback/#app-deletemodelbyname
https://github.com/strongloop/loopback/pull/3858/commits/0cd380c590be7a89d155e5792365d04f23c55851

Resources