Fetch json array in PostgreSQL procedure - json

I'm using PgSQL9.6.
I have procedure named as createBooking in PgSQL. It receive few parameters and one of them is additional type text.
Request is
{
"email": "user2#name.com",
"start_date": "2016-10-21 11:00:00+00",
"end_date": "2016-10-21 15:00:00+00",
"guests": 50,
"firstname": "User",
"lastname": "Name",
"payment_type": "Method 1",
"phone": "+871253875123",
"user_id": 22,
"y_id": 3,
"additionals": [
{
"extrasId": 6,
"money": 19750,
"packageId": null,
"amount": 3
},
{
"extrasId": 11,
"money": 64380,
"packageId": null,
"amount": 5
},
{
"extrasId": null,
"money": null,
"packageId": 3,
"amount": 1
}
]
}
I'm trying fetch 'additioanl'.
FOR i IN SELECT json_array_elements(createBooking.additional)
LOOP
sum := sum + i->>'money';
END LOOP;
It does not mater what is in body of loop.
When I calling this function via PostMan it is sending me response with error.
{
"hint": "No function matches the given name and argument types. You might need to add explicit type casts.",
"details": null,
"code": "42883",
"message": "function json_array_elements(text) does not exist"
}
Can some body explain me how should I fetch it?
Thank you.

Related

How to multiple update a nested array within document at couchbase?

I have a bucket called chat which includes document model like
{
"id": "50542400778551298_5001_abcdef",
"customer": {
...
},
"seller": {
...
},
"complaints": [],
"messages": [
{
"id": "2828911d-b96f-4b90-a52c-252d0324ab69",
"owner": "SELLER",
"content": "Merhabalar",
"readingDate": null,
"createdAt": "2022-06-27T09:16:42.49158+03:00"
},
{
"id": "8cd8c4b7-d599-4dfa-9fa7-be304a3b72b9",
"owner": "SELLER",
"content": "İlan hakkında detay bilgi verir misin?",
"readingDate": null,
"createdAt": "2022-06-27T09:17:43.329741+03:00"
},
{
"id": "4f0ae912-7d65-4e79-9058-2b048c4d9845",
"owner": "SELLER",
"content": "Huhuu",
"readingDate": null,
"createdDate": "2022-06-27T09:18:37.491531+03:00"
},
{
"id": "d2dd61b3-7ff1-4139-a411-9a917c00d4f3",
"owner": "SELLER",
"content": "Orada kimse var mı?",
"readingDate": null,
"createdAt": "2022-06-27T09:18:45.917564+03:00"
},
{
"id": "ea2d8dca-2f8c-4987-9c07-7515a6a0063f",
"owner": "SELLER",
"content": "Döner misininiz?",
"readingDate": null,
"createdAt": "2022-06-27T09:18:52.80337+03:00"
}
],
"status": "ACTIVE"
}
I want to update all messages readingDate a specific time like (2022-06-27T09:18:52.80337+03:00)
But I cannot find an easy way to accomplish this task.
I tried to write an N1QL like
UPDATE c.messages SET c.messages.readingDate = NOW_LOCAL()
FROM chat AS c
WHERE META().id = '50542400778551298_5001_abcdef';
but it is not working and gives error
[
{
"code": 3000,
"msg": "syntax error - at SET",
"query": "UPDATE c.messages SET c.messages.readingDate = NOW_LOCAL()\nFROM chat AS c \nWHERE META().id = '50542400778551298_5001_abcdef';"
}
]
I saw https://docs.couchbase.com/server/current/guides/bulk-operations.html.
As far as to my understanding, the general solution gets all messages and updates them based on cas.
But maybe I believe there is an easy way so I wanted to ask the StackOverflow community.
Thanks to this question I wrote n1ql like
UPDATE chat USE KEYS '50542400778551298_5001_abcdef'
SET c.readAt = NOW_LOCAL() FOR c IN messages END
RETURNING messages;

How to set 2 collection variables in postman using pre-request script if the id's in the response body have dependency?

I'm trying to set 2 collections variables in the postman using a pre-request script by picking the id's from response body. There are two id's namely id and subId, I need to set both the id's in the collection variables only if the id is linked to subId.
Need to get id and subId from below json response (there may be multiple records where id doesn't have subId value). Please help me to solve this.
{
"result": [
{
"id": 26,
"name": "Testing",
"code": "TST-012",
"branches": [
{
"emailId": null,
"country": {
"shortName": "Niu",
"currency": "New Zealand Dollar"
}
}
],
"subId": [
{
"id": 46,
"name": "qa",
"code": "qa"
}
]
},
{
"id": 27,
"name": "Testing",
"code": "TST-012",
"branches": [
{
"emailId": null,
"country": {
"shortName": "US",
"currency": "US Dollar"
}
}
],
"subId": null
}
]
}
Extract id that contains subId not null
const res = pm.response.json();
const matchEle = res.result.find(e => e.subId !== null);
pm.collectionVariables.set("id", matchEle.id); //26
Extract id inside subId
If get all id
const subIds = _.map(matchEle.subId, _.property("id"));
pm.collectionVariables.set("subIds", JSON.stringify(subIds)); //[46]
If only get first id
pm.collectionVariables.set("subId", matchEle.subId[0].id); //46

Laravel Join Relations for JSON

I am using Eloquent Relationship in Laravel to output my data to JSON. When I use Eloquent Relations they show up inside JSON as an object but I need only JSON output with variables inside without object or json, As shown below:
{
"id": 1,
"name": "Status changed",
"history_id": null,
"admin_id": 4,
"created_at": "2021-05-21T16:10:08.000000Z",
"updated_at": "2021-04-21T16:10:08.000000Z",
"type_id": 2,
"comment": "Horoshiy Specialist",
"result_id": 2,
"result_comment": "Prowerili anketu i obrazowanie",
"start_time": null,
"end_time": null,
},
Instead of this (the result for the relation one to many toJSON in LARVEL) :
$activity = Activity::find(1)->load('history')->toJSON();
result:
{
"id": 1,
"name": "Status changed",
"history_id": null,
"admin_id": 4,
"created_at": "2021-05-21T16:10:08.000000Z",
"updated_at": "2021-04-21T16:10:08.000000Z",
"history": [
{
"id": 1,
"type_id": 2,
"comment": "Horoshiy Specialist",
"result_id": 2,
"result_comment": "Prowerili anketu i obrazowanie",
"start_time": null,
"end_time": null,
}
]
},
Any ideas how this can be done ? I can do it using DB join (), but is there a way to do it using Eloquent? Besides :
$activity = \App\Models\Activity::find(1)
->join('activity_history', 'activity_history.id', '=',
'activities.history_id')->select('*')->get()
->toJSON();
Because this is not the best way to do it, because I am using the column names and the table names, which I have to go to the DB behind to look at them
You can around doing this by flattening the array when you get that result. Let's imagine you $object->history to get the result with the history relationship, and simply do:
Arr::dot($object->history);
So you'll get an array like:
{
"id": 1,
"name": "Status changed",
"history_id": null,
"admin_id": 4,
"created_at": "2021-05-21T16:10:08.000000Z",
"updated_at": "2021-04-21T16:10:08.000000Z",
"history.id": 1,
"history.type_id": 2,
"history.comment": "Horoshiy Specialist",
"history.result_id": 2,
"history.result_comment": "Prowerili anketu i obrazowanie",
"history.start_time": null,
"history.end_time": null,
}

How to return result that belongs to user and restrict other data being sent in the result in FeathersJS?

I am new to feathersjs and I've breaking my head on how to restrict users from viewing other's data. For instance from postman when you do find/get(id) http://localhost/users you see list of all the users that are currently registered. However I would only like to return the current users data.
From:
{
"total": 2,
"limit": 10,
"skip": 0,
"data": [
{
"id": 1,
"email": "test#feathersjs.com",
"auth0Id": null,
"createdAt": "2020-02-01T23:16:30.833Z",
"updatedAt": "2020-02-01T23:16:30.833Z"
},
{
"id": 2,
"email": "test1#feathersjs.com",
"auth0Id": null,
"createdAt": "2020-02-01T23:31:50.904Z",
"updatedAt": "2020-02-01T23:31:50.904Z"
}
]
}
To:
{
"total": 1,
"limit": 10,
"skip": 0,
"data": [
{
"id": 1,
"email": "test#feathersjs.com",
"auth0Id": null,
"createdAt": "2020-02-01T23:16:30.833Z",
"updatedAt": "2020-02-01T23:16:30.833Z"
},
]
}
How do I filter result in the after hook function? Can someone please explain.
Answer:
Missed a basic step. All the find(), update() etc return result based on parent class. By Customizing find() method in your own class and using Sequlize.findOne({}) you can return the result you desire.
Damn! I was stupid to not read the docs fully.

ember data custom serializer for a json

I have a Session object I want to model in Ember data. (actually display sessions but it's the same)
The JSON from the server looks like this (cannot be changed):
{
"metadata": {
"page": 1,
"page_size": 100,
"total_num_objects": 7,
"total_num_pages": 1
},
"result": [
{
"api_path": "/rest/sessions/2",
"end_time": 1412687629.42063,
"hostname": "127.0.0.1",
"id": 2,
"logical_id": "c6656738-4e23-11e4-9017-685b35b63131_0",
"product_name": null,
"product_revision": null,
"product_version": null,
"start_time": 1412687629.26851,
"status": "SUCCESS",
"type": "session",
"user_name": null
},
{
"api_path": "/rest/sessions/3",
"end_time": 1412688377.15329,
"hostname": "127.0.0.1",
"id": 3,
"logical_id": "84707366-4e25-11e4-a659-685b35b63131_0",
"product_name": null,
"product_revision": null,
"product_version": null,
"start_time": 1412688377.11507,
"status": "SUCCESS",
"type": "session",
"user_name": null
},
...
I realize I need to write a custom RESTSerializer but I can't figure out what do I need to do in order go get rid of metadata + make ember realize that result is actually a session.
Side question:
Can I make the DS.Model.extend attributes like what I get from the API or do I must use CamelCase and use normalizeHash
You'd probably need to do something like this:
App.SessionSerializer = DS.RESTSerializer.extend({
normalizePayload: function(payload) {
return {
sessions: payload.result
};
}
});
See: http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_normalizePayload
Regarding your other issue, look at DS.ActiveModelAdapter / ActiveModelSerializer. ActiveModelSerializer handles the underscore convention in the JSON.