I would like to be able to track the mutated (modified) attributes of an object in sails during afterUpdate lifecycle callback.
Assuming an object is updated I would like to know which attribute has been modified during the update operation, I've been using sails-mongo and I believe I could write a proxy adapter that keep a local instance and attach it to the one thats going to be modified and do a diff on save but there might be an already existing way of doing so :)
Thanks !
Waterline doesn't currently have a built in diff mechanism. To accomplish this you can explore the use of native queries where some databases will allow you to return the values being updated or store the previous records in a diff on the record in the database.
afterUpdate won't be able to handle this because by then the results have already been updated in the database. You could write a controller method that uses the same criteria to capture all the records before you issue the update criteria.
Related
I am writing a CRUD app using Quarkus and Mongo, and thus am using a MongoCollection to implement this.
I am utilizing Hibernate Validators for validation to ensure my data is as it should be.
The issue I am running into is that the MongoCollection only provides updates using Bson (collection.updateOne(Bson search, Bson update)), and not for the entire object. This would be fine but keeps me from being able to properly use validators to ensure proper data integrity.
Until I hit this block, my idea for updating was to:
ingest generic json, in the form of ObjectNode and the object's id
get the object to update
Use Jackson's built-in updating features to apply the updates to the object from the given ObjectNode
Validate the resulting state
save the object to Mongo
However, this doesn't work when I can't update the whole object at once. Am I attacking this from the right angle? I've found a lot on how to do updates, but not a lot related to validation. I also see that I can specify on the Mongo side validation rules, but as I am fairly 'hands off' when using Mongo in this way, so needing to apply special Bson validation isn't ideal.
Is it possible for me to just re-insert the updated object to Mongo using `collection.insertOne(object)`? this assumes that the object would have the same `_id` as the original. Would this update the object as intended, or are there side effects?
Edit:: no, it is not. Mongo throws an error for duplicate keys.
Found it, what I wanted was collection.findOneAndReplace()
I would like to hook an event after an object is retrieved from the database. This event should be fired regardless if the object is retrieved from .find() or via a .populate() call. I would think it should be similar to the other lifecycle callbacks like afterCreate or afterUpdate, etc.
Is this possible to do with Waterline and if so, how can it be accomplished?
To give an idea of what I'm trying to accomplish: I'm using Mongo to
store my data. There is the potential that the model schema has
changed since the record was last saved. I'd like to "upgrade" the
record retrieved from the database to the latest version using a
function defined on the model itself. I would like this process to
happen as transparently as possible after a query returns (ie. Not
have to explicitly call the function in each query callback function).
I have been trying to get my head wrapped around MongoDB, as it's used by Spring, so I decided to start a little project in Spring Roo.
In my project, I am storing my User Login data to MongoDB. The trouble is that the registration process, which creates a new User object and stores it in the MongoDB, has a tendency to create duplicates despite the fact I have #Unique on the loginId field.
Now, I know part of the problem is that I am thinking about things from a JPA/RDBMS perspective, and MongoDB is not a relational DB and thus has a different set of parameters in which to operate with, but I having trouble finding guidance in anything more than a VERY simple sample code.
First, what Spring/Other annotations are available, and more importantly, commonly used when dealing with MongoDB from a Spring-world? Second, when dealing with documents that need to be "uniqued", how does one typically do this? Do you first search on the unique field to ensure it's not already there first, then do the insert? Third, in JPA-land, I could use the annotations #PrePersist and #PreUpdate to do last-minute data manipulation, like MD5-hashing passwords that have been updated or adding/updating a "Last Modified" date just prior to storing. I know this are JPA-isms, but can I still use those, and if not, is there an alternative for use with Spring Data/MongoDB?
I ended up using the #Id annotation on my Entities, which indicates which field is used as the id field. As long as the field is unique, writting subsequent updates will properly replace the existing entity instead of adding a new one.
I ended up creating additional method to check if there exists a data which have a duplicate value to the one we are entering.
If it exists, i return failure mentioning that there exist duplicate value. Otherwise it saves the newly entered value
I would like to know when entities in a certain database table are either created or updated. The application is essentially a CMS, and I need to know when changes are made to the content so that I can reindex them for searches.
I know that the autogenerated LINQ to EF class has overridable methods for when certain fields change, but I need to know when the whole object is created/updated, not just a single field. I tried putting it in OnCreated, only to find that meant OnObjectInitialized and not OnObjectInsertedIntoDBTable xD
I did some searching and came across this link. The "Entity State" section looks like its what I want, but I'm not sure how to use this information. Where do I override those methods?
Or perhaps there is a another/better way?
(I also need to know this for another part of the system, which will send notifications when certain content is changed. I would prefer this code to execute automatically when the insert/update occurs instead of placing it in a controller and hoping hoping I always call that method.)
You need to get ObjectStateEntry(s) from the ObjectStateManager property of the ObjectContect.
var objectStateEntries = this.ObjectStateManager.GetObjectStateEntries();
This entries contain every object state you've pulled down per context and what kind of actions where performed on them.
If you are using EF4 you can override the SaveChanges method to include this functionality. I've used this technique to audit every change that occurs in the database instead of triggers.
I have a requirement to only save data to a table in a database (I don't need to read it)
If the record already exists I want to update it otherwise I will add it.
It usually exists.
My entity context might already hold the object .. if it does I want to find it and use it again without causing it to refresh from the database when I 'find' it
i.e. The context holds a collection of entities (rows of a database) I want to find an entity in the collection and only want the context to go to the database if entity is not in the collection. I don't care about the current values of the entity .. I just want to update them.
Hope this is clear ..... thanks
I may not be quite seeing the question, but I believe your looking for some sort of caching mechanism, I know for work we use devForces IdeaBlade which does the trick, however I believe you can create a simple caching mechanism custom to you needs.
Link
The bits on caching will be helpful, if this doesnt help tell me and I can dig a little deeper.
I believe you need to use GetObjectByKey() instead of using an ObjectQuery I believe an ObjectQuery always hits the backend datastore whatever it may be.
More Info here http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.getobjectbykey.aspx