Skip to content

Commit

Permalink
Merge pull request #8 from mgcostaParedes/mgcosta_feat_facade
Browse files Browse the repository at this point in the history
feat: facade class
  • Loading branch information
mgcostaParedes authored May 9, 2021
2 parents f086d82 + d00bc57 commit 8107056
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 4 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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();
Expand Down Expand Up @@ -91,6 +92,18 @@ $user->save();

```

**Using the query builder without Model Class**
```PHP
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();
```


The implementation of the query builder is inspired on Laravel Query Builder, to get more documentation follow the [link](https://laravel.com/docs/master/queries).

## Roadmap
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mgcosta/spanner-orm-builder",
"type": "library",
"description": "Google Spanner ORM With Query Builder",
"version": "0.0.2",
"version": "0.0.3",
"license": "MIT",
"authors": [
{
Expand Down
29 changes: 29 additions & 0 deletions src/Spanner/Facade/SpannerDB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace MgCosta\Spanner\Facade;

use Google\Cloud\Spanner\Database;
use MgCosta\Spanner\Builder\Builder;
use MgCosta\Spanner\Manager\Manager;

class SpannerDB
{
/**
* The spanner connection instance.
*
* @var Database;
*/
protected $connection;

public function __construct(Database $connection = null)
{
$this->connection = $connection ?? Manager::getConnection();
}

public function table(string $name): Builder
{
return (new Builder($this->connection))->from($name);
}
}
1 change: 1 addition & 0 deletions src/Spanner/Manager/Manageable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
interface Manageable
{
public function boot();
public static function getConnection();
}
11 changes: 8 additions & 3 deletions src/Spanner/Manager/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@

class Manager implements Manageable
{
protected $connection;
protected static $connection;

public function __construct(Database $connection)
{
$this->connection = $connection;
static::$connection = $connection;
}

public function boot()
{
Model::setConnectionDatabase($this->connection);
Model::setConnectionDatabase(static::$connection);
Model::setStrategyFactory(new StrategyFactory());
}

public static function getConnection(): Database
{
return static::$connection;
}
}
27 changes: 27 additions & 0 deletions tests/unit/FacadeSpannerDBTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Tests\unit;

use Google\Cloud\Spanner\Database;
use MgCosta\Spanner\Builder\Builder;
use MgCosta\Spanner\Facade\SpannerDB;
use Codeception\Test\Unit;
use MgCosta\Spanner\Manager\Manager;
use Mockery as m;

class FacadeSpannerDBTest extends Unit
{
private $facade;

public function setUp(): void
{
parent::setUp();
new Manager(m::mock(Database::class));
$this->facade = new SpannerDB();
}

public function testShouldReturnAQueryBuilderInstanceWhenCallingWithinMethodTable()
{
$this->assertInstanceOf(Builder::class, $this->facade->table('test'));
}
}

0 comments on commit 8107056

Please sign in to comment.