mgcosta / spanner-orm-builder
Google Spanner ORM With Query Builder
v0.0.5
2021-06-01 22:20 UTC
Requires
- php: ^7.3 || ^8.0
- ext-grpc: *
- ext-json: *
- ext-pdo: *
- google/cloud-spanner: ^1.36
- grpc/grpc: ^1.36
- illuminate/collections: ^8.37.0
- ramsey/uuid: ^4.1.1
Requires (Dev)
- ext-xdebug: *
- codeception/c3: ^2.4
- codeception/codeception: ^4.1
- codeception/module-asserts: ^1.2
- mockery/mockery: ^1.4
- phpcompatibility/php-compatibility: ^9.3
- phpunit/phpunit: ^9
- sebastian/phpcpd: ^5.0
- squizlabs/php_codesniffer: ^3.5
README
Spanner ORM Builder 是一个数据库工具包,为 PHP 提供 expressive 查询构建器、ActiveRecord 风格的 ORM,如果你打算使用 Google Cloud Spanner,它可以作为你 PHP 应用程序的数据库层。
安装
通过 Composer
$ composer require mgcosta/spanner-orm-builder
使用说明
首先,我们应该创建一个新的 "Manager" 实例。Manager 的目的是让为每个框架配置库尽可能容易。
use MgCosta\Spanner\Manager; use Google\Cloud\Spanner\Database; // $database = your database instance for google cloud spanner; // instance of Google\Cloud\Spanner\Database; $manager = new Manager($database); $manager->boot();
就这样,你就可以使用这个库了,只需确保尽快在你的 APP 中实例化 manager,通常是在你的 bootstrap 或配置文件中。
一旦 Manager 实例被注册,我们可以像这样使用它
使用查询构建器
use MgCosta\Spanner\Model\Model; class User extends Model {} $users = User::where('age', '>', 30)->get(); $id = 1; $user = User::find($id);
使用查询构建器更新/删除
use MgCosta\Spanner\Model\Model; class User extends Model {} // deleting User::where('id', 1)->delete(); // updating $status = User::where('id', 5)->update(['name' => 'Richard', 'age' => 30]);
保存模型
use MgCosta\Spanner\Model\Model; class User extends Model { protected $primaryKey = 'userId'; // available strategies [uuid4, increment] // increment is not recommend by cloud spanner protected $keyStrategy = 'uuid4'; // we must define the properties which corresponds to the columns of the table as public public $userId; public $name; public $age; public $email; } $user = new User(); $user->name = 'Miguel'; $user->age = 28; $user->email = 'email@gmail.com'; $user->save();
在不使用模型类的情况下使用查询构建器
use MgCosta\Spanner\Facade\SpannerDB; (new SpannerDB())->table('users')->whereIn('id', [1, 2, 3])->get(); // you can also provide a custom spanner Database Instance // $database = instance of Google\Cloud\Spanner\Database; (new SpannerDB($database))->table('users')->where('id', 1)->first();
查询构建器的实现受到了 Laravel Query Builder 的启发,要获取更多文档,请点击 链接。
路线图
有关此早期版本的更多计划细节,请点击以下 链接。
贡献
请参阅 CONTRIBUTING 了解详情。
鸣谢
- [Miguel Costa][https://github.com/mgcostaParedes]
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。