All Projects → Lazer-Database → Lazer Database

Lazer-Database / Lazer Database

Licence: mit
PHP flat file database to store data with JSON

Projects that are alternatives of or similar to Lazer Database

Filecontextcore
FileContextCore is a "Database"-Provider for Entity Framework Core and adds the ability to store information in files instead of being limited to databases.
Stars: ✭ 91 (-64.17%)
Mutual labels:  json, database, file
Filemasta
A search application to explore, discover and share online files
Stars: ✭ 571 (+124.8%)
Mutual labels:  json, database, file
Spimedb
EXPLORE & EDIT REALITY
Stars: ✭ 14 (-94.49%)
Mutual labels:  json, database, db
Framework
Strongly-typed JavaScript object with support for validation and error handling.
Stars: ✭ 136 (-46.46%)
Mutual labels:  json, db
Bible Database
Bible databases as XML, JSON, SQL & SQLITE3 Database format for various languages. Developers can download it freely for their development works. Freely received, freely give.
Stars: ✭ 111 (-56.3%)
Mutual labels:  json, database
Radon
RadonDB is an open source, cloud-native MySQL database for building global, scalable cloud services
Stars: ✭ 1,584 (+523.62%)
Mutual labels:  json, database
Summitdb
In-memory NoSQL database with ACID transactions, Raft consensus, and Redis API
Stars: ✭ 1,295 (+409.84%)
Mutual labels:  json, database
Autoserver
Create a full-featured REST/GraphQL API from a configuration file
Stars: ✭ 188 (-25.98%)
Mutual labels:  json, database
Githubdb
A Lightweight Cloud based JSON Database with a MongoDB like API for Node.
Stars: ✭ 174 (-31.5%)
Mutual labels:  json, database
Xlog
Android logger, pretty, powerful and flexible, log to everywhere, save to file, all you want is here.
Stars: ✭ 2,468 (+871.65%)
Mutual labels:  json, file
Scribble
A tiny Golang JSON database
Stars: ✭ 218 (-14.17%)
Mutual labels:  json, database
Unqlite
An Embedded NoSQL, Transactional Database Engine
Stars: ✭ 1,583 (+523.23%)
Mutual labels:  json, database
Php Jsondb
A PHP Class that reads JSON file as a database. Use for sample DBs
Stars: ✭ 96 (-62.2%)
Mutual labels:  json, database
Marklogic Data Hub
The MarkLogic Data Hub: documentation ==>
Stars: ✭ 113 (-55.51%)
Mutual labels:  json, database
Json Flatfile Datastore
Simple JSON flat file data store with support for typed and dynamic data.
Stars: ✭ 212 (-16.54%)
Mutual labels:  json, database
Elastic
R client for the Elasticsearch HTTP API
Stars: ✭ 227 (-10.63%)
Mutual labels:  json, database
Web Database Analytics
Web scrapping and related analytics using Python tools
Stars: ✭ 175 (-31.1%)
Mutual labels:  json, database
ElegantData
像操作Room一样操作 SharedPreferences 和 File 文件.
Stars: ✭ 18 (-92.91%)
Mutual labels:  file, db
Go Nulltype
Null friendly types
Stars: ✭ 79 (-68.9%)
Mutual labels:  json, database
Dbwebapi
(Migrated from CodePlex) DbWebApi is a .Net library that implement an entirely generic Web API (RESTful) for HTTP clients to call database (Oracle & SQL Server) stored procedures or functions in a managed way out-of-the-box without any configuration or coding.
Stars: ✭ 84 (-66.93%)
Mutual labels:  json, database

Lazer - php flat file database based on JSON files

Build Status Current Version PHP Version Downloads

PHP Library to use JSON files like a database.
Functionality inspired by ORM's

Requirements

  • PHP 7.0+
  • Composer

Installation

Easiest 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.

To add library to your dependencies, execute:

composer require greg0/lazer-database

Tests

Easiest way to run unit tests is to use composer script

composer run-script test

Structure of table files

table_name.data.json - table file with data
table_name.config.json - table file with configuration

Basic Usage

First of all you should define constant LAZER_DATA_PATH containing absolute path to folder with JSON files:

define('LAZER_DATA_PATH', realpath(__DIR__).'/data/'); //Path to folder with tables

Then set up namespace:

use Lazer\Classes\Database as Lazer; // example

Methods

Chain methods
  • setField() - set value of field (alternative to magic __set())
  • limit() - returns results between a certain number range. Should be used right before ending method findAll().
  • orderBy() - sort rows by key in order, can order by more than one field (just chain it).
  • groupBy() - group rows by field.
  • where() - filter records. Alias: and_where().
  • orWhere() - other type of filtering results.
  • with() - join other tables by defined relations
Ending methods
  • getField - get value of field (alternative to magic __get())
  • issetField - check if field is isset (alternative to magic __isset())
  • addFields() - append new fields into existing table
  • deleteFields() - removing fields from existing table
  • set() - get key/value pair array argument to save.
  • save() - insert or Update data (automatically detect if it needs an insert or update).
  • insert() - force an insert.
  • delete() - deleting data.
  • relations() - returns array with table relations
  • config() - returns object with configuration.
  • fields() - returns array with fields name.
  • schema() - returns assoc array with fields name and fields type field => type.
  • lastId() - returns last ID from table.
  • find() - returns one row with specified ID.
  • findAll() - returns rows.
  • asArray() - returns data as indexed or assoc array: ['field_name' => 'field_name']. Should be used after ending method findAll() or find().
  • count() - returns the number of rows. Should be used after ending method findAll() or find().

Create database

Lazer::create('table_name', array(
    'id' => 'integer',
    'nickname' => 'string',
    {field_name} => {field_type}
));

More information about field types and usage in PHPDoc

Remove database

Lazer::remove('table_name');

Check if a database exists

try{
    \Lazer\Classes\Helpers\Validate::table('table_name')->exists();
} catch(\Lazer\Classes\LazerException $e){
    //Database doesn't exist
}

Select

Multiple select

$table = Lazer::table('table_name')->findAll();
    
foreach($table as $row)
{
    print_r($row);
}

Single record select

$row = Lazer::table('table_name')->find(1);

echo $row->id; // or $row->getField('id')

Type ID of row in find() method.

You also can do something like that to get first matching record:

$row = Lazer::table('table_name')->where('name', '=', 'John')->find();

echo $row->id;

Insert

$row = Lazer::table('table_name');

$row->nickname = 'new_user'; // or $row->setField('nickname', 'new_user')
$row->save();

Do not set the ID.

Update

It's very smilar to Inserting.

$row = Lazer::table('table_name')->find(1); //Edit row with ID 1

$row->nickname = 'edited_user'; // or $row->setField('nickname', 'edited_user')
$row->save();

You can also set many field by simple key-value array

$row = Lazer::table('table_name')->find(1); //Edit row with ID 1

$row->set(array(
    'nickname' => 'user',
    'email' => '[email protected]'
));
$row->save();

Remove

Single record deleting

Lazer::table('table_name')->find(1)->delete(); //Will remove row with ID 1

// OR

Lazer::table('table_name')->where('name', '=', 'John')->find()->delete(); //Will remove John from DB

Multiple records deleting

Lazer::table('table_name')->where('nickname', '=', 'edited_user')->delete();

Clear table

Lazer::table('table_name')->delete();

Relations

To work with relations use class Relation

use Lazer\Classes\Relation; // example

Relation types

  • belongsTo - relation many to one
  • hasMany - relation one to many
  • hasAndBelongsToMany - relation many to many

Methods

Chain methods
  • belongsTo() - set relation belongsTo
  • hasMany() - set relation hasMany
  • hasAndBelongsToMany() - set relation hasAndBelongsToMany
  • localKey() - set relation local key
  • foreignKey() - set relation foreign key
  • with() - allow to work on existing relation
Ending methods
  • setRelation() - creating specified relation
  • removeRelation() - remove relation
  • getRelation() - return informations about relation
  • getJunction() - return name of junction table in hasAndBelongsToMany relation

Create relation

Relation::table('table1')->belongsTo('table2')->localKey('table2_id')->foreignKey('id')->setRelation();
Relation::table('table2')->hasMany('table1')->localKey('id')->foreignKey('table2_id')->setRelation();
Relation::table('table2')->hasAndBelongsToMany('table1')->localKey('id')->foreignKey('id')->setRelation(); // Junction table will be crete automaticly

Remove relation

Relation::table('table1')->with('table2')->removeRelation();

Get relation information

You can do it by two ways. Use Standard Database class or Relation but results will be different.

Relation::table('table1')->with('table2')->getRelation(); // relation with specified table
Lazer::table('table1')->relations(); // all relations
Lazer::table('table1')->relations('table2'); // relation with specified table

Description

For some examples please check Examples and Tutorial file. More informations you can find in PHPDoc, I think it's documented very well.

E-mail: [email protected]

If 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].