Skip to content

Commit 2475974

Browse files
Mohammad Hunan ChughtaiMohammad Hunan Chughtai
Mohammad Hunan Chughtai
and
Mohammad Hunan Chughtai
authored
Docsp 14569 mixed data type (#1064)
* 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 b79ffbc commit 2475974

File tree

5 files changed

+138
-1
lines changed

5 files changed

+138
-1
lines changed

examples/node/Examples/data-types.js

+58
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,64 @@ const BusinessSchema = {
3434
};
3535
// :code-block-end:
3636
describe("Node.js Data Types", () => {
37+
test("should work with Mixed Type", async () => {
38+
// :code-block-start: define-mixed-in-schema
39+
const DogSchema = {
40+
name: "Dog",
41+
properties: {
42+
name: "string",
43+
birthDate: "mixed",
44+
},
45+
};
46+
// :code-block-end:
47+
48+
const realm = await Realm.open({
49+
schema: [DogSchema],
50+
});
51+
52+
// :code-block-start: create-objects-with-mixed-values
53+
realm.write(() => {
54+
// create a Dog with a birthDate value of type string
55+
realm.create("Dog", { name: "Euler", birthDate: "December 25th, 2017" });
56+
57+
// create a Dog with a birthDate value of type date
58+
realm.create("Dog", {
59+
name: "Blaise",
60+
birthDate: new Date("August 17, 2020"),
61+
});
62+
// create a Dog with a birthDate value of type int
63+
realm.create("Dog", {
64+
name: "Euclid",
65+
birthDate: 10152021,
66+
});
67+
// create a Dog with a birthDate value of type null
68+
realm.create("Dog", {
69+
name: "Pythagoras",
70+
birthDate: null,
71+
});
72+
});
73+
// :code-block-end:
74+
75+
// :code-block-start: query-objects-with-mixed-values
76+
// To query for Blaise's birthDate, filter for his name to retrieve the realm object.
77+
// Use dot notation to access the birthDate property.
78+
let blaiseBirthDate = realm.objects("Dog").filtered(`name = 'Blaise'`)[0]
79+
.birthDate;
80+
console.log(`Blaise's birth date is ${blaiseBirthDate}`);
81+
// :code-block-end:
82+
expect(blaiseBirthDate).toEqual(new Date("August 17, 2020"));
83+
84+
// delete the objects specifically created in this test to keep tests idempotent
85+
const Euler = realm.objects("Dog").filtered(`name = 'Euler'`)[0];
86+
const Blaise = realm.objects("Dog").filtered(`name = 'Blaise'`)[0];
87+
const Euclid = realm.objects("Dog").filtered(`name = 'Euclid'`)[0];
88+
const Pythagoras = realm.objects("Dog").filtered(`name = 'Pythagoras'`)[0];
89+
realm.write(() => {
90+
realm.delete(Euler);
91+
realm.delete(Blaise);
92+
realm.delete(Euclid);
93+
realm.delete(Pythagoras);
94+
});
3795
test("should create and read and delete an embedded object", async () => {
3896
const realm = await Realm.open({
3997
schema: [AddressSchema, ContactSchema],
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/data-types/mixed.txt

+49-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,52 @@ Mixed - Node.js SDK
1414
.. versionadded:: 10.5.0-beta.1
1515

1616
Overview
17-
--------
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+

0 commit comments

Comments
 (0)