Git Product home page Git Product logo

ydb's Introduction

YDB

YDB is a simple PHP utility library to use Yaml as a flat file databases

Build Status

YDB is a PHP library used by Morebec in some client projects to allow the use of yaml files as a database. Some of our clients have technical abilities to view and edit YAML files and therefore want their projects to save all their data using this format in such a way that when they require it, they can manually update the data themselves. It also provides the benifit of applying VCS on the database's data. YDB therfore provides an easy way to communicate with such a database in a simple and efficient way. It offers functionalities, like table management, table schema updates and data indexing for improved performance, as well as a custom SQL like language called YQL that allows to query the database in a more user-friendly manner.

Installation

To install the library in a project, add this to your composer.json file:

{
    // ...
    "repositories": [
        {
            "url": "https://github.com/Morebec/YDB.git",
            "type": "git"
        }
    ],

    "require": {
        // ...
        "morebec/ydb": "dev-master"
    }
    // ...
}

Usage

Create a Database

A database can be created by doing the following:

// Create a configuration object for the database
$config = new DatabaseConfig(
    Directory::fromStringPath(__DIR__ . '/../_data/test-db')
);

// To enable logging
$config->enabledLogging();

// To disable indexing
$config->disableIndexing();

// Create the database
$database = new Database($config);

This would result in the following file structure at the directory of the database:

test-db/
    logs/
    tables/
        table-1/
            ...
        table-2/
            ...

Create a Table

Tables are created using a TableSchema that defines the columns of the table.

A table schema is created this way

$schema = new TableSchema('test-query-record', [
    new Column('id', ColumnType::STRING(), true /* indexed */),
    new Column('first_name', ColumnType::STRING()),
    new Column('last_name', ColumnType::STRING()),
    new Column('age', ColumnType::INTEGER())
]);

And to create the table:

// This function will return a table object that can be used to be queried
$table = $database->createTable($schema);

Create a Record

T add a new record to the database, one must use a Record Object. A record can be constructed as follows:

$r = new Record(
    RecordId::generate(), // This will generate a Uuidv4 id.
    [
        'first_name' => 'Barney',
        'last_name' => 'Stinson',
        'age' => 31
    ]
);

// And then add the record to the table
$table->addRecord($record);

Note on ids: If you want to have a different type of id, simply create a class implementing the RecordIdInterface.

Query a Record

In order to query a record, one must create a Query Object:

// Multiple Criteria
$r = $table->queryOne(
    new Query([
        new Criterion('first_name', Operator::STRICTLY_EQUAL(), 'James'),
        new Criterion('last_name', Operator::STRICTLY_EQUAL(), 'Bond'),
        new Criterion('age', Operator::GREATER_OR_EQUAL(), 42)
    ]);
);

// Helper static methods
$r = $table->queryOne(Query::findById($record->getId()));

$r = $table->queryOne(
    Query::findByField('first_name', Operator::STRICTLY_EQUAL(), 'James')
);

However the easiest way is to use the Query builder:

// This query will find all records that have 'James Bond' as their full name or
// that are 35 years old or less
$query = QueryBuilder::find('first_name', Operator::STRICTLY_EQUAL(), 'James')
                     ->and('last_name', Operator::STRICTLY_EQUAL(), 'Bond')
                     ->or('age', Operator::LESS_OR_EQUAL(), 35)
                     ->build()
;
$r = $table->query($query);

Running Tests

# Will run all tests including performance tests
composer test

# Will run all tests excluding performance tests
composer test-no-performance

ydb's People

Contributors

andy-lsh avatar anna0105n avatar jwillp avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

anna0105n

ydb's Issues

Include a migration system

Include a migration system with console commands

  • to run migrations
  • to get status
  • to rollback migrations
  • to create migrations

Add 'unique' column property

To indicate the values of this column should always be unique
Implementation tips:

  • A unique column should automatically be considered indexed.
  • Make the check on insert and update operations

Create a DatabasePerformanceTest that compares indexed vs non indexed query

Querying indexed data should be faster than non-indexed data, to prove that, we'd need to create a test that would create a table with two columns one indexed and one non-indexed.
Both columns would hold the same values for a big number of records.
The test would assert that the query to find one or multiple records should be faster than
on indexed column than on non-indexed columns.

Compare delta in assertions for DatabasePerformanceTest

Sometimes the record creation test fails.
Meaning it takes more than 1.5 seconds to insert 1000 records with the following schema:

 $schema = new TableSchema($tableName, [
            new Column('id', ColumnType::STRING(), true /* indexed */),
            new Column('first_name', ColumnType::STRING()),
            new Column('last_name', ColumnType::STRING()),
            new Column('age', ColumnType::INTEGER()),
            new Column('indexed_column', ColumnType::INTEGER(), true /* indexed */)
        ]);

It would be better for performane improvement debuging to change the assertions
to compare agains the delta rather than doing a assertTrue
That way in the tests result we would a more verbose output along the lines
of failed to assert that 1.67, is less than 1.5

So, in summary, we'd need to change assertTrue to assertLessThan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.