Search by

presentkim / libdb

PresentKim

Async SQL library for axolotl-pm/PocketMine-MP: typed table objects, a query DSL and raw SQL on SQLite and MySQL worker threads

Package info

github.com/presentkim-pm/libdb

pkg:composer/presentkim/libdb

Statistics

Installs: 8

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

dev-main 2026-12-03 13:31 UTC

This package is auto-updated.

Last update: 2026-12-03 13:31:23 UTC


README

Packagist CI Stars License


Logo

libdb

Async SQL for PocketMine-MP 5: write the class of a row and libdb does the rest, with a typed query DSL and raw SQL for special cases, on SQLite and MySQL

Packagist · Documentation · Getting started · Changelog · Report a bug · Request a feature

A tick is one 50 ms step of the server. libdb does not send an UPDATE the moment you change a property: it collects the changes and sends them in one batch at the start of the next tick, the tick flush (glossary).

#[TableName("users")]
final class User extends Entity{
    #[Id, AutoIncrement]
    public int $id;
    #[Varchar(32), Unique]
    public string $name;
    public int $coins = 0;
}

$repo = $db->entities(User::class);
$user = yield from $repo->findById(1);
$user->coins += 10; // that is all: the next tick flush sends the UPDATE, together with the other changes

$new = new User();
$new->name = "Steve";
$repo->persist($new); // INSERTs that wait for the same tick flush share one multi-row statement

New to libdb? Go to Getting started: one page that builds a complete plugin, file by file, and runs it. Recommended setup: axolotl-pm/PocketMine-MP 5.51 or newer with the PocketMine PHP 8.4 binary, the combination that CI runs on a real server. libdb is developed and tested on axolotl-pm 5.51+, Composer allows any PocketMine-MP 5, and stock pocketmine/pocketmine-mp is not tested, see Compatibility. The glossary explains the other words.

🌟 Why libdb?

An async SQL library for plugins that replaces poggit/libasynql. Queries run on worker threads and come back as await-generator generators, so the server thread is never blocked.

  • Write the class, nothing else : typed public properties and a few attributes. libdb creates the table, loads rows as objects and tracks your changes. The changes that wait for the tick flush are sent in one batch, and a loaded row stays in memory only while your code holds it
  • Typed query DSL and raw SQL : joins, aggregates and upserts with typed columns, or plain SQL with ? and :name parameters when you want full control
  • SQLite and MySQL : the same code runs on both. A MySQL pool is configurable, SQLite uses one connection
  • Transactions, migrations, libasynql-friendly : $db->transaction(), numbered $db->migrate() steps, and the same config section and .sql query files as libasynql

📦 Requirements

Requirement Version
PHP 8.4 or newer, with sqlite3 and/or mysqli for the database you use
Server Developed and tested on axolotl-pm/PocketMine-MP 5.51 or newer, with pmmpthread (CI runs a real 5.51.3 server). Composer allows any PocketMine-MP 5 (composer.json requires pocketmine/pocketmine-mp ^5.0; 5.51 is the version of development and tests, not an enforced minimum). Stock pocketmine/pocketmine-mp is not tested and may ship a PHP older than 8.4: see Compatibility
Library sof3/await-generator 3.6.1 or newer (Composer installs it)
Database SQLite 3.24 or newer (libdb keeps its statements below the 999-parameter limit of SQLite before 3.32), or MySQL 8.0 or newer (CI runs 8.0, 8.4 and 9.7). MariaDB 10.11 and 11.4 run the same mysql tests in CI. See Compatibility

Important

libdb needs PHP 8.4, so your server must run PHP 8.4. Run the PHP that starts your server with -v. If it is older, get the PocketMine PHP 8.4 binary: see Getting PHP 8.4. On an older PHP, libdb cannot load: Composer stops with Your Composer dependencies require a PHP version ">= 8.4.0".

Install

libdb has no tagged release yet, so the usual composer require presentkim/libdb:~0.1.0 finds no version until 0.1.0 is tagged. Until then:

composer require presentkim/libdb:dev-main

The package is on Packagist (listed as dev-main, no version is tagged yet), and Composer accepts an explicit dev-main for a direct requirement. If it still complains about stability, add "minimum-stability": "dev" and "prefer-stable": true to your composer.json.

After 0.1.0 is tagged, depend on the tag, not on the branch: replace dev-main with ~0.1.0 (patch releases only, see Stability), remove minimum-stability again if you added it, and run composer update presentkim/libdb:

composer require presentkim/libdb:~0.1.0

libdb is a virion: a library that is packed into your plugin's phar. How to pack it is part of Getting started (a short build.php, no extra tools). To publish a plugin, shade the libraries into a namespace of your own, which Publishing your plugin explains, or build it from presentkim-pm/plugin-template, whose release workflow does it for you. Do not shade pocketmine\*, pmmp\thread\* or the PHP extensions. (If your development computer does not have the PocketMine PHP 8.4, add --ignore-platform-req=php --ignore-platform-req='ext-*' to composer install, see Step 0.)

🚀 Quick start

The snippet at the top is the idea: a class is a table, and changing a property is the update. A plugin around it is not short enough for a README, and a half-plugin would not build, so the complete one is Getting started: "CoinsHomes" gives a player coins for every block they break and keeps homes, with every file shown whole. You need three commands (after you have saved the files; build.php is the short script of Step 1, which you save next to the plugin folder):

composer install --no-dev --ignore-platform-req=php --ignore-platform-req='ext-*'   # in the plugin folder: downloads libdb
                                                                # (the flags skip the check of your computer's PHP; the server's PHP runs the plugin)
php -d phar.readonly=0 build.php CoinsHomes CoinsHomes.phar     # build.php: from Step 1 of Getting started; packs the plugin into one file
cp CoinsHomes.phar /path/to/server/plugins/                     # then start the server

The fast path at the top of that page lists the eight steps in order. An error in the server log? Where errors show up says what each message means.

📚 Documentation

New here? Read Getting started, nothing else is needed for a first plugin. The documentation index says which page is for whom (new plugin, moving from libasynql, running a server) and in which order to read them.

Page What is in it
Getting started The full first plugin (coins and homes), await-generator primer, building and running it, errors, shutdown
Configuration Every config key with type and default, sizing the pool, PHP constructors
Guarantees at a glance Every guarantee (threads, ordering, transactions, shutdown, several servers) in one table, with the test that proves it
Entities Attributes, types, lifecycle, when SQL is sent, guarantees, finders, one-to-many rows, performance tips
Queries Table classes, the typed DSL, raw SQL with list parameters, query files
Injection-safe queries Bound values, identifiers, raw SQL, query files, LIKE, what is logged
Transactions Threads, ordering, isolation, read-modify-write, retries
Several servers Cache staleness, #[Version], #[Counter], findOrCreate races
Errors The exception hierarchy, which call throws what, one table of error codes, failed flushes, outcome-unknown recipe, shutdown time
Schema Table creation and the start-up check, adding columns, migrations, collation
SQLite and MySQL differences String length, integer width, JSON, collation, LIKE, upsert, generated ids, DDL in transactions, IN limits
Moving from libasynql What carries over, a worked before/after plugin, call mapping, behaviour differences, checklist
Observability Statistics, slow queries, error hook, debug log
Operating on MySQL and SQLite Idle timeouts, max_allowed_packet, lock waits, the grants the account needs, failover and proxies, MySQL backup and restore, SQLite durability and backups
Performance Measured numbers, sizing the pool, batches, memory
API reference Every public class, method and attribute with its @throws, generated from the docblocks
Stability What is public API, the data formats libdb writes, deprecation
Upgrading libdb Upgrading between minors, reading the Changelog, mixed versions on one database, rollback
Compatibility Supported PHP, SQLite, MySQL and MariaDB versions, what CI tests
Glossary Tick, flush, batch, managed, detached, identity map, WAL, ...

The API reference lists every public method. It starts with an index of the classes: begin with Database and EntityRepository. New to the words? The glossary explains tick, flush, virion and the rest.

Security

Report a vulnerability privately through GitHub, not in a public issue: see SECURITY.md.

Stability

libdb is pre-1.0: a minor version (0.x) may contain breaking changes, a patch version does not. The stability policy says what counts as public API, which data formats libdb writes into your database and keeps stable (type mapping, the libdb_migrations table, the query-file syntax), and that a deprecated feature works for at least one minor version before it is removed. Every change is listed in the Changelog.

🔁 Moving from libasynql

Your database: config section and your -- # query files work unchanged: load them with $db->loadQueries() and call $db->named("homes.get", $args)->fetchAll(). The migration guide has a worked before/after plugin, the full call mapping, what is different, and a checklist.

🧪 For contributors

You do not need any of this to use libdb.

composer install --ignore-platform-req='ext-*'
composer analyse   # PHPStan level 9
composer test      # PHPUnit

The server's native extensions are not needed for the tests, so ext-* requirements are ignored on install. The example plugins of the docs live in tests/docs. The tests check that the pages of Getting started and the libasynql guide show those files exactly as they are, PHPStan analyses them, and a few recipes (races, schema change, query files and entities side by side) run against a database. CI also builds the Getting started plugin and starts it on a real server (tests/server/run.sh docs). Short snippets on the other pages (transactions, errors, observability, queries) are not executed: they were written against the source, but a change of the API can leave one behind.

📄 License

Distributed under the MIT License. See LICENSE for more information.