new Model()
Create a representation of a database Doc. Should only be used by the
library.
Classes
Members
-
static collectionName :String
-
This is the name of the collection this model is for. By default, the collection name is the model's class name. However, classes may choose to override this method and provide there own name (e.g., for co-existed models where multiple models have data in one collection).
-
FIELDS
-
Defines the non-key fields. By default there are no fields. Properties are defined as a map from field names to Schema objects:
Example
static FIELDS = { someNumber: S.double, someNumberWithOptions: S.double.optional().default(0).readOnly() } -
KEY
-
Defines the key. Every doc in the database is uniquely identified by its' key. The default 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) }
Methods
-
static data(vals) → {Data}
-
Returns a Data fully describing a unique document in this model's DB collection.
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 document in this model's DB collection.
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. -
static makeQuery()
-
Returns a query object for querying this model.
-
async, static runQuery() → {Array.<Model>}
-
Runs a query object returned by this model's makeQuery() function.
Returns:
Array.<Model> - returns an array of instances of this class that matched the query -
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 nameString the name of a field from FIELDS -
getSnapshot(params)
-
Return snapshot of the model, all fields included.
Parameters:
Name Type Description paramsObject Properties
Name Type Description initialBoolean Whether to return the initial state dbKeysBoolean Whether to return _id instead of raw key fields. omitKeyBoolean whether to omit the key -
toString()
-
Returns the document path to this object.