tomasruizr / sails-cb

Waterline Adapter for Couchbase.
Other
0 stars 1 forks source link

image_squidhome@2x.png

sails-cb

Provides easy access to Couchbase from Sails.js & Waterline.

Is mostly based in the N1QL language to query the data and the SDK api to insert and update the documents.

Interfaces

This adapter implements the semantic, queryable and the association interfaces. For more information on interfaces please review the waterline interfaces documentation.

Requirements

This adapter was developed and tested with Couchbase Server CE 4.0.0-4051

It should work with the later versions and also with the Enterprise Edition, although it's not tested... yet.

Since the adapter makes an extensive use of N1QL for Selects, Updates and Deletes, It is assumed that it won't work with prior versions or version that do not support N1QL DP4~.

Installation

To install this adapter, run:

$ npm install sails-cb

Configuration

This are the defaults values for the attributes that can be specified when adding the connection to config/connections.js

defaults: {
      host: '127.0.0.1',
      port: '8091',
      username: '',
      password: '',
      bucket: 'default',
      bucketPassword: '',
      updateConcurrency: 'optimistic',
      maxOptimisticRetries: 3,
      persist_to: 1,
      replicate_to: 0,
      doNotReturn: false,
      caseSensitive: false,
      testMode: false,
      consistency: 1,
      idStrategy: 'uuid_v1'
    }

The next attributes can be specified when specifying the connection for defaults, and can be overridden per transactions. Look at the find, update, create, delete methods for more info and examples.

Take into account that when doNotReturn is set to true, the default implementation of the sails blueprints WILL NOT work as expected. In case you want to use it create your own blueprints implementation under the api folder in the sails project. For more info on how to do that go here.

Please refer to the Couchbase documentation for more information about the Server configuration

Usage

The methods exposed below accepts special attributes. The usage is to pass an object with the attributes you need as second parameter as long as any other waterline option.

User.find({id: 'user::some-uuid-123456'}, {consistency:1, caseSensitive: true, testMode: false, limit: 5}, function (err, record) {
  console.log(record); 
}

Keep in mind that to use them you won't be able to use Dynamic Finders like findOne, or findById, findByName, etc, since those methods override the options parameter.

This adapter exposes the following methods:

find()
create()
update()
destroy()
query()

Running the tests

You can run the integration tests provided by waterline just by running npm test command.

The configuration used to run the integration tests is:

config: {
    schema: false,
    //Added to wait for all the n1ql response in order to pass the waterline integration tests.
    //Check more about consistency in the Couchbase N1QL Documentation.
    consistency:2,
    //Added to make adjusments for test time in order to pass the waterline integration tests.
    //Basically what is does is to force Order By Primary Key when no other criteria is present.
    testMode: true,
    //The Next one is Important to ensure the order by id works properly.
    idStrategy: 'increment' // will work all the times. Recommended to perform waterline integration tests.
    // idStrategy: 'uuid_v1' // will work almost all the time. (Once happened that it did not).
    // idStrategy: 'uuid_v4' // will work some times
},

Considerations

More Resources

License

MIT © 2016 Tomás Ruiz.

Sails is free and open-source under the MIT License.

githalytics.com alpha