I get the following data from the req.body:
{
"user_id": "1",
"user_name": "jkalandarov",
"email": "jkalandarov#gmail.com",
"courses": [
{
"course_id": 1,
"course_name": "English",
"course_price": 265
},
{
"course_id": 2,
"course_name": "Node.js",
"course_price": 200
},
{
"course_id": 3,
"course_name": "Backend Developement",
"course_price": 300
}
]
}
I want to insert this data into three rows with the help of knex into mysql database table like this with a single query:
Any ideas?
Thanks in advance!
Assuming you already have your knex connection set up, it should simply allow you to pass an array of objects into the insert function to insert multiple rows with a single statement. https://knexjs.org/#Builder-insert
Step 1: Build an array of objects
const objectsToInsert = req.body.courses.map(course => {
return {
user_id: req.body.user_id,
user_name: req.body.user_name,
email: req.body.email,
...course
}
});
Step 2: Insert them using your knex connection
await knex('your_table_name').insert(objectsToInsert);
This should insert those three rows into your table in a single query.
As this data is coming directly from the request body it is obviously also a good idea to add some validation here but that feels out of scope for your original question.
Related
using postgresql db for persistence. one of my table's column's data type is json and stored data format is like
{
"Terms": [
{
"no": 1,
"name": "Vivek",
"salary": 123
},
{
"no": 2,
"name": "Arjun",
"salary": 123
},
{
"no":3,
"name": "Ashok",
"salary": 123
}
]
}
I need to update any of no or name or salary of 1st Term object only.
Used native queried to load and for better performance, I should use native query only for UPDATE. I tried postgresql jsonb_set function for the update, but unable to update.
I tried:
UPDATE table_name
SET terms = jsonb_set(terms->'Terms','{0,name}','"VVVV"',FALSE)
WHERE some condition
and response message in pgAdmin tool is
Query returned successfully: 0 rows affected, x msec execution time.
Can any one help me with this one?
I'm having trouble using the AWS CLI to delete Route 53 records. I have a list of hundreds of domains and each one needs both 'A' records deleted. I wanted to do this using the CLI to save time, but I can't get the functionality working.
For example, let's say I have the following domain and I want to delete both 'A' records:
I'm using boto3 here, but it is the same AWS CLI API that I can't get working (https://docs.aws.amazon.com/cli/latest/reference/route53/change-resource-record-sets.html). My issue is somewhere in the json filter for this api call:
HostedZoneId='ABC123DEF456',
ChangeBatch={
'Comment': 'deleteing A records for domains',
'Changes': [
{
'Action': 'DELETE',
'ResourceRecordSet': {
'Name': 'example.com',
'Type': 'A',
'Region': 'us-east-1',
'ResourceRecords': [
{
"Value": "1.2.3.4"
}
],
'AliasTarget': {
'HostedZoneId': 'ABC123DEF456',
'DNSName': 'example.com',
'EvaluateTargetHealth': False
}
}
}
]
}
The error I am getting is:
InvalidInput: An error occurred (InvalidInput) when calling the ChangeResourceRecordSets operation: Invalid request: Expected exactly one of [AliasTarget, all of [TTL, and ResourceRecords], or TrafficPolicyInstanceId], but found more than one in Change with [Action=DELETE, Name=example.com, Type=A, SetIdentifier=null]
I think there is some confusion between simple record of A type, and simple record of alias A type. Namely, simple alias record should not don't have ResourceRecords.
To check how they are described in your case, you can use the following command:
aws route53 list-resource-record-sets --hosted-zone-id <your-zone-id>
The output of the above command should be helpful in constructing your DELETE.
Below are examples of outputs from my route53:
simple record
{
"Name": "<simple-a.example.com.>",
"Type": "A",
"TTL": 300,
"ResourceRecords": [
{
"Value": "1.2.3.4"
}
]
}
simple record with alias
{
"Name": "<simple-alias.example.com.>",
"Type": "A",
"AliasTarget": {
"HostedZoneId": "Z06990762X86XLR2ZGTK4",
"DNSName": "<example>.",
"EvaluateTargetHealth": true
}
},
I am trying to update my mysql table and insert json data to my mysql table's json-datatype column using JSON_INSERT. Here is the structure of my column.
{
"Data":
[{
"Devce": "ios",
"Status": 1
}]
}
This is the query I am using to insert more data to this field.
UPDATE table SET `Value` = JSON_INSERT
(`Value`,'$.Data','{\"Device\":\"ios\",\"Status\":1}') WHERE Meta = 'REQUEST_APP'
This is supposed to update the field to this:
{
"Data":
[{
"Devce": "ios",
"Status": 1
},
{
"Devce": "ios",
"Status": 1
}
]
}
But instead it the result is:
0 rows affected. (Query took 0.0241 seconds.)
Any help regarding this would be appreciated.
JSON_APPEND serves your purpose better JSON_APPEND docs
Sorry, if the title sounds confusing, I tried my best.
Right now I have this :
{
"success": true,
"data": {
"id_reservation": 101,
"id_venue": 2,
"id_user": 67
}
}
When GETing a reservation from my API, what I'm looking for is
{
"success": true,
"data": {
"id_reservation": 101,
"venue": {
"id_venue": 2,
"venue_name": "TEST",
"location": "TEST",
"venue_type": "TEST"
},
"id_user": 67
}
}
I want to have the object using the ID in my JSON response, how could I achieve this with CakePHP 3?
I guess you're getting the Venue Models with a \Cake\ORM\Query or the object VenuesTable.
You also need to load the associations' Reservation and User, you should read this documentation: Loading Associations
You have to use the contain method like this :
$venueEntity = $this->Venues
->find()
->contain([
'Reservations',
'Users',
])
// then your conditions
->where(['venue_id' => $venueId])
->first();
"Venue" looks like a related table. So you need to make a find requesting all related models. To ensure receiving what you want make a join in the find and set in the "fields" those you want.
I am new to AngularJS. Trying to get specific element from a JSON array via $resource.
The structure of JSON file staffs.json is like:
[{
"id": 0,
"facility_id": [0],
"name": "Tim",
"role_id": 0
},
{
"id": 1,
"facility_id": [0],
"name": "Duncan",
"role_id": 0
},
{
"id": 2,
"facility_id": [0],
"name": "Tony",
"role_id": 1
},
{
"id": 3,
"facility_id": [0],
"name": "Parker",
"role_id": 1
},
{
"id": 4,
"facility_id": [0],
"name": "Manu",
"role_id": 2
},
{
"id": 5,
"facility_id": [0],
"name": "Ginobili",
"role_id": 2
},
{
"id": 6,
"facility_id": [0],
"name": "Tiago",
"role_id": 3
},
{
"id": 7,
"facility_id": [0],
"name": "Splitter",
"role_id": 3
}]
I am trying to get a staff whose name is "Tiago".
The code is:
var url = 'data/staffs.json';
var username = 'Tiago';
users = $resource(url);
users.get({name: username}, function(data){
alert(data.name);
});
It seems the alert() function inside the get() never gets called. However if I changed the method from users.get() to users.query(), it can get the list of the staffs. I guess this is because the data inside the JSON file is an array, so the query() which is used to get array works, while the get() does not work because it is not for array operation. Am I correct?
I am just wondering if I have to use query() get the whole array and match the elements one by one until I find the one with the same name, or there are some simpler ways to get the element I want.
Thanks
AngularJS resource has a separate query function to avoid JSONP vulnerability for arrays. You have two options:
get all and find the element in the array on the client side
add extra API endpoint for single user and fetch it by the name
I vote for option two, since you don't have to send everything over the wire and you use server (DB) to get the specific user. Server software is optimised for that.
Your best bet would by fetching data with the query() method, then using indexOf()
var url = 'data/staffs.json';
var username = 'Tiago';
users = $resource(url);
users.get({name: username}, function(data){
dataOfMyUser = data.map(function(cur) { return cur.name }).indexOf(username);
alert(dataofMyUser);
});