@fourlights/strapi-plugin-deep-populate 1.1.2 → 1.2.2

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.
package/README.md CHANGED
@@ -1,65 +1,80 @@
1
- # @fourlights/strapi-plugin-deep-populate
1
+ # Strapi v5 Deep Populate Plugin
2
2
 
3
3
  [![npm version](https://badge.fury.io/js/@fourlights%2Fstrapi-plugin-deep-populate.svg)](https://badge.fury.io/js/@fourlights%2Fstrapi-plugin-deep-populate)
4
4
 
5
- This Strapi v5 plugin provides a simple way of retrieving all nested objects in a single request.
6
- It does this by traversing the schema and comparing that to the actual retrieved document(s).
7
- Only relations that are actually set will be populated.
5
+ A Strapi v5 plugin that automatically populates all nested relations in a single request using `populate: '*'`.
6
+ It does not impose a limit on the level of nesting and can cache the populate object for improved performance.
8
7
 
9
- ## Usage
8
+ ## Features
10
9
 
11
- ```ts
12
- // Option 1: get the populate object and use where you see fit
13
- const populate = await strapi.plugin("deep-populate").service("populate").get({ documentId: 'xyz', contentType: 'api::page.page' })
14
- const document = strapi.documents('api::page.page').findOne({ documentId: 'xyz', populate })
15
- ```
16
- ```ts
17
- // Option 2: use the `findOne` method that wraps around documentService.findOne
18
- const { findOne } = strapi.plugin("deep-populate").service("populate").documents("api::page.page")
19
- const document = await findOne({ documentId: 'xyz' })
20
- ```
21
-
22
- ```ts
23
- // Using the wrapped FindOne provides some handy features:
10
+ - Automatically resolves and populates all nested relations
11
+ - Supports all relation types including dynamic zones
12
+ - Handles circular references and edge cases
13
+ - Includes caching for improved performance
14
+ - Honors `populateCreatorFields` setting
24
15
 
25
- // Allow you to override the populate this way:
26
- const documentWithCreatedBy = findOne({ documentId: 'xyz', populate: ['createdBy']})
27
- const documentWithoutSection = findOne({ documentId: 'xyz', populate: { section: false }})
16
+ ## Installation
28
17
 
29
- // And if you supply a `*` as populate, it will return a fully populated document (i.e. non-sparse)
30
- const sparseDocument = findOne({ documentId: 'xyz' }) // sparse, so only attributes are returned that have a value
31
- const fullDocument = findOne({ documentId: 'xyz', populate: '*' }) // fully populated, so all attributes are returned
18
+ ```bash
19
+ npm install @fourlights/strapi-plugin-deep-populate
32
20
  ```
33
21
 
34
- ### populateCreatorFields
35
-
36
- The plugin honors the `populateCreatorFields`<a href="#fn1"><sup>[1]</sup></a> parameter at the content-type level. When set to true, the `createdBy` and `updatedBy` fields will be populated automatically.
22
+ ## Usage
37
23
 
38
- <sup id="fn1">[1]</sup>: https://docs.strapi.io/dev-docs/api/rest/guides/populate-creator-fields
24
+ Enable deep population in your Strapi config:
39
25
 
26
+ ```js
27
+ // config/plugins.js
28
+ module.exports = ({ env }) => ({
29
+ 'deep-populate': {
30
+ augmentPopulateStar: true, // default
31
+ cachePopulate: true // default
32
+ }
33
+ });
34
+ ```
40
35
 
41
- ---
36
+ ### Basic Usage
42
37
 
43
- ## When should you use this?
38
+ ```ts
39
+ // Get fully populated document
40
+ const document = await strapi.documents("api.page.page").findOne({
41
+ documentId: 'xyz',
42
+ populate: '*'
43
+ });
44
+ ```
44
45
 
45
- There are multiple arguments on why Strapi does not populate nested relations itself and requires you to explicitly state the population scheme:
46
+ ### Advanced Usage
46
47
 
47
- - it's quicker and consumes less resources
48
- - you don't accidentally expose too much data
49
- - ... (add your own here, there are probably more)
48
+ ```ts
49
+ // Get populate object for custom usage
50
+ const { populate } = await strapi.plugin("deep-populate")
51
+ .service("populate")
52
+ .get({
53
+ documentId: 'xyz',
54
+ contentType: 'api::page.page',
55
+ omitEmpty: true // optional
56
+ });
57
+
58
+ const document = await strapi.documents('api::page.page').findOne({
59
+ documentId: 'xyz',
60
+ populate
61
+ });
62
+ ```
50
63
 
51
- However, in some data schemes (see the playground for an example), you don't know beforehand the relations that are going to be nested. This then requires you to implement some form of recursion to get the nested relations. That's where this plugin can be used so you don't have to think about it.
64
+ ### Caching
52
65
 
53
- ## How does it work
66
+ The plugin caches populate objects to improve performance. Cache can be disabled via the `cachePopulate` setting.
54
67
 
55
- In short: it recursively resolves the content type schema for each attribute and queries the database to see if there is any value set.
68
+ ### Creator Fields
56
69
 
57
- A bit longer: You provide it with a documentId and a content type. It then recursively traverses the schema, retrieves the actual document with one-level deep populate and keeps doing this until all relations are resolved. Then it returns the full populate object that you can use to retrieve all relations in one go for that specific document.
58
- It takes care of some edge cases (e.g. circular relations) and works for all types of relations, most notably the dynamic zone (which has a different format to populate in Strapi).
70
+ The plugin automatically populates `createdBy` and `updatedBy` fields when `populateCreatorFields` is enabled in the content-type configuration.
59
71
 
60
- ## Planned features
72
+ ## How It Works
61
73
 
62
- The following features are planned:
74
+ The plugin recursively:
75
+ 1. Traverses the content-type schema
76
+ 2. Retrieves documents with one-level deep populate
77
+ 3. Resolves all nested relations
78
+ 4. Returns a complete populate object
63
79
 
64
- - Cache the populate object for a documentId/content-type combo and its latest changes to prevent having to parse the schema every time
65
- - Invalidate said cache using db lifecycle hooks
80
+ This process handles all relation types including dynamic zones and circular references.