Coder Social home page Coder Social logo

laravel-couchbase's Introduction

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

$ composer require ytake/laravel-couchbase

or your config/app.php

'providers' => [
    // added service provider
    \Ytake\LaravelCouchbase\CouchbaseServiceProvider::class,
    \Ytake\LaravelCouchbase\ConsoleServiceProvider::class,
]

usage

database extension

add database driver(config/database.php)

'couchbase' => [
    'driver' => 'couchbase',
    'host' => 'couchbase://127.0.0.1',
    'user' => 'userName', // optional administrator
    'password' => 'password', // optional administrator
    // optional configuration / management operations against a bucket.
    'administrator' => [
        'user'     => 'Administrator',
        'password' => 'password',
    ],
],

case cluster

'couchbase' => [
    'driver' => 'couchbase',
    'host' => 'couchbase://127.0.0.1,192.168.1.2',
    'user' => 'userName', // optional administrator
    'password' => 'password', // optional administrator
],

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

// service container access
$this->app['db']->connection('couchbase')
    ->table('testing')->where('whereKey', 'value')->first();

// use DB facades
\DB::connection('couchbase')
    ->table('testing')->where('whereKey', 'value')->get();

INSERT / UPSERT

$value = [
    'click' => 'to edit',
    'content' => 'testing'
];
$key = 'insert:and:delete';

$result = \DB::connection('couchbase')
    ->table('testing')->key($key)->insert($value);

\DB::connection('couchbase')
    ->table('testing')->key($key)->upsert([
        'click'   => 'to edit',
        'content' => 'testing for upsert',
    ]);

DELETE / UPDATE

\DB::connection('couchbase')
    ->table('testing')->key($key)->where('clicking', 'to edit')->delete();

\DB::connection('couchbase')
    ->table('testing')->key($key)
    ->where('click', 'to edit')->update(
        ['click' => 'testing edit']
    );
execute queries

example)

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

returning

default *

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

View Query

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

cache extension

for bucket type couchbase

config/cache.php

'couchbase' => [
   'driver' => 'couchbase',
   'bucket' => 'session'
],

for bucket type memcached

'couchbase-memcached' => [
    'driver'  => 'couchbase-memcached',
    'servers' => [
        [
            'host' => '127.0.0.1',
            'port' => 11255,
            'weight' => 100,
            'bucket' => 'memcached-bucket-name',
            'option' => [
                // curl option
            ],
        ],
    ],
],

not supported

couchbase bucket, use bucket password

config/cache.php

'couchbase' => [
   'driver' => 'couchbase',
   'bucket' => 'session',
   'bucket_password' => 'your bucket password'
],

session extension

.env etc..

specify couchbase driver

consistency

default :CouchbaseN1qlQuery::NOT_BOUNDED

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

callable consistency

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

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)

use Ytake\LaravelCouchbase\Schema\Blueprint as CouchbaseBlueprint;

\Schema::create('samples', function (CouchbaseBlueprint $table) {
    $table->primaryIndex(); // primary index
    $table->index(['message', 'identifier'], 'sample_secondary_index'); // secondary index
    // dropped
    $table->dropIndex('sample_secondary_index'); 
    $table->dropPrimary();
});

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

return [
    'design' => [
        'Your Design Document Name' => [
            'views' => [
                'Your View Name' => [
                    'map' => file_get_contents(__DIR__ . '/../resources/sample.ddoc'),
                ],
            ],
        ],
    ]
];

Queue

Change the the driver in config/queue.php:

    'connections' => [
        'couchbase' => [
            'driver' => 'couchbase',
            'bucket' => 'jobs',
            'queue' => 'default',
            'retry_after' => 90,
        ],
    ],

example

php artisan queue:work couchbase --queue=send_email

hacking

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

$cluster = new CouchbaseCluster('couchbase://127.0.0.1');
$clusterManager = $cluster->manager('Administrator', 'password');
$clusterManager->createBucket('testing', ['bucketType' => 'couchbase', 'saslPassword' => '', 'flushEnabled' => true]);
$clusterManager->createBucket('memcache-couch', ['bucketType' => 'memcached', 'saslPassword' => '', 'flushEnabled' => true]);
sleep(5);
$bucketManager = $cluster->openBucket('testing')->manager();
$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

laravel-couchbase's People

Contributors

avsej avatar delatbabel avatar ytake avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

laravel-couchbase's Issues

Error on Laravel 5.2

Hi,
Before installing on Laravel 5.2 i have error
CouchbaseConnector.php line 35.
Class 'CouchbaseCluster' not found
Do you see anything like this?
Tks

tags()->flush() does not work

Hi,

I built this sample app with a Laravel console command: https://github.com/delatbabel/cachetest

After I put exception handlers into LegacyCouchbaseStore in the forever() and forget() functions (otherwise forget() throws an exception trying to forget a key that does not exist), I am still having one issue. In the Cache tag forget test I am doing this:

Cache::tags(['testone', 'testtwo'])->put('testthree', $random_value, 60);
Cache::tags(['testone', 'testtwo'])->flush();
$value_to_check = Cache::tags(['testone', 'testtwo'])->get('testthree');

and it appears that the flush() does nothing because I can still retrieve the value using get() after it has been flushed().

Testing against memcached and redis caches does not show the same issue.

Single item returned instead of full set on select

Hello, thanks for the awesome work!

I'm doing this

DB::connection('couchbase')->table(env('DB_DATABASE'))
            ->select('meta(' . env('DB_DATABASE') . ').id as docId, id, blah blah other fields')
            ->whereRaw("aField is null || aSecondField is null")
            ->get();

That should return a big load of records, but is returning just one.
The "code" reason for that is

if (!isset($row->{$this->bucket})) {

and my records are not nested inside a "bucket" key. Is there any particular reason you are doing this check?
And can you confirm I must write my records with a bucket key that wraps the real value if I want to get multiple results from my selects?

Like, say, my bucket is named "db", I need to

$result = \DB::connection('couchbase')
    ->table('testing')->key($key)->insert(['mybucket' => [ /* all my values..*/]]);

?

Thank you!

Class 'CouchbaseN1qlQuery' not found

I'm getting this error when configuring Couchbase as cachedriver:

FatalErrorException in CouchbaseServiceProvider.php line 74:
Class 'CouchbaseN1qlQuery' not found

I'm using:

  • Laravel 5.3
  • ext-couchbase 2.2.4
  • PHP 7.1

Thanks.

Laravel 5.2 Error

When trying to connect to my couchDB instance I get the following error in the image attached. Please advise. Any help would be great.
laravel5 2-couchdb-error

I've using the "ytake/laravel-couchbase": "0.5.7" version.

flush command

curl -X POST localhost:8091/pools/default/buckets/[name]/controller/doFlush

Basic usage example

Can you provide some basic usage examples please? I've added CouchbaseServiceProvider to the providers and not sure how to use it.
Now I do the following:

$query = 'SELECT * FROM `beer-sample` where type="beer" LIMIT 50';
$res = DB::connection()->bucket('beer-sample')->select($query);

and just wondering if there is some better way to make a query. Thanks

Compatibility for php71 and php-pecl-couchbase 2.3.x

With version 2.3.x of the php extension, the API of the PHP SDK has changed and only one parameter is allowed when constructing a CouchbaseCluster.

Documentation for 2.2.3
http://docs.couchbase.com/sdk-api/couchbase-php-client-2.2.3/classes/CouchbaseCluster.html

Documentation for 2.3.3
http://docs.couchbase.com/sdk-api/couchbase-php-client-2.3.3/classes/Couchbase.Cluster.html

The fix is simple in Ytake\LaravelCouchbase\Database\CouchbaseConnector

OLD:

    public function connect(array $servers)
    {
        $configure = array_merge($this->configure, $servers);

        return new CouchbaseCluster(
            $configure['host'],
            $configure['user'],
            $configure['password']
        );
    }

NEW:

    public function connect(array $servers)
    {
        $configure = array_merge($this->configure, $servers);

        return new CouchbaseCluster(
            $configure['host']
        );
    }

If this change is OK for you I can also provide it as a pull request

Getting timout error unexpectedly

CouchbaseError: Client-Side timeout exceeded for operation
Note: We are using ytake Laravel-Couchbase with laravel 5.2. I am getting timeout issue regurlarly. How can I increase operation timeout in the framework or is there any other option to resolve this issue?

Call to undefined method Couchbase\N1qlQuery::toObject() in Laravel 5.3, PHP 7.1

Using examples given in the documentation, in Laravel 5.3 using package version ^0.6, gives an exception when using any of the documentation configuration configurations.

When running this code in a configured 5.3 install on PHP 7.1:

\DB::connection('couchbase')
            ->table('beer-sample')
            ->where('name', 'like', '%21%')
            ->get()

A FatalThrowableError is thrown, with the following stack trace:

1. in QueryPrepared.php line 33
2. at QueryPrepared->__construct(object(N1qlQuery)) in CouchbaseConnection.php line 519
3. at CouchbaseConnection->firePreparedQuery(object(N1qlQuery)) in CouchbaseConnection.php line 299
4. at CouchbaseConnection->executeQuery(object(N1qlQuery)) in CouchbaseConnection.php line 318
5. at CouchbaseConnection->Ytake\LaravelCouchbase\Database\{closure}(object(CouchbaseConnection), object(N1qlQuery), array('%21%')) in Connection.php line 763
6. at Connection->runQueryCallback('select * from beer-sample where name like ?', array('%21%'), object(Closure)) in Connection.php line 726
7. at Connection->run('select * from beer-sample where name like ?', array('%21%'), object(Closure)) in CouchbaseConnection.php line 322
8. at CouchbaseConnection->select('select * from beer-sample where name like ?', array('%21%'), true) in Builder.php line 1648
...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.