Loopback 3: Multiple HasOne relation on one model - loopback

So, I opened an issue here, coz in my opinion it should work as I think... but might be wrong so looking for another way
So, pretty much I have two models, Wedding and Person. The Wedding one has these relations set:
"people": {
"type": "hasMany",
"model": "person",
"foreignKey": "",
"options": {
"nestRemoting": true
}
},
"partner1": {
"type": "hasOne",
"model": "person",
"foreignKey": ""
},
"partner2": {
"type": "hasOne",
"model": "person",
"foreignKey": ""
}
And one of my wedding documents looks like this (I am using mongoDB if you cannot tell):
{
"_id": "5de78c76f89d1a8ad4091ca5",
"date": "2019-12-04T10:37:42.000Z",
"userId": "5de78c76f89d1a8ad4091ca4",
"created": "2019-12-04T10:37:42.720Z",
"partner1Id": "5de78c77f89d1a8ad4091ca6",
"partner2Id": "5de78c77f89d1a8ad4091ca7"
}
So, when I set include filter and do:
{ "include": ["partner1", "partner2"]}
in my loopback API explorer on
http://localhost:3000/api/weddings/5de78c76f89d1a8ad4091ca5
I get:
{
"date": "2019-12-04T10:37:42.000Z",
"id": "5de78c76f89d1a8ad4091ca5",
"userId": "5de78c76f89d1a8ad4091ca4",
"created": "2019-12-04T10:37:42.720Z",
"partner1Id": "5de78c77f89d1a8ad4091ca6",
"partner2Id": "5de78c77f89d1a8ad4091ca7",
"partner1": {
"id": "5de78c77f89d1a8ad4091ca7",
"fullName": "Jessica Alba",
"spouse": "spouse2",
"contacts": [],
"verified": false,
"created": "2019-12-04T10:37:43.292Z",
"updated": "2019-12-04T10:37:43.292Z",
"userId": "5de78c76f89d1a8ad4091ca4",
"weddingId": "5de78c76f89d1a8ad4091ca5"
},
"partner2": {
"id": "5de78c77f89d1a8ad4091ca7",
"fullName": "Jessica Alba",
"spouse": "spouse2",
"contacts": [],
"verified": false,
"created": "2019-12-04T10:37:43.292Z",
"updated": "2019-12-04T10:37:43.292Z",
"userId": "5de78c76f89d1a8ad4091ca4",
"weddingId": "5de78c76f89d1a8ad4091ca5"
}
}
But, I am expecting this:
{
"date": "2019-12-04T10:37:42.000Z",
"id": "5de78c76f89d1a8ad4091ca5",
"userId": "5de78c76f89d1a8ad4091ca4",
"created": "2019-12-04T10:37:42.720Z",
"partner1Id": "5de78c77f89d1a8ad4091ca6",
"partner2Id": "5de78c77f89d1a8ad4091ca7",
"partner1": {
"id": "5de78c77f89d1a8ad4091ca6",
"fullName": "Michael Knight",
"spouse": "spouse1",
"contacts": [],
"verified": false,
"created": "2019-12-04T10:37:43.292Z",
"updated": "2019-12-04T10:37:43.292Z",
"userId": "5de78c76f89d1a8ad4091ca4",
"weddingId": "5de78c76f89d1a8ad4091ca5"
},
"partner2": {
"id": "5de78c77f89d1a8ad4091ca7",
"fullName": "Jessica Alba",
"spouse": "spouse2",
"contacts": [],
"verified": false,
"created": "2019-12-04T10:37:43.292Z",
"updated": "2019-12-04T10:37:43.292Z",
"userId": "5de78c76f89d1a8ad4091ca4",
"weddingId": "5de78c76f89d1a8ad4091ca5"
}
}
Any ideas as of why? why do I get the same two records for partner1 and partner2?

Instead of using "hasOne", use "belongsTo".

Related

Mongodb query inside array with single ObjectID

Here is the json data i am trying to process. I am trying to get messages between two dates. The data is already imported to mongodb.
{
"items": [
{
"date": "2017-04-06T09:46:20.387420+00:00",
"from": {
"id": 4624534,
"links": {
"self": "https://api.hipchat.com/v2/user/4624534"
},
"mention_name": "holy",
"name": "holy god",
"version": "Y1ML0DRJ"
},
"id": "38f90558-2a23-458b-b87b-88dbdf997f7a",
"mentions": [],
"message": "ping",
"type": "message"
},
{
"date": "2017-04-08T04:30:44.240163+00:00",
"from": {
"id": 4624534,
"links": {
"self": "https://api.hipchat.com/v2/user/4624534"
},
"mention_name": "holy",
"name": "holy god",
"version": "Y1ML0DRJ"
},
"id": "822b81e0-8077-41d7-bc50-fc9e4eba7d9e",
"mentions": [],
"message": "https://twitter.com/",
"type": "message"
},
{
"attach_to": "822b81e0-8077-41d7-bc50-fc9e4eba7d9e",
"card": "{\"style\": \"link\", \"description\": \"From breaking news and entertainment to sports and politics, get the full story with all the live commentary.\", \"format\": \"medium\", \"url\": \"https://twitter.com/i/hello\", \"title\": \"Twitter. It's what's happening.\", \"id\": \"https://twitter.com/i/hello\", \"validation\": {\"safehtmls\": [\"activity.html\"], \"safeurls\": [\"url\", \"images.image\", \"images.image-small\", \"images.image-big\", \"icon.url\", \"icon.url#2x\", \"icon\", \"thumbnail.url#2x\", \"thumbnail.url\"]}, \"type\": \"link\", \"thumbnail\": {\"url\": \"https://pbs.twimg.com/ext_tw_video_thumb/850335753108324353/pu/img/T8cV-7bGbbguiRGV.jpg\", \"width\": 599, \"type\": \"image/jpeg\", \"height\": 337}, \"icon\": {\"url\": \"https://abs.twimg.com/a/1491551685/img/t1/favicon.svg\", \"type\": \"image\"}}",
"color": "gray",
"date": "2017-04-08T04:30:44.825185+00:00",
"from": "Link",
"id": "7ccaf2b9-09bb-45ac-a025-c93f1f7df745",
"mentions": [],
"message": "\n\n\n<p><b>Twitter. It's what's happening.</b></p>\n\n\n<p>From breaking news and entertainment to sports and politics, get the full story with all the live commentary.</p>\n\n",
"message_format": "html",
"notification_sender": {
"client_id": "888aec94-afee-45d8-89f7-ae077fcc4a7c",
"id": "hipchat-clinky",
"type": "addon"
},
"type": "notification"
},
{
"date": "2017-04-08T09:39:00.468858+00:00",
"from": {
"id": 4624534,
"links": {
"self": "https://api.hipchat.com/v2/user/4624534"
},
"mention_name": "abcholy",
"name": "holy god",
"version": "Y1ML0DRJ"
},
"id": "8a0de0e0-c312-490e-afcc-b0b16404cd67",
"mentions": [],
"message": "second message",
"type": "message"
},
{
"date": "2017-04-11T15:32:39.367744+00:00",
"from": {
"id": 4624534,
"links": {
"self": "https://api.hipchat.com/v2/user/4624534"
},
"mention_name": "abcholy",
"name": "holy god",
"version": "Y1ML0DRJ"
},
"id": "4c1a090c-cb71-4548-8f96-a03ec2f3fb3b",
"mentions": [],
"message": "https://gist.github.com/abcholy/6d86352e73eab21cdd4fe78b37bd5aa0",
"type": "message"
},
{
"date": "2017-04-11T15:33:42.730696+00:00",
"from": {
"id": 4624534,
"links": {
"self": "https://api.hipchat.com/v2/user/4624534"
},
"mention_name": "abcholy",
"name": "holy god",
"version": "Y1ML0DRJ"
},
"id": "a42b5267-937b-4de5-8c51-de4625742a4a",
"mentions": [],
"message": "hello",
"type": "message"
}
],
"links": {
"self": "https://api.hipchat.com/v2/room/3452990/history"
},
"maxResults": 100,
"startIndex": 0
}
Now, I entered this mongo query:
db.data.find( {"_id" : ObjectId("58ee59f7f35120aaba26cff0")},{ items: { $elemMatch: { "date": {$gte:"2017-04-08T04:30:44.240163+00:00"} } } } )
But it returns just a single item which is the first one. If I try $lte, it also returns single item but I want all the items which fall under the specification of date. How to achieve that?
Your date field is a string, it has no concept of what's greater than that string. You'll need to convert it to a ISODate or another date format for you to use the $gte or $lte operation.

MongoDB EmbedsMany Query

So I'm using loopback to create a RESTful node.js API. I'm using mLab for my database. I'm trying to query data that is embedded inside of my Account model.
An Account model has a property called favorites which has an array of Songs.
Here is an example:
{
"_id": {
"$oid": "58512c1b7c05c491b2cff1f6"
},
"firstName": "Matt",
"lastName": "Thomas",
"username": "mattietea",
"password": "$2a$10$dcquPHK48H5Q7FcaTyzgf.vnrUOgKpBENS9fSa0hjoaASs0uyOWba",
"email": "mattcthomas#me.com",
"_favorites": [
{
"title": "Ellusive & Eljay - Like This Before...",
"artist": "Ellusive",
"audio": "https://api.soundcloud.com/tracks/236100069/stream?client_id=90d140308348273b897fab79f44a7c89",
"image": "https://i1.sndcdn.com/artworks-000145902859-5467c0-t500x500.jpg",
"download": "https://api.soundcloud.com/tracks/236100069/download",
"url": "https://soundcloud.com/iamellusive/like-this-before",
"created": {
"$date": "2016-12-13T19:00:50.136Z"
},
"genres": {
"hipHop": 100
},
"id": "5850456247ef7e110099748b"
},
{
"title": "Dazed and Confused",
"artist": "Sir Skiff",
"audio": "https://api.soundcloud.com/tracks/103322663/stream?client_id=90d140308348273b897fab79f44a7c89",
"image": "https://i1.sndcdn.com/artworks-000054154948-m9cwii-t500x500.jpg",
"download": null,
"url": "http://soundcloud.com/skiffmusic/dazed-and-confused",
"created": {
"$date": "2016-12-13T08:49:59.128Z"
},
"genres": {
"hipHop": 100
},
"id": "584fb637fbf7df995f4341d3"
},
{
"title": "Runnat",
"artist": "Rory Fresco",
"audio": "https://api.soundcloud.com/tracks/258497600/stream?client_id=90d140308348273b897fab79f44a7c89",
"image": "https://i1.sndcdn.com/artworks-000157594908-cu10q4-t500x500.jpg",
"download": null,
"url": "http://soundcloud.com/roryxxx/runnat-2",
"created": {
"$date": "2016-12-13T08:50:52.397Z"
},
"genres": {
"hipHop": 100
},
"id": "584fb66cfbf7df995f4341d4"
},
{
"title": "Breathe (Prod. by Yuri Beat$)",
"artist": "Chiddy Bang",
"audio": "https://api.soundcloud.com/tracks/106182458/stream?client_id=90d140308348273b897fab79f44a7c89",
"image": "https://i1.sndcdn.com/artworks-000055716908-f9qdau-t500x500.jpg",
"download": null,
"url": "http://soundcloud.com/chiddybang/breathe-prod-by-yuri-beat",
"created": {
"$date": "2016-12-13T08:32:36.447Z"
},
"genres": {
"hipHop": 100
},
"id": "584fb224fbf7df995f4341ce"
}
]
}
I have an endpoint set up which returns all the favorites
api/Accounts/{id}/favorites
But I can't seem to query them. When I query all the songs from api/Songs it works but not when I query favorites.
The query I'm using is:
{'order': 'created DESC'}
I've tried multiple other queries, and none work.
Thank you!

Facebook real time updates giving incomplete information on page events

I have subscribed for page subscription for Facebook real time updates with Fields as "feed". When i am creating a new event, the Facebook real time updates sends me a wrong entry. The entry i got as a real time update is below.
{
"entry": [
{
"changes": [
{
"field": "feed",
"value": {
"photo_id": 1128416807216170,
"**post_id": "902483569809496_1128416807216170"**,
"sender_name": "Unofficial: Aerospace Demodavid",
"sender_id": 902483569809496,
"item": "photo",
"verb": "edited",
"link": "https://scontent.ford1-1.fna.fbcdn.net/t31.0-8/13717445_1128416807216170_1696828468485436508_o.jpg",
"published": 1,
"created_time": 1470269376
}
}
],
"id": "902483569809496",
"time": 1470269377
}
],
"object": "page"
}
From the above entry, if i query the Graph API for postid : 902483569809496_1128416807216170 , i get the below JSON response
{
"created_time": "2016-08-04T00:09:36+0000",
"actions": [
{
"name": "Like",
"link": "https://www.facebook.com/902483569809496/photos/gm.713007008848308/1128416807216170/?type=3"
},
{
"name": "Comment",
"link": "https://www.facebook.com/902483569809496/photos/gm.713007008848308/1128416807216170/?type=3"
},
{
"name": "See Friendship",
"link": "https://m.facebook.com/friendship/902483569809496/713006992181643/"
}
],
"from": {
"name": "Unofficial: Aerospace Demodavid",
"category": "Aerospace/Defense",
"id": "902483569809496"
},
"icon": "https://www.facebook.com/images/icons/photo.gif",
"is_hidden": false,
"is_expired": false,
"link": "https://www.facebook.com/902483569809496/photos/gm.713007008848308/1128416807216170/?type=3",
"object_id": "1128416807216170",
"picture": "https://scontent.xx.fbcdn.net/v/t1.0-0/s130x130/13892116_1128416807216170_1696828468485436508_n.jpg?oh=967e908192fcadc72eae9c11047f87e2&oe=582D6009",
"privacy": {
"allow": "",
"deny": "",
"description": "",
"friends": "",
"value": "EVERYONE"
},
"status_type": "added_photos",
"subscribed": false,
"to": {
"data": [
{
"id": "713006992181643",
"name": "Stevie G Event"
}
]
},
"type": "photo",
"updated_time": "2016-08-04T00:09:36+0000",
"id": "902483569809496_1128416807216170"
}
But the actual thing added here was an event and not a photo. The id should be 902483569809496_713006992181643 as in the below JSON and not 902483569809496_1128416807216170 which came from the real time update. Below is the JSON response which i am getting when i query the Graph API manually using the account id without using the real time update information.
{
"id": "902483569809496_713006992181643",
"from": {
"name": "Unofficial: Aerospace Demodavid",
"category": "Aerospace/Defense",
"id": "902483569809496"
},
"story": "Unofficial: Aerospace Demodavid added an event.",
"story_tags": {
"0": [
{
"id": "902483569809496",
"name": "Unofficial: Aerospace Demodavid",
"type": "page",
"offset": 0,
"length": 31
}
],
"41": [
{
"id": "713006992181643",
"name": "Stevie G Event",
"type": "event",
"offset": 41,
"length": 5
}
]
},
"picture": "https://scontent.xx.fbcdn.net/v/t1.0-0/c39.0.130.130/p130x130/13892116_1128416807216170_1696828468485436508_n.jpg?oh=5adc792bca82e0b110b3d6d98b81feae&oe=581E2680",
"link": "https://www.facebook.com/events/713006992181643/",
"name": "Stevie G Event",
"caption": "Stevie G Event",
"description": "Stevie G is the best",
"icon": "https://www.facebook.com/images/icons/event.gif",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/902483569809496/posts/713006992181643"
},
{
"name": "Like",
"link": "https://www.facebook.com/902483569809496/posts/713006992181643"
}
],
"privacy": {
"value": "EVERYONE",
"description": "",
"friends": "",
"allow": "",
"deny": ""
},
"type": "event",
"status_type": "created_event",
"object_id": "713006992181643",
"created_time": "2016-08-04T00:09:35+0000",
"updated_time": "2016-08-04T00:09:35+0000",
"is_hidden": false,
"is_expired": false
}
Why is there an inconsistency with the Facebook real time updates sent for an event creation. It treats it as a photo edit and not an event creation.
Please let me know if i am missing something.

what is the date format from stash api?

In the below json response, what is the date format for createdDate and updatedDate? I am not sure how to work in reverse to find what format the api is using for date. I couldn't find this any where in the documentation.
{
"size": 1,
"limit": 25,
"isLastPage": true,
"values": [
{
"id": 101,
"version": 1,
"title": "Talking Nerdy",
"description": "It’s a kludge, but put the tuple from the database in the cache.",
"state": "OPEN",
"open": true,
"closed": false,
"createdDate": 1359075920,
"updatedDate": 1359085920,
"fromRef": {
"id": "refs/heads/feature-ABC-123",
"repository": {
"slug": "my-repo",
"name": null,
"project": {
"key": "PRJ"
}
}
},
"toRef": {
"id": "refs/heads/master",
"repository": {
"slug": "my-repo",
"name": null,
"project": {
"key": "PRJ"
}
}
},
"locked": false,
"author": {
"user": {
"name": "tom",
"emailAddress": "tom#example.com",
"id": 115026,
"displayName": "Tom",
"active": true,
"slug": "tom",
"type": "NORMAL"
},
"role": "AUTHOR",
"approved": true
},
"reviewers": [
{
"user": {
"name": "jcitizen",
"emailAddress": "jane#example.com",
"id": 101,
"displayName": "Jane Citizen",
"active": true,
"slug": "jcitizen",
"type": "NORMAL"
},
"role": "REVIEWER",
"approved": true
}
],
"participants": [
{
"user": {
"name": "dick",
"emailAddress": "dick#example.com",
"id": 3083181,
"displayName": "Dick",
"active": true,
"slug": "dick",
"type": "NORMAL"
},
"role": "PARTICIPANT",
"approved": false
},
{
"user": {
"name": "harry",
"emailAddress": "harry#example.com",
"id": 99049120,
"displayName": "Harry",
"active": true,
"slug": "harry",
"type": "NORMAL"
},
"role": "PARTICIPANT",
"approved": true
}
],
"link": {
"url": "http://link/to/pullrequest",
"rel": "self"
},
"links": {
"self": [
{
"href": "http://link/to/pullrequest"
}
]
}
}
],
"start": 0
}
Just making a note that in my case, it is a UNIX timestamp, but I have to remove three trailing zeroes. E.g. the data looks like this:
"createdDate":1555621993000
If interpreted as a UNIX timestamp, that would be 09/12/51265 # 4:16am (UTC).
By removing the three trailing zeroes I get 1555621993, which is the correct time 04/18/2019 # 9:13pm (UTC)
Your mileage may vary but that was a key discovery for me :)
It looks like a UNIX timestamp.
https://en.wikipedia.org/wiki/Unix_time

Domino 9.x calendar service create meeting

I have been following this guide to work on Domino 9.0.1
Domino Calendar services
I am using JSON and the POST command works but creates an appointment, what I want to do is create a meeting. I have tried setting other fields like event['x-lotus-appttype'].data or event.AppointmentType = 3 but I still get an appointment.
JSON I am sending
{
"events": [
{
"summary":"Meeting 1",
"location":"Location 1",
"start": {
"date":"2013-12-01",
"time":"13:00:00",
"utc":true
},
"end": {
"date":"2013-12-01",
"time":"14:00:00",
"utc":true
}
}
]
}
What is the correct JSON format to create a meeting ?
Take a look at the following documentation: Event with attendees represented in JSON format
EXAMPLE 4. Event with attendees and time zone array:
{
"x-lotus-charset": {
"data": "UTF-8"
},
"timezones": [
{
"tzid": "Eastern",
"standard": {
"start": {
"date": "1950-11-05",
"time": "02:00:00"
},
"offsetFrom": "-0400",
"offsetTo": "-0500",
"recurrenceRule": "FREQ=YEARLY;BYMONTH=11;BYDAY=1SU;BYHOUR=2;BYMINUTE=0"
},
"daylight": {
"start": {
"date": "1950-03-12",
"time": "02:00:00"
},
"offsetFrom": "-0500",
"offsetTo": "-0400",
"recurrenceRule": "FREQ=YEARLY;BYMONTH=3;BYDAY=2SU;BYHOUR=2;BYMINUTE=0"
}
}
],
"events": [
{
"href": "/mail/dlawson.nsf/api/calendar/events/DB7E0BAC21EC322A85257BD200756E26-Lotus_Notes_Generated",
"id": "DB7E0BAC21EC322A85257BD200756E26-Lotus_Notes_Generated",
"summary": "Staff meeting",
"location": "Ray's office",
"description": "Please email your status update 24 hours before the meeting.",
"start": {
"date": "2013-09-12",
"time": "09:00:00",
"tzid": "Eastern"
},
"end": {
"date": "2013-09-12",
"time": "10:00:00",
"tzid": "Eastern"
},
"class": "public",
"transparency": "opaque",
"sequence": 0,
"last-modified": "20130825T212457Z",
"attendees": [
{
"role": "chair",
"status": "accepted",
"rsvp": false,
"displayName": "Duke Lawson/Peaks",
"email": "DukeLawson#swg.usma.ibm.com"
},
{
"role": "req-participant",
"status": "needs-action",
"rsvp": true,
"displayName": "Dean Melnyk/Peaks",
"email": "DeanMelnyk#swg.usma.ibm.com"
},
{
"role": "req-participant",
"status": "needs-action",
"rsvp": true,
"displayName": "Raymond Chan/Peaks",
"email": "RaymondChan#swg.usma.ibm.com"
}
],
"organizer": {
"displayName": "Duke Lawson/Peaks",
"email": "DukeLawson#swg.usma.ibm.com"
},
"x-lotus-broadcast": {
"data": "FALSE"
},
"x-lotus-notesversion": {
"data": "2"
},
"x-lotus-appttype": {
"data": "3"
}
}
]
}
I hope this can help :)