diff --git a/examples/node/Examples/data-types.js b/examples/node/Examples/data-types.js index 285ee631f6..cc1209df70 100644 --- a/examples/node/Examples/data-types.js +++ b/examples/node/Examples/data-types.js @@ -194,6 +194,7 @@ describe("Node.js Data Types", () => { const Blaise = realm.objects("Dog").filtered(`name = 'Blaise'`)[0]; const Euclid = realm.objects("Dog").filtered(`name = 'Euclid'`)[0]; const Pythagoras = realm.objects("Dog").filtered(`name = 'Pythagoras'`)[0]; + // delete the objects to keep the tests idempotent realm.write(() => { realm.delete(Euler); realm.delete(Blaise); @@ -299,4 +300,44 @@ describe("Node.js Data Types", () => { // close the realm realm.close(); }); + test("should work with UUID", async () => { + // :code-block-start: work-with-uuid + const { UUID } = Realm.BSON; + const ProfileSchema = { + name: "Profile", + primaryKey: "_id", + properties: { + _id: "uuid", + name: "string", + }, + }; + const realm = await Realm.open({ + schema: [ProfileSchema], + }); + realm.write(() => { + realm.create("Profile", { + name: "John Doe.", + _id: new UUID(), // create a _id with a randomly generated UUID + }); + realm.create("Profile", { + name: "Tim Doe.", + _id: new UUID("882dd631-bc6e-4e0e-a9e8-f07b685fec8c"), // create a _id with a specific UUID value + }); + }); + // :code-block-end: + + const johnDoeProfile = realm + .objects("Profile") + .filtered("name = 'John Doe.'")[0]; + + // test if johnDoeProfile's _id is a valid UUID field + expect(UUID.isValid(johnDoeProfile._id)).toBe(true); + + // delete the objects to keep the tests idempotent + realm.write(() => { + realm.delete(johnDoeProfile); + }); + // close the realm + realm.close(); + }); }); diff --git a/source/examples/generated/node/data-types.codeblock.work-with-uuid.js b/source/examples/generated/node/data-types.codeblock.work-with-uuid.js new file mode 100644 index 0000000000..cea1351927 --- /dev/null +++ b/source/examples/generated/node/data-types.codeblock.work-with-uuid.js @@ -0,0 +1,22 @@ +const { UUID } = Realm.BSON; +const ProfileSchema = { + name: "Profile", + primaryKey: "_id", + properties: { + _id: "uuid", + name: "string", + }, +}; +const realm = await Realm.open({ + schema: [ProfileSchema], +}); +realm.write(() => { + realm.create("Profile", { + name: "John Doe.", + _id: new UUID(), // create a _id with a randomly generated UUID + }); + realm.create("Profile", { + name: "Tim Doe.", + _id: new UUID("882dd631-bc6e-4e0e-a9e8-f07b685fec8c"), // create a _id with a specific UUID value + }); +}); diff --git a/source/sdk/node/data-types/collections.txt b/source/sdk/node/data-types/collections.txt index 22faee6f5c..07955f3244 100644 --- a/source/sdk/node/data-types/collections.txt +++ b/source/sdk/node/data-types/collections.txt @@ -18,8 +18,7 @@ Overview {+service-short+} has several types to represent groups of objects, which we call **collections**. A collection is an object that contains zero or more instances of one :ref:`{+service-short+} type -`. {+service-short+} collections are **homogenous**: -all objects in a collection are of the same type. +`. You can filter and sort any collection using {+client-database+}'s :ref:`query engine `. Collections are diff --git a/source/sdk/node/data-types/field-types.txt b/source/sdk/node/data-types/field-types.txt index 9037883fa7..5dead1e9bf 100644 --- a/source/sdk/node/data-types/field-types.txt +++ b/source/sdk/node/data-types/field-types.txt @@ -21,4 +21,7 @@ Field Types -Node.js SDK - ``objectId`` maps to BSON :manual:`ObjectId ` type. - ``data`` maps to the JavaScript :mdn:`ArrayBuffer ` type. - ``date`` maps to the JavaScript :mdn:`Date ` type. -- ``list`` maps to the JavaScript :mdn:`Array ` type. You can also specify that a field contains a list of a primitive value type by appending ``[]`` to the type name. \ No newline at end of file +- ``list`` maps to the JavaScript :mdn:`Array ` type. You can also specify that a field contains a list of a primitive value type by appending ``[]`` to the type name. +- ``uuid`` is a universally unique identifier from :js-sdk:`Realm.BSON `. + + diff --git a/source/sdk/node/data-types/uuid.txt b/source/sdk/node/data-types/uuid.txt index b0a3a597ab..ef0cfaafd3 100644 --- a/source/sdk/node/data-types/uuid.txt +++ b/source/sdk/node/data-types/uuid.txt @@ -14,4 +14,27 @@ UUID - Node.js SDK .. versionadded:: 10.5.0-beta.1 Overview --------- \ No newline at end of file +-------- + +``UUID`` (Universal Unique Identifier) is a 16-byte :wikipedia:`unique value +`. You can use ``UUID`` as an identifier for +objects. ``UUID`` is :ref:`indexable ` and you can use it as a +:ref:`primary key `. + +.. note:: Using UUID Instead of ObjectId + + In general, you can use ``UUID`` in any field that you need a unique + identifier. Using ``UUID`` might be particularly useful if you are migrating + data not stored in MongoDB since it is likely that your object's unique + identifiers are already of a ``UUID`` type. + +Usage +----- +To define a property as a ``UUID``, set its type to the string ``"uuid"`` in +your :ref:`object model `. Create a {+service-short+} +object within a write transaction. To set any unique identifier properties of +your object to a random value, call ``new UUID()``. Alternatively, set a string +to ``new UUID()`` to set the unique identifier property to a specific value. + +.. literalinclude:: /examples/generated/node/data-types.codeblock.work-with-uuid.js + :language: javascript \ No newline at end of file