joins.test.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. var Test = require('../../support/convert-runner');
  2. describe('Converter :: ', function() {
  3. describe('Using Cursor Instructions (for single query situations) :: ', function() {
  4. describe('With Strategy 1 :: ', function() {
  5. it('should generate a find query with a left outer join', function() {
  6. Test({
  7. criteria: {
  8. model: 'user',
  9. method: 'find',
  10. criteria: {
  11. // Parent Criteria
  12. where: {
  13. type: 'beta user'
  14. },
  15. sort: [
  16. {
  17. amount: 'DESC'
  18. }
  19. ]
  20. }
  21. },
  22. // Join Instructions
  23. joins: [
  24. {
  25. strategy: {
  26. strategy: 1,
  27. meta: {
  28. parentFK: 'pet_id'
  29. }
  30. },
  31. instructions: [
  32. {
  33. parent: 'user',
  34. parentAlias: 'user__pet',
  35. parentKey: 'pet_id',
  36. child: 'pet',
  37. childAlias: 'pet__pet',
  38. childKey: 'id',
  39. alias: 'pet',
  40. removeParentKey: true,
  41. model: true,
  42. collection: false,
  43. criteria: {
  44. select: ['id', 'name', 'breed']
  45. }
  46. }
  47. ]
  48. }
  49. ],
  50. query: {
  51. select: ['pet__pet.id as pet__id', 'pet__pet.name as pet__name', 'pet__pet.breed as pet__breed'],
  52. from: 'user',
  53. orderBy: [
  54. {
  55. amount: 'DESC'
  56. }
  57. ],
  58. where: {
  59. type: 'beta user'
  60. },
  61. leftOuterJoin: [
  62. {
  63. from: 'pet as pet__pet',
  64. on: {
  65. user: 'pet_id',
  66. 'pet__pet': 'id'
  67. }
  68. }
  69. ]
  70. }
  71. });
  72. });
  73. });
  74. describe('With Strategy 2 :: ', function() {
  75. it('should generate a find query with a left outer join', function() {
  76. Test({
  77. criteria: {
  78. model: 'user',
  79. method: 'find',
  80. criteria: {
  81. // Parent Criteria
  82. where: {
  83. type: 'beta user'
  84. },
  85. sort: [
  86. {
  87. amount: 'DESC'
  88. }
  89. ],
  90. select: []
  91. }
  92. },
  93. // Join Instructions
  94. joins: [
  95. {
  96. strategy: {
  97. strategy: 2,
  98. meta: {
  99. childFK: 'user_id'
  100. }
  101. },
  102. instructions: [
  103. {
  104. parent: 'user',
  105. parentAlias: 'user__pets',
  106. parentKey: 'id',
  107. child: 'pet',
  108. childAlias: 'pet__pets',
  109. childKey: 'user_id',
  110. alias: 'pets',
  111. removeParentKey: true,
  112. model: false,
  113. collection: true,
  114. criteria: {
  115. select: ['id', 'name', 'breed', 'user_id']
  116. }
  117. }
  118. ]
  119. }
  120. ],
  121. query: {
  122. select: ['pet__pets.id as pets__id', 'pet__pets.name as pets__name', 'pet__pets.breed as pets__breed', 'pet__pets.user_id as pets__user_id'],
  123. from: 'user',
  124. orderBy: [
  125. {
  126. amount: 'DESC'
  127. }
  128. ],
  129. where: {
  130. type: 'beta user'
  131. },
  132. leftOuterJoin: [
  133. {
  134. from: 'pet as pet__pets',
  135. on: {
  136. user: 'id',
  137. 'pet__pets': 'user_id'
  138. }
  139. }
  140. ]
  141. }
  142. });
  143. });
  144. });
  145. describe('With Strategy 3 :: ', function() {
  146. it('should generate a find query with a two left outer join clauses', function() {
  147. Test({
  148. criteria: {
  149. model: 'user',
  150. method: 'find',
  151. criteria: {
  152. // Parent Criteria
  153. where: {
  154. type: 'beta user'
  155. },
  156. sort: [
  157. {
  158. amount: 'DESC'
  159. }
  160. ]
  161. }
  162. },
  163. // Join Instructions
  164. joins: [
  165. {
  166. strategy: {
  167. strategy: 3,
  168. meta: {
  169. junctorIdentity: 'user_pets__pets_users',
  170. junctorPK: 'id',
  171. junctorFKToParent: 'user_pets',
  172. junctorFKToChild: 'pet_users'
  173. }
  174. },
  175. instructions: [
  176. {
  177. parent: 'user',
  178. parentAlias: 'user__pets',
  179. parentKey: 'id',
  180. child: 'user_pets__pets_users',
  181. childAlias: 'user_pets__pets_users__pets',
  182. childKey: 'user_pets',
  183. alias: 'pets',
  184. removeParentKey: false,
  185. model: false,
  186. collection: true
  187. },
  188. {
  189. parent: 'user_pets__pets_users',
  190. parentAlias: 'user_pets__pets_users__pets',
  191. parentKey: 'pet_users',
  192. child: 'pet',
  193. childAlias: 'pet_pets',
  194. childKey: 'id',
  195. alias: 'pets',
  196. removeParentKey: false,
  197. model: false,
  198. collection: true,
  199. criteria: {
  200. select: ['id', 'name', 'breed']
  201. }
  202. }
  203. ]
  204. }
  205. ],
  206. query: {
  207. select: ['pet_pets.id as pets__id', 'pet_pets.name as pets__name', 'pet_pets.breed as pets__breed'],
  208. from: 'user',
  209. orderBy: [
  210. {
  211. amount: 'DESC'
  212. }
  213. ],
  214. where: {
  215. type: 'beta user'
  216. },
  217. leftOuterJoin: [
  218. {
  219. from: 'user_pets__pets_users as user_pets__pets_users__pets',
  220. on: {
  221. user: 'id',
  222. 'user_pets__pets_users__pets': 'user_pets'
  223. }
  224. },
  225. {
  226. from: 'pet as pet_pets',
  227. on: {
  228. 'pet_pets': 'id',
  229. 'user_pets__pets_users__pets': 'pet_users'
  230. }
  231. }
  232. ]
  233. }
  234. });
  235. });
  236. });
  237. });
  238. });