From f3ae7813e80d71de41534559e0dfa7fc2cba1f74 Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Mon, 10 May 2021 08:13:46 -0400 Subject: [PATCH 1/4] attempt to add bluehawked mixed example snippets --- examples/node/Examples/data-types.js | 60 ++++++++++++++++++- ...eblock.create-objects-with-mixed-values.js | 20 +++++++ ...-types.codeblock.define-mixed-in-schema.js | 7 +++ ...deblock.query-objects-with-mixed-values.js | 3 + source/sdk/node/data-types/mixed.txt | 50 +++++++++++++++- 5 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 source/examples/generated/node/data-types.codeblock.create-objects-with-mixed-values.js create mode 100644 source/examples/generated/node/data-types.codeblock.define-mixed-in-schema.js create mode 100644 source/examples/generated/node/data-types.codeblock.query-objects-with-mixed-values.js diff --git a/examples/node/Examples/data-types.js b/examples/node/Examples/data-types.js index dd899119fe..4fd41c4bbf 100644 --- a/examples/node/Examples/data-types.js +++ b/examples/node/Examples/data-types.js @@ -1,4 +1,62 @@ import Realm from "realm"; describe("Node.js Data Types", () => { -}) \ No newline at end of file + test("should work with Mixed Type", async () => { + // :code-block-start: define-mixed-in-schema + const DogSchema = { + name: "Dog", + properties: { + name: "string", + birthDate: "mixed", + }, + }; + // :code-block-end: + + const realm = await Realm.open({ + schema: [DogSchema], + }); + + // :code-block-start: create-objects-with-mixed-values + realm.write(() => { + // create a Dog with a birthDate value of type string + realm.create("Dog", { name: "Euler", birthDate: "December 25th, 2017" }); + + // create a Dog with a birthDate value of type date + realm.create("Dog", { + name: "Blaise", + birthDate: new Date("August 17, 2020"), + }); + // create a Dog with a birthDate value of type int + realm.create("Dog", { + name: "Euclid", + birthDate: 10152021, + }); + // create a Dog with a birthDate value of type null + realm.create("Dog", { + name: "Pythagoras", + birthDate: null, + }); + }); + // :code-block-end: + + // :code-block-start: query-objects-with-mixed-values + // query for Blaise's birthDate by filtering for his name to get the entire Realm object and using dot notation to get the birthDate + let blaiseBirthDate = realm.objects("Dog").filtered(`name = 'Blaise'`)[0] + .birthDate; + console.log(`Blaise's birth date is ${blaiseBirthDate}`); + // :code-block-end: + expect(blaiseBirthDate).toEqual(new Date("August 17, 2020")); + + // delete the objects specifically created in this test to keep tests idempotent + const Euler = realm.objects("Dog").filtered(`name = 'Euler'`)[0]; + 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]; + realm.write(() => { + realm.delete(Euler); + realm.delete(Blaise); + realm.delete(Euclid); + realm.delete(Pythagoras); + }); + }); +}); diff --git a/source/examples/generated/node/data-types.codeblock.create-objects-with-mixed-values.js b/source/examples/generated/node/data-types.codeblock.create-objects-with-mixed-values.js new file mode 100644 index 0000000000..ab8b0517f0 --- /dev/null +++ b/source/examples/generated/node/data-types.codeblock.create-objects-with-mixed-values.js @@ -0,0 +1,20 @@ +realm.write(() => { + // create a Dog with a birthDate value of type string + realm.create("Dog", { name: "Euler", birthDate: "December 25th, 2017" }); + + // create a Dog with a birthDate value of type date + realm.create("Dog", { + name: "Blaise", + birthDate: new Date("August 17, 2020"), + }); + // create a Dog with a birthDate value of type int + realm.create("Dog", { + name: "Euclid", + birthDate: 10152021, + }); + // create a Dog with a birthDate value of type null + realm.create("Dog", { + name: "Pythagoras", + birthDate: null, + }); +}); diff --git a/source/examples/generated/node/data-types.codeblock.define-mixed-in-schema.js b/source/examples/generated/node/data-types.codeblock.define-mixed-in-schema.js new file mode 100644 index 0000000000..50f8ee7229 --- /dev/null +++ b/source/examples/generated/node/data-types.codeblock.define-mixed-in-schema.js @@ -0,0 +1,7 @@ +const DogSchema = { + name: "Dog", + properties: { + name: "string", + birthDate: "mixed", + }, +}; diff --git a/source/examples/generated/node/data-types.codeblock.query-objects-with-mixed-values.js b/source/examples/generated/node/data-types.codeblock.query-objects-with-mixed-values.js new file mode 100644 index 0000000000..b21e4dda8b --- /dev/null +++ b/source/examples/generated/node/data-types.codeblock.query-objects-with-mixed-values.js @@ -0,0 +1,3 @@ +// query for Blaise's birthDate by filtering for his name to get the entire Realm object and using dot notation to get the birthDate +let blaiseBirthDate = realm.objects("Dog").filtered(`name = 'Blaise'`)[0].birthDate; +console.log(`Blaise's birth date is ${blaiseBirthDate}`); diff --git a/source/sdk/node/data-types/mixed.txt b/source/sdk/node/data-types/mixed.txt index 06a5d9b3fa..68a39fcc58 100644 --- a/source/sdk/node/data-types/mixed.txt +++ b/source/sdk/node/data-types/mixed.txt @@ -14,4 +14,52 @@ Mixed - Node.js SDK .. versionadded:: 10.5.0-beta.1 Overview --------- \ No newline at end of file +-------- +The mixed data type is a {+realm+} property type can hold different data types. +Supported data types include: + +- bool +- int +- float +- double +- string +- Date +- Data +- UUID +- Set +- null + +.. note:: + + The mixed data type is indexable, but you can't use it as a primary key. + Because null is a permitted value, you can't declare a Mixed property as + optional. + +Realm Object Models +------------------- +To :ref:`define a property of your object model +` as mixed, set the property's type to +``"mixed"``. + +.. literalinclude:: /examples/generated/node/data-types.codeblock.define-mixed-in-schema.js + :language: javascript + +Create an Object With a Mixed Value +----------------------------------- +Create an object with a mixed value by running the :js-sdk:`realm.create() +` method within a write transaction. + +.. literalinclude:: /examples/generated/node/data-types.codeblock.create-objects-with-mixed-values.js + :language: javascript + +Query for Objects with a Mixed Value +------------------------------------ +Query for objects with a mixed value by running the +:js-sdk:`Collection.filtered() ` method and +passing in a :ref:`filter ` for a non-mixed field. You can +then print the value of the mixed property or the entire object itself. + +.. literalinclude:: /examples/generated/node/data-types.codeblock.query-objects-with-mixed-values.js + :language: javascript + + From 825a32625ecae9eac006d851ce4f35cdae124aaf Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Mon, 10 May 2021 08:19:38 -0400 Subject: [PATCH 2/4] fixed wording --- source/sdk/node/data-types/mixed.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/sdk/node/data-types/mixed.txt b/source/sdk/node/data-types/mixed.txt index 68a39fcc58..4b3e280228 100644 --- a/source/sdk/node/data-types/mixed.txt +++ b/source/sdk/node/data-types/mixed.txt @@ -15,7 +15,7 @@ Mixed - Node.js SDK Overview -------- -The mixed data type is a {+realm+} property type can hold different data types. +The mixed data type is a {+realm+} property type that can hold different data types. Supported data types include: - bool @@ -37,7 +37,7 @@ Supported data types include: Realm Object Models ------------------- -To :ref:`define a property of your object model +To :ref:`set a property of your object model ` as mixed, set the property's type to ``"mixed"``. From e9c17120998b7fe985235ae3c61da600ad0b4238 Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Tue, 11 May 2021 11:22:42 -0400 Subject: [PATCH 3/4] Update source/examples/generated/node/data-types.codeblock.query-objects-with-mixed-values.js --- .../data-types.codeblock.query-objects-with-mixed-values.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/examples/generated/node/data-types.codeblock.query-objects-with-mixed-values.js b/source/examples/generated/node/data-types.codeblock.query-objects-with-mixed-values.js index b21e4dda8b..cd3ea34e2f 100644 --- a/source/examples/generated/node/data-types.codeblock.query-objects-with-mixed-values.js +++ b/source/examples/generated/node/data-types.codeblock.query-objects-with-mixed-values.js @@ -1,3 +1,4 @@ -// query for Blaise's birthDate by filtering for his name to get the entire Realm object and using dot notation to get the birthDate +// To query for Blaise's birthDate, filter for his name to retrieve the realm object. +// Use dot notation to access the birthDate property. let blaiseBirthDate = realm.objects("Dog").filtered(`name = 'Blaise'`)[0].birthDate; console.log(`Blaise's birth date is ${blaiseBirthDate}`); From 5e75e84e5142246dd67e79b9f29e376e9b2dc8ac Mon Sep 17 00:00:00 2001 From: Mohammad Hunan Chughtai Date: Tue, 11 May 2021 11:22:47 -0400 Subject: [PATCH 4/4] Update examples/node/Examples/data-types.js --- examples/node/Examples/data-types.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/node/Examples/data-types.js b/examples/node/Examples/data-types.js index 4fd41c4bbf..ab3803d9a7 100644 --- a/examples/node/Examples/data-types.js +++ b/examples/node/Examples/data-types.js @@ -40,7 +40,8 @@ describe("Node.js Data Types", () => { // :code-block-end: // :code-block-start: query-objects-with-mixed-values - // query for Blaise's birthDate by filtering for his name to get the entire Realm object and using dot notation to get the birthDate + // To query for Blaise's birthDate, filter for his name to retrieve the realm object. + // Use dot notation to access the birthDate property. let blaiseBirthDate = realm.objects("Dog").filtered(`name = 'Blaise'`)[0] .birthDate; console.log(`Blaise's birth date is ${blaiseBirthDate}`);