Skip to content

Latest commit

 

History

History
479 lines (362 loc) · 15.2 KB

documentation.md

File metadata and controls

479 lines (362 loc) · 15.2 KB

Transactional DB Documentation

Modules:

Click a module name below to see its documentation

Transactional

Written By: Joel Dentici

Written On: 6/20/2017

The transactional library provides a mechanism for performing database transactions on an RDBMS from a node.js application.

This library only implements a driver for MySQL right now, but other drivers can be registered by calling the register function. Drivers are responsible for adapting to the interface used by this library.

create :: String → int → Object → (IDBH → IDBH) → DBHManager

Uses the registered database driver creation function to create a database manager. An optional logger can be specified.

drivers :: Map String (int → Object → DriverFuncs)

Store references to driver creation functions

register :: String → (int → Object → DriverFuncs) → ()

Registers a database driver to be used to create a database manager at a later time.

Transactional.DBHManager

Written By: Joel Dentici

Written On: 6/20/2017

Provides methods to manage a set of connections to a database, including creating connections, retrieving a connection from a pool, and performing an atomic transaction with a connection retrieved from the pool.

Does not provide pooling capabilities on its own. It instead relies on provided functions to interact with the pool. May provide a default pool implementation at some point.

close :: DBHManager → ()

Closes the connection pool. After calling this method, the object should no longer be used.

createConnection :: DBHManager → Async DBError RecordMapper

Creates a new connection that is not from the connection pool. A promise for the connection is returned. You are responsible for closing the connection.

createMapper :: IDBH → RecordMapper

Wraps a connection in a record mapper.

createPooled :: DBHManager → (IDBH → RecordMapper) → RecordMapper

Wraps a connection in a pooled connection, then wraps that in a record mapper to provide additional functionality.

getConnection :: DBHManager → Async DBError RecordMapper

Returns a promise for a connection from the connection pool. The connection is already open.

new :: DriverFuncs → (IDBH → IDBH) → DBHManager

Constructs a new DBHManager with the provided functions to create connections, get connections from a connection pool, and to return connections to the pool, and shutdown the pool.

createLogger (possibly) wraps a connection in a logger

The provided functions must take and return IDBH implementations.

toPool is guaranteed to receive the same type of IDBH that fromPool provides.

runTransaction :: DBHManager → Free Transaction a → Async DBError a

Convenience method to interpret transactions. The transaction is a Free monad whose values can be of type Async or Transaction.

The transaction is interpreted to an Async. You can use Free.Control primitives as well as Transaction instructions.

For most applications, this should not be used -- instead create and use your own composite interpreter including the transactional interpreter and run the resulting Async at the root of your application.

Transactional.IBus

Written By: Joel Dentici

Written On: 6/20/2017

An interface for publishing to an Event Bus.

publish :: IBus → String → Object → ()

Publishes an event of the given type with the given data to the bus. Returns immediately.

Transactional.IDBH

Written By: Joel Dentici

Written On: 6/20/2017

An interface to a database handle for a relational database.

beginTransaction :: IDBH → Async DBError ()

Begins a transaction. Returns a promise for the result of beginning the transaction.

close :: IDBH → ()

Closes the database connection.

commit :: IDBH → Async DBError ()

Commits the transaction. Returns a promise for the result.

connect :: IDBH → Async DBError ()

Connects to the database. Returns a promise for the result of the connection.

prepare :: IDBH → String → Async DBError (IStatement ([Object] | int | ()))

Prepares a statement to run on the database. Returns a promise for a statement handle. The statement must be disposed of when the connection is closed.

rollback :: IDBH → Async DBError ()

Rolls back the transaction. Returns a promise for when rollingback finishes.

status$ :: IDBH → Observable DBStatus DBFatalError

Returns a stream of status events.

The only required functionality is that when the connection is closed (in any possible way), the stream returned by this method is ended.

Transactional.IStatement

Written By: Joel Dentici

Written On: 6/20/2017

An interface to a statement handle for a relational database.

destroy :: IStatement ([Object] | int | ()) → ()

Cleans up underlying resources associated with the statement handle.

execute :: IStatement ([Object] | int | ()) → ...any → Async DBError ([Object] | int | ())

Executes the prepared statement with the provided placeholder bindings. Returns a promise with the result of the query.

The result type will depend on the type of statement that is executed. If it is a Select Query, then a list of objects, representing the rows selected is returned. If it is an Insert Statement, then the ID of the new row is returned. If it is any other kind of statement, then nothing is returned.

Transactional.LogDBH

Written By: Joel Dentici

Written On: 6/20/2017

Adds logging to connections, so that the queries ran on them are written to some medium.

beginTransaction :: IDBH → Async DBError ()

Begins a transaction on the base IDBH.

close :: IDBH → ()

Closes the base connection.

commit :: IDBH → Async DBError ()

Commits the transaction on the base IDBH

connect :: IDBH → Async DBError ()

Connects the base IDBH.

ConsoleMedia :: () → Media

Medium that writes to the console.

FileMedia :: string → Media

Medium that writes to a file

ListMedia :: () → Media

Medium that writes to a list

log :: IDBH → IDBH

Creates an IDBH that writes to the closed over media.

makeLogger :: Media → (IDBH → IDBH)

Returns a function that will wrap a provided connection in a logger that writes to the provided media.

makeStmt :: IStatement → IStatement

Writes the query, with parameters substituted for arguments, to the media, then executes underlying statement.

makeWrapper :: string → Media → (IStatement → IStatement)

Returns a function that will wrap a prepared statement to write to the provided media, with the provided query.

MultiMedia :: [Media] → Media

Medium that writes to underlying media

new :: IDBH → Media → ()

Constructs a new IDBH that writes queries to some media before calling the same methods on the base connection.

nolog :: IDBH → IDBH

The identity function by another name.

prepare :: IDBH → String → Async DBError (IStatement ([Object] | int | ()))

Prepares a statement on the base IDBH.

replaceParameters :: string → ...any → string

Replaces the parameters of a prepared statement with the binded arguments to execute it with. This is for display purposes only and does not escape the arguments.

rollback :: IDBH → Async DBError ()

Rolls back the transaction on the base IDBH

status$ :: IDBH → Observable DBStatus DBFatalError

Returns a stream of status events for both the pooled connection and underlying IDBH.

Transactional.MySQLDBH

Written By: Joel Dentici

Written On: 6/20/2017

Adapts node-mysql to use the IDBH interface.

beginTransaction :: MySQLDBH → Async DBError ()

Begins a transaction. Returns a promise for the result of beginning the transaction.

close :: MySQLDBH → ()

Closes the database connection.

commit :: MySQLDBH → Async DBError ()

Commits the transaction. Returns a promise for the result.

connect :: MySQLDBH → Async DBError ()

Connects to the database. Returns a promise for the result of the connection.

createManager :: int → node-mysqlOptions → DriverFuncs

Returns functions to manage a pool of MySQLDBH connections.

new :: NodeMySQLConnection → MySQLDBH

Wrap the connection

prepare :: MySQLDBH → String → Async DBError (IStatement ([Object] | int | ()))

Prepares a statement to run on the database. Returns a promise for a statement handle.

rollback :: MySQLDBH → Async DBError ()

Rolls back the transaction. Returns a promise for when rollingback finishes.

status$ :: MySQLDBH → Observable DBStatus DBFatalError

Returns a stream of status events.

Transactional.MySQLStatement

Written By: Joel Dentici

Written On: 6/20/2017

A "prepared" statement for node-mysql.

destroy :: IStatement ([Object] | int | ()) → ()

Cleans up underlying resources associated with the statement handle.

execute :: MySQLStatement ([Object] | int | ()) → ...any → Async DBError ([Object] | int | ())

Executes the prepared statement with the provided placeholder bindings. Returns a promise with the result of the query.

mapper :: Map string (DBResult → [Object] | int | ())

Maps the result of a query to the correct type per the IDBH contract.

new :: NodeMySQLConnection → string → MySQLStatement ([Object] | int | ())

Construct a new MySQLStatement.

Transactional.PooledDBH

Written By: Joel Dentici

Written On: 6/20/2017

Wraps an underlying dbh that is pooled so closing it returns it to the connection pool.

Attempting to use the pooled connection after it is closed will cause each method to return an error.

beginTransaction :: IDBH → Async DBError ()

Begins a transaction on the base IDBH.

close :: IDBH → ()

Closes the pooled connection. This will cause the underlying IDBH to be returned to its pool.

commit :: IDBH → Async DBError ()

Commits the transaction on the base IDBH

connect :: IDBH → Async DBError ()

Connects the base IDBH.

new :: IDBH → PooledDBH

Constructs a Pooled DBH connection from an underlying dbh.

prepare :: IDBH → String → Async DBError (IStatement ([Object] | int | ()))

Prepares a statement on the base IDBH.

rollback :: IDBH → Async DBError ()

Rolls back the transaction on the base IDBH

status$ :: IDBH → Observable DBStatus DBFatalError

Returns a stream of status events for both the pooled connection and underlying IDBH.

Transactional.RecordMapper

Written By: Joel Dentici

Written On: 6/20/2017

Extends the IDBH interface to include record mapping.

beginTransaction :: IDBH → Async DBError ()

Begins a transaction on the base IDBH.

close :: IDBH → ()

Closes the base connection.

commit :: IDBH → Async DBError ()

Commits the transaction on the base IDBH

connect :: IDBH → Async DBError ()

Connects the base IDBH.

delete :: RecordMapper → string → (Map string any) | int → Async DBError ()

Deletes the record from the table. Returns a promise for the result of the deletion.

insert :: RecordMapper → string → Map string any → Async DBError int

Inserts a record into a table. Returns a promise with the result of the insertion.

new :: IDBH → RecordMapper

Extends the underlying IDBH with additional functionality.

objectSplit :: Map any any → [[any], [any]]

Splits an object into lists of its keys and values.

prepare :: IDBH → String → Async DBError (IStatement ([Object] | int | ()))

Prepares a statement on the base IDBH.

query :: RecordMapper → string → ...any → Async DBError ([Object] | int | ())

Runs a sql query on the database, preparing a statement, bindings the bindings to placeholders, and executing it. Returns a promise for the result.

Useful when you only want to execute a statement once.

read :: RecordMapper → String → int → Async DBError (Maybe (Map string any))

Reads a record from the table. Returns a promise for the row read from the table.

rollback :: IDBH → Async DBError ()

Rolls back the transaction on the base IDBH

status$ :: IDBH → Observable DBStatus DBFatalError

Returns a stream of status events from the base IDBH.

update :: RecordMapper → string → Map string any → Async DBError ()

Updates the record in the table. Returns a promise with the result of the update.

Transactional.Transaction

Written By: Joel Dentici

Written On: 6/20/2017

Provides a DSL to compose and execute database transactions. This comes in the form of a Free monad over a (rather large) functor defined below, along with an interpreter that translates monadic values into calls in the library.

This is the preferred way to use this library and produces much cleaner code than directly using the library functions to interact with your database.

delete :: string → Map string any | int → Transaction ()

Delete a row in a table.

emit :: string → Object → Transaction ()

Emit an event. This is used to signal that an event occurred while executing the transaction. The interpreter will automatically publish these events to a provided event bus.

execute :: IStatement → ...any → Transaction ([Object] | int | ())

Execute a prepared statement on the database.

insert :: string → Map string any → Transaction int

Insert a row into a table.

interpreter :: (DBHManager, IBus) → () → Interpreter

Creates an interpreter constructor that can be used with ConcurrentFree.interpret.

This interprets to Async and runs queries/statements in a transaction.

of/unit :: a → Transaction a

Lift a normal value into the Transaction context.

prepare :: string → Transaction IStatement

Prepare a statement for execution on the database.

query :: string → ...any → Transaction ([Object] | int | ())

Execute a query on the database.

read :: string → int → Transaction (Maybe (Map string any))

Read a row from a table.

update :: string → Map string any → Transaction ()

Update a row in a table.