Problem

      This is kinda a follow up issue related to #2393 but not really.

      I am facing a problem when I have a schema like this:

      const SystemInfoSchema = {
        name: "SystemInfo",
        primaryKey: "key",
        properties: {
          key: "string",
          value: "string",
          createdAt: { type: "date", default: new Date() }, // This line.
        },
      };
      

      Everytime I use realm.create to create an object, the field createdAt uses the time I opened the app to be the default value.

      I have tried to use realm.open for different creation but they are still all having the same createdAt field in one app launch.

      Re-launching or reloading the app does generate a different default value, but I think it should be generated everytime we create an entry.

      I believe that this behavior should be available for nearly every database framework and SQL statements, and Realm for Swift also support this behavior, so I am not sure if it is due to any configuration issue on my end, or is it not supported yet?

      If it is supported, I wonder how I can get the expected behavior.

      Solution

      The solution in #2393 could be useful.

      And if you are worrying about the API changes, it is possible to make another field to contain a function to generate the default value (so it will not stick to the value generated at the time schema is loaded).

      const SystemInfoSchema = {
        name: "SystemInfo",
        primaryKey: "key",
        properties: {
          key: "string",
          value: "string",
          createdAt: { type: "date", defaultGenerator: () => new Date() }, // This line.
        },
      };
      

      This should be fairly important because manually creating default values every time (even by wrapping it) is really annoying and it should not exist in 2022. In SQL or other frameworks, it is already supported for a long time, so it struggled me at first when I realized that it is probably not supported by the realm-js yet.

      How important is this improvement for you?

      Dealbreaker

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: