{"id":26530031,"url":"https://github.com/dabinuss/flatfiledb","last_synced_at":"2025-03-21T17:29:07.982Z","repository":{"id":283659870,"uuid":"952483359","full_name":"dabinuss/FlatFileDB","owner":"dabinuss","description":"FlatFileDB is a lightweight, file-based database for PHP with JSON lines, indexing, schema validation, and transaction logging—ideal for small to medium projects.","archived":false,"fork":false,"pushed_at":"2025-03-21T11:27:30.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-21T12:28:14.674Z","etag":null,"topics":["file-based-database","file-storage","flat-file","flatfile","flatfiledb","json","json-lines","nosql","nosql-database","nosql-databases","php","php-database","php8"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dabinuss.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-21T10:57:51.000Z","updated_at":"2025-03-21T11:25:22.000Z","dependencies_parsed_at":"2025-03-21T12:43:18.386Z","dependency_job_id":null,"html_url":"https://github.com/dabinuss/FlatFileDB","commit_stats":null,"previous_names":["dabinuss/flatfiledb"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabinuss%2FFlatFileDB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabinuss%2FFlatFileDB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabinuss%2FFlatFileDB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabinuss%2FFlatFileDB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dabinuss","download_url":"https://codeload.github.com/dabinuss/FlatFileDB/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244837621,"owners_count":20518664,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["file-based-database","file-storage","flat-file","flatfile","flatfiledb","json","json-lines","nosql","nosql-database","nosql-databases","php","php-database","php8"],"created_at":"2025-03-21T17:29:07.361Z","updated_at":"2025-03-21T17:29:07.971Z","avatar_url":"https://github.com/dabinuss.png","language":"PHP","readme":"FlatFileDB Documentation\n========================\n\nFlatFileDB: A Simple File-Based Database Solution for PHP\n=========================================================\n\nIntroduction\n------------\n\nFlatFileDB is a lightweight, file-based database solution for PHP applications that requires no external database servers such as MySQL or PostgreSQL. This system is perfect for small to medium-sized projects where setting up and maintaining a full-fledged database would be disproportionate in terms of effort.\n\n### Why FlatFileDB?\n\n-   **Easy Installation**: No complex server configuration needed -- simply include the PHP classes and get started.\n-   **Portability**: The entire database consists of plain files, which can be easily moved between different environments.\n-   **No Dependencies**: Works without external libraries or services.\n-   **Transparency**: Records are stored in readable JSON-lines files, making debugging and manual intervention straightforward.\n-   **Performance**: Indexing ensures that data access remains efficient even with larger datasets.\n\n### Use Cases\n\nFlatFileDB is particularly suitable for:\n\n-   Prototypes and proof-of-concept applications\n-   Small web applications with limited data volume\n-   Local tools and utilities\n-   Projects with restricted server resources\n-   Educational settings to learn about database concepts\n\n### Technical Fundamentals\n\nFlatFileDB is based on the following principles:\n\n-   Data stored in JSON-lines format (one JSON object per line)\n-   Indexing of records to speed up access\n-   Transaction-safe logging for traceability of changes\n-   Schemas for validating records\n\nIt is fully implemented in PHP and can be used in any environment that supports PHP.\n\n* * * * *\n\n1\\. Overview and Preparation\n----------------------------\n\nFlatFileDB is a simple file-based database that stores records in JSON-lines files. Its key features include:\n\n-   **CRUD Operations**: Insert, update, delete, and retrieve records.\n-   **Index Management**: An internal index maps record IDs to byte offsets in the file, ensuring efficient access even with large files.\n-   **Transaction Logging**: Each operation is logged, which is particularly useful for error tracing or auditing.\n-   **Compaction**: Redundant (deleted or outdated) records can be removed from the file, and the index can be rebuilt.\n\nBefore you start, you should have the following files in your project:\n\n-   FlatFileDB.php (contains all classes: FlatFileDatabase, FlatFileTableEngine, etc.)\n-   A file in which you write your application logic (e.g. testdb.php)\n\n2\\. Including and Initializing the Database\n-------------------------------------------\n\nFirst, include the database classes and create a database instance.\n\nExample:\n\nphp\n\nKopierenBearbeiten\n\n`\u003c?php\n// testdb.php\n\n// Enable error display (development only)\nini_set('display_errors', 1);\nini_set('display_startup_errors', 1);\nerror_reporting(E_ALL);\n\n// Include FlatFileDB\nrequire_once 'FlatFileDB.php'; // Contains all classes (Namespace: FlatFileDB)\n\n// Use the classes with \"use\"\nuse FlatFileDB\\FlatFileDatabase;\nuse FlatFileDB\\FlatFileDBConstants;\n\n// Create a database instance\n$db = new FlatFileDatabase(FlatFileDBConstants::DEFAULT_BASE_DIR, false);\n\n// Register tables -- for example, here we register the \"users\" and \"products\" tables\n$db-\u003eregisterTables(['users', 'products']);`\n\n3\\. Defining the Schema\n-----------------------\n\nFor each table, you can define a schema. The schema specifies required fields and expected data types. This is especially helpful to ensure that only valid records make it into the database.\n\nExample:\n\nphp\n\nKopierenBearbeiten\n\n`// For the \"users\" table, we define that 'name' and 'email' are required fields.\n// We also specify that 'name' and 'email' are strings, and 'age' is an integer.\n$db-\u003etable('users')-\u003esetSchema(\n    ['name', 'email'],\n    ['name' =\u003e 'string', 'email' =\u003e 'string', 'age' =\u003e 'int']\n);`\n\n4\\. CRUD Operations\n-------------------\n\n### a) Inserting a Record (Insert)\n\nUse the `insertRecord()` method to add a new record. It's important that the ID is unique.\n\nExample:\n\nphp\n\nKopierenBearbeiten\n\n`// Insert a new user\n$success = $db-\u003etable('users')-\u003einsertRecord('user123', [\n    'name'  =\u003e 'Alice Johnson',\n    'email' =\u003e 'alice@example.com',\n    'age'   =\u003e 32\n]);\n\nif ($success) {\n    echo \"User successfully inserted.\";\n} else {\n    echo \"Error: A user with this ID already exists.\";\n}\n\n// After writing, manually commit the index\n// so the current index file is used on page reload\n$db-\u003ecommitAllIndexes();`\n\n### b) Updating a Record (Update)\n\nUse `updateRecord()` to update an existing record. Older versions of the record are marked as deleted, and a new entry is appended.\n\nExample:\n\nphp\n\nKopierenBearbeiten\n\n`// Update an existing user\n$success = $db-\u003etable('users')-\u003eupdateRecord('user123', [\n    'name'  =\u003e 'Alice J.',\n    'email' =\u003e 'alice_j@example.com',\n    'age'   =\u003e 33\n]);\n\nif ($success) {\n    echo \"User successfully updated.\";\n} else {\n    echo \"Error: User not found.\";\n}\n\n// Commit the index\n$db-\u003ecommitAllIndexes();`\n\n### c) Deleting a Record (Delete)\n\nUse `deleteRecord()` to delete a record. The record is marked as deleted, and the index is updated accordingly.\n\nExample:\n\nphp\n\nKopierenBearbeiten\n\n`// Delete a user\n$success = $db-\u003etable('users')-\u003edeleteRecord('user123');\n\nif ($success) {\n    echo \"User successfully deleted.\";\n} else {\n    echo \"Error: User could not be found.\";\n}\n\n// Save the index\n$db-\u003ecommitAllIndexes();`\n\n### d) Retrieving a Record (Select)\n\nUse `selectRecord()` to retrieve a single record, and `selectAllRecords()` to fetch all active (non-deleted) records.\n\nExample:\n\nphp\n\nKopierenBearbeiten\n\n`// Retrieve a single user\n$user = $db-\u003etable('users')-\u003eselectRecord('user123');\nif ($user) {\n    print_r($user);\n} else {\n    echo \"User not found.\";\n}\n\n// Retrieve all active users\n$allUsers = $db-\u003etable('users')-\u003eselectAllRecords();\nforeach ($allUsers as $user) {\n    echo \"ID: {$user['id']}, Name: {$user['name']}\u003cbr\u003e\";\n}`\n\n5\\. Additional Functions\n------------------------\n\n### a) Index Management\n\n**compactTable()**:\n\n-   This operation \"cleans up\" the data file by removing outdated and deleted entries and rebuilding the index.\n-   Usage: Run compaction manually or periodically, as it can be relatively intensive.\n\nExample:\n\nphp\n\nKopierenBearbeiten\n\n`$db-\u003etable('users')-\u003ecompactTable();\necho \"Table 'users' has been compacted.\";`\n\n### b) Backup and Clearing the Database\n\n**Creating a Backup**:\n\n-   Use `createBackup($backupDir)` to back up all tables.\n\nExample:\n\nphp\n\nKopierenBearbeiten\n\n`$backupResults = $db-\u003ecreateBackup(FlatFileDBConstants::DEFAULT_BACKUP_DIR);\necho \"Backup has been created.\";`\n\n**Clearing the Database**:\n\n-   Use `clearDatabase()` to delete all data, indexes, and logs.\n\nExample:\n\nphp\n\nKopierenBearbeiten\n\n`$db-\u003eclearDatabase();\necho \"The database has been cleared.\";`\n\n6\\. Integrating into Your HTML Interface\n----------------------------------------\n\nTypically, you combine the operations described above with an HTML form to allow user interactions. A sample workflow might look like this:\n\n**Submitting a Form via POST**:\n\n-   Each action (insert, update, delete, search, backup, compaction) is defined by a hidden field `action`, for example:\n\nhtml\n\nKopierenBearbeiten\n\n`\u003cform method=\"post\"\u003e\n    \u003cinput type=\"hidden\" name=\"action\" value=\"insert_user\"\u003e\n    \u003c!-- Other fields for user ID, name, etc. --\u003e\n    \u003cbutton type=\"submit\"\u003eAdd User\u003c/button\u003e\n\u003c/form\u003e`\n\n**Executing the PHP Logic**:\n\n-   In your PHP code, read `$_POST['action']` and run the corresponding case in a switch statement (as in the examples above).\n\n**Feedback and Updates**:\n\n-   After the operation, commit the index (or optionally compact the table). Then display a success message. On page reload, the current data is loaded from the file (or from the persisted index).","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdabinuss%2Fflatfiledb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdabinuss%2Fflatfiledb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdabinuss%2Fflatfiledb/lists"}