PHPackages                             sw-serv/relational-db - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. sw-serv/relational-db

AbandonedLibrary[Database &amp; ORM](/categories/database)

sw-serv/relational-db
=====================

This package provide advanced features to manipulate openswoole tables

1.3.6(1y ago)6181GPL-3.0-onlyPHPPHP &gt;=8.3

Since Feb 24Pushed 1y ago1 watchersCompare

[ Source](https://github.com/crustphp/Swoole-RelationalDB)[ Packagist](https://packagist.org/packages/sw-serv/relational-db)[ RSS](/packages/sw-serv-relational-db/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (4)Versions (27)Used By (0)

This package provide advanced features to manipulate OpenSwoole\\Table.

The heavy advantages of OpenSwoole\\Table is heavy speed access : you can read about 2 million records in one second, and it is shared between processes.

This package intend to allow you to easy manage OpenSwoole\\Table.

Registries
----------

[](#registries)

### Table registry

[](#table-registry)

Table registry is your entry point to small-swoole-db

You can :

- create a table in memory and register it
- get a table already registered
- destroy a table in memory
- persist a table to :
    - Json file
    - Redis
    - Mysql
- Load a table previously persisted

#### Create a table

[](#create-a-table)

To create a table, use registry method :

```
use \Crust\SwooleDb\Registry\TableRegistry;

$testTable = TableRegistry::getInstance()->createTable('testTable', 128);
```

- The first param is the table name in registry. Use it to recall table from other parts of code.
- The second param is the max number of rows in the table

Once the table is created in registry, you can add columns :

Only these type are accepted :

- ColumnType::string
- ColumnType::float
- ColumnType::int

Note the size param is required, except for flot type

```
use \Crust\SwooleDb\Registry\TableRegistry;
use \Crust\SwooleDb\Core\Column;
use \Crust\SwooleDb\Core\Enum\ColumnType;

TableRegistry::getInstance()->getTable('testTable')
    ->addColumn(new Column('firstname', ColumnType::string, 256))
    ->addColumn(new Column('credit', ColumnType::float))
    ->addColumn(new Column('rank', ColumnType::int, 8))
```

Now we have added the columns, we can create in memory :

```
use \Crust\SwooleDb\Registry\TableRegistry;

$success = TableRegistry::getInstance()->getTable('testTable')->create();
```

The table is now ready to use as a OpenSwoole\\Table object :

```
use \Crust\SwooleDb\Registry\TableRegistry;

$table = TableRegistry::getInstance()->getTable('testTable')
$table->set(0, ['franck', 12.5, 11]);
$table->set(1, ['joe', 55.2, 26]);
```

#### Foreign key

[](#foreign-key)

You can link two tables throw foreign key.

```
use Crust\SwooleDb\Registry\TableRegistry;

$table = TableRegistry::getInstance()->createTable('testSelectJoin', 5);
$table->addColumn(new Column('name', ColumnType::string, 255));
$table->addColumn(new Column('price', ColumnType::float));
$table->create();
$table->set(0, ['name' => 'john', 'price' => 12.5]);
$table->set(1, ['name' => 'paul', 'price' => 34.9]);

$table2 = TableRegistry::getInstance()->createTable('testSelectJoinPost', 5);
$table2->addColumn(new Column('message', ColumnType::string, 255));
$table2->addColumn(new Column('ownerId', ColumnType::int, 16));
$table2->create();
$table2->set(0, ['message' => 'ceci est un test', 'ownerId' => 0]);
$table2->set(1, ['message' => 'ceci est un autre test', 'ownerId' => 1]);
$table2->set(2, ['message' => 'ceci est une suite de test', 'ownerId' => 1]);

$table2->addForeignKey('messageOwner', 'testSelectJoin', 'ownerId');
```

See [OpenSwoole documentation for Table](https://openswoole.com/docs/modules/swoole-table)

#### Destroy a table

[](#destroy-a-table)

Once you don't need anymore table, you can destroy it to free all associated memory.

```
use \Crust\SwooleDb\Registry\TableRegistry;

$table = TableRegistry::getInstance()->destroy('testTable');
```

This will destroy table and remove it from registry

#### Persistence

[](#persistence)

When you need persistence for table, you can store table to a json file.

Soon, I will develop redis and mysql persistence but for now, use default persistence :

```
use \Crust\SwooleDb\Registry\TableRegistry;

TableRegistry::getInstance()->persist('testTable');
```

This will store table definition in a json file stored in /var/lib/small-swoole-db/data/testTable.json

To reload table from disk (at server restart for example) :

```
use \Crust\SwooleDb\Registry\TableRegistry;

TableRegistry::getInstance()->loadFromChannel('testTable');
```

This will restore table in registry and memory with all data from last persist.

### ParamRegistry

[](#paramregistry)

If you want, you can use ParamRegistry to change :

- location of /var/lib direcoty :

```
use Crust\SwooleDb\Registry\ParamRegistry;
use Crust\SwooleDb\Registry\Enum\ParamType;

ParamRegistry::getInstance()->set(ParamType::varLibDir, '/home/some-user');
```

- data dir name :

```
use Crust\SwooleDb\Registry\ParamRegistry;
use Crust\SwooleDb\Registry\Enum\ParamType;

ParamRegistry::getInstance()->set(ParamType::dataDirName, 'persistence');
```

In this example, the testTable table will be stored in :

```
/home/some-user/testTable.json

```

### Selector

[](#selector)

You can use selector to build complex requests.

Basically, you can select all records :

```
use Crust\SwooleDb\Selector\TableSelector;

$selector = new TableSelector('testSelect');
$records = $selector->execute();

foreach ($records as $record) {
    echo $record['testSelect']->getValue('name');
}
```

You can use *where* to add conditions :

```
use Crust\SwooleDb\Selector\TableSelector;
use Crust\SwooleDb\Selector\Enum\ConditionElementType;
use Crust\SwooleDb\Selector\Enum\ConditionOperator;

$selector = new TableSelector('testSelect');
$selector->where()
    ->firstCondition(new Condition(
        new ConditionElement(ConditionElementType::var, 'price', 'testSelect'),
        ConditionOperator::superior,
        new ConditionElement(ConditionElementType::const, 15)
    ));
$records = $selector->execute();

foreach ($records as $record) {
    echo $record['testSelect']->getValue('name');
}
```

You can also join result throw foreign keys :

```
use Crust\SwooleDb\Selector\TableSelector;

$result = (new TableSelector('user'))
    ->join('post', 'messageOwner', 'message')
    ->execute()

foreach ($result as $record) {
    echo $record['message']->getValue('body') . ' : by ' . $record['user']->getValue('name');
}
```

And select any of fields (note that you can change record and persist easily) :

```
use Crust\SwooleDb\Selector\TableSelector;
use Crust\SwooleDb\Selector\Enum\ConditionElementType;
use Crust\SwooleDb\Selector\Enum\ConditionOperator;

$selector = (new TableSelector('user'))
    ->join('post', 'messageOwner', 'message')
;

$selector->where()
    ->firstCondition(new Condition(
        new ConditionElement(ConditionElementType::var, 'name', 'user'),
        ConditionOperator::equal,
        new ConditionElement(ConditionElementType::const, 'john')
    ))->andCondition(new Condition(
        new ConditionElement(ConditionElementType::var, 'subject', 'message'),
        ConditionOperator::like,
        new ConditionElement(ConditionElementType::const, '%hiring%')
    ))
;

$result = $selector->execute();

foreach ($result as $record) {
    echo $record['message']->getValue('body') . ' : by ' . $record['user']->getValue('name');

    $record['message']
        ->setValue('read', 1)
        ->persist()
    ;
}
```

Testing
-------

[](#testing)

You must have docker installed to run tests.

To build unit-test container and run test suite, use command :

```
$ bin/test --build
```

Once the container is build, you can quickly run test using :

```
$ bin/test
```

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance41

Moderate activity, may be stable

Popularity12

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~26 days

Recently: every ~14 days

Total

24

Last Release

564d ago

Major Versions

0.4.0 → 1.0.02024-03-17

### Community

Maintainers

![](https://www.gravatar.com/avatar/a53d08e1aee4461dbef1a5418f7086bf27342d499d7350216ac81d90fb8cdb4f?d=identicon)[fakharakgit](/maintainers/fakharakgit)

---

Top Contributors

[![sebk69](https://avatars.githubusercontent.com/u/6600508?v=4)](https://github.com/sebk69 "sebk69 (11 commits)")[![devDeveloperMohsin](https://avatars.githubusercontent.com/u/54468168?v=4)](https://github.com/devDeveloperMohsin "devDeveloperMohsin (6 commits)")[![fakharak](https://avatars.githubusercontent.com/u/9947110?v=4)](https://github.com/fakharak "fakharak (5 commits)")[![shoaib-arshad-ksa](https://avatars.githubusercontent.com/u/175412812?v=4)](https://github.com/shoaib-arshad-ksa "shoaib-arshad-ksa (4 commits)")[![mohsin-devdksa](https://avatars.githubusercontent.com/u/176268511?v=4)](https://github.com/mohsin-devdksa "mohsin-devdksa (3 commits)")[![shoaibarshad6578303](https://avatars.githubusercontent.com/u/61176506?v=4)](https://github.com/shoaibarshad6578303 "shoaibarshad6578303 (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sw-serv-relational-db/health.svg)

```
[![Health](https://phpackages.com/badges/sw-serv-relational-db/health.svg)](https://phpackages.com/packages/sw-serv-relational-db)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)[wildside/userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
