@strapi/core 0.0.0-next.84e5c7c3e470f80271a7a6a88a55244be1d168bd → 0.0.0-next.86f7c82a340bd1cb833f5f785b8001df5461c5a9
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.
Potentially problematic release.
This version of @strapi/core might be problematic. Click here for more details.
- package/dist/Strapi.d.ts +1 -0
- package/dist/Strapi.d.ts.map +1 -1
- package/dist/Strapi.js +20 -1
- package/dist/Strapi.js.map +1 -1
- package/dist/Strapi.mjs +20 -1
- package/dist/Strapi.mjs.map +1 -1
- package/dist/constants.d.ts +3 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +6 -0
- package/dist/constants.js.map +1 -0
- package/dist/constants.mjs +4 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/core-api/controller/index.d.ts.map +1 -1
- package/dist/core-api/controller/index.js +2 -1
- package/dist/core-api/controller/index.js.map +1 -1
- package/dist/core-api/controller/index.mjs +2 -1
- package/dist/core-api/controller/index.mjs.map +1 -1
- package/dist/core-api/controller/transform.d.ts +3 -2
- package/dist/core-api/controller/transform.d.ts.map +1 -1
- package/dist/core-api/controller/transform.js +13 -3
- package/dist/core-api/controller/transform.js.map +1 -1
- package/dist/core-api/controller/transform.mjs +13 -3
- package/dist/core-api/controller/transform.mjs.map +1 -1
- package/dist/core-api/routes/index.d.ts +4 -22
- package/dist/core-api/routes/index.d.ts.map +1 -1
- package/dist/core-api/routes/index.js +150 -8
- package/dist/core-api/routes/index.js.map +1 -1
- package/dist/core-api/routes/index.mjs +131 -8
- package/dist/core-api/routes/index.mjs.map +1 -1
- package/dist/core-api/routes/validation/attributes.d.ts +244 -0
- package/dist/core-api/routes/validation/attributes.d.ts.map +1 -0
- package/dist/core-api/routes/validation/attributes.js +560 -0
- package/dist/core-api/routes/validation/attributes.js.map +1 -0
- package/dist/core-api/routes/validation/attributes.mjs +521 -0
- package/dist/core-api/routes/validation/attributes.mjs.map +1 -0
- package/dist/core-api/routes/validation/common.d.ts +105 -0
- package/dist/core-api/routes/validation/common.d.ts.map +1 -0
- package/dist/core-api/routes/validation/common.js +116 -0
- package/dist/core-api/routes/validation/common.js.map +1 -0
- package/dist/core-api/routes/validation/common.mjs +95 -0
- package/dist/core-api/routes/validation/common.mjs.map +1 -0
- package/dist/core-api/routes/validation/component.d.ts +34 -0
- package/dist/core-api/routes/validation/component.d.ts.map +1 -0
- package/dist/core-api/routes/validation/component.js +45 -0
- package/dist/core-api/routes/validation/component.js.map +1 -0
- package/dist/core-api/routes/validation/component.mjs +43 -0
- package/dist/core-api/routes/validation/component.mjs.map +1 -0
- package/dist/core-api/routes/validation/constants.d.ts +8 -0
- package/dist/core-api/routes/validation/constants.d.ts.map +1 -0
- package/dist/core-api/routes/validation/constants.js +18 -0
- package/dist/core-api/routes/validation/constants.js.map +1 -0
- package/dist/core-api/routes/validation/constants.mjs +16 -0
- package/dist/core-api/routes/validation/constants.mjs.map +1 -0
- package/dist/core-api/routes/validation/content-type.d.ts +128 -0
- package/dist/core-api/routes/validation/content-type.d.ts.map +1 -0
- package/dist/core-api/routes/validation/content-type.js +201 -0
- package/dist/core-api/routes/validation/content-type.js.map +1 -0
- package/dist/core-api/routes/validation/content-type.mjs +180 -0
- package/dist/core-api/routes/validation/content-type.mjs.map +1 -0
- package/dist/core-api/routes/validation/index.d.ts +5 -0
- package/dist/core-api/routes/validation/index.d.ts.map +1 -0
- package/dist/core-api/routes/validation/mappers.d.ts +105 -0
- package/dist/core-api/routes/validation/mappers.d.ts.map +1 -0
- package/dist/core-api/routes/validation/mappers.js +273 -0
- package/dist/core-api/routes/validation/mappers.js.map +1 -0
- package/dist/core-api/routes/validation/mappers.mjs +249 -0
- package/dist/core-api/routes/validation/mappers.mjs.map +1 -0
- package/dist/core-api/routes/validation/utils.d.ts +47 -0
- package/dist/core-api/routes/validation/utils.d.ts.map +1 -0
- package/dist/core-api/routes/validation/utils.js +128 -0
- package/dist/core-api/routes/validation/utils.js.map +1 -0
- package/dist/core-api/routes/validation/utils.mjs +106 -0
- package/dist/core-api/routes/validation/utils.mjs.map +1 -0
- package/dist/domain/content-type/index.d.ts.map +1 -1
- package/dist/domain/content-type/index.js +17 -1
- package/dist/domain/content-type/index.js.map +1 -1
- package/dist/domain/content-type/index.mjs +17 -1
- package/dist/domain/content-type/index.mjs.map +1 -1
- package/dist/domain/module/index.d.ts.map +1 -1
- package/dist/domain/module/index.js +3 -0
- package/dist/domain/module/index.js.map +1 -1
- package/dist/domain/module/index.mjs +3 -0
- package/dist/domain/module/index.mjs.map +1 -1
- package/dist/factories.d.ts +3 -1
- package/dist/factories.d.ts.map +1 -1
- package/dist/factories.js +10 -2
- package/dist/factories.js.map +1 -1
- package/dist/factories.mjs +10 -3
- package/dist/factories.mjs.map +1 -1
- package/dist/loaders/plugins/index.js +1 -1
- package/dist/loaders/plugins/index.js.map +1 -1
- package/dist/loaders/plugins/index.mjs +1 -1
- package/dist/loaders/plugins/index.mjs.map +1 -1
- package/dist/middlewares/cors.d.ts +9 -1
- package/dist/middlewares/cors.d.ts.map +1 -1
- package/dist/middlewares/cors.js +39 -17
- package/dist/middlewares/cors.js.map +1 -1
- package/dist/middlewares/cors.mjs +39 -18
- package/dist/middlewares/cors.mjs.map +1 -1
- package/dist/migrations/first-published-at.d.ts +4 -0
- package/dist/migrations/first-published-at.d.ts.map +1 -0
- package/dist/migrations/first-published-at.js +51 -0
- package/dist/migrations/first-published-at.js.map +1 -0
- package/dist/migrations/first-published-at.mjs +49 -0
- package/dist/migrations/first-published-at.mjs.map +1 -0
- package/dist/migrations/index.d.ts.map +1 -1
- package/dist/migrations/index.js +5 -0
- package/dist/migrations/index.js.map +1 -1
- package/dist/migrations/index.mjs +5 -0
- package/dist/migrations/index.mjs.map +1 -1
- package/dist/package.json.js +17 -12
- package/dist/package.json.js.map +1 -1
- package/dist/package.json.mjs +17 -12
- package/dist/package.json.mjs.map +1 -1
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +2 -0
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/index.mjs +2 -0
- package/dist/providers/index.mjs.map +1 -1
- package/dist/providers/session-manager.d.ts +3 -0
- package/dist/providers/session-manager.d.ts.map +1 -0
- package/dist/providers/session-manager.js +23 -0
- package/dist/providers/session-manager.js.map +1 -0
- package/dist/providers/session-manager.mjs +21 -0
- package/dist/providers/session-manager.mjs.map +1 -0
- package/dist/services/content-api/index.d.ts +1 -1
- package/dist/services/content-api/index.d.ts.map +1 -1
- package/dist/services/content-api/index.js +1 -1
- package/dist/services/content-api/index.js.map +1 -1
- package/dist/services/content-api/index.mjs +2 -2
- package/dist/services/content-api/index.mjs.map +1 -1
- package/dist/services/content-source-maps.d.ts +13 -0
- package/dist/services/content-source-maps.d.ts.map +1 -0
- package/dist/services/content-source-maps.js +108 -0
- package/dist/services/content-source-maps.js.map +1 -0
- package/dist/services/content-source-maps.mjs +106 -0
- package/dist/services/content-source-maps.mjs.map +1 -0
- package/dist/services/document-service/components.d.ts +31 -1
- package/dist/services/document-service/components.d.ts.map +1 -1
- package/dist/services/document-service/components.js +109 -0
- package/dist/services/document-service/components.js.map +1 -1
- package/dist/services/document-service/components.mjs +107 -1
- package/dist/services/document-service/components.mjs.map +1 -1
- package/dist/services/document-service/entries.d.ts.map +1 -1
- package/dist/services/document-service/entries.js.map +1 -1
- package/dist/services/document-service/entries.mjs.map +1 -1
- package/dist/services/document-service/first-published-at.d.ts +7 -0
- package/dist/services/document-service/first-published-at.d.ts.map +1 -0
- package/dist/services/document-service/first-published-at.js +31 -0
- package/dist/services/document-service/first-published-at.js.map +1 -0
- package/dist/services/document-service/first-published-at.mjs +28 -0
- package/dist/services/document-service/first-published-at.mjs.map +1 -0
- package/dist/services/document-service/internationalization.d.ts +6 -1
- package/dist/services/document-service/internationalization.d.ts.map +1 -1
- package/dist/services/document-service/internationalization.js +32 -0
- package/dist/services/document-service/internationalization.js.map +1 -1
- package/dist/services/document-service/internationalization.mjs +32 -1
- package/dist/services/document-service/internationalization.mjs.map +1 -1
- package/dist/services/document-service/repository.d.ts.map +1 -1
- package/dist/services/document-service/repository.js +16 -8
- package/dist/services/document-service/repository.js.map +1 -1
- package/dist/services/document-service/repository.mjs +18 -10
- package/dist/services/document-service/repository.mjs.map +1 -1
- package/dist/services/document-service/utils/clean-component-join-table.d.ts +7 -0
- package/dist/services/document-service/utils/clean-component-join-table.d.ts.map +1 -0
- package/dist/services/document-service/utils/clean-component-join-table.js +138 -0
- package/dist/services/document-service/utils/clean-component-join-table.js.map +1 -0
- package/dist/services/document-service/utils/clean-component-join-table.mjs +136 -0
- package/dist/services/document-service/utils/clean-component-join-table.mjs.map +1 -0
- package/dist/services/document-service/utils/unidirectional-relations.d.ts +19 -2
- package/dist/services/document-service/utils/unidirectional-relations.d.ts.map +1 -1
- package/dist/services/document-service/utils/unidirectional-relations.js +21 -6
- package/dist/services/document-service/utils/unidirectional-relations.js.map +1 -1
- package/dist/services/document-service/utils/unidirectional-relations.mjs +21 -6
- package/dist/services/document-service/utils/unidirectional-relations.mjs.map +1 -1
- package/dist/services/entity-validator/index.d.ts.map +1 -1
- package/dist/services/entity-validator/index.js +9 -0
- package/dist/services/entity-validator/index.js.map +1 -1
- package/dist/services/entity-validator/index.mjs +9 -0
- package/dist/services/entity-validator/index.mjs.map +1 -1
- package/dist/services/entity-validator/validators.d.ts +1 -0
- package/dist/services/entity-validator/validators.d.ts.map +1 -1
- package/dist/services/entity-validator/validators.js +3 -0
- package/dist/services/entity-validator/validators.js.map +1 -1
- package/dist/services/entity-validator/validators.mjs +3 -0
- package/dist/services/entity-validator/validators.mjs.map +1 -1
- package/dist/services/metrics/index.d.ts +1 -1
- package/dist/services/metrics/index.d.ts.map +1 -1
- package/dist/services/metrics/index.js +11 -9
- package/dist/services/metrics/index.js.map +1 -1
- package/dist/services/metrics/index.mjs +11 -9
- package/dist/services/metrics/index.mjs.map +1 -1
- package/dist/services/metrics/middleware.d.ts +2 -1
- package/dist/services/metrics/middleware.d.ts.map +1 -1
- package/dist/services/metrics/middleware.js +2 -2
- package/dist/services/metrics/middleware.js.map +1 -1
- package/dist/services/metrics/middleware.mjs +2 -2
- package/dist/services/metrics/middleware.mjs.map +1 -1
- package/dist/services/metrics/sender.d.ts.map +1 -1
- package/dist/services/metrics/sender.js +2 -2
- package/dist/services/metrics/sender.js.map +1 -1
- package/dist/services/metrics/sender.mjs +2 -2
- package/dist/services/metrics/sender.mjs.map +1 -1
- package/dist/services/server/register-routes.js +22 -2
- package/dist/services/server/register-routes.js.map +1 -1
- package/dist/services/server/register-routes.mjs +22 -2
- package/dist/services/server/register-routes.mjs.map +1 -1
- package/dist/services/server/routing.d.ts +10 -0
- package/dist/services/server/routing.d.ts.map +1 -1
- package/dist/services/server/routing.js +7 -1
- package/dist/services/server/routing.js.map +1 -1
- package/dist/services/server/routing.mjs +7 -1
- package/dist/services/server/routing.mjs.map +1 -1
- package/dist/services/session-manager.d.ts +160 -0
- package/dist/services/session-manager.d.ts.map +1 -0
- package/dist/services/session-manager.js +476 -0
- package/dist/services/session-manager.js.map +1 -0
- package/dist/services/session-manager.mjs +473 -0
- package/dist/services/session-manager.mjs.map +1 -0
- package/dist/services/utils/conditional-fields.d.ts +3 -0
- package/dist/services/utils/conditional-fields.d.ts.map +1 -0
- package/dist/services/utils/conditional-fields.js +22 -0
- package/dist/services/utils/conditional-fields.js.map +1 -0
- package/dist/services/utils/conditional-fields.mjs +20 -0
- package/dist/services/utils/conditional-fields.mjs.map +1 -0
- package/dist/utils/transform-content-types-to-models.d.ts +197 -0
- package/dist/utils/transform-content-types-to-models.d.ts.map +1 -1
- package/package.json +17 -12
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var components = require('../components.js');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Cleans ghost relations with publication state mismatches from a join table
|
|
7
|
+
* Uses schema-based draft/publish checking like prevention fix
|
|
8
|
+
*/ const cleanComponentJoinTable = async (db, joinTableName, relation, sourceModel)=>{
|
|
9
|
+
try {
|
|
10
|
+
// Get the target model metadata
|
|
11
|
+
const targetModel = db.metadata.get(relation.target);
|
|
12
|
+
if (!targetModel) {
|
|
13
|
+
db.logger.debug(`Target model ${relation.target} not found, skipping ${joinTableName}`);
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
// Check if target supports draft/publish using schema-based approach (like prevention fix)
|
|
17
|
+
const targetContentType = strapi.contentTypes[relation.target];
|
|
18
|
+
const targetSupportsDraftPublish = targetContentType?.options?.draftAndPublish || false;
|
|
19
|
+
if (!targetSupportsDraftPublish) {
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
// Find entries with publication state mismatches
|
|
23
|
+
const ghostEntries = await findPublicationStateMismatches(db, joinTableName, relation, targetModel, sourceModel);
|
|
24
|
+
if (ghostEntries.length === 0) {
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
// Remove ghost entries
|
|
28
|
+
await db.connection(joinTableName).whereIn('id', ghostEntries).del();
|
|
29
|
+
db.logger.debug(`Removed ${ghostEntries.length} ghost relations with publication state mismatches from ${joinTableName}`);
|
|
30
|
+
return ghostEntries.length;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
33
|
+
db.logger.error(`Failed to clean join table "${joinTableName}": ${errorMessage}`);
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const findContentTypeParentForComponentInstance = async (componentSchema, componentId)=>{
|
|
38
|
+
// Get the parent schemas that could contain this component
|
|
39
|
+
const parentSchemas = components.getParentSchemasForComponent(componentSchema);
|
|
40
|
+
if (parentSchemas.length === 0) {
|
|
41
|
+
// No potential parents
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
// Find the actual parent for THIS specific component instance
|
|
45
|
+
const parent = await components.findComponentParent(componentSchema, componentId, parentSchemas);
|
|
46
|
+
if (!parent) {
|
|
47
|
+
// No parent found for this component instance
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
if (strapi.components[parent.uid]) {
|
|
51
|
+
// If the parent is a component, we need to check its parents recursively
|
|
52
|
+
const parentComponentSchema = strapi.components[parent.uid];
|
|
53
|
+
return findContentTypeParentForComponentInstance(parentComponentSchema, parent.parentId);
|
|
54
|
+
}
|
|
55
|
+
if (strapi.contentTypes[parent.uid]) {
|
|
56
|
+
// Found a content type parent
|
|
57
|
+
return parent;
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Finds join table entries with publication state mismatches
|
|
63
|
+
* Uses existing component parent detection from document service
|
|
64
|
+
*/ const findPublicationStateMismatches = async (db, joinTableName, relation, targetModel, sourceModel)=>{
|
|
65
|
+
try {
|
|
66
|
+
// Get join column names using proper functions (addressing PR feedback)
|
|
67
|
+
const sourceColumn = relation.joinTable.joinColumn.name;
|
|
68
|
+
const targetColumn = relation.joinTable.inverseJoinColumn.name;
|
|
69
|
+
// Get all join entries with their target entities
|
|
70
|
+
const query = db.connection(joinTableName).select(`${joinTableName}.id as join_id`, `${joinTableName}.${sourceColumn} as source_id`, `${joinTableName}.${targetColumn} as target_id`, `${targetModel.tableName}.published_at as target_published_at`).leftJoin(targetModel.tableName, `${joinTableName}.${targetColumn}`, `${targetModel.tableName}.id`);
|
|
71
|
+
const joinEntries = await query;
|
|
72
|
+
// Group by source_id to find duplicates pointing to draft/published versions of same entity
|
|
73
|
+
const entriesBySource = {};
|
|
74
|
+
for (const entry of joinEntries){
|
|
75
|
+
const sourceId = entry.source_id;
|
|
76
|
+
if (!entriesBySource[sourceId]) {
|
|
77
|
+
entriesBySource[sourceId] = [];
|
|
78
|
+
}
|
|
79
|
+
entriesBySource[sourceId].push(entry);
|
|
80
|
+
}
|
|
81
|
+
const ghostEntries = [];
|
|
82
|
+
// Check if this is a join table (ends with _lnk)
|
|
83
|
+
const isRelationJoinTable = joinTableName.endsWith('_lnk');
|
|
84
|
+
const isComponentModel = !sourceModel.uid?.startsWith('api::') && !sourceModel.uid?.startsWith('plugin::') && sourceModel.uid?.includes('.');
|
|
85
|
+
// Check for draft/publish inconsistencies
|
|
86
|
+
for (const [sourceId, entries] of Object.entries(entriesBySource)){
|
|
87
|
+
// Skip entries with single relations
|
|
88
|
+
if (entries.length <= 1) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
// For component join tables, check if THIS specific component instance's parent supports D&P
|
|
92
|
+
if (isRelationJoinTable && isComponentModel) {
|
|
93
|
+
try {
|
|
94
|
+
const componentSchema = strapi.components[sourceModel.uid];
|
|
95
|
+
if (!componentSchema) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const parent = await findContentTypeParentForComponentInstance(componentSchema, sourceId);
|
|
99
|
+
if (!parent) {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
// Check if THIS component instance's parent supports draft/publish
|
|
103
|
+
const parentContentType = strapi.contentTypes[parent.uid];
|
|
104
|
+
if (!parentContentType?.options?.draftAndPublish) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
// If we reach here, this component instance's parent DOES support D&P
|
|
108
|
+
// Continue to process this component instance for ghost relations
|
|
109
|
+
} catch (error) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// Find ghost relations (same logic as original but with improved parent checking)
|
|
114
|
+
for (const entry of entries){
|
|
115
|
+
if (entry.target_published_at === null) {
|
|
116
|
+
// This is a draft target - find its published version
|
|
117
|
+
const draftTarget = await db.connection(targetModel.tableName).select('document_id').where('id', entry.target_id).first();
|
|
118
|
+
if (draftTarget) {
|
|
119
|
+
const publishedVersion = await db.connection(targetModel.tableName).select('id', 'document_id').where('document_id', draftTarget.document_id).whereNotNull('published_at').first();
|
|
120
|
+
if (publishedVersion) {
|
|
121
|
+
// Check if we also have a relation to the published version
|
|
122
|
+
const publishedRelation = entries.find((e)=>e.target_id === publishedVersion.id);
|
|
123
|
+
if (publishedRelation) {
|
|
124
|
+
ghostEntries.push(publishedRelation.join_id);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return ghostEntries;
|
|
132
|
+
} catch (error) {
|
|
133
|
+
return [];
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
exports.cleanComponentJoinTable = cleanComponentJoinTable;
|
|
138
|
+
//# sourceMappingURL=clean-component-join-table.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clean-component-join-table.js","sources":["../../../../src/services/document-service/utils/clean-component-join-table.ts"],"sourcesContent":["import type { Database } from '@strapi/database';\nimport type { Schema } from '@strapi/types';\nimport { findComponentParent, getParentSchemasForComponent } from '../components';\n\n/**\n * Cleans ghost relations with publication state mismatches from a join table\n * Uses schema-based draft/publish checking like prevention fix\n */\nexport const cleanComponentJoinTable = async (\n db: Database,\n joinTableName: string,\n relation: any,\n sourceModel: any\n): Promise<number> => {\n try {\n // Get the target model metadata\n const targetModel = db.metadata.get(relation.target);\n if (!targetModel) {\n db.logger.debug(`Target model ${relation.target} not found, skipping ${joinTableName}`);\n return 0;\n }\n\n // Check if target supports draft/publish using schema-based approach (like prevention fix)\n const targetContentType =\n strapi.contentTypes[relation.target as keyof typeof strapi.contentTypes];\n const targetSupportsDraftPublish = targetContentType?.options?.draftAndPublish || false;\n\n if (!targetSupportsDraftPublish) {\n return 0;\n }\n\n // Find entries with publication state mismatches\n const ghostEntries = await findPublicationStateMismatches(\n db,\n joinTableName,\n relation,\n targetModel,\n sourceModel\n );\n\n if (ghostEntries.length === 0) {\n return 0;\n }\n\n // Remove ghost entries\n await db.connection(joinTableName).whereIn('id', ghostEntries).del();\n db.logger.debug(\n `Removed ${ghostEntries.length} ghost relations with publication state mismatches from ${joinTableName}`\n );\n\n return ghostEntries.length;\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n db.logger.error(`Failed to clean join table \"${joinTableName}\": ${errorMessage}`);\n return 0;\n }\n};\n\nconst findContentTypeParentForComponentInstance = async (\n componentSchema: Schema.Component,\n componentId: number | string\n): Promise<{ uid: string; table: string; parentId: number | string } | null> => {\n // Get the parent schemas that could contain this component\n const parentSchemas = getParentSchemasForComponent(componentSchema);\n if (parentSchemas.length === 0) {\n // No potential parents\n return null;\n }\n\n // Find the actual parent for THIS specific component instance\n const parent = await findComponentParent(componentSchema, componentId, parentSchemas);\n if (!parent) {\n // No parent found for this component instance\n return null;\n }\n\n if (strapi.components[parent.uid as keyof typeof strapi.components]) {\n // If the parent is a component, we need to check its parents recursively\n const parentComponentSchema = strapi.components[parent.uid as keyof typeof strapi.components];\n return findContentTypeParentForComponentInstance(parentComponentSchema, parent.parentId);\n }\n\n if (strapi.contentTypes[parent.uid as keyof typeof strapi.contentTypes]) {\n // Found a content type parent\n return parent;\n }\n\n return null;\n};\n\n/**\n * Finds join table entries with publication state mismatches\n * Uses existing component parent detection from document service\n */\nconst findPublicationStateMismatches = async (\n db: Database,\n joinTableName: string,\n relation: any,\n targetModel: any,\n sourceModel: any\n): Promise<number[]> => {\n try {\n // Get join column names using proper functions (addressing PR feedback)\n const sourceColumn = relation.joinTable.joinColumn.name;\n const targetColumn = relation.joinTable.inverseJoinColumn.name;\n\n // Get all join entries with their target entities\n const query = db\n .connection(joinTableName)\n .select(\n `${joinTableName}.id as join_id`,\n `${joinTableName}.${sourceColumn} as source_id`,\n `${joinTableName}.${targetColumn} as target_id`,\n `${targetModel.tableName}.published_at as target_published_at`\n )\n .leftJoin(\n targetModel.tableName,\n `${joinTableName}.${targetColumn}`,\n `${targetModel.tableName}.id`\n );\n\n const joinEntries = await query;\n\n // Group by source_id to find duplicates pointing to draft/published versions of same entity\n const entriesBySource: { [key: string]: any[] } = {};\n for (const entry of joinEntries) {\n const sourceId = entry.source_id;\n if (!entriesBySource[sourceId]) {\n entriesBySource[sourceId] = [];\n }\n entriesBySource[sourceId].push(entry);\n }\n\n const ghostEntries: number[] = [];\n\n // Check if this is a join table (ends with _lnk)\n const isRelationJoinTable = joinTableName.endsWith('_lnk');\n const isComponentModel =\n !sourceModel.uid?.startsWith('api::') &&\n !sourceModel.uid?.startsWith('plugin::') &&\n sourceModel.uid?.includes('.');\n\n // Check for draft/publish inconsistencies\n for (const [sourceId, entries] of Object.entries(entriesBySource)) {\n // Skip entries with single relations\n if (entries.length <= 1) {\n // eslint-disable-next-line no-continue\n continue;\n }\n\n // For component join tables, check if THIS specific component instance's parent supports D&P\n if (isRelationJoinTable && isComponentModel) {\n try {\n const componentSchema = strapi.components[sourceModel.uid] as Schema.Component;\n if (!componentSchema) {\n // eslint-disable-next-line no-continue\n continue;\n }\n\n const parent = await findContentTypeParentForComponentInstance(componentSchema, sourceId);\n if (!parent) {\n continue;\n }\n\n // Check if THIS component instance's parent supports draft/publish\n const parentContentType =\n strapi.contentTypes[parent.uid as keyof typeof strapi.contentTypes];\n if (!parentContentType?.options?.draftAndPublish) {\n // This component instance's parent does NOT support D&P - skip cleanup\n // eslint-disable-next-line no-continue\n continue;\n }\n\n // If we reach here, this component instance's parent DOES support D&P\n // Continue to process this component instance for ghost relations\n } catch (error) {\n // Skip this component instance on error\n // eslint-disable-next-line no-continue\n continue;\n }\n }\n\n // Find ghost relations (same logic as original but with improved parent checking)\n for (const entry of entries) {\n if (entry.target_published_at === null) {\n // This is a draft target - find its published version\n const draftTarget = await db\n .connection(targetModel.tableName)\n .select('document_id')\n .where('id', entry.target_id)\n .first();\n\n if (draftTarget) {\n const publishedVersion = await db\n .connection(targetModel.tableName)\n .select('id', 'document_id')\n .where('document_id', draftTarget.document_id)\n .whereNotNull('published_at')\n .first();\n\n if (publishedVersion) {\n // Check if we also have a relation to the published version\n const publishedRelation = entries.find((e) => e.target_id === publishedVersion.id);\n if (publishedRelation) {\n ghostEntries.push(publishedRelation.join_id);\n }\n }\n }\n }\n }\n }\n\n return ghostEntries;\n } catch (error) {\n return [];\n }\n};\n"],"names":["cleanComponentJoinTable","db","joinTableName","relation","sourceModel","targetModel","metadata","get","target","logger","debug","targetContentType","strapi","contentTypes","targetSupportsDraftPublish","options","draftAndPublish","ghostEntries","findPublicationStateMismatches","length","connection","whereIn","del","error","errorMessage","Error","message","String","findContentTypeParentForComponentInstance","componentSchema","componentId","parentSchemas","getParentSchemasForComponent","parent","findComponentParent","components","uid","parentComponentSchema","parentId","sourceColumn","joinTable","joinColumn","name","targetColumn","inverseJoinColumn","query","select","tableName","leftJoin","joinEntries","entriesBySource","entry","sourceId","source_id","push","isRelationJoinTable","endsWith","isComponentModel","startsWith","includes","entries","Object","parentContentType","target_published_at","draftTarget","where","target_id","first","publishedVersion","document_id","whereNotNull","publishedRelation","find","e","id","join_id"],"mappings":";;;;AAIA;;;AAGC,IACYA,MAAAA,uBAAAA,GAA0B,OACrCC,EAAAA,EACAC,eACAC,QACAC,EAAAA,WAAAA,GAAAA;IAEA,IAAI;;AAEF,QAAA,MAAMC,cAAcJ,EAAGK,CAAAA,QAAQ,CAACC,GAAG,CAACJ,SAASK,MAAM,CAAA;AACnD,QAAA,IAAI,CAACH,WAAa,EAAA;AAChBJ,YAAAA,EAAAA,CAAGQ,MAAM,CAACC,KAAK,CAAC,CAAC,aAAa,EAAEP,QAAAA,CAASK,MAAM,CAAC,qBAAqB,EAAEN,cAAc,CAAC,CAAA;YACtF,OAAO,CAAA;AACT;;AAGA,QAAA,MAAMS,oBACJC,MAAOC,CAAAA,YAAY,CAACV,QAAAA,CAASK,MAAM,CAAqC;QAC1E,MAAMM,0BAAAA,GAA6BH,iBAAmBI,EAAAA,OAAAA,EAASC,eAAmB,IAAA,KAAA;AAElF,QAAA,IAAI,CAACF,0BAA4B,EAAA;YAC/B,OAAO,CAAA;AACT;;AAGA,QAAA,MAAMG,eAAe,MAAMC,8BAAAA,CACzBjB,EACAC,EAAAA,aAAAA,EACAC,UACAE,WACAD,EAAAA,WAAAA,CA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AAmB,KAAK,IAAM,EAAA;;AAEtC,oBAAA,MAAMC,cAAc,MAAM/D,EAAAA,CACvBmB,UAAU,CAACf,YAAY0C,SAAS,CAAA,CAChCD,MAAM,CAAC,eACPmB,KAAK,CAAC,MAAMd,KAAMe,CAAAA,SAAS,EAC3BC,KAAK,EAAA;AAER,oBAAA,IAAIH,WAAa,EAAA;wBACf,MAAMI,gBAAAA,GAAmB,MAAMnE,EAC5BmB,CAAAA,UAAU,CAACf,WAAY0C,CAAAA,SAAS,EAChCD,MAAM,CAAC,MAAM,aACbmB,CAAAA,CAAAA,KAAK,CAAC,aAAeD,EAAAA,WAAAA,CAAYK,WAAW,CAC5CC,CAAAA,YAAY,CAAC,cAAA,CAAA,CACbH,KAAK,EAAA;AAER,wBAAA,IAAIC,gBAAkB,EAAA;;4BAEpB,MAAMG,iBAAAA,GAAoBX,OAAQY,CAAAA,IAAI,CAAC,CAACC,IAAMA,CAAEP,CAAAA,SAAS,KAAKE,gBAAAA,CAAiBM,EAAE,CAAA;AACjF,4BAAA,IAAIH,iBAAmB,EAAA;gCACrBtD,YAAaqC,CAAAA,IAAI,CAACiB,iBAAAA,CAAkBI,OAAO,CAAA;AAC7C;AACF;AACF;AACF;AACF;AACF;QAEA,OAAO1D,YAAAA;AACT,KAAA,CAAE,OAAOM,KAAO,EAAA;AACd,QAAA,OAAO,EAAE;AACX;AACF,CAAA;;;;"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { getParentSchemasForComponent, findComponentParent } from '../components.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Cleans ghost relations with publication state mismatches from a join table
|
|
5
|
+
* Uses schema-based draft/publish checking like prevention fix
|
|
6
|
+
*/ const cleanComponentJoinTable = async (db, joinTableName, relation, sourceModel)=>{
|
|
7
|
+
try {
|
|
8
|
+
// Get the target model metadata
|
|
9
|
+
const targetModel = db.metadata.get(relation.target);
|
|
10
|
+
if (!targetModel) {
|
|
11
|
+
db.logger.debug(`Target model ${relation.target} not found, skipping ${joinTableName}`);
|
|
12
|
+
return 0;
|
|
13
|
+
}
|
|
14
|
+
// Check if target supports draft/publish using schema-based approach (like prevention fix)
|
|
15
|
+
const targetContentType = strapi.contentTypes[relation.target];
|
|
16
|
+
const targetSupportsDraftPublish = targetContentType?.options?.draftAndPublish || false;
|
|
17
|
+
if (!targetSupportsDraftPublish) {
|
|
18
|
+
return 0;
|
|
19
|
+
}
|
|
20
|
+
// Find entries with publication state mismatches
|
|
21
|
+
const ghostEntries = await findPublicationStateMismatches(db, joinTableName, relation, targetModel, sourceModel);
|
|
22
|
+
if (ghostEntries.length === 0) {
|
|
23
|
+
return 0;
|
|
24
|
+
}
|
|
25
|
+
// Remove ghost entries
|
|
26
|
+
await db.connection(joinTableName).whereIn('id', ghostEntries).del();
|
|
27
|
+
db.logger.debug(`Removed ${ghostEntries.length} ghost relations with publication state mismatches from ${joinTableName}`);
|
|
28
|
+
return ghostEntries.length;
|
|
29
|
+
} catch (error) {
|
|
30
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
31
|
+
db.logger.error(`Failed to clean join table "${joinTableName}": ${errorMessage}`);
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const findContentTypeParentForComponentInstance = async (componentSchema, componentId)=>{
|
|
36
|
+
// Get the parent schemas that could contain this component
|
|
37
|
+
const parentSchemas = getParentSchemasForComponent(componentSchema);
|
|
38
|
+
if (parentSchemas.length === 0) {
|
|
39
|
+
// No potential parents
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
// Find the actual parent for THIS specific component instance
|
|
43
|
+
const parent = await findComponentParent(componentSchema, componentId, parentSchemas);
|
|
44
|
+
if (!parent) {
|
|
45
|
+
// No parent found for this component instance
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
if (strapi.components[parent.uid]) {
|
|
49
|
+
// If the parent is a component, we need to check its parents recursively
|
|
50
|
+
const parentComponentSchema = strapi.components[parent.uid];
|
|
51
|
+
return findContentTypeParentForComponentInstance(parentComponentSchema, parent.parentId);
|
|
52
|
+
}
|
|
53
|
+
if (strapi.contentTypes[parent.uid]) {
|
|
54
|
+
// Found a content type parent
|
|
55
|
+
return parent;
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Finds join table entries with publication state mismatches
|
|
61
|
+
* Uses existing component parent detection from document service
|
|
62
|
+
*/ const findPublicationStateMismatches = async (db, joinTableName, relation, targetModel, sourceModel)=>{
|
|
63
|
+
try {
|
|
64
|
+
// Get join column names using proper functions (addressing PR feedback)
|
|
65
|
+
const sourceColumn = relation.joinTable.joinColumn.name;
|
|
66
|
+
const targetColumn = relation.joinTable.inverseJoinColumn.name;
|
|
67
|
+
// Get all join entries with their target entities
|
|
68
|
+
const query = db.connection(joinTableName).select(`${joinTableName}.id as join_id`, `${joinTableName}.${sourceColumn} as source_id`, `${joinTableName}.${targetColumn} as target_id`, `${targetModel.tableName}.published_at as target_published_at`).leftJoin(targetModel.tableName, `${joinTableName}.${targetColumn}`, `${targetModel.tableName}.id`);
|
|
69
|
+
const joinEntries = await query;
|
|
70
|
+
// Group by source_id to find duplicates pointing to draft/published versions of same entity
|
|
71
|
+
const entriesBySource = {};
|
|
72
|
+
for (const entry of joinEntries){
|
|
73
|
+
const sourceId = entry.source_id;
|
|
74
|
+
if (!entriesBySource[sourceId]) {
|
|
75
|
+
entriesBySource[sourceId] = [];
|
|
76
|
+
}
|
|
77
|
+
entriesBySource[sourceId].push(entry);
|
|
78
|
+
}
|
|
79
|
+
const ghostEntries = [];
|
|
80
|
+
// Check if this is a join table (ends with _lnk)
|
|
81
|
+
const isRelationJoinTable = joinTableName.endsWith('_lnk');
|
|
82
|
+
const isComponentModel = !sourceModel.uid?.startsWith('api::') && !sourceModel.uid?.startsWith('plugin::') && sourceModel.uid?.includes('.');
|
|
83
|
+
// Check for draft/publish inconsistencies
|
|
84
|
+
for (const [sourceId, entries] of Object.entries(entriesBySource)){
|
|
85
|
+
// Skip entries with single relations
|
|
86
|
+
if (entries.length <= 1) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
// For component join tables, check if THIS specific component instance's parent supports D&P
|
|
90
|
+
if (isRelationJoinTable && isComponentModel) {
|
|
91
|
+
try {
|
|
92
|
+
const componentSchema = strapi.components[sourceModel.uid];
|
|
93
|
+
if (!componentSchema) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
const parent = await findContentTypeParentForComponentInstance(componentSchema, sourceId);
|
|
97
|
+
if (!parent) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
// Check if THIS component instance's parent supports draft/publish
|
|
101
|
+
const parentContentType = strapi.contentTypes[parent.uid];
|
|
102
|
+
if (!parentContentType?.options?.draftAndPublish) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
// If we reach here, this component instance's parent DOES support D&P
|
|
106
|
+
// Continue to process this component instance for ghost relations
|
|
107
|
+
} catch (error) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// Find ghost relations (same logic as original but with improved parent checking)
|
|
112
|
+
for (const entry of entries){
|
|
113
|
+
if (entry.target_published_at === null) {
|
|
114
|
+
// This is a draft target - find its published version
|
|
115
|
+
const draftTarget = await db.connection(targetModel.tableName).select('document_id').where('id', entry.target_id).first();
|
|
116
|
+
if (draftTarget) {
|
|
117
|
+
const publishedVersion = await db.connection(targetModel.tableName).select('id', 'document_id').where('document_id', draftTarget.document_id).whereNotNull('published_at').first();
|
|
118
|
+
if (publishedVersion) {
|
|
119
|
+
// Check if we also have a relation to the published version
|
|
120
|
+
const publishedRelation = entries.find((e)=>e.target_id === publishedVersion.id);
|
|
121
|
+
if (publishedRelation) {
|
|
122
|
+
ghostEntries.push(publishedRelation.join_id);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return ghostEntries;
|
|
130
|
+
} catch (error) {
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
export { cleanComponentJoinTable };
|
|
136
|
+
//# sourceMappingURL=clean-component-join-table.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clean-component-join-table.mjs","sources":["../../../../src/services/document-service/utils/clean-component-join-table.ts"],"sourcesContent":["import type { Database } from '@strapi/database';\nimport type { Schema } from '@strapi/types';\nimport { findComponentParent, getParentSchemasForComponent } from '../components';\n\n/**\n * Cleans ghost relations with publication state mismatches from a join table\n * Uses schema-based draft/publish checking like prevention fix\n */\nexport const cleanComponentJoinTable = async (\n db: Database,\n joinTableName: string,\n relation: any,\n sourceModel: any\n): Promise<number> => {\n try {\n // Get the target model metadata\n const targetModel = db.metadata.get(relation.target);\n if (!targetModel) {\n db.logger.debug(`Target model ${relation.target} not found, skipping ${joinTableName}`);\n return 0;\n }\n\n // Check if target supports draft/publish using schema-based approach (like prevention fix)\n const targetContentType =\n strapi.contentTypes[relation.target as keyof typeof strapi.contentTypes];\n const targetSupportsDraftPublish = targetContentType?.options?.draftAndPublish || false;\n\n if (!targetSupportsDraftPublish) {\n return 0;\n }\n\n // Find entries with publication state mismatches\n const ghostEntries = await findPublicationStateMismatches(\n db,\n joinTableName,\n relation,\n targetModel,\n sourceModel\n );\n\n if (ghostEntries.length === 0) {\n return 0;\n }\n\n // Remove ghost entries\n await db.connection(joinTableName).whereIn('id', ghostEntries).del();\n db.logger.debug(\n `Removed ${ghostEntries.length} ghost relations with publication state mismatches from ${joinTableName}`\n );\n\n return ghostEntries.length;\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n db.logger.error(`Failed to clean join table \"${joinTableName}\": ${errorMessage}`);\n return 0;\n }\n};\n\nconst findContentTypeParentForComponentInstance = async (\n componentSchema: Schema.Component,\n componentId: number | string\n): Promise<{ uid: string; table: string; parentId: number | string } | null> => {\n // Get the parent schemas that could contain this component\n const parentSchemas = getParentSchemasForComponent(componentSchema);\n if (parentSchemas.length === 0) {\n // No potential parents\n return null;\n }\n\n // Find the actual parent for THIS specific component instance\n const parent = await findComponentParent(componentSchema, componentId, parentSchemas);\n if (!parent) {\n // No parent found for this component instance\n return null;\n }\n\n if (strapi.components[parent.uid as keyof typeof strapi.components]) {\n // If the parent is a component, we need to check its parents recursively\n const parentComponentSchema = strapi.components[parent.uid as keyof typeof strapi.components];\n return findContentTypeParentForComponentInstance(parentComponentSchema, parent.parentId);\n }\n\n if (strapi.contentTypes[parent.uid as keyof typeof strapi.contentTypes]) {\n // Found a content type parent\n return parent;\n }\n\n return null;\n};\n\n/**\n * Finds join table entries with publication state mismatches\n * Uses existing component parent detection from document service\n */\nconst findPublicationStateMismatches = async (\n db: Database,\n joinTableName: string,\n relation: any,\n targetModel: any,\n sourceModel: any\n): Promise<number[]> => {\n try {\n // Get join column names using proper functions (addressing PR feedback)\n const sourceColumn = relation.joinTable.joinColumn.name;\n const targetColumn = relation.joinTable.inverseJoinColumn.name;\n\n // Get all join entries with their target entities\n const query = db\n .connection(joinTableName)\n .select(\n `${joinTableName}.id as join_id`,\n `${joinTableName}.${sourceColumn} as source_id`,\n `${joinTableName}.${targetColumn} as target_id`,\n `${targetModel.tableName}.published_at as target_published_at`\n )\n .leftJoin(\n targetModel.tableName,\n `${joinTableName}.${targetColumn}`,\n `${targetModel.tableName}.id`\n );\n\n const joinEntries = await query;\n\n // Group by source_id to find duplicates pointing to draft/published versions of same entity\n const entriesBySource: { [key: string]: any[] } = {};\n for (const entry of joinEntries) {\n const sourceId = entry.source_id;\n if (!entriesBySource[sourceId]) {\n entriesBySource[sourceId] = [];\n }\n entriesBySource[sourceId].push(entry);\n }\n\n const ghostEntries: number[] = [];\n\n // Check if this is a join table (ends with _lnk)\n const isRelationJoinTable = joinTableName.endsWith('_lnk');\n const isComponentModel =\n !sourceModel.uid?.startsWith('api::') &&\n !sourceModel.uid?.startsWith('plugin::') &&\n sourceModel.uid?.includes('.');\n\n // Check for draft/publish inconsistencies\n for (const [sourceId, entries] of Object.entries(entriesBySource)) {\n // Skip entries with single relations\n if (entries.length <= 1) {\n // eslint-disable-next-line no-continue\n continue;\n }\n\n // For component join tables, check if THIS specific component instance's parent supports D&P\n if (isRelationJoinTable && isComponentModel) {\n try {\n const componentSchema = strapi.components[sourceModel.uid] as Schema.Component;\n if (!componentSchema) {\n // eslint-disable-next-line no-continue\n continue;\n }\n\n const parent = await findContentTypeParentForComponentInstance(componentSchema, sourceId);\n if (!parent) {\n continue;\n }\n\n // Check if THIS component instance's parent supports draft/publish\n const parentContentType =\n strapi.contentTypes[parent.uid as keyof typeof strapi.contentTypes];\n if (!parentContentType?.options?.draftAndPublish) {\n // This component instance's parent does NOT support D&P - skip cleanup\n // eslint-disable-next-line no-continue\n continue;\n }\n\n // If we reach here, this component instance's parent DOES support D&P\n // Continue to process this component instance for ghost relations\n } catch (error) {\n // Skip this component instance on error\n // eslint-disable-next-line no-continue\n continue;\n }\n }\n\n // Find ghost relations (same logic as original but with improved parent checking)\n for (const entry of entries) {\n if (entry.target_published_at === null) {\n // This is a draft target - find its published version\n const draftTarget = await db\n .connection(targetModel.tableName)\n .select('document_id')\n .where('id', entry.target_id)\n .first();\n\n if (draftTarget) {\n const publishedVersion = await db\n .connection(targetModel.tableName)\n .select('id', 'document_id')\n .where('document_id', draftTarget.document_id)\n .whereNotNull('published_at')\n .first();\n\n if (publishedVersion) {\n // Check if we also have a relation to the published version\n const publishedRelation = entries.find((e) => e.target_id === publishedVersion.id);\n if (publishedRelation) {\n ghostEntries.push(publishedRelation.join_id);\n }\n }\n }\n }\n }\n }\n\n return ghostEntries;\n } catch (error) {\n return [];\n }\n};\n"],"names":["cleanComponentJoinTable","db","joinTableName","relation","sourceModel","targetModel","metadata","get","target","logger","debug","targetContentType","strapi","contentTypes","targetSupportsDraftPublish","options","draftAndPublish","ghostEntries","findPublicationStateMismatches","length","connection","whereIn","del","error","errorMessage","Error","message","String","findContentTypeParentForComponentInstance","componentSchema","componentId","parentSchemas","getParentSchemasForComponent","parent","findComponentParent","components","uid","parentComponentSchema","parentId","sourceColumn","joinTable","joinColumn","name","targetColumn","inverseJoinColumn","query","select","tableName","leftJoin","joinEntries","entriesBySource","entry","sourceId","source_id","push","isRelationJoinTable","endsWith","isComponentModel","startsWith","includes","entries","Object","parentContentType","target_published_at","draftTarget","where","target_id","first","publishedVersion","document_id","whereNotNull","publishedRelation","find","e","id","join_id"],"mappings":";;AAIA;;;AAGC,IACYA,MAAAA,uBAAAA,GAA0B,OACrCC,EAAAA,EACAC,eACAC,QACAC,EAAAA,WAAAA,GAAAA;IAEA,IAAI;;AAEF,QAAA,MAAMC,cAAcJ,EAAGK,CAAAA,QAAQ,CAACC,GAAG,CAACJ,SAASK,MAAM,CAAA;AACnD,QAAA,IAAI,CAACH,WAAa,EAAA;AAChBJ,YAAAA,EAAAA,CAAGQ,MAAM,CAACC,KAAK,CAAC,CAAC,aAAa,EAAEP,QAAAA,CAASK,MAAM,CAAC,qBAAqB,EAAEN,cAAc,CAAC,CAAA;YACtF,OAAO,CAAA;AACT;;AAGA,QAAA,MAAMS,oBACJC,MAAOC,CAAAA,YAAY,CAACV,QAAAA,CAASK,MAAM,CAAqC;QAC1E,MAAMM,0BAAAA,GAA6BH,iBAAmBI,EAAAA,OAAAA,EAASC,eAAmB,IAAA,KAAA;AAElF,QAAA,IAAI,CAACF,0BAA4B,EAAA;YAC/B,OAAO,CAAA;AACT;;AAGA,QAAA,MAAMG,eAAe,MAAMC,8BAAAA,CACzBjB,EACAC,EAAAA,aAAAA,EACAC,UACAE,WACAD,EAAAA,WAAAA,CAAAA;QAGF,IAAIa,YAAAA,CAAaE,MAAM,KAAK,CAAG,EAAA;YAC7B,OAAO,CAAA;AACT;;QAGA,MAAMlB,EAAAA,CAAGmB,UAAU,CAAClB,aAAAA,CAAAA,CAAemB,OAAO,CAAC,IAAA,EA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A,SAAS,EAC3BC,KAAK,EAAA;AAER,oBAAA,IAAIH,WAAa,EAAA;wBACf,MAAMI,gBAAAA,GAAmB,MAAMnE,EAC5BmB,CAAAA,UAAU,CAACf,WAAY0C,CAAAA,SAAS,EAChCD,MAAM,CAAC,MAAM,aACbmB,CAAAA,CAAAA,KAAK,CAAC,aAAeD,EAAAA,WAAAA,CAAYK,WAAW,CAC5CC,CAAAA,YAAY,CAAC,cAAA,CAAA,CACbH,KAAK,EAAA;AAER,wBAAA,IAAIC,gBAAkB,EAAA;;4BAEpB,MAAMG,iBAAAA,GAAoBX,OAAQY,CAAAA,IAAI,CAAC,CAACC,IAAMA,CAAEP,CAAAA,SAAS,KAAKE,gBAAAA,CAAiBM,EAAE,CAAA;AACjF,4BAAA,IAAIH,iBAAmB,EAAA;gCACrBtD,YAAaqC,CAAAA,IAAI,CAACiB,iBAAAA,CAAkBI,OAAO,CAAA;AAC7C;AACF;AACF;AACF;AACF;AACF;QAEA,OAAO1D,YAAAA;AACT,KAAA,CAAE,OAAOM,KAAO,EAAA;AACd,QAAA,OAAO,EAAE;AACX;AACF,CAAA;;;;"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { UID } from '@strapi/types';
|
|
1
|
+
import type { UID, Schema } from '@strapi/types';
|
|
2
|
+
import type { JoinTable } from '@strapi/database';
|
|
2
3
|
interface LoadContext {
|
|
3
4
|
oldVersions: {
|
|
4
5
|
id: string;
|
|
@@ -9,15 +10,30 @@ interface LoadContext {
|
|
|
9
10
|
locale: string;
|
|
10
11
|
}[];
|
|
11
12
|
}
|
|
13
|
+
interface RelationUpdate {
|
|
14
|
+
joinTable: JoinTable;
|
|
15
|
+
relations: Record<string, any>[];
|
|
16
|
+
}
|
|
17
|
+
interface RelationFilterOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Function to determine if a relation should be propagated to new document versions
|
|
20
|
+
* This replaces the hardcoded component-specific logic
|
|
21
|
+
*/
|
|
22
|
+
shouldPropagateRelation?: (relation: Record<string, any>, model: Schema.Component | Schema.ContentType, trx: any) => Promise<boolean>;
|
|
23
|
+
}
|
|
12
24
|
/**
|
|
13
25
|
* Loads lingering relations that need to be updated when overriding a published or draft entry.
|
|
14
26
|
* This is necessary because the relations are uni-directional and the target entry is not aware of the source entry.
|
|
15
27
|
* This is not the case for bi-directional relations, where the target entry is also linked to the source entry.
|
|
16
28
|
*/
|
|
17
|
-
declare const load: (uid: UID.ContentType, { oldVersions, newVersions }: LoadContext) => Promise<
|
|
29
|
+
declare const load: (uid: UID.ContentType, { oldVersions, newVersions }: LoadContext, options?: RelationFilterOptions) => Promise<RelationUpdate[]>;
|
|
18
30
|
/**
|
|
19
31
|
* Updates uni directional relations to target the right entries when overriding published or draft entries.
|
|
20
32
|
*
|
|
33
|
+
* This function:
|
|
34
|
+
* 1. Creates new relations pointing to the new entry versions
|
|
35
|
+
* 2. Precisely deletes only the old relations being replaced to prevent orphaned links
|
|
36
|
+
*
|
|
21
37
|
* @param oldEntries The old entries that are being overridden
|
|
22
38
|
* @param newEntries The new entries that are overriding the old ones
|
|
23
39
|
* @param oldRelations The relations that were previously loaded with `load` @see load
|
|
@@ -33,4 +49,5 @@ declare const sync: (oldEntries: {
|
|
|
33
49
|
relations: any[];
|
|
34
50
|
}[]) => Promise<void>;
|
|
35
51
|
export { load, sync };
|
|
52
|
+
export type { RelationFilterOptions };
|
|
36
53
|
//# sourceMappingURL=unidirectional-relations.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unidirectional-relations.d.ts","sourceRoot":"","sources":["../../../../src/services/document-service/utils/unidirectional-relations.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,GAAG,
|
|
1
|
+
{"version":3,"file":"unidirectional-relations.d.ts","sourceRoot":"","sources":["../../../../src/services/document-service/utils/unidirectional-relations.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD,UAAU,WAAW;IACnB,WAAW,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAC9C,WAAW,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CAC/C;AAED,UAAU,cAAc;IACtB,SAAS,EAAE,SAAS,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC;CAClC;AAED,UAAU,qBAAqB;IAC7B;;;OAGG;IACH,uBAAuB,CAAC,EAAE,CACxB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC7B,KAAK,EAAE,MAAM,CAAC,SAAS,GAAG,MAAM,CAAC,WAAW,EAC5C,GAAG,EAAE,GAAG,KACL,OAAO,CAAC,OAAO,CAAC,CAAC;CACvB;AAED;;;;GAIG;AACH,QAAA,MAAM,IAAI,QACH,IAAI,WAAW,gCACU,WAAW,YAChC,qBAAqB,KAC7B,QAAQ,cAAc,EAAE,CAyG1B,CAAC;AAEF;;;;;;;;;;GAUG;AACH,QAAA,MAAM,IAAI,eACI;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAAE,cAChC;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAAE,gBAC9B;IAAE,SAAS,EAAE,GAAG,CAAC;IAAC,SAAS,EAAE,GAAG,EAAE,CAAA;CAAE,EAAE,kBAiCrD,CAAC;AAEF,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;AACtB,YAAY,EAAE,qBAAqB,EAAE,CAAC"}
|
|
@@ -6,7 +6,7 @@ var fp = require('lodash/fp');
|
|
|
6
6
|
* Loads lingering relations that need to be updated when overriding a published or draft entry.
|
|
7
7
|
* This is necessary because the relations are uni-directional and the target entry is not aware of the source entry.
|
|
8
8
|
* This is not the case for bi-directional relations, where the target entry is also linked to the source entry.
|
|
9
|
-
*/ const load = async (uid, { oldVersions, newVersions })=>{
|
|
9
|
+
*/ const load = async (uid, { oldVersions, newVersions }, options = {})=>{
|
|
10
10
|
const updates = [];
|
|
11
11
|
// Iterate all components and content types to find relations that need to be updated
|
|
12
12
|
await strapi.db.transaction(async ({ trx })=>{
|
|
@@ -53,17 +53,28 @@ var fp = require('lodash/fp');
|
|
|
53
53
|
* create link to new draft version
|
|
54
54
|
*/ if (!model.options?.draftAndPublish) {
|
|
55
55
|
const ids = newVersions.map((entry)=>entry.id);
|
|
56
|
+
// This is the step were we query the join table based on the id of the document
|
|
56
57
|
const newVersionsRelations = await strapi.db.getConnection().select('*').from(joinTable.name).whereIn(targetColumnName, ids).transacting(trx);
|
|
57
|
-
|
|
58
|
+
let versionRelations = newVersionsRelations;
|
|
59
|
+
if (options.shouldPropagateRelation) {
|
|
60
|
+
const relationsToPropagate = [];
|
|
61
|
+
for (const relation of newVersionsRelations){
|
|
62
|
+
if (await options.shouldPropagateRelation(relation, model, trx)) {
|
|
63
|
+
relationsToPropagate.push(relation);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
versionRelations = relationsToPropagate;
|
|
67
|
+
}
|
|
68
|
+
if (versionRelations.length > 0) {
|
|
58
69
|
// when publishing a draft that doesn't have a published version yet,
|
|
59
70
|
// copy the links to the draft over to the published version
|
|
60
71
|
// when discarding a published version, if no drafts exists
|
|
61
|
-
const discardToAdd =
|
|
62
|
-
const
|
|
72
|
+
const discardToAdd = versionRelations.filter((relation)=>{
|
|
73
|
+
const matchingOldVersion = oldVersionsRelations.find((oldRelation)=>{
|
|
63
74
|
return oldRelation[sourceColumnName] === relation[sourceColumnName];
|
|
64
75
|
});
|
|
65
|
-
return !
|
|
66
|
-
}).map(fp.omit(
|
|
76
|
+
return !matchingOldVersion;
|
|
77
|
+
}).map(fp.omit(strapi.db.metadata.identifiers.ID_COLUMN));
|
|
67
78
|
updates.push({
|
|
68
79
|
joinTable,
|
|
69
80
|
relations: discardToAdd
|
|
@@ -78,6 +89,10 @@ var fp = require('lodash/fp');
|
|
|
78
89
|
/**
|
|
79
90
|
* Updates uni directional relations to target the right entries when overriding published or draft entries.
|
|
80
91
|
*
|
|
92
|
+
* This function:
|
|
93
|
+
* 1. Creates new relations pointing to the new entry versions
|
|
94
|
+
* 2. Precisely deletes only the old relations being replaced to prevent orphaned links
|
|
95
|
+
*
|
|
81
96
|
* @param oldEntries The old entries that are being overridden
|
|
82
97
|
* @param newEntries The new entries that are overriding the old ones
|
|
83
98
|
* @param oldRelations The relations that were previously loaded with `load` @see load
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unidirectional-relations.js","sources":["../../../../src/services/document-service/utils/unidirectional-relations.ts"],"sourcesContent":["/* eslint-disable no-continue */\nimport { keyBy, omit } from 'lodash/fp';\n\nimport type { UID, Schema } from '@strapi/types';\n\ninterface LoadContext {\n oldVersions: { id: string; locale: string }[];\n newVersions: { id: string; locale: string }[];\n}\n\n/**\n * Loads lingering relations that need to be updated when overriding a published or draft entry.\n * This is necessary because the relations are uni-directional and the target entry is not aware of the source entry.\n * This is not the case for bi-directional relations, where the target entry is also linked to the source entry.\n */\nconst load = async (uid: UID.ContentType, { oldVersions, newVersions }: LoadContext) => {\n const updates = [] as any;\n\n // Iterate all components and content types to find relations that need to be updated\n await strapi.db.transaction(async ({ trx }) => {\n const contentTypes = Object.values(strapi.contentTypes) as Schema.ContentType[];\n const components = Object.values(strapi.components) as Schema.Component[];\n\n for (const model of [...contentTypes, ...components]) {\n const dbModel = strapi.db.metadata.get(model.uid);\n\n for (const attribute of Object.values(dbModel.attributes) as any) {\n /**\n * Only consider unidirectional relations\n */\n if (\n attribute.type !== 'relation' ||\n attribute.target !== uid ||\n attribute.inversedBy ||\n attribute.mappedBy\n ) {\n continue;\n }\n\n // TODO: joinColumn relations\n const joinTable = attribute.joinTable;\n if (!joinTable) {\n continue;\n }\n\n const { name: sourceColumnName } = joinTable.joinColumn;\n const { name: targetColumnName } = joinTable.inverseJoinColumn;\n\n /**\n * Load all relations that need to be updated\n */\n // NOTE: when the model has draft and publish, we can assume relation are only draft to draft & published to published\n const ids = oldVersions.map((entry) => entry.id);\n\n const oldVersionsRelations = await strapi.db\n .getConnection()\n .select('*')\n .from(joinTable.name)\n .whereIn(targetColumnName, ids)\n .transacting(trx);\n\n if (oldVersionsRelations.length > 0) {\n updates.push({ joinTable, relations: oldVersionsRelations });\n }\n\n /**\n * if publishing\n * if published version exists\n * updated published versions links\n * else\n * create link to newly published version\n *\n * if discarding\n * if published version link exists & not draft version link\n * create link to new draft version\n */\n\n if (!model.options?.draftAndPublish) {\n const ids = newVersions.map((entry) => entry.id);\n\n const newVersionsRelations = await strapi.db\n .getConnection()\n .select('*')\n .from(joinTable.name)\n .whereIn(targetColumnName, ids)\n .transacting(trx);\n\n if (newVersionsRelations.length > 0) {\n // when publishing a draft that doesn't have a published version yet,\n // copy the links to the draft over to the published version\n // when discarding a published version, if no drafts exists\n const discardToAdd = newVersionsRelations\n .filter((relation) => {\n const matchingOldVerion = oldVersionsRelations.find((oldRelation) => {\n return oldRelation[sourceColumnName] === relation[sourceColumnName];\n });\n\n return !matchingOldVerion;\n })\n .map(omit('id'));\n\n updates.push({ joinTable, relations: discardToAdd });\n }\n }\n }\n }\n });\n\n return updates;\n};\n\n/**\n * Updates uni directional relations to target the right entries when overriding published or draft entries.\n *\n * @param oldEntries The old entries that are being overridden\n * @param newEntries The new entries that are overriding the old ones\n * @param oldRelations The relations that were previously loaded with `load` @see load\n */\nconst sync = async (\n oldEntries: { id: string; locale: string }[],\n newEntries: { id: string; locale: string }[],\n oldRelations: { joinTable: any; relations: any[] }[]\n) => {\n /**\n * Create a map of old entry ids to new entry ids\n *\n * Will be used to update the relation target ids\n */\n const newEntryByLocale = keyBy('locale', newEntries);\n const oldEntriesMap = oldEntries.reduce(\n (acc, entry) => {\n const newEntry = newEntryByLocale[entry.locale];\n if (!newEntry) return acc;\n acc[entry.id] = newEntry.id;\n return acc;\n },\n {} as Record<string, string>\n );\n\n await strapi.db.transaction(async ({ trx }) => {\n // Iterate old relations that are deleted and insert the new ones\n for (const { joinTable, relations } of oldRelations) {\n // Update old ids with the new ones\n const column = joinTable.inverseJoinColumn.name;\n\n const newRelations = relations.map((relation) => {\n const newId = oldEntriesMap[relation[column]];\n return { ...relation, [column]: newId };\n });\n\n // Insert those relations into the join table\n await trx.batchInsert(joinTable.name, newRelations, 1000);\n }\n });\n};\n\nexport { load, sync };\n"],"names":["load","uid","oldVersions","newVersions","updates","strapi","db","transaction","trx","contentTypes","Object","values","components","model","dbModel","metadata","get","attribute","attributes","type","target","inversedBy","mappedBy","joinTable","name","sourceColumnName","joinColumn","targetColumnName","inverseJoinColumn","ids","map","entry","id","oldVersionsRelations","getConnection","select","from","whereIn","transacting","length","push","relations","options","draftAndPublish","newVersionsRelations","discardToAdd","filter","relation","matchingOldVerion","find","oldRelation","omit","sync","oldEntries","newEntries","oldRelations","newEntryByLocale","keyBy","oldEntriesMap","reduce","acc","newEntry","locale","column","newRelations","newId","batchInsert"],"mappings":";;;;AAUA;;;;IAKA,MAAMA,OAAO,OAAOC,GAAAA,EAAsB,EAAEC,WAAW,EAAEC,WAAW,EAAe,GAAA;AACjF,IAAA,MAAMC,UAAU,EAAE;;IAGlB,MAAMC,MAAAA,CAAOC,EAAE,CAACC,WAAW,CAAC,OAAO,EAAEC,GAAG,EAAE,GAAA;AACxC,QAAA,MAAMC,YAAeC,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOI,YAAY,CAAA;AACtD,QAAA,MAAMG,UAAaF,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOO,UAAU,CAAA;AAElD,QAAA,KAAK,MAAMC,KAAS,IAAA;AAAIJ,YAAAA,GAAAA,YAAAA;AAAiBG,YAAAA,GAAAA;SAAW,CA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AAAA;AACJ,QAAA,MAAM8B,QAAWL,GAAAA,gBAAgB,CAACzB,KAAAA,CAAM+B,MAAM,CAAC;QAC/C,IAAI,CAACD,UAAU,OAAOD,GAAAA;AACtBA,QAAAA,GAAG,CAAC7B,KAAMC,CAAAA,EAAE,CAAC,GAAG6B,SAAS7B,EAAE;QAC3B,OAAO4B,GAAAA;AACT,KAAA,EACA,EAAC,CAAA;IAGH,MAAMvD,MAAAA,CAAOC,EAAE,CAACC,WAAW,CAAC,OAAO,EAAEC,GAAG,EAAE,GAAA;;AAExC,QAAA,KAAK,MAAM,EAAEe,SAAS,EAAEkB,SAAS,EAAE,IAAIc,YAAc,CAAA;;AAEnD,YAAA,MAAMQ,MAASxC,GAAAA,SAAAA,CAAUK,iBAAiB,CAACJ,IAAI;AAE/C,YAAA,MAAMwC,YAAevB,GAAAA,SAAAA,CAAUX,GAAG,CAAC,CAACiB,QAAAA,GAAAA;AAClC,gBAAA,MAAMkB,QAAQP,aAAa,CAACX,QAAQ,CAACgB,OAAO,CAAC;gBAC7C,OAAO;AAAE,oBAAA,GAAGhB,QAAQ;AAAE,oBAAA,CAACgB,SAASE;AAAM,iBAAA;AACxC,aAAA,CAAA;;AAGA,YAAA,MAAMzD,IAAI0D,WAAW,CAAC3C,SAAUC,CAAAA,IAAI,EAAEwC,YAAc,EAAA,IAAA,CAAA;AACtD;AACF,KAAA,CAAA;AACF;;;;;"}
|
|
1
|
+
{"version":3,"file":"unidirectional-relations.js","sources":["../../../../src/services/document-service/utils/unidirectional-relations.ts"],"sourcesContent":["/* eslint-disable no-continue */\nimport { keyBy, omit } from 'lodash/fp';\n\nimport type { UID, Schema } from '@strapi/types';\n\nimport type { JoinTable } from '@strapi/database';\n\ninterface LoadContext {\n oldVersions: { id: string; locale: string }[];\n newVersions: { id: string; locale: string }[];\n}\n\ninterface RelationUpdate {\n joinTable: JoinTable;\n relations: Record<string, any>[];\n}\n\ninterface RelationFilterOptions {\n /**\n * Function to determine if a relation should be propagated to new document versions\n * This replaces the hardcoded component-specific logic\n */\n shouldPropagateRelation?: (\n relation: Record<string, any>,\n model: Schema.Component | Schema.ContentType,\n trx: any\n ) => Promise<boolean>;\n}\n\n/**\n * Loads lingering relations that need to be updated when overriding a published or draft entry.\n * This is necessary because the relations are uni-directional and the target entry is not aware of the source entry.\n * This is not the case for bi-directional relations, where the target entry is also linked to the source entry.\n */\nconst load = async (\n uid: UID.ContentType,\n { oldVersions, newVersions }: LoadContext,\n options: RelationFilterOptions = {}\n): Promise<RelationUpdate[]> => {\n const updates: RelationUpdate[] = [];\n\n // Iterate all components and content types to find relations that need to be updated\n await strapi.db.transaction(async ({ trx }) => {\n const contentTypes = Object.values(strapi.contentTypes) as Schema.ContentType[];\n const components = Object.values(strapi.components) as Schema.Component[];\n\n for (const model of [...contentTypes, ...components]) {\n const dbModel = strapi.db.metadata.get(model.uid);\n\n for (const attribute of Object.values(dbModel.attributes) as any) {\n /**\n * Only consider unidirectional relations\n */\n if (\n attribute.type !== 'relation' ||\n attribute.target !== uid ||\n attribute.inversedBy ||\n attribute.mappedBy\n ) {\n continue;\n }\n\n // TODO: joinColumn relations\n const joinTable = attribute.joinTable;\n if (!joinTable) {\n continue;\n }\n\n const { name: sourceColumnName } = joinTable.joinColumn;\n const { name: targetColumnName } = joinTable.inverseJoinColumn;\n\n /**\n * Load all relations that need to be updated\n */\n // NOTE: when the model has draft and publish, we can assume relation are only draft to draft & published to published\n const ids = oldVersions.map((entry) => entry.id);\n\n const oldVersionsRelations = await strapi.db\n .getConnection()\n .select('*')\n .from(joinTable.name)\n .whereIn(targetColumnName, ids)\n .transacting(trx);\n\n if (oldVersionsRelations.length > 0) {\n updates.push({ joinTable, relations: oldVersionsRelations });\n }\n\n /**\n * if publishing\n * if published version exists\n * updated published versions links\n * else\n * create link to newly published version\n *\n * if discarding\n * if published version link exists & not draft version link\n * create link to new draft version\n */\n if (!model.options?.draftAndPublish) {\n const ids = newVersions.map((entry) => entry.id);\n\n // This is the step were we query the join table based on the id of the document\n const newVersionsRelations = await strapi.db\n .getConnection()\n .select('*')\n .from(joinTable.name)\n .whereIn(targetColumnName, ids)\n .transacting(trx);\n\n let versionRelations = newVersionsRelations;\n if (options.shouldPropagateRelation) {\n const relationsToPropagate = [];\n for (const relation of newVersionsRelations) {\n if (await options.shouldPropagateRelation(relation, model, trx)) {\n relationsToPropagate.push(relation);\n }\n }\n versionRelations = relationsToPropagate;\n }\n\n if (versionRelations.length > 0) {\n // when publishing a draft that doesn't have a published version yet,\n // copy the links to the draft over to the published version\n // when discarding a published version, if no drafts exists\n const discardToAdd = versionRelations\n .filter((relation) => {\n const matchingOldVersion = oldVersionsRelations.find((oldRelation) => {\n return oldRelation[sourceColumnName] === relation[sourceColumnName];\n });\n\n return !matchingOldVersion;\n })\n .map(omit(strapi.db.metadata.identifiers.ID_COLUMN));\n\n updates.push({ joinTable, relations: discardToAdd });\n }\n }\n }\n }\n });\n\n return updates;\n};\n\n/**\n * Updates uni directional relations to target the right entries when overriding published or draft entries.\n *\n * This function:\n * 1. Creates new relations pointing to the new entry versions\n * 2. Precisely deletes only the old relations being replaced to prevent orphaned links\n *\n * @param oldEntries The old entries that are being overridden\n * @param newEntries The new entries that are overriding the old ones\n * @param oldRelations The relations that were previously loaded with `load` @see load\n */\nconst sync = async (\n oldEntries: { id: string; locale: string }[],\n newEntries: { id: string; locale: string }[],\n oldRelations: { joinTable: any; relations: any[] }[]\n) => {\n /**\n * Create a map of old entry ids to new entry ids\n *\n * Will be used to update the relation target ids\n */\n const newEntryByLocale = keyBy('locale', newEntries);\n const oldEntriesMap = oldEntries.reduce(\n (acc, entry) => {\n const newEntry = newEntryByLocale[entry.locale];\n if (!newEntry) return acc;\n acc[entry.id] = newEntry.id;\n return acc;\n },\n {} as Record<string, string>\n );\n\n await strapi.db.transaction(async ({ trx }) => {\n // Iterate old relations that are deleted and insert the new ones\n for (const { joinTable, relations } of oldRelations) {\n // Update old ids with the new ones\n const column = joinTable.inverseJoinColumn.name;\n\n const newRelations = relations.map((relation) => {\n const newId = oldEntriesMap[relation[column]];\n return { ...relation, [column]: newId };\n });\n\n // Insert those relations into the join table\n await trx.batchInsert(joinTable.name, newRelations, 1000);\n }\n });\n};\n\nexport { load, sync };\nexport type { RelationFilterOptions };\n"],"names":["load","uid","oldVersions","newVersions","options","updates","strapi","db","transaction","trx","contentTypes","Object","values","components","model","dbModel","metadata","get","attribute","attributes","type","target","inversedBy","mappedBy","joinTable","name","sourceColumnName","joinColumn","targetColumnName","inverseJoinColumn","ids","map","entry","id","oldVersionsRelations","getConnection","select","from","whereIn","transacting","length","push","relations","draftAndPublish","newVersionsRelations","versionRelations","shouldPropagateRelation","relationsToPropagate","relation","discardToAdd","filter","matchingOldVersion","find","oldRelation","omit","identifiers","ID_COLUMN","sync","oldEntries","newEntries","oldRelations","newEntryByLocale","keyBy","oldEntriesMap","reduce","acc","newEntry","locale","column","newRelations","newId","batchInsert"],"mappings":";;;;AA6BA;;;;AAIC,IACKA,MAAAA,IAAAA,GAAO,OACXC,GAAAA,EACA,EAAEC,WAAW,EAAEC,WAAW,EAAe,EACzCC,OAAiC,GAAA,EAAE,GAAA;AAEnC,IAAA,MAAMC,UAA4B,EAAE;;IAGpC,MAAMC,MAAAA,CAAOC,EAAE,CAACC,WAAW,CAAC,OAAO,EAAEC,GAAG,EAAE,GAAA;AACxC,QAAA,MAAMC,YAAeC,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOI,YAAY,CAAA;AACtD,QAAA,MAAMG,UAAaF,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOO,UAAU,CAAA;AAElD,QAAA,KAAK,MAAMC,KAAS,IAAA;AAAIJ,YAAAA,GAAAA,YAAAA;AAAiBG,YAAAA,GAAAA;SAAW,CAAE;YACpD,MAAME,OAAAA,GAAUT,OAAOC,EAAE,CAACS,QAAQ,CAACC,GAAG,CAACH,KAAAA,CAAMb,GAAG,CAAA;AAEhD,YAAA,KAAK,MAAMiB,SAAaP,IAAAA,MAAA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|
|
4
4
|
* Loads lingering relations that need to be updated when overriding a published or draft entry.
|
|
5
5
|
* This is necessary because the relations are uni-directional and the target entry is not aware of the source entry.
|
|
6
6
|
* This is not the case for bi-directional relations, where the target entry is also linked to the source entry.
|
|
7
|
-
*/ const load = async (uid, { oldVersions, newVersions })=>{
|
|
7
|
+
*/ const load = async (uid, { oldVersions, newVersions }, options = {})=>{
|
|
8
8
|
const updates = [];
|
|
9
9
|
// Iterate all components and content types to find relations that need to be updated
|
|
10
10
|
await strapi.db.transaction(async ({ trx })=>{
|
|
@@ -51,17 +51,28 @@ import { omit, keyBy } from 'lodash/fp';
|
|
|
51
51
|
* create link to new draft version
|
|
52
52
|
*/ if (!model.options?.draftAndPublish) {
|
|
53
53
|
const ids = newVersions.map((entry)=>entry.id);
|
|
54
|
+
// This is the step were we query the join table based on the id of the document
|
|
54
55
|
const newVersionsRelations = await strapi.db.getConnection().select('*').from(joinTable.name).whereIn(targetColumnName, ids).transacting(trx);
|
|
55
|
-
|
|
56
|
+
let versionRelations = newVersionsRelations;
|
|
57
|
+
if (options.shouldPropagateRelation) {
|
|
58
|
+
const relationsToPropagate = [];
|
|
59
|
+
for (const relation of newVersionsRelations){
|
|
60
|
+
if (await options.shouldPropagateRelation(relation, model, trx)) {
|
|
61
|
+
relationsToPropagate.push(relation);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
versionRelations = relationsToPropagate;
|
|
65
|
+
}
|
|
66
|
+
if (versionRelations.length > 0) {
|
|
56
67
|
// when publishing a draft that doesn't have a published version yet,
|
|
57
68
|
// copy the links to the draft over to the published version
|
|
58
69
|
// when discarding a published version, if no drafts exists
|
|
59
|
-
const discardToAdd =
|
|
60
|
-
const
|
|
70
|
+
const discardToAdd = versionRelations.filter((relation)=>{
|
|
71
|
+
const matchingOldVersion = oldVersionsRelations.find((oldRelation)=>{
|
|
61
72
|
return oldRelation[sourceColumnName] === relation[sourceColumnName];
|
|
62
73
|
});
|
|
63
|
-
return !
|
|
64
|
-
}).map(omit(
|
|
74
|
+
return !matchingOldVersion;
|
|
75
|
+
}).map(omit(strapi.db.metadata.identifiers.ID_COLUMN));
|
|
65
76
|
updates.push({
|
|
66
77
|
joinTable,
|
|
67
78
|
relations: discardToAdd
|
|
@@ -76,6 +87,10 @@ import { omit, keyBy } from 'lodash/fp';
|
|
|
76
87
|
/**
|
|
77
88
|
* Updates uni directional relations to target the right entries when overriding published or draft entries.
|
|
78
89
|
*
|
|
90
|
+
* This function:
|
|
91
|
+
* 1. Creates new relations pointing to the new entry versions
|
|
92
|
+
* 2. Precisely deletes only the old relations being replaced to prevent orphaned links
|
|
93
|
+
*
|
|
79
94
|
* @param oldEntries The old entries that are being overridden
|
|
80
95
|
* @param newEntries The new entries that are overriding the old ones
|
|
81
96
|
* @param oldRelations The relations that were previously loaded with `load` @see load
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unidirectional-relations.mjs","sources":["../../../../src/services/document-service/utils/unidirectional-relations.ts"],"sourcesContent":["/* eslint-disable no-continue */\nimport { keyBy, omit } from 'lodash/fp';\n\nimport type { UID, Schema } from '@strapi/types';\n\ninterface LoadContext {\n oldVersions: { id: string; locale: string }[];\n newVersions: { id: string; locale: string }[];\n}\n\n/**\n * Loads lingering relations that need to be updated when overriding a published or draft entry.\n * This is necessary because the relations are uni-directional and the target entry is not aware of the source entry.\n * This is not the case for bi-directional relations, where the target entry is also linked to the source entry.\n */\nconst load = async (uid: UID.ContentType, { oldVersions, newVersions }: LoadContext) => {\n const updates = [] as any;\n\n // Iterate all components and content types to find relations that need to be updated\n await strapi.db.transaction(async ({ trx }) => {\n const contentTypes = Object.values(strapi.contentTypes) as Schema.ContentType[];\n const components = Object.values(strapi.components) as Schema.Component[];\n\n for (const model of [...contentTypes, ...components]) {\n const dbModel = strapi.db.metadata.get(model.uid);\n\n for (const attribute of Object.values(dbModel.attributes) as any) {\n /**\n * Only consider unidirectional relations\n */\n if (\n attribute.type !== 'relation' ||\n attribute.target !== uid ||\n attribute.inversedBy ||\n attribute.mappedBy\n ) {\n continue;\n }\n\n // TODO: joinColumn relations\n const joinTable = attribute.joinTable;\n if (!joinTable) {\n continue;\n }\n\n const { name: sourceColumnName } = joinTable.joinColumn;\n const { name: targetColumnName } = joinTable.inverseJoinColumn;\n\n /**\n * Load all relations that need to be updated\n */\n // NOTE: when the model has draft and publish, we can assume relation are only draft to draft & published to published\n const ids = oldVersions.map((entry) => entry.id);\n\n const oldVersionsRelations = await strapi.db\n .getConnection()\n .select('*')\n .from(joinTable.name)\n .whereIn(targetColumnName, ids)\n .transacting(trx);\n\n if (oldVersionsRelations.length > 0) {\n updates.push({ joinTable, relations: oldVersionsRelations });\n }\n\n /**\n * if publishing\n * if published version exists\n * updated published versions links\n * else\n * create link to newly published version\n *\n * if discarding\n * if published version link exists & not draft version link\n * create link to new draft version\n */\n\n if (!model.options?.draftAndPublish) {\n const ids = newVersions.map((entry) => entry.id);\n\n const newVersionsRelations = await strapi.db\n .getConnection()\n .select('*')\n .from(joinTable.name)\n .whereIn(targetColumnName, ids)\n .transacting(trx);\n\n if (newVersionsRelations.length > 0) {\n // when publishing a draft that doesn't have a published version yet,\n // copy the links to the draft over to the published version\n // when discarding a published version, if no drafts exists\n const discardToAdd = newVersionsRelations\n .filter((relation) => {\n const matchingOldVerion = oldVersionsRelations.find((oldRelation) => {\n return oldRelation[sourceColumnName] === relation[sourceColumnName];\n });\n\n return !matchingOldVerion;\n })\n .map(omit('id'));\n\n updates.push({ joinTable, relations: discardToAdd });\n }\n }\n }\n }\n });\n\n return updates;\n};\n\n/**\n * Updates uni directional relations to target the right entries when overriding published or draft entries.\n *\n * @param oldEntries The old entries that are being overridden\n * @param newEntries The new entries that are overriding the old ones\n * @param oldRelations The relations that were previously loaded with `load` @see load\n */\nconst sync = async (\n oldEntries: { id: string; locale: string }[],\n newEntries: { id: string; locale: string }[],\n oldRelations: { joinTable: any; relations: any[] }[]\n) => {\n /**\n * Create a map of old entry ids to new entry ids\n *\n * Will be used to update the relation target ids\n */\n const newEntryByLocale = keyBy('locale', newEntries);\n const oldEntriesMap = oldEntries.reduce(\n (acc, entry) => {\n const newEntry = newEntryByLocale[entry.locale];\n if (!newEntry) return acc;\n acc[entry.id] = newEntry.id;\n return acc;\n },\n {} as Record<string, string>\n );\n\n await strapi.db.transaction(async ({ trx }) => {\n // Iterate old relations that are deleted and insert the new ones\n for (const { joinTable, relations } of oldRelations) {\n // Update old ids with the new ones\n const column = joinTable.inverseJoinColumn.name;\n\n const newRelations = relations.map((relation) => {\n const newId = oldEntriesMap[relation[column]];\n return { ...relation, [column]: newId };\n });\n\n // Insert those relations into the join table\n await trx.batchInsert(joinTable.name, newRelations, 1000);\n }\n });\n};\n\nexport { load, sync };\n"],"names":["load","uid","oldVersions","newVersions","updates","strapi","db","transaction","trx","contentTypes","Object","values","components","model","dbModel","metadata","get","attribute","attributes","type","target","inversedBy","mappedBy","joinTable","name","sourceColumnName","joinColumn","targetColumnName","inverseJoinColumn","ids","map","entry","id","oldVersionsRelations","getConnection","select","from","whereIn","transacting","length","push","relations","options","draftAndPublish","newVersionsRelations","discardToAdd","filter","relation","matchingOldVerion","find","oldRelation","omit","sync","oldEntries","newEntries","oldRelations","newEntryByLocale","keyBy","oldEntriesMap","reduce","acc","newEntry","locale","column","newRelations","newId","batchInsert"],"mappings":";;AAUA;;;;IAKA,MAAMA,OAAO,OAAOC,GAAAA,EAAsB,EAAEC,WAAW,EAAEC,WAAW,EAAe,GAAA;AACjF,IAAA,MAAMC,UAAU,EAAE;;IAGlB,MAAMC,MAAAA,CAAOC,EAAE,CAACC,WAAW,CAAC,OAAO,EAAEC,GAAG,EAAE,GAAA;AACxC,QAAA,MAAMC,YAAeC,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOI,YAAY,CAAA;AACtD,QAAA,MAAMG,UAAaF,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOO,UAAU,CAAA;AAElD,QAAA,KAAK,MAAMC,KAAS,IAAA;AAAI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UAAAA,CAAWM,MAAM,CACrC,CAACC,GAAK7B,EAAAA,KAAAA,GAAAA;AACJ,QAAA,MAAM8B,QAAWL,GAAAA,gBAAgB,CAACzB,KAAAA,CAAM+B,MAAM,CAAC;QAC/C,IAAI,CAACD,UAAU,OAAOD,GAAAA;AACtBA,QAAAA,GAAG,CAAC7B,KAAMC,CAAAA,EAAE,CAAC,GAAG6B,SAAS7B,EAAE;QAC3B,OAAO4B,GAAAA;AACT,KAAA,EACA,EAAC,CAAA;IAGH,MAAMvD,MAAAA,CAAOC,EAAE,CAACC,WAAW,CAAC,OAAO,EAAEC,GAAG,EAAE,GAAA;;AAExC,QAAA,KAAK,MAAM,EAAEe,SAAS,EAAEkB,SAAS,EAAE,IAAIc,YAAc,CAAA;;AAEnD,YAAA,MAAMQ,MAASxC,GAAAA,SAAAA,CAAUK,iBAAiB,CAACJ,IAAI;AAE/C,YAAA,MAAMwC,YAAevB,GAAAA,SAAAA,CAAUX,GAAG,CAAC,CAACiB,QAAAA,GAAAA;AAClC,gBAAA,MAAMkB,QAAQP,aAAa,CAACX,QAAQ,CAACgB,OAAO,CAAC;gBAC7C,OAAO;AAAE,oBAAA,GAAGhB,QAAQ;AAAE,oBAAA,CAACgB,SAASE;AAAM,iBAAA;AACxC,aAAA,CAAA;;AAGA,YAAA,MAAMzD,IAAI0D,WAAW,CAAC3C,SAAUC,CAAAA,IAAI,EAAEwC,YAAc,EAAA,IAAA,CAAA;AACtD;AACF,KAAA,CAAA;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"unidirectional-relations.mjs","sources":["../../../../src/services/document-service/utils/unidirectional-relations.ts"],"sourcesContent":["/* eslint-disable no-continue */\nimport { keyBy, omit } from 'lodash/fp';\n\nimport type { UID, Schema } from '@strapi/types';\n\nimport type { JoinTable } from '@strapi/database';\n\ninterface LoadContext {\n oldVersions: { id: string; locale: string }[];\n newVersions: { id: string; locale: string }[];\n}\n\ninterface RelationUpdate {\n joinTable: JoinTable;\n relations: Record<string, any>[];\n}\n\ninterface RelationFilterOptions {\n /**\n * Function to determine if a relation should be propagated to new document versions\n * This replaces the hardcoded component-specific logic\n */\n shouldPropagateRelation?: (\n relation: Record<string, any>,\n model: Schema.Component | Schema.ContentType,\n trx: any\n ) => Promise<boolean>;\n}\n\n/**\n * Loads lingering relations that need to be updated when overriding a published or draft entry.\n * This is necessary because the relations are uni-directional and the target entry is not aware of the source entry.\n * This is not the case for bi-directional relations, where the target entry is also linked to the source entry.\n */\nconst load = async (\n uid: UID.ContentType,\n { oldVersions, newVersions }: LoadContext,\n options: RelationFilterOptions = {}\n): Promise<RelationUpdate[]> => {\n const updates: RelationUpdate[] = [];\n\n // Iterate all components and content types to find relations that need to be updated\n await strapi.db.transaction(async ({ trx }) => {\n const contentTypes = Object.values(strapi.contentTypes) as Schema.ContentType[];\n const components = Object.values(strapi.components) as Schema.Component[];\n\n for (const model of [...contentTypes, ...components]) {\n const dbModel = strapi.db.metadata.get(model.uid);\n\n for (const attribute of Object.values(dbModel.attributes) as any) {\n /**\n * Only consider unidirectional relations\n */\n if (\n attribute.type !== 'relation' ||\n attribute.target !== uid ||\n attribute.inversedBy ||\n attribute.mappedBy\n ) {\n continue;\n }\n\n // TODO: joinColumn relations\n const joinTable = attribute.joinTable;\n if (!joinTable) {\n continue;\n }\n\n const { name: sourceColumnName } = joinTable.joinColumn;\n const { name: targetColumnName } = joinTable.inverseJoinColumn;\n\n /**\n * Load all relations that need to be updated\n */\n // NOTE: when the model has draft and publish, we can assume relation are only draft to draft & published to published\n const ids = oldVersions.map((entry) => entry.id);\n\n const oldVersionsRelations = await strapi.db\n .getConnection()\n .select('*')\n .from(joinTable.name)\n .whereIn(targetColumnName, ids)\n .transacting(trx);\n\n if (oldVersionsRelations.length > 0) {\n updates.push({ joinTable, relations: oldVersionsRelations });\n }\n\n /**\n * if publishing\n * if published version exists\n * updated published versions links\n * else\n * create link to newly published version\n *\n * if discarding\n * if published version link exists & not draft version link\n * create link to new draft version\n */\n if (!model.options?.draftAndPublish) {\n const ids = newVersions.map((entry) => entry.id);\n\n // This is the step were we query the join table based on the id of the document\n const newVersionsRelations = await strapi.db\n .getConnection()\n .select('*')\n .from(joinTable.name)\n .whereIn(targetColumnName, ids)\n .transacting(trx);\n\n let versionRelations = newVersionsRelations;\n if (options.shouldPropagateRelation) {\n const relationsToPropagate = [];\n for (const relation of newVersionsRelations) {\n if (await options.shouldPropagateRelation(relation, model, trx)) {\n relationsToPropagate.push(relation);\n }\n }\n versionRelations = relationsToPropagate;\n }\n\n if (versionRelations.length > 0) {\n // when publishing a draft that doesn't have a published version yet,\n // copy the links to the draft over to the published version\n // when discarding a published version, if no drafts exists\n const discardToAdd = versionRelations\n .filter((relation) => {\n const matchingOldVersion = oldVersionsRelations.find((oldRelation) => {\n return oldRelation[sourceColumnName] === relation[sourceColumnName];\n });\n\n return !matchingOldVersion;\n })\n .map(omit(strapi.db.metadata.identifiers.ID_COLUMN));\n\n updates.push({ joinTable, relations: discardToAdd });\n }\n }\n }\n }\n });\n\n return updates;\n};\n\n/**\n * Updates uni directional relations to target the right entries when overriding published or draft entries.\n *\n * This function:\n * 1. Creates new relations pointing to the new entry versions\n * 2. Precisely deletes only the old relations being replaced to prevent orphaned links\n *\n * @param oldEntries The old entries that are being overridden\n * @param newEntries The new entries that are overriding the old ones\n * @param oldRelations The relations that were previously loaded with `load` @see load\n */\nconst sync = async (\n oldEntries: { id: string; locale: string }[],\n newEntries: { id: string; locale: string }[],\n oldRelations: { joinTable: any; relations: any[] }[]\n) => {\n /**\n * Create a map of old entry ids to new entry ids\n *\n * Will be used to update the relation target ids\n */\n const newEntryByLocale = keyBy('locale', newEntries);\n const oldEntriesMap = oldEntries.reduce(\n (acc, entry) => {\n const newEntry = newEntryByLocale[entry.locale];\n if (!newEntry) return acc;\n acc[entry.id] = newEntry.id;\n return acc;\n },\n {} as Record<string, string>\n );\n\n await strapi.db.transaction(async ({ trx }) => {\n // Iterate old relations that are deleted and insert the new ones\n for (const { joinTable, relations } of oldRelations) {\n // Update old ids with the new ones\n const column = joinTable.inverseJoinColumn.name;\n\n const newRelations = relations.map((relation) => {\n const newId = oldEntriesMap[relation[column]];\n return { ...relation, [column]: newId };\n });\n\n // Insert those relations into the join table\n await trx.batchInsert(joinTable.name, newRelations, 1000);\n }\n });\n};\n\nexport { load, sync };\nexport type { RelationFilterOptions };\n"],"names":["load","uid","oldVersions","newVersions","options","updates","strapi","db","transaction","trx","contentTypes","Object","values","components","model","dbModel","metadata","get","attribute","attributes","type","target","inversedBy","mappedBy","joinTable","name","sourceColumnName","joinColumn","targetColumnName","inverseJoinColumn","ids","map","entry","id","oldVersionsRelations","getConnection","select","from","whereIn","transacting","length","push","relations","draftAndPublish","newVersionsRelations","versionRelations","shouldPropagateRelation","relationsToPropagate","relation","discardToAdd","filter","matchingOldVersion","find","oldRelation","omit","identifiers","ID_COLUMN","sync","oldEntries","newEntries","oldRelations","newEntryByLocale","keyBy","oldEntriesMap","reduce","acc","newEntry","locale","column","newRelations","newId","batchInsert"],"mappings":";;AA6BA;;;;AAIC,IACKA,MAAAA,IAAAA,GAAO,OACXC,GAAAA,EACA,EAAEC,WAAW,EAAEC,WAAW,EAAe,EACzCC,OAAiC,GAAA,EAAE,GAAA;AAEnC,IAAA,MAAMC,UAA4B,EAAE;;IAGpC,MAAMC,MAAAA,CAAOC,EAAE,CAACC,WAAW,CAAC,OAAO,EAAEC,GAAG,EAAE,GAAA;AACxC,QAAA,MAAMC,YAAeC,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOI,YAAY,CAAA;AACtD,QAAA,MAAMG,UAAaF,GAAAA,MAAAA,CAAOC,MAAM,CAACN,OAAOO,UAAU,CAAA;AAElD,QAAA,KAAK,MAAMC,KAAS,IAAA;AAAIJ,YAAAA,GAAAA,YAAAA;AAAiBG,YAAAA,GAAAA;SAAW,CAAE;YACpD,MAAME,OAAAA,GAAUT,OAAOC,EAAE,CAACS,QAAQ,CAACC,GAAG,CAACH,KAAAA,CAAMb,GAA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}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/entity-validator/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/entity-validator/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAMH,OAAO,KAAK,EAAE,OAAO,EAAO,MAAM,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAalE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,aAAa,EAAE;QAEb,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC;QAErB,EAAE,CAAC,EAAE,MAAM,CAAC;QAGZ,OAAO,CAAC,EAAE,gBAAgB,CAAC;KAC5B,CAAC;IAEF,eAAe,EAAE,MAAM,EAAE,CAAC;IAG1B,cAAc,EAAE,OAAO,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC;IACjD,sBAAsB,CAAC,EAAE,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;CAC/E,CAAC;AAWF,UAAU,gBAAgB;IACxB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACxB;AA6hBD,QAAA,MAAM,eAAe,EAAE,OAAO,CAAC,eAAe,CAAC,eAG9C,CAAC;AAEF,eAAe,eAAe,CAAC"}
|