findOne.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. 'use strict';
  2. const upperFirst = require('lodash/upperFirst');
  3. const chai = require('chai');
  4. const expect = chai.expect;
  5. const Support = require('../support');
  6. const { DataTypes, sql } = require('@sequelize/core');
  7. describe(Support.getTestDialectTeaser('Include'), () => {
  8. describe('findOne', () => {
  9. it('should include a non required model, with conditions and two includes N:M 1:M', async function () {
  10. const A = this.sequelize.define('A', { name: DataTypes.STRING(40) }, { paranoid: true });
  11. const B = this.sequelize.define('B', { name: DataTypes.STRING(40) }, { paranoid: true });
  12. const C = this.sequelize.define('C', { name: DataTypes.STRING(40) }, { paranoid: true });
  13. const D = this.sequelize.define('D', { name: DataTypes.STRING(40) }, { paranoid: true });
  14. // Associations
  15. A.hasMany(B);
  16. B.belongsTo(D);
  17. B.belongsToMany(C, {
  18. through: 'BC',
  19. });
  20. C.belongsToMany(B, {
  21. through: 'BC',
  22. });
  23. D.hasMany(B);
  24. await this.sequelize.sync({ force: true });
  25. await A.findOne({
  26. include: [
  27. {
  28. model: B,
  29. required: false,
  30. include: [{ model: C, required: false }, { model: D }],
  31. },
  32. ],
  33. });
  34. });
  35. it('should work with a 1:M to M:1 relation with a where on the last include', async function () {
  36. const Model = this.sequelize.define('Model', {});
  37. const Model2 = this.sequelize.define('Model2', {});
  38. const Model4 = this.sequelize.define('Model4', { something: { type: DataTypes.INTEGER } });
  39. Model.belongsTo(Model2);
  40. Model2.hasMany(Model);
  41. Model2.hasMany(Model4);
  42. Model4.belongsTo(Model2);
  43. await this.sequelize.sync({ force: true });
  44. await Model.findOne({
  45. include: [
  46. {
  47. model: Model2,
  48. include: [{ model: Model4, where: { something: 2 } }],
  49. },
  50. ],
  51. });
  52. });
  53. it('should include a model with a where condition but no required', async function () {
  54. const User = this.sequelize.define('User', {}, { paranoid: false });
  55. const Task = this.sequelize.define(
  56. 'Task',
  57. {
  58. deletedAt: {
  59. type: DataTypes.DATE,
  60. },
  61. },
  62. { paranoid: false },
  63. );
  64. User.hasMany(Task, { foreignKey: 'userId' });
  65. Task.belongsTo(User, { foreignKey: 'userId' });
  66. await this.sequelize.sync({
  67. force: true,
  68. });
  69. const user0 = await User.create();
  70. await Task.bulkCreate([
  71. { userId: user0.get('id'), deletedAt: new Date() },
  72. { userId: user0.get('id'), deletedAt: new Date() },
  73. { userId: user0.get('id'), deletedAt: new Date() },
  74. ]);
  75. const user = await User.findOne({
  76. include: [{ model: Task, where: { deletedAt: null }, required: false }],
  77. });
  78. expect(user).to.be.ok;
  79. expect(user.tasks.length).to.equal(0);
  80. });
  81. it('should include a model with a where clause when the PK field name and attribute name are different', async function () {
  82. const User = this.sequelize.define('User', {
  83. id: {
  84. type: DataTypes.UUID,
  85. defaultValue: sql.uuidV4,
  86. field: 'main_id',
  87. primaryKey: true,
  88. },
  89. });
  90. const Task = this.sequelize.define('Task', {
  91. searchString: { type: DataTypes.STRING },
  92. });
  93. User.hasMany(Task, { foreignKey: 'userId' });
  94. Task.belongsTo(User, { foreignKey: 'userId' });
  95. await this.sequelize.sync({
  96. force: true,
  97. });
  98. const user0 = await User.create();
  99. await Task.bulkCreate([
  100. { userId: user0.get('id'), searchString: 'one' },
  101. { userId: user0.get('id'), searchString: 'two' },
  102. ]);
  103. const user = await User.findOne({
  104. include: [{ model: Task, where: { searchString: 'one' } }],
  105. });
  106. expect(user).to.be.ok;
  107. expect(user.tasks.length).to.equal(1);
  108. });
  109. it('should include a model with a through.where and required true clause when the PK field name and attribute name are different', async function () {
  110. const A = this.sequelize.define('a', {});
  111. const B = this.sequelize.define('b', {});
  112. const AB = this.sequelize.define('a_b', {
  113. name: {
  114. type: DataTypes.STRING(40),
  115. field: 'name_id',
  116. primaryKey: true,
  117. },
  118. });
  119. A.belongsToMany(B, { through: AB });
  120. B.belongsToMany(A, { through: AB });
  121. await this.sequelize.sync({ force: true });
  122. const [a0, b] = await Promise.all([A.create({}), B.create({})]);
  123. await a0.addB(b, { through: { name: 'Foobar' } });
  124. const a = await A.findOne({
  125. include: [{ model: B, through: { where: { name: 'Foobar' } }, required: true }],
  126. });
  127. expect(a).to.not.equal(null);
  128. expect(a.get('bs')).to.have.length(1);
  129. });
  130. it('should still pull the main record when an included model is not required and has where restrictions without matches', async function () {
  131. const A = this.sequelize.define('a', {
  132. name: DataTypes.STRING(40),
  133. });
  134. const B = this.sequelize.define('b', {
  135. name: DataTypes.STRING(40),
  136. });
  137. A.belongsToMany(B, { through: 'a_b' });
  138. B.belongsToMany(A, { through: 'a_b' });
  139. await this.sequelize.sync({ force: true });
  140. await A.create({
  141. name: 'Foobar',
  142. });
  143. const a = await A.findOne({
  144. where: { name: 'Foobar' },
  145. include: [{ model: B, where: { name: 'idontexist' }, required: false }],
  146. });
  147. expect(a).to.not.equal(null);
  148. expect(a.get('bs')).to.deep.equal([]);
  149. });
  150. it('should support a nested include (with a where)', async function () {
  151. const A = this.sequelize.define('A', {
  152. name: DataTypes.STRING,
  153. });
  154. const B = this.sequelize.define('B', {
  155. flag: DataTypes.BOOLEAN,
  156. });
  157. const C = this.sequelize.define('C', {
  158. name: DataTypes.STRING,
  159. });
  160. A.hasOne(B);
  161. B.belongsTo(A);
  162. B.hasMany(C);
  163. C.belongsTo(B);
  164. await this.sequelize.sync({ force: true });
  165. const a = await A.findOne({
  166. include: [
  167. {
  168. model: B,
  169. where: { flag: true },
  170. include: [
  171. {
  172. model: C,
  173. },
  174. ],
  175. },
  176. ],
  177. });
  178. expect(a).to.not.exist;
  179. });
  180. it('should support a belongsTo with the targetKey option', async function () {
  181. const User = this.sequelize.define('User', {
  182. username: { type: DataTypes.STRING, unique: true },
  183. });
  184. const Task = this.sequelize.define('Task', { title: DataTypes.STRING });
  185. User.removeAttribute('id');
  186. Task.belongsTo(User, { foreignKey: 'user_name', targetKey: 'username' });
  187. await this.sequelize.sync({ force: true });
  188. const newUser = await User.create({ username: 'bob' });
  189. const newTask = await Task.create({ title: 'some task' });
  190. await newTask.setUser(newUser);
  191. const foundTask = await Task.findOne({
  192. where: { title: 'some task' },
  193. include: [{ model: User }],
  194. });
  195. expect(foundTask).to.be.ok;
  196. expect(foundTask.user.username).to.equal('bob');
  197. });
  198. it('should support many levels of belongsTo (with a lower level having a where)', async function () {
  199. const A = this.sequelize.define('a', {});
  200. const B = this.sequelize.define('b', {});
  201. const C = this.sequelize.define('c', {});
  202. const D = this.sequelize.define('d', {});
  203. const E = this.sequelize.define('e', {});
  204. const F = this.sequelize.define('f', {});
  205. const G = this.sequelize.define('g', {
  206. name: DataTypes.STRING,
  207. });
  208. const H = this.sequelize.define('h', {
  209. name: DataTypes.STRING,
  210. });
  211. A.belongsTo(B);
  212. B.belongsTo(C);
  213. C.belongsTo(D);
  214. D.belongsTo(E);
  215. E.belongsTo(F);
  216. F.belongsTo(G);
  217. G.belongsTo(H);
  218. await this.sequelize.sync({ force: true });
  219. const [a0, b] = await Promise.all([
  220. A.create({}),
  221. (function (singles) {
  222. let promise = Promise.resolve();
  223. let previousInstance;
  224. let b;
  225. for (const model of singles) {
  226. const values = {};
  227. if (model.name === 'g') {
  228. values.name = 'yolo';
  229. }
  230. promise = (async () => {
  231. await promise;
  232. const instance = await model.create(values);
  233. if (previousInstance) {
  234. await previousInstance[`set${upperFirst(model.name)}`](instance);
  235. previousInstance = instance;
  236. return;
  237. }
  238. previousInstance = b = instance;
  239. })();
  240. }
  241. promise = promise.then(() => {
  242. return b;
  243. });
  244. return promise;
  245. })([B, C, D, E, F, G, H]),
  246. ]);
  247. await a0.setB(b);
  248. const a = await A.findOne({
  249. include: [
  250. {
  251. model: B,
  252. include: [
  253. {
  254. model: C,
  255. include: [
  256. {
  257. model: D,
  258. include: [
  259. {
  260. model: E,
  261. include: [
  262. {
  263. model: F,
  264. include: [
  265. {
  266. model: G,
  267. where: {
  268. name: 'yolo',
  269. },
  270. include: [{ model: H }],
  271. },
  272. ],
  273. },
  274. ],
  275. },
  276. ],
  277. },
  278. ],
  279. },
  280. ],
  281. },
  282. ],
  283. });
  284. expect(a.b.c.d.e.f.g.h).to.be.ok;
  285. });
  286. it('should work with combinding a where and a scope', async function () {
  287. const User = this.sequelize.define(
  288. 'User',
  289. {
  290. id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  291. name: DataTypes.STRING,
  292. },
  293. { underscored: true },
  294. );
  295. const Post = this.sequelize.define(
  296. 'Post',
  297. {
  298. id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, unique: true },
  299. owner_id: { type: DataTypes.INTEGER, unique: 'combiIndex' },
  300. owner_type: {
  301. type: DataTypes.ENUM(['user', 'org']),
  302. defaultValue: 'user',
  303. unique: 'combiIndex',
  304. },
  305. private: { type: DataTypes.BOOLEAN, defaultValue: false },
  306. },
  307. { underscored: true },
  308. );
  309. User.hasMany(Post, {
  310. foreignKey: 'owner_id',
  311. scope: { owner_type: 'user' },
  312. as: 'UserPosts',
  313. foreignKeyConstraints: false,
  314. });
  315. Post.belongsTo(User, { foreignKey: 'owner_id', as: 'Owner', foreignKeyConstraints: false });
  316. await this.sequelize.sync({ force: true });
  317. await User.findOne({
  318. where: { id: 2 },
  319. include: [{ model: Post, as: 'UserPosts', where: { private: true } }],
  320. });
  321. });
  322. });
  323. });