Skip to content

Commit 4690fca

Browse files
committed
Upsert finished
1 parent fab8d4b commit 4690fca

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Promise resolve an array of deleted tuples.
7777

7878
**update(spaceId: Number or String, indexId: Number or String, key: tuple, ops) : Promise(Array of tuples)**
7979

80-
Ops: http://tarantool.org/doc/book/box/box_space.html(search for update here).
80+
Ops: http://tarantool.org/doc/book/box/box_space.html (search for update here).
8181

8282
Promise resolve an array of updated tuples.
8383

@@ -87,12 +87,14 @@ So it's insert. More you can read here: http://tarantool.org/doc/book/box/box_sp
8787

8888
Promise resolve a new tuple.
8989

90-
**upsert(spaceId: Number or String, tuple: tuple) : Promise(Tuple)**
90+
**upsert(spaceId: Number or String, key: tuple, ops: array of operations, tuple: tuple) : Promise()**
9191

92-
So it's insert. More you can read here: http://tarantool.org/doc/book/box/box_space.html
92+
About operation: https://github.com/tarantool/tarantool/issues/905
9393

94-
Promise resolve a new tuple.
94+
Ops: http://tarantool.org/doc/book/box/box_space.html (search for update here).
9595

96+
Promise resolve nothing.
97+
Knowing issues if it cannot be updated by your ops it wouldn't return error just only at tarantool side but not at protocol. See https://github.com/tarantool/tarantool/issues/966
9698

9799
**replace(spaceId: Number or String, tuple: tuple) : Promise(Tuple)**
98100

@@ -145,6 +147,12 @@ Then just a use **npm test** and it will use mocha and launch test.
145147

146148
It's ok you can do whatever you need. I add log options for some technical information it can be help for you. If i don't answer i just miss email :( it's a lot emails from github so please write me to newbiecraft@gmail.com directly if i don't answer in one day.
147149

150+
##Changelog
151+
152+
###0.3.0
153+
Add upsert operation.
154+
Key is now can be just a number.
155+
148156
##ToDo
149157

150158
Test **eval** methods and make benchmarks and improve performance.

lib/connection.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,11 @@ TarantoolConnection.prototype.upsert = function(spaceId, key, ops, tuple){
423423
if (Number.isInteger(key))
424424
key = [key];
425425
if (Array.isArray(ops) && Array.isArray(key)){
426-
if (typeof(spaceId)=='string' || typeof(indexId)=='string')
426+
if (typeof(spaceId)=='string')
427427
{
428-
return this._getMetadata(spaceId, indexId)
428+
return this._getMetadata(spaceId, 0)
429429
.then(function(info){
430-
return this.update(info[0], info[1], key, ops);
430+
return this.upsert(info[0], key, ops, tuple);
431431
}.bind(this))
432432
.then(resolve)
433433
.catch(reject);
@@ -444,9 +444,10 @@ TarantoolConnection.prototype.upsert = function(spaceId, key, ops, tuple){
444444
var body = Buffer.concat([new Buffer([0x84,tarantoolConstants.KeysCode.space_id]), buffered.spaceId,
445445
//new Buffer([tarantoolConstants.KeysCode.index_id]), buffered.indexId,
446446
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);
447+
new Buffer([tarantoolConstants.KeysCode.def_tuple]), buffered.ops,
448+
new Buffer([tarantoolConstants.KeysCode.tuple]), buffered.tuple]);
449+
if (this.options.log)
450+
console.log(body);
450451
this._request(header, body);
451452
this.commandsQueue.push([tarantoolConstants.RequestCode.rqUpsert, reqId, {resolve: resolve, reject: reject}]);
452453
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tarantool-driver",
3-
"version": "0.2.2",
3+
"version": "0.3.0",
44
"description": "Tarantool driver for 1.6",
55
"main": "index.js",
66
"scripts": {

test/app.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,14 @@ describe('Tarantool Connection tests', function(){
242242
var conn;
243243
before(function(done){
244244
try{
245-
conn = new TarantoolConnection({port: 33013, log: true});
245+
conn = new TarantoolConnection({port: 33013});
246246
conn.connect().then(function(){
247247
return conn.auth('test', 'test');
248248
}, function(e){ throw 'not connected'; done();})
249249
.then(function(){
250250
return Promise.all([
251-
conn.delete('upstest', 'primary', 1)
251+
conn.delete('upstest', 'primary', 1),
252+
conn.delete('upstest', 'primary', 2)
252253
]);
253254
})
254255
.then(function(){
@@ -262,16 +263,38 @@ describe('Tarantool Connection tests', function(){
262263
console.log(e);
263264
}
264265
});
265-
it('try', function(done){
266-
conn.upsert(517, [1], [['+', 3, 3]], [1, 2, 3])
267-
.then(function(tuple){
268-
assert.deepEqual(tuple, [1, 2, 3]);
266+
it('insert', function(done){
267+
conn.upsert('upstest', 1, [['+', 3, 3]], [1, 2, 3])
268+
.then(function() {
269+
return conn.select('upstest', 'primary', 1, 0, 'eq', 1);
270+
})
271+
.then(function(tuples){
272+
assert.equal(tuples.length, 1);
273+
assert.deepEqual(tuples[0], [1, 2, 3]);
269274
done();
270275
})
271276

272277
.catch(function(e){
273278
done(e);
274279
})
275-
})
280+
});
281+
it('update', function(done){
282+
conn.upsert('upstest', 2, [['+', 2, 2]], [2, 4, 3])
283+
.then(function(){
284+
return conn.upsert('upstest', 2, [['+', 2, 2]], [2, 4, 3]);
285+
})
286+
.then(function() {
287+
return conn.select('upstest', 'primary', 1, 0, 'eq', 2) ;
288+
})
289+
.then(function(tuples){
290+
assert.equal(tuples.length, 1);
291+
assert.deepEqual(tuples[0], [2, 4, 5]);
292+
done();
293+
})
294+
295+
.catch(function(e){
296+
done(e);
297+
})
298+
});
276299
});
277300
});

0 commit comments

Comments
 (0)