Skip to content

Commit f9c25c7

Browse files
committed
Update Schemas
1 parent eb10407 commit f9c25c7

File tree

14 files changed

+236
-7
lines changed

14 files changed

+236
-7
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Realm from 'realm';
2+
3+
// :snippet-start: js-address-schema
4+
class Address extends Realm.Object {
5+
static schema = {
6+
name: "Address",
7+
embedded: true, // default: false
8+
properties: {
9+
street: "string?",
10+
city: "string?",
11+
country: "string?",
12+
postalCode: "string?",
13+
},
14+
};
15+
}
16+
// :snippet-end:
17+
export default Address;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Realm from 'realm';
2+
3+
// :snippet-start: js-business-schema
4+
class Business extends Realm.Object {
5+
static schema = {
6+
name: "Business",
7+
primaryKey: "_id",
8+
properties: {
9+
_id: "objectId",
10+
name: "string",
11+
addresses: { type: "list", objectType: "Address" }, // Embed an array of objects
12+
},
13+
};
14+
}
15+
// :snippet-end:
16+
export default Business;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Realm from 'realm';
2+
3+
// :snippet-start: js-contact-schema
4+
class Contact extends Realm.Object {
5+
static schema = {
6+
name: "Contact",
7+
primaryKey: "_id",
8+
properties: {
9+
_id: "objectId",
10+
name: "string",
11+
address: "Address", // Embed a single object
12+
},
13+
};
14+
}
15+
// :snippet-end:
16+
export default Contact;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Realm from 'realm';
2+
3+
// TODO: Replace `static schema` with TS-first models + realm-babel-plugin (https://www.npmjs.com/package/@realm/babel-plugin) approach once realm-babel-plugin version 0.1.2 releases with bug fixes
4+
// :snippet-start: ts-address-schema
5+
class Address extends Realm.Object<Address> {
6+
street?: string;
7+
city?: string;
8+
country?: string;
9+
postalCode?: string;
10+
11+
static schema = {
12+
name: "Address",
13+
embedded: true, // default: false
14+
properties: {
15+
street: "string?",
16+
city: "string?",
17+
country: "string?",
18+
postalCode: "string?",
19+
},
20+
};
21+
}
22+
// :snippet-end:
23+
export default Address;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Realm from 'realm';
2+
import Address from './Address';
3+
4+
// TODO: Replace `static schema` with TS-first models + realm-babel-plugin (https://www.npmjs.com/package/@realm/babel-plugin) approach once realm-babel-plugin version 0.1.2 releases with bug fixes
5+
// :snippet-start: ts-business-schema
6+
class Business extends Realm.Object {
7+
_id!: string;
8+
name!: string;
9+
addresses!: Realm.List<Address>;
10+
static schema = {
11+
name: "Business",
12+
primaryKey: "_id",
13+
properties: {
14+
_id: "objectId",
15+
name: "string",
16+
addresses: { type: "list", objectType: "Address" }, // Embed an array of objects
17+
},
18+
};
19+
}
20+
// :snippet-end:
21+
export default Business;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Realm from 'realm';
2+
import Address from './Address';
3+
4+
// TODO: Replace `static schema` with TS-first models + realm-babel-plugin (https://www.npmjs.com/package/@realm/babel-plugin) approach once realm-babel-plugin version 0.1.2 releases with bug fixes
5+
// :snippet-start: ts-contact-schema
6+
class Contact extends Realm.Object {
7+
_id!: string;
8+
name!: string;
9+
address!: Address;
10+
11+
static schema = {
12+
name: "Contact",
13+
primaryKey: "_id",
14+
properties: {
15+
_id: "objectId",
16+
name: "string",
17+
address: "Address", // Embed a single object
18+
},
19+
};
20+
}
21+
// :snippet-end:
22+
export default Contact;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Address extends Realm.Object {
2+
static schema = {
3+
name: "Address",
4+
embedded: true, // default: false
5+
properties: {
6+
street: "string?",
7+
city: "string?",
8+
country: "string?",
9+
postalCode: "string?",
10+
},
11+
};
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Business extends Realm.Object {
2+
static schema = {
3+
name: "Business",
4+
primaryKey: "_id",
5+
properties: {
6+
_id: "objectId",
7+
name: "string",
8+
addresses: { type: "list", objectType: "Address" }, // Embed an array of objects
9+
},
10+
};
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Contact extends Realm.Object {
2+
static schema = {
3+
name: "Contact",
4+
primaryKey: "_id",
5+
properties: {
6+
_id: "objectId",
7+
name: "string",
8+
address: "Address", // Embed a single object
9+
},
10+
};
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Address extends Realm.Object<Address> {
2+
street?: string;
3+
city?: string;
4+
country?: string;
5+
postalCode?: string;
6+
7+
static schema = {
8+
name: "Address",
9+
embedded: true, // default: false
10+
properties: {
11+
street: "string?",
12+
city: "string?",
13+
country: "string?",
14+
postalCode: "string?",
15+
},
16+
};
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Business extends Realm.Object {
2+
_id!: string;
3+
name!: string;
4+
addresses!: Realm.List<Address>;
5+
6+
static schema = {
7+
name: "Business",
8+
primaryKey: "_id",
9+
properties: {
10+
_id: "objectId",
11+
name: "string",
12+
addresses: { type: "list", objectType: "Address" }, // Embed an array of objects
13+
},
14+
};
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Contact extends Realm.Object {
2+
_id!: string;
3+
name!: string;
4+
address!: Address;
5+
6+
static schema = {
7+
name: "Contact",
8+
primaryKey: "_id",
9+
properties: {
10+
_id: "objectId",
11+
name: "string",
12+
address: "Address", // Embed a single object
13+
},
14+
};
15+
}

source/includes/embedded-object-json-schema.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ This behavior differs from regular Realm objects, which map to their own MongoDB
3131
{
3232
"title": "Business",
3333
"bsonType": "object",
34-
"required": ["_id", "name", "addresses"],
34+
"required": ["_id", "name"],
3535
"properties": {
36-
"_id": "objectId",
36+
"_id": { "bsonType": "objectId" },
3737
"name": { "bsonType": "string" },
3838
"addresses": {
3939
"bsonType": "array",

source/sdk/react-native/realm-database/schemas/embedded-objects.txt

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,47 @@ To define an embedded object, set ``embedded``
5050
to ``true``. You can reference an embedded object type from parent object types
5151
in the same way as you would define a relationship:
5252

53+
.. tabs-realm-languages::
54+
55+
.. tab::
56+
:tabid: typescript
57+
58+
.. literalinclude:: /examples/generated/react-native/ts/Address.snippet.ts-address-schema.ts
59+
:language: typescript
60+
:emphasize-lines: 9
61+
:linenos:
62+
63+
.. literalinclude:: /examples/generated/react-native/ts/Contact.snippet.ts-contact-schema.ts
64+
:language: typescript
65+
:emphasize-lines: 12
66+
:linenos:
67+
68+
.. literalinclude:: /examples/generated/react-native/ts/Business.snippet.ts-business-schema.ts
69+
:language: typescript
70+
:emphasize-lines: 12
71+
:linenos:
72+
73+
.. tab::
74+
:tabid: javascript
75+
76+
.. literalinclude:: /examples/generated/react-native/js/Address.snippet.js-address-schema.js
77+
:language: javascript
78+
:emphasize-lines: 4
79+
:linenos:
80+
81+
.. literalinclude:: /examples/generated/react-native/js/Contact.snippet.js-contact-schema.js
82+
:language: javascript
83+
:emphasize-lines: 8
84+
:linenos:
85+
86+
.. literalinclude:: /examples/generated/react-native/js/Business.snippet.js-business-schema.js
87+
:language: javascript
88+
:emphasize-lines: 8
89+
:linenos:
90+
5391
.. important::
5492

5593
Embedded objects cannot have a :ref:`primary key <react-native-primary-keys>`.
56-
57-
.. literalinclude:: /examples/generated/node/data-types.snippet.define-embedded-objects.js
58-
:language: javascript
59-
:emphasize-lines: 3, 18, 28
60-
6194

6295
JSON Schema
6396
~~~~~~~~~~~

0 commit comments

Comments
 (0)