-
Notifications
You must be signed in to change notification settings - Fork 89
(DOCSP-14573): set data type #1079
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
Changes from 6 commits
e59b6ae
b8be1d8
10ef9db
ad8f25e
b5322f9
5ddbb5f
62dae49
3030419
179fdfc
e5baab5
1c4a3a3
4a469f3
15fbdb8
81c94ad
55f6e4f
dc7d071
c5561b7
5312d69
ac8a4ec
cad7aec
545a3c0
a2e8387
2e37543
d6f993d
0114373
5d8338d
b5edd4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
realm.write(() => { | ||
link.inventory.add("hammer"); | ||
link.levelsCompleted.add(32); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// check if the hunter has completed level 3 by calling the `set.has()` method | ||
const hunterHasCompletedLevelThree = hunter.levelsCompleted.has(3); | ||
console.log( | ||
`Is level three completed by the hunter: ${hunterHasCompletedLevelThree}` | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// check how many items the hunter has in his inventory through the `set.size` property | ||
const hunterInventorySize = hunter.inventory.size; | ||
console.log(`The hunter has ${hunterInventorySize} inventory items`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
let link, hunter; | ||
realm.write(() => { | ||
link = realm.create("Character", { | ||
_id: new BSON.ObjectId(), | ||
name: "Link", | ||
inventory: ["elixir", "compass", "glowing shield"], | ||
levelsCompleted: [4, 9], | ||
}); | ||
hunter = realm.create("Character", { | ||
_id: new BSON.ObjectId(), | ||
name: "Hunter", | ||
inventory: ["estus flask", "gloves", "rune"], | ||
levelsCompleted: [1, 2, 5, 24], | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const characterSchema = { | ||
name: "Character", | ||
primaryKey: "_id", | ||
properties: { | ||
_id: "objectId", | ||
name: "string", | ||
levelsCompleted: "int<>", | ||
inventory: "string<>", | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
realm.write(() => { | ||
// clear all data from the inventory slot of the hunter by calling `set.clear()` in a write transaction | ||
hunter.inventory.clear(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
realm.write(() => { | ||
// remove the compass from link's inventory by calling `set.delete()` within a write transaction | ||
link.inventory.delete("compass"); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,88 @@ Sets - Node.js SDK | |
.. versionadded:: 10.5.0-beta.1 | ||
|
||
Overview | ||
-------- | ||
-------- | ||
A {+service-short+} set is a special object that allows you to store a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should make it conspicuous that "Realm Set" is a term, either through capitalization similar to Javascript "Set" or using a bold format. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree: A Realm Set is not a 1:1 mapping of a JavaScript Set (but heavily inspired by it). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair point, I've altered it to be " As for indicating that it is not a 1:1 mapping, I've included the line: "Realm sets work similarly to JavaScript Sets, except they must be a single type and can only be modified within a write-transaction" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "special object" sounds weird and doesn't really tel us anything. Since everyone is familiar with Arrays, perhaps a start like this?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm down for being a bit clearer on the advantages of Set. I do want to keep it a bit more subtle and not necessarily bring up array for the following reasons:
As for showing when to use set, I've added some info in the intro, the new intro reads: "A Realm Set is a special object that allows you to store a collection of unique values. Realm Sets are based on JavaScript Sets, but can only contain values of a single type and can only be modified within a write transaction. Sets allow you to perform math operations such as finding the union, intersection, or difference between two sets. To learn more about performing these operations, see the MDN docs for Implementing basic set operations." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The JS Set does not currently support the math operations. Its implementation has been kept as close as possible to JS's own set type. |
||
collection of unique values. {+service-short+} sets work similarly to JavaScript | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. took this suggestion almost verbatim 👍 |
||
:mdn:`Sets <Web/JavaScript/Reference/Global_Objects/Set>`, except they must be a single | ||
type and can only be modified within a write-transaction. | ||
mohammadhunan-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. note:: {+service-short+} Sets Do Not Guarantee Traversal Order | ||
|
||
When using a ``forEach()`` loop or other :mdn:`iteration methods | ||
<Web/JavaScript/Reference/Global_Objects/Set#iteration_methods>` to iterate | ||
through a loop, {+service-short+} sets may be in a different order than | ||
mohammadhunan-dev marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed the sentence to: "When using a forEach() loop or other iteration method to traverse the set in a loop, the content of the Realm Set may be in a different order than originally written to." |
||
originally written to. If you require an ordered version of your set, you | ||
must implement that order yourself. You can do this by creating an array of | ||
mohammadhunan-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
the set's values in the insertion order. | ||
mohammadhunan-dev marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The last sentence here is confusing. Are you saying the user should create an Array that gets updated every time the set is updated, and insert into the array in the order I want? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Essentially, that's it. I've altered it to be a bit clearer: "You can do this by creating an array from the set, using Array.from(mySet), and updating that array when the set has been modified through a change listener." |
||
|
||
.. _node-define-set-objects: | ||
|
||
Realm Object Models | ||
------------------- | ||
To define a property's values as a {+service-short+} set, specify the data type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be shortened to:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That works, fixed now 👍 |
||
followed by a less than and equal sign. For instance, for a set made of integer | ||
mohammadhunan-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
values, specify ``"int<>"``. | ||
|
||
.. literalinclude:: /examples/generated/node/data-types.codeblock.define-set-objects.js | ||
:language: javascript | ||
|
||
.. _node-create-set-objects: | ||
|
||
Create an Object With a Set | ||
--------------------------- | ||
To create an object with a property that value is of the set data type, create a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The wording here is confusing me.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about a combo of the two phrasings: "To create an object with a |
||
{+service-short+} object within a write transaction. When defining your | ||
{+service-short+} object, specify your set property's value as an array of | ||
initial values. | ||
|
||
.. literalinclude:: /examples/generated/node/data-types.codeblock.create-set-objects.js | ||
:language: javascript | ||
|
||
|
||
.. _node-add-items-to-set: | ||
|
||
Add Items to a Set | ||
------------------ | ||
To add items to a set, pass the new value to the ``set.add()`` method within a write transaction. | ||
mohammadhunan-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. literalinclude:: /examples/generated/node/data-types.codeblock.add-items-to-set.js | ||
:language: javascript | ||
|
||
.. _node-check-if-set-has-items: | ||
|
||
Check if a Set has Specific Items | ||
--------------------------------- | ||
To find out if a set has a particular value, pass the value to the ``set.has()`` method. The | ||
mohammadhunan-dev marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider changing "To find out" to "To determine" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
``set.has()`` method will return true if the set contains the value specified. | ||
mohammadhunan-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. literalinclude:: /examples/generated/node/data-types.codeblock.check-if-set-has-items.js | ||
:language: javascript | ||
mohammadhunan-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. _node-check-set-size: | ||
|
||
Check the Size of a Set | ||
----------------------- | ||
To discover how many items are in a set, you can check the set's ``size`` property. | ||
|
||
.. literalinclude:: /examples/generated/node/data-types.codeblock.check-set-size.js | ||
:language: javascript | ||
|
||
.. _node-remove-specific-item-from-set: | ||
|
||
Remove an Item from a Set | ||
------------------------- | ||
To remove a specific value from a set, pass the value to the ``set.delete()`` method within a write transaction. | ||
|
||
.. literalinclude:: /examples/generated/node/data-types.codeblock.remove-specific-item-from-set.js | ||
:language: javascript | ||
|
||
|
||
.. _node-remove-all-items-from-set: | ||
|
||
Remove all Items from a Set | ||
--------------------------- | ||
To clear the set, run the ``set.clear()`` method within a write transaction. | ||
|
||
.. literalinclude:: /examples/generated/node/data-types.codeblock.remove-all-items-from-set.js | ||
:language: javascript |
Uh oh!
There was an error while loading. Please reload this page.