Download the PHP package robsonvn/laravel-couchdb without Composer
On this page you can find all versions of the php package robsonvn/laravel-couchdb. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download robsonvn/laravel-couchdb
More information about robsonvn/laravel-couchdb
Files in robsonvn/laravel-couchdb
Package laravel-couchdb
Short Description A CouchDB v2.0.0 based Eloquent model and Query builder for Laravel
License MIT
Informations about the package laravel-couchdb
Laravel CouchDB
Laravel CouchDB is an Eloquent model and Query builder with support for CouchDB 2.x, using the original Laravel API. This library extends the original Laravel classes, so it uses exactly the same methods.
Good to know before using it
- CouchDB has many limitations dealing with Mango Query that force us to process somethings in memory, which directly impacts on our library performance, please check out the Limitations sections for more details.
Table of contents
- Installation
- Configuration
- Eloquent
- Query Builder
- Extensions
- Examples
- Inserts, updates and deletes
- Relations
- CouchDB specific operators
- CouchDB Limitations
- Limitations
- TODO
Installation
Installation using composer:
And add the service provider in config/app.php
:
Laravel version Compatibility
For now, this project only works with Laravel 5.4.x
Configuration
Change your default database connection name in config/database.php
:
And add a new couchdb connection:
And this on yours .env file
Please note, the database user must be an admin since this library creates indexes on the fly (design_docs)
You can read more about CouchDB Authorization here.
Eloquent
This package includes a CouchDB enabled Eloquent class that you can use to define models for corresponding collections.
Note that we did not tell Eloquent which collection to use for the Book
model. Just like the original Eloquent, the lower-case, plural name of the class will be used as the collection name unless another name is explicitly specified. You may specify a custom collection (alias for table) by defining a collection
property on your model:
Query Builder
The database driver plugs right into the original query builder. When using couchdb connections, you will be able to build fluent queries to perform database operations. For your convenience, there is a collection
alias for table
as well as some additional couchdb specific operators/operations.
If you did not change your default database connection, you will need to specify it when querying.
Read more about the query builder on http://laravel.com/docs/queries
Extensions
Auth
If you want to use Laravel's native Auth functionality, register this included service provider:
This service provider will slightly modify the internal DatabaseTokenRepository to add support for CouchDB based password reminders. If you don't use password reminders, you don't have to register this service provider and everything else should work just fine.
You also needs to extends the CouchDB Authenticatable class in your User class instead of the native one.
Queues
If you want to use CouchDB as your database backend, change the the driver in config/queue.php
:
If you want to use CouchDB to handle failed jobs, change the database in config/queue.php
:
And add the service provider in config/app.php
:
Examples
Basic Usage
Retrieving All Models
Retrieving A Record By Primary Key
Wheres
Or Statements
And Statements
Using Where In With An Array
When using whereNotIn
objects will be returned if the field is non existent. Combine with whereNotNull('age')
to leave out those documents.
Using Where Between
Where null
Order By
Offset & Limit
Advanced Wheres
Like (case-sensitive)
Like (case-insensitive)
Incrementing or decrementing a value of a column
Perform increments or decrements (default 1) on specified attributes:
The number of updated objects is returned:
You may also specify additional columns to update:
Soft deleting
When soft deleting a model, it is not actually removed from your database. Instead, a deleted_at timestamp is set on the record. To enable soft deletes for a model, apply the SoftDeletingTrait to the model:
For more information check http://laravel.com/docs/eloquent#soft-deleting
CouchDB specific operators
Exists
Matches documents that have the specified field.
All
Matches arrays that contain all elements specified in the query.
Size
Selects documents if the array field is a specified size.
Regex
Selects documents where values match a specified regular expression.
NOTE: Mango query uses Erlang regular expression implementation.
Most selector expressions work exactly as you would expect for the given operator. The matching algorithms used by the $regex operator are currently based on the Perl Compatible Regular Expression (PCRE) library. However, not all of the PCRE library is implemented, and some parts of the $regex operator go beyond what PCRE offers. For more information about what is implemented, see the Erlang Regular Expression information http://erlang.org/doc/man/re.html.
Type
Selects documents if a field is of the specified type. Valid values are "null", "boolean", "number", "string", "array", and "object".
Mod
Performs a modulo operation on the value of a field and selects documents with a specified result.
Inserts, updates and deletes
Inserting, updating and deleting records works just like the original Eloquent.
Saving a new model
You may also use the create method to save a new model in a single line:
Updating a model
To update a model, you may retrieve it, change an attribute, and use the save method.
Deleting a model
To delete a model, simply call the delete method on the instance:
Or deleting a model by its key:
For more information about model manipulation, check http://laravel.com/docs/eloquent#insert-update-delete
Dates
Eloquent allows you to work with Carbon/DateTime object. Internally, these dates will be converted to a formated string 'yyyy-mm-dd H:i:s' when saved to the database. If you wish to use this functionality on non-default date fields you will need to manually specify them as described here: http://laravel.com/docs/eloquent#date-mutators
Example:
Which allows you to execute queries like:
Relations
Supported relations are:
- hasOne
- hasMany
- belongsTo
- belongsToMany
- morphToMany
- embedsOne
- embedsMany
Example:
And the inverse relation:
The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead. This makes the second parameter for the belongsToMany method useless. If you want to define custom keys for your relation, set it to null
:
Other relations are not yet supported, but may be added in the future. Read more about these relations on http://laravel.com/docs/eloquent#relationships
EmbedsMany Relations
If you want to embed models, rather than referencing them, you can use the embedsMany
relation. This relation is similar to the hasMany
relation, but embeds the models inside the parent object.
REMEMBER: these relations return Eloquent collections, they don't return query builder objects!
You access the embedded models through the dynamic property:
The inverse relation is automagically available, you don't need to define this reverse relation.
Inserting and updating embedded models works similar to the hasMany
relation:
You can update embedded models using their save
method:
You can remove an embedded model by using the destroy
method on the relation, or the delete
method on the model:
If you want to add or remove an embedded model, without touching the database, you can use the associate
and dissociate
methods. To eventually write the changes to the database, save the parent object:
Like other relations, embedsMany assumes the local key of the relationship based on the model name. You can override the default local key by passing a second argument to the embedsMany method:
Embedded relations will return a Collection of embedded items instead of a query builder. Check out the available operations here: https://laravel.com/docs/master/collections
EmbedsOne Relations
The embedsOne relation is similar to the EmbedsMany relation, but only embeds a single model.
You access the embedded models through the dynamic property:
Inserting and updating embedded models works similar to the hasOne
relation:
You can update the embedded model using the save
method:
You can replace the embedded model with a new model like this:
Raw Expressions
These expressions will be injected directly into the query.
You can also perform raw expressions on the internal CouchDBCollection object. If this is executed on the model class, it will return a collection of models. If this is executed on the query builder, it will return the original response.
The internal CouchDBClient can be accessed like this:
CouchDB Limitations
- Currently, there's no way to update and delete using Mango Query. In this case, we have to query the data, bring it to memory, update the fields and bulk an update.
- CouchDB is really touchy in matter of indexes, even the documentation recommends to always explicit the index that your query should use. In this case, we are automatically creating all necessaries index on the fly.
- CouchDB does not have the concept of collection as MongoDB, so we are using "collections" by adding an attribute (type) in every single document. Please, treat type as a reserved attribute. Use of collections is not optional.
Limitations
- Due the way we're creating index this library does not work with the Full Text Search engine enabled yet.
- Aggregation, group by and distinct operations is not supported yet.
- If you want to use any library that extends the original Eloquent classes you may have to fork it and change to our classes.
TODO
- Add compatibility to work with Full Text Search engine.
- Add support to aggregation, group by and distinct operations.
- Create a query cursor.
- Add support to get casted attribute using doting notation.
Special Thanks
Fred Booking for supporting this project.
Jens Segers and the Laravel MongoDB contributors because many of the code and structure of this project came from there.
All versions of laravel-couchdb with dependencies
illuminate/database Version 5.4.*
illuminate/support Version 5.4.*
illuminate/container Version 5.4.*
illuminate/events Version 5.4.*
doctrine/couchdb Version ^2.0.0-alpha1
adbario/php-dot-notation Version ^1.2