-
Notifications
You must be signed in to change notification settings - Fork 0
Model
The base Model class for Mythix ORM. Every Mythix ORM model should inherit from this class. This class provides support for all model operations in Mythix ORM.
Notes:
- Many model methods have both static and instance methods of the same name that do the same thing. This is because it is common to access Model methods directly on the model as well as an instance. For example, Model is both a static method, and an instance method.
- An underscore prefix on a method in Mythix ORM implies that this method should not be overloaded unless you know exactly what you are doing. It also implies that there is another method that can and should be overloaded instead.
This property assists with type checking.
method Model::_getConnection(connection?: Connection)
Arguments:
connection?: ConnectionAn optional connection that can be provided. If provided, it will simply be returned. Otherwise, if not provided, or a falsy value, then attempt to fallback to
modelInstance._connection, if that also fails to find a connection, the finally the method will call static _getConnection to get the bound connection, if any is available.Return value: Connection
Notes:
- Pay attention that unlike static _getConnection this checks the model's instance for
._connectionproperty. If that is a valid connection, then it will be returned before the bound connection. This is helpful when you have chosen not to bind a connection to your models. This will allow you to provide a connection directly when you create the model, which can make interacting with the model less tiresome. i.e.new Model(null, { connection }).
static method Model::_getConnection(connection?: Connection)
Get the underlying connection bound to the model's class. Connection binding is optional, so this method can also be provided a connection. If a connection is provided, then it will simply be returned. Because Mythix ORM has no globals, this is a design pattern to supply a connection if you have one available, or fallback to a "bound" connection (if one can be found).
Arguments:
connection?: ConnectionAn optional connection that can be provided. If provided, it will simply be returned. Otherwise, if not provided, or a falsy value, then attempt to fallback to the bound connection, if any is available.
Return value: Connection
static method Model::bindConnection(connection?: Connection)
A Connection instance will call this method on a Model class to bind the model to the connection. Overload this to change the behavior of binding connections to models. This method should return a model class. The default behavior is to return
thisclass. However, you might return a different class for example if you generated a new class that inherited fromthisclass.Arguments:
connection?: ConnectionThe connection instance to bind to this model class.
Return value: class extends Model
static method Model::defaultScope(query: QueryEngine)
One can apply a "default scope" to any model simply by providing this static method on the model. By default it will simply return the QueryEngine instance (a query) that it was provided.
The way this works is simple. The caller provides the query as the first and only argument. The user, by overloading this method can then easily modify this provided query, changing the "default scope" whenever the model is used for queries. For example, let's say you have a User model, and by default, you only want to query against Users that have their
activecolumn set totrue. In order to do this, you could easily provide astatic defaultScopemethod on your model class that would modify the base query. See the following example:Examples:
class User extends Model { static defaultScope(baseQuery) { // `baseQuery` is equal to `User.where` // without a default scope. return baseQuery.active.EQ(true); } }Arguments:
query: QueryEngineThe query that you should add onto.
Return value: class QueryEngine
static method Model::getConnection(connection?: Connection)
Get the underlying connection bound to the model's class. Connection binding is optional, so this method can also be provided a connection. If a connection is provided, then it will simply be returned. Because Mythix ORM has no globals, this is a design pattern to supply a connection if you have one available, or fallback to a "bound" connection (if one can be found). This method should be overloaded if you wish to provide your own connection for a model.
Arguments:
connection?: ConnectionAn optional connection that can be provided. If provided, it will simply be returned. Otherwise, if not provided, or a falsy value, then attempt to fallback to the bound connection, if any is available.
Return value: Connection
static method Model::getForeignKeyFieldsMap(connection?: Connection)
Return the foreign key relationships for the model.
Arguments:
connection?: ConnectionAn optional connection to pass through. This is needed if your models are not bound to a connection.
Return value:
Map<string, array<object>>The format is
Map[modelName] = [ { targetFieldName, sourceFieldName }, ... ]
static method Model::getForeignKeysTargetField(connection?: Connection, modelName: string, fieldName: string)
Get a specific foreign key field's relationship information. This will be what is stored in the relationship field map for the specified target field. See static getForeignKeyFieldsMap for more information.
Arguments:
connection?: ConnectionAn optional connection to pass through. This is needed if your models are not bound to a connection.
modelName:stringThe model name for which you wish to fetch foreign key fields from.
fieldName:stringThe field name for which you wish to fetch foreign key relational information about.
Return value:
object<{ targetFieldName, sourceFieldName }>This will be the relationship info for this foreign key field which will be an object of the shape
{ targetFieldName, sourceFieldName }.
static method Model::getForeignKeysTargetFieldNames(connection?: Connection, modelName: string)
Return all the foreign key target field names. This will return the target field names of all foreign key fields specified on the model.
Arguments:
connection?: ConnectionAn optional connection to pass through. This is needed if your models are not bound to a connection.
modelName:stringThe model name for which you wish to fetch foreign key fields from.
Return value:
Array<string>The format is
Array[] = modelName
static method Model::getForeignKeysTargetModelNames(connection?: Connection)
Return all the foreign key target model names. This will be all models targeted by all foreign keys defined by foreign key fields on this model. This will only return the models name's.
Arguments:
connection?: ConnectionAn optional connection to pass through. This is needed if your models are not bound to a connection.
Return value:
Array<string>The format is
Array[] = modelName
static method Model::getForeignKeysTargetModels(connection?: Connection)
Return all the foreign key target models. This will be all models targeted by all foreign keys defined by foreign key fields on this model.
Arguments:
connection?: ConnectionAn optional connection to pass through. This is needed if your models are not bound to a connection.
Return value:
Map<string, Model>The format is
Map[modelName] = Model
Get the model name for this model. By default this will be the name of the class itself, i.e.
this.name.Return value:
stringThe name the model.
Get the plural model name for this model. By default this will be the singular name of the model, converted to plural using the Inflection library.
Return value:
stringThe name the model in plural form.
Notes:
- This will return the "plural" name of the model.
See also: static getSingularName
static method Model::getQueryEngine(connection?: Connection, options?: object)
This method is called any time a
Model.whereorModel.$property is accessed. It returns a query, based off this model class (as the root model). It will first call static getUnscopedQueryEngine to get the root query for the model, and then it will call static defaultScope to apply any default scope to the root query. Finally, it will return the query to the user to start interacting with.Arguments:
connection?: ConnectionAn optional connection to pass through. This is needed if your models are not bound to a connection.
options?:objectAny extra options to pass to the QueryEngine constructor.
Return value: QueryEngine
static method Model::getQueryEngineClass(connection?: Connection)
This method is called every time a
Model.whereorModel.$property is accessed. It should return a class that inherits from (or is)QueryEngine. By default, it will callthis.getConnection().getQueryEngineClass()to get the query class defined in the connection options. Though this can be overloaded per-model (creating a different type ofQueryEngineper-model), theQueryEngineclass to instantiate to use for queries will generally be supplied by the connection.Arguments:
connection?: ConnectionAn optional connection to pass through. This is needed if your models are not bound to a connection.
Return value: class QueryEngine
Get the model name for this model. By default this will be the name of the class itself, i.e.
this.name.Return value:
stringThe name the model in singular form.
Notes:
- This will return the "singular" name of the model which is the same as static getModelName.
See also: static getPluralModelName
static method Model::getTableName(connection?: Connection)
Get the table name for this model. By default Mythix ORM will take the model's name, and convert it to snake_case.
Arguments:
connection?: ConnectionAn optional connection to pass through. This is needed if your models are not bound to a connection.
Return value:
stringThe name of the table for this model.
static method Model::getUnscopedQueryEngine(connection?: Connection, options?: object)
This method is called any time a
query.unscoped()call is made. It will return the query's "root class"wherewith no static defaultScope scope applied. Calling "unscoped()" will reset the query, so make sure you always call it first:Model.where.unscoped()...Arguments:
connection?: ConnectionAn optional connection to pass through. This is needed if your models are not bound to a connection.
options?:objectAny extra options to pass to the QueryEngine constructor.
Return value: QueryEngine
static method Model::isForeignKeyTargetModel(connection?: Connection, modelName: string)
Check if the specified model is a model pointed to by a foreign key field.
Arguments:
connection?: ConnectionAn optional connection to pass through. This is needed if your models are not bound to a connection.
modelName:stringThe model name for which you wish to fetch foreign key fields from.
Return value:
boolean
trueif the specifiedmodelNamemodel is pointed to by one of the foreign key fields,falseotherwise.
Check to see if the provided value is an instance of a Mythix ORM Model. Unlike static isModelClass, which checks if a class is a Model, this will check to see if an instance is an instance of a Mythix ORM Model. It will return
trueif the provided value is aninstanceofModel, or if the value'sconstructorproperty has a truthy_isMythixModelproperty (value.constructor._isMythixModel).Arguments:
value:anyValue to check.
Return value:
boolean
Use this method to check if a class is a Mythix ORM model. It will return
trueif the provided value is a class that inherits from Model, or if the provided value has an attribute named_isMythixModelthat is truthy.Arguments:
value:FunctionValue to check.
Return value:
boolean
Like everywhere in Javascript, we can call
.toString()to a get a string representation of our Model class. The optionalshowFieldsargument, if true, will list the models fields as well. Without theshowFieldsargument, the model name alone will be returned as a string.Arguments:
showFields?:booleanIf
true, then list the models fields.Return value:
string
- Associations
- Certifications
- Connection Binding
- Home
- Models
- Queries
- TypeScript
- Types Reference
-
namespace AsyncStore
- function getContextStore
- function getContextValue
- function runInContext
- function setContextValue
-
namespace Helpers
- function checkDefaultValueFlags
- function defaultValueFlags
- function getDefaultValueFlags
- property FLAG_LITERAL
- property FLAG_ON_INITIALIZE
- property FLAG_ON_INSERT
- property FLAG_ON_STORE
- property FLAG_ON_UPDATE
- property FLAG_REMOTE
-
namespace MiscUtils
- function collect
- function valueToDateTime
-
namespace ModelUtils
- function parseQualifiedName
-
namespace QueryUtils
- function generateQueryFromFilter
- function mergeFields
- function parseFilterFieldAndOperator
-
class AverageLiteral
- method static isAggregate
- method toString
-
class BigIntType
- property Default
- method castToType
- method constructor
- method isValidValue
- method static getDisplayName
- method toString
-
class BlobType
- method castToType
- method constructor
- method isValidValue
- method static getDisplayName
- method toString
-
class BooleanType
- method castToType
- method isValidValue
- method static getDisplayName
- method toString
-
class CacheKey
- method constructor
- method valueOf
-
class CharType
- method castToType
- method isValidValue
- method static getDisplayName
- method toString
-
class ConnectionBase
- property _isMythixConnection
- property DefaultQueryGenerator
- property dialect
- property Literals
- method _averageLiteralToString
- method _bigintTypeToString
- method _blobTypeToString
- method _booleanTypeToString
- method _charTypeToString
- method _countLiteralToString
- method _datetimeTypeToString
- method _dateTypeToString
- method _distinctLiteralToString
- method _escape
- method _escapeID
- method _fieldLiteralToString
- method _getFromModelCache
- method _integerTypeToString
- method _maxLiteralToString
- method _minLiteralToString
- method _numericTypeToString
- method _realTypeToString
- method _setToModelCache
- method _stringTypeToString
- method _sumLiteralToString
- method _textTypeToString
- method _uuidV1TypeToString
- method _uuidV3TypeToString
- method _uuidV4TypeToString
- method _uuidV5TypeToString
- method _xidTypeToString
- method addColumn
- method addIndex
- method aggregate
- method alterColumn
- method alterTable
- method average
- method buildConnectionContext
- method bulkModelOperation
- method constructor
- method convertDateToDBTime
- method count
- method createContext
- method createQueryGenerator
- method createTable
- method createTables
- method destroy
- method destroyModels
- method dirtyFieldHelper
- method dropColumn
- method dropIndex
- method dropTable
- method dropTables
- method ensureAllModelsAreInstances
- method escape
- method escapeID
- method exists
- method finalizeQuery
- method findModelField
- method getContextValue
- method getDefaultFieldValue
- method getDefaultOrder
- method getField
- method getLockMode
- method getModel
- method getModels
- method getOptions
- method getQueryEngineClass
- method getQueryGenerator
- method insert
- method isStarted
- method literalToString
- method max
- method min
- method parseQualifiedName
- method pluck
- method prepareAllModelsAndSubModelsForOperation
- method prepareAllModelsForOperation
- method query
- method registerModel
- method registerModels
- method runSaveHooks
- method select
- method setContextValue
- method setPersisted
- method setQueryGenerator
- method splitModelAndSubModels
- method stackAssign
- method start
- method static getLiteralClassByName
- method static isConnection
- method static isConnectionClass
- method static Literal
- method stop
- method sum
- method toQueryEngine
- method transaction
- method truncate
- method typeToString
- method update
- method updateAll
- method upsert
-
class CountLiteral
- method static isAggregate
- method static isFieldRequired
- method toString
-
class DateTimeType
- property Default
- method castToType
- method constructor
- method deserialize
- method isValidValue
- method serialize
- method static getDisplayName
- method toString
-
class DateType
- property Default
- method castToType
- method constructor
- method deserialize
- method isValidValue
- method serialize
- method static getDisplayName
- method toString
-
class DistinctLiteral
- method toString
-
class Field
- property _isMythixField
- property allowNull
- property defaultValue
- property fieldName
- property get
- property index
- property primaryKey
- property set
- property type
- property unique
- property validate
- method clone
- method constructor
- method setModel
- method static isField
- method static isFieldClass
-
class FieldLiteral
- method toString
- class FieldScope
-
class ForeignKeyType
- method castToType
- method constructor
- method getOptions
- method getTargetField
- method getTargetFieldName
- method getTargetModel
- method getTargetModelName
- method initialize
- method isValidValue
- method parseOptionsAndCheckForErrors
- method static getDisplayName
- method static isForeignKey
- method toString
-
class IntegerType
- property Default
- method castToType
- method constructor
- method isValidValue
- method static getDisplayName
- method toString
-
class Literal
- method constructor
-
class LiteralBase
- property _isMythixLiteral
- method constructor
- method definitionToField
- method fullyQualifiedNameToDefinition
- method static isAggregate
- method static isLiteral
- method static isLiteralClass
- method static isLiteralType
- method toString
- method valueOf
-
class LiteralFieldBase
- method constructor
- method getField
- method getFullyQualifiedFieldName
- method static isFieldRequired
- method valueOf
-
class MaxLiteral
- method static isAggregate
- method toString
-
class MinLiteral
- method static isAggregate
- method toString
-
class Model
- property _isMythixModel
- method _castFieldValue
- method _constructField
- method _constructFields
- method _constructor
- method _getConnection
- method _getDirtyFields
- method _getFieldValue
- method _initializeFieldData
- method _initializeModelData
- method _setFieldValue
- method clearDirty
- method constructor
- method destroy
- method getAttributes
- method getConnection
- method getDataValue
- method getDirtyFields
- method getOptions
- method hasValidPrimaryKey
- method isDirty
- method isPersisted
- method onAfterCreate
- method onAfterSave
- method onAfterUpdate
- method onBeforeCreate
- method onBeforeSave
- method onBeforeUpdate
- method onValidate
- method reload
- method save
- method setAttributes
- method setDataValue
- method static _getConnection
- method static all
- method static bindConnection
- method static count
- method static create
- method static cursor
- method static defaultScope
- method static finalizeQuery
- method static first
- method static getConcreteFieldCount
- method static getContextValue
- method static getField
- method static getFields
- method static getForeignKeyFieldsMap
- method static getForeignKeysTargetField
- method static getForeignKeysTargetFieldNames
- method static getForeignKeysTargetModelNames
- method static getForeignKeysTargetModels
- method static getModel
- method static getModelContext
- method static getModelName
- method static getPluralModelName
- method static getPrimaryKeyField
- method static getPrimaryKeyFieldName
- method static getQueryEngine
- method static getQueryEngineClass
- method static getSingularName
- method static getSortedFields
- method static getTableName
- method static getUnscopedQueryEngine
- method static getWhereWithConnection
- method static hasField
- method static hasRemoteFieldValues
- method static initializeFields
- method static isForeignKeyTargetModel
- method static isModel
- method static isModelClass
- method static iterateFields
- method static last
- method static mergeFields
- method static pluck
- method static primaryKeyHasRemoteValue
- method static setContextValue
- method static toString
- method static updateModelContext
- method toJSON
- method toString
- method updateDirtyID
-
class ModelScope
- method _getField
- method AND
- method CROSS_JOIN
- method DISTINCT
- method EXISTS
- method Field
- method FULL_JOIN
- method GROUP_BY
- method HAVING
- method INNER_JOIN
- method JOIN
- method LEFT_JOIN
- method LIMIT
- method mergeFields
- method NOT
- method OFFSET
- method OR
- method ORDER
- method PROJECT
- method RIGHT_JOIN
-
class ModelType
- method fieldNameToOperationName
- method initialize
-
class ModelsType
- method fieldNameToOperationName
- method initialize
-
class NumericType
- method castToType
- method constructor
- method isValidValue
- method static getDisplayName
- method toString
-
class ProxyClass
- property APPLY
- property AUTO_CALL
- property AUTO_CALL_CALLED
- property AUTO_CALL_CALLER
- property CALLABLE
- property CONSTRUCT
- property DEFINE_PROPERTY
- property DELETE_PROPERTY
- property GET
- property GET_OWN_PROPERTY_DESCRIPTOR
- property GET_PROTOTYPEOF
- property HAS
- property IS_EXTENSIBLE
- property MISSING
- property OWN_KEYS
- property PREVENT_EXTENSIONS
- property PROXY
- property SELF
- property SET
- property SET_PROTOTYPEOF
- property shouldSkipProxy
- property TARGET
- method ___autoCall
- method ___call
- method constructor
-
class QueryEngine
- method all
- method average
- method constructor
- method count
- method cursor
- method destroy
- method exists
- method finalizeQuery
- method first
- method getFieldScopeClass
- method getModelScopeClass
- method last
- method max
- method MERGE
- method min
- method Model
- method pluck
- method sum
- method toString
- method unscoped
- method updateAll
-
class QueryEngineBase
- method _fetchScope
- method _inheritContext
- method _newFieldScope
- method _newModelScope
- method _newQueryEngineScope
- method _pushOperationOntoStack
- method clone
- method constructor
- method filter
- method getAllModelsUsedInQuery
- method getConnection
- method getFieldScopeClass
- method getModel
- method getModelScopeClass
- method getOperationContext
- method getOperationStack
- method getQueryEngineClass
- method getQueryEngineScope
- method getQueryEngineScopeClass
- method getQueryID
- method isLastOperationCondition
- method isLastOperationControl
- method isModelUsedInQuery
- method logQueryOperations
- method map
- method queryHasConditions
- method queryHasJoins
- method static generateID
- method static getQueryOperationInfo
- method static isQuery
- method static isQueryOperationContext
- method walk
-
class QueryGeneratorBase
- method _averageLiteralToString
- method _countLiteralToString
- method _distinctLiteralToString
- method _fieldLiteralToString
- method _maxLiteralToString
- method _minLiteralToString
- method _sumLiteralToString
- method constructor
- method escape
- method escapeID
- method getConnection
- method getFieldDefaultValue
- method getIndexFieldsFromFieldIndex
- method setConnection
- method stackAssign
- method toConnectionString
-
class RealType
- method castToType
- method constructor
- method isValidValue
- method static getDisplayName
- method toString
-
class SerializedType
- method castToType
- method constructor
- method deserialize
- method getOptions
- method initialize
- method isDirty
- method isValidValue
- method onSetFieldValue
- method serialize
- method static getDisplayName
- method toString
-
class StringType
- method castToType
- method constructor
- method isValidValue
- method static getDisplayName
- method toString
-
class SumLiteral
- method static isAggregate
- method toString
-
class TextType
- method castToType
- method constructor
- method isValidValue
- method static getDisplayName
- method toString
-
class Type
- property _isMythixFieldType
- property clone
- method castToType
- method clone
- method constructor
- method deserialize
- method exposeToModel
- method getDisplayName
- method getField
- method getModel
- method initialize
- method isDirty
- method isForeignKey
- method isRelational
- method isRemote
- method isValidValue
- method isVirtual
- method onSetFieldValue
- method serialize
- method setField
- method setModel
- method static instantiateType
- method static isSameType
- method static isType
- method static isTypeClass
- method static wrapConstructor
- method toConnectionType
-
class UUIDV1Type
- property Default
- method castToType
- method getArgsForUUID
- method isValidValue
- method static getDisplayName
- method validateOptions
-
class UUIDV3Type
- property Default
- method castToType
- method getArgsForUUID
- method isValidValue
- method static getDisplayName
- method validateOptions
-
class UUIDV4Type
- property Default
- method castToType
- method getArgsForUUID
- method isValidValue
- method static getDisplayName
- method validateOptions
-
class UUIDV5Type
- property Default
- method castToType
- method getArgsForUUID
- method isValidValue
- method static getDisplayName
- method validateOptions
-
class XIDType
- property Default
- method castToType
- method isValidValue
- method static getDisplayName