Coder Social home page Coder Social logo

foo123 / dialectorm Goto Github PK

View Code? Open in Web Editor NEW
2.0 4.0 0.0 164 KB

Tiny, fast, super-simple but versatile Object-Relational-Mapper (ORM) w/ Relationships and Object-NoSql-Mapper, based on Dialect, for PHP, Python, JavaScript

Home Page: https://foo123.github.io/

JavaScript 33.78% PHP 36.16% Python 30.06%
orm object-relational-mapper php python node-js relationships active-record nosql-data-storage

dialectorm's Introduction

DialectORM

Tiny, fast, super-simple but versatile Object-Relational-Mapper (ORM) with Relationships and Object-NoSql-Mapper for PHP, Python, JavaScript

DialectORM

version 2.0.1

see also:

  • ModelView a simple, fast, powerful and flexible MVVM framework for JavaScript
  • tico a tiny, super-simple MVC framework for PHP
  • LoginManager a simple, barebones agnostic login manager for PHP, JavaScript, Python
  • SimpleCaptcha a simple, image-based, mathematical captcha with increasing levels of difficulty for PHP, JavaScript, Python
  • Dromeo a flexible, and powerful agnostic router for PHP, JavaScript, Python
  • PublishSubscribe a simple and flexible publish-subscribe pattern implementation for PHP, JavaScript, Python
  • Importer simple class & dependency manager and loader for PHP, JavaScript, Python
  • Contemplate a fast and versatile isomorphic template engine for PHP, JavaScript, Python
  • HtmlWidget html widgets, made as simple as possible, both client and server, both desktop and mobile, can be used as (template) plugins and/or standalone for PHP, JavaScript, Python (can be used as plugins for Contemplate)
  • Paginator simple and flexible pagination controls generator for PHP, JavaScript, Python
  • Formal a simple and versatile (Form) Data validation framework based on Rules for PHP, JavaScript, Python
  • Dialect a cross-vendor & cross-platform SQL Query Builder, based on GrammarTemplate, for PHP, JavaScript, Python
  • DialectORM an Object-Relational-Mapper (ORM) and Object-Document-Mapper (ODM), based on Dialect, for PHP, JavaScript, Python
  • Unicache a simple and flexible agnostic caching framework, supporting various platforms, for PHP, JavaScript, Python
  • Xpresion a simple and flexible eXpression parser engine (with custom functions and variables support), based on GrammarTemplate, for PHP, JavaScript, Python
  • Regex Analyzer/Composer Regular Expression Analyzer and Composer for PHP, JavaScript, Python

Dependencies:

  • Dialect
  • PHP: 5.3+
  • Python: 3+
  • JavaScript: ES7+

Supports:

  • Relational Datastores: MySql, MariaDB, Postgresql, Sql Server, Sqlite, easily extended to other Relational Stores via adding Dialect configuration
  • NoSql / Key-Value DataStores: Redis, Mongodb (example in progress), easily extended to other NoSql Stores via providing adapter

example (see /test folder)

define('DIR', dirname(__FILE__));
include(DIR.'/../../src/php/DialectORM.php');
include(DIR.'/PDODb.php');

DialectORM::dependencies([
    'Dialect' => DIR.'/Dialect.php',
]);
DialectORM::DBHandler(new PDODb([
    'dsn' => 'mysql:host=localhost;dbname=dialectorm',
    'user' => 'dialectorm',
    'password' => 'dialectorm'
], 'mysql'));

class Post extends DialectORM
{
    public static $table = 'posts';
    public static $pk = 'id'; // primary and/or foreign keys can also be composite keys, define with an array of keys eg ['k1', 'k2']
    public static $fields = ['id', 'content'];
    public static $relationships = [
        'meta' => ['hasOne', 'PostMeta', 'post_id'], // primary and/or foreign keys can also be composite keys, define with an array of keys eg ['k1', 'k2']
        'comments' => ['hasMany', 'Comment', 'post_id'], // primary and/or foreign keys can also be composite keys, define with an array of keys eg ['k1', 'k2']
        'authors' => ['belongsToMany', 'User', 'user_id', 'post_id', 'user_post'], // primary and/or foreign keys can also be composite keys, define with an array of keys eg ['k1', 'k2']
    ];

    public function typeId($x)
    {
        return (int)$x;
    }

    public function typeContent($x)
    {
        return (string)$x;
    }

    public function validateContent($x)
    {
        return 0 < strlen($x);
    }
}

class PostMeta extends DialectORM
{
    public static $table = 'postmeta';
    public static $pk = 'id'; // primary and/or foreign keys can also be composite keys, define with an array of keys eg ['k1', 'k2']
    public static $fields = ['id', 'status', 'type', 'post_id'];
    public static $relationships = [
        'post' => ['belongsTo', 'Post', 'post_id'] // primary and/or foreign keys can also be composite keys, define with an array of keys eg ['k1', 'k2']
    ];

    public function typeId($x)
    {
        return (int)$x;
    }

    public function typePostId($x)
    {
        return (int)$x;
    }

    public function typeStatus($x)
    {
        return strtolower((string)$x);
    }

    public function typeType($x)
    {
        return strtolower((string)$x);
    }

    public function validateStatus($x)
    {
        return in_array($x, ['approved', 'published', 'suspended']);
    }

    public function validateType($x)
    {
        return in_array($x, ['article', 'tutorial', 'general']);
    }
}

class Comment extends DialectORM
{
    public static $table = 'comments';
    public static $pk = 'id'; // primary and/or foreign keys can also be composite keys, define with an array of keys eg ['k1', 'k2']
    public static $fields = ['id', 'content', 'post_id'];
    public static $relationships = [
        'post' => ['belongsTo', 'Post', 'post_id'], // primary and/or foreign keys can also be composite keys, define with an array of keys eg ['k1', 'k2']
    ];

    public function typeId($x)
    {
        return (int)$x;
    }

    public function typeContent($x)
    {
        return (string)$x;
    }

    public function typePostId($x)
    {
        return (int)$x;
    }

    public function validateContent($x)
    {
        return 0 < strlen($x);
    }
}

class User extends DialectORM
{
    public static $table = 'users';
    public static $pk = 'id'; // primary and/or foreign keys can also be composite keys, define with an array of keys eg ['k1', 'k2']
    public static $fields = ['id', 'name'];
    public static $relationships = [
        'posts' => ['belongsToMany', 'Post', 'post_id', 'user_id', 'user_post'], // primary and/or foreign keys can also be composite keys, define with an array of keys eg ['k1', 'k2']
    ];

    public function typeId($x)
    {
        return (int)$x;
    }

    public function typeName($x)
    {
        return (string)$x;
    }

    public function validateName($x)
    {
        return 0 < strlen($x);
    }
}

$post = new Post(['content'=>'another post..']);
// alternative
//$post = new Post(); $post->setContent('another post..');

$post->setComments([new Comment(['content'=>'another comment..'])]);
$post->setComments([new Comment(['content'=>'still another comment..'])], ['merge'=>true]);

$post->setAuthors([new User(['name'=>'bar'])], ['merge'=>true]);

// alternative
//$user = new User(['name'=>'bar']);
//$user->save();
//$post->assocAuthors([$user]);

// to simply dissociate from a many-to-many relationship
//$post->dissocAuthors([$user]);

$post->save(['withRelated'=>['comments','authors']]);

//$post->delete(['withRelated'=>true]);

$count = Post::count(['conditions'=>[/*..*/]]);
$posts = Post::fetchAll(['withRelated'=>['comments','authors']]); // eager load of relationships, no N+1 problem

//foreach(Post::fetchAll() as $post) $post->getComments(); // lazy load of relationships, N+1 problem

dialectorm's People

Contributors

foo123 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

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.