@strapi/core 0.0.0-next.d2d15ef227d67cce89c2673764c0555c841cd29c → 0.0.0-next.f6dca5adf05ef6bed9605a1535999ab0bbbf063e
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/dist/ee/index.d.ts.map +1 -1
- package/dist/ee/index.js +6 -1
- package/dist/ee/index.js.map +1 -1
- package/dist/ee/index.mjs +6 -1
- package/dist/ee/index.mjs.map +1 -1
- package/dist/loaders/plugins/get-enabled-plugins.js +1 -1
- package/dist/loaders/plugins/get-enabled-plugins.js.map +1 -1
- package/dist/loaders/plugins/get-enabled-plugins.mjs +1 -1
- package/dist/loaders/plugins/get-enabled-plugins.mjs.map +1 -1
- package/dist/migrations/database/5.0.0-discard-drafts.d.ts.map +1 -1
- package/dist/migrations/database/5.0.0-discard-drafts.js +13 -1
- package/dist/migrations/database/5.0.0-discard-drafts.js.map +1 -1
- package/dist/migrations/database/5.0.0-discard-drafts.mjs +13 -1
- package/dist/migrations/database/5.0.0-discard-drafts.mjs.map +1 -1
- package/dist/services/cron.js +9 -4
- package/dist/services/cron.js.map +1 -1
- package/dist/services/cron.mjs +9 -4
- package/dist/services/cron.mjs.map +1 -1
- package/dist/services/document-service/common.d.ts +1 -1
- package/dist/services/document-service/common.d.ts.map +1 -1
- package/dist/services/document-service/common.js.map +1 -1
- package/dist/services/document-service/common.mjs.map +1 -1
- package/dist/services/document-service/entries.d.ts +2 -2
- package/dist/services/document-service/entries.d.ts.map +1 -1
- package/dist/services/document-service/entries.js +6 -7
- package/dist/services/document-service/entries.js.map +1 -1
- package/dist/services/document-service/entries.mjs +1 -2
- package/dist/services/document-service/entries.mjs.map +1 -1
- package/dist/services/document-service/index.d.ts +2 -1
- package/dist/services/document-service/index.d.ts.map +1 -1
- package/dist/services/document-service/index.js +3 -2
- package/dist/services/document-service/index.js.map +1 -1
- package/dist/services/document-service/index.mjs +3 -2
- package/dist/services/document-service/index.mjs.map +1 -1
- package/dist/services/document-service/repository.d.ts.map +1 -1
- package/dist/services/document-service/repository.js +21 -6
- package/dist/services/document-service/repository.js.map +1 -1
- package/dist/services/document-service/repository.mjs +21 -6
- package/dist/services/document-service/repository.mjs.map +1 -1
- package/dist/services/document-service/transform/id-map.d.ts.map +1 -1
- package/dist/services/document-service/transform/id-map.js +13 -4
- package/dist/services/document-service/transform/id-map.js.map +1 -1
- package/dist/services/document-service/transform/id-map.mjs +14 -5
- package/dist/services/document-service/transform/id-map.mjs.map +1 -1
- package/dist/services/document-service/utils/unidirectional-relations.d.ts +11 -8
- package/dist/services/document-service/utils/unidirectional-relations.d.ts.map +1 -1
- package/dist/services/document-service/utils/unidirectional-relations.js +27 -16
- package/dist/services/document-service/utils/unidirectional-relations.js.map +1 -1
- package/dist/services/document-service/utils/unidirectional-relations.mjs +28 -17
- package/dist/services/document-service/utils/unidirectional-relations.mjs.map +1 -1
- package/dist/utils/transform-content-types-to-models.d.ts +353 -21
- package/dist/utils/transform-content-types-to-models.d.ts.map +1 -1
- package/package.json +15 -15
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"id-map.js","sources":["../../../../src/services/document-service/transform/id-map.ts"],"sourcesContent":["import { Core, Data } from '@strapi/types';\nimport { async } from '@strapi/utils';\n\n/**\n * TODO: Find a better way to encode keys than this\n * This converts an object into a string by joining its keys and values,\n * so it can be used as a key in a Map.\n *\n * @example\n * const obj = { a: 1, b: 2 };\n * const key = encodeKey(obj);\n * ^ \"a:::1&&b:::2\"\n */\nconst encodeKey = (obj: any) => {\n // Sort keys to always keep the same order when encoding\n const keys = Object.keys(obj).sort();\n return keys.map((key) => `${key}:::${obj[key]}`).join('&&');\n};\n\ninterface KeyFields {\n uid: string;\n documentId: Data.ID;\n locale?: string | null;\n status?: 'draft' | 'published';\n}\n\nexport interface IdMap {\n loadedIds: Map<string, string>;\n toLoadIds: Map<string, KeyFields>;\n // Make the Keys type to be the params of add\n add(keys: KeyFields): void;\n load(): Promise<void>;\n get(keys: KeyFields): string | undefined;\n clear(): void;\n}\n\n/**\n * Holds a registry of document ids and their corresponding entity ids.\n */\nconst createIdMap = ({ strapi }: { strapi: Core.Strapi }): IdMap => {\n const loadedIds = new Map();\n const toLoadIds = new Map();\n\n return {\n loadedIds,\n toLoadIds,\n /**\n * Register a new document id and its corresponding entity id.\n */\n add(keyFields: KeyFields) {\n const key = encodeKey({ status: 'published', locale: null, ...keyFields });\n\n // If the id is already loaded, do nothing\n if (loadedIds.has(key)) return;\n // If the id is already in the toLoadIds, do nothing\n if (toLoadIds.has(key)) return;\n\n // Add the id to the toLoadIds\n toLoadIds.set(key, keyFields);\n },\n\n /**\n * Load all ids from the registry.\n */\n async load() {\n // Document Id to Entry Id queries are batched by its uid and locale\n // TODO: Add publication state too\n const loadIdValues = Array.from(toLoadIds.values());\n\n // 1. Group ids to query together\n const idsByUidAndLocale = loadIdValues.reduce((acc, { documentId, ...rest }) => {\n const key = encodeKey(rest);\n const ids = acc[key] || { ...rest, documentIds: [] };\n ids.documentIds.push(documentId);\n return { ...acc, [key]: ids };\n }, {});\n\n // 2. Query ids\n await async.map(\n Object.values(idsByUidAndLocale),\n async ({ uid, locale, documentIds, status }: any) => {\n const findParams = {\n select: ['id', 'documentId', 'locale', 'publishedAt'],\n where: {\n documentId: { $in: documentIds },\n locale: locale || null,\n
|
1
|
+
{"version":3,"file":"id-map.js","sources":["../../../../src/services/document-service/transform/id-map.ts"],"sourcesContent":["import { Core, Data, UID } from '@strapi/types';\nimport { async, contentTypes } from '@strapi/utils';\n\nconst hasDraftAndPublish = (uid: UID.CollectionType) => {\n const model = strapi.getModel(uid);\n return contentTypes.hasDraftAndPublish(model);\n};\n\n/**\n * TODO: Find a better way to encode keys than this\n * This converts an object into a string by joining its keys and values,\n * so it can be used as a key in a Map.\n *\n * @example\n * const obj = { a: 1, b: 2 };\n * const key = encodeKey(obj);\n * ^ \"a:::1&&b:::2\"\n */\nconst encodeKey = (obj: any) => {\n // Ignore status field for models without draft and publish\n if (!hasDraftAndPublish(obj.uid)) {\n delete obj.status;\n }\n\n // Sort keys to always keep the same order when encoding\n const keys = Object.keys(obj).sort();\n return keys.map((key) => `${key}:::${obj[key]}`).join('&&');\n};\n\ninterface KeyFields {\n uid: string;\n documentId: Data.ID;\n locale?: string | null;\n status?: 'draft' | 'published';\n}\n\nexport interface IdMap {\n loadedIds: Map<string, string>;\n toLoadIds: Map<string, KeyFields>;\n // Make the Keys type to be the params of add\n add(keys: KeyFields): void;\n load(): Promise<void>;\n get(keys: KeyFields): string | undefined;\n clear(): void;\n}\n\n/**\n * Holds a registry of document ids and their corresponding entity ids.\n */\nconst createIdMap = ({ strapi }: { strapi: Core.Strapi }): IdMap => {\n const loadedIds = new Map();\n const toLoadIds = new Map();\n\n return {\n loadedIds,\n toLoadIds,\n /**\n * Register a new document id and its corresponding entity id.\n */\n add(keyFields: KeyFields) {\n const key = encodeKey({ status: 'published', locale: null, ...keyFields });\n\n // If the id is already loaded, do nothing\n if (loadedIds.has(key)) return;\n // If the id is already in the toLoadIds, do nothing\n if (toLoadIds.has(key)) return;\n\n // Add the id to the toLoadIds\n toLoadIds.set(key, keyFields);\n },\n\n /**\n * Load all ids from the registry.\n */\n async load() {\n // Document Id to Entry Id queries are batched by its uid and locale\n // TODO: Add publication state too\n const loadIdValues = Array.from(toLoadIds.values());\n\n // 1. Group ids to query together\n const idsByUidAndLocale = loadIdValues.reduce((acc, { documentId, ...rest }) => {\n const key = encodeKey(rest);\n const ids = acc[key] || { ...rest, documentIds: [] };\n ids.documentIds.push(documentId);\n return { ...acc, [key]: ids };\n }, {});\n\n // 2. Query ids\n await async.map(\n Object.values(idsByUidAndLocale),\n async ({ uid, locale, documentIds, status }: any) => {\n const findParams = {\n select: ['id', 'documentId', 'locale', 'publishedAt'],\n where: {\n documentId: { $in: documentIds },\n locale: locale || null,\n },\n } as any;\n\n if (hasDraftAndPublish(uid)) {\n findParams.where.publishedAt = status === 'draft' ? null : { $ne: null };\n }\n\n const result = await strapi?.db?.query(uid).findMany(findParams);\n\n // 3. Store result in loadedIds\n result?.forEach(({ documentId, id, locale, publishedAt }: any) => {\n const key = encodeKey({\n documentId,\n uid,\n locale,\n status: publishedAt ? 'published' : 'draft',\n });\n loadedIds.set(key, id);\n });\n }\n );\n\n // 4. Clear toLoadIds\n toLoadIds.clear();\n },\n\n /**\n * Get the entity id for a given document id.\n */\n get(keys: KeyFields) {\n const key = encodeKey({ status: 'published', locale: null, ...keys });\n return loadedIds.get(key);\n },\n\n /**\n * Clear the registry.\n */\n clear() {\n loadedIds.clear();\n toLoadIds.clear();\n },\n };\n};\n\nexport { createIdMap };\n"],"names":["contentTypes","strapi","async","locale"],"mappings":";;;AAGA,MAAM,qBAAqB,CAAC,QAA4B;AAChD,QAAA,QAAQ,OAAO,SAAS,GAAG;AAC1B,SAAAA,YAAA,aAAa,mBAAmB,KAAK;AAC9C;AAYA,MAAM,YAAY,CAAC,QAAa;AAE9B,MAAI,CAAC,mBAAmB,IAAI,GAAG,GAAG;AAChC,WAAO,IAAI;AAAA,EACb;AAGA,QAAM,OAAO,OAAO,KAAK,GAAG,EAAE,KAAK;AACnC,SAAO,KAAK,IAAI,CAAC,QAAQ,GAAG,GAAG,MAAM,IAAI,GAAG,CAAC,EAAE,EAAE,KAAK,IAAI;AAC5D;AAsBA,MAAM,cAAc,CAAC,EAAE,QAAAC,cAA6C;AAC5D,QAAA,gCAAgB;AAChB,QAAA,gCAAgB;AAEf,SAAA;AAAA,IACL;AAAA,IACA;AAAA;AAAA;AAAA;AAAA,IAIA,IAAI,WAAsB;AAClB,YAAA,MAAM,UAAU,EAAE,QAAQ,aAAa,QAAQ,MAAM,GAAG,UAAA,CAAW;AAGrE,UAAA,UAAU,IAAI,GAAG;AAAG;AAEpB,UAAA,UAAU,IAAI,GAAG;AAAG;AAGd,gBAAA,IAAI,KAAK,SAAS;AAAA,IAC9B;AAAA;AAAA;AAAA;AAAA,IAKA,MAAM,OAAO;AAGX,YAAM,eAAe,MAAM,KAAK,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;AACN,gBAAU,MAAM;AAChB,gBAAU,MAAM;AAAA,IAClB;AAAA,EAAA;AAEJ;;"}
|
@@ -1,9 +1,16 @@
|
|
1
|
-
import { async } from "@strapi/utils";
|
1
|
+
import { async, contentTypes } from "@strapi/utils";
|
2
|
+
const hasDraftAndPublish = (uid) => {
|
3
|
+
const model = strapi.getModel(uid);
|
4
|
+
return contentTypes.hasDraftAndPublish(model);
|
5
|
+
};
|
2
6
|
const encodeKey = (obj) => {
|
7
|
+
if (!hasDraftAndPublish(obj.uid)) {
|
8
|
+
delete obj.status;
|
9
|
+
}
|
3
10
|
const keys = Object.keys(obj).sort();
|
4
11
|
return keys.map((key) => `${key}:::${obj[key]}`).join("&&");
|
5
12
|
};
|
6
|
-
const createIdMap = ({ strapi }) => {
|
13
|
+
const createIdMap = ({ strapi: strapi2 }) => {
|
7
14
|
const loadedIds = /* @__PURE__ */ new Map();
|
8
15
|
const toLoadIds = /* @__PURE__ */ new Map();
|
9
16
|
return {
|
@@ -38,11 +45,13 @@ const createIdMap = ({ strapi }) => {
|
|
38
45
|
select: ["id", "documentId", "locale", "publishedAt"],
|
39
46
|
where: {
|
40
47
|
documentId: { $in: documentIds },
|
41
|
-
locale: locale || null
|
42
|
-
publishedAt: status === "draft" ? null : { $ne: null }
|
48
|
+
locale: locale || null
|
43
49
|
}
|
44
50
|
};
|
45
|
-
|
51
|
+
if (hasDraftAndPublish(uid)) {
|
52
|
+
findParams.where.publishedAt = status === "draft" ? null : { $ne: null };
|
53
|
+
}
|
54
|
+
const result = await strapi2?.db?.query(uid).findMany(findParams);
|
46
55
|
result?.forEach(({ documentId, id, locale: locale2, publishedAt }) => {
|
47
56
|
const key = encodeKey({
|
48
57
|
documentId,
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"id-map.mjs","sources":["../../../../src/services/document-service/transform/id-map.ts"],"sourcesContent":["import { Core, Data } from '@strapi/types';\nimport { async } from '@strapi/utils';\n\n/**\n * TODO: Find a better way to encode keys than this\n * This converts an object into a string by joining its keys and values,\n * so it can be used as a key in a Map.\n *\n * @example\n * const obj = { a: 1, b: 2 };\n * const key = encodeKey(obj);\n * ^ \"a:::1&&b:::2\"\n */\nconst encodeKey = (obj: any) => {\n // Sort keys to always keep the same order when encoding\n const keys = Object.keys(obj).sort();\n return keys.map((key) => `${key}:::${obj[key]}`).join('&&');\n};\n\ninterface KeyFields {\n uid: string;\n documentId: Data.ID;\n locale?: string | null;\n status?: 'draft' | 'published';\n}\n\nexport interface IdMap {\n loadedIds: Map<string, string>;\n toLoadIds: Map<string, KeyFields>;\n // Make the Keys type to be the params of add\n add(keys: KeyFields): void;\n load(): Promise<void>;\n get(keys: KeyFields): string | undefined;\n clear(): void;\n}\n\n/**\n * Holds a registry of document ids and their corresponding entity ids.\n */\nconst createIdMap = ({ strapi }: { strapi: Core.Strapi }): IdMap => {\n const loadedIds = new Map();\n const toLoadIds = new Map();\n\n return {\n loadedIds,\n toLoadIds,\n /**\n * Register a new document id and its corresponding entity id.\n */\n add(keyFields: KeyFields) {\n const key = encodeKey({ status: 'published', locale: null, ...keyFields });\n\n // If the id is already loaded, do nothing\n if (loadedIds.has(key)) return;\n // If the id is already in the toLoadIds, do nothing\n if (toLoadIds.has(key)) return;\n\n // Add the id to the toLoadIds\n toLoadIds.set(key, keyFields);\n },\n\n /**\n * Load all ids from the registry.\n */\n async load() {\n // Document Id to Entry Id queries are batched by its uid and locale\n // TODO: Add publication state too\n const loadIdValues = Array.from(toLoadIds.values());\n\n // 1. Group ids to query together\n const idsByUidAndLocale = loadIdValues.reduce((acc, { documentId, ...rest }) => {\n const key = encodeKey(rest);\n const ids = acc[key] || { ...rest, documentIds: [] };\n ids.documentIds.push(documentId);\n return { ...acc, [key]: ids };\n }, {});\n\n // 2. Query ids\n await async.map(\n Object.values(idsByUidAndLocale),\n async ({ uid, locale, documentIds, status }: any) => {\n const findParams = {\n select: ['id', 'documentId', 'locale', 'publishedAt'],\n where: {\n documentId: { $in: documentIds },\n locale: locale || null,\n
|
1
|
+
{"version":3,"file":"id-map.mjs","sources":["../../../../src/services/document-service/transform/id-map.ts"],"sourcesContent":["import { Core, Data, UID } from '@strapi/types';\nimport { async, contentTypes } from '@strapi/utils';\n\nconst hasDraftAndPublish = (uid: UID.CollectionType) => {\n const model = strapi.getModel(uid);\n return contentTypes.hasDraftAndPublish(model);\n};\n\n/**\n * TODO: Find a better way to encode keys than this\n * This converts an object into a string by joining its keys and values,\n * so it can be used as a key in a Map.\n *\n * @example\n * const obj = { a: 1, b: 2 };\n * const key = encodeKey(obj);\n * ^ \"a:::1&&b:::2\"\n */\nconst encodeKey = (obj: any) => {\n // Ignore status field for models without draft and publish\n if (!hasDraftAndPublish(obj.uid)) {\n delete obj.status;\n }\n\n // Sort keys to always keep the same order when encoding\n const keys = Object.keys(obj).sort();\n return keys.map((key) => `${key}:::${obj[key]}`).join('&&');\n};\n\ninterface KeyFields {\n uid: string;\n documentId: Data.ID;\n locale?: string | null;\n status?: 'draft' | 'published';\n}\n\nexport interface IdMap {\n loadedIds: Map<string, string>;\n toLoadIds: Map<string, KeyFields>;\n // Make the Keys type to be the params of add\n add(keys: KeyFields): void;\n load(): Promise<void>;\n get(keys: KeyFields): string | undefined;\n clear(): void;\n}\n\n/**\n * Holds a registry of document ids and their corresponding entity ids.\n */\nconst createIdMap = ({ strapi }: { strapi: Core.Strapi }): IdMap => {\n const loadedIds = new Map();\n const toLoadIds = new Map();\n\n return {\n loadedIds,\n toLoadIds,\n /**\n * Register a new document id and its corresponding entity id.\n */\n add(keyFields: KeyFields) {\n const key = encodeKey({ status: 'published', locale: null, ...keyFields });\n\n // If the id is already loaded, do nothing\n if (loadedIds.has(key)) return;\n // If the id is already in the toLoadIds, do nothing\n if (toLoadIds.has(key)) return;\n\n // Add the id to the toLoadIds\n toLoadIds.set(key, keyFields);\n },\n\n /**\n * Load all ids from the registry.\n */\n async load() {\n // Document Id to Entry Id queries are batched by its uid and locale\n // TODO: Add publication state too\n const loadIdValues = Array.from(toLoadIds.values());\n\n // 1. Group ids to query together\n const idsByUidAndLocale = loadIdValues.reduce((acc, { documentId, ...rest }) => {\n const key = encodeKey(rest);\n const ids = acc[key] || { ...rest, documentIds: [] };\n ids.documentIds.push(documentId);\n return { ...acc, [key]: ids };\n }, {});\n\n // 2. Query ids\n await async.map(\n Object.values(idsByUidAndLocale),\n async ({ uid, locale, documentIds, status }: any) => {\n const findParams = {\n select: ['id', 'documentId', 'locale', 'publishedAt'],\n where: {\n documentId: { $in: documentIds },\n locale: locale || null,\n },\n } as any;\n\n if (hasDraftAndPublish(uid)) {\n findParams.where.publishedAt = status === 'draft' ? null : { $ne: null };\n }\n\n const result = await strapi?.db?.query(uid).findMany(findParams);\n\n // 3. Store result in loadedIds\n result?.forEach(({ documentId, id, locale, publishedAt }: any) => {\n const key = encodeKey({\n documentId,\n uid,\n locale,\n status: publishedAt ? 'published' : 'draft',\n });\n loadedIds.set(key, id);\n });\n }\n );\n\n // 4. Clear toLoadIds\n toLoadIds.clear();\n },\n\n /**\n * Get the entity id for a given document id.\n */\n get(keys: KeyFields) {\n const key = encodeKey({ status: 'published', locale: null, ...keys });\n return loadedIds.get(key);\n },\n\n /**\n * Clear the registry.\n */\n clear() {\n loadedIds.clear();\n toLoadIds.clear();\n },\n };\n};\n\nexport { createIdMap };\n"],"names":["strapi","locale"],"mappings":";AAGA,MAAM,qBAAqB,CAAC,QAA4B;AAChD,QAAA,QAAQ,OAAO,SAAS,GAAG;AAC1B,SAAA,aAAa,mBAAmB,KAAK;AAC9C;AAYA,MAAM,YAAY,CAAC,QAAa;AAE9B,MAAI,CAAC,mBAAmB,IAAI,GAAG,GAAG;AAChC,WAAO,IAAI;AAAA,EACb;AAGA,QAAM,OAAO,OAAO,KAAK,GAAG,EAAE,KAAK;AACnC,SAAO,KAAK,IAAI,CAAC,QAAQ,GAAG,GAAG,MAAM,IAAI,GAAG,CAAC,EAAE,EAAE,KAAK,IAAI;AAC5D;AAsBA,MAAM,cAAc,CAAC,EAAE,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U;AAAA,cACpB;AAAA,cACA;AAAA,cACA,QAAAA;AAAAA,cACA,QAAQ,cAAc,cAAc;AAAA,YAAA,CACrC;AACS,sBAAA,IAAI,KAAK,EAAE;AAAA,UAAA,CACtB;AAAA,QACH;AAAA,MAAA;AAIF,gBAAU,MAAM;AAAA,IAClB;AAAA;AAAA;AAAA;AAAA,IAKA,IAAI,MAAiB;AACb,YAAA,MAAM,UAAU,EAAE,QAAQ,aAAa,QAAQ,MAAM,GAAG,KAAA,CAAM;AAC7D,aAAA,UAAU,IAAI,GAAG;AAAA,IAC1B;AAAA;AAAA;AAAA;AAAA,IAKA,QAAQ;AACN,gBAAU,MAAM;AAChB,gBAAU,MAAM;AAAA,IAClB;AAAA,EAAA;AAEJ;"}
|
@@ -1,17 +1,20 @@
|
|
1
1
|
import { UID } from '@strapi/types';
|
2
|
+
interface LoadContext {
|
3
|
+
oldVersions: {
|
4
|
+
id: string;
|
5
|
+
locale: string;
|
6
|
+
}[];
|
7
|
+
newVersions: {
|
8
|
+
id: string;
|
9
|
+
locale: string;
|
10
|
+
}[];
|
11
|
+
}
|
2
12
|
/**
|
3
13
|
* Loads lingering relations that need to be updated when overriding a published or draft entry.
|
4
14
|
* This is necessary because the relations are uni-directional and the target entry is not aware of the source entry.
|
5
15
|
* This is not the case for bi-directional relations, where the target entry is also linked to the source entry.
|
6
|
-
*
|
7
|
-
* @param uid The content type uid
|
8
|
-
* @param oldEntries The old entries that are being overridden
|
9
|
-
* @returns An array of relations that need to be updated with the join table reference.
|
10
16
|
*/
|
11
|
-
declare const load: (uid: UID.ContentType,
|
12
|
-
id: string;
|
13
|
-
locale: string;
|
14
|
-
}[]) => Promise<any>;
|
17
|
+
declare const load: (uid: UID.ContentType, { oldVersions, newVersions }: LoadContext) => Promise<any>;
|
15
18
|
/**
|
16
19
|
* Updates uni directional relations to target the right entries when overriding published or draft entries.
|
17
20
|
*
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"unidirectional-relations.d.ts","sourceRoot":"","sources":["../../../../src/services/document-service/utils/unidirectional-relations.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,GAAG,EAAU,MAAM,eAAe,CAAC;AAE5C
|
1
|
+
{"version":3,"file":"unidirectional-relations.d.ts","sourceRoot":"","sources":["../../../../src/services/document-service/utils/unidirectional-relations.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,GAAG,EAAU,MAAM,eAAe,CAAC;AAE5C,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;;;;GAIG;AACH,QAAA,MAAM,IAAI,QAAe,IAAI,WAAW,gCAAgC,WAAW,iBA8FlF,CAAC;AAEF;;;;;;GAMG;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"}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
3
3
|
const fp = require("lodash/fp");
|
4
|
-
const load = async (uid,
|
4
|
+
const load = async (uid, { oldVersions, newVersions }) => {
|
5
5
|
const updates = [];
|
6
6
|
await strapi.db.transaction(async ({ trx }) => {
|
7
7
|
const contentTypes = Object.values(strapi.contentTypes);
|
@@ -9,21 +9,33 @@ const load = async (uid, oldEntries) => {
|
|
9
9
|
for (const model of [...contentTypes, ...components]) {
|
10
10
|
const dbModel = strapi.db.metadata.get(model.uid);
|
11
11
|
for (const attribute of Object.values(dbModel.attributes)) {
|
12
|
-
if (attribute.type !== "relation")
|
13
|
-
continue;
|
14
|
-
if (attribute.target !== uid)
|
15
|
-
continue;
|
16
|
-
if (attribute.inversedBy || attribute.mappedBy)
|
12
|
+
if (attribute.type !== "relation" || attribute.target !== uid || attribute.inversedBy || attribute.mappedBy) {
|
17
13
|
continue;
|
14
|
+
}
|
18
15
|
const joinTable = attribute.joinTable;
|
19
|
-
if (!joinTable)
|
20
|
-
continue;
|
21
|
-
const { name } = joinTable.inverseJoinColumn;
|
22
|
-
const oldEntriesIds = oldEntries.map((entry) => entry.id);
|
23
|
-
const relations = await strapi.db.getConnection().select("*").from(joinTable.name).whereIn(name, oldEntriesIds).transacting(trx);
|
24
|
-
if (relations.length === 0)
|
16
|
+
if (!joinTable) {
|
25
17
|
continue;
|
26
|
-
|
18
|
+
}
|
19
|
+
const { name: sourceColumnName } = joinTable.joinColumn;
|
20
|
+
const { name: targetColumnName } = joinTable.inverseJoinColumn;
|
21
|
+
const ids = oldVersions.map((entry) => entry.id);
|
22
|
+
const oldVersionsRelations = await strapi.db.getConnection().select("*").from(joinTable.name).whereIn(targetColumnName, ids).transacting(trx);
|
23
|
+
if (oldVersionsRelations.length > 0) {
|
24
|
+
updates.push({ joinTable, relations: oldVersionsRelations });
|
25
|
+
}
|
26
|
+
if (!model.options?.draftAndPublish) {
|
27
|
+
const ids2 = newVersions.map((entry) => entry.id);
|
28
|
+
const newVersionsRelations = await strapi.db.getConnection().select("*").from(joinTable.name).whereIn(targetColumnName, ids2).transacting(trx);
|
29
|
+
if (newVersionsRelations.length > 0) {
|
30
|
+
const discardToAdd = newVersionsRelations.filter((relation) => {
|
31
|
+
const matchingOldVerion = oldVersionsRelations.find((oldRelation) => {
|
32
|
+
return oldRelation[sourceColumnName] === relation[sourceColumnName];
|
33
|
+
});
|
34
|
+
return !matchingOldVerion;
|
35
|
+
}).map(fp.omit("id"));
|
36
|
+
updates.push({ joinTable, relations: discardToAdd });
|
37
|
+
}
|
38
|
+
}
|
27
39
|
}
|
28
40
|
}
|
29
41
|
});
|
@@ -42,14 +54,13 @@ const sync = async (oldEntries, newEntries, oldRelations) => {
|
|
42
54
|
{}
|
43
55
|
);
|
44
56
|
await strapi.db.transaction(async ({ trx }) => {
|
45
|
-
const con = strapi.db.getConnection();
|
46
57
|
for (const { joinTable, relations } of oldRelations) {
|
58
|
+
const column = joinTable.inverseJoinColumn.name;
|
47
59
|
const newRelations = relations.map((relation) => {
|
48
|
-
const column = joinTable.inverseJoinColumn.name;
|
49
60
|
const newId = oldEntriesMap[relation[column]];
|
50
61
|
return { ...relation, [column]: newId };
|
51
62
|
});
|
52
|
-
await
|
63
|
+
await trx.batchInsert(joinTable.name, newRelations, 1e3);
|
53
64
|
}
|
54
65
|
});
|
55
66
|
};
|
@@ -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 } from 'lodash/fp';\n\nimport { UID, Schema } from '@strapi/types';\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
|
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 { 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":["ids","omit","keyBy"],"mappings":";;;AAeA,MAAM,OAAO,OAAO,KAAsB,EAAE,aAAa,kBAA+B;AACtF,QAAM,UAAU,CAAA;AAGhB,QAAM,OAAO,GAAG,YAAY,OAAO,EAAE,UAAU;AAC7C,UAAM,eAAe,OAAO,OAAO,OAAO,YAAY;AACtD,UAAM,aAAa,OAAO,OAAO,OAAO,UAAU;AAElD,eAAW,SAAS,CAAC,GAAG,cAAc,GAAG,UAAU,GAAG;AACpD,YAAM,UAAU,OAAO,GAAG,SAAS,IAAI,MAAM,GAAG;AAEhD,iBAAW,aAAa,OAAO,OAAO,QAAQ,UAAU,GAAU;AAK9D,YAAA,UAAU,SAAS,cACnB,UAAU,W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,YAAA,WAAW,iBAAiB,MAAM,MAAM;AAC9C,UAAI,CAAC;AAAiB,eAAA;AAClB,UAAA,MAAM,EAAE,IAAI,SAAS;AAClB,aAAA;AAAA,IACT;AAAA,IACA,CAAC;AAAA,EAAA;AAGH,QAAM,OAAO,GAAG,YAAY,OAAO,EAAE,UAAU;AAE7C,eAAW,EAAE,WAAW,UAAU,KAAK,cAAc;AAE7C,YAAA,SAAS,UAAU,kBAAkB;AAE3C,YAAM,eAAe,UAAU,IAAI,CAAC,aAAa;AAC/C,cAAM,QAAQ,cAAc,SAAS,MAAM,CAAC;AAC5C,eAAO,EAAE,GAAG,UAAU,CAAC,MAAM,GAAG,MAAM;AAAA,MAAA,CACvC;AAGD,YAAM,IAAI,YAAY,UAAU,MAAM,cAAc,GAAI;AAAA,IAC1D;AAAA,EAAA,CACD;AACH;;;"}
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { keyBy } from "lodash/fp";
|
2
|
-
const load = async (uid,
|
1
|
+
import { omit, keyBy } from "lodash/fp";
|
2
|
+
const load = async (uid, { oldVersions, newVersions }) => {
|
3
3
|
const updates = [];
|
4
4
|
await strapi.db.transaction(async ({ trx }) => {
|
5
5
|
const contentTypes = Object.values(strapi.contentTypes);
|
@@ -7,21 +7,33 @@ const load = async (uid, oldEntries) => {
|
|
7
7
|
for (const model of [...contentTypes, ...components]) {
|
8
8
|
const dbModel = strapi.db.metadata.get(model.uid);
|
9
9
|
for (const attribute of Object.values(dbModel.attributes)) {
|
10
|
-
if (attribute.type !== "relation")
|
11
|
-
continue;
|
12
|
-
if (attribute.target !== uid)
|
13
|
-
continue;
|
14
|
-
if (attribute.inversedBy || attribute.mappedBy)
|
10
|
+
if (attribute.type !== "relation" || attribute.target !== uid || attribute.inversedBy || attribute.mappedBy) {
|
15
11
|
continue;
|
12
|
+
}
|
16
13
|
const joinTable = attribute.joinTable;
|
17
|
-
if (!joinTable)
|
18
|
-
continue;
|
19
|
-
const { name } = joinTable.inverseJoinColumn;
|
20
|
-
const oldEntriesIds = oldEntries.map((entry) => entry.id);
|
21
|
-
const relations = await strapi.db.getConnection().select("*").from(joinTable.name).whereIn(name, oldEntriesIds).transacting(trx);
|
22
|
-
if (relations.length === 0)
|
14
|
+
if (!joinTable) {
|
23
15
|
continue;
|
24
|
-
|
16
|
+
}
|
17
|
+
const { name: sourceColumnName } = joinTable.joinColumn;
|
18
|
+
const { name: targetColumnName } = joinTable.inverseJoinColumn;
|
19
|
+
const ids = oldVersions.map((entry) => entry.id);
|
20
|
+
const oldVersionsRelations = await strapi.db.getConnection().select("*").from(joinTable.name).whereIn(targetColumnName, ids).transacting(trx);
|
21
|
+
if (oldVersionsRelations.length > 0) {
|
22
|
+
updates.push({ joinTable, relations: oldVersionsRelations });
|
23
|
+
}
|
24
|
+
if (!model.options?.draftAndPublish) {
|
25
|
+
const ids2 = newVersions.map((entry) => entry.id);
|
26
|
+
const newVersionsRelations = await strapi.db.getConnection().select("*").from(joinTable.name).whereIn(targetColumnName, ids2).transacting(trx);
|
27
|
+
if (newVersionsRelations.length > 0) {
|
28
|
+
const discardToAdd = newVersionsRelations.filter((relation) => {
|
29
|
+
const matchingOldVerion = oldVersionsRelations.find((oldRelation) => {
|
30
|
+
return oldRelation[sourceColumnName] === relation[sourceColumnName];
|
31
|
+
});
|
32
|
+
return !matchingOldVerion;
|
33
|
+
}).map(omit("id"));
|
34
|
+
updates.push({ joinTable, relations: discardToAdd });
|
35
|
+
}
|
36
|
+
}
|
25
37
|
}
|
26
38
|
}
|
27
39
|
});
|
@@ -40,14 +52,13 @@ const sync = async (oldEntries, newEntries, oldRelations) => {
|
|
40
52
|
{}
|
41
53
|
);
|
42
54
|
await strapi.db.transaction(async ({ trx }) => {
|
43
|
-
const con = strapi.db.getConnection();
|
44
55
|
for (const { joinTable, relations } of oldRelations) {
|
56
|
+
const column = joinTable.inverseJoinColumn.name;
|
45
57
|
const newRelations = relations.map((relation) => {
|
46
|
-
const column = joinTable.inverseJoinColumn.name;
|
47
58
|
const newId = oldEntriesMap[relation[column]];
|
48
59
|
return { ...relation, [column]: newId };
|
49
60
|
});
|
50
|
-
await
|
61
|
+
await trx.batchInsert(joinTable.name, newRelations, 1e3);
|
51
62
|
}
|
52
63
|
});
|
53
64
|
};
|
@@ -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 } from 'lodash/fp';\n\nimport { UID, Schema } from '@strapi/types';\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
|
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 { 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":["ids"],"mappings":";AAeA,MAAM,OAAO,OAAO,KAAsB,EAAE,aAAa,kBAA+B;AACtF,QAAM,UAAU,CAAA;AAGhB,QAAM,OAAO,GAAG,YAAY,OAAO,EAAE,UAAU;AAC7C,UAAM,eAAe,OAAO,OAAO,OAAO,YAAY;AACtD,UAAM,aAAa,OAAO,OAAO,OAAO,UAAU;AAElD,eAAW,SAAS,CAAC,GAAG,cAAc,GAAG,UAAU,GAAG;AACpD,YAAM,UAAU,OAAO,GAAG,SAAS,IAAI,MAAM,GAAG;AAEhD,iBAAW,aAAa,OAAO,OAAO,QAAQ,UAAU,GAAU;AAK9D,YAAA,UAAU,SAAS,cACnB,UAAU,WAAW,OACrB,UAAU,cACV,UAAU,UACV;AACA;AAAA,QACF;AAGA,cAAM,YAAY,UAAU;AAC5B,YAAI,CAAC,WAAW;AACd;AAAA,QACF;AAEA,cAAM,EAAE,MAAM,qBAAqB,UAAU;AAC7C,cAAM,EAAE,MAAM,qBAAqB,UAAU;AAM7C,cAAM,MAAM,YAAY,IAAI,CAAC,UAAU,MAAM,EAAE;AAE/C,cAAM,uBAAuB,MAAM,OAAO,GACvC,gBACA,OAAO,GAAG,EACV,KAAK,UAAU,IAAI,EACnB,QAAQ,kBAAkB,GAAG,EAC7B,YAAY,GAAG;AAEd,YAAA,qBAAqB,SAAS,GAAG;AACnC,kBAAQ,KAAK,E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