Skip to content

Commit

Permalink
Incorporate feedback about creating objects for JS/TS
Browse files Browse the repository at this point in the history
  • Loading branch information
dacharyc committed Jul 10, 2024
1 parent e7cf6f8 commit 6fee88c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
In the Node.js SDK, to define an embedded object, set ``embedded``
In the JavaScript SDK, to define an embedded object, set ``embedded``
to ``true``. You can reference an embedded object type from parent object types
in the same way as you would define a relationship.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
You can define SDK models as JavaScript classes that extend ``Realm.Object``
(like most of the examples on this page). Or you can define models as
JavaScript objects.
You can define SDK models as JavaScript classes that extend ``Realm.Object``.

When you define models as JavaScript classes that extend ``Realm.Object``,
you can pass those model objects directly to the database. Prefer this approach
when possible.
When you create objects, use an object literal. This lets you create an
**unmanaged** object, and pass it to the database when it makes sense to do so.

If you do define a model that does not extend ``Realm.Object``, you cannot pass
the model directly to the database. Instead, you pass only the schema to the
database that manages the object.
Do not use ``new`` to construct a new object instance. If you use ``new`` with
class-based models, this creates a new **managed** object, which has the
following side effects:

.. TODO: Provide more info about why you would choose one of these approaches over the other
- Constructing a ``new`` object calls the ``realm.create()`` method, which can
only be used in a write transcation.
- Constructing a ``new`` object has complications when the object contains or
is itself an embedded object.

For more information about object creation and managed objects, refer to
:ref:`sdks-crud-create` and :ref:`sdks-create-specific-object-types`.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
You can define SDK models as TypeScript classes that extend ``Realm.Object``
(like most of the examples on this page). Or you can define models as
TypeScript objects.
You can define SDK models as TypeScript classes that extend ``Realm.Object``.

When you define models as TypeScript classes that extend ``Realm.Object``,
you can pass those model objects directly to the database. Prefer this approach
when possible.
When you create objects, use an object literal. This lets you create an
**unmanaged** object, and pass it to the database when it makes sense to do so.

If you do define a model that does not extend ``Realm.Object``, you cannot pass
the model directly to the database. Instead, you pass only the schema to the
database that manages the object.
Do not use ``new`` to construct a new object instance. If you use ``new`` with
class-based models, this creates a new **managed** object, which has the
following side effects:

.. TODO: Provide more info about why you would choose one of these approaches over the other
- Constructing a ``new`` object calls the ``realm.create()`` method, which can
only be used in a write transcation.
- Constructing a ``new`` object has complications when the object contains or
is itself an embedded object.

For more information about object creation and managed objects, refer to
:ref:`sdks-crud-create` and :ref:`sdks-create-specific-object-types`.

0 comments on commit 6fee88c

Please sign in to comment.