new Model()
Create a representation of a database Item. Should only be used by the
library.
Members
-
static tableName :String
-
The table name this model is associated with, excluding the service ID prefix. This is the model's class name. However, subclasses may choose to override this method and provide duplicated table name for co-existed models.
-
FIELDS
-
Defines the non-key fields. By default there are no fields. Properties are defined as a map from field names to a Todea schema:
Example
static FIELDS = { someNumber: S.double, someNumberWithOptions: S.double.optional().default(0).readOnly() }
-
INDEX_INCLUDE_KEYS
-
If this is enabled, we will create individual fields internally for all the field in KEY, SORT_KEY. This will incrase the storage cost but enable users to use lazy filter on individual key fields. This works only for querying via indexes, if the key field isn't part of a key in that index.
-
INDEXES
-
Defines the global secondary indexes for the table. By default there are no secondary indexes. Properties are defined as a map from index names to an object containing the keys for the index (fields defined in an index key should already exist in the table):
Example
static INDEXES = { index1: { KEY: [field1, field2] } index2: { KEY: [field3], SORT_KEY: [field4] } }
-
KEY
-
Defines the partition key. Every item in the database is uniquely identified by the combination of its partition and sort key. The default partition key is a UUIDv4. A key can simply be some scalar value: static KEY = { id: S.str } A key may can be "compound key", i.e., a key with one or components, each with their own name and schema: static KEY = { email: S.str, birthYear: S.int.min(1900) }
-
SORT_KEY
-
Defines the sort key, if any. Uses the compound key format from KEY.
Methods
-
static __computeKeyAttrMap(vals)
-
Returns a map containing the model's computed key values (_id, as well as _sk if model has a sort key). Verifies that the keys are valid (i.e., they match the required schema).
Parameters:
Name Type Description vals
Object map of field names to values Returns:
- map of _id (and _sk attribute values) -
static __decodeCompoundValue(keyOrder, strVal, attrName)
-
Returns the map which corresponds to the given compound value string This method throws InvalidFieldError if the decoded string does not match the required schema.
Parameters:
Name Type Description keyOrder
Array.<String> order of keys in the string representation strVal
String the string representation of a compound value attrName
String which key we're parsing -
static __doOneTimeModelPrep()
-
Check that field names don't overlap, etc.
-
static __encodeCompoundFieldName(fieldsopt)
-
Generate a compound field name given a list of fields. For compound field containing a single field that is not either a PK or SK, we use the same name as the original field to reduce data duplication. We also auto-detect if _id, or _sk can be re-used
Parameters:
Name Type Attributes Description fields
<optional>
a list of string denoting the fields Returns:
- a string denoting the compound field's internal name -
static __encodeCompoundValue(keyOrder, values)
-
Returns the string representation for the given compound values. This method throws InvalidFieldError if the compound value does not match the required schema.
Parameters:
Name Type Description keyOrder
Array.<String> order of keys in the string representation values
Object maps component names to values; may have extra fields (they will be ignored) -
package, static __getParams(encodedKeys, options) → {Object}
-
Parameters:
Name Type Description encodedKeys
CompositeID options
GetParams Returns:
Object - parameters for a get request to DynamoDB -
static __splitKeysAndData(data)
-
Given a mapping, split compositeKeys from other model fields. Return a 3-tuple, [encodedKeys, keyComponents, modelData].
Parameters:
Name Type Description data
Object data to be split -
static data(vals) → {Data}
-
Returns a Data fully describing a unique row in this model's DB table.
Parameters:
Name Type Description vals
* like the argument to key() but also includes non-key data Returns:
Data - a Data object for use with tx.create() or tx.get(..., { createIfMissing: true }) -
static key(vals) → {Key}
-
Returns a Key identifying a unique row in this model's DB table.
Parameters:
Name Type Description vals
* map of key component names to values; if there is only one partition key field (whose type is not object), then this MAY instead be just that field's value. Returns:
Key - a Key object. -
package __conditionCheckParams() → {Boolean}
-
Used for optimistic locking within transactWrite requests, when the model was read in a transaction, and was subsequently used for updating other models but never written back to DB. Having conditionCheck ensures this model's data hasn't been changed so the updates to other models are also correct.
Returns:
Boolean - An Object for ConditionCheck request. -
__isMutated(expectWrites)
-
Indicates if any field was mutated. New models are considered to be mutated as well.
Parameters:
Name Type Default Description expectWrites
Boolean true whether the model will be updated, default is true. -
__nonexistentModelCondition()
-
Returns:
- a [ConditionExpression, ExpressionAttributeNames, ExpressionAttributeValues] tuple to make sure the model does not exist on server. -
package __putParams()
-
Generates parameters for a put request to DynamoDB. Put overrides item entirely, removing untracked fields from DynamoDB. This library supports optimistic locking for put. Since put overrides all fields of an item, optimistic locking is performed on all fields. This means if any fields is modified after the item is read calling put would fail. Effectively the lock applies to the entire item, which may lead to more contention. Have update in most use cases is more desirable.
Returns:
- parameters for a put request to DynamoDB -
package __updateParams(omitUpdates)
-
Generates parameters for an update request to DynamoDB. Update only overrides fields that got updated to a different value. Untracked fields will not be removed from DynamoDB. This library supports optimistic locking for update. Since update only touches specific fields of an item, optimistic locking is only performed on fields accessed (read or write). This locking mechanism results in less likely contentions, hence is preferred over put.
Parameters:
Name Type Default Description omitUpdates
Boolean false (default = false) When True, generates only condition expressions for read values; skipping update expressions, related Attribute Names/Values and schema validation, with the expectation that any accessed value is either unmodified (and therefore valid) or explicitly unchecked (written but not read). Returns:
- parameters for a update request to DynamoDB -
async, package __write()
-
Writes model to database. Uses DynamoDB update under the hood.
-
async finalize()
-
Hook for finalizing a model before writing to database
-
getField(name) → {BooleanField|ArrayField|ObjectField|NumberField|StringField}
-
Returns the underlying __Field associated with an attribute.
Parameters:
Name Type Description name
String the name of a field from FIELDS Returns:
BooleanField | ArrayField | ObjectField | NumberField | StringField -
getSnapshot(params)
-
Return snapshot of the model, all fields included.
Parameters:
Name Type Description params
Object Properties
Name Type Description initial
Boolean Whether to return the initial state dbKeys
Boolean Whether to return _id and _sk instead of raw key fields. -
toString()
-
Must be the same as NonExistentModel.toString() because it is used as the unique identifier of an item for Objects and Sets.