mdbgoose.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. var P = require('bluebird');
  17. var util = require('util');
  18. var should = require('should');
  19. var env = require('../env');
  20. var memdb = require('../../lib');
  21. var logger = require('memdb-logger').getLogger('test', __filename);
  22. describe('mdbgoose test', function(){
  23. beforeEach(env.flushdb);
  24. it('mdbgoose', function(cb){
  25. var mdbgoose = memdb.goose;
  26. delete mdbgoose.connection.models.player;
  27. var Player = mdbgoose.model('player', new mdbgoose.Schema({
  28. _id : String,
  29. areaId : String,
  30. name : String,
  31. fullname : {first: String, second: String},
  32. items : [mdbgoose.SchemaTypes.Mixed],
  33. extra : mdbgoose.SchemaTypes.Mixed,
  34. }, {collection : 'player', versionKey: false}));
  35. return P.try(function(){
  36. return env.startCluster('s1', function(config){
  37. config.persistentDelay = 0;
  38. });
  39. })
  40. .then(function(ret){
  41. return mdbgoose.connectAsync(env.config);
  42. })
  43. .then(function(){
  44. return mdbgoose.transaction(function(){
  45. var player1 = new Player({
  46. _id : 'p1',
  47. areaId: 'a2',
  48. name: 'rain',
  49. fullname: {first: 'first', second: 'second'},
  50. items : [{name : 'item1', type : 1}],
  51. extra: {xx: 'extra val'},
  52. });
  53. var player2 = new Player({
  54. _id : 'p2',
  55. areaId: 'a1',
  56. name: 'snow',
  57. fullname: {first: 'first', second: 'second'},
  58. items : [{name : 'item1', type : 1}],
  59. extra: {},
  60. });
  61. return P.try(function(){
  62. return player1.saveAsync();
  63. })
  64. .then(function(){
  65. return Player.findAsync({_id : 'p1'})
  66. .then(function(players){
  67. players.length.should.eql(1);
  68. players[0]._id.should.eql('p1');
  69. });
  70. })
  71. .then(function(){
  72. return Player.find({_id : 'p1'})
  73. .select('name')
  74. .execAsync()
  75. .then(function(players){
  76. players[0].name.should.eql('rain');
  77. });
  78. })
  79. .then(function(){
  80. return Player.findOneAsync({_id : 'p1'})
  81. .then(function(player){
  82. player._id.should.eql('p1');
  83. });
  84. })
  85. .then(function(){
  86. return Player.findByIdAsync('p1')
  87. .then(function(player){
  88. player._id.should.eql('p1');
  89. });
  90. })
  91. .then(function(){
  92. return Player.findReadOnly({_id : 'p1'})
  93. .then(function(players){
  94. players.length.should.eql(1);
  95. players[0]._id.should.eql('p1');
  96. });
  97. })
  98. .then(function(){
  99. return Player.findOneReadOnlyAsync({_id : 'p1'})
  100. .then(function(player){
  101. player._id.should.eql('p1');
  102. });
  103. })
  104. .then(function(){
  105. return Player.findByIdReadOnlyAsync('p1')
  106. .then(function(player){
  107. player._id.should.eql('p1');
  108. });
  109. })
  110. .then(function(){
  111. return Player.findByIdAsync('p1');
  112. })
  113. .then(function(player){
  114. player.name = 'snow';
  115. player.areaId = 'a1';
  116. player.fullname.first = 'changed first';
  117. player.fullname.second = 'changed second';
  118. player.items.push({name : 'item2', type : 2});
  119. player.extra = {xx1 : 'changed extra'};
  120. return player.saveAsync();
  121. })
  122. .then(function(){
  123. return P.try(function(){
  124. return Player.findByIdAsync('p1');
  125. })
  126. .then(function(player){
  127. logger.debug('%j', player);
  128. player.name.should.eql('snow');
  129. player.items.length.should.eql(2);
  130. });
  131. })
  132. .then(function(){
  133. return player2.saveAsync();
  134. })
  135. .then(function(){
  136. return P.try(function(){
  137. return Player.findAsync({areaId : 'a1'});
  138. })
  139. .then(function(players){
  140. logger.debug('%j', players);
  141. players.length.should.eql(2);
  142. players.forEach(function(player){
  143. player.areaId.should.eql('a1');
  144. });
  145. });
  146. })
  147. .then(function(){
  148. return P.try(function(){
  149. return Player.countAsync({areaId : 'a1'});
  150. })
  151. .then(function(count){
  152. count.should.eql(2);
  153. });
  154. })
  155. .then(function(){
  156. return P.try(function(){
  157. return Player.findAsync({areaId : 'a1'}, null, {limit : 1});
  158. })
  159. .then(function(players){
  160. logger.debug('%j', players);
  161. players.length.should.eql(1);
  162. });
  163. })
  164. .then(function(){
  165. return Player.findByIdReadOnlyAsync('p1')
  166. .then(function(ret){
  167. logger.debug('%j', ret);
  168. });
  169. });
  170. }, 's1');
  171. })
  172. .delay(1000)
  173. .then(function(){
  174. // Call mongodb directly
  175. return Player.findMongoAsync();
  176. })
  177. .then(function(players){
  178. logger.debug('%j', players);
  179. players.length.should.eql(2);
  180. // players[0].name = 'changed';
  181. // return players[0].saveAsync()
  182. // .catch(function(e){
  183. // logger.error(e.stack); // should throw error
  184. // });
  185. })
  186. .then(function(){
  187. return Player.countMongoAsync({}); //TODO: callback not called when not pass any arguments, bluebird's bug?
  188. })
  189. .then(function(ret){
  190. ret.should.eql(2);
  191. })
  192. .then(function(){
  193. return mdbgoose.disconnectAsync();
  194. })
  195. .finally(function(){
  196. return env.stopCluster();
  197. })
  198. .nodeify(cb);
  199. });
  200. it('gen collection config', function(cb){
  201. var mdbgoose = memdb.goose;
  202. var Schema = mdbgoose.Schema;
  203. var types = mdbgoose.SchemaTypes;
  204. delete mdbgoose.connection.models.player;
  205. var DummySchema = new Schema({
  206. _id : String,
  207. name : String,
  208. first : {type : String, indexIgnore : ['']},
  209. last : {type : String, indexIgnore : ['']},
  210. groupId : {type : String, index : true, indexIgnore : [-1]},
  211. uniqKey : {type : String, unique : true},
  212. uniqKey2 : {type : String, index : {unique : true}},
  213. }, {collection : 'dummy'});
  214. DummySchema.index({first : 1, last : 1}, {unique : true});
  215. mdbgoose.model('Dummy', DummySchema);
  216. var config = mdbgoose.genCollectionConfig();
  217. var expected = {
  218. dummy: {
  219. indexes: [
  220. {
  221. keys: ['groupId'],
  222. valueIgnore: {
  223. groupId: [-1]
  224. }
  225. },
  226. {
  227. keys: ['uniqKey'],
  228. unique: true,
  229. valueIgnore: {
  230. uniqKey: []
  231. }
  232. },
  233. {
  234. keys: ['uniqKey2'],
  235. unique: true,
  236. valueIgnore: {
  237. uniqKey2: []
  238. }
  239. }, {
  240. keys: ['first', 'last'],
  241. valueIgnore: {
  242. first: [''],
  243. last: ['']
  244. },
  245. unique: true
  246. }]
  247. }
  248. };
  249. config.dummy.should.eql(expected.dummy);
  250. cb();
  251. });
  252. });