Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node and React Native Data Types #1065

Merged
merged 29 commits into from
May 19, 2021
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
849b626
New data types node rn (#1040)
May 6, 2021
d9d42cd
fixed refs for node.js data types
May 6, 2021
765c511
update doc
May 6, 2021
bbc7817
Remove accidental changes
May 6, 2021
9054308
added empty test file
May 6, 2021
c894f77
(DOCSP-15613): Added Node.js field types (#1041)
May 11, 2021
2755206
(DOCSP-15613): Node.js collections type (#1044)
May 11, 2021
b79ffbc
(DOCSP-15613): Node.js embedded objects type (#1047)
May 11, 2021
2475974
Docsp 14569 mixed data type (#1064)
May 11, 2021
efae8d0
(DOCSP-14565): Dictionary data type (#1058)
May 13, 2021
5f43200
(DOCSP-14577): UUID (#1067)
May 18, 2021
c003974
(DOCSP-14573): set data type (#1079)
May 19, 2021
2202f42
fill out field types
May 19, 2021
43e9e37
add react native data types as a copy of node data types
May 19, 2021
6b5aaef
add data types to toc
May 19, 2021
63a342e
fix rn mixed links
May 19, 2021
33d63be
attempt to add realm-js links
May 19, 2021
b2587f1
more grammar and woridng fixes
May 19, 2021
d7dbb9e
wording fixes
May 19, 2021
96870ba
Update source/sdk/node/data-types/uuid.txt
May 19, 2021
efb91b9
change uuid note on rn to match node
May 19, 2021
375093f
added note about obj id to both rn and node
May 19, 2021
f2035ae
Update source/sdk/node/data-types/mixed.txt
May 19, 2021
897704a
fix 'mixed' formatting on rn to match node
May 19, 2021
c44908b
change monospace to bold
May 19, 2021
e0ea0fc
fix spacing errors
May 19, 2021
97a5350
fix wording
May 19, 2021
99078cc
correct supported types for mixed
May 19, 2021
e0cc203
wording update
May 19, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -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"
},
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
@@ -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>
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