Skip to content

Node and React Native Data Types #1065

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

Merged
merged 29 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
(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]>
  • Loading branch information
Mohammad Hunan Chughtai and Mohammad Hunan Chughtai authored May 13, 2021
commit efae8d09e60736e7724276a5a74122c5496b53d2
118 changes: 117 additions & 1 deletion examples/node/Examples/data-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,115 @@ const BusinessSchema = {
},
};
// :code-block-end:

describe("Node.js Data Types", () => {
test("should create, update and query Realm dictionaries", async () => {
// :code-block-start: define-dictionary-in-schema
const PersonSchema = {
name: "Person",
properties: {
name: "string",
home: "{}",
},
};
// :code-block-end:

const realm = await Realm.open({
schema: [PersonSchema],
});

// :code-block-start: create-realm-obj-with-dictionary
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,
},
});
});
// :code-block-end:

// :code-block-start: query-a-dictionary
// 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;
// :code-block-end:

// the following assertion tests both creation of a dictionary + querying a dictionary
expect(peopleWithHousesWithAListedPrice.length).toBe(1); // there should only be one house with a listed price
expect(redHouse.doors).toBe(3); // the red house should have 3 doors

let dictionaryListenerHasBeenCalled = false;
// :code-block-start: add-a-listener-to-a-dictionary
summerHillHouse.addListener((changedHouse, changes) => {
// :hide-start:
dictionaryListenerHasBeenCalled = true;
// :hide-end:
console.log("A change has occurred to the Summer Hill House object");
});
// :code-block-end:

// :code-block-start: update-a-dictionary
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;
});
// :code-block-end:

expect(dictionaryListenerHasBeenCalled).toBe(true); // a home (dictionary inside a realm object) should be able to have a change listener
expect(summerHillHouse.price).toBe(400100); // the summerHillHouse should be $400,100 now
expect(summerHillHouse.color).toBe("brown"); // the summerHillHouse should be brown now
expect(summerHillHouse.yearBuilt).toBe(2004); // the summerHillHouse should've been built in 2004

console.log(summerHillHouse);
// :code-block-start: remove-fields-of-the-dictionary
realm.write(() => {
// remove the 'windows' and 'doors' field of the Summerhill House.
// :uncomment-start:
// summerHillHouse.remove(["windows", "doors"]);
// :uncomment-end:
});
// :code-block-end:

// expect(summerHillHouse.windows).toBe(undefined); // since windows has been removed as a field, it should be undefined
// expect(summerHillHouse.doors).toBe(undefined); // since doors has been removed as a field, it should be undefined

// delete the objects to keep the test idempotent
realm.write(() => {
realm.delete(johnDoe);
realm.delete(janeSmith);
});
// close the realm to avoid memory leaks
realm.close();
});
test("should work with Mixed Type", async () => {
// :code-block-start: define-mixed-in-schema
const DogSchema = {
Expand Down Expand Up @@ -92,6 +200,9 @@ describe("Node.js Data Types", () => {
realm.delete(Euclid);
realm.delete(Pythagoras);
});
// close the realm
realm.close();
});
test("should create and read and delete an embedded object", async () => {
const realm = await Realm.open({
schema: [AddressSchema, ContactSchema],
Expand Down Expand Up @@ -131,7 +242,6 @@ describe("Node.js Data Types", () => {
);
});
// :code-block-end:

// close the realm
realm.close();
});
Expand Down Expand Up @@ -182,5 +292,11 @@ describe("Node.js Data Types", () => {
// :code-block-end:

expect(harryPotter.address.city).toBe("London");
// delete the object specifically created in this test to keep tests idempotent
realm.write(() => {
realm.delete(harryPotter);
});
// close the realm
realm.close();
});
});
Loading