Skip to content

Commit c003974

Browse files
Mohammad Hunan Chughtaikneth
andauthored
(DOCSP-14573): set data type (#1079)
* added set examples + bluehawked * literal included set exampkes * added delete one set item example * fix typo * added descriptions to set * fix grammar * changed link + hunter to generic names * added capitalization to clearly define Realm Set as a term * fix wording + formatting" * clarified wording * fix literalincldue indentation * fix typo * more wording fixes * clean up realm object model for set wording * fix character names for examples * clarified traversal order wording * rst typo in <set> * changed set to mySet * fix overview wording * updated overview to be more clear on differences between array * updated create wording * Update source/examples/generated/node/data-types.codeblock.remove-all-items-from-set.js Co-authored-by: Kenneth Geisshirt <[email protected]> * Update source/examples/generated/node/data-types.codeblock.remove-specific-item-from-set.js Co-authored-by: Kenneth Geisshirt <[email protected]> * Update source/sdk/node/data-types/sets.txt Co-authored-by: Kenneth Geisshirt <[email protected]> * fix grammar + wording + changed Set to Realm.Set in js snippets * Fix woridng Co-authored-by: Kenneth Geisshirt <[email protected]>
1 parent 5f43200 commit c003974

9 files changed

+228
-1
lines changed

examples/node/Examples/data-types.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,92 @@ describe("Node.js Data Types", () => {
340340
// close the realm
341341
realm.close();
342342
});
343+
test("should work with the Set data type", async () => {
344+
// :code-block-start: define-set-objects
345+
const characterSchema = {
346+
name: "Character",
347+
primaryKey: "_id",
348+
properties: {
349+
_id: "objectId",
350+
name: "string",
351+
levelsCompleted: "int<>",
352+
inventory: "string<>",
353+
},
354+
};
355+
// :code-block-end:
356+
const realm = await Realm.open({
357+
schema: [characterSchema],
358+
});
359+
360+
// :code-block-start: create-set-objects
361+
let playerOne, playerTwo;
362+
realm.write(() => {
363+
playerOne = realm.create("Character", {
364+
_id: new BSON.ObjectId(),
365+
name: "PlayerOne",
366+
inventory: ["elixir", "compass", "glowing shield"],
367+
levelsCompleted: [4, 9],
368+
});
369+
playerTwo = realm.create("Character", {
370+
_id: new BSON.ObjectId(),
371+
name: "PlayerTwo",
372+
inventory: ["estus flask", "gloves", "rune"],
373+
levelsCompleted: [1, 2, 5, 24],
374+
});
375+
});
376+
// :code-block-end:
377+
378+
expect(playerOne.inventory.has("elixir")).toBe(true);
379+
expect(playerTwo.inventory.has("gloves")).toBe(true);
380+
381+
// :code-block-start: add-items-to-set
382+
realm.write(() => {
383+
playerOne.inventory.add("hammer");
384+
playerOne.levelsCompleted.add(32);
385+
});
386+
// :code-block-end:
387+
388+
expect(playerOne.inventory.size).toBe(4);
389+
expect(playerOne.levelsCompleted.size).toBe(3);
390+
391+
// :code-block-start: check-if-set-has-items
392+
// check if the playerTwo has completed level 3 by calling the `set.has()` method
393+
const playerTwoHasCompletedLevelThree = playerTwo.levelsCompleted.has(3);
394+
console.log(
395+
`Is level three completed by the playerTwo: ${playerTwoHasCompletedLevelThree}`
396+
);
397+
// :code-block-end:
398+
expect(playerTwoHasCompletedLevelThree).toBe(false);
399+
400+
// :code-block-start: remove-specific-item-from-set
401+
realm.write(() => {
402+
// remove the compass from playerOne's inventory by calling `set.delete()` within a write transaction
403+
playerOne.inventory.delete("compass");
404+
});
405+
406+
// :code-block-end:
407+
expect(playerOne.inventory.has("compass")).toBe(false);
408+
409+
// :code-block-start: remove-all-items-from-set
410+
realm.write(() => {
411+
// clear all data from the inventory slot of the playerTwo by calling `set.clear()` in a write transaction
412+
playerTwo.inventory.clear();
413+
});
414+
// :code-block-end:
415+
416+
// :code-block-start: check-set-size
417+
// check how many items the playerTwo has in his inventory through the `set.size` property
418+
const playerTwoInventorySize = playerTwo.inventory.size;
419+
console.log(`The playerTwo has ${playerTwoInventorySize} inventory items`);
420+
// :code-block-end:
421+
expect(playerTwo.inventory.size).toBe(0);
422+
423+
// delete the object specifically created in this test to keep tests idempotent
424+
realm.write(() => {
425+
realm.delete(playerOne);
426+
realm.delete(playerTwo);
427+
});
428+
// close the realm
429+
realm.close();
430+
});
343431
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
realm.write(() => {
2+
characterOne.inventory.add("hammer");
3+
characterOne.levelsCompleted.add(32);
4+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// check if the characterTwo has completed level 3 by calling the `Realm.Set.has()` method
2+
const characterTwoHasCompletedLevelThree = characterTwo.levelsCompleted.has(3);
3+
console.log(
4+
`Is level three completed by the characterTwo: ${characterTwoHasCompletedLevelThree}`
5+
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// check how many items the characterTwo has in his inventory through the `Realm.Set.size` property
2+
const characterTwoInventorySize = characterTwo.inventory.size;
3+
console.log(`The characterTwo has ${characterTwoInventorySize} inventory items`);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let characterOne, characterTwo;
2+
realm.write(() => {
3+
characterOne = realm.create("Character", {
4+
_id: new BSON.ObjectId(),
5+
name: "CharacterOne",
6+
inventory: ["elixir", "compass", "glowing shield"],
7+
levelsCompleted: [4, 9],
8+
});
9+
characterTwo = realm.create("Character", {
10+
_id: new BSON.ObjectId(),
11+
name: "CharacterTwo",
12+
inventory: ["estus flask", "gloves", "rune"],
13+
levelsCompleted: [1, 2, 5, 24],
14+
});
15+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const characterSchema = {
2+
name: "Character",
3+
primaryKey: "_id",
4+
properties: {
5+
_id: "objectId",
6+
name: "string",
7+
levelsCompleted: "int<>",
8+
inventory: "string<>",
9+
},
10+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
realm.write(() => {
2+
// clear all data from the inventory slot of the characterTwo by calling `Realm.Set.clear()` in a write transaction
3+
characterTwo.inventory.clear();
4+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
realm.write(() => {
2+
// remove the compass from characterOne's inventory by calling `Realm.Set.delete()` within a write transaction
3+
characterOne.inventory.delete("compass");
4+
});

source/sdk/node/data-types/sets.txt

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,98 @@ Sets - Node.js SDK
1515
.. versionadded:: 10.5.0-beta.1
1616

1717
Overview
18-
--------
18+
--------
19+
A ``{+service-short+} Set`` is a special object that allows you to store a
20+
collection of unique values. ``{+service-short+} Sets`` are based on JavaScript
21+
:mdn:`sets <Web/JavaScript/Reference/Global_Objects/Set>`, but can only contain
22+
values of a single type and can only be modified within a write transaction.
23+
Sets allow you to perform math operations such as finding the union,
24+
intersection, or difference between two sets. To learn more about performing
25+
these operations, see the MDN docs for :mdn:`Implementing basic set operations
26+
<Web/JavaScript/Reference/Global_Objects/Set#implementing_basic_set_operations>`.
27+
28+
.. note:: ``{+service-short+} Sets`` Do Not Guarantee Traversal Order
29+
30+
When using a ``forEach()`` loop or alternative :mdn:`iteration method
31+
<Web/JavaScript/Reference/Global_Objects/Set#iteration_methods>` to traverse
32+
the set in a loop, the content of the ``{+service-short+} Set`` may be in a
33+
different order than originally written to. If you require an ordered version
34+
of your set, you must implement that ordering yourself. If you require an
35+
ordered version of your set, you must implement that order yourself. You can
36+
do this by creating an array from the set, using :mdn:`Array.from(mySet)
37+
<Web/JavaScript/Reference/Global_Objects/Array/from>` or the :mdn:`spread
38+
operator <Web/JavaScript/Reference/Operators/Spread_syntax>`. You can keep
39+
that array updated by using a :ref:`change listener <node-object-listener>`
40+
to react to changes to the set.
41+
42+
43+
.. _node-define-set-objects:
44+
45+
Realm Object Models
46+
-------------------
47+
To define a property type as a ``{+service-short+} Set``, specify the data type
48+
you want in the set, followed by ``<>``. For instance, for a set made of integer
49+
values, specify ``"int<>"``.
50+
51+
.. literalinclude:: /examples/generated/node/data-types.codeblock.define-set-objects.js
52+
:language: javascript
53+
54+
.. _node-create-set-objects:
55+
56+
Create an Object With a Set
57+
---------------------------
58+
To create an object with a ``{+service-short+} Set`` property, you must create
59+
the object within a write transaction. When defining your {+service-short+}
60+
object, initialize the ``{+service-short+} Set`` by passing an empty array or an
61+
array with your initial values.
62+
63+
.. literalinclude:: /examples/generated/node/data-types.codeblock.create-set-objects.js
64+
:language: javascript
65+
66+
67+
.. _node-add-items-to-set:
68+
69+
Add Items to a Set
70+
------------------
71+
To add an item to a set, pass the new value to the ``<Realm.Set>.add()`` method within a write transaction.
72+
73+
.. literalinclude:: /examples/generated/node/data-types.codeblock.add-items-to-set.js
74+
:language: javascript
75+
76+
.. _node-check-if-set-has-items:
77+
78+
Check if a Set has Specific Items
79+
---------------------------------
80+
To determine if a set contains a particular value, pass the value to the ``<Realm.Set>.has()`` method. The
81+
``set.has()`` method will return true if the set contains the value specified.
82+
83+
.. literalinclude:: /examples/generated/node/data-types.codeblock.check-if-set-has-items.js
84+
:language: javascript
85+
86+
.. _node-check-set-size:
87+
88+
Check the Size of a Set
89+
-----------------------
90+
To discover how many items are in a set, you can check the set's ``size`` property.
91+
92+
.. literalinclude:: /examples/generated/node/data-types.codeblock.check-set-size.js
93+
:language: javascript
94+
95+
.. _node-remove-specific-item-from-set:
96+
97+
Remove an Item from a Set
98+
-------------------------
99+
To remove a specific value from a set, pass the value to the ``<Realm.Set>.delete()`` method within a write transaction.
100+
101+
.. literalinclude:: /examples/generated/node/data-types.codeblock.remove-specific-item-from-set.js
102+
:language: javascript
103+
104+
105+
.. _node-remove-all-items-from-set:
106+
107+
Remove all Items from a Set
108+
---------------------------
109+
To clear the set, run the ``<Realm.Set>.clear()`` method within a write transaction.
110+
111+
.. literalinclude:: /examples/generated/node/data-types.codeblock.remove-all-items-from-set.js
112+
:language: javascript

0 commit comments

Comments
 (0)