Node js + Postgres, update Json column with SQL query - javascript

I am making a db query upon hitting a POST API endpoint. The query needs to update the Json column in my networks table, which only has 3 columns (id, name, and json). I need to specifically update the coreEssentials array with another value, so I have been using the set 'json' = ? SQL query where I paste in the entire column with my changes in the specific field and it works (manually in the db). The only issues are, I need to do make a SQL call to SELECT the json column for a specific id first, (long story, but a backend application generates some data into the JSON (the coreEssentials key/object I need to update) then puts it into the data, then after I need to update).
I was doing this manually in my Postgresql GUI (DBbeaver) and my query simply looks like this:
update network set "json" = '{
"uid": "randomUid",
"etag": "randomEtag",
"name": "randomNameAgain",
"state": "PENDING",
"Type": "ABC",
"version": 1,
"dealerId": "random_uuid",
"Param": {
"AreaId": 0,
"AreaIdStr": "0.0.0.0",
"DeadInterval": 0,
"HelloInterval": 0
},
"networkData": {
"tic": "311",
"toe": "980",
"tac": "201",
"tac_id": "201",
"timeZone": null
},
"production": false,
"customerName": "random_name",
"IPPool": "0.0.0.0/32",
"customerEmail": "random#email.com",
"coreEssentials": [ ],
"deployment": "A"
}'
coreEssentials starts out as an Empty array but I need to set it to this:
[{
"version": 1,
"component": "purple",
"instanceId": "1"
},
{
"version": 1,
"component": "gray",
"instanceId": "1"
},
{
"version": 1,
"component": "blue",
"instanceId": "1"
} ]
I'm using a Node JS backend with pg-promise (Postgresql) library. Can anyone give me advice how to do this query?

I just set coreEssentials array to the data object returned from the first SQL query like #vitaly-t suggested :D

Related

Modify mapping of field in Elastic Search

I have a collection named templates. It has a field 'sec_1' which is an array of objects with properties 'name' and 'des' both of type string. Now I'm using elastic search and when I try to execute a query that matches only title fields I get an error saying the sec_1 field is not nested. Upon checking the mappings here is what I get mapping type of sec_1 is text.
I checked online and saw the fix was to modify the mapping. I created a new index like:
`curl base_url/new_index_name
{
"mappings":
{"properties": {
"sec_1": {
"type":
"nested","properties": {
"name": { "type": "text" },
"des": { "type": "text" }
}
}
}
}
}
and then reindexed like this:
curl base_url/_reindex
{
"source": {
"index": "old_index"
},
"dest": {
"index": "new_index"
}
}`
First request is successful, the second one fails with this error:
{
"index": "new_index",
"type": "_doc",
"id": "be5123a4-d0e8-4d7b-a8f1-42f31d37fe55",
"cause": {
"type": "mapper_parsing_exception",
"reason": "object mapping for [sec_1] tried to parse field [null] as object, but found a concrete value"
},
"status": 400
},
I don't understand why this is happening and what I'm doing wrong. Please help. I do not know much about elastic search, I've looked for solutions online, tried chatgpt too but the same steps appear. I'm finding the reason for this error is that sec_1 is not nested type but I've checked in database it is an array of objects. What else could be wrong?
I tried creaing new index but cannot do that and without new index my search query cant function properly

How to link two json array data's with Google apps script

Using a UrlFetchapp request, I retrieve data in json format in a sheet sheet, for non-table objects the processing is not that hard, but when it comes to tables it's a little less Easy,
I retrieve and display the label content in 4 columns in my sheets sheet by browsing the array object "feezeInformationList" then "feezeTypeCode" then "label", outside the "feezeInformationList" object we find additional data to the label, which is present in another array object, which is "glcfeezeInclusionInformationList", the complementary data is boolean and it is "glcfeezeIsIncludedInPrice" next to each column label, I would like to retrieve the true or false status of each label using maybe as input , the code attribute, here for example "2282".
"data": [
{
"glcfeezeInclusionInformationList": [
{
"feezeTypeCode": {
"code": "2282",
"description": "",
"label": "MMG"
},
"glcfeezeIsIncludedInPrice": true
}
feezeInformationList [
"feezeTypeCode": {
"code": "2282",
"description": "",
"label": "MMG"
},
]
Any help would be appreciated! thanks

Is it appropriate to use Apollo local state to store a different representation of returned data

I have an Apollo Client that I'm using to request data from a service. I want to use the data I get in response to create a network of nodes and links e.g.
// Response data:
{
"Team": [
{
"name": "Example Team",
"members": [
{ "name": "Bob" },
{ "name": "Alice" }
]
}
]
}
// Network data:
{
"nodes": [
{ "name": "Example Team" }
{ "name": "Bob" },
{ "name": "Alice" }
],
"links": [
{ "source": "Example Team", "target": "Bob" },
{ "source": "Example Team", "target": "Alice" }
]
}
Historically, before using GraphQL, I would have used Redux to store the munged API response in state and read from there.
Is it appropriate to take a GraphQL result from Apollo and immediately save it back to Apollo local state in a different form so it can be queried by components in that format?
The main problem I foresee is that I think I'd have to query to check if the data I want exists in local state, then make another query if it didn't. In a Redux-world this would be wrapped up inside my store which would then only make the request off to the API if it didn't have the data it needed which 'feels' much cleaner.
In my case this could be solved using Afterware in Apollo Client, see this answer for more information.
It would allow me to munge the returned data into the form I need, and return it in the response alongside the original data.

DocumentDB: How to filter document on array within array?

Let's say I have the following document:
{
"Id": "1",
"Properties": [
{
"Name": "Name1",
"PropertyTypes": [
"Type1"
]
},
{
"Name": "Name2",
"PropertyTypes": [
"Type1",
"Type2",
"Type3"
]
}
]
}
When I use the following SQL:
SELECT c.Id FROM c
JOIN p in c.Properties
WHERE ARRAY_CONTAINS(p.PropertyTypes,"Type1")
I get as return:
[
{
"Id": "1"
},
{
"Id": "1"
}
]
How do I change my query so that it only returns distinct documents?
As far as I know, Distinct hasn't supported by Azure Cosmos DB yet.
It seems that there is no way to remove the repeat data in the query SQL level.
You could handle with your query result set in the loop locally.
However, if your result data is large,I suggest you using a stored procedure to handle with result data in Azure Cosmos DB to release the pressure on your local server.
You could refer to the official tutorial about SP.

How to query for Metadata in CloudKit JS?

I've figured out how to query for some metadata for example calling this in my query:
<td>" + record['created'].value + "</td> <-- populates some table data
But this comes back undefined which confuses me because the CloudKit dashboard has a value for Created By (as part of the automatic Metadata that is created with each record.)
How do I query for the ID/UUID as well as get the response in a HTML/JS-readable manner?
After fetching one or more records with CloudKit.js you'll notice you get back an Object that looks like this:
{
"recordName": "6E863D3D-5371-4376-A9BA-3B294F480E4E",
"recordType": "Message",
"fields": {
"text": {
"value": "hi",
"type": "STRING"
},
},
"recordChangeTag": "ib461v1s",
"created": {
"timestamp": 1434751588720,
"userRecordName": "_36ed4b61e7a11c9302f851fe25a9b911",
"deviceID": "_2"
},
"modified": {
"timestamp": 1434751588720,
"userRecordName": "_36ed4b61e7a11c9302f851fe25a9b911",
"deviceID": "_2"
}
}
The recordName is a record's unique identifier, and the Object has a created attribute which points to an Object with a timestamp field.

Categories

Resources