PHP code example of midorikocak / nanodb

1. Go to this page and download the library: Download midorikocak/nanodb library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

midorikocak / nanodb example snippets


    use midorikocak\nanodb\Database;

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

    use midorikocak\nanodb\Database;

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

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

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

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

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

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

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

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

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



declare(strict_types=1);

namespace midorikocak\nanodb;

use midorikocak\querymaker\QueryInterface;

interface RepositoryInterface
{
    public function read(string $id);

    public function readAll(?QueryInterface $query = null): array;


    public function save($item);

    public function remove($data): int;
}

use midorikocak\nanodb\ArrayRepository;


$tableName = 'users';
$schema = [
    'username'=>'string',
    'password'=>'string',
    'email'=>'email'
];


$repository = new ArrayRepository($tableName, $this->db, $schema);




declare(strict_types=1);

class User
{
    private ?string $id;
    private string $username;
    private string $email;
    private string $password;

    public function __construct(?string $id, string $username, string $email, string $password)
    {
        $this->id = $id;
        $this->username = $username;
        $this->password = $password;
        $this->email = $email;
    }

    public function getId(): ?string
    {
        return $this->id;
    }

    public function setId(string $id)
    {
        $this->id = $id;
    }

    public function getUsername(): string
    {
        return $this->username;
    }

    public function setUsername(string $username)
    {
        $this->username = $username;
    }

    public function getEmail(): string
    {
        return $this->email;
    }

    public function setEmail(string $email)
    {
        $this->email = $email;
    }

    public function getPassword(): string
    {
        return $this->password;
    }

    public function setPassword(string $password)
    {
        $this->password = $password;
    }
}



declare(strict_types=1);

namespace midorikocak\nanodb;

use Exception;

use midorikocak\querymaker\QueryInterface;

use function array_map;

class Users implements RepositoryInterface
{
    private DatabaseInterface $db;

    public function __construct(DatabaseInterface $db)
    {
        $this->db = $db;
    }

    /**
     * @return User
    */
    public function read(string $id)
    {
        $data = $this->db->select('users')->where('id', $id)->fetch();
        if (!$data) {
            throw new Exception('not found');
        }
        return self::fromArray($data);
    }

    public function readAll(?QueryInterface $query = null): array
    {
        if ($query !== null) {
            $db = $this->db->query($query);
        } else {
            $db = $this->db->select('users');
        }
        $db->execute();
        return array_map(fn($data) => User::fromArray($data), $db->fetchAll());
    }

    /**
     * @param User $user
     * @return User
    */
    public function save($user)
    {
        if ($user->getId()) {
            $id = $user->getId();
            $userData = self::toArray($user);
            unset($userData['id']);
            $this->db->update('users', $userData)->where('id', $id)->execute();
            return $user;
        }

        $this->db->insert('users', self::toArray($user))->execute();

        $lastInsertId = $this->db->lastInsertId();
        $user->setId($lastInsertId);
        return $user;
    }

    /**
     * @param User $user
     */
    public function remove($user): int
    {
        $id = $user->getId();
        $this->db->delete('users')->where('id', $id)->execute();
        return $this->db->rowCount();
    }

    /**
     * @param User $user
     * @return User
    */
    public static function fromArray(array $array): User
    {
        if (!isset($array['id'])) {
            $array['id'] = null;
        }
        return new User($array['id'], $array['username'], $array['email'], $array['password']);
    }

    /**
     * @param User $user
     * @return array
    */
    public static function toArray(User $user): array
    {
        $toReturn = [
            'username' => $user->getUsername(),
            'email' => $user->getEmail(),
            'password' => $user->getPassword(),
        ];

        if ($user->getId()) {
            $toReturn['id'] = $user->getId();
        }

        return $toReturn;
    }
}