Skip to content

(DOCSP-14577): UUID #1067

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

41 changes: 41 additions & 0 deletions examples/node/Examples/data-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
});
});
Original file line number Diff line number Diff line change
@@ -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
});
});
3 changes: 1 addition & 2 deletions source/sdk/node/data-types/collections.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
<node-object-types>`. {+service-short+} collections are **homogenous**:
all objects in a collection are of the same type.
<node-object-types>`.

You can filter and sort any collection using {+client-database+}'s
:ref:`query engine <node-client-query-engine>`. Collections are
Expand Down
5 changes: 4 additions & 1 deletion source/sdk/node/data-types/field-types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ Field Types -Node.js SDK
- ``objectId`` maps to BSON :manual:`ObjectId </reference/method/ObjectId/>` type.
- ``data`` maps to the JavaScript :mdn:`ArrayBuffer <Web/JavaScript/Reference/Global_Objects/ArrayBuffer>` type.
- ``date`` maps to the JavaScript :mdn:`Date <Web/JavaScript/Reference/Global_Objects/Date>` type.
- ``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.
- ``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.
- ``uuid`` is a universally unique identifier from :js-sdk:`Realm.BSON <Realm.html#.BSON>`.


25 changes: 24 additions & 1 deletion source/sdk/node/data-types/uuid.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,27 @@ UUID - Node.js SDK
.. versionadded:: 10.5.0-beta.1

Overview
--------
--------

``UUID`` (Universal Unique Identifier) is a 16-byte :wikipedia:`unique value
<Universally_unique_identifier>`. You can use ``UUID`` as an identifier for
objects. ``UUID`` is :ref:`indexable <node-indexes>` and you can use it as a
:ref:`primary key <node-primary-keys>`.

.. 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 <node-object-schemas>`. 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