-
Notifications
You must be signed in to change notification settings - Fork 88
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
(DOCSP-15613): Node.js embedded objects type #1047
Merged
mohammadhunan-dev
merged 11 commits into
mongodb:new-data-types-node-rn
from
mohammadhunan-dev:Nodejs-embedded-objects-type
May 11, 2021
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5bab40e
Added documentation of Node.js embedded objects under data types
2511b65
clean up relationships and embedded objects
3ea0b70
Merge branch 'new-data-types-node-rn' of github.com:mongodb/docs-real…
701f1e5
added CRUD examples for embedded obj
69316ab
added bluehawked examples
995556e
fix rst syntax highlight + add description for deletes
e2584ba
Update source/sdk/node/data-types/embedded-objects.txt
59865bd
Update source/sdk/node/data-types/embedded-objects.txt
66bfaac
Update source/sdk/node/data-types/embedded-objects.txt
efdd888
Update source/sdk/node/data-types/embedded-objects.txt
3806bff
Update source/sdk/node/data-types/embedded-objects.txt
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,128 @@ | ||
import Realm from "realm"; | ||
import BSON from "bson"; | ||
|
||
// :code-block-start: define-embedded-objects | ||
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 | ||
}, | ||
}; | ||
// :code-block-end: | ||
describe("Node.js Data Types", () => { | ||
}) | ||
test("should create and read and delete an embedded object", async () => { | ||
const realm = await Realm.open({ | ||
schema: [AddressSchema, ContactSchema], | ||
}); | ||
|
||
// :code-block-start: create-an-embedded-object | ||
// 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 | ||
}); | ||
}); | ||
// :code-block-end: | ||
|
||
// :code-block-start: query-an-embedded-object | ||
const philipShermanAddress = realm | ||
.objects("Contact") | ||
.filtered("name = 'Philip Sherman'")[0].address.street; | ||
console.log(`Philip Sherman's address is ${philipShermanAddress}`); | ||
// :code-block-end: | ||
expect(philipShermanAddress).toBe("42 Wallaby Way"); // this assertion tests both the 'query-an-embedded-object' and 'create-an-embedded-object' code blocks | ||
|
||
// // :code-block-start: delete-an-embedded-object | ||
realm.write(() => { | ||
// Deleting the contact will delete the embedded address of that contact | ||
realm.delete( | ||
realm.objects("Contact").filtered("name = 'Philip Sherman'") | ||
); | ||
}); | ||
// :code-block-end: | ||
|
||
// close the realm | ||
realm.close(); | ||
}); | ||
// update and delete an embedded object | ||
test("should update and overwrite an embedded object", async () => { | ||
const realm = await Realm.open({ | ||
schema: [AddressSchema, ContactSchema], | ||
}); | ||
const harryAddress = { | ||
street: "4 Privet Drive", | ||
city: "Little Whinging, Surrey", | ||
country: "UK", | ||
postalCode: "WD4 8PN", | ||
}; | ||
realm.write(() => { | ||
realm.create("Contact", { | ||
_id: new BSON.ObjectId(), | ||
name: "Harry Potter", | ||
address: harryAddress, | ||
}); | ||
}); | ||
|
||
// :code-block-start: update-an-embedded-object | ||
// 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"; | ||
}); | ||
// :code-block-end: | ||
expect(harryPotter.address.street).toBe("1 Hogwarts Ave"); | ||
|
||
// :code-block-start: overwrite-an-embedded-object | ||
// 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; | ||
}); | ||
// :code-block-end: | ||
|
||
expect(harryPotter.address.city).toBe("London"); | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
source/examples/generated/node/data-types.codeblock.create-an-embedded-object.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
source/examples/generated/node/data-types.codeblock.define-embedded-objects.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
}; |
6 changes: 6 additions & 0 deletions
6
source/examples/generated/node/data-types.codeblock.delete-an-embedded-object.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'") | ||
); | ||
}); |
11 changes: 11 additions & 0 deletions
11
source/examples/generated/node/data-types.codeblock.overwrite-an-embedded-object.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
4 changes: 4 additions & 0 deletions
4
source/examples/generated/node/data-types.codeblock.query-an-embedded-object.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); |
9 changes: 9 additions & 0 deletions
9
source/examples/generated/node/data-types.codeblock.update-an-embedded-object.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm kind of fascinated by the idea of recursively referencing an embedded object type as an optional property in its own definition, but am having trouble visualizing a use case for it. I wonder if this might be a good example to add to the page?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might look something like:
Though that tip is copied over from the existing docs, and is a scenario we probably don't want to actually show in our docs since it's so unique that I can't think of a practical use-case. The tip does show the sheer flexibility of embedded objects though.