@fourlights/strapi-plugin-deep-populate 1.0.0-beta.0
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/LICENSE.md +21 -0
- package/README.md +55 -0
- package/dist/server/index.js +25233 -0
- package/dist/server/index.mjs +25221 -0
- package/dist/server/src/index.d.ts +23 -0
- package/dist/server/src/services/deep-populate/index.d.ts +3 -0
- package/dist/server/src/services/deep-populate/utils.d.ts +4 -0
- package/dist/server/src/services/index.d.ts +21 -0
- package/dist/server/src/services/populate.d.ts +20 -0
- package/package.json +80 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Four Lights
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# @fourlights/strapi-plugin-deep-populate
|
|
2
|
+
|
|
3
|
+
This Strapi v5 plugin provides a simple way of retrieving all nested objects in a single request.
|
|
4
|
+
It does this by traversing the schema and comparing that to the actual retrieved document(s).
|
|
5
|
+
Only relations that are actually set will be populated.
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
// Option 1: get the populate object and use where you see fit
|
|
11
|
+
const populate = await strapi.plugin("deep-populate").service("populate").getPopulate({ documentId: 'xyz', contentType: 'api::page.page' })
|
|
12
|
+
const document = strapi.documents('api::page.page').findOne({ documentId: 'xyz', populate })
|
|
13
|
+
```
|
|
14
|
+
```ts
|
|
15
|
+
// Option 2: use the wrapped `findOne` method around documentService
|
|
16
|
+
const { findOne } = strapi.plugin("deep-populate").service("populate").documents("api::page.page")
|
|
17
|
+
const document = await findOne({ documentId: 'xyz' })
|
|
18
|
+
|
|
19
|
+
// You can also override the populate this way:
|
|
20
|
+
const documentWithCreatedBy = findOne({ documentId: 'xyz', populate: ['createdBy']})
|
|
21
|
+
const documentWithoutSection = findOne({ documentId: 'xyz', populate: { section: false }})
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### populateCreatorFields
|
|
25
|
+
|
|
26
|
+
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.
|
|
27
|
+
|
|
28
|
+
<sup id="fn1">[1]</sup>: https://docs.strapi.io/dev-docs/api/rest/guides/populate-creator-fields
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## When should you use this?
|
|
34
|
+
|
|
35
|
+
There are multiple arguments on why Strapi does not populate nested relations itself and requires you to explicitly state the population scheme:
|
|
36
|
+
|
|
37
|
+
- it's quicker and consumes less resources
|
|
38
|
+
- you don't accidentally expose too much data
|
|
39
|
+
- ... (add your own here, there are probably more)
|
|
40
|
+
|
|
41
|
+
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.
|
|
42
|
+
|
|
43
|
+
## How does it work
|
|
44
|
+
|
|
45
|
+
In short: it recursively resolves the content type schema for each attribute and queries the database to see if there is any value set.
|
|
46
|
+
|
|
47
|
+
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.
|
|
48
|
+
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).
|
|
49
|
+
|
|
50
|
+
## Planned features
|
|
51
|
+
|
|
52
|
+
The following features are planned:
|
|
53
|
+
|
|
54
|
+
- Cache the populate object for a documentId/content-type combo and its latest changes to prevent having to parse the schema every time
|
|
55
|
+
- Invalidate said cache using db lifecycle hooks
|