How to delete entry in dynamodb list? - javascript

I've got a lambda function, which acts as a trigger on a table with best scores of users to handle a leaderboard table.
In my leaderboard table, the sort key is the score, and the player's name is a separate entry with a list, because it's possible that there could be more than one player with the same score. Never mind.
So when adding a player I do:
var paramsNewEntry = {
"TableName": leaderboardTable,
"Key": {
"trackId": trackId,
"time": newValue
},
"UpdateExpression": "SET players = list_append(if_not_exists(players, :emptyList), :playersList),
"ExpressionAttributeValues": {
":playersList": [userId],
":emptyList":[]
},
"ReturnValues": "NONE"
};
And this works fine. I wanted to remove it this way:
var paramsOldEntry = {
"TableName": myTable,
"Key": {
"trackId": trackId,
"time": oldValue
},
"UpdateExpression": "DELETE players :playerToRemove",
"ExpressionAttributeValues": {
":playerToRemove": [userId]
},
"ReturnValues": "ALL_NEW"
}
But I get: Invalid UpdateExpression: Incorrect operand type for operator or function; operator: DELETE, operand type: LIST error.
The players attribute is a list, query response example:
{
"Items": [
{
"time": {
"N": "99994"
},
"players": {
"L": [
{
"S": "krystianPostman2"
}
]
},
"trackId": {
"S": "betaTrack001"
}
}
],
"Count": 1,
"ScannedCount": 1,
"LastEvaluatedKey": {
"time": {
"N": "99994"
},
"trackId": {
"S": "betaTrack001"
}
}
}
I've not seen any question on SO which would provide any details on this in javascript, when using the dynamodb Document API.

DynamoDB API doesn't have an option to delete the value from LIST datatype based on its value. However, if you know the index of the value to be deleted, you can use REMOVE to delete the entry from list.
The DELETE action only supports Set data types.
UpdateExpression: 'REMOVE players[0]'
If the LIST is going to have only name attribute, it is better to save it as SET rather than LIST DynamoDB datatype.
Creating Set:-
var docClient = new AWS.DynamoDB.DocumentClient();
docClient.createSet( ["v1", "v2"]);
Deleting the values from SET using DELETE

Related

How can I extract object/key names from nested and varying JSON payload in a Postman Test script?

I am an old-school C++ programmer - trying to get to grips with Postman, JSON, REST APIs, etc, and struggling..
I am trying to write a Postman test to visualize some JSON response data - which I would like to show in a table format with some merged key names as column headings.
The problem is that the number of data items in a response can vary and the key names can also vary - depending on the input parameters.
So say, for the following JSON which has two data items:
{
"data": [
{
"input": [
{
"value": "ABC",
"identifierType": "a1"
}
],
"output": [
{
"value": "BT",
"identifierType": "b1",
"name": "BT GROUP",
"status": "Active",
"classification": "Ordinary"
}
]
},
{
"input": [
{
"value": "BCD",
"identifierType": "a1"
}
],
"output": [
{
"value": "EFG",
"identifierType": "b1",
"name": "SIEMENS",
"status": "Active",
"classification": "Ordinary"
}
]
}
]
}
I want to end up with a collection containing column headings that looks something like this:
[“Input value”, “Input identifierType”,“Output value”,“Output identifierType”,“Output name”,“Output status”,“Output classification”]
I can get part of the way with something like the following:
function parseData(response, host) {
const results = response.map(elem => (
[
elem.input[0].value,
elem.input[0].identifierType,
elem.output[0].value,
elem.output[0].identifierType,
elem.output[0].name,
elem.output[0].status,
elem.output[0].classification,
]
));
const headers = [];
for (const key in response[0].input[0]){
headers.push(`Input ${key}`)
}
for (const key in response[0].output[0]){
headers.push(`Output ${key}`)
}
return [results, headers]
}
This gives me the desired headers: [“Input value”, “Input identifierType”,“Output value”,“Output identifierType”,“Output name”,“Output status”,“Output classification”]
However, I want to make this more general i.e. not have to specify input[0] and output[0] in the for loops - as these key names could differ for different query responses.
I did ask this question on the Postman forums and someone helpfully provided a code snippet that allows me to extract the 'input' and 'output' names.
for (const _outer in response[0]) {
for (const _inner in _outer) {
for (const key in _inner) {
headers.push(`${_outer} ${key}`)
}
}
}
But that only gives me: ["input 0", "input 0", "input 0", "input 0", "input 0", "output 0' …] for the headers. For some reason I cannot access the inner keys such as value, name, identifierType, status etc
Can someone please suggest where the above is going wrong / how to get what I am after?
Thanks.
Ok - I managed to get an answer on the Postman Forums - thanks.
the solution is:
for (const outerkey in response[0]){
for (const innerkey in response[0][`${outerkey}`][0]){
headers.push(`${outerkey} ${innerkey}`)
}
}
and gave me:
"input value", "input identifierType", "output value", "output identifierType", "output name", …]
UPDATE: The data items extraction was still hardcoded with key names (e.g. elem.input[0].identifierType and so), so I replaced that also to give the following solution:
function parseData(response, host) {
const results = response.map(function(val, index){
temp = [];
for (const outerkey in val){
for (const innerkey in val[`${outerkey}`][0]){
temp.push(val[`${outerkey}`][0][`${innerkey}`]);
}
}
return temp
}
);
const headers = [];
for (const outerkey in response[0]){
for (const innerkey in response[0][`${outerkey}`][0]){
headers.push(`${outerkey} ${innerkey}`)
}
}
return [results, headers]
}

Elastic Search Json field query with number key

How can I query elastic search based on the number key?
JSON field name
years_of_experience :
"{\"61\": \"10\", \"8240\": \"5\", \"8249\": \"2\", \"50\": \"0\", \"2079\": \"2\"}"
I want to filter years_of_experience like 50:0.
So, according to your sample, you have documents like the below:
POST myindex/_doc
{
"years_of_experience": {
"50": "0",
"61": "10",
"2079": "2",
"8240": "5",
"8249": "2"
}
}
So, you have an object for years_of_experience, and you want to do an exact match with the field name and values. You need to set all fields inside this field you want to set as a keyword type. First, you need to handle the mapping part of this problem. Here is a solution for this :
PUT myindex
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"dynamic_templates": [
{
"strings_as_keyword": {
"match_mapping_type": "string",
"path_match": "years_of_experience.*",
"mapping": {
"type": "keyword"
}
}
}
],
"properties": {
"years_of_experience": {
"type": "object"
}
}
}
}
While creating your index for this data, you need to use a dynamic template for the years_of_experience object. And all the fields inside this will be keyword type, and you can run term queries on these fields.
So now we can create the documents after creating an index with the above settings. And you can filter the data as below :
GET myindex/_search
{
"query": {
"term": {
"years_of_experience.50": "0"
}
}
}

Get SAPUI5 VizFrame's id in dataPointStyle rule callback

I am generating some dynamic vizframe column charts by looping at the response I get from my Odata service. One of my requirement for the chart is to show columns in different color depending on the value of a field I have in data. Let's call it validation status.
I have a fairly good idea on how to achieve this in normal situations by using the setVizProperties method and setting up rules for the dataPointStyle properties. It would still have been possible if I had the same criteria for all the charts and all the values. But that isn't the case since I need to check each record individually to determine it's status. So I thought of using the callback functionality of the dataPointStyle. But the problem here is that although it gives me the context but it doesn't tell me from which chart this callback has been triggered. My idea is that if I get the chart name or it's reference then I can access it's model and determine the color.
So if I can somehow get a reference of the vizframe from where the call back is being triggered, it will solve my problem.
callback
Description:
function (data, extData) {...} => true|false
A function to determine whether a given data matches the rule. Parameters:
data is an object with all bound field ids as keys, and corresponding values as values. It helps to consider it as the object containing everything you see in datapoint mouseover tooltip. If unbound dimensions or measures are set in FlatTableDataset context field, the related key/value pairs will be included in this parameter as well.
extData is an object with all other measure fields that in the same line with current data point. Measure Ids as keys, and corresponding values as values. It helps to compare value between different measures.
Link to the vizFrame documentation
JSFiddle
My Data looks something like this:
[{
"RunLogId": "0000000040",
"RuleId": "00016",
"CreatedOn": "2020-07-21",
"CreatedAt": "09:44:35",
"NAV_SUBSCRIBED_LOGS": {
"results": [
{
"RunLogId": "0000000040",
"Sequence": "00001",
"RuleId": "00016",
"Variation": "-3.94",
"ValidationStatus": "F",
"Dimension": "ABC"
},
{
"RunLogId": "0000000040",
"Sequence": "00002",
"RuleId": "00016",
"Variation": "1.04",
"ValidationStatus": "S",
"Dimension": "DEF"
}
]
}
},
{
"RunLogId": "0000000033",
"RuleId": "00014",
"CreatedOn": "2020-07-15",
"CreatedAt": "11:10:09",
"NAV_SUBSCRIBED_LOGS": {
"results": [
{
"RunLogId": "0000000033",
"Sequence": "00001",
"RuleId": "00014",
"Variation": "-2.36",
"ValidationStatus": "F",
"Dimension": "ABC"
},
{
"RunLogId": "0000000033",
"Sequence": "00002",
"RuleId": "00014",
"Variation": "-5.05",
"ValidationStatus": "F",
"Dimension": "DEF"
}
]
}
}]
My code looks some like this :
for (var i = 0; i < chartsCount; i++) {
var oModel = new JSONModel();
var chartData = aSubscriptions[i].NAV_SUBSCRIBED_LOGS.results;
var aDimensions = [];
var aDimFeeds = [];
aDimensions.push({
name: "Dimension",
value: "{Dimension}"
});
aDimFeeds.push("Dimension");
oModel.setData(chartData);
oModel.refresh();
var oDataset = new FlattenedDataset({
dimensions: aDimensions,
measures: [{
name: "Variation",
value: "{Variation}"
}],
data: {
path: "/"
}
});
var oVizFrame = new VizFrame();
oVizFrame.setVizType("column");
oVizFrame.setHeight("450px");
oVizFrame.setDataset(oDataset);
oVizFrame.setModel(oModel);
var feedValueAxisActual = new sap.viz.ui5.controls.common.feeds.FeedItem({
"uid": "valueAxis",
"type": "Measure",
"values": ["Variation"]
}),
feedCategoryAxis = new sap.viz.ui5.controls.common.feeds.FeedItem({
"uid": "categoryAxis",
"type": "Dimension",
"values": aDimFeeds
});
oVizFrame.addFeed(feedValueAxisActual);
oVizFrame.addFeed(feedCategoryAxis);
oVizFrame.setVizProperties({
plotArea: {
dataPointStyle: {
"rules": [
{
callback: function (oContext, extData) {
that.checkValue(oContext, "S");
},
"properties": {
"color": "sapUiChartPaletteSemanticGoodLight1"
},
"displayName": "Successful"
}
, {
callback: function (oContext, extData) {
that.checkValue(oContext, "F");
},
properties: {
color: "sapUiChartPaletteSemanticBadLight1"
},
"displayName": "Failed"
}
],
others: {
properties: {
color: "sapUiChartPaletteSemanticNeutral"
},
"displayName": "Undefined"
}
}
}
});
//Chart Container
var oChartContainer = new ChartContainer();
var oChartContainerContent = new ChartContainerContent();
oChartContainerContent.setContent(oVizFrame);
oChartContainer.addContent(oChartContainerContent);
}
Not sure if I understand you correctly but I'll give it a shot anyway. If I was wrong let me know.
You create charts in a loop. You want to access the specific chart in a callback.
Why don't you access oVizFrame in your callback?
First I would replace the for loop with a forEach. forEach calls a given function for every element in your array:
aSubscriptions.forEach(function(oSubscription) {
const oModel = new JSONModel();
const chartData = oSubscription.NAV_SUBSCRIBED_LOGS.results;
...
}
In a for loop your variables are reused. In a forEach function a new scope is created for every item. So when you access oVizFrame in your callback it is the same oVizFrame that you declared earlier.
Then you should be able to access oVizFrame in your callback.
oVizFrame.setVizProperties({
plotArea: {
dataPointStyle: {
"rules": [{
callback: function(oContext, extData) {
// >>>>>>>> Do something with oVizFrame <<<<<<<<
that.checkValue(oContext, "S");
},
...
}, {
callback: function(oContext, extData) {
// >>>>>>>> Do something with oVizFrame <<<<<<<<
that.checkValue(oContext, "F");
},
...
}],
...
}
}
});

How to get all values of given specific keys (for e.g: name) without loop from json?

I want to fetch all the names and label from JSON without loop. Is there a way to fetch with any filter method?
"sections": [
{
"id": "62ee1779",
"name": "Drinks",
"items": [
{
"id": "1902b625",
"name": "Cold Brew",
"optionSets": [
{
"id": "45f2a845-c83b-49c2-90ae-a227dfb7c513",
"label": "Choose a size",
},
{
"id": "af171c34-4ca8-4374-82bf-a418396e375c",
"label": "Additional Toppings",
},
],
},
]
}
When you say "without loops" I take it as without For Loops. because any kind of traversal of arrays, let alone nested traversal, involve iterating.
You can use the reduce method to have it done for you internally and give you the format you need.
Try this :
const data = {
sections: [
{
id: "62ee1779",
name: "Drinks",
items: [
{
id: "1902b625",
name: "Cold Brew",
optionSets: [
{
id: "45f2a845-c83b-49c2-90ae-a227dfb7c513",
label: "Choose a size"
},
{
id: "af171c34-4ca8-4374-82bf-a418396e375c",
label: "Additional Toppings"
}
]
}
]
}
]
};
x = data.sections.reduce((acc, ele) => {
acc.push(ele.name);
otherName = ele.items.reduce((acc2, elem2) => {
acc2.push(elem2.name);
label = elem2.optionSets.reduce((acc3, elem3) => {
acc3.push(elem3.label);
return acc3;
}, []);
return acc2.concat(label);
}, []);
return acc.concat(otherName);
}, []);
console.log(x);
Go ahead and press run snippet to see if this matches your desired output.
For More on info reduce method
In the context of cJSON
yes, we can fetch the key value for any of the object.
1 - each key value is pointed by one of the objects. will simply fetch that object and from there will get the key value.
In the above case for
pre-requisition: root must contain the json format and root must be the cJSON pointer. if not we can define it and use cJSON_Parse() to parse the json.
1st name object is "sections" will use
cJSON *test = cJSON_GetObjectItem(root, "sections");
char *name1 = cJSON_GetObjectItem(test, "name" )->valuestring;
2nd name key value
cJSON *test2 = cJSON_GetObjectItem(test, "items");
char *name2 = cJSON_GetObjectItem(tes2, "name")->valuestring;
likewise, we can do for others as well to fetch the key value.

RethinkDB: Javascript - How to deleted nested objects

I'm having a rather large amount of difficulty with trying to remove nested objects from my table, without accidentally deleting all my data in the process (happened three times now, thank god I made copies).
My Object:
{
"value1": thing,
"value2": thing,
"value3": thing,
"roles": {
"1": {
"name": "Dave",
"id": "1"
},
"2": {
"name": "Jeff",
"id": "2"
},
"3": {
"name": "Rick",
"id": "3"
},
"4": {
"name": "Red",
"id": "4"
}
}
}`
I've tried a number of rethink queries, but none have worked thus far. It should be noted that 1, 2, 3, & 4 are variables that can have any amount of numbers, and thus my query must reflect that.
Some attempted queries:
function removeRole(id, roleName) {
let role = `${roleName}`
return this.r.table('guilds').get(id).replace(function(s){
return s.without({roles : {[role] : { "name": role }}})
})
}
function removeRole(id, roleName) {
return this.r.table('guilds').getAll(id).filter(this.r.replace(this.r.row.without(roleName))).run()
}
function removeRole(id, roleName) {
return this.r.table('guilds').get(id)('roles')(roleName).delete()
}
Any assistance is greatly appreciated, and if the question has issues, please let me know. Still rather new to this so feedback is appreciated.
I'm not sure if I understood your intention, but the following query seems to do what you're trying to accomplish:
r.db('test')
.table('test')
.get(id)
.replace((doc) => {
// This expression makes sure that we delete the specified keys only
const roleKeys = doc
.getField('roles')
.values()
// Make sure we have a role name is in the names array
.filter(role => r.expr(names).contains(role.getField('name')))
// This is a bit tricky, and I believe I implemented this in a not efficient
// way probably missing a first-class RethinkDB expression that supports
// such a case out of box. Since we are going to delete by nested dynamic
// ids, RethinkDB requires special syntax to denote nested ids:
// {roles: {ID_1: true, ID_2: true}}
// Well, this is just a JavaScript syntax workaround, so we're building
// such an object dynamically using fold.
.fold({}, (acc, role) => acc.merge(r.object(role.getField('id'), true)));
return doc.without({roles: roleKeys});
})
For example, if names is an array, say ['Jeff', 'Rick'], the nested roleKeys expession will be dynamically evaluated into:
{2: true, 3: true}
that is merged into the roles selector, and the above query will transform the document as follows:
{
"value1": ...,
"value2": ...,
"value3": ...,
"roles": {
"1": {"name": "Dave", "id": "1"},
"4": {"name": "Red", "id": "4"}
}
}

Categories

Resources