Skip to content

Commit 5f43200

Browse files
author
Mohammad Hunan Chughtai
authored
(DOCSP-14577): UUID (#1067)
* bump realmjs to 10.5.0-beta-1 * removed uuid as it's own page and added a subsection on it * added uuid bluehawked snippet + readded uuid to toc * added uuid examples * removed uuid from field types as a paragraph * update wording * update wording * fix passive voice * add specific uuid example * removed innacurate collections are homogenous (per mixed)
1 parent efae8d0 commit 5f43200

File tree

5 files changed

+92
-4
lines changed

5 files changed

+92
-4
lines changed

examples/node/Examples/data-types.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ describe("Node.js Data Types", () => {
194194
const Blaise = realm.objects("Dog").filtered(`name = 'Blaise'`)[0];
195195
const Euclid = realm.objects("Dog").filtered(`name = 'Euclid'`)[0];
196196
const Pythagoras = realm.objects("Dog").filtered(`name = 'Pythagoras'`)[0];
197+
// delete the objects to keep the tests idempotent
197198
realm.write(() => {
198199
realm.delete(Euler);
199200
realm.delete(Blaise);
@@ -299,4 +300,44 @@ describe("Node.js Data Types", () => {
299300
// close the realm
300301
realm.close();
301302
});
303+
test("should work with UUID", async () => {
304+
// :code-block-start: work-with-uuid
305+
const { UUID } = Realm.BSON;
306+
const ProfileSchema = {
307+
name: "Profile",
308+
primaryKey: "_id",
309+
properties: {
310+
_id: "uuid",
311+
name: "string",
312+
},
313+
};
314+
const realm = await Realm.open({
315+
schema: [ProfileSchema],
316+
});
317+
realm.write(() => {
318+
realm.create("Profile", {
319+
name: "John Doe.",
320+
_id: new UUID(), // create a _id with a randomly generated UUID
321+
});
322+
realm.create("Profile", {
323+
name: "Tim Doe.",
324+
_id: new UUID("882dd631-bc6e-4e0e-a9e8-f07b685fec8c"), // create a _id with a specific UUID value
325+
});
326+
});
327+
// :code-block-end:
328+
329+
const johnDoeProfile = realm
330+
.objects("Profile")
331+
.filtered("name = 'John Doe.'")[0];
332+
333+
// test if johnDoeProfile's _id is a valid UUID field
334+
expect(UUID.isValid(johnDoeProfile._id)).toBe(true);
335+
336+
// delete the objects to keep the tests idempotent
337+
realm.write(() => {
338+
realm.delete(johnDoeProfile);
339+
});
340+
// close the realm
341+
realm.close();
342+
});
302343
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { UUID } = Realm.BSON;
2+
const ProfileSchema = {
3+
name: "Profile",
4+
primaryKey: "_id",
5+
properties: {
6+
_id: "uuid",
7+
name: "string",
8+
},
9+
};
10+
const realm = await Realm.open({
11+
schema: [ProfileSchema],
12+
});
13+
realm.write(() => {
14+
realm.create("Profile", {
15+
name: "John Doe.",
16+
_id: new UUID(), // create a _id with a randomly generated UUID
17+
});
18+
realm.create("Profile", {
19+
name: "Tim Doe.",
20+
_id: new UUID("882dd631-bc6e-4e0e-a9e8-f07b685fec8c"), // create a _id with a specific UUID value
21+
});
22+
});

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ Overview
1818
{+service-short+} has several types to represent groups of objects,
1919
which we call **collections**. A collection is an object that contains
2020
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.
21+
<node-object-types>`.
2322

2423
You can filter and sort any collection using {+client-database+}'s
2524
:ref:`query engine <node-client-query-engine>`. Collections are

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ Field Types -Node.js SDK
2121
- ``objectId`` maps to BSON :manual:`ObjectId </reference/method/ObjectId/>` type.
2222
- ``data`` maps to the JavaScript :mdn:`ArrayBuffer <Web/JavaScript/Reference/Global_Objects/ArrayBuffer>` type.
2323
- ``date`` maps to the JavaScript :mdn:`Date <Web/JavaScript/Reference/Global_Objects/Date>` type.
24-
- ``list`` maps to the JavaScript :mdn:`Array <Web/JavaScript/Reference/Global_Objects/Array>` type. You can also specify that a field contains a list of a primitive value type by appending ``[]`` to the type name.
24+
- ``list`` maps to the JavaScript :mdn:`Array <Web/JavaScript/Reference/Global_Objects/Array>` type. You can also specify that a field contains a list of a primitive value type by appending ``[]`` to the type name.
25+
- ``uuid`` is a universally unique identifier from :js-sdk:`Realm.BSON <Realm.html#.BSON>`.
26+
27+

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,27 @@ UUID - Node.js SDK
1414
.. versionadded:: 10.5.0-beta.1
1515

1616
Overview
17-
--------
17+
--------
18+
19+
``UUID`` (Universal Unique Identifier) is a 16-byte :wikipedia:`unique value
20+
<Universally_unique_identifier>`. You can use ``UUID`` as an identifier for
21+
objects. ``UUID`` is :ref:`indexable <node-indexes>` and you can use it as a
22+
:ref:`primary key <node-primary-keys>`.
23+
24+
.. note:: Using UUID Instead of ObjectId
25+
26+
In general, you can use ``UUID`` in any field that you need a unique
27+
identifier. Using ``UUID`` might be particularly useful if you are migrating
28+
data not stored in MongoDB since it is likely that your object's unique
29+
identifiers are already of a ``UUID`` type.
30+
31+
Usage
32+
-----
33+
To define a property as a ``UUID``, set its type to the string ``"uuid"`` in
34+
your :ref:`object model <node-object-schemas>`. Create a {+service-short+}
35+
object within a write transaction. To set any unique identifier properties of
36+
your object to a random value, call ``new UUID()``. Alternatively, set a string
37+
to ``new UUID()`` to set the unique identifier property to a specific value.
38+
39+
.. literalinclude:: /examples/generated/node/data-types.codeblock.work-with-uuid.js
40+
:language: javascript

0 commit comments

Comments
 (0)