Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fatfingers23/replit-database-client

Simple Php Replit Database Client
https://github.com/fatfingers23/replit-database-client

Last synced: 10 days ago
JSON representation

Simple Php Replit Database Client

Awesome Lists containing this project

README

        

Repl.it PHP Database Client
============

Simple Repl.it database client in PHP. Loosely based off of the [replit/database-node](https://github.com/replit/database-node).

[![License](http://poser.pugx.org/fatfingers23/replit-database-client/license)](https://packagist.org/packages/fatfingers23/replit-database-client)
[![Run on Repl.it](https://repl.it/badge/github/fatfingers23/Replit-Database-Client)](https://repl.it/github/fatfingers23/Replit-Database-Client)

Requirements
------------

* PHP >= 8.0;
* Composer.

Installation
============

`composer require fatfingers23/replit-database-client`

## Get started
```php
set('key', 'value');
$key = $client->get('key');
echo $key;
```
## Docs
Client
> ### `class DatabaseClient(String url?)`
Ability to pass a custom url

**Native Functions**

> `set(string $key, array|string $value): void`

Sets a key with a string value
```php
set('key', 'value');
```

Sets a key with an array value
```php
set('key', ['greeting' => 'Hello World!']);
```

> `get(string $key): array|string|null`

Gets a key with a string value
```php
get('key');
echo $key;
```

Gets a key with an array value
```php
get('key');
var_dump($key);
echo $key['greeting'];
```

> `delete(string $key): void`

Deletes an entry in the database by its key
```php
delete('key');
```

> `getPrefixKeys(string $prefix): array`
* Returns an array of keys that start with the prefix
* If no prefix is given returns all the keys in the database
* Returns an empty array if it finds no keys
```php
set('poet.1', 'John Keats');
$client->set('poet.2', 'Emily Dickinson');
$poetKeys = $client->getPrefixKeys('poet');

#var_export($poetKeys) result below
array (
0 => 'poet.1',
1 => 'poet.2',
)
```

**Extended Functions**

> `getPrefix(string $prefix): ?array`
* Returns an array of all the values to a prefix
* If no prefix is given returns the whole database
* Returns null if no prefixs are found
```php
set('poet.1', 'John Keats');
$client->set('poet.2', 'Emily Dickinson');
$poets = $client->getPrefix('poet');

#var_export($poets) result below
array (
'poet.1' => 'John Keats',
'poet.2' => 'Emily Dickinson',
)
```

> `deleteByPrefix(string $prefix = '')`
* Deletes a series of keys by their prefix
* Careful if no prefix is given, this function will delete the entire database
```php
deleteByPrefix('poet');
```

## Tests
```sh
composer test
```
or can just click run if this is open in a Repl.it

Contributing
============

1. Fork it.
2. Create your feature branch (git checkout -b my-new-feature).
3. Make your changes.
4. Run the tests, adding new ones for your own code if necessary (phpunit).
5. Commit your changes (git commit -am 'Added some feature').
6. Push to the branch (git push origin my-new-feature).
7. Create new pull request.