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

Implement support for flat collections in Mixed #6364

Merged
merged 42 commits into from
Feb 1, 2024

Conversation

elle-j
Copy link
Contributor

@elle-j elle-j commented Jan 11, 2024

What, How & Why?

Adds support for storing flat lists and dictionaries as the underlying value of a Mixed property on non-synced realms. Hence, the list or dictionary can contain any of the previously allowed Mixed values, but not another nested collection. Nested lists and dictionaries will be implemented as a separate PR.

Sets are not supported as a Mixed value.

Notes

  • Tests for the array methods on List and OrderedCollection will be added as part of the nested collections implementation.
  • Core branch used.

Brief overview of usage

class CustomObject extends Realm.Object {
  value!: Realm.Types.Mixed;

  static schema: ObjectSchema = {
    name: "CustomObject",
    properties: {
      value: "mixed",
    },
  };
}

const realm = await Realm.open({ schema: [CustomObject] });

// Create an object with a list value as the Mixed property.
const realmObject = realm.write(() => {
  return realm.create(CustomObject, { value: [1, "hello", true] });
});

// Get all objects with a list as the Mixed `value` property
// that contains an item matching `"hello"` at index 1.
realm.objects(CustomObject).filtered(`value[1] == $0`, "hello");

// Update the Mixed property to a dictionary.
realm.write(() => {
  realmObject.value = { int: 1, string: "hello", bool: true };
});

// Get all objects with a dictionary as the Mixed `value`
// property that has a key `string` with the value `"hello"`.
realm.objects(CustomObject).filtered(`value.string == $0`, "hello");

This closes #6342 and #6343.

☑️ ToDos

  • 📝 Changelog entry (could add when the feature branch is merged into main)
  • 🚦 Tests

Copy link

coveralls-official bot commented Jan 11, 2024

Coverage Status

coverage: 85.931% (+0.3%) from 85.663%
when pulling efdb3d3 on lj/flat-collections-in-mixed
into 276dc67 on lj/collections-in-mixed.

integration-tests/tests/src/tests/mixed.ts Outdated Show resolved Hide resolved
packages/realm/src/Realm.ts Outdated Show resolved Hide resolved
@elle-j elle-j force-pushed the lj/flat-collections-in-mixed branch from 9c84b57 to 88c8d28 Compare January 19, 2024 14:06
@takameyer takameyer self-requested a review January 24, 2024 10:18
Copy link
Contributor

@kneth kneth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is amazing to see that collections in mixed is coming to JS 😍

packages/realm/src/PropertyHelpers.ts Show resolved Hide resolved
packages/realm/src/PropertyHelpers.ts Show resolved Hide resolved
Copy link
Contributor

@takameyer takameyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! LGTM

Copy link
Member

@kraenhansen kraenhansen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to see this much test vs implementation code. Great job!

@cla-bot cla-bot bot added the cla: yes label Jan 30, 2024
@elle-j elle-j force-pushed the lj/flat-collections-in-mixed branch from ab06b3f to efdb3d3 Compare February 1, 2024 12:40
@elle-j elle-j merged commit 90cac30 into lj/collections-in-mixed Feb 1, 2024
28 of 34 checks passed
@elle-j elle-j deleted the lj/flat-collections-in-mixed branch February 1, 2024 14:57
elle-j added a commit that referenced this pull request Feb 9, 2024
* Differentiate use of 'mixedToBinding()' for query arg validation.

* Refactor 'mixedFromBinding()' to own function and account for List and Dictionary.

* Implement setting a flat list and dictionary in Mixed.

* Implement accessing a flat list and dictionary in Mixed.

* Add tests for storing and accessing flat lists and dictionaries in Mixed.

* Refactor helper in test to not rely on collection position.

* Add tests for Set in Mixed throwing.

* Add tests for updating lists and dictionaries.

* Add tests for removing items in lists and dictionaries.

* Add tests for filtering lists and dictionaries by path.

* Throw if adding a set via property accessors.

* Group tests into separate sub-suites.

* Guard for embedded objects being set as Mixed value.

* Add tests for embedded objects in Mixed throwing.

* Add more filtering tests.

* Add 'at_keys' query tests to uncomment after Core bug fix.

* Add tests for inserting into lists and dictionaries.

* Add tests for notifications on lists.

* Add tests for notifications on dictionaries.

* Add tests for notifications on object when changed prop is list in mixed.

* Add tests for invalidating old list and dictionary.

* Minor updates to names.

* Add tests for notifications on object when changed prop is dictionary in mixed.

* Add tests for creating dictionary via object without prototype.

* Add tests filtering by query path using IN.

* Access array of changes only 1 time in notifications tests.

* Remove unnecessary type assertion.

* Update schema object names in tests.

* Add link to Core bug.

* Add tests for default list and dictionary in schema.

* Add tests for setting lists and dictionaries outside transaction.

* Add tests for spreading Realm and non-Realm objects as Dictionary.

* Add unit tests for 'isPOJO()'.

* Point to updated Core commit and enable related tests.

* Wrap chai's 'instanceOf()' in custom helper to assert type.

* Update helper function name to be consistent with other helpers.

* Add internal docs for 'isQueryArg'.

* Rename unit test file.

* Refactor notification tests into 'observable.ts'.

* Refactor notification tests to use test context.

* Use named import of 'ObjectSchema'.

* Group CRUD tests into subsuites.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants