-
Notifications
You must be signed in to change notification settings - Fork 0
ForeignKeyType
class ForeignKeyType extends Type 📜
FOREIGN_KEY type.
This represents a foreign key to another column. It takes on the type of the column it points to. For example, if it points to an INTEGER column, then it will also be the same type of integer.
Foreign keys are one of the ways Mythix ORM knows that models
are related, along with the ModelType and ModelsType virtual types.
The "target" field must be a fully qualified field name. A
fully qualified field name is a name that is also prefixed by
the model that owns it. For example, 'User:firstName' would
be a fully qualified field name, but 'firstName' would not be.
Client-side storage for this field will be backed by the same type as the field the foreign key targets.
Example:
-
class Role extends Model { static fields = { userID: { type: Types.FOREIGN_KEY('User:id', { onDelete: 'CASCADE', onUpdate: 'CASCADE', }), allowNull: false, }, roleMetaID: { type: Types.FOREIGN_KEY({ modelName: 'RoleMeta', fieldName: 'id', onDelete: 'CASCADE', onUpdate: 'CASCADE', }), allowNull: false, }, }; }
See also: Type
method ForeignKeyType::castToType(
context: CastToTypeContext,
): any 📜
Cast provided value to underlying type.
This will cast the incoming value to the underlying type of this field, which is the type of the field that it targets.
See Type.castToType for a more detailed description.
Arguments:
-
context: CastToTypeContext
Return value: any
Return the incoming value, cast to this
type. null and undefined are simply
returned without casting.
method ForeignKeyType::constructor(
fullyQualifiedName: string | object,
options?: object,
): ForeignKeyType 📜
Construct a new ForeignKeyType type.
This constructor has two call patterns.
First, it can be called the shorthand way
with a string (a fully qualified field name)
as the first argument, and the field options
as the second argument. Or, you can simply pass
a single options argument, that must contain
a modelName and fieldName property that are
strings.
Interface:
-
interface ForeignKeyTypeOptions { // A direct field from a model, used for the target field // directly. i.e. `Field: User.fields.id`. // If this is present and valid then it // will supersede the `modelName` and `fieldName` // properties. Field?: Field; // The model name that owns the target field modelName?: string; // The field name of the target field on the // target `modelName`. fieldName?: string; // onDelete specifies what action to take when // a row in the target table is deleted. onDelete?: 'CASCADE' | 'SET NULL' | 'NO ACTION' | 'SET DEFAULT' | 'RESTRICT' // onUpdate specifies what action to take when // a row in the target table is updated // (specifically the target column). onUpdate?: 'CASCADE' | 'SET NULL' | 'NO ACTION' | 'SET DEFAULT' | 'RESTRICT' }
Arguments:
-
fullyQualifiedName:string|object- The fully qualified name of the target field as
a string (i.e.
'User:id'), optionally followed anoptionsargument, or 2) the one and onlyoptionsargument requiringmodelNameandfieldNameproperties.
- The fully qualified name of the target field as
a string (i.e.
-
options?:objectIf
fullyQualifiedNameis defined as a string, then this will be the options to the field.
Return value: ForeignKeyType
A new instance of the ForeignKeyType type.
method ForeignKeyType::getOptions(): object 📜
Get the options that were passed to the
ForeignKeyType on creation.
Return value: object
The options object that was given to the field.
method ForeignKeyType::getTargetField(
connection?: Connection,
): Field 📜
Fetch the target field. You can always access
the owning model via the field.Model property
on the field.
Notes:
- If this is called before any model has been initialized,
or before a connection has been bound to your models, then
you must provide a
connectionor this method will fail.
Arguments:
-
connection?: ConnectionA database connection, which will be used to fiend the target field.
Return value: Field
The target field from the target model.
method ForeignKeyType::getTargetFieldName(
connection?: Connection,
): string 📜
Fetch the target field's name.
Notes:
- If this is called before any model has been initialized,
or before a connection has been bound to your models, then
you must provide a
connectionor this method will fail.
Arguments:
-
connection?: ConnectionA database connection, which will be used to fiend the target field.
Return value: string
The target field's name.
method ForeignKeyType::getTargetModel(
connection?: Connection,
): ModelClass 📜
Fetch the target model, which is the model owning the target field.
Notes:
- If this is called before any model has been initialized,
or before a connection has been bound to your models, then
you must provide a
connectionor this method will fail.
Arguments:
-
connection?: ConnectionA database connection, which will be used to fiend the target field and owning model.
Return value: ModelClass
The target field's owning model.
method ForeignKeyType::getTargetModelName(
connection?: Connection,
): string 📜
Fetch the name of target model, which is the model owning the target field.
Notes:
- If this is called before any model has been initialized,
or before a connection has been bound to your models, then
you must provide a
connectionor this method will fail.
Arguments:
-
connection?: ConnectionA database connection, which will be used to fiend the target field and owning model.
Return value: string
The target field's owning model name.
method ForeignKeyType::initialize(
connection: Connection,
self: Model,
): undefined 📜
Initialize a model instance against this type.
Initialize is called whenever a model instance is
created. Note that the type instance is shared
across all model instances. initialize is still
called for every model instance that is created however,
because the type class can modify the model it
exists on. For example, the ModelType and ModelsType types
inject custom relational methods onto each model instance.
This specific initialize for the ForeignKeyType
only runs once however. The first owning model instance
that is created will call this method, and at that point
the target field will be looked up and found. After this
first lookup, the target field will be cached for all
future model instances of the same type.
Arguments:
-
connection: ConnectionThe database connection of the calling model instance.
-
self:ModelThe actual model instance that is calling this method.
Return value: undefined
method ForeignKeyType::isValidValue(
value: any,
): boolean 📜
Check if the provided value is valid.
This will check if the provided value valid.
It does so by calling the isValidValue of
the type that it inherits from its target
field.
Arguments:
-
value:anyThe value to check.
Return value: boolean
method ForeignKeyType::parseOptionsAndCheckForErrors(
SourceModel: ModelClass,
sourceField,
connection: Connection,
): { Model: ModelClass, Field: Field } 📜
This is called when the field is first initialized on any model instance. It will verify the options provided to the field, and ensure that it can find the target field requested. If it can't find the target field requested for any reason, then it will throw an exception.
Arguments:
-
SourceModel:ModelClassThe source model (model owning this foreign key field).
-
sourceFieldThe source field (the field containing this foreign key type).
-
connection: ConnectionThe database connection. This is needed to fetch the target field and model.
Return value: { Model: ModelClass, Field: Field }
Return the target model and field.
static method ForeignKeyType::getDisplayName(): string 📜
Get the "display" name for this type.
This method is called from Model.toString when stringifying the model for representation.
Notes:
- This is also an instance method that can be called from an instance of the type.
Return value: string
Return the string value 'FOREIGN_KEY'
static method ForeignKeyType::isForeignKey(): boolean 📜
Check if this is a foreign key type.
There are multiple different "type checking"
static methods that exist for all field types.
This is one of them. It checks if the type
class is a foreign key type. This returns
true for FOREIGN_KEY.
Return value: boolean
Return true, as the FOREIGN_KEY type is a
foreign key field.
method ForeignKeyType::toString(
connection?: Connection,
): string 📜
Stringify the type itself. This will be the type of the target field.
If a connection argument is provided, then this
will go through the connection to generate the type
for the underlying database. If no connection is
provided, then a "standard" SQL type will be returned
for target field type instead.
Arguments:
-
connection?: ConnectionAn optional connection. If provided, send this type through Type.toConnectionType to have the connection itself generate the underlying type for the database. If
connectionis not provided, then this will simply return a "standard" generic matching SQL type of the target field type.
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