mdbgoose.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // Add the following index config in memdb.conf.js and restart memdbcluster
  17. //
  18. // collections : {
  19. // player : {
  20. // indexes : [
  21. // {
  22. // keys : ['areaId'],
  23. // valueIgnore : {
  24. // areaId : ['', -1],
  25. // },
  26. // },
  27. // {
  28. // keys : ['deviceType', 'deviceId'],
  29. // unique : true,
  30. // },
  31. // ]
  32. // }
  33. // },
  34. // npm install memdb-client
  35. // run with node >= 0.12 with --harmony option
  36. // We assume you have started shard 's1' on localhost:31017
  37. var memdb = require('memdb-client');
  38. var P = memdb.Promise;
  39. var mdbgoose = memdb.goose;
  40. // Define player schema
  41. var playerSchema = new mdbgoose.Schema({
  42. _id : String,
  43. name : String,
  44. areaId : Number,
  45. deviceType : Number,
  46. deviceId : String,
  47. items : [mdbgoose.SchemaTypes.Mixed],
  48. }, {collection : 'player'});
  49. // Define player model
  50. var Player = mdbgoose.model('player', playerSchema);
  51. var main = P.coroutine(function*(){
  52. // Connect to memdb
  53. yield mdbgoose.connectAsync({
  54. shards : { // specify all shards here
  55. s1 : {host : '127.0.0.1', port: 31017},
  56. s2 : {host : '127.0.0.1', port: 31018},
  57. }
  58. });
  59. // Make a transaction in s1
  60. yield mdbgoose.transactionAsync(P.coroutine(function*(){
  61. var player = new Player({
  62. _id : 'p1',
  63. name: 'rain',
  64. areaId : 1,
  65. deviceType : 1,
  66. deviceId : 'id1',
  67. items : [],
  68. });
  69. // insert a player
  70. yield player.saveAsync();
  71. // find player by id
  72. var doc = yield Player.findByIdAsync('p1');
  73. console.log('%j', doc);
  74. // find player by areaId, return array of players
  75. var docs = yield Player.findAsync({areaId : 1});
  76. console.log('%j', docs);
  77. // find player by deviceType and deviceId
  78. player = yield Player.findOneAsync({deviceType : 1, deviceId : 'id1'});
  79. // update player
  80. player.areaId = 2;
  81. yield player.saveAsync();
  82. // remove the player
  83. yield player.removeAsync();
  84. }), 's1');
  85. });
  86. if (require.main === module) {
  87. main().finally(process.exit);
  88. }