Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic default object #4400

Open
lilingxi01 opened this issue Mar 8, 2022 · 1 comment
Open

Dynamic default object #4400

lilingxi01 opened this issue Mar 8, 2022 · 1 comment

Comments

@lilingxi01
Copy link

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

@kneth
Copy link
Contributor

kneth commented Mar 8, 2022

@lilingxi01 Dynamic defaults are currently not supported. I agree it is a useful feature.

We are investigating how to support class-based models (related to #3587), which will be a stepping stone to become on par with Realm Swift's many new data modeling features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants