Skip to content

Commit d8fec46

Browse files
Mohammad Hunan ChughtaiMohammad Hunan Chughtai
Mohammad Hunan Chughtai
and
Mohammad Hunan Chughtai
authored
(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 <[email protected]> * 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 <[email protected]>
1 parent d01c954 commit d8fec46

13 files changed

+287
-0
lines changed

examples/node/Examples/data-types.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import Realm from "realm";
2+
3+
describe("Node.js Data Types", () => {
4+
test("should work with Mixed Type", async () => {
5+
// :code-block-start: define-mixed-in-schema
6+
const DogSchema = {
7+
name: "Dog",
8+
properties: {
9+
name: "string",
10+
birthDate: "mixed",
11+
},
12+
};
13+
// :code-block-end:
14+
15+
const realm = await Realm.open({
16+
schema: [DogSchema],
17+
});
18+
19+
// :code-block-start: create-objects-with-mixed-values
20+
realm.write(() => {
21+
// create a Dog with a birthDate value of type string
22+
realm.create("Dog", { name: "Euler", birthDate: "December 25th, 2017" });
23+
24+
// create a Dog with a birthDate value of type date
25+
realm.create("Dog", {
26+
name: "Blaise",
27+
birthDate: new Date("August 17, 2020"),
28+
});
29+
// create a Dog with a birthDate value of type int
30+
realm.create("Dog", {
31+
name: "Euclid",
32+
birthDate: 10152021,
33+
});
34+
// create a Dog with a birthDate value of type null
35+
realm.create("Dog", {
36+
name: "Pythagoras",
37+
birthDate: null,
38+
});
39+
});
40+
// :code-block-end:
41+
42+
// :code-block-start: query-objects-with-mixed-values
43+
// To query for Blaise's birthDate, filter for his name to retrieve the realm object.
44+
// Use dot notation to access the birthDate property.
45+
let blaiseBirthDate = realm.objects("Dog").filtered(`name = 'Blaise'`)[0]
46+
.birthDate;
47+
console.log(`Blaise's birth date is ${blaiseBirthDate}`);
48+
// :code-block-end:
49+
expect(blaiseBirthDate).toEqual(new Date("August 17, 2020"));
50+
51+
// delete the objects specifically created in this test to keep tests idempotent
52+
const Euler = realm.objects("Dog").filtered(`name = 'Euler'`)[0];
53+
const Blaise = realm.objects("Dog").filtered(`name = 'Blaise'`)[0];
54+
const Euclid = realm.objects("Dog").filtered(`name = 'Euclid'`)[0];
55+
const Pythagoras = realm.objects("Dog").filtered(`name = 'Pythagoras'`)[0];
56+
realm.write(() => {
57+
realm.delete(Euler);
58+
realm.delete(Blaise);
59+
realm.delete(Euclid);
60+
realm.delete(Pythagoras);
61+
});
62+
});
63+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
realm.write(() => {
2+
// create a Dog with a birthDate value of type string
3+
realm.create("Dog", { name: "Euler", birthDate: "December 25th, 2017" });
4+
5+
// create a Dog with a birthDate value of type date
6+
realm.create("Dog", {
7+
name: "Blaise",
8+
birthDate: new Date("August 17, 2020"),
9+
});
10+
// create a Dog with a birthDate value of type int
11+
realm.create("Dog", {
12+
name: "Euclid",
13+
birthDate: 10152021,
14+
});
15+
// create a Dog with a birthDate value of type null
16+
realm.create("Dog", {
17+
name: "Pythagoras",
18+
birthDate: null,
19+
});
20+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const DogSchema = {
2+
name: "Dog",
3+
properties: {
4+
name: "string",
5+
birthDate: "mixed",
6+
},
7+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// To query for Blaise's birthDate, filter for his name to retrieve the realm object.
2+
// Use dot notation to access the birthDate property.
3+
let blaiseBirthDate = realm.objects("Dog").filtered(`name = 'Blaise'`)[0].birthDate;
4+
console.log(`Blaise's birth date is ${blaiseBirthDate}`);

source/sdk/node.txt

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ MongoDB Realm Node.js SDK
1212
Quick Start </sdk/node/quick-start-local>
1313
Quick Start with Sync </sdk/node/quick-start>
1414
Fundamentals </sdk/node/fundamentals>
15+
Data Types </sdk/node/data-types>
1516
Usage Examples </sdk/node/examples>
1617
Integration Guides </sdk/node/integrations>
1718
Advanced Guides </sdk/node/advanced>

source/sdk/node/data-types.txt

+29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
.. _node-data-types:
2+
13
==============================
24
Realm Data Types - Node.js SDK
35
==============================
6+
.. default-domain:: mongodb
7+
8+
.. contents:: On this page
9+
:local:
10+
:backlinks: none
11+
:depth: 2
12+
:class: singlecol
13+
14+
.. toctree::
15+
:titlesonly:
16+
:hidden:
17+
18+
Field Types </sdk/node/data-types/field-types>
19+
Collections </sdk/node/data-types/collections>
20+
Dictionaries </sdk/node/data-types/dictionaries>
21+
Sets </sdk/node/data-types/sets>
22+
Mixed </sdk/node/data-types/mixed>
23+
UUID </sdk/node/data-types/uuid>
24+
Embedded Objects </sdk/node/data-types/embedded-objects>
25+
26+
- :doc:`Field Types </sdk/node/data-types/field-types>`
27+
- :doc:`Collections </sdk/node/data-types/collections>`
28+
- :doc:`Dictionaries </sdk/node/data-types/dictionaries>`
29+
- :doc:`Sets </sdk/node/data-types/sets>`
30+
- :doc:`Mixed </sdk/node/data-types/mixed>`
31+
- :doc:`UUID </sdk/node/data-types/uuid>`
32+
- :doc:`Embedded Objects </sdk/node/data-types/embedded-objects>`
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.. _node-data-types-collections:
2+
3+
=========================
4+
Collections - Node.js SDK
5+
=========================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
15+
Overview
16+
--------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.. _node-data-types-dictionaries:
2+
3+
==========================
4+
Dictionaries - Node.js SDK
5+
==========================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
15+
.. versionadded:: 10.5.0-beta.1
16+
17+
Overview
18+
--------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.. _node-data-types-embedded-objects:
2+
3+
==============================
4+
Embedded Objects - Node.js SDK
5+
==============================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.. _node-data-types-field-types:
2+
3+
=========================
4+
Field Types - Node.js SDK
5+
=========================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
15+
16+
{+client-database+} supports the following field data types:

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

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
.. _node-data-types-mixed:
2+
3+
===================
4+
Mixed - Node.js SDK
5+
===================
6+
.. default-domain:: mongodb
7+
8+
.. contents:: On this page
9+
:local:
10+
:backlinks: none
11+
:depth: 2
12+
:class: singlecol
13+
14+
.. versionadded:: 10.5.0-beta.1
15+
16+
Overview
17+
--------
18+
The mixed data type is a {+realm+} property type that can hold different data types.
19+
Supported data types include:
20+
21+
- bool
22+
- int
23+
- float
24+
- double
25+
- string
26+
- Date
27+
- Data
28+
- UUID
29+
- Set
30+
- null
31+
32+
.. note::
33+
34+
The mixed data type is indexable, but you can't use it as a primary key.
35+
Because null is a permitted value, you can't declare a Mixed property as
36+
optional.
37+
38+
Realm Object Models
39+
-------------------
40+
To :ref:`set a property of your object model
41+
<node-define-a-realm-object-schema>` as mixed, set the property's type to
42+
``"mixed"``.
43+
44+
.. literalinclude:: /examples/generated/node/data-types.codeblock.define-mixed-in-schema.js
45+
:language: javascript
46+
47+
Create an Object With a Mixed Value
48+
-----------------------------------
49+
Create an object with a mixed value by running the :js-sdk:`realm.create()
50+
<Realm.html#create>` method within a write transaction.
51+
52+
.. literalinclude:: /examples/generated/node/data-types.codeblock.create-objects-with-mixed-values.js
53+
:language: javascript
54+
55+
Query for Objects with a Mixed Value
56+
------------------------------------
57+
Query for objects with a mixed value by running the
58+
:js-sdk:`Collection.filtered() <Realm.Collection.html#filtered>` method and
59+
passing in a :ref:`filter <node-filter-queries>` for a non-mixed field. You can
60+
then print the value of the mixed property or the entire object itself.
61+
62+
.. literalinclude:: /examples/generated/node/data-types.codeblock.query-objects-with-mixed-values.js
63+
:language: javascript
64+
65+

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

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.. _node-data-types-sets:
2+
3+
==================
4+
Sets - Node.js SDK
5+
==================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
15+
.. versionadded:: 10.5.0-beta.1
16+
17+
Overview
18+
--------

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

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.. _node-data-types-uuid:
2+
3+
==================
4+
UUID - Node.js SDK
5+
==================
6+
.. default-domain:: mongodb
7+
8+
.. contents:: On this page
9+
:local:
10+
:backlinks: none
11+
:depth: 2
12+
:class: singlecol
13+
14+
.. versionadded:: 10.5.0-beta.1
15+
16+
Overview
17+
--------

0 commit comments

Comments
 (0)