PHPackages                             midorikocak/nanodb - 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. midorikocak/nanodb

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

midorikocak/nanodb
==================

Nano DB is a tiny php library that allows you to define easily usable repositories.

v1.2.6(6y ago)710822MITPHPPHP ~7.4

Since Jan 30Pushed 6y ago2 watchersCompare

[ Source](https://github.com/midorikocak/nanodb)[ Packagist](https://packagist.org/packages/midorikocak/nanodb)[ Docs](https://github.com/midorikocak/nanodb)[ RSS](/packages/midorikocak-nanodb/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (6)Versions (14)Used By (2)

[![nano API](nano.png)](nano.png)

Nano DB
=======

[](#nano-db)

[![Latest Version on Packagist](https://camo.githubusercontent.com/878e1bafe9003d735a77db45c6cffffdd3f82a61d4c4839e4e9ee1bc0b3f3908/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d69646f72696b6f63616b2f6e616e6f64622e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/midorikocak/nanodb)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/16808c8cd6b9716aa458f9db5171ff5142589682fcb9bf7cac214f2fd3df7c2a/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6d69646f72696b6f63616b2f6e616e6f64622f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/midorikocak/nanodb)[![Coverage Status](https://camo.githubusercontent.com/66825c66bd2c1af2685d88ef0880aaf679acef75a6bdfbfe67689da99e0b7948/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6d69646f72696b6f63616b2f6e616e6f64622e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/midorikocak/nanodb/code-structure)[![Quality Score](https://camo.githubusercontent.com/29e342924940f79a5128596a84f715d47f86f826f3d4a361a0d84e9e19e6d6ca/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6d69646f72696b6f63616b2f6e616e6f64622e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/midorikocak/nanodb)[![Total Downloads](https://camo.githubusercontent.com/f03177771775cc151be5cabbd01f496b3e62f4bd2d09123fec85351cd9fd4cc3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d69646f72696b6f63616b2f6e616e6f64622e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/midorikocak/nanodb)

Nano DB is a tiny php library that allows you to define easily usable repositories.

There are 3 handy classes and 1 example in this library. Let's start with basics:

Requirements
------------

[](#requirements)

Strictly requires PHP 7.4.

Install
-------

[](#install)

Via Composer

```
$ composer require midorikocak/nanodb
```

Usage
-----

[](#usage)

### Database

[](#database)

To use database library, simple inject it with pdo.

```
    use midorikocak\nanodb\Database;

    $pdo = new PDO('sqlite::memory:');
    $db = new Database($pdo);
```

Alternatively, if you want to use a query object, you can inject it as well.

```
    use midorikocak\nanodb\Database;

    $pdo = new PDO('sqlite::memory:');
    $query = new QueryMaker();
    $db = new Database($pdo, $query);
```

Operations are chained.

```
    $db->insert($tableName, $data)->execute();

    $lastInsertId = $db->lastInsertId();
    $insertedItem = $db->select($tableName)->where('id', $lastInsertId)->fetch();
```

### Select

[](#select)

If found, returns the data you need. If nothing found, empty array is returned.

```
    print_r($db->select($tableName)->where('id', $id)->fetch());
```

Example output:

```
Array
(
    [id] => 1
    [username] => username
    [email] => email@email.com
    [password] => 123456789
)

```

### Order by

[](#order-by)

To order results use the `orderBy($fieldName)` method.

```
    print_r($db->select($tableName)->where('id', $id)->orderBy('id', 'DESC')->fetch());
```

### Offset and Limit

[](#offset-and-limit)

To limit and offset results use the `limit($limit)` and `offset($offset)` methods.

```
    print_r($db->select($tableName)->where('id', $id)->limit(1)->offset(1)->fetch());
```

### Insert

[](#insert)

Insert using an array of data. Validation is your responsibility.

```
    $db->select($tableName)->insert($tableName, $data)->executre();
```

### Update

[](#update)

Insert using an array of data. Again validation is your responsibility. If id does not exist, throws exception.

```
   $db->update($tableName, $data)->update($tableName, $data)->where('id', $id)->execute();
```

### Delete

[](#delete)

Returns affected rows. If id does not exist, throws exception.

```
    $db->delete($tableName)->delete('id', $id)->execute();
```

RepositoryInterface
-------------------

[](#repositoryinterface)

The repository interface is the interface of repositories.

```
