Realm

A Realm instance represents a Realm database.

const Realm = require('realm');

Related Classes

List
Results
Realm.defaultPath
static

The default path where to create and access the Realm file.

Type:
string
new Realm(config)

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.

Parameters:
  • config optional
addListener(name, callback)

Add a listener callback for the specified event name.

Parameters:
  • name
    • Type: string
    • The name of event that should cause the callback to be called. Currently, only the "change" event supported.

  • callback
    • Type: function
    • Function to be called when the event occurs. Each callback will only be called once per event, regardless of the number of times it was added.

Throws:
  • Error
    • If an invalid event name is supplied, or if callback is not a function.

create(type, properties, update)Realm.Object

Create a new Realm object of the given type and with the specified properties.

Parameters:
  • type
    • Type: string
    • The type of object as specified by its name in the ObjectSchema definition.

  • properties
    • Type: Object
    • Property values for all required properties without a default value.

  • update optional
    • Type: boolean
    • Default: false
    • Signals that an existing object with matching primary key should be updated. Only the primary key property and properties which should be updated need to be specified. All missing property values will remain unchanged.

Returns: Realm.Object
delete(object)

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

Parameters:
deleteAll()

WARNING: This will delete all objects in the Realm!

objects(type)Realm.Results

Returns all objects of the given type in the Realm.

Parameters:
  • type
    • Type: string
    • The type of object as specified by its name in the ObjectSchema definition.

Throws:
  • Error
    • If type passed into this method is invalid.

Returns: Realm.Results that will live-update as objects are created and destroyed.
removeAllListeners(name)

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

Parameters:
  • name optional
    • Type: string
    • The name of the event whose listeners should be removed. Currently, only the "change" event supported.

Throws:
  • Error
    • When invalid event name is supplied

removeListener(name, callback)

Remove the listener callback for the specfied event name.

Parameters:
  • name
    • Type: string
    • The event name. Currently, only the "change" event supported.

  • callback
    • Type: function
    • Function that was previously added as a listener for this event through the addListener method.

Throws:
  • Error
    • If an invalid event name is supplied, or if callback is not a function.

write(callback)

Synchronously call the provided callback inside a write transaction.

Parameters:
  • callback
    • Type: function
Configuration

This describes the different options used to create a Realm instance.

Type:
Object
Properties:
  • path optional
    • Type: string
    • Default: Realm.defaultPath
    • The path to the file where the Realm database should be stored.

  • schema optional
    • Type: [ObjectSchema, ...]
    • Specifies all the object types in the realm. Required when first creating realm at this path.

  • schemaVersion optional
    • Type: number
    • Required (and must be incremented) after changing the schema.

ObjectSchema
Type:
Object
Properties:
  • name
    • Type: string
    • Represents the object type.

  • primaryKey optional
    • Type: string
    • The name of a "string" or "int" property that must be unique across all objects of this type within the same Realm.

  • properties
ObjectSchemaProperty
Type:
Object
Properties:
  • objectType optional
    • Type: string
    • Required when type is "list", and must match the type of an object in the same schema.

  • default optional
    • Type: any
    • The default value for this property on creation when not otherwise specified.

  • optional optional
    • Type: boolean
    • Signals if this property may be assigned null or undefined.

PropertyType

A property type may be specified as one of the standard builtin types, or as an object type inside the same schema.

Type:
"bool" or "int" or "float" or "double" or "string" or "date" or "data" or "list" or "<ObjectType>"
Properties:
  • "bool"
    • Type: boolean
    • Property value may either be true or false.

  • "int"
    • Type: number
    • Property may be assigned any number, but will be stored as a round integer, meaning anything after the decimal will be truncated.

  • "float"
    • Type: number
    • Property may be assigned any number, but will be stored as a float, which may result in a loss of precision.

  • "double"
    • Type: number
    • Property may be assigned any number, and will have no loss of precision.

  • "string"
    • Type: string
    • Property value may be any arbitrary string.

  • "date"
    • Type: Date
    • Property may be assigned any Date instance, but will be stored with second-level precision (a fix for this is in progress).

  • "data"
    • Type: ArrayBuffer
    • Property may either be assigned an ArrayBuffer or ArrayBufferView (e.g. DataView, Int8Array, Float32Array, etc.) instance, but will always be returned as an ArrayBuffer.

  • "<ObjectType>"
    • Type: Realm.Object
    • A string that matches the name of an object in the same schema (see ObjectSchema) – this property may be assigned any object of this type from inside the same Realm, and will always be optional (meaning it may also be assigned null or undefined).