-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(DOCSP-14569): mixed data type (#1057)
* New data types node rn (#1040) * new data types for node * added data types to TOC Co-authored-by: Mohammad Hunan Chughtai <mohammad.hunan@mongodb.com> * fixed refs for node.js data types * update doc * Remove accidental changes * added empty test file * attempt to add bluehawked mixed example snippets * fixed wording * Update source/examples/generated/node/data-types.codeblock.query-objects-with-mixed-values.js * Update examples/node/Examples/data-types.js Co-authored-by: Mohammad Hunan Chughtai <mohammad.hunan@mongodb.com>
- Loading branch information
Mohammad Hunan Chughtai
and
Mohammad Hunan Chughtai
committed
May 11, 2021
1 parent
d01c954
commit d8fec46
Showing
13 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import Realm from "realm"; | ||
|
||
describe("Node.js Data Types", () => { | ||
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 | ||
// 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}`); | ||
// :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); | ||
}); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
source/examples/generated/node/data-types.codeblock.create-objects-with-mixed-values.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}); | ||
}); |
7 changes: 7 additions & 0 deletions
7
source/examples/generated/node/data-types.codeblock.define-mixed-in-schema.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const DogSchema = { | ||
name: "Dog", | ||
properties: { | ||
name: "string", | ||
birthDate: "mixed", | ||
}, | ||
}; |
4 changes: 4 additions & 0 deletions
4
source/examples/generated/node/data-types.codeblock.query-objects-with-mixed-values.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// 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}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,32 @@ | ||
.. _node-data-types: | ||
|
||
============================== | ||
Realm Data Types - Node.js SDK | ||
============================== | ||
.. default-domain:: mongodb | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. toctree:: | ||
:titlesonly: | ||
:hidden: | ||
|
||
Field Types </sdk/node/data-types/field-types> | ||
Collections </sdk/node/data-types/collections> | ||
Dictionaries </sdk/node/data-types/dictionaries> | ||
Sets </sdk/node/data-types/sets> | ||
Mixed </sdk/node/data-types/mixed> | ||
UUID </sdk/node/data-types/uuid> | ||
Embedded Objects </sdk/node/data-types/embedded-objects> | ||
|
||
- :doc:`Field Types </sdk/node/data-types/field-types>` | ||
- :doc:`Collections </sdk/node/data-types/collections>` | ||
- :doc:`Dictionaries </sdk/node/data-types/dictionaries>` | ||
- :doc:`Sets </sdk/node/data-types/sets>` | ||
- :doc:`Mixed </sdk/node/data-types/mixed>` | ||
- :doc:`UUID </sdk/node/data-types/uuid>` | ||
- :doc:`Embedded Objects </sdk/node/data-types/embedded-objects>` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.. _node-data-types-collections: | ||
|
||
========================= | ||
Collections - Node.js SDK | ||
========================= | ||
|
||
.. default-domain:: mongodb | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
Overview | ||
-------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.. _node-data-types-dictionaries: | ||
|
||
========================== | ||
Dictionaries - Node.js SDK | ||
========================== | ||
|
||
.. default-domain:: mongodb | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. versionadded:: 10.5.0-beta.1 | ||
|
||
Overview | ||
-------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.. _node-data-types-embedded-objects: | ||
|
||
============================== | ||
Embedded Objects - Node.js SDK | ||
============================== | ||
|
||
.. default-domain:: mongodb | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.. _node-data-types-field-types: | ||
|
||
========================= | ||
Field Types - Node.js SDK | ||
========================= | ||
|
||
.. default-domain:: mongodb | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
|
||
{+client-database+} supports the following field data types: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
.. _node-data-types-mixed: | ||
|
||
=================== | ||
Mixed - Node.js SDK | ||
=================== | ||
.. default-domain:: mongodb | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. versionadded:: 10.5.0-beta.1 | ||
|
||
Overview | ||
-------- | ||
The mixed data type is a {+realm+} property type that 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:`set a property of your object model | ||
<node-define-a-realm-object-schema>` 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() | ||
<Realm.html#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() <Realm.Collection.html#filtered>` method and | ||
passing in a :ref:`filter <node-filter-queries>` 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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.. _node-data-types-sets: | ||
|
||
================== | ||
Sets - Node.js SDK | ||
================== | ||
|
||
.. default-domain:: mongodb | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. versionadded:: 10.5.0-beta.1 | ||
|
||
Overview | ||
-------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.. _node-data-types-uuid: | ||
|
||
================== | ||
UUID - Node.js SDK | ||
================== | ||
.. default-domain:: mongodb | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. versionadded:: 10.5.0-beta.1 | ||
|
||
Overview | ||
-------- |