New in version 10.5.0.
You can use the dictionary
data type to manage a collection of unique String keys paired with values. The dictionary
data maps to the Javascript Object type.
To define a dictionary of mixed values in your schema, set the data type of your field to an empty object, "{}"
. Alternatively, to create a dictionary with values of a specific type, add the data type before the brackets. For instance, "int{}"
to specify that dictionary values must be integers or "string{}"
to specify that dictionary values must be strings.
const PersonSchema = { name: "Person", properties: { name: "string", home: "{}", },};
Realm disallows the use of .
or $
characters in map keys. You can use percent encoding and decoding to store a map key that contains one of these disallowed characters.
const mapKey = "kitchen.windows";const encodedMapKey = mapKey.replace(".", "%2E");
Create an object with a dictionary value by running the realm.create() method within a write transaction.
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, }, });});
To filter a query, run collection.filtered() to specify a subset of results based on the value(s) of one or more object properties. You can specify results based on the value of a dictionary's properties by using bracket-notation.
You can also determine whether a results collection has a certain key or value by using <dictionary>.@keys
or <dictionary>.@values
. For instance, if you had a Person
collection with a nested home
dictionary, you could return all Person
objects with a home
with a "price"
property by running the query: home.@keys = "price"
.
const persons = realm.objects("Person");const summerHillHouse = persons.filtered( `home['address'] = "Summerhill St."`)[0].home;const peopleWithHousesWithAListedPrice = persons.filtered( `home.@keys = "price" `);const redHouse = persons.filtered(`home.@values = "red" `)[0].home;
You can add a listener to a dictionary by running the dictionary.addListener() method. The addListener
method's callback function has two parameters, the changed dictionary and an array of changes describing how the dictionary was changed.
summerHillHouse.addListener((changedHouse, changes) => { console.log("A change has occurred to the Summer Hill House object");});
To update a dictionary's properties, use dot notation or the dictionary.put()
method.
realm.write(() => { summerHillHouse.set({ price: 400100 }); summerHillHouse.color = "brown"; summerHillHouse.yearBuilt = 2004;});
To delete members of a dictionary, use the dictionary.remove()
method with an array of properties to remove from the dictionary.
realm.write(() => { summerHillHouse.remove(["windows", "doors"]);});
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4