mysql-json-table-0.1.4.1: Using MySQL to store id-to-json tables.
Safe HaskellNone
LanguageHaskell2010

Database.MySQL.JSONTable

Description

Interface to use a MySQL table with a very specific format, where each row consists of a row identifier - used for lookups - and a JSON-encoded value.

iddata
Row identifier (type Id)JSON-encoded value
Synopsis

JSON tables

Types

data Id a Source #

Row identifier used for table lookups. The type parameter indicates the type of data stored in the table.

Instances

Instances details
FromJSON (Id a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

FromJSONKey (Id a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

ToJSON (Id a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

Methods

toJSON :: Id a -> Value #

toEncoding :: Id a -> Encoding #

toJSONList :: [Id a] -> Value #

toEncodingList :: [Id a] -> Encoding #

omitField :: Id a -> Bool #

ToJSONKey (Id a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

Show (Id a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

Methods

showsPrec :: Int -> Id a -> ShowS #

show :: Id a -> String #

showList :: [Id a] -> ShowS #

Eq (Id a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

Methods

(==) :: Id a -> Id a -> Bool #

(/=) :: Id a -> Id a -> Bool #

Ord (Id a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

Methods

compare :: Id a -> Id a -> Ordering #

(<) :: Id a -> Id a -> Bool #

(<=) :: Id a -> Id a -> Bool #

(>) :: Id a -> Id a -> Bool #

(>=) :: Id a -> Id a -> Bool #

max :: Id a -> Id a -> Id a #

min :: Id a -> Id a -> Id a #

Hashable (Id a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

Methods

hashWithSalt :: Int -> Id a -> Int #

hash :: Id a -> Int #

FromHttpApiData (Id a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

ToHttpApiData (Id a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

Param (Id a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

Methods

render :: Id a -> Action #

ToField (Id a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

Methods

toField :: Id a -> ByteString #

FromField (Id a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

Methods

fromField :: ([Type], ByteString -> Either String (Id a)) #

Typeable a => Result (Id a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

Methods

convert :: Field -> Maybe ByteString -> Id a #

data Row a Source #

A single row.

Constructors

Row 

Fields

Instances

Instances details
FromJSON a => FromJSON (Row a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

ToJSON a => ToJSON (Row a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

Methods

toJSON :: Row a -> Value #

toEncoding :: Row a -> Encoding #

toJSONList :: [Row a] -> Value #

toEncodingList :: [Row a] -> Encoding #

omitField :: Row a -> Bool #

Show a => Show (Row a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

Methods

showsPrec :: Int -> Row a -> ShowS #

show :: Row a -> String #

showList :: [Row a] -> ShowS #

Eq a => Eq (Row a) Source # 
Instance details

Defined in Database.MySQL.JSONTable

Methods

(==) :: Row a -> Row a -> Bool #

(/=) :: Row a -> Row a -> Bool #

data JSONTable a Source #

A MySQL table with two columns:

iddata
Row identifier (type Id)JSON data

The type parameter indicates the type of data stored in the table.

Constructors

JSONTable 

Fields

Connections

defaultConnectInfo :: ConnectInfo #

Default information for setting up a connection.

Defaults are as follows:

  • Server on localhost
  • User root
  • No password
  • Database test
  • Character set utf8

Use as in the following example:

connect defaultConnectInfo { connectHost = "db.example.com" }

data Connection #

Connection to a MySQL database.

withSQL :: (MonadMask m, MonadIO m) => ConnectInfo -> (Connection -> m a) -> m a Source #

Open a connection to a MySQL server and apply a function to it. The connection is closed both when the function completes or throws an exception.

Table operations

createTable Source #

Arguments

:: Connection

MySQL database connection.

-> Bool

Fail if table already exists.

-> String

Table name.

-> IO (JSONTable a) 

Create a new JSON table in a MySQL database.

deleteTable Source #

Arguments

:: Connection

MySQL database connection.

-> Bool

Fail if table doesn't exist.

-> JSONTable a 
-> IO () 

Delete a JSON table from a MySQL database, together with all of its content.

Row operations

insert Source #

Arguments

:: ToJSON a 
=> Connection

MySQL database connection.

-> JSONTable a

Table to insert the new row.

-> a

Data for the new row.

-> IO (Id a)

Identifier of the new row.

Insert a new row into a table.

Warning: It is recommended not to call insert with the same Connection argument from multiple threads. The Ids returned might get mixed up. If you need to call insert from multiple threads, use a different Connection on each thread.

lookup Source #

Arguments

:: (Typeable a, FromJSON a) 
=> Connection

MySQL database connection.

-> JSONTable a

Table for lookup.

-> Id a

Identifier to use for the table lookup.

-> IO (Maybe a) 

Lookup a row in a table.

adjust Source #

Arguments

:: (Typeable a, FromJSON a, ToJSON a) 
=> Connection

MySQL database connection.

-> JSONTable a 
-> (a -> IO a)

Update function.

-> Id a 
-> IO () 

Update a row by applying the supplied function. If the row doesn't exist, it does nothing.

delete Source #

Arguments

:: Connection

MySQL database connection.

-> JSONTable a

Table to delete the row from.

-> Id a

Identifier of the row to delete.

-> IO () 

Delete a row from a table. It does nothing if the row doesn't exist.

replace Source #

Arguments

:: ToJSON a 
=> Connection

MySQL database connection.

-> JSONTable a 
-> Id a

Row identifier.

-> a

New value.

-> IO () 

Replace the current value of a row. It does nothing if the row doesn't exist.

Streaming

sourceRows Source #

Arguments

:: (Typeable a, FromJSON a) 
=> Connection

MySQL database connection.

-> JSONTable a

Table to stream rows from.

-> ConduitT i (Row a) (ResourceT IO) () 

Stream all rows using a conduit.

Id tables

Types

data IdTable key a Source #

Table that stores a map from keys to row identifiers from some JSONTable. It has the following shape:

keyid
User-provided key typeRow identifier (type Id)

Constructors

IdTable 

Fields

Table operations

createIdTable Source #

Arguments

:: FromField key 
=> Connection

MySQL database connection.

-> Bool

Fail if table already exists.

-> String

Table name.

-> IO (IdTable key a) 

Create a new Id table in a MySQL database.

The type of the key column will be set to the first type listed in fromField.

deleteIdTable Source #

Arguments

:: Connection

MySQL database connection.

-> Bool

Fail if table doesn't exist.

-> IdTable key a 
-> IO () 

Delete an Id table from a MySQL database, together with all of its content.

Row operations

insertId :: ToField key => Connection -> IdTable key a -> key -> Id a -> IO () Source #

Insert a new Id into an Id table.

lookupId :: (ToField key, Typeable a) => Connection -> IdTable key a -> key -> IO (Maybe (Id a)) Source #

Id table lookup.

adjustId Source #

Arguments

:: (ToField key, Typeable a) 
=> Connection

MySQL database connection.

-> IdTable key a 
-> (Id a -> IO (Id a))

Update function.

-> key 
-> IO () 

Update an Id by applying the supplied function. If the key is not found, it does nothing.

alterId Source #

Arguments

:: (ToField key, Typeable a) 
=> Connection

MySQL database connection.

-> IdTable key a 
-> (Maybe (Id a) -> IO (Maybe (Id a)))

Update function.

-> key 
-> IO () 

Alter an Id by applying the supplied function, either inserting it, removing it, or updating it.

deleteId :: ToField key => Connection -> IdTable key a -> key -> IO () Source #

Delete an Id from and Id table. It does nothing if the key is not found.

replaceId :: ToField key => Connection -> IdTable key a -> key -> Id a -> IO () Source #

Replace the Id associated to the given key. It does nothing if the key isn't found.

moveId Source #

Arguments

:: (ToField key, Typeable a) 
=> Connection 
-> IdTable key a 
-> key

Original key

-> key

New key

-> IO () 

Move an Id from one key to another. This fails if the original key doesn't exist or the target key already exists.

Streaming

sourceIds Source #

Arguments

:: (Typeable key, FromField key, Typeable a) 
=> Connection

MySQL database connection.

-> IdTable key a

Table to stream ids from.

-> ConduitT i (key, Id a) (ResourceT IO) () 

Stream all ids using a conduit.