{"id":13504364,"url":"https://github.com/Lazer-Database/Lazer-Database","last_synced_at":"2025-03-29T21:30:32.585Z","repository":{"id":7037350,"uuid":"8313554","full_name":"Lazer-Database/Lazer-Database","owner":"Lazer-Database","description":"PHP flat file database to store data with JSON","archived":false,"fork":false,"pushed_at":"2023-11-28T16:45:25.000Z","size":395,"stargazers_count":281,"open_issues_count":0,"forks_count":37,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-03-20T21:24:47.586Z","etag":null,"topics":["database","db","file","flat","json","php"],"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/Lazer-Database.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":"anant-svc","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2013-02-20T13:27:27.000Z","updated_at":"2025-02-27T14:01:23.000Z","dependencies_parsed_at":"2024-01-13T19:20:20.988Z","dependency_job_id":"b873f7d1-d06c-4841-9ec5-b8ac6a65064f","html_url":"https://github.com/Lazer-Database/Lazer-Database","commit_stats":{"total_commits":298,"total_committers":14,"mean_commits":"21.285714285714285","dds":"0.38926174496644295","last_synced_commit":"bc7b264a31c4b0f2aeaa7ede4cf3726550dd624f"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lazer-Database%2FLazer-Database","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lazer-Database%2FLazer-Database/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lazer-Database%2FLazer-Database/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lazer-Database%2FLazer-Database/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lazer-Database","download_url":"https://codeload.github.com/Lazer-Database/Lazer-Database/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245938233,"owners_count":20697008,"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":["database","db","file","flat","json","php"],"created_at":"2024-08-01T00:00:35.431Z","updated_at":"2025-03-29T21:30:32.565Z","avatar_url":"https://github.com/Lazer-Database.png","language":"PHP","readme":"Lazer - php flat file database based on JSON files\n====================================\n[![Unit](https://github.com/Lazer-Database/Lazer-Database/actions/workflows/unit.yml/badge.svg)](https://github.com/Lazer-Database/Lazer-Database/actions/workflows/unit.yml)\n[![Current Version](https://img.shields.io/packagist/v/greg0/lazer-database.svg)](https://packagist.org/packages/greg0/lazer-database#latest)\n![PHP Version](https://img.shields.io/packagist/php-v/greg0/lazer-database/2.0.1)\n![Downloads](https://poser.pugx.org/greg0/lazer-database/d/total.svg)\n\nPHP Library to use JSON files like a database.   \nFunctionality inspired by ORM's\n\nRequirements\n------------\n- PHP 7.1+\n- Composer\n\nInstallation\n------------\nEasiest way to install `Lazer Database` is to use Composer. Of course you can use your own autoloader but you must configure it properly by yourself. You can find my Package on [Packagist.org](https://packagist.org/packages/greg0/lazer-database).\n\nTo add library to your dependencies, execute:\n```\ncomposer require greg0/lazer-database\n```\n\nTests\n-----\nEasiest way to run unit tests is to use composer script \n```\ncomposer run test\n```\n\nYou can also use docker \n\n```shell\ndocker build -t lazer-db .\ndocker run -it --rm lazer-db\n```\n\nStructure of table files\n-------\n\n`table_name.data.json` - table file with data   \n`table_name.config.json` - table file with configuration \n\n    \nBasic Usage\n-----------\n\nFirst of all you should define constant `LAZER_DATA_PATH` containing absolute path to folder with JSON files:\n```php\ndefine('LAZER_DATA_PATH', realpath(__DIR__).'/data/'); //Path to folder with tables\n```\n\nThen set up namespace:\n```php\nuse Lazer\\Classes\\Database as Lazer; // example\n```\n\n### Methods\n\n##### Chain methods\n\n- `setField()` - set value of field (alternative to magic `__set()`)\n- `limit()`    - returns results between a certain number range. Should be used right before ending method `findAll()`.\n- `orderBy()`  - sort rows by key in order, can order by more than one field (just chain it). \n- `groupBy()`  - group rows by field.\n- `where()`    - filter records. Alias: `and_where()`.\n- `orWhere()`  - other type of filtering results. \n- `with()`     - join other tables by defined relations\n\n##### Ending methods\n\n- `getField`        - get value of field (alternative to magic `__get()`)\n- `issetField`      - check if field is isset (alternative to magic `__isset()`)\n- `addFields()`     - append new fields into existing table\n- `deleteFields()`  - removing fields from existing table\n- `set()`           - get key/value pair array argument to save.\n- `save()`          - insert or Update data (automatically detect if it needs an insert or update).\n- `insert()`        - force an insert.\n- `delete()`        - deleting data.\n- `relations()`     - returns array with table relations\n- `config()`        - returns object with configuration.\n- `fields()`        - returns array with fields name.\n- `schema()`        - returns assoc array with fields name and fields type `field =\u003e type`.\n- `lastId()`        - returns last ID from table.\n- `find()`          - returns one row with specified ID.\n- `findAll()`       - returns rows.\n- `asArray()`       - returns data as indexed or assoc array: `['field_name' =\u003e 'field_name']`. Should be used after ending method `findAll()` or `find()`.\n- `count()`         - returns the number of rows. Should be used after ending method `findAll()` or `find()`.\n\n### Create database\n```php\nLazer::create('table_name', array(\n    'id' =\u003e 'integer',\n    'nickname' =\u003e 'string',\n    {field_name} =\u003e {field_type}\n));\n```\nMore information about field types and usage in PHPDoc\n\t\n### Remove database\n```php\nLazer::remove('table_name');\n```\n\n### Check if a database exists\n```php\ntry{\n    \\Lazer\\Classes\\Helpers\\Validate::table('table_name')-\u003eexists();\n} catch(\\Lazer\\Classes\\LazerException $e){\n    //Database doesn't exist\n}\n```\n\n### Select\n\n#### Multiple select\n```php\n$table = Lazer::table('table_name')-\u003efindAll();\n    \nforeach($table as $row)\n{\n    print_r($row);\n}\n```\n#### Single record select\n```php\n$row = Lazer::table('table_name')-\u003efind(1);\n\necho $row-\u003eid; // or $row-\u003egetField('id')\n```\nType ID of row in `find()` method.\n\nYou also can do something like that to get first matching record:\n```php\n$row = Lazer::table('table_name')-\u003ewhere('name', '=', 'John')-\u003efind();\n\necho $row-\u003eid;\n```\n\n### Insert\n```php\n$row = Lazer::table('table_name');\n\n$row-\u003enickname = 'new_user'; // or $row-\u003esetField('nickname', 'new_user')\n$row-\u003esave();\n```\nDo not set the ID.\n\n### Update\n\nIt's very smilar to `Inserting`.\n```php\n$row = Lazer::table('table_name')-\u003efind(1); //Edit row with ID 1\n\n$row-\u003enickname = 'edited_user'; // or $row-\u003esetField('nickname', 'edited_user')\n$row-\u003esave();\n```\n\nYou can also set many field by simple key-value array\n```php\n$row = Lazer::table('table_name')-\u003efind(1); //Edit row with ID 1\n\n$row-\u003eset(array(\n    'nickname' =\u003e 'user',\n    'email' =\u003e 'user@example.com'\n));\n$row-\u003esave();\n```\n\n\n### Remove\n\n#### Single record deleting\n```php\nLazer::table('table_name')-\u003efind(1)-\u003edelete(); //Will remove row with ID 1\n\n// OR\n\nLazer::table('table_name')-\u003ewhere('name', '=', 'John')-\u003efind()-\u003edelete(); //Will remove John from DB\n\n```\n#### Multiple records deleting\n```php\nLazer::table('table_name')-\u003ewhere('nickname', '=', 'edited_user')-\u003edelete();\n```\n#### Clear table\n```php\nLazer::table('table_name')-\u003edelete();\n```\n### Relations\n\nTo work with relations use class Relation\n```php\nuse Lazer\\Classes\\Relation; // example\n```\n\n#### Relation types\n\n- `belongsTo` - relation many to one\n- `hasMany` - relation one to many\n- `hasAndBelongsToMany` - relation many to many\n\n#### Methods\n\n##### Chain methods\n\n- `belongsTo()` - set relation belongsTo\n- `hasMany()` - set relation hasMany\n- `hasAndBelongsToMany()` - set relation hasAndBelongsToMany\n- `localKey()` - set relation local key\n- `foreignKey()` - set relation foreign key\n- `with()` - allow to work on existing relation\n\n##### Ending methods\n\n- `setRelation()` - creating specified relation\n- `removeRelation()` - remove relation\n- `getRelation()` - return informations about relation\n- `getJunction()` - return name of junction table in `hasAndBelongsToMany` relation\n\n#### Create relation\n```php\nRelation::table('table1')-\u003ebelongsTo('table2')-\u003elocalKey('table2_id')-\u003eforeignKey('id')-\u003esetRelation();\nRelation::table('table2')-\u003ehasMany('table1')-\u003elocalKey('id')-\u003eforeignKey('table2_id')-\u003esetRelation();\nRelation::table('table2')-\u003ehasAndBelongsToMany('table1')-\u003elocalKey('id')-\u003eforeignKey('id')-\u003esetRelation(); // Junction table will be crete automaticly\n```\n\n#### Remove relation\n```php\nRelation::table('table1')-\u003ewith('table2')-\u003eremoveRelation();\n```\n#### Get relation information\nYou can do it by two ways. Use Standard Database class or Relation but results will be different.\n```php\nRelation::table('table1')-\u003ewith('table2')-\u003egetRelation(); // relation with specified table\nLazer::table('table1')-\u003erelations(); // all relations\nLazer::table('table1')-\u003erelations('table2'); // relation with specified table\n```\n\nDescription\n-----------\nFor some examples please check \n[Examples](docs/examples.md) and [Tutorial](docs/tutorial.md) file.\nMore informations you can find in PHPDoc, I think it's documented very well.\n\nE-mail: \u003cgerg0sz92@gmail.com\u003e\n\nIf you like and using/want to use my repo or you have any suggestions I will be greatful for sending me few words on e-mail.\n","funding_links":["https://github.com/sponsors/anant-svc"],"categories":["Databases"],"sub_categories":["Key-Value Databases"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLazer-Database%2FLazer-Database","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FLazer-Database%2FLazer-Database","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLazer-Database%2FLazer-Database/lists"}