@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
2 lines • 2.96 kB
JavaScript
import{copy as e}from"../../libs/i18n/copy.mjs";import{generateKeyBetween as t,isFractionalOrderKey as n}from"../../utils/helpers/fractional-order.mjs";import r from"../../libs/repositories/documents.mjs";import{getTableNames as i}from"../../libs/collection/schema/runtime/runtime-schema-selectors.mjs";import a from"../../libs/collection/get-collection-migration-status.mjs";import o from"../document-workflows/create-initial.mjs";import s from"./checks/check-collection.mjs";import c from"./checks/check-document-access.mjs";import l from"./checks/check-single-collection-document-count.mjs";import u from"./helpers/invalidate-client-cache.mjs";import d from"../../libs/collection/migration/get-current-collection-migration-id.mjs";import f from"./helpers/cleanup-failed-create.mjs";import p from"../documents-versions/create-single.mjs";const m=async(m,h)=>{let g=new r(m.db.client,m.config.db),_=await s(m,{key:h.collectionKey});if(_.error)return _;let v=await i(m,h.collectionKey);if(v.error)return v;if(_.data.getData.locked)return{error:{type:`basic`,name:e(`server:core.error.locked.collection.name`),message:e(`server:core.error.locked.collection.message`),status:400},data:void 0};let y=await a(m,{collection:_.data});if(y.error)return y;if(y.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 b=await d(m,h.collectionKey);if(b.error)return b;if(h.documentId!==void 0){let e=await c(m,{collectionKey:h.collectionKey,id:h.documentId});if(e.error)return e}let x=await l(m,{collectionKey:h.collectionKey,collectionMode:_.data.getData.mode,documentId:h.documentId,documentTable:v.data.document});if(x.error)return x;let S;if(h.documentId===void 0&&_.data.getData.orderable===!0){let e=await g.selectHighestOrderKey({tenantKey:m.request.tenantKey},{tableName:v.data.document});if(e.error)return e;let r=e.data?.order??null;S=t(n(r)?r:null,null)}let C=await g.upsertSingle({data:{id:h.documentId,collection_key:h.collectionKey,collection_migration_id:b.data,tenant_key:m.request.tenantKey??null,order:S??null,created_by:h.userId,updated_by:h.userId,is_deleted:!1,updated_at:new Date().toISOString()},returning:[`id`],validation:{enabled:!0}},{tableName:v.data.document});if(C.error)return C;let[w,T]=await Promise.all([p(m,{documentId:C.data.id,userId:h.userId,bricks:h.bricks,fields:h.fields,collection:_.data}),h.documentId===void 0?o(m,{collectionKey:h.collectionKey,documentId:C.data.id,userId:h.userId}):void 0]);return w.error?(h.documentId===void 0&&await f(m,{collectionKey:h.collectionKey,documentId:C.data.id,tableName:v.data.document}),w):T?.error?(h.documentId===void 0&&await f(m,{collectionKey:h.collectionKey,documentId:C.data.id,tableName:v.data.document}),T):(await u(m,h.collectionKey),{error:void 0,data:C.data.id})};export{m as default};
//# sourceMappingURL=upsert-single.mjs.map