-
Notifications
You must be signed in to change notification settings - Fork 0
SerializedType
A field type that serializes and deserializes raw objects.
The SERIALIZED type can be used to serialize
other data into a single field value. By default this
field type is configured to serialize JSON.
However, it can be configured to serialize and
deserialize in any encoding you desire.
Something important to note right up front is that
this type hooks into the model instance's "dirty" system,
and will call your provided serialize and deserialize
methods to check if your object has internally become
dirty. For example, if you use this type for a field called
metadata on your User model, and you have an instance
of that user model you are working with, and you internally
change the metadata field like so user.metadata.class = 'wizard';
then this field type will detect that the metadata attribute
of your model is "dirty" by serializing the metadata value
and comparing it to the "undirty" serialized value, even if
the actual value of the metadata field itself hasn't changed.
For this reason it is important that your serializer sort
the serialized data if possible. If sorting isn't possible,
then the side-effect will be that this field may be serialized
and stored more often than actually needed. This system was
designed this way because it is likely better to get a false
positive and save your data, then not check at all and lose data.
If needed, you can always create your own child type of
SerializedType, and overload the isDirty method, or provided
an isDirty method to the field options to check
for dirtiness yourself, based on what makes sense with your
serializing algorithm.
method SerializedType::castToType(context: CastToTypeContext): any | null | undefined
Cast provided value to underlying type.
This will cast the incoming value by calling SerializedType.deserialize on the type with the provided value. If SerializedType.deserialize detects that the type is the same as the underlying storage type (i.e.
STRING), then it will deserialize and return the value. However, if the provided value is not the type of the underlying storage field type, and the value is notnullorundefined, then it will be assumed to already be deserialized and will be simply returned.See Type.castToType for a more detailed description.
Arguments:
context: CastToTypeContextReturn value:
any|null|undefinedReturn the incoming
value, cast to this type (deserialized).nullandundefinedare simply returned without casting by the default built-in JSON deserializer... however, they may be treated differently for other serializers.
Construct a new instance of the
SerializedTypetype.Interfaces:
interface SerializeCallContext { // The value to serialize or deserialize value: any; // The database driver connection. connection: Connection; } interface SerializedTypeOptions { // Method to serialize data. Note how the // return type is `any`. Though commonly an // underlying `STRING` type will be used for // the field value, that isn't a requirement. // For example, you might want to encode a bitmask // into a `BIGINT`. The world is your oyster. // // Unlike other fields, `null` and `undefined` are // not simply returned. They are left up to the // serializer and deserializer to handle and do // with as they want. For this reason, you need // to properly handle `null` and `undefined` values, // even if you just return them (allowing the underlying // database field to contain a `NULL` value). serialize?: (context: SerializeCallContext) => any; // Deserialize the data provided. deserialize?: (context: SerializeCallContext) => any; // The underlying storage type of this field. This will // commonly be a `STRING` of some larger size, but isn't // required to be. It can be any type that works for your // serializer and deserializer. type: Type // Provide your own method to detect // "dirtiness" of the field's value // See the "isDirty" method of this // class for more information. isDirty?: (context: DirtyFieldValueContext) => any; }Arguments:
options?:Type|SerializedTypeOptionsIf this is a
Type, then it will specify the underlying field type that will store the serialized value, and is the same as specifying the{ type: Type }option.Return value:
SerializedType
method SerializedType::deserialize(value: any, connection?: Connection): any
Deserialize the field's value, and return the deserialized result.
This is called any time
castToTypeis called. If the value provided is not the same type as the underlying storage type (i.e.STRING), and the value is also notnullorundefined, then the value is simply returned. This is because it is assumed if the value isn't of the same type as storage (i.e.stringtype), then it is already deserialized, so will simply be returned.nullandundefinedare handed directly off to the providedserializeanddeserializemethods, so make sure your serializer can properly handle these values (even if that means simply returning them).Arguments:
value:anyThe value to deserialize. If this is not the same type as the underlying storage type, and is also not
nullorundefined, then the value will simply be returned.connection?: ConnectionThe database driver connection requesting that this field's value be deserialized (usually during load).
Return value:
anyThe underlying field's value. If the value provided has a type that matches the underlying storage type (i.e. typeof value === 'string' && internalType === 'STRING') then the value will first be passed through the provided
deserializemethod before it is returned.
Get the options passed to the field when it was defined.
Return value:
objectThe options object provided to the field.
Call initialize on the underling type field.
Arguments:
connectionselfSee also: Type.initialize
Check if this field is dirty, including if the object value stored on the field has changed since the last persist.
This compares the current value of the field to the last persisted value by serializing the field's value and comparing it to the last serialized value when the field was persisted/loaded.
Interface:
interface DirtyFieldValueContext { // The current field value to check. value: any; // The field that contains this type field: Field; // The name of the field that contains this type fieldName: string; // The model instance itself self: Model; // The underlying database connection // that is attempting to insert or update // this field connection: Connection; }Arguments:
context:DirtyFieldValueContextReturn value:
anyIf
undefinedis returned, then this field is not considered dirty. If any other value is returned, then that will be used as the "dirty" value for the field.
Validate the underlying field value. For the
SerializedTypethis simply always returnstrue.Return value:
booleanAlways return
true.
This is called whenever the model's field owning this type is changed. This method will update the "dirty" serialized cache contained on the model for this type. This is later used to see if the field value is dirty because any of the internal data of the field value has changed (via a serialize call that compares the value to last serialize).
In short, this updates the last serialize cache to compare it with a serialize on insert/update to see if the serialized data is "dirty".
Interface:
interface SetFieldValueContext { // The field value that was just set onto the field. value: any; // The field that contains this type field: Field; // The name of the field that contains this type fieldName: string; // The model instance itself self: Model; }Notes:
- Because type instances for fields are shared across model instances, the actual "dirty" cache is stored on the model itself in a
_typeDataproperty on the model instance.Arguments:
context:SetFieldValueContextReturn value:
undefined
method SerializedType::serialize(value: any, connection?: Connection): any
Serialize the field's value, and return the serialized result.
Note that if no
connectionis provided the current field's value is simply returned. This is because it is assumed that serializing will only be happening when the database driver connection requests it, otherwise you likely just want the raw value of the field.Arguments:
value:anyThe value to serialize. If no
connectionargument is provided, then this value will simply be returned.connection?: ConnectionThe database driver connection requesting that this field's value be serialized for storage.
Return value:
anyThe underlying field's value if no
connectionargument was provided, or the serialized result if aconnectionargument was provided.
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:
stringReturn the string value
'SERIALIZED'
method SerializedType::toString(connection?: Connection): string
Stringify the type itself. This will be the type of the underlying storage field.
If a
connectionargument 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 underlying storage 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 underlying storage 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