strapi-plugin-populate-all 1.5.0 → 1.5.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -35,21 +35,27 @@ const config = {
35
35
  }
36
36
  };
37
37
  const queryCache = {};
38
+ const buildCacheKey = (modelUid, parentsModelUids) => {
39
+ if (parentsModelUids.length === 0) {
40
+ return modelUid;
41
+ }
42
+ return `${parentsModelUids.join("|")}|${modelUid}`;
43
+ };
38
44
  const getPopulateQuery = (modelUid, parentsModelUids = []) => {
39
45
  try {
40
46
  const useCache = strapi.plugin("populate-all").config("cache") ?? true;
41
- if (useCache && queryCache[modelUid]) {
42
- strapi.log.debug(`[populate-all] query cache hit: ${modelUid}`);
43
- return structuredClone(queryCache[modelUid]);
47
+ const cacheKey = buildCacheKey(modelUid, parentsModelUids);
48
+ if (useCache && queryCache[cacheKey]) {
49
+ strapi.log.debug(`[populate-all] query cache hit: ${cacheKey}`);
50
+ return structuredClone(queryCache[cacheKey]);
44
51
  }
45
52
  if (parentsModelUids.includes(modelUid)) {
46
53
  strapi.log.debug(
47
54
  `[populate-all] loop detected skipping population: ${modelUid}`
48
55
  );
49
56
  return { populate: {} };
50
- } else {
51
- parentsModelUids.push(modelUid);
52
57
  }
58
+ const nextParentsModelUids = [...parentsModelUids, modelUid];
53
59
  const query = { populate: {} };
54
60
  const model = strapi.getModel(modelUid);
55
61
  for (const [fieldName, attribute] of Object.entries(
@@ -63,7 +69,7 @@ const getPopulateQuery = (modelUid, parentsModelUids = []) => {
63
69
  const components = Object.fromEntries(
64
70
  attribute.components.map((component) => [
65
71
  component,
66
- getPopulateQuery(component, parentsModelUids)
72
+ getPopulateQuery(component, nextParentsModelUids)
67
73
  ])
68
74
  );
69
75
  query.populate[fieldName] = { on: components };
@@ -72,7 +78,7 @@ const getPopulateQuery = (modelUid, parentsModelUids = []) => {
72
78
  if (attribute.type === "component") {
73
79
  query.populate[fieldName] = getPopulateQuery(
74
80
  attribute.component,
75
- parentsModelUids
81
+ nextParentsModelUids
76
82
  );
77
83
  continue;
78
84
  }
@@ -85,7 +91,7 @@ const getPopulateQuery = (modelUid, parentsModelUids = []) => {
85
91
  query.populate[fieldName] = getPopulateQuery(
86
92
  // @ts-expect-error target actually exists on attribute
87
93
  attribute.target,
88
- parentsModelUids
94
+ nextParentsModelUids
89
95
  );
90
96
  continue;
91
97
  }
@@ -93,7 +99,7 @@ const getPopulateQuery = (modelUid, parentsModelUids = []) => {
93
99
  query.populate[fieldName] = getPopulateQuery(
94
100
  // @ts-expect-error target actually exists on attribute
95
101
  attribute.target,
96
- parentsModelUids
102
+ nextParentsModelUids
97
103
  );
98
104
  continue;
99
105
  }
@@ -106,8 +112,8 @@ const getPopulateQuery = (modelUid, parentsModelUids = []) => {
106
112
  query.populate = true;
107
113
  }
108
114
  if (useCache) {
109
- strapi.log.debug(`[populate-all] new query cached: ${modelUid}`);
110
- queryCache[modelUid] = query;
115
+ strapi.log.debug(`[populate-all] new query cached: ${cacheKey}`);
116
+ queryCache[cacheKey] = query;
111
117
  }
112
118
  return structuredClone(query);
113
119
  } catch (error) {
@@ -34,21 +34,27 @@ const config = {
34
34
  }
35
35
  };
36
36
  const queryCache = {};
37
+ const buildCacheKey = (modelUid, parentsModelUids) => {
38
+ if (parentsModelUids.length === 0) {
39
+ return modelUid;
40
+ }
41
+ return `${parentsModelUids.join("|")}|${modelUid}`;
42
+ };
37
43
  const getPopulateQuery = (modelUid, parentsModelUids = []) => {
38
44
  try {
39
45
  const useCache = strapi.plugin("populate-all").config("cache") ?? true;
40
- if (useCache && queryCache[modelUid]) {
41
- strapi.log.debug(`[populate-all] query cache hit: ${modelUid}`);
42
- return structuredClone(queryCache[modelUid]);
46
+ const cacheKey = buildCacheKey(modelUid, parentsModelUids);
47
+ if (useCache && queryCache[cacheKey]) {
48
+ strapi.log.debug(`[populate-all] query cache hit: ${cacheKey}`);
49
+ return structuredClone(queryCache[cacheKey]);
43
50
  }
44
51
  if (parentsModelUids.includes(modelUid)) {
45
52
  strapi.log.debug(
46
53
  `[populate-all] loop detected skipping population: ${modelUid}`
47
54
  );
48
55
  return { populate: {} };
49
- } else {
50
- parentsModelUids.push(modelUid);
51
56
  }
57
+ const nextParentsModelUids = [...parentsModelUids, modelUid];
52
58
  const query = { populate: {} };
53
59
  const model = strapi.getModel(modelUid);
54
60
  for (const [fieldName, attribute] of Object.entries(
@@ -62,7 +68,7 @@ const getPopulateQuery = (modelUid, parentsModelUids = []) => {
62
68
  const components = Object.fromEntries(
63
69
  attribute.components.map((component) => [
64
70
  component,
65
- getPopulateQuery(component, parentsModelUids)
71
+ getPopulateQuery(component, nextParentsModelUids)
66
72
  ])
67
73
  );
68
74
  query.populate[fieldName] = { on: components };
@@ -71,7 +77,7 @@ const getPopulateQuery = (modelUid, parentsModelUids = []) => {
71
77
  if (attribute.type === "component") {
72
78
  query.populate[fieldName] = getPopulateQuery(
73
79
  attribute.component,
74
- parentsModelUids
80
+ nextParentsModelUids
75
81
  );
76
82
  continue;
77
83
  }
@@ -84,7 +90,7 @@ const getPopulateQuery = (modelUid, parentsModelUids = []) => {
84
90
  query.populate[fieldName] = getPopulateQuery(
85
91
  // @ts-expect-error target actually exists on attribute
86
92
  attribute.target,
87
- parentsModelUids
93
+ nextParentsModelUids
88
94
  );
89
95
  continue;
90
96
  }
@@ -92,7 +98,7 @@ const getPopulateQuery = (modelUid, parentsModelUids = []) => {
92
98
  query.populate[fieldName] = getPopulateQuery(
93
99
  // @ts-expect-error target actually exists on attribute
94
100
  attribute.target,
95
- parentsModelUids
101
+ nextParentsModelUids
96
102
  );
97
103
  continue;
98
104
  }
@@ -105,8 +111,8 @@ const getPopulateQuery = (modelUid, parentsModelUids = []) => {
105
111
  query.populate = true;
106
112
  }
107
113
  if (useCache) {
108
- strapi.log.debug(`[populate-all] new query cached: ${modelUid}`);
109
- queryCache[modelUid] = query;
114
+ strapi.log.debug(`[populate-all] new query cached: ${cacheKey}`);
115
+ queryCache[cacheKey] = query;
110
116
  }
111
117
  return structuredClone(query);
112
118
  } catch (error) {
@@ -1,5 +1,5 @@
1
1
  import type { UID } from "@strapi/strapi";
2
- export declare const queryCache: Partial<Record<UID.Schema, any>>;
2
+ export declare const queryCache: Record<string, any>;
3
3
  export declare const getPopulateQuery: (modelUid: UID.Schema, parentsModelUids?: UID.Schema[]) => {
4
4
  populate: object | true;
5
5
  } | undefined;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.5.0",
2
+ "version": "1.5.1-beta.1",
3
3
  "name": "strapi-plugin-populate-all",
4
4
  "description": "A lightweight plugin to recursively populate nested data in RESTful API requests",
5
5
  "keywords": [],