hasManyThrough.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. var assert = require('assert');
  2. var _ = require('@sailshq/lodash');
  3. var SchemaBuilder = require('../lib/waterline-schema');
  4. describe('Has Many Through :: ', function() {
  5. describe('Junction Tables', function() {
  6. var schema;
  7. before(function() {
  8. var fixtures = [
  9. {
  10. identity: 'user',
  11. primaryKey: 'id',
  12. attributes: {
  13. id: {
  14. type: 'number'
  15. },
  16. cars: {
  17. collection: 'car',
  18. through: 'drive',
  19. via: 'user'
  20. }
  21. }
  22. },
  23. {
  24. identity: 'drive',
  25. primaryKey: 'id',
  26. attributes: {
  27. id: {
  28. type: 'number'
  29. },
  30. car: {
  31. model: 'car'
  32. },
  33. user: {
  34. model: 'user'
  35. }
  36. }
  37. },
  38. {
  39. identity: 'car',
  40. primaryKey: 'id',
  41. attributes: {
  42. id: {
  43. type: 'number'
  44. },
  45. drivers: {
  46. collection: 'user',
  47. through: 'drive',
  48. via: 'car'
  49. }
  50. }
  51. }
  52. ];
  53. var collections = _.map(fixtures, function(obj) {
  54. var collection = function() {};
  55. collection.prototype = obj;
  56. return collection;
  57. });
  58. // Build the schema
  59. schema = SchemaBuilder(collections);
  60. });
  61. it('should flag the "through" table and not mark it as a junction table', function() {
  62. assert(schema.drive);
  63. assert(!schema.drive.junctionTable);
  64. assert(schema.drive.throughTable);
  65. });
  66. });
  67. describe('Reference Mapping', function() {
  68. var schema;
  69. before(function() {
  70. var fixtures = [
  71. {
  72. identity: 'foo',
  73. primaryKey: 'id',
  74. attributes: {
  75. id: {
  76. type: 'number'
  77. },
  78. bars: {
  79. collection: 'bar',
  80. through: 'foobar',
  81. via: 'foo'
  82. }
  83. }
  84. },
  85. {
  86. identity: 'foobar',
  87. primaryKey: 'id',
  88. attributes: {
  89. id: {
  90. type: 'number'
  91. },
  92. type: {
  93. type: 'string'
  94. },
  95. foo: {
  96. model: 'foo',
  97. columnName: 'foo_id'
  98. },
  99. bar: {
  100. model: 'bar',
  101. columnName: 'bar_id'
  102. }
  103. }
  104. },
  105. {
  106. identity: 'bar',
  107. primaryKey: 'id',
  108. attributes: {
  109. id: {
  110. type: 'number'
  111. },
  112. foo: {
  113. collection: 'foo',
  114. through: 'foobar',
  115. via: 'bar'
  116. }
  117. }
  118. }
  119. ];
  120. var collections = _.map(fixtures, function(obj) {
  121. var collection = function() {};
  122. collection.prototype = obj;
  123. return collection;
  124. });
  125. // Build the schema
  126. schema = SchemaBuilder(collections);
  127. });
  128. it('should update the parent collection to point to the join table', function() {
  129. assert.equal(schema.foo.schema.bars.references, 'foobar');
  130. assert.equal(schema.foo.schema.bars.on, 'foo_id');
  131. });
  132. });
  133. });