I need to handle with tde templates some documents with sometimes lacks of data. But still need to display it in tables. But my templates have problems to return this documents with empty data.
var doc1 = xdmp.toJSON(
{
"customer":{
"Name": "Name1",
"Addr": [
{
"AddrTypeCd": "MailingAddress",
"Addr1": "911 FORBES AVE",
"Addr2": "SUITE XXX",
"CityName": "asdfasfd",
"StateProvCd": "PA",
"PostalCode": "15219"
},
{
"AddrTypeCd": "OfficeAddress",
"Addr1": "911 Watson AVE",
"Addr2": "SUITE XXX",
"CityName": "asdfasfd",
"StateProvCd": "CT",
"PostalCode": "15119"
}
]
}
});
var doc2 = xdmp.toJSON(
{
"customer":{
"Name": "Name2",
"Addr": [
]
}
});
var rowtde1 = xdmp.toJSON(
{
"template":{
"context":"/customer/Addr",
"rows":[
{
"schemaName":"Schemas",
"viewName":"CustomerAddress",
"columns":[
{
"name":"CustomerName",
"scalarType":"string",
"val":"../../Name"
},
{
"name":"AddrTypeCd",
"scalarType":"string",
"val":"AddrTypeCd",
"nullable": true
},
{
"name":"Addr1",
"scalarType":"string",
"val":"Addr1",
"nullable": true
},
{
"name":"Addr2",
"scalarType":"string",
"val":"Addr2",
"nullable": true
},
{
"name":"CityName",
"scalarType":"string",
"val":"CityName",
"nullable": true
},
{
"name":"StateProvCd",
"scalarType":"string",
"val":"StateProvCd",
"nullable": true
},
{
"name":"PostalCode",
"scalarType":"string",
"val":"PostalCode",
"nullable": true
}
]
}
]
}
}
);
tde.nodeDataExtract([doc1, doc2],[rowtde1]);
For this data extract the response is:
{
"document1": [
{
"row": {
"schema": "Schemas",
"view": "CustomerAddress",
"data": {
"rownum": "1",
"CustomerName": "Name1",
"AddrTypeCd": "MailingAddress",
"Addr1": "911 FORBES AVE",
"Addr2": "SUITE XXX",
"CityName": "asdfasfd",
"StateProvCd": "PA",
"PostalCode": "15219"
}
}
},
{
"row": {
"schema": "Schemas",
"view": "CustomerAddress",
"data": {
"rownum": "2",
"CustomerName": "Name1",
"AddrTypeCd": "OfficeAddress",
"Addr1": "911 Watson AVE",
"Addr2": "SUITE XXX",
"CityName": "asdfasfd",
"StateProvCd": "CT",
"PostalCode": "15119"
}
}
}
],
"document2": []
}
But I need to get in response data for customers without addresses too, like this:
{ "document1": [
{
"row": {
"schema": "Schemas",
"view": "CustomerAddress",
"data": {
"rownum": "1",
"CustomerName": "Name1",
"AddrTypeCd": "MailingAddress",
"Addr1": "911 FORBES AVE",
"Addr2": "SUITE XXX",
"CityName": "asdfasfd",
"StateProvCd": "PA",
"PostalCode": "15219"
}
}
},
{
"row": {
"schema": "Schemas",
"view": "CustomerAddress",
"data": {
"rownum": "2",
"CustomerName": "Name1",
"AddrTypeCd": "OfficeAddress",
"Addr1": "911 Watson AVE",
"Addr2": "SUITE XXX",
"CityName": "asdfasfd",
"StateProvCd": "CT",
"PostalCode": "15119"
}
}
} ], "document2": [ {
"row": {
"schema": "Schemas",
"view": "CustomerAddress",
"data": {
"rownum": "1",
"CustomerName": "Name2"
}
}
} ] }
Do I need to create second view to handle Name from document and then join it with address view? Do that multiple joins will have impact on performance?
Since there is an array of items you should create a second template to express those rows. You will have a view for the main customer and a view for the addresses. It is ideal to have a unique key to join with as well. Once you have the two views you can use SQL or the Optic API (MarkLogic's Multi-Model Querying Library) to query and join.
Example of querying with Optic API
Results