Why Choose SleekDB? A Lightweight PHP NoSQL Database Explained
This article introduces SleekDB, a dependency‑free PHP flat‑file NoSQL database that stores data as JSON, outlines its installation via Composer, highlights key features such as powerful queries, full‑text search, pagination, joins and caching, and provides a practical code example for a blog system.
Overview
SleekDB is a lightweight, dependency‑free PHP flat‑file NoSQL database. Data is stored as plain JSON files, which makes the database easy to read, back up (by copying the folder), and migrate. It targets use‑cases that involve a few gigabytes of data under low to medium traffic. The library requires PHP 8.4+ with the ext-json and ext-mbstring extensions.
Installation
Install the package via Composer: composer require sleekdb/sleekdb Composer adds the library to the autoloader, so no additional configuration is needed.
Key Features
Simple data management – All records are stored as JSON, enabling human‑readable backups and straightforward file‑system operations.
Rich query language
Nested property queries, e.g. user.address.city.
Conditional operators: where, orWhere, LIKE, IN, BETWEEN, EXISTS, etc.
Full‑text search with relevance scoring via the searchScore field and customizable searchable columns.
Pagination using limit and skip, sorting, distinct results, grouping ( groupBy) and aggregation functions.
Join‑like relationships through closure‑based store linking.
Built‑in query cache to accelerate repeated queries.
Concurrency handling – Each store uses multiple JSON files combined with file‑locking, providing better concurrent read/write support than single‑file flat databases. It is suitable for low‑to‑medium traffic sites but not for high‑write‑throughput scenarios.
Developer‑friendly API – Methods are concise and chainable. Comprehensive documentation and examples cover CRUD, complex queries, and custom functions.
Example: Blog Article Store
The following code demonstrates creating a store, inserting a document, performing a full‑text search, and retrieving the latest articles with pagination.
use SleekDB\Store;
// Initialise a store named "articles" in the "database" directory
$articles = new Store("articles", __DIR__ . "/database");
// Insert a new article document
$articles->insert([
"title" => "SleekDB Getting Started Guide",
"content" => "This is an article about a lightweight PHP database...",
"author" => [
"name" => "Tinywan",
"email" => "[email protected]"
],
"tags" => ["php", "nosql"],
"created_at" => date("Y-m-d H:i:s")
]);
// Full‑text search across "title" and "content" fields, sorted by relevance
$results = $articles->search([
"title",
"content"
], "PHP database", ["searchScore" => "DESC"]);
// Retrieve the 10 most recent articles (page 1)
$latest = $articles->createQueryBuilder()
->orderBy(["created_at" => "DESC"])
->limit(10)
->skip(0)
->fetch();This workflow requires no external database server, keeping the application code clean and maintainable.
Reference
Official documentation site: https://sleekdb.github.io
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
