@@ -55,36 +55,43 @@ TarantoolConnection.prototype._getSpaceId = function(name){
5555 return this . select ( tarantoolConstants . Space . space , tarantoolConstants . IndexSpace . name , 1 , 0 ,
5656 tarantoolConstants . IteratorsType . all , [ name ] )
5757 . then ( function ( value ) {
58- var spaceId = value [ 0 ] [ 0 ] ;
59- this . namespace [ name ] = {
60- id : spaceId ,
61- name : name ,
62- indexes : { }
63- } ;
64- this . namespace [ spaceId ] = {
65- id : spaceId ,
66- name : name ,
67- indexes : { }
68- } ;
69- return spaceId ;
58+ if ( value && value . length && value [ 0 ] )
59+ {
60+ var spaceId = value [ 0 ] [ 0 ] ;
61+ this . namespace [ name ] = {
62+ id : spaceId ,
63+ name : name ,
64+ indexes : { }
65+ } ;
66+ this . namespace [ spaceId ] = {
67+ id : spaceId ,
68+ name : name ,
69+ indexes : { }
70+ } ;
71+ return spaceId ;
72+ }
73+ else
74+ {
75+ throw new Error ( 'Cannot read a space name or space is not defined' ) ;
76+ }
7077 } . bind ( this ) )
71- . catch ( function ( e ) {
72- reject ( new Error ( 'cannot detect space by name' ) ) ;
73- } ) ;
7478} ;
7579
7680TarantoolConnection . prototype . _getIndexId = function ( spaceId , indexName ) {
7781 return this . select ( tarantoolConstants . Space . index , tarantoolConstants . IndexSpace . indexName , 1 , 0 ,
7882 tarantoolConstants . IteratorsType . all , [ spaceId , indexName ] )
79- . then ( function ( value ) {
80- var indexId = value [ 0 ] [ 1 ] ;
81- var space = this . namespace [ spaceId ] ;
82- if ( space )
83- {
84- this . namespace [ space . name ] . indexes [ indexName ] = indexId ;
85- this . namespace [ space . id ] . indexes [ indexName ] = indexId ;
83+ . then ( function ( value ) {
84+ if ( value && value [ 0 ] && value [ 0 ] . length > 1 ) {
85+ var indexId = value [ 0 ] [ 1 ] ;
86+ var space = this . namespace [ spaceId ] ;
87+ if ( space ) {
88+ this . namespace [ space . name ] . indexes [ indexName ] = indexId ;
89+ this . namespace [ space . id ] . indexes [ indexName ] = indexId ;
90+ }
91+ return indexId ;
8692 }
87- return indexId ;
93+ else
94+ throw new Error ( 'Cannot read a space name indexes or index is not defined' ) ;
8895 } . bind ( this ) ) ;
8996} ;
9097
@@ -305,6 +312,8 @@ TarantoolConnection.prototype.ping = function(){
305312} ;
306313
307314TarantoolConnection . prototype . select = function ( spaceId , indexId , limit , offset , iterator , key ) {
315+ if ( Number . isInteger ( key ) )
316+ key = [ key ] ;
308317 return new Promise ( function ( resolve , reject ) {
309318 if ( typeof ( spaceId ) == 'string' || typeof ( indexId ) == 'string' )
310319 {
@@ -341,6 +350,8 @@ TarantoolConnection.prototype.select = function(spaceId, indexId, limit, offset,
341350} ;
342351
343352TarantoolConnection . prototype . delete = function ( spaceId , indexId , key ) {
353+ if ( Number . isInteger ( key ) )
354+ key = [ key ] ;
344355 return new Promise ( function ( resolve , reject ) {
345356 if ( Array . isArray ( key ) )
346357 {
@@ -372,6 +383,8 @@ TarantoolConnection.prototype.delete = function(spaceId, indexId, key){
372383} ;
373384
374385TarantoolConnection . prototype . update = function ( spaceId , indexId , key , ops ) {
386+ if ( Number . isInteger ( key ) )
387+ key = [ key ] ;
375388 return new Promise ( function ( resolve , reject ) {
376389 if ( Array . isArray ( ops ) && Array . isArray ( key ) ) {
377390 if ( typeof ( spaceId ) == 'string' || typeof ( indexId ) == 'string' )
@@ -403,6 +416,46 @@ TarantoolConnection.prototype.update = function(spaceId, indexId, key, ops){
403416 } . bind ( this ) ) ;
404417} ;
405418
419+
420+
421+ TarantoolConnection . prototype . upsert = function ( spaceId , key , ops , tuple ) {
422+ return new Promise ( function ( resolve , reject ) {
423+ if ( Number . isInteger ( key ) )
424+ key = [ key ] ;
425+ if ( Array . isArray ( ops ) && Array . isArray ( key ) ) {
426+ if ( typeof ( spaceId ) == 'string' || typeof ( indexId ) == 'string' )
427+ {
428+ return this . _getMetadata ( spaceId , indexId )
429+ . then ( function ( info ) {
430+ return this . update ( info [ 0 ] , info [ 1 ] , key , ops ) ;
431+ } . bind ( this ) )
432+ . then ( resolve )
433+ . catch ( reject ) ;
434+ }
435+ var reqId = requestId . getId ( ) ;
436+ var header = this . _header ( tarantoolConstants . RequestCode . rqUpsert , reqId ) ;
437+ var buffered = {
438+ spaceId : msgpack . encode ( spaceId ) ,
439+ //indexId: msgpack.encode(indexId),
440+ ops : msgpack . encode ( ops ) ,
441+ key : msgpack . encode ( key ) ,
442+ tuple : msgpack . encode ( tuple )
443+ } ;
444+ var body = Buffer . concat ( [ new Buffer ( [ 0x84 , tarantoolConstants . KeysCode . space_id ] ) , buffered . spaceId ,
445+ //new Buffer([tarantoolConstants.KeysCode.index_id]), buffered.indexId,
446+ new Buffer ( [ tarantoolConstants . KeysCode . key ] ) , buffered . key ,
447+ new Buffer ( [ tarantoolConstants . KeysCode . tuple ] ) , buffered . ops ,
448+ new Buffer ( [ tarantoolConstants . KeysCode . def_tuple ] ) , buffered . tuple ] ) ;
449+ console . log ( body ) ;
450+ this . _request ( header , body ) ;
451+ this . commandsQueue . push ( [ tarantoolConstants . RequestCode . rqUpsert , reqId , { resolve : resolve , reject : reject } ] ) ;
452+ }
453+ else
454+ reject ( new Error ( 'need array' ) ) ;
455+ } . bind ( this ) ) ;
456+ } ;
457+
458+
406459TarantoolConnection . prototype . eval = function ( expression ) {
407460 var tuple = Array . prototype . slice . call ( arguments , 1 ) ;
408461 return new Promise ( function ( resolve , reject ) {
0 commit comments