The Realm database.

Constructors

  • Create a new Realm instance, at the default path.

    Returns export=

    Throws

    An Error when an incompatible synced Realm is opened.

  • Create a new Realm instance at the provided path.

    Parameters

    • path: string

      Required when first creating the Realm.

    Returns export=

    Throws

    An Error if the Realm cannot be opened at the provided path.

    Throws

    An Error when an incompatible synced Realm is opened.

  • Create a new Realm instance using the provided config. If a Realm does not yet exist at config.path (or Realm.defaultPath if not provided), then this constructor will create it with the provided config.schema (which is required in this case). Otherwise, the instance will access the existing Realm from the file at that path. In this case, config.schema is optional or not have changed, unless config.schemaVersion is incremented, in which case the Realm will be automatically migrated to use the new schema. In the case of query-based sync, config.schema is required. An exception will be thrown if config.schema is not defined.

    Parameters

    Returns export=

    Throws

    An Error if anything in the provided config is invalid.

    Throws

    An Error when an incompatible synced Realm is opened.

Properties

syncSession: null | Session

The sync session if this is a synced Realm

defaultPath: string = ...

Accessors

  • get isClosed(): boolean
  • Indicates if this Realm has been closed.

    Returns boolean

    true if closed, false otherwise.

    Since

    2.1.0

  • get isEmpty(): boolean
  • Indicates if this Realm contains any objects.

    Returns boolean

    true if empty, false otherwise.

    Since

    1.10.0

  • get isInMemory(): boolean
  • Indicates if this Realm was opened in-memory.

    Returns boolean

    true if this Realm is in-memory, false otherwise.

  • get isInMigration(): boolean
  • Indicates if this Realm is in migration.

    Returns boolean

    true if in migration, false otherwise

    Since

    12.3.0

  • get isInTransaction(): boolean
  • Indicates if this Realm is in a write transaction.

    Returns boolean

    true if in a write transaction, false otherwise.

    Since

    1.10.3

  • get isReadOnly(): boolean
  • Indicates if this Realm was opened as read-only.

    Returns boolean

    true if this Realm is read-only, false otherwise.

    Since

    0.12.0

  • get path(): string
  • The path to the file where this Realm is stored.

    Returns string

    A string containing the path to the file where this Realm is stored.

    Since

    0.12.0

  • get schemaVersion(): number
  • The current schema version of the Realm.

    Returns number

    The schema version of this Realm, as a number.

    Since

    0.12.0

Methods

  • Add a listener callback for the specified eventName.

    Parameters

    • eventName: RealmEventName

      The name of event that should cause the callback to be called.

    • callback: RealmListenerCallback

      Function to be called when a change event occurs. Each callback will only be called once per event, regardless of the number of times it was added.

    Returns void

    Throws

    An Error if an invalid event eventName is supplied, if Realm is closed or if callback is not a function.

  • Initiate a write transaction.

    When doing a transaction, it is highly recommended to do error handling. If you don't handle errors, your data might become inconsistent. Error handling will often involve canceling the transaction.

    Returns void

    Throws

    An Error if already in write transaction

    See

    Example

    realm.beginTransaction();
    try {
    realm.create('Person', { name: 'Arthur Dent', origin: 'Earth' });
    realm.create('Person', { name: 'Ford Prefect', origin: 'Betelgeuse Five' });
    realm.commitTransaction();
    } catch (e) {
    realm.cancelTransaction();
    throw e;
    }
  • Closes this Realm so it may be re-opened with a newer schema version. All objects and collections from this Realm are no longer valid after calling this method. The method is idempotent.

    Returns void

  • Replaces all string columns in this Realm with a string enumeration column and compacts the database file.

    Cannot be called from a write transaction.

    Compaction will not occur if other Realm instances exist.

    While compaction is in progress, attempts by other threads or processes to open the database will wait.

    Be warned that resource requirements for compaction is proportional to the amount of live data in the database. Compaction works by writing the database contents to a temporary database file and then replacing the database with the temporary one.

    Returns boolean

    true if compaction succeeds, false if not.

  • Deletes the provided Realm object, or each one inside the provided collection.

    Parameters

    • subject: any

      The Realm object to delete, or a collection containing multiple Realm objects to delete.

    Returns void

  • Searches for a Realm object by its primary key.

    Type Parameters

    Parameters

    • type: string

      The type of Realm object to search for.

    • primaryKey: T[keyof T]

      The primary key value of the object to search for.

    Returns null | Object<T, never> & T

    A RealmObject or null if no object is found.

    Throws

    An Error if type passed into this method is invalid, or if the object type did not have a primaryKey specified in the schema, or if it was marked asymmetric.

    Since

    0.14.0

  • Type Parameters

    Parameters

    Returns null | T

  • Remove all event listeners (restricted to the event eventName, if provided).

    Parameters

    • Optional eventName: RealmEventName

      The name of the event whose listeners should be removed.

    Returns void

    Throws

    An Error when invalid event eventName is supplied.

  • Remove the listener callback for the specified event eventName.

    Parameters

    Returns void

    Throws

    an Error If an invalid event eventName is supplied, if Realm is closed or if callback is not a function.

  • Synchronously call the provided callback inside a write transaction. If an exception happens inside a transaction, you’ll lose the changes in that transaction, but the Realm itself won’t be affected (or corrupted). More precisely, beginTransaction and commitTransaction will be called automatically. If any exception is thrown during the transaction cancelTransaction will be called instead of commitTransaction and the exception will be re-thrown to the caller of write.

    Nested transactions (calling write within write) is not possible.

    Type Parameters

    • T

    Parameters

    • callback: (() => T)

      Function to be called inside a write transaction.

        • (): T
        • Returns T

    Returns T

    Returned value from the callback.

  • Writes a compacted copy of the Realm with the given configuration.

    The destination file cannot already exist. All conversions between synced and non-synced Realms are supported, and will be performed according to the config parameter, which describes the desired output.

    Note that if this method is called from within a write transaction, the current data is written, not the data from the point when the previous write transaction was committed.

    Parameters

    • config: Configuration

      Realm configuration that describes the output realm.

    Returns void

  • Copy any Realm files (i.e. *.realm) bundled with the application from the application directory into the application's documents directory, so that they can be opened and used by Realm. If the file already exists in the documents directory, it will not be overwritten, so this can safely be called multiple times.

    This should be called before opening the Realm, in order to move the bundled Realm files into a place where they can be written to.

    Returns void

    Example

    // Given a bundled file, example.realm, this will copy example.realm (and any other .realm files)
    // from the app bundle into the app's documents directory. If the file already exists, it will
    // not be overwritten, so it is safe to call this every time the app starts.
    Realm.copyBundledRealmFiles();

    const realm = await Realm.open({
    // This will open example.realm from the documents directory, with the bundled data in.
    path: "example.realm"
    });

    This is only implemented for React Native.

    Throws

    an Error If an I/O error occurred or method is not implemented.

  • Creates a template object for a Realm model class where all optional fields are undefined and all required fields have the default value for the given data type, either the value set by the default property in the schema or the default value for the datatype if the schema doesn't specify one, i.e. 0, false and "".

    Type Parameters

    • T extends Record<string, unknown>

    Parameters

    Returns T

  • Delete the Realm file for the given configuration.

    Parameters

    • config: Configuration

      The configuration for the Realm being deleted.

    Returns void

    Throws

    An Error if anything in the provided config is invalid.

  • Checks if the Realm already exists on disk.

    Parameters

    • arg: string | Configuration = {}

      The configuration for the Realm or the path to it.

    Returns boolean

    true if the Realm exists on the device, false if not.

    Throws

    An Error if anything in the provided config is invalid.

  • Open a Realm asynchronously with a promise. If the Realm is synced, it will be fully synchronized before it is available. In the case of query-based sync, Configuration.scheme | config.schema is required. An exception will be thrown if Configuration.scheme | config.schema is not defined.

    Parameters

    • arg: string | Configuration = {}

      The configuration for the Realm or the path to it.

    Returns ProgressRealmPromise

    A promise that will be resolved with the Realm instance when it's available.

    Throws

    An Error if anything in the provided arg is invalid.

  • Get the current schema version of the Realm at the given path.

    Parameters

    • path: string

      The path to the file where the Realm database is stored.

    • Optional encryptionKey: ArrayBuffer | ArrayBufferView

      Required only when accessing encrypted Realms.

    Returns number

    Version of the schema as an integer, or -1 if no Realm exists at path.

    Throws

    An Error if passing an invalid or non-matching encryption key.

    Since

    0.11.0

  • Sets the log level.

    Parameters

    • level: LogLevel

      The log level to be used by the logger. The default value is info.

    Returns void

    Note

    The log level can be changed during the lifetime of the application.

    Since

    12.0.0

  • Sets the logger callback.

    Parameters

    • loggerCallback: LoggerCallback

      The callback invoked by the logger. The default callback uses console.log, console.warn and console.error, depending on the level of the message.

    Returns void

    Note

    The logger callback needs to be setup before opening the first realm.

    Since

    12.0.0

Generated using TypeDoc