Giter Site home page Giter Site logo

plasmaphp / schemas Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 2.0 416 KB

Schemas map any data source into a PHP object.

Home Page: https://plasmaphp.github.io/schemas/

License: Apache License 2.0

PHP 100.00%
php databse php7 plasma php-library schemas

schemas's Introduction

Schemas CI status

Schemas is a simple Object Relational Mapper (ORM) for Plasma. Schemas maps any data source into a PHP object.

Getting Started

Schemas can be installed through composer.

composer require plasma/schemas

You first need to create a Plasma client and then create a Repository (which acts like a client) with the created client. Then you need to create your schema classes and the directory for these schema classes. You will need to register these directories to the Repository.

Directories build schemas from query results and interface with the repository for queries.

After that, each call onto the Repository query or execute methods will give you a dedicated SchemaCollection with the Schema instances. A call to Repository::prepare will give you, if successful, a wrapped Statement instance. The wrapper has the same purpose as the Repository.

$loop = \React\EventLoop\Factory::create();
$factory = new \Plasma\Drivers\MySQL\DriverFactory($loop, array());

$client = \Plasma\Client::create($factory, 'root:1234@localhost');
$repository = new \Plasma\Schemas\Repository($client);

/**
 * Our example table "users" consists of two columns:
 * - id ; auto incremented integer (length 12) primary
 * - name ; varchar(255) utf8mb4_generl_ci
 */
class Users extends \Plasma\Schemas\AbstractSchema {
    public $id;
    public $name;
    
    /**
     * Returns the schema definition.
     * @return \Plasma\Schemas\ColumnDefinitionInterface[]
     */
    static function getDefinition(): array {
        return array(
            // A generic column definition builder
            // solely for ease of use and does not
            // have to be used.
            // Any Plasma Column Definition
            // can be used.
            
            static::getColDefBuilder()
                ->name('id')
                ->type('INTEGER')
                ->length(12)
                ->autoIncrement()
                ->primary()
                ->getDefinition(),
            static::getColDefBuilder()
                ->name('name')
                ->type('VARCHAR')
                ->length(255)
                ->getDefinition()
        );
    }
    
    /**
     * Returns the name of the table.
     * @return string
     */
    static function getTableName(): string {
        return 'users';
    }
    
    /**
     * Returns the name of the identifier column (primary or unique), or null.
     * @return string|null
     */
    static function getIdentifierColumn(): ?string {
        return 'id';
    }
}

// null is the SQL grammar (see plasma/sql-common)
$builderA = new \Plasma\Schemas\SQLDirectory(Users::class, null);
$repository->registerDirectory('users', $builderA);

$repository->execute('SELECT * FROM `users`', array())
    ->done(function (\Plasma\Schemas\SchemaCollection $collection) {
        // Do something with the collection
    });

$loop->run();

Preloads

Schemas has a mechanism called Preloads.

Preloads are a way to load foreign references at the same time as a schema gets loaded, and let your schema be always filled with the foreign reference schema. How the preloads are exactly loaded depends on the Directory implementation.

Preloads are foreign targets with fetch mode ALWAYS and are automatically handled. Foreign target with fetch mode LAZY are not automatically loaded and need to be explicitely asked for by calling resolveForeignTargets on the schema.

Whether one uses one over the other fetch mode depends on the use case. It makes sense to only preload schemas you actually really always need.

Preloads are supported through the ColumnDefinitionInterface. Current implementations are the ColumnDefinition implementation and the ColumnDefinitionBuilder.

Documentation

https://plasmaphp.github.io/schemas/

schemas's People

Contributors

gitneko avatar wyrihaximus avatar

Stargazers

 avatar  avatar

Watchers

 avatar

schemas's Issues

Add SQL extension

We should add a SQL extension to schema and column definition (builder).

Specifically this would be foreign keys and fetching the referenced row, at the very least.

Add insertAll to SchemaBuilderInterface

We should add a new method called insertAll to schema builders, so multiple rows can be efficiently inserted. We should also be able to ignore constraint violations.

Add SQL Column Definition Builder

We should add a very simple column definition builder with a very simple column definition implementation.

This way creating schemas will get more straight forward and easier than the current way.

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.