logo

Tutorials: Working with NoSQL Databases

Setting NoSQL Databases

In order to be able to query SQL databases (eg: MySQL), you first need to open bootstrap index.php file add this event listener to object:

$object->addEventListener(Lucinda\STDOUT\EventType::APPLICATION, Lucinda\Project\EventListeners\NoSQLDataSource::class);

and / or to object:

$object->addEventListener(Lucinda\ConsoleSTDOUT\EventType::APPLICATION, Lucinda\Project\EventListeners\Console\NoSQLDataSource::class);

then create in stdout.xml a tag (which will hold your connection settings), child of DEVELOPMENT ENVIRONMENT tag (because credentials will be different on another environment), child of tag. Example:

<nosql> <local> <server driver="redis" host="localhost"/> </local> </sql>

If your site uses multiple database servers for a single DEVELOPMENT ENVIRONMENT, it is allowed to have multiple entries there. To learn more how to configure this tag and drivers supported, check official documentation!

Querying NoSQL Databases

Querying NoSQL databases requires you completed setting NoSQL databases section!

Retrieving a Connection

To retrieve a connection for a DEVELOPMENT ENVIRONMENT, use this instead:

$driver = ::getInstance(SERVER_NAME);

Where SERVER_NAME is value of "name" attribute at matching tag. If no such attribute was defined, empty string is assumed!

This will return a single per session, reused until script ends and automatically closed afterwards (unless driver is apc/apcu, requiring no connections).

Alternatively you can use function framework already comes with::

$driver = NoSQL(SERVER_NAME = "");

Running Statements

Once a connection is retrieved, you can run basic document database logic such as:

$driver->set(KEY, VALUE, EXPIRATION);

Example:

// saves a key named "foo" with value "bar" in DB that expires in 10 seconds $driver->set("foo", "bar", 10); // retrieves value of "foo" from DB $x = $driver->get("foo");

This works the same way as PDO, hiding complexities of driver used. If, however, basic operations are not enough and you need to work with actual vendor logic use:

$vendorDriver = $driver->getDriver();

then use vendor official documentation (eg: redis) to query that object.

Modifying Pre-Installed DAOs

When framework is installed, a number of DAO classes are pushed already to your project, all assuming you will only use one NoSQL driver per DEVELOPMENT ENVIRONMENT. If that is not true for your project, modify DRIVER_NAME constant appropriately in following files:

Value of DRIVER_NAME must match with value of a "name" attribute in a tag for that DEVELOPMENT ENVIRONMENT!

×