midorikocak/nanodb
Composer 安装命令:
composer require midorikocak/nanodb
包简介
Nano DB is a tiny php library that allows you to define easily usable repositories.
README 文档
README
Nano DB
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
Strictly requires PHP 7.4.
Install
Via Composer
$ composer require midorikocak/nanodb
Usage
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
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
To order results use the orderBy($fieldName) method.
print_r($db->select($tableName)->where('id', $id)->orderBy('id', 'DESC')->fetch());
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 using an array of data. Validation is your responsibility.
$db->select($tableName)->insert($tableName, $data)->executre();
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
Returns affected rows. If id does not exist, throws exception.
$db->delete($tableName)->delete('id', $id)->execute();
RepositoryInterface
The repository interface is the interface of repositories.
<?php 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; }
If you want to use arrays to interact with your database, you can use the array repository.
use midorikocak\nanodb\ArrayRepository; $tableName = 'users'; $schema = [ 'username'=>'string', 'password'=>'string', 'email'=>'email' ]; $repository = new ArrayRepository($tableName, $this->db, $schema);
Here $schema array is a simple optional array for Array validator, checked on every input with data. You can override it by extending ArrayValidator class.
Class Repositories
Let's say you have a simple user class.
<?php 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; } }
You can create a Usersrepository, by implementing the RepositoryInterface.
<?php 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; } }
Motivation and Warning
Mostly educational purposes. Please use at your own risk.
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email mtkocak@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
midorikocak/nanodb 适用场景与选型建议
midorikocak/nanodb 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 108 次下载、GitHub Stars 达 7, 最近一次更新时间为 2020 年 01 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「datasource」 「midorikocak」 「nanodb」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 midorikocak/nanodb 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 midorikocak/nanodb 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 midorikocak/nanodb 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
FSi DataSource Component
FSi DataSource Bundle
Store your language lines in the database, yaml or other sources
MongoDB Datasource for CakePHP
AWS S3 datasource in CakePHP3
统计信息
- 总下载量: 108
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-01-30
