Skip to content

Commit 2755206

Browse files
Mohammad Hunan ChughtaiMohammad Hunan Chughtaidacharyc
authored
(DOCSP-15613): Node.js collections type (#1044)
* Add content to Node.js > Data Types > Collections.txt * Removed unneeded spacing * clean up collections page + add headers * Filled out collections page * fix refs * fix typo * Update source/sdk/node/data-types/collections.txt * Update source/sdk/node/data-types/collections.txt * Update source/sdk/node/data-types/collections.txt Co-authored-by: Dachary <[email protected]> * fix rst * fix woridng * added ref * removed unneeded word * removed unneeded word * added period Co-authored-by: Mohammad Hunan Chughtai <[email protected]> Co-authored-by: Dachary <[email protected]>
1 parent c894f77 commit 2755206

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

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

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,125 @@ Collections - Node.js SDK
1414

1515
Overview
1616
--------
17+
18+
{+service-short+} has several types to represent groups of objects,
19+
which we call **collections**. A collection is an object that contains
20+
zero or more instances of one :ref:`{+service-short+} type
21+
<node-object-types>`. {+service-short+} collections are **homogenous**:
22+
all objects in a collection are of the same type.
23+
24+
You can filter and sort any collection using {+client-database+}'s
25+
:ref:`query engine <node-client-query-engine>`. Collections are
26+
:ref:`live <node-live-objects>`, so they always reflect the current state
27+
of the :term:`{+realm+} instance` on the current thread. You can also
28+
listen for changes in the collection by subscribing to :ref:`collection
29+
notifications <node-register-a-collection-change-listener>`.
30+
31+
.. _node-realm-results:
32+
33+
Results
34+
-------
35+
A :js-sdk:`Results <Realm.Results.html>` collection represents the
36+
lazily-evaluated results of a query operation. Results are immutable:
37+
you cannot add or remove elements to or from the results collection.
38+
Results have an associated query that determines their contents.
39+
40+
.. seealso::
41+
42+
:ref:`Reads <node-realm-database-reads>`
43+
44+
.. _node-realm-list:
45+
46+
Lists
47+
-----
48+
A :js-sdk:`List <Realm.List.html>` represents a :ref:`to-many
49+
relationship <node-to-many-relationship>` between two {+service-short+}
50+
types. Lists are mutable: within a write transaction, you can add and
51+
remove elements to and from a list. Lists are not associated with a
52+
query and are declared as a property of an :ref:`object model
53+
<node-object-schemas>`.
54+
55+
.. seealso::
56+
57+
:ref:`To-Many Relationships <node-to-many-relationship>`
58+
59+
.. _node-lazy-evaluated-results:
60+
61+
Results are Lazily Evaluated
62+
----------------------------
63+
{+client-database+} only runs a query when you request the
64+
results of that query. This lazy evaluation enables you to write
65+
elegant, highly-performant code for handling large data sets and complex
66+
queries.
67+
68+
.. _node-live-collections:
69+
70+
Collections are Live
71+
--------------------
72+
Like :ref:`live objects <node-live-objects>`, {+service-short+} collections
73+
are usually **live**:
74+
75+
- Live results collections always reflect the current results of the associated query.
76+
- Live lists always reflect the current state of the relationship on the {+realm+} instance.
77+
78+
A collection is **not** live when:
79+
80+
- it is a :ref:`results collection <node-realm-results>` that you are iterating through using a :mdn:`for..in <Web/JavaScript/Reference/Statements/for...in>` or :mdn:`for..of <Web/JavaScript/Reference/Statements/for...of>` statement. Both statements will continue to iterate through objects in the collection even if you have deleted or modified the collection's objects to exclude them from the filter that produced the results collection.
81+
- the collection is a frozen :js-sdk:`Results.snapshot() <Realm.Collection.html#snapshot>`.
82+
83+
Combined with :ref:`collection notifications
84+
<node-change-notifications>`, live collections enable
85+
reactive code. For example, suppose your view displays the
86+
results of a query. You can keep a reference to the results
87+
collection in your view class, then read the results
88+
collection as needed without having to refresh it or
89+
validate that it is up-to-date.
90+
91+
.. important:: Indexes may change
92+
93+
Since results update themselves automatically, do not
94+
store the positional index of an object in the collection
95+
or the count of objects in a collection. The stored index
96+
or count value could be outdated by the time you use
97+
it.
98+
99+
.. _node-working-with-collections:
100+
101+
Working With Collections
102+
------------------------
103+
104+
.. _node-limiting-query-results:
105+
106+
Limiting Query Results
107+
~~~~~~~~~~~~~~~~~~~~~~
108+
As a result of lazy evaluation, you do not need any special
109+
mechanism to limit query results with {+client-database+}. For example, if
110+
your query matches thousands of objects, but you only want
111+
to load the first ten, access only the first ten
112+
elements of the results collection.
113+
114+
.. _node-realm-result-pagination:
115+
116+
Pagination
117+
~~~~~~~~~~
118+
Thanks to lazy evaluation, the common task of pagination
119+
becomes quite simple. For example, suppose you have a
120+
results collection associated with a query that matches
121+
thousands of objects in your {+realm+}. You display one hundred
122+
objects per page. To advance to any page, simply access the
123+
elements of the results collection starting at the index
124+
that corresponds to the target page.
125+
126+
Summary
127+
-------
128+
- A {+service-short+} **collection** is a homogenous container of zero
129+
or more instances of one
130+
:ref:`{+service-short+} type <node-object-types>`.
131+
- There are two main kinds of collection: **lists** and **results**.
132+
Lists define the :ref:`to-many relationships <node-to-many-relationship>`
133+
of your {+service-short+} types, while results represent the
134+
lazily-loaded output of a :ref:`read operation <node-realm-database-reads>`.
135+
- Lazy evaluation of results collections means there is no need to
136+
design a special query to get limited or paginated results. Perform
137+
the query and read from the results collection as needed.
138+
- Data in {+service-short+} is *live*, which means that an object always reflects its most recent saved state.

0 commit comments

Comments
 (0)