Database abstraction object.

Provides a way to access, manipulate, and store data in Nymph.

The GUID is not set until the entity is saved. GUIDs must be unique forever, even after deletion. It's the job of the Nymph DB driver to make sure no two entities ever have the same GUID. This is generally done by using a large randomly generated ID.

Each entity class has an etype that determines which table(s) in the database it belongs to. If two entity classes have the same etype, their data will be stored in the same table(s). This isn't a good idea, however, because references to an entity store a class name, not an etype.

Tags are used to classify entities. Where an etype is used to separate data by tables, tags can be used to separate entities within a table. You can define specific tags to be protected, meaning they cannot be added/removed from the client. It can be useful to allow user defined tags, such as for blog posts.

Simply calling $delete() will not unset the entity. It will still take up memory. Likewise, simply calling unset will not delete the entity from the DB.

Some notes about $equals() and $is(), the replacements for "==":

The == operator will likely not give you the result you want, since two instances of the same entity will fail that check, even though they represent the same data in the database.

$equals() performs a more strict comparison of the entity to another. Use $equals() instead of the == operator when you want to check both the entities they represent, and the data inside them. In order to return true for $equals(), the entity and object must meet the following criteria:

  • They must be entities.
  • They must have equal GUIDs, or both must have no GUID.
  • Their data and tags must be equal.

$is() performs a less strict comparison of the entity to another. Use $is() instead of the == operator when the entity's data may have been changed, but you only care if they represent the same entity. In order to return true, the entity and object must meet the following criteria:

  • They must be entities.
  • They must have equal GUIDs, or both must have no GUID.
  • If they have no GUIDs, their data and tags must be equal.

Some notes about saving entities in other entity's properties:

Entities use references in the DB to store an entity in their properties. The reference is stored as an array with the values:

  • 0 => The string 'nymph_entity_reference'
  • 1 => The referenced entity's GUID.
  • 2 => The referenced entity's class name.

Since the referenced entity's class name (meaning the class static property, not the name of the class itself) is stored in the reference on the parent entity, if you change the class name in an update, you need to reassign all referenced entities of that class and resave.

When an entity is loaded, it does not request its referenced entities from Nymph. Instead, it creates instances without data called sleeping references. When you first access an entity's data, if it is a sleeping reference, it will fill its data from the DB. You can call clearCache() to turn all the entities back into sleeping references.

Type Parameters

Hierarchy

  • Entity

    Implements

    Constructors

    Properties

    $allowlistData?: string[]

    If this is defined, then it lists the only properties that will be accepted from incoming JSON. Any other properties will be ignored.

    If you use an allowlist, you don't need to use protectedData, since you can simply leave those entries out of allowlistData.

    $allowlistTags?: string[]

    If this is defined, then it lists the only tags that will be accepted from incoming JSON. Any other tags will be ignored.

    $clientEnabledMethods: string[] = []

    The names of methods allowed to be called by the frontend with serverCall.

    $data: T

    The data proxy object.

    $dataHandler: Object

    The data proxy handler.

    $dataStore: T

    The actual data store.

    $isASleepingReference: boolean = false

    Whether this instance is a sleeping reference.

    $nymph: Nymph

    The instance of Nymph to use for queries.

    $privateData: string[] = []

    Properties that will not be serialized into JSON with toJSON(). This can be considered a denylist, because these properties will not be set with incoming JSON.

    Clients CAN still determine what is in these properties, unless they are also listed in searchRestrictedData.

    $protectedData: string[] = []

    Properties that can only be modified by server side code. They will still be visible on the frontend, unlike $privateData, but any changes to them that come from the frontend will be ignored.

    In addition to what's listed here, all of the access control properties will be included when Tilmeld is being used. These are:

    • acUser
    • acGroup
    • acOther
    • acRead
    • acWrite
    • acFull
    • user
    • group

    You should modify these through client enabled methods or the $save method instead, for safety.

    $protectedTags: string[] = []

    Tags that can only be added/removed by server side code. They will still be visible on the frontend, but any changes to them that come from the frontend will be ignored.

    The actual sdata store.

    $sleepingReference: null | EntityReference = null

    The reference to use to wake.

    $wakePromise: null | Promise<Entity<T>> = null

    A promise that resolved when the entity's data is wake.

    cdate: null | number = null

    The creation date of the entity as a Unix timestamp in milliseconds.

    guid: null | string = null

    The entity's Globally Unique ID.

    This is a 12 byte number represented as a lower case HEX string (24 characters).

    mdate: null | number = null

    The modified date of the entity as a Unix timestamp in milliseconds.

    tags: string[] = []

    Array of the entity's tags.

    class: string = 'Entity'

    The lookup name for this entity.

    This is used for reference arrays (and sleeping references) and client requests.

    clientEnabledStaticMethods: string[] = []

    The names of static methods allowed to be called by the frontend with serverCallStatic.

    ETYPE: string = 'entity'

    A unique name for this type of entity used to separate its data from other types of entities in the database.

    nymph: Nymph = ...

    The instance of Nymph to use for queries.

    pubSubEnabled: boolean = true

    Whether this entity should publish changes to PubSub servers.

    restEnabled: boolean = true

    Whether this entity should be accessible on the frontend through the REST server.

    If this is false, any request from the client that attempts to use this entity will fail.

    searchRestrictedData: string[] = []

    Properties that will not be searchable from the frontend. If the frontend includes any of these properties in any of their clauses, they will be filtered out before the search is executed.

    Methods

    • Search the array for this object and return the corresponding index.

      If strict is false, is() is used to compare. If strict is true, equals() is used.

      Parameters

      • array: any[]

        The array to search.

      • strict: boolean = false

        Whether to use stronger comparison.

      Returns number

      The index if the object is in the array, -1 if it isn't.

    • Replace any referenced entities in the data with sleeping references.

      Calling this function ensures that the next time a referenced entity is accessed, it will be retrieved from the DB (unless it is in Nymph's cache).

      Returns void

    • Used to retrieve the data object.

      This should only be used by Nymph to save the data into storage.

      Parameters

      • includeSData: boolean = false

        Whether to include the serialized data as well.

      • OptionalreferenceOnlyExisting: boolean

        Whether to only turn existing entities into references.

      Returns any

      The entity's data object.

    • Get a GUID for the entity.

      If the entity has already been saved, this will just return the GUID.

      If the entity has not yet been saved, this will return a new GUID that gets held by the entity. The guid property will remain null, but this method will then always return the same GUID. When the entity is eventually saved into the database, this GUID will be used.

      Returns string

    • Get an array of strings that must be unique across the current etype.

      When you try to save another entity with any of the same unique strings, Nymph will throw an error.

      The default implementation of this method returns an empty array, meaning there are no uniqueness constraints applied to its etype.

      Returns Promise<string[]>

      Resolves to an array of entity's unique constraint strings.

    • Check whether this object is in an array.

      If strict is false, is() is used to compare. If strict is true, equals() is used.

      Parameters

      • array: any[]

        The array to search.

      • strict: boolean = false

        Whether to use stronger comparison.

      Returns boolean

      True if the object is in the array, false if it isn't.

    • Accept JSON data from the client.

      This function uses the security protection lists:

      • $protectedTags
      • $protectedData
      • $allowlistTags
      • $allowlistData

      Parameters

      • input: EntityJson

        The input data. Please note, this will be modified (destroyed).

      • allowConflict: boolean = false

        Allow to accept data that is older than the current data.

      Returns void

    • Accept JSON patch from the client.

      This function uses the security protection lists:

      • $protectedTags
      • $protectedData
      • $allowlistTags
      • $allowlistData

      Parameters

      • patch: EntityPatch

        The patch data. Please note, this will be modified (destroyed).

      • allowConflict: boolean = false

        Allow to accept data that is older than the current data.

      Returns void

    • Used to set the data.

      This should only be used by Nymph to push the data from storage or the client.

      sdata is used by Nymph to avoid unserializing data that hasn't been requested yet.

      If source is set to "server", the data is coming from the DB or the cache. If not, assume the data is coming from the client and can't be trusted.

      Parameters

      • data: EntityData

        The data object.

      • Optionalsdata: SerializedEntityData

        The serialized data object.

      • Optional_source: "server"

        If this is set to "server", the data is coming from the DB.

      Returns void

    • Refresh the object from storage. (Bypasses Nymph's cache.)

      If the object has been deleted from storage, the database cannot be reached, or a database error occurs, refresh() will return 0.

      Returns Promise<boolean | 0>

      False if the data has not been saved, 0 if it can't be refreshed, true on success.

    • Return a Nymph Entity Reference for this entity.

      If the entity hasn't been saved yet (and has no GUID), it will use the guaranteed GUID from $getGuaranteedGUID, unless existingOnly is true, then it will return the entity.

      Parameters

      • OptionalexistingOnly: boolean

        Whether to only turn existing entities into references.

      Returns Entity<T> | EntityReference

      A Nymph Entity Reference array.

    • Alter the options for a query for this entity.

      Type Parameters

      Parameters

      • options: T

        The current options.

      Returns T

      The altered options.

    • Get a string for full text search for one of an entity's properties.

      The result will be tokenized and stored as the full text search index for use with "search" clauses.

      Return null to not include any tokens in the full text search storage.

      You shouldn't use any other entity data to transform the text, as it won't always be available. This function is meant for things like stripping HTML tags.

      Parameters

      • _name: string
      • value: any

      Returns null | string

      By default, returns the value if it is a string.

    • Get an array of strings that must be unique across the current etype.

      When you try to save another entity with any of the same unique strings, Nymph will throw an error.

      The default implementation of this static method instantiates the entity, assigns all of the given data, then calls $getUniques and returns its output. This can have a performance impact if a lot of extra processing happens during any of these steps. You can override this method to calculate the unique strings faster, but you must return the same strings that would be returned by $getUniques.

      Parameters

      • __namedParameters: {
            cdate?: number;
            data: EntityData;
            guid?: string;
            mdate?: number;
            sdata?: SerializedEntityData;
            tags: string[];
        }

      Returns Promise<string[]>

      Resolves to an array of entity's unique constraint strings.