PHP code example of hiraeth / turso

1. Go to this page and download the library: Download hiraeth/turso 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/ */

    

hiraeth / turso example snippets


$database = new Hiraeth\Turso\Database(
    new GuzzleHttp\Client(),
    'https://<dbname>-<organization>.turso.io',
    'Bearer <token>'
);

$result = $database->execute(
    "SELECT * FROM users"
);

$result = $database->execute(
    "SELECT * FROM users WHERE email LIKE '%@hiraeth.dev'"
);

$result = $database->execute(
    "SELECT * FROM @table WHERE email LIKE {domain}",
    [
        'domain' => '%@hiraeth.dev'
    ],
    [
        'table' => 'users'
    ]
);

if ($result->isError()) {
    //
    // handle the error
    //
}

$result->throw('Failed executing the query');

foreach ($result as $record) {
    echo sprintf(
        'User with e-mail %s has an ID of %s',
        $record->email,
        $record->id
    );
}

$record = $result->getRecord(0);

$records = $result->getRecords();

$count = count($result);

use Hiraeth\Turso\Types;

class User extends Hiraeth\Turso\Entity
{
    const table = 'users';
    
    const identity = [
        'id'
    ];

    const types = [
        'dateOfBirth' => Types\Date::class
    ];
    
    protected $id;

    public $firstName;

    public $lastName;

    public $email;
    
    public $dateOfBirth;

    public function fullName()
    {
        return trim(sprintf(
            '%s %s',
            $this->firstName,
            $this->lastName
        ));
    }
}

$records = $result->of(User::class);

foreach ($result->of(User::class) as $user) {
    echo sprintf(
        '%s has an e-mail of %s' . PHP_EOL,
        $user->fullName(),
        $user->email
    );
}

const types = [
    'dateOfBirth' => Types\Date::class
];

namespace Hiraeth\Turso\Types;

use DateTime;

/**
 * Handles dates
 */
class Date {
	/**
	 * Convert a value from the database to the entity
	 */
	static public function from(string|null $date): DateTime|null
	{
		return $date ? new DateTime($date) : NULL;
	}


	/**
	 * Convert a value from an entity to the database
	 */
	static public function to(DateTime|null $date): string|null
	{
		return $date ? $date->format('Y-m-d') : NULL;
	}
}

public function occupation()
{
    return $this(Occupation::class)->hasOne(
        [
            'occupation' => 'id'
        ],
        FALSE
    );
}

$this(Occupation::class)

[
    'occupation' => 'id'
]

public function occupation(bool $refresh = FALSE): ?Occupation
{
    return $this(Occupation::class)->hasOne(
        [
            'occupation' => 'id'
        ],
        $refresh
    );
}

public function users(bool $refresh = FALSE): Result
{
    return $this(User::class)->hasMany(
        [
            'id' => 'occupation'
        ],
        $refresh
    );
}

if (count($occupation->users())) {
    foreach ($occupation->users() as $user) {
        // ...
    }
}

public function friends($bool $refresh = FALSE): Result
{
    return $this(User::class, 'friends')->hasMany(
        [
            'id' => 'user', 'friend' => 'id'
        ],
        $refresh
    );
}

public function occupation(bool|Occupation $refresh = FALSE): ?Occupation
{
    if ($refresh instanceof Occupation) {
        $this(Occupation::class)->changeOne($refresh, [
            'id' => 'occupation'
        ]);
    }

    return $this(Occupation::class)->hasOne(
        [
            'occupation' => 'id'
        ],
    	$refresh
    );
}

$user->occupation($occupation);

class Users extends Hiraeth\Turso\Repository
{
    const entity = User::class;

    const order = [
        'firstName' => 'asc',
        'lastName'  => 'asc'
    ];
}

$users = $database->getRepository(Users::class);

$users = new Users($database);

$user = $users->create([
    'firstName' => 'Hiraeth',
    'lastName'  => 'User',
    'email'     => '[email protected]'
]);

$user = $users->create();

$user->firstName = 'Hiraeth';
$user->lastName  = 'User';
$user->email     = '[email protected]';

$result = $users->insert($user);

$user->firstName = 'Laravel';

$result = $users->update($user);

if (!$result->getAffectedRows()) {
    //
    // Handle the user having gone missing
    //
}

$users->delete($user);

$user = $users->find(1);

$user = $users->find(['email' => '[email protected]'])

$all_users = $users->findAll();

$all_users = $users->findAll(['email' => 'asc']);

$taylors = $users->findBy(
    [
        'firstName' = 'Taylor'
    ],
    [
        'lastName' => 'desc'
    ],
    20,
    1
);

use Hiraeth\Turso\SelectQuery;
use Hiraeth\Turso\Expression;

$entities = $users->select(
    function(SelectQuery $query, Expression $is) {
        $query
            ->where(
                $is->like('email', '%@hiraeth.dev'),
                $is->gte('age', 30)
            )
            ->order(
                $query->sort('age', 'desc')
            )
            ->limit(20)
            ->offset(0)
    }
);

/**
 * Set the "ORDER BY" portion of the statement
 */
public function order(Query ...$sorts): static
{
    if (empty($sorts)) {
        $clause = $this('');
    } else {
        $clause = $this('ORDER BY @sorts')->bind(', ', FALSE)->raw('sorts', $sorts);
    }

    $this->raw('order', $clause);

    return $this;
}

where(
    $is->like('email', '%@hiraeth.dev'),
    $is->any(
        $is->gte('age', 30),
        $is->lte('age', 50)
    )
);
sqlite
WHERE email LIKE '%@hiraeth.dev' AND (age >= 30 OR age <= 50)