Skip to content

Commit f43ae61

Browse files
Mohammad Hunan ChughtaiMohammad Hunan Chughtaidacharycknethnathan-contino-mongo
authored
Node and React Native Data Types (#1065)
* 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 * (DOCSP-15613): Added Node.js field types (#1041) * Added node.js field types * fixed wording * fixed typo * fix typo in mdn urls * added additional data types * fix monospace err Co-authored-by: Mohammad Hunan Chughtai <[email protected]> * (DOCSP-15613): Node.js collections type (#1044) * Add content to Node.js > Data Types > Collections.txt * Removed unneeded spacing * clean up collections page + add headers * Filled out collections page * fix refs * fix typo * Update source/sdk/node/data-types/collections.txt * Update source/sdk/node/data-types/collections.txt * Update source/sdk/node/data-types/collections.txt Co-authored-by: Dachary <[email protected]> * fix rst * fix woridng * added ref * removed unneeded word * removed unneeded word * added period Co-authored-by: Mohammad Hunan Chughtai <[email protected]> Co-authored-by: Dachary <[email protected]> * (DOCSP-15613): Node.js embedded objects type (#1047) * Added documentation of Node.js embedded objects under data types * clean up relationships and embedded objects * added CRUD examples for embedded obj * added bluehawked examples * fix rst syntax highlight + add description for deletes * Update source/sdk/node/data-types/embedded-objects.txt * Update source/sdk/node/data-types/embedded-objects.txt * Update source/sdk/node/data-types/embedded-objects.txt * Update source/sdk/node/data-types/embedded-objects.txt * Update source/sdk/node/data-types/embedded-objects.txt Co-authored-by: Mohammad Hunan Chughtai <[email protected]> * 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]> * (DOCSP-14565): Dictionary data type (#1058) * (DOCSP-14565): Dictionary Data Type - Node.js * added unit tested + bluehawked dictionary examples * Update examples/node/package.json * removed unneeded file * Added new examples for dictionaries * update dictionary examples * fix capitalization * moved addlistener down * fix comment * update wording to be clearer on type usage in dict * fix wording * fix wording Co-authored-by: Mohammad Hunan Chughtai <[email protected]> * (DOCSP-14577): UUID (#1067) * bump realmjs to 10.5.0-beta-1 * removed uuid as it's own page and added a subsection on it * added uuid bluehawked snippet + readded uuid to toc * added uuid examples * removed uuid from field types as a paragraph * update wording * update wording * fix passive voice * add specific uuid example * removed innacurate collections are homogenous (per mixed) * (DOCSP-14573): set data type (#1079) * added set examples + bluehawked * literal included set exampkes * added delete one set item example * fix typo * added descriptions to set * fix grammar * changed link + hunter to generic names * added capitalization to clearly define Realm Set as a term * fix wording + formatting" * clarified wording * fix literalincldue indentation * fix typo * more wording fixes * clean up realm object model for set wording * fix character names for examples * clarified traversal order wording * rst typo in <set> * changed set to mySet * fix overview wording * updated overview to be more clear on differences between array * updated create wording * Update source/examples/generated/node/data-types.codeblock.remove-all-items-from-set.js Co-authored-by: Kenneth Geisshirt <[email protected]> * Update source/examples/generated/node/data-types.codeblock.remove-specific-item-from-set.js Co-authored-by: Kenneth Geisshirt <[email protected]> * Update source/sdk/node/data-types/sets.txt Co-authored-by: Kenneth Geisshirt <[email protected]> * fix grammar + wording + changed Set to Realm.Set in js snippets * Fix woridng Co-authored-by: Kenneth Geisshirt <[email protected]> * fill out field types * add react native data types as a copy of node data types * add data types to toc * fix rn mixed links * attempt to add realm-js links * more grammar and woridng fixes * wording fixes * Update source/sdk/node/data-types/uuid.txt Co-authored-by: nate contino <[email protected]> * change uuid note on rn to match node * added note about obj id to both rn and node * Update source/sdk/node/data-types/mixed.txt Co-authored-by: nate contino <[email protected]> * fix 'mixed' formatting on rn to match node * change monospace to bold * fix spacing errors * fix wording * correct supported types for mixed * wording update Co-authored-by: Mohammad Hunan Chughtai <[email protected]> Co-authored-by: Dachary <[email protected]> Co-authored-by: Kenneth Geisshirt <[email protected]> Co-authored-by: nate contino <[email protected]>
1 parent 4e9f44a commit f43ae61

File tree

46 files changed

+2255
-310
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2255
-310
lines changed

examples/node/Examples/data-types.js

Lines changed: 431 additions & 0 deletions
Large diffs are not rendered by default.

examples/node/package-lock.json

Lines changed: 355 additions & 250 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"jest": "^26.5.3",
2626
"prettier": "^2.1.2",
2727
"random-email": "^1.0.3",
28-
"realm": "^10.0.1",
28+
"realm": "^10.5.0-beta.1",
2929
"ts-jest": "^26.4.1",
3030
"typescript": "^4.0.3"
3131
},
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
summerHillHouse.addListener((changedHouse, changes) => {
2+
console.log("A change has occurred to the Summer Hill House object");
3+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
realm.write(() => {
2+
characterOne.inventory.add("hammer");
3+
characterOne.levelsCompleted.add(32);
4+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// check if the characterTwo has completed level 3 by calling the `Realm.Set.has()` method
2+
const characterTwoHasCompletedLevelThree = characterTwo.levelsCompleted.has(3);
3+
console.log(
4+
`Is level three completed by the characterTwo: ${characterTwoHasCompletedLevelThree}`
5+
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// check how many items the characterTwo has in his inventory through the `Realm.Set.size` property
2+
const characterTwoInventorySize = characterTwo.inventory.size;
3+
console.log(`The characterTwo has ${characterTwoInventorySize} inventory items`);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// create an embedded address object
2+
const sydneyOrthodontics = {
3+
street: "42 Wallaby Way",
4+
city: "Sydney",
5+
country: "Australia",
6+
postalCode: "2774",
7+
};
8+
realm.write(() => {
9+
// create a contact object
10+
realm.create("Contact", {
11+
_id: new BSON.ObjectId(),
12+
name: "Philip Sherman",
13+
address: sydneyOrthodontics, // embed the address in the contact object
14+
});
15+
});
Lines changed: 20 additions & 0 deletions
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+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
let johnDoe;
2+
let janeSmith;
3+
realm.write(() => {
4+
johnDoe = realm.create("Person", {
5+
name: "John Doe",
6+
home: {
7+
windows: 5,
8+
doors: 3,
9+
color: "red",
10+
address: "Summerhill St.",
11+
price: 400123,
12+
},
13+
});
14+
janeSmith = realm.create("Person", {
15+
name: "Jane Smith",
16+
home: {
17+
address: "100 northroad st.",
18+
yearBuilt: 1990,
19+
},
20+
});
21+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let characterOne, characterTwo;
2+
realm.write(() => {
3+
characterOne = realm.create("Character", {
4+
_id: new BSON.ObjectId(),
5+
name: "CharacterOne",
6+
inventory: ["elixir", "compass", "glowing shield"],
7+
levelsCompleted: [4, 9],
8+
});
9+
characterTwo = realm.create("Character", {
10+
_id: new BSON.ObjectId(),
11+
name: "CharacterTwo",
12+
inventory: ["estus flask", "gloves", "rune"],
13+
levelsCompleted: [1, 2, 5, 24],
14+
});
15+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const PersonSchema = {
2+
name: "Person",
3+
properties: {
4+
name: "string",
5+
home: "{}",
6+
},
7+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const AddressSchema = {
2+
name: "Address",
3+
embedded: true, // default: false
4+
properties: {
5+
street: "string?",
6+
city: "string?",
7+
country: "string?",
8+
postalCode: "string?",
9+
},
10+
};
11+
12+
const ContactSchema = {
13+
name: "Contact",
14+
primaryKey: "_id",
15+
properties: {
16+
_id: "objectId",
17+
name: "string",
18+
address: "Address", // Embed a single object
19+
},
20+
};
21+
22+
const BusinessSchema = {
23+
name: "Business",
24+
primaryKey: "_id",
25+
properties: {
26+
_id: "objectId",
27+
name: "string",
28+
addresses: { type: "list", objectType: "Address" }, // Embed an array of objects
29+
},
30+
};
Lines changed: 7 additions & 0 deletions
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+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const characterSchema = {
2+
name: "Character",
3+
primaryKey: "_id",
4+
properties: {
5+
_id: "objectId",
6+
name: "string",
7+
levelsCompleted: "int<>",
8+
inventory: "string<>",
9+
},
10+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
realm.write(() => {
2+
// Deleting the contact will delete the embedded address of that contact
3+
realm.delete(
4+
realm.objects("Contact").filtered("name = 'Philip Sherman'")
5+
);
6+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// create a new address
2+
const harryNewAddress = {
3+
street: "12 Grimmauld Place",
4+
city: "London",
5+
country: "UK",
6+
postalCode: "E1 7AA",
7+
};
8+
realm.write(() => {
9+
// overwrite the embedded object with the new address within a write transaction
10+
harryPotter.address = harryNewAddress;
11+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// query for all Person objects
2+
const persons = realm.objects("Person");
3+
4+
// run the `.filtered()` method on all the returned persons to find the house with the address "Summerhill St."
5+
const summerHillHouse = persons.filtered(
6+
`home['address'] = "Summerhill St."`
7+
)[0].home;
8+
9+
// Find all people that have a house with a listed price
10+
const peopleWithHousesWithAListedPrice = persons.filtered(
11+
`home.@keys = "price" `
12+
);
13+
// find a house that has any field with a value of 'red'
14+
const redHouse = persons.filtered(`home.@values = "red" `)[0].home;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const philipShermanAddress = realm
2+
.objects("Contact")
3+
.filtered("name = 'Philip Sherman'")[0].address.street;
4+
console.log(`Philip Sherman's address is ${philipShermanAddress}`);
Lines changed: 4 additions & 0 deletions
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}`);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
realm.write(() => {
2+
// clear all data from the inventory slot of the characterTwo by calling `Realm.Set.clear()` in a write transaction
3+
characterTwo.inventory.clear();
4+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
realm.write(() => {
2+
// remove the 'windows' and 'doors' field of the Summerhill House.
3+
summerHillHouse.remove(["windows", "doors"]);
4+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
realm.write(() => {
2+
// remove the compass from characterOne's inventory by calling `Realm.Set.delete()` within a write transaction
3+
characterOne.inventory.delete("compass");
4+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let newVictorianHome;
2+
realm.write(() => {
3+
newVictorianHome = {
4+
doors: 4,
5+
floor: 3,
6+
color: "white",
7+
address: "Trailwoods Rd.",
8+
};
9+
// use the `put()` method to add a dictionary to a pre-existing city in the database
10+
summerHillHouse.home.put(newVictorianHome);
11+
12+
// alternatively, use dot notation to add a dictionary to a pre-existing city
13+
yakimaCity.home = newVictorianHome;
14+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
realm.write(() => {
2+
// use the `put()` method to update a field of a dictionary
3+
summerHillHouse.put({ price: 400100 });
4+
// alternatively, update a field of a dictionary through dot notation
5+
summerHillHouse.color = "brown";
6+
// update a dictionary by adding a field
7+
summerHillHouse.yearBuilt = 2004;
8+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Find the contact with the address you want to update
2+
const harryPotter = realm
3+
.objects("Contact")
4+
.filtered("name = 'Harry Potter'")[0];
5+
// modify the property of the embedded object in a write transaction
6+
realm.write(() => {
7+
// update the embedded object directly through the contact
8+
harryPotter.address.street = "1 Hogwarts Ave";
9+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { UUID } = Realm.BSON;
2+
const ProfileSchema = {
3+
name: "Profile",
4+
primaryKey: "_id",
5+
properties: {
6+
_id: "uuid",
7+
name: "string",
8+
},
9+
};
10+
const realm = await Realm.open({
11+
schema: [ProfileSchema],
12+
});
13+
realm.write(() => {
14+
realm.create("Profile", {
15+
name: "John Doe.",
16+
_id: new UUID(), // create a _id with a randomly generated UUID
17+
});
18+
realm.create("Profile", {
19+
name: "Tim Doe.",
20+
_id: new UUID("882dd631-bc6e-4e0e-a9e8-f07b685fec8c"), // create a _id with a specific UUID value
21+
});
22+
});

source/sdk/node.txt

Lines changed: 1 addition & 0 deletions
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

Lines changed: 29 additions & 0 deletions
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>`

0 commit comments

Comments
 (0)