PHPackages                             jonbaldie/type-db - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. jonbaldie/type-db

ActiveLibrary[Database &amp; ORM](/categories/database)

jonbaldie/type-db
=================

Type-safe database connector for PHP.

v0.9.3(3y ago)03MITPHPPHP &gt;=8.1

Since Sep 22Pushed 3y ago1 watchersCompare

[ Source](https://github.com/jonbaldie/type-db)[ Packagist](https://packagist.org/packages/jonbaldie/type-db)[ RSS](/packages/jonbaldie-type-db/feed)WikiDiscussions main Synced 6d ago

READMEChangelogDependencies (5)Versions (12)Used By (0)

[![CircleCI](https://camo.githubusercontent.com/9a444058a208d10e78fe013c60a649a2ab4c969d910080cfb7ab26fd0f4403bc/68747470733a2f2f646c2e636972636c6563692e636f6d2f7374617475732d62616467652f696d672f67682f6a6f6e62616c6469652f747970652d64622f747265652f6d61696e2e7376673f7374796c653d736869656c64)](https://dl.circleci.com/status-badge/redirect/gh/jonbaldie/type-db/tree/main)

This library provides a type-safe database connector for PHP.

Inspired by the HDBC package in Haskell, this library provides a series of classes and three helper functions to help make your database interaction more deterministic and testable.

Strings, floats, integers, and nulls are automatically converted to and from `SqlValue` objects, giving you more type-safe precision in your queries.

- Strings are mapped to `SqlString`
- Floats are mapped to `SqlFloat`
- Integers are mapped to `SqlInteger`
- Finally, nulls are mapped to `SqlNull`

All of these implement an `SqlValue` interface.

Once you see how these types can be type-hinted throughout your codebase, you bolster your codebase's testability and precision.

The connector works with any database driver compatible with the PDO abstraction layer. That means you can use it with your SQLite3, MySQL (and MariaDB), and PostgreSQL databases with ease.

Installation
------------

[](#installation)

```
composer require jonbaldie/type-db
```

The library requires PHP 8.1 as a minimum along with whichever PDO driver you need for your database. Once you have your `PDO` object instantiated, you're good to go!

Usage
-----

[](#usage)

The test files inside `tests/` provide a range of example usages for you to peruse, but here is some documentation on how to use the library.

### \\TypeDb\\Connection

[](#typedbconnection)

This class wraps the `PDO` class but adds the type-safe features that makes this library useful.

Here is a simple way you can query your database:

```
