项目作者: ytake

项目描述 :
Couchbase providers for Laravel
高级语言: PHP
项目地址: git://github.com/ytake/Laravel-Couchbase.git
创建时间: 2015-10-29T11:02:15Z
项目社区:https://github.com/ytake/Laravel-Couchbase

开源协议:MIT License

下载


Laravel-Couchbase

for Laravel 5.1.*(higher)

cache, session, database, queue extension package
required ext-couchbase

Build Status
Code Coverage
Scrutinizer Code Quality
StyleCI

Packagist
Packagist
Packagist
Codacy Badge

SensioLabsInsight

Notice

Supported Auto-Discovery, Design Document, Cache Lock (Laravel5.5)

Laravel version Laravel-Couchbase version ext-couchbase
Laravel 5.6 ^1.1 >=2.3.2
Laravel 5.5 ^1.0 >=2.3.2
Laravel 5.4 ^0.7 ^2.2.2
Laravel 5.3 ^0.6 ^2.2.2
Laravel 5.2 ^0.5 ^2.2.2
Laravel 5.1 ^0.4 ^2.2.2

Deprecated

not recommended couchbase-memcached driver couchbase session driver

install

  1. $ composer require ytake/laravel-couchbase

or your config/app.php

  1. 'providers' => [
  2. // added service provider
  3. \Ytake\LaravelCouchbase\CouchbaseServiceProvider::class,
  4. \Ytake\LaravelCouchbase\ConsoleServiceProvider::class,
  5. ]

usage

database extension

add database driver(config/database.php)

  1. 'couchbase' => [
  2. 'driver' => 'couchbase',
  3. 'host' => 'couchbase://127.0.0.1',
  4. 'user' => 'userName', // optional administrator
  5. 'password' => 'password', // optional administrator
  6. // optional configuration / management operations against a bucket.
  7. 'administrator' => [
  8. 'user' => 'Administrator',
  9. 'password' => 'password',
  10. ],
  11. ],

case cluster

  1. 'couchbase' => [
  2. 'driver' => 'couchbase',
  3. 'host' => 'couchbase://127.0.0.1,192.168.1.2',
  4. 'user' => 'userName', // optional administrator
  5. 'password' => 'password', // optional administrator
  6. ],

choose bucket table() method
or

basic usage bucket() method

N1QL supported(upsert enabled)

see http://developer.couchbase.com/documentation/server/4.1/n1ql/n1ql-language-reference/index.html

SELECT

  1. // service container access
  2. $this->app['db']->connection('couchbase')
  3. ->table('testing')->where('whereKey', 'value')->first();
  4. // use DB facades
  5. \DB::connection('couchbase')
  6. ->table('testing')->where('whereKey', 'value')->get();

INSERT / UPSERT

  1. $value = [
  2. 'click' => 'to edit',
  3. 'content' => 'testing'
  4. ];
  5. $key = 'insert:and:delete';
  6. $result = \DB::connection('couchbase')
  7. ->table('testing')->key($key)->insert($value);
  8. \DB::connection('couchbase')
  9. ->table('testing')->key($key)->upsert([
  10. 'click' => 'to edit',
  11. 'content' => 'testing for upsert',
  12. ]);

DELETE / UPDATE

  1. \DB::connection('couchbase')
  2. ->table('testing')->key($key)->where('clicking', 'to edit')->delete();
  3. \DB::connection('couchbase')
  4. ->table('testing')->key($key)
  5. ->where('click', 'to edit')->update(
  6. ['click' => 'testing edit']
  7. );
execute queries

example)

  1. "delete from testing USE KEYS "delete" RETURNING *"
  2. "update testing USE KEYS "insert" set click = ? where click = ? RETURNING *"

returning

default *

  1. \DB::connection('couchbase')
  2. ->table('testing')
  3. ->where('id', 1)
  4. ->offset($from)->limit($perPage)
  5. ->orderBy('created_at', $sort)
  6. ->returning(['id', 'name'])->get();

View Query

  1. $view = \DB::view("testing");
  2. $result = $view->execute($view->from("dev_testing", "testing"));

cache extension

for bucket type couchbase

config/cache.php

  1. 'couchbase' => [
  2. 'driver' => 'couchbase',
  3. 'bucket' => 'session'
  4. ],

for bucket type memcached

  1. 'couchbase-memcached' => [
  2. 'driver' => 'couchbase-memcached',
  3. 'servers' => [
  4. [
  5. 'host' => '127.0.0.1',
  6. 'port' => 11255,
  7. 'weight' => 100,
  8. 'bucket' => 'memcached-bucket-name',
  9. 'option' => [
  10. // curl option
  11. ],
  12. ],
  13. ],
  14. ],

not supported

couchbase bucket, use bucket password

config/cache.php

  1. 'couchbase' => [
  2. 'driver' => 'couchbase',
  3. 'bucket' => 'session',
  4. 'bucket_password' => 'your bucket password'
  5. ],

session extension

.env etc..

specify couchbase driver

consistency

default :CouchbaseN1qlQuery::NOT_BOUNDED

  1. $this->app['db']->connection('couchbase')
  2. ->consistency(\CouchbaseN1qlQuery::REQUEST_PLUS)
  3. ->table('testing')
  4. ->where('id', 1)
  5. ->returning(['id', 'name'])->get();

callable consistency

  1. $this->app['db']->connection('couchbase')
  2. ->callableConsistency(\CouchbaseN1qlQuery::REQUEST_PLUS, function ($con) {
  3. return $con->table('testing')->where('id', 1)->returning(['id', 'name'])->get();
  4. });

Event

for N1QL

events description
\Ytake\LaravelCouchbase\Events\QueryPrepared get n1ql query
\Ytake\LaravelCouchbase\Events\ResultReturning get all property from returned result
\Ytake\LaravelCouchbase\Events\ViewQuerying for view query (request uri)

Schema / Migrations

The database driver also has (limited) schema builder support.
easily manipulate indexes(primary and secondary)

  1. use Ytake\LaravelCouchbase\Schema\Blueprint as CouchbaseBlueprint;
  2. \Schema::create('samples', function (CouchbaseBlueprint $table) {
  3. $table->primaryIndex(); // primary index
  4. $table->index(['message', 'identifier'], 'sample_secondary_index'); // secondary index
  5. // dropped
  6. $table->dropIndex('sample_secondary_index');
  7. $table->dropPrimary();
  8. });

Supported operations:

  • create and drop
  • index and dropIndex (primary index and secondary index)

Artisan

for couchbase manipulate indexes

commands description
couchbase:create-index Create a secondary index for the current bucket.
couchbase:create-primary-index Create a primary N1QL index for the current bucket.
couchbase:drop-index Drop the given secondary index associated with the current bucket.
couchbase:drop-primary-index Drop the given primary index associated with the current bucket.
couchbase:indexes List all N1QL indexes that are registered for the current bucket.
couchbase:create-queue-index Create primary index, secondary indexes for the queue jobs couchbase bucket.
couchbase:create-design Inserts design document and fails if it is exist already. for MapReduce views

-h more information.

create design

config/couchbase.php

  1. return [
  2. 'design' => [
  3. 'Your Design Document Name' => [
  4. 'views' => [
  5. 'Your View Name' => [
  6. 'map' => file_get_contents(__DIR__ . '/../resources/sample.ddoc'),
  7. ],
  8. ],
  9. ],
  10. ]
  11. ];

Queue

Change the the driver in config/queue.php:

  1. 'connections' => [
  2. 'couchbase' => [
  3. 'driver' => 'couchbase',
  4. 'bucket' => 'jobs',
  5. 'queue' => 'default',
  6. 'retry_after' => 90,
  7. ],
  8. ],

example

  1. php artisan queue:work couchbase --queue=send_email

hacking

To run tests there are should be following buckets created on local Couchbase cluster:

  1. $cluster = new CouchbaseCluster('couchbase://127.0.0.1');
  2. $clusterManager = $cluster->manager('Administrator', 'password');
  3. $clusterManager->createBucket('testing', ['bucketType' => 'couchbase', 'saslPassword' => '', 'flushEnabled' => true]);
  4. $clusterManager->createBucket('memcache-couch', ['bucketType' => 'memcached', 'saslPassword' => '', 'flushEnabled' => true]);
  5. sleep(5);
  6. $bucketManager = $cluster->openBucket('testing')->manager();
  7. $bucketManager->createN1qlPrimaryIndex();

Also tests are expecting regular Memcached daemon listening on port 11255.

soon

  • authintication driver
  • Eloquent support

Couchbase Document

REST API / Creating and Editing Buckets
couchbase-cli / user-manage
Authentication
Authorization API