@strapi/content-manager 5.0.0-rc.5 → 5.0.0-rc.6

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.
@@ -1,5 +1,5 @@
1
1
  import strapiUtils, { validateYupSchema, errors, async, contentTypes as contentTypes$1, yup as yup$1, validateYupSchemaSync, policy, traverse, setCreatorFields, isOperatorOfType, relations as relations$1, traverseEntity, pagination } from "@strapi/utils";
2
- import { pick, omit, difference, intersection, pipe, propOr, isEqual, isEmpty, set, isNil as isNil$1, has, prop, assoc, mapValues, flow, uniq, uniqBy, concat, getOr, propEq, merge, groupBy, castArray } from "lodash/fp";
2
+ import { pick, omit, difference, castArray, intersection, pipe, propOr, isEqual, isEmpty, set, isNil as isNil$1, has, prop, assoc, mapValues, flow, uniq, uniqBy, concat, getOr, propEq, merge, groupBy } from "lodash/fp";
3
3
  import "@strapi/types";
4
4
  import * as yup from "yup";
5
5
  import { scheduleJob } from "node-schedule";
@@ -200,9 +200,10 @@ const createServiceUtils = ({ strapi: strapi2 }) => {
200
200
  const meta = await documentMetadataService.getMetadata(contentTypeUid, document);
201
201
  return documentMetadataService.getStatus(document, meta.availableStatus);
202
202
  };
203
- const getDeepPopulate2 = (uid2) => {
203
+ const getDeepPopulate2 = (uid2, useDatabaseSyntax = false) => {
204
204
  const model = strapi2.getModel(uid2);
205
205
  const attributes = Object.entries(model.attributes);
206
+ const fieldSelector = useDatabaseSyntax ? "select" : "fields";
206
207
  return attributes.reduce((acc, [attributeName, attribute]) => {
207
208
  switch (attribute.type) {
208
209
  case "relation": {
@@ -212,12 +213,12 @@ const createServiceUtils = ({ strapi: strapi2 }) => {
212
213
  }
213
214
  const isVisible2 = contentTypes$1.isVisibleAttribute(model, attributeName);
214
215
  if (isVisible2) {
215
- acc[attributeName] = { fields: ["documentId", "locale", "publishedAt"] };
216
+ acc[attributeName] = { [fieldSelector]: ["documentId", "locale", "publishedAt"] };
216
217
  }
217
218
  break;
218
219
  }
219
220
  case "media": {
220
- acc[attributeName] = { fields: ["id"] };
221
+ acc[attributeName] = { [fieldSelector]: ["id"] };
221
222
  break;
222
223
  }
223
224
  case "component": {
@@ -468,6 +469,42 @@ const createHistoryService = ({ strapi: strapi2 }) => {
468
469
  }
469
470
  };
470
471
  };
472
+ const shouldCreateHistoryVersion = (context) => {
473
+ if (!strapi.requestContext.get()?.request.url.startsWith("/content-manager")) {
474
+ return false;
475
+ }
476
+ if (context.action !== "create" && context.action !== "update" && context.action !== "clone" && context.action !== "publish" && context.action !== "unpublish" && context.action !== "discardDraft") {
477
+ return false;
478
+ }
479
+ if (context.action === "update" && strapi.requestContext.get()?.request.url.endsWith("/actions/publish")) {
480
+ return false;
481
+ }
482
+ if (!context.contentType.uid.startsWith("api::")) {
483
+ return false;
484
+ }
485
+ return true;
486
+ };
487
+ const getSchemas = (uid2) => {
488
+ const attributesSchema = strapi.getModel(uid2).attributes;
489
+ const componentsSchemas = Object.keys(attributesSchema).reduce(
490
+ (currentComponentSchemas, key) => {
491
+ const fieldSchema = attributesSchema[key];
492
+ if (fieldSchema.type === "component") {
493
+ const componentSchema = strapi.getModel(fieldSchema.component).attributes;
494
+ return {
495
+ ...currentComponentSchemas,
496
+ [fieldSchema.component]: componentSchema
497
+ };
498
+ }
499
+ return currentComponentSchemas;
500
+ },
501
+ {}
502
+ );
503
+ return {
504
+ schema: omit(FIELDS_TO_IGNORE, attributesSchema),
505
+ componentsSchemas
506
+ };
507
+ };
471
508
  const createLifecyclesService = ({ strapi: strapi2 }) => {
472
509
  const state = {
473
510
  deleteExpiredJob: null,
@@ -480,63 +517,43 @@ const createLifecyclesService = ({ strapi: strapi2 }) => {
480
517
  return;
481
518
  }
482
519
  strapi2.documents.use(async (context, next) => {
483
- if (!strapi2.requestContext.get()?.request.url.startsWith("/content-manager")) {
484
- return next();
485
- }
486
- if (context.action !== "create" && context.action !== "update" && context.action !== "clone" && context.action !== "publish" && context.action !== "unpublish" && context.action !== "discardDraft") {
487
- return next();
488
- }
489
- if (context.action === "update" && strapi2.requestContext.get()?.request.url.endsWith("/actions/publish")) {
490
- return next();
491
- }
492
- const contentTypeUid = context.contentType.uid;
493
- if (!contentTypeUid.startsWith("api::")) {
494
- return next();
495
- }
496
520
  const result = await next();
497
- const documentContext = {
498
- documentId: context.action === "create" || context.action === "clone" ? result.documentId : context.params.documentId,
499
- locale: context.params?.locale
500
- };
521
+ if (!shouldCreateHistoryVersion(context)) {
522
+ return result;
523
+ }
524
+ const documentId = context.action === "create" || context.action === "clone" ? result.documentId : context.params.documentId;
501
525
  const defaultLocale = await serviceUtils.getDefaultLocale();
502
- const locale = documentContext.locale || defaultLocale;
503
- if (Array.isArray(locale)) {
504
- strapi2.log.warn(
505
- "[Content manager history middleware]: An array of locales was provided, but only a single locale is supported for the findOne operation."
506
- );
507
- return next();
526
+ const locales = castArray(context.params?.locale || defaultLocale);
527
+ if (!locales.length) {
528
+ return result;
508
529
  }
509
- const document = await strapi2.documents(contentTypeUid).findOne({
510
- documentId: documentContext.documentId,
511
- locale,
512
- populate: serviceUtils.getDeepPopulate(contentTypeUid)
530
+ const uid2 = context.contentType.uid;
531
+ const schemas = getSchemas(uid2);
532
+ const localeEntries = await strapi2.db.query(uid2).findMany({
533
+ where: {
534
+ documentId,
535
+ locale: { $in: locales },
536
+ publishedAt: null
537
+ },
538
+ populate: serviceUtils.getDeepPopulate(
539
+ uid2,
540
+ true
541
+ /* use database syntax */
542
+ )
513
543
  });
514
- const status = await serviceUtils.getVersionStatus(contentTypeUid, document);
515
- const attributesSchema = strapi2.getModel(contentTypeUid).attributes;
516
- const componentsSchemas = Object.keys(
517
- attributesSchema
518
- ).reduce((currentComponentSchemas, key) => {
519
- const fieldSchema = attributesSchema[key];
520
- if (fieldSchema.type === "component") {
521
- const componentSchema = strapi2.getModel(fieldSchema.component).attributes;
522
- return {
523
- ...currentComponentSchemas,
524
- [fieldSchema.component]: componentSchema
525
- };
526
- }
527
- return currentComponentSchemas;
528
- }, {});
529
544
  await strapi2.db.transaction(async ({ onCommit }) => {
530
- onCommit(() => {
531
- getService(strapi2, "history").createVersion({
532
- contentType: contentTypeUid,
533
- data: omit(FIELDS_TO_IGNORE, document),
534
- schema: omit(FIELDS_TO_IGNORE, attributesSchema),
535
- componentsSchemas,
536
- relatedDocumentId: documentContext.documentId,
537
- locale,
538
- status
539
- });
545
+ onCommit(async () => {
546
+ for (const entry of localeEntries) {
547
+ const status = await serviceUtils.getVersionStatus(uid2, entry);
548
+ await getService(strapi2, "history").createVersion({
549
+ contentType: uid2,
550
+ data: omit(FIELDS_TO_IGNORE, entry),
551
+ relatedDocumentId: documentId,
552
+ locale: entry.locale,
553
+ status,
554
+ ...schemas
555
+ });
556
+ }
540
557
  });
541
558
  });
542
559
  return result;
@@ -4124,7 +4141,13 @@ const documentMetadata = ({ strapi: strapi2 }) => ({
4124
4141
  */
4125
4142
  async formatDocumentWithMetadata(uid2, document, opts = {}) {
4126
4143
  if (!document) {
4127
- return document;
4144
+ return {
4145
+ data: document,
4146
+ meta: {
4147
+ availableLocales: [],
4148
+ availableStatus: []
4149
+ }
4150
+ };
4128
4151
  }
4129
4152
  const hasDraftAndPublish = contentTypes$1.hasDraftAndPublish(strapi2.getModel(uid2));
4130
4153
  if (!hasDraftAndPublish) {