Code Monkey home page Code Monkey logo

Comments (5)

Timu57 avatar Timu57 commented on August 15, 2024 2

@rakibtg Your right, for more complex joins that is excellent and a very nice solution.

I think if the developer just needs simple joins, providing an alternative method can make the business code much cleaner and the usage much easier.

$userStore = \SleekDB\SleekDB::store('user', './sleekdb_test_scripts');
$articleStore = \SleekDB\SleekDB::store('article', './sleekdb_test_scripts');

$users = $userStore
            ->simpleJoin($articleStore, ['_id', '=', 'author'], 'articles')
            ->fetch();

If the developer has to join three stores:

$userStore = \SleekDB\SleekDB::store('user', './sleekdb_test_scripts');
$articleStore = \SleekDB\SleekDB::store('article', './sleekdb_test_scripts');
$commentStore = \SleekDB\SleekDB::store('comments', './sleekdb_test_scripts');

$users = $userStore
            ->simpleJoin($articleStore, ['_id', '=', 'author'], 'articles')
            ->simpleJoin($commentStore, ['_id', '=', 'user'], 'comments')
            ->fetch();

If the developer needs for example also the articles related to the comments:

$userStore = \SleekDB\SleekDB::store('user', './sleekdb_test_scripts');
$articleStore = \SleekDB\SleekDB::store('article', './sleekdb_test_scripts');
$commentStore = \SleekDB\SleekDB::store('comments', './sleekdb_test_scripts');

$users = $userStore
            ->simpleJoin($articleStore, ['_id', '=', 'author'], 'articles')
            ->simpleJoin([$commentStore, $articleStore], [['_id', '=', 'user'], ['comments.article', '=', '_id']], 'comments')
            ->fetch();

Or:

$userStore = \SleekDB\SleekDB::store('user', './sleekdb_test_scripts');
$articleStore = \SleekDB\SleekDB::store('article', './sleekdb_test_scripts');
$commentStore = \SleekDB\SleekDB::store('comments', './sleekdb_test_scripts');

$users = $userStore
            ->simpleJoin($articleStore, ['_id', '=', 'author'], 'articles')
            ->simpleJoin([$commentStore, $articleStore], [['_id', '=', 'user'], ['article', '=', '_id']], 'comments')
            ->fetch();

Where the second condition just consider the comments-result and joins it with the article store.

But I don't know what could be a nice name for the method.

from sleekdb.

rakibtg avatar rakibtg commented on August 15, 2024

Data output for above sample query:

Array
(
    [0] => Array
        (
            [name] => Foo
            [followers] => 10
            [_id] => 1
            [articles] => Array
                (
                    [0] => Array
                        (
                            [title] => Lorem ipsum
                            [author] => 1
                            [likes] => 0
                            [_id] => 1
                        )

                    [1] => Array
                        (
                            [title] => Flock of birds
                            [author] => 1
                            [likes] => 66
                            [_id] => 3
                        )

                    [2] => Array
                        (
                            [title] => Flying in above
                            [author] => 1
                            [likes] => 0
                            [_id] => 4
                        )

                )

        )

    [1] => Array
        (
            [name] => Bar
            [followers] => 0
            [_id] => 2
            [articles] => Array
                (
                    [0] => Array
                        (
                            [title] => Dollar sit amet
                            [author] => 2
                            [likes] => 12
                            [_id] => 2
                        )

                )

        )

    [2] => Array
        (
            [name] => Bif
            [followers] => 5
            [_id] => 3
            [articles] => Array
                (
                    [0] => Array
                        (
                            [title] => The sun rise
                            [author] => 3
                            [likes] => 2842
                            [_id] => 5
                        )

                )

        )

    [3] => Array
        (
            [name] => Baf
            [followers] => 12
            [_id] => 4
            [articles] => Array
                (
                )

        )

)

from sleekdb.

Timu57 avatar Timu57 commented on August 15, 2024

That is an awesome idea!

Wouldn't it be nicer if we could tweak the join method, for a much easier use?

I am thinking about something like this:

public function join($storeToJoin, $condition, $resultPropertyName)

With an usage like this:

$articleStore = \SleekDB\SleekDB::store('article', './sleekdb_test_scripts');
$userStore = \SleekDB\SleekDB::store('user', './sleekdb_test_scripts');

$users = $userStore->join($articleStore, ['author', '=', '_id'], 'articles')->fetch();

Or add a new method called leftJoin with a more easy use.

from sleekdb.

rakibtg avatar rakibtg commented on August 15, 2024

@Timu57 Thanks for your input. I have thought about it at first, but then I realised that we could achieve more with closure JOIN function.

For example,

  • Nested join
  • Nested join pagination
  • Sorting ... etc.

Code example:

<?php
$userStore
      ->join(function ($user) use ($articleStore) {
            return $articleStore
                  ->where('author', '=', $user['_id'])
                  ->join(function ($article) use ($userStore) {
                        return $userStore
                            ->in('_id', '=', $article['likedUsers'])
                            ->orderBy('desc', '_id' )
                            ->limit(10);
                  }, 'likedBy');
      }, 'articles')
      ->fetch();

Let me know your opinion.

from sleekdb.

rakibtg avatar rakibtg commented on August 15, 2024

Added in SleekDB 2.0!

from sleekdb.

Related Issues (20)

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.