Getting joined data from strongloop/loopback - node.js

How can I get data from two joined tables?
Suppose, there are two models called Category (CategoryId, CategoryName) and Product(ProductId, ProductName, CategoryId), Is there a way to get a result like:(ProductId, ProductName, CategoryId, CategoryName)

There should be a relation between your Category model and your Product model. A Category hasMany Products and each Product belongsTo a Category. So your model json files should be something like
Category:
{
"name":"Category",
"properties":{
"CategoryId": {
"type":"Number",
"id":1
},
"CategoryName":{
"type":"String"
}
},
"relations": {
"products": {
"type":"hasMany",
"model":"Product",
"foreignKey":"CategoryId"
}
}
}
Product
{
"name":"Product",
"properties":{
"ProductId: {
"type":"Number",
"id":1
},
"ProductName":{
"type":"String"
},
"CategoryId": {
"type":"Number"
}
},
"relations": {
"category": {
"type":"belongsTo",
"model":"Category",
"foreignKey":"CategoryId"
}
}
}
Of course these json definitions must be completed with the corresponding options and datasource properties, which only you do know.
The relation between those two models will add endpoints to loopback explorer so you can query for products in a certan category:
GET /api/Categorys/:id_category/products
of the category to which a product belongs
GET /api/Products/:id_product/category
Please note that, unless you specify a plural option for Category, its plural will be Categorys. This is not a typo.
Finally, if you want to query for a product and its category, you would use the include filter
GET /api/Products/:id_product?filter[include]=category
hope it helps.

Related

How to make complex Prisma Schema from JSON data?

I'm very new to Prisma ORM. All the tutorials and articles show the basics of model and relations.
But my use case is different, I want to convert the below JSON to Prisma model.
Think this is an Item model and has a relation with options and additions. And options has relation with list.
And what would be the model definition for image object in Prisma model.
Thanks in advance.
{
"id":1,
"name":"American Classic",
"image":{
"title":"Image Title",
"url":"https://assets.suelo.pl/soup/img/products/burger01.jpg"
},
"description":"Beef, Cheese, Potato, Onion, Fries",
"price":9,
"options":[
{
"id":1,
"name":"Size",
"list":[
{
"id":1,
"name":"Normal - 200g",
"price":0
}
]
},
],
"additions":[
{
"id":1,
"name":"Prosciutto",
"price":3
}
]
}

It is possible to have varying data structures in an Azure search index?

Below is some of the data I'm putting into an Azure search index:
I could go with this rigid structure but it needs to support different data types. I could keep adding fields - i.e. Field4, Field5, ... but I wondered if I could have something like a JSON field? So the index could be modelled like below:
[
{
"entityId":"dba656d3-f044-4cc0-9930-b5e77e664a8f",
"entityName":"character",
"data":{
"name":"Luke Skywalker",
"role":"Jedi"
}
},
{
"entityId":"b37bf987-0978-4fc4-9a51-b02b4a5eed53",
"entityName":"character",
"data":{
"name":"C-3PO",
"role":"Droid"
}
},
{
"entityId":"b161b9dc-552b-4744-b2d7-4584a9673669",
"entityName":"film",
"data":{
"name":"A new hope"
}
},
{
"entityId":"e59acdaf-5bcd-4536-a8e9-4f3502cc7d85",
"entityName":"film",
"data":{
"name":"The Empire Strikes Back"
}
},
{
"entityId":"00501b4a-5279-41e9-899d-a914ddcc562e",
"entityName":"vehicle",
"data":{
"name":"Sand Crawler",
"model":"Digger Crawler",
"manufacturer":"Corellia Mining Corporation"
}
},
{
"entityId":"fe815cb6-b03c-401e-a871-396f2cd3eaba",
"entityName":"vehicle",
"data":{
"name":"TIE/LN starfighter",
"model":"win Ion Engine/Ln Starfighter",
"manufacturer":"Sienar Fleet Systems"
}
}
]
I know that I can put JSON in a string field, but that would negatively impact the search matching and also filtering.
Is this possible in Azure search or is there a different way to achieve this kind of requirement?
See the article How to model complex data types. The hotel example data translates nicely to your use-case I believe. If your different entities have different sets of properties you can create a "complex type" similar to the Address or Amenities example below.
Structural updates
You can add new sub-fields to a complex field at any time without the
need for an index rebuild. For example, adding "ZipCode" to Address or
"Amenities" to Rooms is allowed, just like adding a top-level field to
an index.
{
"HotelId": "1",
"HotelName": "Secret Point Motel",
"Description": "Ideally located on the main commercial artery of the city in the heart of New York.",
"Tags": ["Free wifi", "on-site parking", "indoor pool", "continental breakfast"]
"Address": {
"StreetAddress": "677 5th Ave",
"City": "New York",
"StateProvince": "NY"
},
"Rooms": [
{
"Description": "Budget Room, 1 Queen Bed (Cityside)",
"RoomNumber": 1105,
"BaseRate": 96.99,
},
{
"Description": "Deluxe Room, 2 Double Beds (City View)",
"Type": "Deluxe Room",
"BaseRate": 150.99,
}
. . .
]
}

How to get confidence score for detected entities?

When I call the LUIS API, I get confidence scores associated with my intents. I also get a list of entities, but I don't get the corresponding confidence scores. How do I get the confidence scores?
This somewhat depends on how you're calling the API (directly or with some connector/recognizer). My answer assumes you're calling directly via the URL. In that case, whether you get a confidence or not is going to depend on the type of entity. Things like Regex or List entities aren't going to have a confidence because they only are identified if they are a 100% match. If you are using Machine Learned entities, you will have a confidence score. Not sure about any other entity types or features. Here is an example payload from my application. You can see that I have both an orderNumberML and orderNumber entity, the former being Machine Learned with a confidence value and the latter Regex without. You have to go into the $instance property, as the top level json.prediction.entities will just give you the list without any additional context.
{
"query":"what is the status of order ABC123 and order DEF456?",
"prediction":{
"topIntent":"viewOrder",
"intents":{
"viewOrder":{
"score":0.999304056
},
"cancelChangeQuantity":{
"score":0.0195436124
},
"escalate":{
"score":0.018896237
},
"qna":{
"score":0.0164053086
},
"changeShipMethod":{
"score":0.0147548188
},
"expediteOrder":{
"score":0.0100477394
},
"mainMenu":{
"score":0.00383487041
},
"requestCoc":{
"score":0.00324145844
},
"orderShortage":{
"score":0.00208944362
},
"Utilities.Help":{
"score":0.00205096183
},
"generalSupport":{
"score":0.001971956
},
"trcSupport":{
"score":0.00169838977
},
"trcEscalate":{
"score":0.00165500911
},
"getPricing":{
"score":0.00135509949
},
"getAvailability":{
"score":0.00125210814
},
"orderOverage":{
"score":0.000846677169
},
"srStatus":{
"score":0.0006817043
},
"shippingProblem":{
"score":0.000577154336
},
"warrantyClaim":{
"score":0.000458181225
},
"getTranscript":{
"score":0.000367239147
},
"None":{
"score":0.000275740429
},
"manageProfile":{
"score":0.0002755769
},
"confirmShipDate":{
"score":0.0001726267
},
"Utilities.Cancel":{
"score":7.628063E-05
}
},
"entities":{
"orderNumberML":[
"ABC123",
"DEF456"
],
"orderNumber":[
"ABC123",
"DEF456"
],
"$instance":{
"orderNumberML":[
{
"type":"orderNumberML",
"text":"ABC123",
"startIndex":28,
"length":6,
"score":0.916349649,
"modelTypeId":1,
"modelType":"Entity Extractor",
"recognitionSources":[
"model"
]
},
{
"type":"orderNumberML",
"text":"DEF456",
"startIndex":45,
"length":6,
"score":0.9027585,
"modelTypeId":1,
"modelType":"Entity Extractor",
"recognitionSources":[
"model"
]
}
],
"orderNumber":[
{
"type":"orderNumber",
"text":"ABC123",
"startIndex":28,
"length":6,
"modelTypeId":8,
"modelType":"Regex Entity Extractor",
"recognitionSources":[
"model"
]
},
{
"type":"orderNumber",
"text":"DEF456",
"startIndex":45,
"length":6,
"modelTypeId":8,
"modelType":"Regex Entity Extractor",
"recognitionSources":[
"model"
]
}
]
}
}
}
}

How to define an index to use in a Mango Query

I am trying to create a CouchDB Mango Query with an index with the hope that the query runs faster. At the moment I have the following Mango Query which returns what I am looking for but it's slow. Therefore, I assume, I need to create an index to make it faster. I need help figuring out how to create that index.
selector: {
categoryIds: {
$in: categoryIds,
},
},
sort: [{ publicationDate: 'desc' }],
You can assume that my documents are let say news articles from different categories. Therefore in each document I have a field that contains one or more categories that the news article belongs to. For that I have an array of categoryIds for each document. My query needs to be optimized for queries like "Give me all news that have categoryId1 in their array of categoryIds sorted by publicationDate". What I don't know how to do is 1. How to define an index 2. What that index should be 3. How to use that index in "use_index" field of the Mango Query. Any help is appreciated.
Update after "Alexis Côté" answer:
If I define the index like this:
{
"_id": "_design/0f11ca4ef1ea06de05b31e6bd8265916c1bbe821",
"_rev": "6-adce50034e870aa02dc7e1e075c78361",
"language": "query",
"views": {
"categoryIds-json-index": {
"map": {
"fields": {
"categoryIds": "asc"
},
"partial_filter_selector": {}
},
"reduce": "_count",
"options": {
"def": {
"fields": [
"categoryIds"
]
}
}
}
}
}
And run the Mango Query like this:
{
"selector": {
"categoryIds": {
"$in": [
"e0bd5f97ac35bdf6893351337d269230"
]
}
},
"use_index": "categoryIds-json-index"
}
It still does return the results but they are not sorted in the order I want by publicationDate. So I am not clear what you are suggesting the solution is.
You can create an index as documented here
In your case, you will need an index on the "categoryIds" field.
You can specify the index using "use_index": "_design/<name>"
Note:The query planner should automatically pick this index if it's compatible.

How to match and join results between two resolvers in one graphql query?

I have two resolver.
The one is Company resolve that return the company details like id, name and list of documents ids, like this example:
{
"data": {
"companyOne": {
"name": "twitter",
"documents": [
"5c6c0213f0fa854bd7d4a38c",
"5c6c02948e0001a16529a1a1",
"5c6c02ee7e76c12075850119",
"5c6ef2ddd16e19889ffaffd0",
"5c72fb723ebf7b2881679ced",
"5c753d1c2e080fa4a2f86c87",
...
]
}
}
}
And the another resolver gets me all the details of documents like this example:
{
"data": {
"documentsMany": [{
"name": "doc1",
"_id": 5c6c0213f0fa854bd7d4a38c,
}, {
"name": "doc2",
"_id": 5c6c02948e0001a16529a1a1,
},
...
]
}
}
How to match every data.companyOne.documents[id] to data.documentsMany[..]._id? in the query level? is it possible to do this graphql?
The expect results should be when I run the companyOne query (without change the code - just in the query level) it's should return with documents as object instead of array of string ids.
maybe something like?
query {
companyOne {
name,
documents on documentsMany where _id is ___???
}
}

Resources