Class Entity<T>

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.

$originalAcValues: null | ACProperties = null

The AC properties' values when the entity was loaded.

$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.

$skipAc: boolean = false

Whether to use "skipAc" when accessing entity references.

$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.

ETYPE: string = 'entity'

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

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.

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

  • Returns {
        acFull: null | string[];
        acGroup: any;
        acOther: any;
        acRead: null | string[];
        acUser: any;
        acWrite: null | string[];
        group: null | string;
        user: null | string;
    }

    • acFull: null | string[]
    • acGroup: any
    • acOther: any
    • acRead: null | string[]
    • acUser: any
    • acWrite: null | string[]
    • group: null | string
    • user: null | string
  • 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.

    Returns any

    The entity's data object.

  • 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

  • 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.

Generated using TypeDoc