populateArray.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. var assert = require('assert');
  2. var Waterline = require('../../../../lib/waterline');
  3. describe('Collection Query ::', function() {
  4. describe('specific populated associations ::', function() {
  5. var Car;
  6. before(function(done) {
  7. var waterline = new Waterline();
  8. var collections = {};
  9. collections.user = Waterline.Model.extend({
  10. identity: 'user',
  11. datastore: 'foo',
  12. primaryKey: 'id',
  13. attributes: {
  14. id: {
  15. type: 'number'
  16. },
  17. car: {
  18. model: 'car'
  19. },
  20. name: {
  21. columnName: 'my_name',
  22. type: 'string'
  23. }
  24. }
  25. });
  26. collections.ticket = Waterline.Model.extend({
  27. identity: 'ticket',
  28. datastore: 'foo',
  29. primaryKey: 'id',
  30. attributes: {
  31. id: {
  32. type: 'number'
  33. },
  34. reason: {
  35. columnName: 'reason',
  36. type: 'string'
  37. },
  38. car: {
  39. model: 'car'
  40. }
  41. }
  42. });
  43. collections.car = Waterline.Model.extend({
  44. identity: 'car',
  45. datastore: 'foo',
  46. primaryKey: 'id',
  47. attributes: {
  48. id: {
  49. type: 'number'
  50. },
  51. driver: {
  52. model: 'user',
  53. columnName: 'foobar'
  54. },
  55. tickets: {
  56. collection: 'ticket',
  57. via: 'car'
  58. }
  59. }
  60. });
  61. waterline.registerModel(collections.user);
  62. waterline.registerModel(collections.car);
  63. waterline.registerModel(collections.ticket);
  64. // Fixture Adapter Def
  65. var adapterDef = {
  66. identity: 'foo',
  67. find: function(con, query, cb) {
  68. if(query.using === 'user') {
  69. return cb(null, [{ id: 1, car: 1, name: 'John Doe' }]);
  70. }
  71. if(query.using === 'car') {
  72. return cb(null, [{ id: 1, foobar: 1, tickets: [1, 2]}]);
  73. }
  74. if(query.using === 'ticket') {
  75. return cb(null, [{ id: 1, reason: 'red light', car:1}, { id: 2, reason: 'Parking in a disabled space', car: 1 }]);
  76. }
  77. return cb();
  78. }
  79. };
  80. var connections = {
  81. 'foo': {
  82. adapter: 'foobar'
  83. }
  84. };
  85. waterline.initialize({ adapters: { foobar: adapterDef }, datastores: connections }, function(err, orm) {
  86. if (err) {
  87. return done(err);
  88. }
  89. Car = orm.collections.car;
  90. return done();
  91. });
  92. });
  93. it('should populate all related collections', function(done) {
  94. Car.find()
  95. .populate('driver')
  96. .populate('tickets')
  97. .exec(function(err, car) {
  98. if (err) {
  99. return done(err);
  100. }
  101. assert(car[0].driver);
  102. assert(car[0].driver.name);
  103. assert(car[0].tickets);
  104. assert(car[0].tickets[0].car);
  105. assert(car[0].tickets[1].car);
  106. return done();
  107. });
  108. });
  109. });
  110. });