FenyDB is a simple, high-performance flat-file JSON database engine for PHP. Ideal for small projects, prototypes, or applications that need a no-SQL solution without the overhead of a dedicated database server.
- Zero Dependency: Pure PHP, no extra extensions or servers required.
- Fast Lookups: Uses inverted indexing for O(1) searches on specific columns.
- Human Readable: Transparent storage in standard JSON files.
- Automatic Metadata: Handles
id,created_at, andupdated_atautomatically. - Simple Migration: Moving data is as easy as copying folders.
Simply include the FenyDB.php file in your project.
require_once 'FenyDB.php';
// Initialize the database in a specific directory
$db = new FenyDB('my_database');Manage your database structure easily.
// Create a new table
$db->createTable('users');
// Drop a table (deletes all data)
$db->dropTable('old_logs');
// Delete the entire database directory
$db->dropDatabase();Indexing is crucial for performance. You can define which columns should be indexed.
// Parameters: (tableName, columnName, type, is_indexed)
$db->createColumn('users', 'username', 'string', true);
$db->createColumn('users', 'email', 'string', true);
$db->createColumn('users', 'bio', 'text', false); // No index for bio
$db->createColumn('users', 'profile_pic', 'image', false); // Arrays and images are not indexableData is sanitized according to the structure defined by your columns.
$userId = $db->insert('users', [
'username' => 'antigravity',
'email' => 'ai@example.com',
'bio' => 'Coding assistant extraordinaire',
'extra_field' => 'This will be ignored' // Only defined columns are saved
]);
echo "Created user with ID: " . $userId;Retrieve data by ID or fetch all records.
// Fetch a single record by its ID
$user = $db->findById('users', 1);
// Fetch all records from a table
$allUsers = $db->getAll('users');Search for records efficiently using indexes.
// find() returns an array of IDs matching the value
$ids = $db->find('users', 'username', 'antigravity');
if (!empty($ids)) {
$user = $db->findById('users', $ids[0]);
}Updates are handled by ID. Indexes are automatically updated if the value changes.
$db->update('users', 1, [
'username' => 'antigravity_pro',
'email' => 'ai@example.com',
'bio' => 'Upgraded coding assistant'
]);Deleting a record also cleans up related entries in the indexes.
$db->delete('users', 1);Here is a complete workflow for managing a simple blog system.
require_once 'FenyDB.php';
$db = new FenyDB('blog_data');
// Setup
$db->createTable('posts');
$db->createColumn('posts', 'title', 'string', true);
$db->createColumn('posts', 'author', 'string', true);
$db->createColumn('posts', 'content', 'text', false);
// Create
$postId = $db->insert('posts', [
'title' => 'The Future of AI',
'author' => 'Alice',
'content' => 'AI is evolving rapidly...'
]);
// Search
$authoredByAlice = $db->find('posts', 'author', 'Alice');
// Display
foreach ($authoredByAlice as $id) {
$post = $db->findById('posts', $id);
echo "Title: " . $post['title'] . "\n";
}
// Update
$db->update('posts', $postId, [
'title' => 'The Future of AI (Updated)',
'author' => 'Alice',
'content' => 'Revised content.'
]);
// Cleanup
// $db->dropDatabase();FenyDB maintains the following structure for efficiency:
/my_database
/posts
1.json # Record data
2.json # Record data
structure.json # Column definitions & types
/index
title.json # Inverted index for 'title'
author.json # Inverted index for 'author'
MIT License. Free to use and modify.