Skip to content

Commit

Permalink
Added tests & D1 code
Browse files Browse the repository at this point in the history
  • Loading branch information
rennokki committed Aug 4, 2023
1 parent 3a6be19 commit 0cea830
Show file tree
Hide file tree
Showing 29 changed files with 2,419 additions and 81 deletions.
37 changes: 26 additions & 11 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 10
- package-ecosystem: composer
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 10
versioning-strategy: increase
- package-ecosystem: github-actions
groups:
all:
patterns: ["*"]
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 100
versioning-strategy: auto
rebase-strategy: auto
labels:
- dependabot:actions
- auto:dependabot
- dependencies

- package-ecosystem: composer
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 100
versioning-strategy: auto
rebase-strategy: auto
labels:
- dependabot:composer
- auto:dependabot
- dependencies
2 changes: 1 addition & 1 deletion .github/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exemptLabels:
- pinned
- security
# Label to use when marking an issue as stale
staleLabel: wontfix
staleLabel: status:ignored
# Comment to post when marking an issue as stale. Set to `false` to disable
markComment: false
# Comment to post when closing a stale issue. Set to `false` to disable
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
php:
- '8.1'
- '8.2'
- '8.3'
laravel:
- 9.*
- 10.*
Expand Down Expand Up @@ -60,6 +61,13 @@ jobs:
run: |
vendor/bin/psalm
- name: Run worker server
run: |
cd tests/worker
npm ci
npm run start &
sleep 5
- name: Run tests
run: |
vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Laravel D1 driver for Laravel
# L1

![CI](https://github.com/renoki-co/laravel-d1/workflows/CI/badge.svg?branch=master)
[![codecov](https://codecov.io/gh/renoki-co/laravel-d1/branch/master/graph/badge.svg)](https://codecov.io/gh/renoki-co/laravel-d1/branch/master)
![CI](https://github.com/renoki-co/l1/workflows/CI/badge.svg?branch=master)
[![codecov](https://codecov.io/gh/renoki-co/l1/branch/master/graph/badge.svg)](https://codecov.io/gh/renoki-co/l1/branch/master)
[![StyleCI](https://github.styleci.io/repos/:styleci_code/shield?branch=master)](https://github.styleci.io/repos/:styleci_code)
[![Latest Stable Version](https://poser.pugx.org/renoki-co/laravel-d1/v/stable)](https://packagist.org/packages/renoki-co/laravel-d1)
[![Total Downloads](https://poser.pugx.org/renoki-co/laravel-d1/downloads)](https://packagist.org/packages/renoki-co/laravel-d1)
[![Monthly Downloads](https://poser.pugx.org/renoki-co/laravel-d1/d/monthly)](https://packagist.org/packages/renoki-co/laravel-d1)
[![License](https://poser.pugx.org/renoki-co/laravel-d1/license)](https://packagist.org/packages/renoki-co/laravel-d1)
[![Latest Stable Version](https://poser.pugx.org/renoki-co/l1/v/stable)](https://packagist.org/packages/renoki-co/l1)
[![Total Downloads](https://poser.pugx.org/renoki-co/l1/downloads)](https://packagist.org/packages/renoki-co/l1)
[![Monthly Downloads](https://poser.pugx.org/renoki-co/l1/d/monthly)](https://packagist.org/packages/renoki-co/l1)
[![License](https://poser.pugx.org/renoki-co/l1/license)](https://packagist.org/packages/renoki-co/l1)

This is where your description should go. Try and limit it to a paragraph or two. Consider adding a small example.

Expand All @@ -15,13 +15,13 @@ This is where your description should go. Try and limit it to a paragraph or two
You can install the package via composer:

```bash
composer require renoki-co/laravel-d1
composer require renoki-co/l1
```

Publish the config:

```bash
php artisan vendor:publish --provider="RenokiCo\LaravelD1\LaravelD1ServiceProvider" --tag="config"
php artisan vendor:publish --provider="RenokiCo\L1\L1ServiceProvider" --tag="config"
```

## 🙌 Usage
Expand Down
31 changes: 23 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
{
"name": "renoki-co/laravel-d1",
"description": "Cloudflare D1 database driver for Laravel.",
"name": "renoki-co/l1",
"description": "Laravel integration for Cloudflare Workers services.",
"keywords": [
"laravel",
"php",
"database",
"query",
"sqlite",
"cloudflare",
"d1"
"d1",
"kv",
"r2",
"workers",
"serverless",
"serverless functions",
"serverless framework",
"serverless architecture"
],
"license": "Apache-2.0",
"homepage": "https://github.com/renoki-co/laravel-d1",
"homepage": "https://github.com/renoki-co/l1",
"authors": [
{
"name": "Alex Renoki",
Expand All @@ -20,16 +27,24 @@
}
],
"require": {
"illuminate/database": "^9.0|^10.0"
"doctrine/dbal": "^3.6",
"illuminate/cache": "^9.0|^10.0",
"illuminate/database": "^9.0|^10.0",
"saloonphp/laravel-http-sender": "^1.2",
"sammyjo20/saloon": "^2.0",
"sammyjo20/saloon-laravel": "^2.0"
},
"suggest": {
"ext-pdo": "Required to extend the PDO driver for Cloudflare D1."
},
"autoload": {
"psr-4": {
"RenokiCo\\LaravelD1\\": "src/"
"RenokiCo\\L1\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"RenokiCo\\LaravelD1\\Test\\": "tests"
"RenokiCo\\L1\\Test\\": "tests"
}
},
"scripts": {
Expand All @@ -48,7 +63,7 @@
"extra": {
"laravel": {
"providers": [
"RenokiCo\\LaravelD1\\LaravelD1ServiceProvider"
"RenokiCo\\L1\\L1ServiceProvider"
]
}
}
Expand Down
2 changes: 2 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@
</filter>
<php>
<server name="APP_ENV" value="testing" />
<server name="CLOUDFLARE_TOKEN" value="test" />
<server name="CLOUDFLARE_ACCOUNT_ID" value="1234" />
</php>
</phpunit>
29 changes: 29 additions & 0 deletions src/CloudflareConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace RenokiCo\L1;

use Saloon\Http\Connector;

abstract class CloudflareConnector extends Connector
{
public function __construct(
protected ?string $token = null,
public ?string $accountId = null,
public string $apiUrl = 'https://api.cloudflare.com/client/v4',
) {
$this->withTokenAuth($token);
}

public function resolveBaseUrl(): string
{
return $this->apiUrl;
}

protected function defaultHeaders(): array
{
return [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
];
}
}
24 changes: 24 additions & 0 deletions src/CloudflareD1Connector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace RenokiCo\L1;

use Saloon\Contracts\Response;

class CloudflareD1Connector extends CloudflareConnector
{
public function __construct(
public ?string $database = null,
protected ?string $token = null,
public ?string $accountId = null,
public string $apiUrl = 'https://api.cloudflare.com/client/v4',
) {
parent::__construct($token, $accountId, $apiUrl);
}

public function databaseQuery(string $query, array $params): Response
{
return $this->send(
new D1\Requests\D1QueryRequest($this, $this->database, $query, $params),
);
}
}
21 changes: 21 additions & 0 deletions src/CloudflareRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace RenokiCo\L1;

use Saloon\Http\Request;
use Saloon\Contracts\Connector;

abstract class CloudflareRequest extends Request
{
protected CloudflareConnector $connector;

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

protected function resolveConnector(): Connector
{
return $this->connector;
}
}
34 changes: 34 additions & 0 deletions src/D1/D1Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace RenokiCo\L1\D1;

use Illuminate\Database\SQLiteConnection;
use RenokiCo\L1\D1\Pdo\D1Pdo;
use RenokiCo\L1\CloudflareD1Connector;

class D1Connection extends SQLiteConnection
{
public function __construct(
protected CloudflareD1Connector $connector,
protected $config = [],
) {
parent::__construct(
new D1Pdo('sqlite::memory:', $this->connector),
$config['database'] ?? '',
$config['prefix'] ?? '',
$config,
);
}

protected function getDefaultSchemaGrammar()
{
($grammar = new D1SchemaGrammar)->setConnection($this);

return $this->withTablePrefix($grammar);
}

public function d1(): CloudflareD1Connector
{
return $this->connector;
}
}
44 changes: 44 additions & 0 deletions src/D1/D1SchemaGrammar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace RenokiCo\L1\D1;

use Illuminate\Support\Str;
use Illuminate\Database\Schema\Grammars\SQLiteGrammar;

class D1SchemaGrammar extends SQLiteGrammar
{
public function compileTableExists()
{
return Str::of(parent::compileTableExists())
->replace('sqlite_master', 'sqlite_schema')
->__toString();
}

public function compileDropAllTables()
{
return Str::of(parent::compileDropAllTables())
->replace('sqlite_master', 'sqlite_schema')
->__toString();
}

public function compileDropAllViews()
{
return Str::of(parent::compileDropAllViews())
->replace('sqlite_master', 'sqlite_schema')
->__toString();
}

public function compileGetAllTables()
{
return Str::of(parent::compileGetAllTables())
->replace('sqlite_master', 'sqlite_schema')
->__toString();
}

public function compileGetAllViews()
{
return Str::of(parent::compileGetAllViews())
->replace('sqlite_master', 'sqlite_schema')
->__toString();
}
}
Loading

0 comments on commit 0cea830

Please sign in to comment.