Giter Club home page Giter Club logo

walnut-lib-dborm's Introduction

Lightweight ORM

Examples

User Domain Model

#[ModelRoot('users')]
class UserDomainModel {
    public function __construct(
        #[Table("org_users")]
        #[KeyField('id'), Fields('first_name', 'org_id')]
        #[ListOf(fieldName: 'credentials', targetName: 'userCredentials')]
        #[ListOf(fieldName: 'roles', targetName: 'roles')]
        #[ListOf(fieldName: 'tags', targetName: 'tags')]
        public array $users,

            #[Table('org_user_roles'), KeyField('id'), ParentField('user_id'), Fields('role_id'), GroupField('code')]
            public array $roles,

            #[Table('org_user_credentials')]
            #[KeyField('id'), ParentField('user_id'), Fields('username', 'password')]
            public array $userCredentials,

            #[Table("org_user_tags"), KeyField('id'), ParentField('user_id'), Fields('tag_id')]
            public array $tags
    ) {}
}

User Query Model

#[ModelRoot('users')]
class UserQueryModel {
    public function __construct(
        #[Table("org_users")]
        #[KeyField('id'), Fields('first_name', 'org_id')]
        #[OneOf(fieldName: 'org', targetName: 'orgs', sourceField: 'org_id')]
        #[ListOf(fieldName: 'credentials', targetName: 'userCredentials')]
        #[ListOf(fieldName: 'roles', targetName: 'roles')]
        #[ListOf(fieldName: 'tags', targetName: 'tags')]
        public array $users,

            #[CrossTable('org_user_roles', parentField: 'user_id', sourceField: 'role_id', targetField: 'id')]
            #[Table('org_roles'), KeyField('id'), Fields('name', 'code'), GroupField('code')]
            public array $roles,

            #[Table('orgs')]
            #[KeyField('id'), Fields('name'), ParentField('id')]
            public array $orgs,

            #[Table('org_user_credentials')]
            #[KeyField('id'), ParentField('user_id'), Fields('username', 'password')]
            public array $userCredentials,

            #[CrossTable('org_user_tags', parentField: 'user_id', sourceField: 'tag_id', targetField: 'id')]
            #[Table("org_tag_user_group_values"), KeyField('id'), Fields('value', 'group_id'), GroupField('id')]
            #[OneOf(fieldName: 'group', targetName: 'tagGroups', sourceField: 'group_id')]
            public array $tags,

                #[Table("org_tag_user_groups"), KeyField('id'), Fields('name'), ParentField('id')]
                public array $tagGroups
    ) {}
}

Working with data

Preparation steps

$queryExecutor = new PdoQueryExecutor($connector);
$factory = new DataModelFactory(new SqliteQuoter, $queryExecutor);

Fetching data

$model = (new DataModelBuilder)->build(UserQueryModel::class);
$dataModelFetcher = $factory->getFetcher($model);
$data = $dataModelFetcher->fetchData(new QueryFilter(
    new FieldExpression('id', '<', new SqlValue(2))
)); //contains the full object as defined in the model above

Synchronizing data

$model = (new DataModelBuilder)->build(UserDomainModel::class);
$dataModelSynchronizer = $factory->getSynchronizer($model);
$data = $dataModelSynchronizer->syncData(
    [$recordToBeRemoved, $oldData],
    [$newData, $recordToBeAdded]
); 
//1. deletes $recordToBeRemoved,
//2. updates $oldData to $newData
//3. inserts $recordToBeAdded

walnut-lib-dborm's People

Contributors

kapitancho avatar

Watchers

 avatar James Cloos 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.