Table of Contents

Defining a data model

The object database works with your own Delphi classes rather than raw JSON. You define a model class, the component serializes its published properties to Firebase on insert, and reconstructs typed instances on read. The only requirements are that the model descends from the Firebase object base and that it is registered so the reader can recreate the right class. This chapter covers declaring a model, registering it, and storing values that are not simple scalars.

Descending from the base object

A model class must descend from TTMSFNCCloudGoogleFireBaseObject. Every published property is serialized automatically when the object is stored and restored when it is read; the base also carries a server-assigned ID that identifies the stored record. Keep non-persistent state in public (not published) so it is ignored by serialization.

type
  TContact = class(TTMSFNCCloudGoogleFireBaseObject)
  private
    FName: string;
    FEmail: string;
    FFriend: Boolean;
  published
    // Published properties are serialized to and from the database automatically.
    property Name: string read FName write FName;
    property Email: string read FEmail write FEmail;
    property Friend: Boolean read FFriend write FFriend;
  end;

procedure RegisterModels;
begin
  // Register every model once at startup so the database can reconstruct the
  // correct class when reading records back.
  RegisterClass(TContact);
end;

Registering the model

Because the stored record carries its class name, the reader needs to know that class to recreate an instance. Call RegisterClass once at startup for every model type you store. Without registration the records still exist in Firebase but the read list cannot reconstruct typed objects from them.

Storing binary or composite data

For values that are not simple scalars — an image, a nested record, a list — expose a published string property that encodes the value, backed by a non-published field that holds the real object. The demo's picture field, for example, publishes a DataString that serializes a bitmap to and from a hex string, while the bitmap itself lives in an unpublished field. This keeps the serialized form a plain string while your code works with the rich type.

Pitfalls

  • Only published properties are stored. A value in public/private is not serialized.
  • Register every model before reading. An unregistered class cannot be reconstructed from its stored records.
  • Override Create/Destroy when the model owns sub-objects (such as a bitmap holder), so the owned instances are created and freed with the model.

See also