I would like to track some backend-changes in the cloud.
The log of my before_save tells me something like this:
before_save triggered for Elements for user oKwM9mvEUn:
Input: {
"original":
{"elements":[
{"__type":"Pointer","className":"Element","objectId":"VzeuG3Z5N6"},
{"__type":"Pointer","className":"Element","objectId":"APkHJpgcms"},
{"__type":"Pointer","className":"Element","objectId":"xHG1jg8fny"}
]},
"update":
{"elements":[
{"__type":"Pointer","className":"Element","objectId":"VzeuG3Z5N6"},
{"__type":"Pointer","className":"Element","objectId":"xHG1jg8fny"}
]}
}
Result: Update changed to
{"elements":[
{"__type":"Pointer","className":"Element","objectId":"VzeuG3Z5N6"},
{"__type":"Pointer","className":"Element","objectId":"xHG1jg8fny"}
]}
The dirtyKeys tells me that "elements" has been changed, but I would like to know what exactly has been added (or removed) because I only like to track the changes...
Is there a possibility to access those "update" values? Or do I have to track it by myself in an other property?
Thanks for a hint!
As I posted on a similar question, you can get relation change details using:
request.operation.op(yourFieldName)
That returns a Parse.Op.Relation object.
Related
I have a table that storing information about an event. The event has a location attribute (String). Now I have a table locations table that stores location names together with their address.
Now I want to fetch all events from the database together with their location.
Because I get all the events from an external source, I can't use ids to setup a relationship.
So what I need is a relationship that works something like $this->hasOne('Location')->where('name','LIKE','%location_name%');
Is something like this possible? If not, what would be the best practice to achieve something like this so I get an output JSON like this:
{
"name":"Event Abc",
"date":"2019-10-18 12:00:00",
"location":"Location X",
"location_details": {
"name":"Location X",
"address":"Street XYZ"
}
}
Thanks
Yes it is possible. But you need little modification. So lets try and let me know what happened
function location () {
return $this->hasOne('App\Location')->where('name','LIKE','%location_name%')->get();
}
then call it from your controller
$eventsWithlocation = Event::with('location')->first();
dd($eventsWithlocation);
In CouchDB 2.0, I'm trying to create an ordered list as the keys from a View, but it doesn't work.
My code for the View document:
var i = 0;
function (doc) {
if (doc.type === "comment") {
emit(i++, doc.webpages);
}
}
The result is that all keys are equal to 0. How can I make it so that each document gets an autoincremented key?
Thanks!
A sequential ID probably isn't the best choice for most real applications. For example, if you were to build a commenting system I would approach it like this (there's a similar example in the couch docs):
Comments would be docs with a structure like this:
{
"_id": "comment_id",
"parent":"comment_id, or article_id if a top level comment"
"timestamp" : "iso datetime populated by the server",
"user_id": "the person who wrote the comment",
"content": "content of the comment"
}
To display all the top level comments of a given parent (either article or parent comment), you could use a view like this:
def function(doc){
emit([doc.parent, doc.timestamp, doc.user_id], doc._id)
}
To query this efficiently, you'd could use the following query options to grab the first twenty:
{
"startkey": ["parent_id"],
"endkey": ["parent_id", {}],
"limit": 20,
"skip": 0,
"include_docs": true
}
The comments will automatically be sorted by the date they were posted because the view is ordered by [parent, datetime, and then user]. You don't have the pass a value for anything other than parent with your key for benefit from this.
Another thing of note is by not passing the content of the comment to the view and instead using include_docs, your index will remain as slim as possible.
To expand on this:
If you want to show replies to a base comment, you can just change
the start and end keys to that comment's id.
If you want to show the next 20 comments, just change skip to 20.
If you want more comments shown initially, just up the limit value.
In answer to your comment, if you had an array or parents in your document like:
"parents" : ["a100", "a101", "a102"]
Everything else would remain the same, except you would emit a row for each parent.
def function(doc){
doc.parents.map( function (parent){
emit([doc.parent, doc.timestamp, doc.user_id], doc._id)
});
}
I have a table of users who each have an array of friends.
A document in it looks something like this:
{
id: "0ab43d81-b883-424a-be56-32f9ff98f7d2",
username: "testUser1234",
friends: [
"04423c56-1890-4028-b38a-cb9aff7112de" ,
"05e4e613-2131-408c-b0ae-a952f3007405" ,
"0395ee53-8ab0-48cc-aa4e-41aad93b8737"
]
}
I want to watch for changes on a user's friends'. A query like this will get me a list of friends:
r.db("Test").table("Users").get("0ab43d81-b883-424a-be56-32f9ff98f7d2")("friends").map(function(id) {
return r.db("Test").table("Users").get(id);
})
But, when I try to throw a .changes() on the end, RethinkDB tells me that it won't work:
RqlRuntimeError: Cannot call `changes` on an eager stream in:
r.db("Test").table("Users").get("0ab43d81-b883-424a-be56-32f9ff98f7d2")("friends").map(function(var_19) { return r.db("Test").table("Users").get(var_19); }).changes()
Is there anyway to get this to work? I am afraid that my only alternative is to subscribe to the friends list (in my app) and update the subscription to the actual friends when it changes:
r.db("Test").table("Users").getAll(friendId1, friendId2 , friendId3, friendId4).changes()
Not the end of the world, but I was really excited about being able to do it entirely in the DB.
Also, can anyone explain what an "eager stream" is? I think it has something to do with lazy vs. immediate evaluation, but I had no idea how to tell what the criteria determines whether a stream is eager or not.
I can get the query working with the following formation, inspired by this post:
r.db("Test").table('Users').getAll(r.args(
r.db('Test').table('Users').get("0ab43d81-b883-424a-be56-32f9ff98f7d2")('friends')
)).changes()
You can attach the .changes before some of the transofrmations.
r.db("Test")
.table("Users")
.get("0ab43d81-b883-424a-be56-32f9ff98f7d2")
.changes()
.getField('new_val')('friends')
.map(function(id) {
return r.db("Test").table("Users").get( id );
})
Basically, every time there is a change, the map function is executed. At the moment, that is the only way to do this type of operations with .changes, but that will change in upcoming versions of RethinkDB.
I am using RSpec 2 and trying to load a few instance variables at the top of a model spec. They are associated, but the associations aren't coming through.
The models are Column and Table -- this is not a problem the way we have it set up. Table has_many columns.
let!(:company) { Company.first }
let!(:events_table) { Table.create(name: "Events Test") }
let!(:text_column) { Column.create(title: "Title", name: "title", column_type: "text", table_id: events_table.id)}
let!(:start_date_column) { Column.create(title: "Start Date", name: "start_date", column_type: "date", table_id: events_table.id) }
The events_table is definitely getting created and returning an ID if I puts.attributes on it later on. I thought the lets! with the bang woudl allow it to get created in order so the association would exist.
I'm not sure that's happening though.
If I puts out the attributes for either of the columns, the table_id is nil.
How can I ensure it is not nil? What am I doing wrong?
Thanks in advance!!
Turns out that for some reason, maybe because I didn't have the inverse_of associations set up, that the relation just wasn't working.
When I add the inverse_of, and then change it to this, it works:
events_table.columns.create!(title: "Title Test")
I have a document like:
{
owner: 'alex',
live: 'some guid'
}
Two or more users can update live field simultaneously.
How can I make sure that only the first user wins and others updates fails?
You can get the semantics you want if you store some variable like "times_updated" in the document. Operations on a single document are atomic, so you can check that the field is the value you expect, and then throw an error if it isn't.
It might look something like:
var timesUpdated = 3
r.table('foo').get(rowId).update(function(row) {
return r.branch(row('timesUpdated').eq(timesUpdated),
{
timesUpdated: row('timesUpdated').add(1),
live: 'some special value'
},
r.error('Someone else updated the live field!')
);
}, {returnChanges: true})
So if another query comes in before you for timesUpdated = 3, your query will blow up. When do you get timesUpdated? That depends on how your app is designed, and what you're trying to do.
Another thing to note is that adding {returnChanges: true} is really useful because it allows you to get the new value of timesUpdated atomically. You can also see what exactly changed in the updated document.