UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

2 lines 3.12 kB
import{copy as e}from"../../libs/i18n/copy.mjs";import t from"../../utils/helpers/get-base-url.mjs";import n from"../../libs/repositories/document-bricks.mjs";import r from"../../libs/repositories/document-versions.mjs";import{getBricksTableSchema as i,getTableNames as a}from"../../libs/collection/schema/runtime/runtime-schema-selectors.mjs";import o from"../../libs/collection/get-collection-migration-status.mjs";import s from"../collections/get-single-instance.mjs";import c from"../../libs/collection/migration/get-current-collection-migration-id.mjs";import l from"../documents-bricks/helpers/aggregate-brick-tables.mjs";import u from"../documents-bricks/insert-brick-tables.mjs";import d from"../../libs/formatters/document-bricks.mjs";const f=async(f,p)=>{let m=new r(f.db.client,f.config.db),h=new n(f.db.client,f.config.db),g=s(f,{key:p.collectionKey});if(g.error)return g;let _=await o(f,{collection:g.data});if(_.error)return _;if(_.data.requiresMigration)return{error:{type:`basic`,name:e(`server:core.error.schema.migration.required.name`),message:e(`server:core.error.schema.migration.required.message`),status:400},data:void 0};let[v,y]=await Promise.all([i(f,p.collectionKey),a(f,p.collectionKey)]);if(v.error)return v;if(y.error)return y;let[b,x,S]=await Promise.all([m.selectSingle({select:[`id`,`type`,`document_id`,`content_id`],where:[{key:`id`,operator:`=`,value:p.fromVersionId}],validation:{enabled:!0,defaultError:{message:e(`server:core.documents.version.not.found.message`),status:404}}},{tableName:y.data.version}),h.selectMultipleByVersionId({versionId:p.fromVersionId,documentId:p.documentId,bricksSchema:v.data},{tableName:y.data.version}),c(f,p.collectionKey)]);if(b.error)return b;if(x.error)return x;if(S.error)return S;if(x.data===void 0)return{error:{status:404,message:e(`server:core.documents.version.not.found.message`)},data:void 0};if(b.data.document_id!==p.documentId)return{error:{type:`basic`,message:e(`server:core.documents.version.document.mismatch`),status:404},data:void 0};let C=await m.createSingle({data:{document_id:p.documentId,collection_key:p.collectionKey,collection_migration_id:S.data,type:p.toVersionType,promoted_from:p.fromVersionId,content_id:b.data.content_id,created_by:p.userId,updated_by:p.userId},returning:[`id`],validation:{enabled:!0,defaultError:{status:400,message:e(`server:core.documents.create.failed`)}}},{tableName:y.data.version});if(C.error)return C;let w=t(f),T=await u(f,{tables:l({collection:g.data,documentId:p.documentId,versionId:C.data.id,localization:f.config.localization,bricks:d.formatMultiple({bricksQuery:x.data,bricksSchema:v.data,refData:{data:{}},collection:g.data,config:f.config,host:w}),fields:d.formatDocumentFields({bricksQuery:x.data,bricksSchema:v.data,refData:{data:{}},collection:g.data,config:f.config,host:w}),tableNameByteLimit:f.config.db.config.tableNameByteLimit}).sort((e,t)=>e.priority-t.priority),collection:g.data});return T.error?T:{error:void 0,data:{versionId:C.data.id,contentId:b.data.content_id,sourceVersionType:b.data.type}}};export{f as default}; //# sourceMappingURL=clone-version.mjs.map