beam-migrate-0.2.0.0: SQL DDL support and migrations support library for Beam

Database.Beam.Migrate.Serialization

Description

Serialization and deserialization helpers for beam data types.

Synopsis

# Serialization helpers

Below we provide various instances of Beam SQL syntax types that produce an aeson Value that reflects the call tree. This allows us to read back these data types in various syntaxes.

Because these are formatted as standard beam syntaxes, backends can easily serialize their data to disk. For an example of what we mean by this, see the instance of IsSql92DataTypeSyntax for SqliteDataTypeSyntax in beam-sqlite.

An IsSql92DataTypeSyntax for JSON. Supports all superclasses of IsSql92DataTypeSyntax declared in beam-core.

Constructors

 BeamSerializedDataType FieldsfromBeamSerializedDataType :: Value

IsSql92MatchTypeSyntax type for JSON

Constructors

 BeamSerializedMatchType FieldsfromBeamSerializedMatchType :: Value

Instances

 Source # Methods Source # Methods Source #

IsSql92ReferentialActionSyntax type for JSON

Constructors

 BeamSerializedReferentialAction FieldsfromBeamSerializedReferentialAction :: Value

IsSql92ExpressionSyntax is too complex for us to store in JSON. Additionally, many backends provide substantial amounts of extensions to the syntax that would make storing this highly unfeasible. Expressions are therefore represented as their full text rendering.

This means that expressions only match as equal if they match exactly. While this may seem overly pedantic, it's not much of a concern if your migrations are generated solely by beam-migrate. If you've modified the schema yourself, you may have to use IsCustomSqlSyntax to provide an exact expression.

Constructors

 BeamSerializedExpression FieldsfromBeamSerializedExpression :: Text

Instances

 Source # Methods Source # Methods

Some backends serialize data that can only be read by that backend. If so, they should wrap these data in beamSerializeJSON, which provides a standard syntax for specifying backend specific data, as well as which backend the data are valid for.

The first argument is a string that is unique to a given backend

Helper for serializing the precision and decimal count parameters to decimalType, etc.

# Deserialization helpers

Deserialization requires that knowledge of every type of data we can deserialize is stored in one place. While this is not much of an issue when compiling full Haskell applications, due to the type class mechanism, beam-migrate tools load backends dynamically. This means that we need a separate way to discover deserialization instances for types we care about.

Values of the BeamDeserializers type represent a set of deserializers all related to one kind of command syntax. You can ask for the deserializers to deserialize any type from an aeson Value. The deserialization will succeed only if a deserializer for the requested type exists and the deserializer was able to parse the Value.

BeamDeserializers compose monoidally. Thus, you can extend any BeamDeserializers with your own custom deserializers, by mappending it with a new BeamDeserializers, created by calling beamDeserializer.

newtype BeamDeserializers cmd Source #

Provides a collection of deserializers from aeson Values for arbitrary types. The cmd type parameter is a phantom type parameter. Notionally, all deserializers within this BeamDeserializers relate to the cmd syntax.

Constructors

 BeamDeserializers FieldsbeamArbitraryDeserializers :: DMap BeamDeserializerLabel BeamDeserializer

Instances

 Source # Methodsmappend :: BeamDeserializers cmd -> BeamDeserializers cmd -> BeamDeserializers cmd #mconcat :: [BeamDeserializers cmd] -> BeamDeserializers cmd #

beamDeserialize :: forall a cmd. Typeable a => BeamDeserializers cmd -> Value -> Parser a Source #

Deserialize the requested type from the given deserializers and aeson Value.

Helper function to deserialize data from a Maybe Value.

beamDeserializeMaybe _ Nothing = pure Nothing
beamDeserializeMaybe d (Just v) = Just \$ beamDeserialize d v


beamDeserializer :: Typeable ty => (forall cmd'. BeamDeserializers cmd' -> Value -> Parser ty) -> BeamDeserializers cmd Source #

sql92Deserializers :: forall cmd. IsSql92DdlCommandSyntax cmd => BeamDeserializers cmd Source #

Deserializers for SQL92 syntaxes

Deserializes data types that are instances of IsSql99DataTypeSyntax

Deserialize data types that are instances of IsSql2003BinaryAndVarBinaryDataTypeSyntax

Deserialize data types that are instance of IsSql2008BigIntDataTypeSyntax