This project is not covered by Drupal’s security advisory policy.
The work contribution done in Github
This is an ORM for RethinkDB. Which mean the module does not intend to replace
the default DB drivers but to create entity representation of your RethinkDB in
your Drupal installation.
Setting up
- install RethinkDB
- In the
settings.phpfile add the RethinkDB connection credentials as listed
below:
<span class="hljs-preprocessor"><?php</span>
<span class="hljs-variable">$settings</span>[<span class="hljs-string">'rethinkdb'</span>] = <span class="hljs-keyword">array</span>(
<span class="hljs-string">'database'</span> => <span class="hljs-string">'drupal8'</span>,
<span class="hljs-string">'host'</span> => <span class="hljs-string">'localhost'</span>,
);
Writing you custom entity
In order to define an entity based on RethinkDB storage you need to apply two
things:
- Add the
rethink = TRUEsettings in the annotatoin. - The entity class need to extends from
AbstractRethinkDbEntity
You can have a look in the next example or in RethinkDB example module:
<span class="hljs-comment">/**
* <span class="hljs-doctag">@ContentEntityType</span>(
* id = "rethinkdb_message",
* label = <span class="hljs-doctag">@Translation</span>("RethinkDB messages"),
* base_table = "rethinkdb_messages",
* translatable = FALSE,
* rethink = TRUE,
* entity_keys = {}
* )
*/</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RethinkMessages</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AbstractRethinkDbEntity</span> </span>{
}
CRUD operations
The CRUD operations are not different from the Drupal’s entity API:
Creating
<span class="hljs-variable">$message</span> = RethinkMessages::create([<span class="hljs-string">'title'</span> => <span class="hljs-string">'Foo'</span>, <span class="hljs-string">'body'</span> => <span class="hljs-string">'Bar'</span>]);
<span class="hljs-variable">$results</span> = <span class="hljs-variable">$message</span>->save();
The returned value is an array with some information from RethinkDB on the
creation operation. You are probably interested with the ID of the entity you
just created. Unlike schematic DBs, RethinkDB is a NoSQL server which mean the
ID’s of the rows, or documents, is a simple hash.
Loading
Loading the entity is very easy:
<span class="hljs-variable">$document</span> = RethinkMessages::load(reset(<span class="hljs-variable">$results</span>[<span class="hljs-string">'generated_keys'</span>]));
Or
<span class="hljs-variable">$document</span> = RethinkMessages::load(<span class="hljs-string">'404bef53-4b2c-433f-9184-bc3f7bda4a15'</span>);
You can get the values of the document:
<span class="hljs-variable">$document</span>->get(<span class="hljs-string">'title'</span>);
<span class="hljs-variable">$document</span>->get(<span class="hljs-string">'body'</span>);
<span class="hljs-variable">$document</span>->getValues();
Updating
Don’t worry. It’s very easy:
<span class="hljs-variable">$document</span>->set(<span class="hljs-string">'title'</span>, <span class="hljs-string">'new title'</span>)->save();
Deleting
As before, it’s EASY:
<span class="hljs-variable">$document</span>->delete();
Query
Basic query
Let’s warm up with some nice query:
<span class="hljs-variable">$messages</span> = \Drupal::entityQuery(<span class="hljs-string">'rethinkdb_message'</span>)
->execute();
condition
You can apply all the operations you know: =, !=, >, >=, <, <=, CONTAINS:
<span class="hljs-variable">$messages</span> = \Drupal::entityQuery(<span class="hljs-string">'rethinkdb_message'</span>)
->condition(<span class="hljs-string">'title'</span>, <span class="hljs-string">'fo'</span>, <span class="hljs-string">'CONTAINS'</span>)
->execute();
When executing the query, the query will return the objects and not the IDs of
the matching documents.
Contribution
Any PR is more than welcome. When creating the PR please ping me so I could know
a contribution has been done. 10x.
Project information
Seeking new maintainer
The current maintainers are looking for new people to take ownership.- Project categories: Developer tools, Integrations
- Created by roysegall on , updated
This project is not covered by the security advisory policy.
Use at your own risk! It may have publicly disclosed vulnerabilities.
