You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This client is a port from the original Golang client.
Internally it's is more or less the same, but this driver provides a friendlier javascript interface.
// You should prepare your queries and send them in a single batch// For optimal performancevarc=client.prepare()// This callback will be the first to be executedc.get("sample_key",function(err,value,res){if(err)throwerr// Do something...})c.get("sample_key2",function(err,value,res){if(err)throwerr// Do something...})c.put("some_key","some_value",function(err){if(err)throwerr// Do something})// The flush callback is the last one to be calledc.flush(function(err,res){if(err)throwerrconsole.log('Sucessfuly flushed %d queries.',res.responses.length)})
Advanced client usage II (Transactions)
varopts={name: "transaction example",}varerrNoApples=newError('Insufficient apples!')vartransaction=function(txn,commit,abort){txn.get("applesInStock",function(err,value,res){if(err||applesInStock.err){returnabort(err)}vardispatch=5varinStock=parseInt(applesInStock.value)if(inStock<dispatch){returnabort(errNoApples)}// Upgrade for a prepared clienttxn=txn.prepare()txn.increment("applesInStock",-dispatch)txn.increment("applesInRoute",+dispatch)// Commit automatically flushescommit()})}client.runTransaction(opts,transaction,function(err,res){if(err===errNoApples){// Alert user there are no more apples...}elseif(err){// Transaction failed...}else{// Transaction commited...}})
ConditionalPut sets the value for a key if the existing value matches the ifValue.
Specifying an empty or null ifValue means the entry must not yet exist.
client.contains("john",function(err,exists,res){if(exists===true){// john exists in the datastore}})
client.increment(key, increment, callback)
Increment increments the value at the specified key by some increment value.
Once called for a key, Put & Get will return errors; only Increment will continue to be a valid command.
The value must be deleted before it can be reset using Put.
Parameters
name
type
description
key
string
increment
integer
callback
callback
function(err, newValue, res) {}
Callback
name
type
description
err
Error()
newValue
integer
the new value for this counter, after the increment operation
Return you a new prepared client. It has all the methods from the original client.
Read Advanced client usage II to understand how to use this client.
You should always use this client when sending in multiple queries, this will batch them together in a single request.
varc=client.prepare()c.get("key",function(err,value,res){// Do something...})c.get("key2",function(err,value,res){// Do something...})c.put("key3","value",function(err,res){// Do something...})c.flush()
client.flush(callback)
Flush the prepared queries buffer, and send it as a batch request.
RunTransaction executes a retryable transaction function in
the context of a distributed transaction. The transaction is
automatically aborted if retryable function returns any error aside from
recoverable internal errors, and is automatically committed otherwise.
retryable should have no side effects which could cause problems in the event
it must be run more than once. The opts contains transaction settings.
The res argument contains the full database response, each database command can
contain a different set of properties. This document will try to state some of the possible properties.
Properties
property
type
description
timestamp
integer
timestamp of the returned entry
wall_time
integer
timestamp of when the read or write operation was performed
Transaction function
The transaction function is an retryable function, it may be
executed more than once. This function should never forget to
call commit or abort. Throwing an error inside this
function also aborts the transaction.
this client is the same as client.prepare(), you can flush yourself if you don't wan't to commit yet.
commit
callback
to try to commit transaction
abort
callback
to abort transaction
abort() accepts an optional Error. This error will be passed to the
.runTransaction callback.
Example
vartransaction=function(txn,commit,abort){txn=txn.prepare()for(vari=0;i<100;i++){varkey=i.toString()txn.put(key,"hello")}// Commit automatically flushes// the prepared transaction.commit()}
Compiling .proto files
Cockroachdb's protocol buffer files are mantained at a repository called cockroachdb/cockroach-proto, this is
maintained as a subtree in this library, in case you need to manually update or change them, follow this steps.
If you want to sync them with the latest proto files
You will need to update the folder cockroach-proto with the latest content of the cockroachdb/cockroach-proto repository, you could do this with:
Notice: I'm not sure if this is a good pattern, just be sure to update the folder contents.
Recompile the .proto files
Run the following npm script to compile the .proto files to javascript, it will automatically place the files in the lib folder.
$ npm run build-proto
Custom clock module
You may wan't to use a custom clock module in some cases, you can pass it through the clock option, when instantiating a new Client.
Your clock module should have a now method, and this method should return the current timestamp in nanoseconds, here is an example:
varclockModule=module.exports={now: functionTimestampNanoseconds(){// Should return the current timestamp in nanosecondsreturn ...;}}varclient=newClient({clock: clockModule})