In our application we're getting initialValues for a form from an API call that returns something like:
{
myDate: "2000-01-01T00:00:00"
}
We then convert that value to a moment and save it to the store and populate initialValues from that. If we then select a new value (say, 2001-01-01, also a moment) and save the form, we read the response back from the api, convert it back to a moment, and throw it back in initialValues. The problem is, now our field is dirty because moment("2001-01-01") !== moment("2001-01-01").
Is our workflow wrong? Is there a better way to handle managing initialState?
The problem is that if moment("2001-01-01") does not equal moment("2001-01-01") then redux-form will always think that the form state has changed if the value for myDate is reinitialized, even though the value used to create the Moment instance is the same.
The fix is simple: Don't store moment objects in the redux store. Instead use a formatted string, like "2000-01-01T00:00:00", or or a number (unix timestamps) to represent datetime information in the redux store. If you need to do operations, like date1.isBefore(date2), you can convert them to Moments temporarily.
Related
I am trying to use the DynamoDBAutoGeneratedTimestamp annotation for generating timestamps for when my records are last updated and when they are created. The annotation currently has 2 strategies, ALWAYS and CREATE.
My initial understanding was that the ALWAYS strategy should update the timestamp every time the record is changed, and the CREATE strategy should set the timestamp only when the record is created. The ALWAYS strategy appears to be working just fine in updating the timestamp each time the record changes; however, the CREATE strategy appears to be behaving exactly the same as the ALWAYS strategy (meaning the timestamp updates each time the record changes).
It is now my understanding that the CREATE strategy doesn't do that, and instead tells the record to generate a timestamp if you're going to write it to the database and the value of the field in the record to be written is null.
This causes the field with the CREATE strategy to always be updated if the record itself is ever updated.
So, are there any easy solutions to this? I could set it myself, of course, but I was wondering if there is some sort of option or config that is not known to me that would address this issue?
Example code:
#DynamoDBTable(tableName = "Foo")
public class Foo {
#DynamoDBAutoGeneratedTimestamp(strategy = DynamoDBAutoGenerateStrategy.CREATE)
private Date createdAt;
#DynamoDBAutoGeneratedTimestamp(strategy = DynamoDBAutoGenerateStrategy.ALWAYS)
private Date updatedAt;
}
Is it good practice to require named parameters for data models in flutter when you know that the data should always have a specific field like an id or username? I think this would remove unnecessary null checking, but I've always seen model classes just use named parameters with fromJson and toJson methods especially if the data is being pulled from a database like Firebase. So is there a reason not to require fields?
Also when would it be a good idea to require a String field and initially populate it with an empty String vs making it nullable and initializing it with a null value?
Below is my opinion, i'm not sure it's true for all but it works for me
Reason 1: it's too long and unnecessary to write the whole thing out, some Jsons can be very complicated to do this, moreover when you get the data in JSON you'll want to simply initialize var A = A.fromJson(jsonData) instead of having to pass it one by one into the Model, moreover this can be repeated many times.
Reason 2: String with null(String?) and String with empty value(String) is very different. With me, I use String? to determine that "it has not received value yet", if the value is non-null then it has received a value including empty. In contrast to a String whose initial value is empty, I cannot distinguish whether it has received empty or has not received data.
Example: when i assign empty value to text2, it cannot distinguish whether it received the value before. This can be used to calculate the display of the Indicators before the variable actually gets a value.
I've recently discovered firestore.Timestamp and so I tried to assign firestore.FieldValue.serverTimestamp(); to one and it didn't work. instead it says Type 'FieldValue' is not assignable to type 'Timestamp' which I would say is strange. Shouldn't serverTimestamp() be a Timestamp and not a FieldValue? What would be the purpose of firestore.Timestamp if not to save timestamps to Firestore? Is there a way to get a server timestamp that is compatible with firestore.Timestamp or should I be avoiding timestamps altogether, and just stick to firestore.FieldValue for all my dates?
serverTimestamp() just returns sentinel value that tells the Firestore server that it should use the current time on the server as the value for the field you're trying to set. It doesn't return an actual Timestamp object itself.
The reason why you'd use this is to make sure that dates are being set consistently on the server instead of depending on the clock being correct on the user's device.
If you want to know the current time on the device, just use the native date/time objects provided by the language or operating system.
I've read almost everywhere about structuring one's Firebase Database for efficient querying, but I am still a little confused between two alternatives that I have.
For example, let's say I want to get all of a user's "maxBenchPressSessions" from the past 7 days or so.
I'm stuck between picking between these two structures:
In the first array, I use the user's id as an attribute to index on whether true or false. In the second, I use userId as the attribute NAME whose value would be the user's id.
Is one faster than the other, or would they be indexed a relatively same manner? I kind of new to database design, so I want to make sure that I'm following correct practices.
PROGRESS
I have come up with a solution that will both flatten my database AND allow me to add a ListenerForSingleValueEvent using orderBy ONLY once, but only when I want to check if a user has a session saved for a specific day.
I can have each maxBenchPressSession object have a key in the format of userId_dateString. However, if I want to get all the user's sessions from the last 7 days, I don't know how to do it in one query.
Any ideas?
I recommend to watch the video. It is told about the structuring of the data very well.
References to the playlist on the firebase 3
Firebase 3.0: Data Modelling
Firebase 3.0: Node Client
As I understand the principle firebase to use it effectively. Should be as small as possible to query the data and it does not matter how many requests.
But you will approach such a request. We'll have to add another field to the database "negativeDate".
This field allows you to get the last seven entries. Here's a video -
https://www.youtube.com/watch?v=nMR_JPfL4qg&feature=youtu.be&t=4m36s
.limitToLast(7) - 7 entries
.orderByChild('negativeDate') - sort by date
Example of a request:
const ref = firebase.database().ref('maxBenchPressSession');
ref.orderByChild('negativeDate').limitToLast(7).on('value', function(snap){ })
Then add the user, and it puts all of its sessions.
const ref = firebase.database().ref('maxBenchPressSession/' + userId);
ref.orderByChild('negativeDate').limitToLast(7).on('value', function(snap){ })
I have a table in my SQL Server DB that holds auditing information for certain actions a user takes within my system. Things like who performed the action, when it was performed, and what action are all pieces of information that can easily span multiple actions. But depending on the action performed, there may be other information that I want to capture, that is specific to the action. To handle this, I elected to add an "XML Metadata" column to the table that holds serialized XML of different metadata objects that I've created. I created a metadata object for each of the actions that I'm interested in tracking extra for. So each object is responsible for tracking specific extra information (metadata) for it's action. The objects are serialized and written to my new column.
I have SystemAction objects that I use to store information from this table, and I've added a string field that holds the XML string from the DB. The problem is, when I'm reading this XML back from the SystemAction objects, I'm struggling with a way to generically translate it back into it's correct metadata object. Each metadata object is going to have different fields, and each object has it's own static method that takes an XML string and attempts to return the metadata object type. So I could say:
SomeActionMetadata mdObj = SomeActionMetadata.BuildFromXML(xmlStringFromDB);
But I really don't know of a way to say "Here's some XML that could translate to any number of different objects. Figure it out and give me the right object back."
Given my current implementation, I could always just assign a unique ID to each metadata object that is stored as a field in each object, then use a case statement to switch on that ID and use the appropriate class's static build method to build the right object. But I was hoping for something a little more automatic than that. What if I have a List of SystemAction objects and just want to loop through them and generate the correct metadata object type?
I was hoping someone might have run across something similar to this before, or could point me to an article or post that could help me out. Thanks very much.
As indicated by Subhash Dike in the comments below, there is a similar SO question here that was able to point me in the right direction.