Skip to content

Commit

Permalink
Node and React Native Data Types (#1065)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
5 people authored May 19, 2021
1 parent 4e9f44a commit f43ae61
Show file tree
Hide file tree
Showing 46 changed files with 2,255 additions and 310 deletions.
431 changes: 431 additions & 0 deletions examples/node/Examples/data-types.js

Large diffs are not rendered by default.

605 changes: 355 additions & 250 deletions examples/node/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"jest": "^26.5.3",
"prettier": "^2.1.2",
"random-email": "^1.0.3",
"realm": "^10.0.1",
"realm": "^10.5.0-beta.1",
"ts-jest": "^26.4.1",
"typescript": "^4.0.3"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
summerHillHouse.addListener((changedHouse, changes) => {
console.log("A change has occurred to the Summer Hill House object");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
realm.write(() => {
characterOne.inventory.add("hammer");
characterOne.levelsCompleted.add(32);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// check if the characterTwo has completed level 3 by calling the `Realm.Set.has()` method
const characterTwoHasCompletedLevelThree = characterTwo.levelsCompleted.has(3);
console.log(
`Is level three completed by the characterTwo: ${characterTwoHasCompletedLevelThree}`
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// check how many items the characterTwo has in his inventory through the `Realm.Set.size` property
const characterTwoInventorySize = characterTwo.inventory.size;
console.log(`The characterTwo has ${characterTwoInventorySize} inventory items`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// create an embedded address object
const sydneyOrthodontics = {
street: "42 Wallaby Way",
city: "Sydney",
country: "Australia",
postalCode: "2774",
};
realm.write(() => {
// create a contact object
realm.create("Contact", {
_id: new BSON.ObjectId(),
name: "Philip Sherman",
address: sydneyOrthodontics, // embed the address in the contact object
});
});
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,
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let johnDoe;
let janeSmith;
realm.write(() => {
johnDoe = realm.create("Person", {
name: "John Doe",
home: {
windows: 5,
doors: 3,
color: "red",
address: "Summerhill St.",
price: 400123,
},
});
janeSmith = realm.create("Person", {
name: "Jane Smith",
home: {
address: "100 northroad st.",
yearBuilt: 1990,
},
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let characterOne, characterTwo;
realm.write(() => {
characterOne = realm.create("Character", {
_id: new BSON.ObjectId(),
name: "CharacterOne",
inventory: ["elixir", "compass", "glowing shield"],
levelsCompleted: [4, 9],
});
characterTwo = realm.create("Character", {
_id: new BSON.ObjectId(),
name: "CharacterTwo",
inventory: ["estus flask", "gloves", "rune"],
levelsCompleted: [1, 2, 5, 24],
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const PersonSchema = {
name: "Person",
properties: {
name: "string",
home: "{}",
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const AddressSchema = {
name: "Address",
embedded: true, // default: false
properties: {
street: "string?",
city: "string?",
country: "string?",
postalCode: "string?",
},
};

const ContactSchema = {
name: "Contact",
primaryKey: "_id",
properties: {
_id: "objectId",
name: "string",
address: "Address", // Embed a single object
},
};

const BusinessSchema = {
name: "Business",
primaryKey: "_id",
properties: {
_id: "objectId",
name: "string",
addresses: { type: "list", objectType: "Address" }, // Embed an array of objects
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const DogSchema = {
name: "Dog",
properties: {
name: "string",
birthDate: "mixed",
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const characterSchema = {
name: "Character",
primaryKey: "_id",
properties: {
_id: "objectId",
name: "string",
levelsCompleted: "int<>",
inventory: "string<>",
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
realm.write(() => {
// Deleting the contact will delete the embedded address of that contact
realm.delete(
realm.objects("Contact").filtered("name = 'Philip Sherman'")
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// create a new address
const harryNewAddress = {
street: "12 Grimmauld Place",
city: "London",
country: "UK",
postalCode: "E1 7AA",
};
realm.write(() => {
// overwrite the embedded object with the new address within a write transaction
harryPotter.address = harryNewAddress;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// query for all Person objects
const persons = realm.objects("Person");

// run the `.filtered()` method on all the returned persons to find the house with the address "Summerhill St."
const summerHillHouse = persons.filtered(
`home['address'] = "Summerhill St."`
)[0].home;

// Find all people that have a house with a listed price
const peopleWithHousesWithAListedPrice = persons.filtered(
`home.@keys = "price" `
);
// find a house that has any field with a value of 'red'
const redHouse = persons.filtered(`home.@values = "red" `)[0].home;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const philipShermanAddress = realm
.objects("Contact")
.filtered("name = 'Philip Sherman'")[0].address.street;
console.log(`Philip Sherman's address is ${philipShermanAddress}`);
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}`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
realm.write(() => {
// clear all data from the inventory slot of the characterTwo by calling `Realm.Set.clear()` in a write transaction
characterTwo.inventory.clear();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
realm.write(() => {
// remove the 'windows' and 'doors' field of the Summerhill House.
summerHillHouse.remove(["windows", "doors"]);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
realm.write(() => {
// remove the compass from characterOne's inventory by calling `Realm.Set.delete()` within a write transaction
characterOne.inventory.delete("compass");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let newVictorianHome;
realm.write(() => {
newVictorianHome = {
doors: 4,
floor: 3,
color: "white",
address: "Trailwoods Rd.",
};
// use the `put()` method to add a dictionary to a pre-existing city in the database
summerHillHouse.home.put(newVictorianHome);

// alternatively, use dot notation to add a dictionary to a pre-existing city
yakimaCity.home = newVictorianHome;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
realm.write(() => {
// use the `put()` method to update a field of a dictionary
summerHillHouse.put({ price: 400100 });
// alternatively, update a field of a dictionary through dot notation
summerHillHouse.color = "brown";
// update a dictionary by adding a field
summerHillHouse.yearBuilt = 2004;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Find the contact with the address you want to update
const harryPotter = realm
.objects("Contact")
.filtered("name = 'Harry Potter'")[0];
// modify the property of the embedded object in a write transaction
realm.write(() => {
// update the embedded object directly through the contact
harryPotter.address.street = "1 Hogwarts Ave";
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { UUID } = Realm.BSON;
const ProfileSchema = {
name: "Profile",
primaryKey: "_id",
properties: {
_id: "uuid",
name: "string",
},
};
const realm = await Realm.open({
schema: [ProfileSchema],
});
realm.write(() => {
realm.create("Profile", {
name: "John Doe.",
_id: new UUID(), // create a _id with a randomly generated UUID
});
realm.create("Profile", {
name: "Tim Doe.",
_id: new UUID("882dd631-bc6e-4e0e-a9e8-f07b685fec8c"), // create a _id with a specific UUID value
});
});
1 change: 1 addition & 0 deletions source/sdk/node.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ MongoDB Realm Node.js SDK
Quick Start </sdk/node/quick-start-local>
Quick Start with Sync </sdk/node/quick-start>
Fundamentals </sdk/node/fundamentals>
Data Types </sdk/node/data-types>
Usage Examples </sdk/node/examples>
Integration Guides </sdk/node/integrations>
Advanced Guides </sdk/node/advanced>
Expand Down
29 changes: 29 additions & 0 deletions source/sdk/node/data-types.txt
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>`
Loading

0 comments on commit f43ae61

Please sign in to comment.