context.manyToMany.fixture.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. var structure = require('./context.fixture');
  2. /**
  3. * Context Fixture for a Many To Many Relationship
  4. */
  5. module.exports = function() {
  6. var context = structure;
  7. context.identity = 'foo';
  8. context.primaryKey = 'id';
  9. context.connections = {
  10. my_foo: {
  11. config: {},
  12. _adapter: {},
  13. _collections: []
  14. }
  15. };
  16. // Build Out Model Definitions
  17. var models = {
  18. foo: {
  19. identity: 'foo',
  20. datastore: 'my_foo',
  21. attributes: {
  22. id: {
  23. type: 'integer',
  24. autoIncrement: true,
  25. primaryKey: true,
  26. unique: true
  27. },
  28. name: {
  29. type: 'string'
  30. },
  31. bars: {
  32. collection: 'bar',
  33. via: 'foos',
  34. dominant: true
  35. },
  36. foobars: {
  37. collection: 'baz' ,
  38. via: 'foo',
  39. dominant: true
  40. }
  41. }
  42. },
  43. bar: {
  44. identity: 'bar',
  45. datastore: 'my_foo',
  46. attributes: {
  47. id: {
  48. type: 'integer',
  49. autoIncrement: true,
  50. primaryKey: true,
  51. unique: true
  52. },
  53. name: {
  54. type: 'string'
  55. },
  56. foos: {
  57. collection: 'foo',
  58. via: 'bars'
  59. }
  60. }
  61. },
  62. baz: {
  63. identity: 'baz',
  64. datastore: 'my_foo',
  65. attributes: {
  66. id: {
  67. type: 'integer',
  68. autoIncrement: true,
  69. primaryKey: true,
  70. unique: true
  71. },
  72. foo: {
  73. model: 'foo'
  74. }
  75. }
  76. },
  77. bar_foos__foo_bars: {
  78. identity: 'bar_foos__foo_bars',
  79. datastore: 'my_foo',
  80. tables: ['bar', 'foo'],
  81. junctionTable: true,
  82. attributes: {
  83. id: {
  84. primaryKey: true,
  85. autoIncrement: true,
  86. type: 'integer'
  87. },
  88. bar_foos: {
  89. columnName: 'bar_foos',
  90. type: 'integer',
  91. foreignKey: true,
  92. references: 'bar',
  93. on: 'id',
  94. via: 'foo_bars',
  95. groupBy: 'bar'
  96. },
  97. foo_bars: {
  98. columnName: 'foo_bars',
  99. type: 'integer',
  100. foreignKey: true,
  101. references: 'foo',
  102. on: 'id',
  103. via: 'bar_foos',
  104. groupBy: 'foo'
  105. }
  106. }
  107. }
  108. };
  109. // Set context collections
  110. context.waterline.collections = models;
  111. // Set collection attributes
  112. context._attributes = models.foo.attributes;
  113. context.attributes = context._attributes;
  114. context.waterline.connections = context.connections;
  115. // Build Up Waterline Schema
  116. context.waterline.schema.foo = {
  117. identity: 'foo',
  118. datastore: 'my_foo',
  119. attributes: {
  120. id: {
  121. type: 'integer',
  122. autoIncrement: true,
  123. primaryKey: true,
  124. unique: true
  125. },
  126. name: {
  127. type: 'string'
  128. },
  129. bars: {
  130. collection: 'bar_foos__foo_bars',
  131. references: 'bar_foos__foo_bars',
  132. on: 'bar_foos'
  133. },
  134. foobars: {
  135. collection: 'baz',
  136. references: 'baz',
  137. on: 'foo_id'
  138. }
  139. }
  140. };
  141. context.waterline.schema.bar = {
  142. identity: 'bar',
  143. datastore: 'my_foo',
  144. attributes: {
  145. id: {
  146. type: 'integer',
  147. autoIncrement: true,
  148. primaryKey: true,
  149. unique: true
  150. },
  151. name: {
  152. type: 'string'
  153. },
  154. foos: {
  155. collection: 'bar_foos__foo_bars',
  156. references: 'bar_foos__foo_bars',
  157. on: 'foo_bars'
  158. }
  159. }
  160. };
  161. context.waterline.schema.baz = {
  162. identity: 'baz',
  163. datastore: 'my_foo',
  164. attributes: {
  165. id: {
  166. type: 'integer',
  167. autoIncrement: true,
  168. primaryKey: true,
  169. unique: true
  170. },
  171. foo: {
  172. columnName: 'foo_id',
  173. type: 'integer',
  174. foreignKey: true,
  175. references: 'foo',
  176. on: 'id'
  177. }
  178. }
  179. };
  180. context.waterline.schema.bar_foos__foo_bars = {
  181. identity: 'bar_foos__foo_bars',
  182. datastore: 'my_foo',
  183. tables: ['bar', 'foo'],
  184. junctionTable: true,
  185. attributes: {
  186. id: {
  187. primaryKey: true,
  188. autoIncrement: true,
  189. type: 'integer'
  190. },
  191. bar_foos: {
  192. columnName: 'bar_foos',
  193. type: 'integer',
  194. foreignKey: true,
  195. references: 'bar',
  196. on: 'id',
  197. via: 'foo_bars',
  198. groupBy: 'bar'
  199. },
  200. foo_bars: {
  201. columnName: 'foo_bars',
  202. type: 'integer',
  203. foreignKey: true,
  204. references: 'foo',
  205. on: 'id',
  206. via: 'bar_foos',
  207. groupBy: 'foo'
  208. }
  209. }
  210. };
  211. return context;
  212. };