Tarantool driver
Connector - high-levelConnector implements Tarantool binary protocol and exposes nice interface to access Tarantool.
Connector uses Transport to compose and parse request and response headers.
NPM
npm install tarantool
Notes on Tarantool Connector
Connector hides protocol-related stuff under the hood but there are things to know about before using it.
Tarantool database consists of Spaces (tables) and Tuples (rows). Spaces have no name, only numeric id.
This module provides three interfaces — Connection, Mapping and Space.
Connection contains methods to send all kinds of requests, but arguments (and results) are binary Tuples — not usable.
Mapping knows how to convert Object to Tuple and vice versa (thanks to spec
) and Space is a Mapping with specified space id.
Call ping
on Connector, call
on Mapping and insert
, select
, update
, delete
on Space.
spec
Object-to-Tuple binding specification — There are three inner types in Tarantool storage: int32
, int64
and buffer
(octet string). All integers (options, flags, fields, space ids) are unsigned.
Connector can encode some pseudo-types:
int32
: Unsigned 32-bit integer.int53
: Stored as internalint64
. A native unsigned JS Number limited to 53 bit, stored natively without lost of significance.int64
: Accepts and returnsBigNum
objects frombignum
.buffer
: Raw binary buffer.string
: Stored asbuffer
UTF-8 string.object
: Stored asbuffer
UTF-8 string with JSON content.
In order to use custom type, instead of providing it's name, pass object having pack: (value) -> buffer
and unpack: (buffer) -> value
methods as in example below.
Build spec
object to map Object and Tuples to each other. Tarantool knows nothing about field names or types and Mapping maps them depending on spec
.
Example of valid spec
:
spec = id: 'int32'name: 'string'flags: 'object'smth_hard: pack: ...: ...
We specify three field-related things: order, name and type. spec
tells Mapping which place should every field take and how to convert it.
Use any string containing '32' to specity int32 type, same for 53 and 64
API
Connection
tc
stands for Tarantool Connection
# create Connection using default Transport # default port is 33013 tc = Tarantoolconnect porthostcallback# OR create Connection using Transport, any object, with `request(type, body, callback)` # tc = new Tarantool transport # make use of connection tcinsert spacetupleflagscallbacktcselect spacetuplesindexoffsetlimitcallbacktcupdate spacetupleoperationsflagscallbacktcdelete spacetupleflagscallbacktccall proctupleflagscallbacktcping callback
space
,flags
,offset
andlimit
are Integersspace
is Space numberflags
is optional field, possible values are stored inTarantool.flags
in camelCase, e.g. Tarantool.flags.returnTupleoffset
andlimit
are optional, use them to specify ammount of returned with selecttuples
is an Array of tuplestuple
is an Array of Fields, each Field is Bufferproc
is a Stringoperations
is an Array ofoperation
, they are constructed via Mapping or Space methods (see below)callback
is a Function that is called ascallback (returnCode, body)
wherebody
is an Array oftuples
or a Number or an error string ifreturnCode
is non-zero.
Mapping
Use Mapping to access several spaces with similar structure.
Mapping API:
# creating mapping with specified spec mapping = tcmapping spec # forgetting about tuples mappinginsert spaceobjectflagscallbackmappingselect spaceobjectsindexoffsetlimitcallbackmappingupdate spaceobjectoperationsflagscallbackmappingdelete spaceobjectflagscallbackmappingcall procobjectflagscallback
callback
will be called as callback (returnCode, body)
where body
is an Array of Objects or a Number or an error string if returnCode
is non-zero.
# creating operations list — see below mappingassign argumentmappingadd argumentmappingand argumentmappingxor argumentmappingor argumentmappingdelete argumentmappinginsertBefore argumentmappingsplice spliceArgument
spliceArgument
is a Hash (Object) with three keys: string
(String), offset
(Number) and length
(Number).
argument
is a Hash (Object) with single key.
Space
Space incapsulates Mapping and space number to give you shortest API:
# creating Space with spec space = tcspace spacespec# OR with mapping # mapping = tc.mapping spec # space = tc.space space, mapping spaceinsert objectflagscallbackspaceselect objectsindexoffsetlimitcallbackspaceupdate objectoperationsflagscallbackspacedelete objectflagscallback # creating operations list spaceassign argumentspaceadd argumentspaceand argumentspacexor argumentspaceor argumentspacedelete argumentspaceinsertBefore argumentspacesplice spliceArgument
Operations
Tarantool's update deals with "operations" — atomic field actions.
Here's an example:
spec = id: 'i32'name: 'string'winner: 'i32'userSpace = tcspace 2specoperations = userSpaceor winner: 1 userSpacesplice name: offset: 0length: 0string: '[Winner] 'userSpaceupdate id: userId operations-> consolelog 'winner now has [Winner] tag before his name'
TODO
- check if Buffer.concat is fast enough, if it is slow - replace with array of buffers, concat only before transport.request
- check argument type in operations
- catch socket errors and reconnect
- write about Tarantool keys and multi-object select
- write tests
Bugs and issues
Bug reports and pull requests are welcome.
LICENSE
Tarantool Connector for node.js is published under MIT license.