mirror of
				https://github.com/strapi/strapi.git
				synced 2025-11-04 11:54:10 +00:00 
			
		
		
		
	Merge branch 'master' into fix/join-multi-database
This commit is contained in:
		
						commit
						ae943d7492
					
				@ -84,7 +84,7 @@ const buildJoinsAndFilter = (qb, model, whereClauses) => {
 | 
			
		||||
 | 
			
		||||
      qb.leftJoin(
 | 
			
		||||
        `${originInfo.model.databaseName}.${assoc.tableCollectionName} AS ${joinTableAlias}`,
 | 
			
		||||
        `${joinTableAlias}.${singular(originInfo.model.collectionName)}_${
 | 
			
		||||
        `${joinTableAlias}.${singular(originInfo.model.globalId.toLowerCase())}_${
 | 
			
		||||
          originInfo.model.attributes[assoc.alias].column
 | 
			
		||||
        }`,
 | 
			
		||||
        `${originInfo.alias}.${originInfo.model.primaryKey}`
 | 
			
		||||
@ -92,7 +92,7 @@ const buildJoinsAndFilter = (qb, model, whereClauses) => {
 | 
			
		||||
 | 
			
		||||
      qb.leftJoin(
 | 
			
		||||
        `${destinationInfo.model.databaseName}.${destinationInfo.model.collectionName} AS ${destinationInfo.alias}`,
 | 
			
		||||
        `${joinTableAlias}.${singular(destinationInfo.model.collectionName)}_${
 | 
			
		||||
        `${joinTableAlias}.${singular(destinationInfo.model.globalId.toLowerCase())}_${
 | 
			
		||||
          destinationInfo.model.primaryKey
 | 
			
		||||
        }`,
 | 
			
		||||
        `${destinationInfo.alias}.${destinationInfo.model.primaryKey}`
 | 
			
		||||
 | 
			
		||||
@ -20,15 +20,22 @@ module.exports = {
 | 
			
		||||
      .filter(model => model !== 'core_store')
 | 
			
		||||
      .forEach(model => {
 | 
			
		||||
        (strapi.models[model].associations || []).forEach(association =>
 | 
			
		||||
          this.createLoader(association.collection || association.model, association.plugin)
 | 
			
		||||
          this.createLoader(
 | 
			
		||||
            association.collection || association.model,
 | 
			
		||||
            association.plugin
 | 
			
		||||
          )
 | 
			
		||||
        );
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
    // Reproduce the same pattern for each plugin.
 | 
			
		||||
    Object.keys(strapi.plugins).forEach(plugin => {
 | 
			
		||||
      Object.keys(strapi.plugins[plugin].models).forEach(model => {
 | 
			
		||||
        (strapi.plugins[plugin].models[model].associations || []).forEach(association =>
 | 
			
		||||
          this.createLoader(association.collection || association.model, association.plugin)
 | 
			
		||||
        (strapi.plugins[plugin].models[model].associations || []).forEach(
 | 
			
		||||
          association =>
 | 
			
		||||
            this.createLoader(
 | 
			
		||||
              association.collection || association.model,
 | 
			
		||||
              association.plugin
 | 
			
		||||
            )
 | 
			
		||||
        );
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
@ -55,9 +62,15 @@ module.exports = {
 | 
			
		||||
        return new Promise(async (resolve, reject) => {
 | 
			
		||||
          try {
 | 
			
		||||
            // Extract queries from keys and merge similar queries.
 | 
			
		||||
            const { queries, map } = this.extractQueries(model, _.cloneDeep(keys));
 | 
			
		||||
            const { queries, map } = this.extractQueries(
 | 
			
		||||
              model,
 | 
			
		||||
              _.cloneDeep(keys)
 | 
			
		||||
            );
 | 
			
		||||
 | 
			
		||||
            // Run queries in parallel.
 | 
			
		||||
            const results = await Promise.all(queries.map(query => this.makeQuery(model, query)));
 | 
			
		||||
            const results = await Promise.all(
 | 
			
		||||
              queries.map(query => this.makeQuery(model, query))
 | 
			
		||||
            );
 | 
			
		||||
 | 
			
		||||
            // Use to match initial queries order.
 | 
			
		||||
            const data = this.mapData(model, keys, map, results);
 | 
			
		||||
@ -80,7 +93,9 @@ module.exports = {
 | 
			
		||||
    // Use map to re-dispatch data correctly based on initial keys.
 | 
			
		||||
    return originalMap.map((query, index) => {
 | 
			
		||||
      // Find the index of where we should extract the results.
 | 
			
		||||
      const indexResults = map.findIndex(queryMap => queryMap.indexOf(index) !== -1);
 | 
			
		||||
      const indexResults = map.findIndex(
 | 
			
		||||
        queryMap => queryMap.indexOf(index) !== -1
 | 
			
		||||
      );
 | 
			
		||||
      const data = results[indexResults];
 | 
			
		||||
 | 
			
		||||
      // Retrieving referring model.
 | 
			
		||||
@ -90,7 +105,8 @@ module.exports = {
 | 
			
		||||
        // Return object instead of array for one-to-many relationship.
 | 
			
		||||
        return data.find(
 | 
			
		||||
          entry =>
 | 
			
		||||
            entry[ref.primaryKey].toString() === (query.params[ref.primaryKey] || '').toString()
 | 
			
		||||
            entry[ref.primaryKey].toString() ===
 | 
			
		||||
            (query.params[ref.primaryKey] || '').toString()
 | 
			
		||||
        );
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
@ -128,7 +144,11 @@ module.exports = {
 | 
			
		||||
 | 
			
		||||
      return data
 | 
			
		||||
        .filter(entry => entry !== undefined)
 | 
			
		||||
        .filter(entry => ids.map(id => id.toString()).includes(entry[ref.primaryKey].toString()))
 | 
			
		||||
        .filter(entry =>
 | 
			
		||||
          ids
 | 
			
		||||
            .map(id => id.toString())
 | 
			
		||||
            .includes(entry[ref.primaryKey].toString())
 | 
			
		||||
        )
 | 
			
		||||
        .slice(skip, skip + limit);
 | 
			
		||||
    });
 | 
			
		||||
  },
 | 
			
		||||
@ -170,10 +190,9 @@ module.exports = {
 | 
			
		||||
      .value();
 | 
			
		||||
 | 
			
		||||
    // Run query and remove duplicated ID.
 | 
			
		||||
    const request = await strapi.plugins['content-manager'].services['contentmanager'].fetchAll(
 | 
			
		||||
      { model },
 | 
			
		||||
      params
 | 
			
		||||
    );
 | 
			
		||||
    const request = await strapi.plugins['content-manager'].services[
 | 
			
		||||
      'contentmanager'
 | 
			
		||||
    ].fetchAll({ model }, params);
 | 
			
		||||
 | 
			
		||||
    return request && request.toJSON ? request.toJSON() : request;
 | 
			
		||||
  },
 | 
			
		||||
@ -196,9 +215,6 @@ module.exports = {
 | 
			
		||||
      // Retrieving referring model.
 | 
			
		||||
      const ref = this.retrieveModel(model, options.source);
 | 
			
		||||
 | 
			
		||||
      // Find similar query.
 | 
			
		||||
      const indexQueries = queries.findIndex(query => _.isEqual(query.options, options));
 | 
			
		||||
 | 
			
		||||
      // Generate array of IDs to fetch.
 | 
			
		||||
      const ids = [];
 | 
			
		||||
 | 
			
		||||
@ -211,21 +227,14 @@ module.exports = {
 | 
			
		||||
        ids.push(query[association.via]);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      if (indexQueries !== -1) {
 | 
			
		||||
        // Push to the same query the new IDs to fetch.
 | 
			
		||||
        queries[indexQueries].ids.push(...ids);
 | 
			
		||||
        map[indexQueries].push(index);
 | 
			
		||||
      } else {
 | 
			
		||||
        // Create new query in the query.
 | 
			
		||||
        queries.push({
 | 
			
		||||
          ids,
 | 
			
		||||
          options,
 | 
			
		||||
          alias: _.first(Object.keys(query)) || ref.primaryKey,
 | 
			
		||||
        });
 | 
			
		||||
      queries.push({
 | 
			
		||||
        ids,
 | 
			
		||||
        options,
 | 
			
		||||
        alias: _.first(Object.keys(query)) || ref.primaryKey,
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
        map[queries.length - 1 > 0 ? queries.length - 1 : 0] = [];
 | 
			
		||||
        map[queries.length - 1].push(index);
 | 
			
		||||
      }
 | 
			
		||||
      map[queries.length - 1 > 0 ? queries.length - 1 : 0] = [];
 | 
			
		||||
      map[queries.length - 1].push(index);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    return {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user