Search code examples
mysqlstored-proceduresgraphqldirectus

Execute stored procedure in directus headless cms


I just find directus headless cms

Looks awesome. It resolve many uses cases for me.

But I am concerned about how to achieve transactions, aggregate functions or complex queries. I understand that maybe is out scope.

If a custom endpoint or graphql allow me execute a stored procedure i will have all my needs achieved.

Is it possible?


Solution

  • Hi finally I find how to use custom endpoints to do plain querys, including stored procedures.

    Maybe is possible implement a module for add admin gui option for that, I try work in that, for the moment this is the example for a select:

    use Directus\Application\Http\Request;
    use Directus\Application\Http\Response;
    
    return [
    '' => [
        'method' => 'GET',
        'handler' => function (Request $request, Response $response) {
    
            $container = \Directus\Application\Application::getInstance()->getContainer();
            $dbConnection = $container->get('database');
            $tableGateway = new \Zend\Db\TableGateway\TableGateway('directus_users', $dbConnection);
    
            $query = $tableGateway->getAdapter()->query("select * from productos where 1=1");
    
            $result = $query->execute();
    
            if ($result->count() > 0) {
                $returnArr = array();
                while ($result->valid()) {
                    $returnArr[] = $result->current();
                    $result->next();
                }
                if (count($returnArr) > 0) {
                    return $response->withJson([
                        'data' => [
                            $returnArr,
                        ],
                    ]);
                }
            }
            return "{}";
        },
    ],
    ];
    

    Sorry for my bad English.