autoconnection.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2015 The MemDB Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. // npm install memdb-client
  17. // run with node >= 0.12 with --harmony option
  18. // We assume you have started shard 's1' on localhost:31017, 's2' on localhost:31018.
  19. var memdb = require('memdb-client');
  20. // just bluebird promise
  21. var P = memdb.Promise;
  22. var main = P.coroutine(function*(){
  23. // All database access should via this autoconn object, you can preserve autoconn object in a global module that can be accessed anywhere
  24. var autoconn = yield memdb.autoConnect({
  25. shards : { // Specify all shards here
  26. s1 : {host : '127.0.0.1', port : 31017},
  27. s2 : {host : '127.0.0.1', port : 31018},
  28. }
  29. });
  30. var doc = {_id : '1', name : 'rain', level : 1};
  31. // Get player collection object
  32. var Player = autoconn.collection('player');
  33. // Make a transaction in shard s1
  34. yield autoconn.transaction(P.coroutine(function*(){
  35. // Upsert a doc (update if exist, insert if not exist)
  36. yield Player.update(doc._id, doc, {upsert : true});
  37. // Find the doc
  38. var ret = yield Player.find(doc._id);
  39. console.log(ret); // {_id : '1', name : 'rain', level : 1}
  40. }), 's1'); // Auto commit after transaction
  41. try{
  42. // Make another transaction in shard s1
  43. yield autoconn.transaction(P.coroutine(function*(){
  44. // Update doc with $set modifier
  45. yield Player.update(doc._id, {$set : {level : 2}});
  46. // Find the changed doc with specified field
  47. var ret = yield Player.find(doc._id, 'level');
  48. console.log(ret); // {level : 2}
  49. // Exception here!
  50. throw new Error('Oops!');
  51. }), 's1');
  52. }
  53. catch(err){ // Catch the exception
  54. // Change is rolled back
  55. yield autoconn.transaction(P.coroutine(function*(){
  56. var ret = yield Player.find(doc._id, 'level');
  57. console.log(ret); // {level : 1}
  58. }), 's1');
  59. }
  60. // Make transcation in another shard
  61. yield autoconn.transaction(P.coroutine(function*(){
  62. yield Player.remove(doc._id);
  63. }), 's2');
  64. // Close all connections
  65. yield autoconn.close();
  66. });
  67. if (require.main === module) {
  68. main().finally(process.exit);
  69. }