| 1871 | } |
| 1872 | |
| 1873 | static async _findSeparate(results, options) { |
| 1874 | if (!options.include || options.raw || !results) return results; |
| 1875 | |
| 1876 | const original = results; |
| 1877 | if (options.plain) results = [results]; |
| 1878 | |
| 1879 | if (!results.length) return original; |
| 1880 | |
| 1881 | await Promise.all(options.include.map(async include => { |
| 1882 | if (!include.separate) { |
| 1883 | return await Model._findSeparate( |
| 1884 | results.reduce((memo, result) => { |
| 1885 | let associations = result.get(include.association.as); |
| 1886 | |
| 1887 | // Might be an empty belongsTo relation |
| 1888 | if (!associations) return memo; |
| 1889 | |
| 1890 | // Force array so we can concat no matter if it's 1:1 or :M |
| 1891 | if (!Array.isArray(associations)) associations = [associations]; |
| 1892 | |
| 1893 | for (let i = 0, len = associations.length; i !== len; ++i) { |
| 1894 | memo.push(associations[i]); |
| 1895 | } |
| 1896 | return memo; |
| 1897 | }, []), |
| 1898 | { |
| 1899 | |
| 1900 | ..._.omit(options, 'include', 'attributes', 'order', 'where', 'limit', 'offset', 'plain', 'scope'), |
| 1901 | include: include.include || [] |
| 1902 | } |
| 1903 | ); |
| 1904 | } |
| 1905 | |
| 1906 | const map = await include.association.get(results, { |
| 1907 | |
| 1908 | ..._.omit(options, nonCascadingOptions), |
| 1909 | ..._.omit(include, ['parent', 'association', 'as', 'originalAttributes']) |
| 1910 | }); |
| 1911 | |
| 1912 | for (const result of results) { |
| 1913 | result.set( |
| 1914 | include.association.as, |
| 1915 | map[result.get(include.association.sourceKey)], |
| 1916 | { raw: true } |
| 1917 | ); |
| 1918 | } |
| 1919 | })); |
| 1920 | |
| 1921 | return original; |
| 1922 | } |
| 1923 | |
| 1924 | /** |
| 1925 | * Search for a single instance by its primary key._ |