@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 9.33 kB
Source Map (JSON)
{"version":3,"file":"upsert-single.mjs","names":["documentServices.checks.checkCollection","documentServices.checks.checkDocumentAccess","documentServices.checks.checkSingleCollectionDocumentCount","documentVersionServices.createSingle","documentWorkflowServices.createInitial"],"sources":["../../../src/services/documents/upsert-single.ts"],"sourcesContent":["import getMigrationStatus from \"../../libs/collection/get-collection-migration-status.js\";\nimport getCurrentCollectionMigrationId from \"../../libs/collection/migration/get-current-collection-migration-id.js\";\nimport { getTableNames } from \"../../libs/collection/schema/runtime/runtime-schema-selectors.js\";\nimport { copy } from \"../../libs/i18n/index.js\";\nimport { DocumentsRepository } from \"../../libs/repositories/index.js\";\nimport type { BrickInputSchema } from \"../../schemas/collection-bricks.js\";\nimport type { FieldInputSchema } from \"../../schemas/collection-fields.js\";\nimport {\n\tgenerateKeyBetween,\n\tisFractionalOrderKey,\n} from \"../../utils/helpers/index.js\";\nimport type { ServiceFn } from \"../../utils/services/types.js\";\nimport {\n\tdocumentServices,\n\tdocumentVersionServices,\n\tdocumentWorkflowServices,\n} from \"../index.js\";\nimport cleanupFailedCreate from \"./helpers/cleanup-failed-create.js\";\nimport invalidateClientDocumentCache from \"./helpers/invalidate-client-cache.js\";\n\nconst upsertSingle: ServiceFn<\n\t[\n\t\t{\n\t\t\tcollectionKey: string;\n\t\t\tuserId: number;\n\n\t\t\tdocumentId?: number;\n\t\t\tbricks?: Array<BrickInputSchema>;\n\t\t\tfields?: Array<FieldInputSchema>;\n\t\t},\n\t],\n\tnumber\n> = async (context, data) => {\n\tconst Document = new DocumentsRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\n\t// ----------------------------------------------\n\t// Checks\n\n\t//* check collection exists\n\tconst collectionRes = await documentServices.checks.checkCollection(context, {\n\t\tkey: data.collectionKey,\n\t});\n\tif (collectionRes.error) return collectionRes;\n\n\tconst tableNamesRes = await getTableNames(context, data.collectionKey);\n\tif (tableNamesRes.error) return tableNamesRes;\n\n\t//* check collection is locked\n\tif (collectionRes.data.getData.locked) {\n\t\treturn {\n\t\t\terror: {\n\t\t\t\ttype: \"basic\",\n\t\t\t\tname: copy(\"server:core.error.locked.collection.name\"),\n\t\t\t\tmessage: copy(\"server:core.error.locked.collection.message\"),\n\t\t\t\tstatus: 400,\n\t\t\t},\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\t//* check the schema status and if a migration is required\n\tconst migrationStatusRes = await getMigrationStatus(context, {\n\t\tcollection: collectionRes.data,\n\t});\n\tif (migrationStatusRes.error) return migrationStatusRes;\n\n\tif (migrationStatusRes.data.requiresMigration) {\n\t\treturn {\n\t\t\terror: {\n\t\t\t\ttype: \"basic\",\n\t\t\t\tname: copy(\"server:core.error.schema.migration.required.name\"),\n\t\t\t\tmessage: copy(\"server:core.error.schema.migration.required.message\"),\n\t\t\t\tstatus: 400,\n\t\t\t},\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tconst migrationIdRes = await getCurrentCollectionMigrationId(\n\t\tcontext,\n\t\tdata.collectionKey,\n\t);\n\tif (migrationIdRes.error) return migrationIdRes;\n\n\t//* check if document exists within the collection\n\tif (data.documentId !== undefined) {\n\t\tconst existingDocumentRes =\n\t\t\tawait documentServices.checks.checkDocumentAccess(context, {\n\t\t\t\tcollectionKey: data.collectionKey,\n\t\t\t\tid: data.documentId,\n\t\t\t});\n\t\tif (existingDocumentRes.error) return existingDocumentRes;\n\t}\n\n\t//* for single collections types, check if a document already exists\n\tconst checkDocumentCountRes =\n\t\tawait documentServices.checks.checkSingleCollectionDocumentCount(context, {\n\t\t\tcollectionKey: data.collectionKey,\n\t\t\tcollectionMode: collectionRes.data.getData.mode,\n\t\t\tdocumentId: data.documentId,\n\t\t\tdocumentTable: tableNamesRes.data.document,\n\t\t});\n\tif (checkDocumentCountRes.error) return checkDocumentCountRes;\n\n\t// ----------------------------------------------\n\t// Data\n\n\t//* append new orderable documents after the current last document\n\tlet order: string | undefined;\n\tif (\n\t\tdata.documentId === undefined &&\n\t\tcollectionRes.data.getData.orderable === true\n\t) {\n\t\tconst highestOrderRes = await Document.selectHighestOrderKey(\n\t\t\t{\n\t\t\t\ttenantKey: context.request.tenantKey,\n\t\t\t},\n\t\t\t{\n\t\t\t\ttableName: tableNamesRes.data.document,\n\t\t\t},\n\t\t);\n\t\tif (highestOrderRes.error) return highestOrderRes;\n\n\t\tconst highestOrder = highestOrderRes.data?.order ?? null;\n\t\torder = generateKeyBetween(\n\t\t\tisFractionalOrderKey(highestOrder) ? highestOrder : null,\n\t\t\tnull,\n\t\t);\n\t}\n\n\t// ----------------------------------------------\n\t// Upsert document\n\tconst upsertDocRes = await Document.upsertSingle(\n\t\t{\n\t\t\tdata: {\n\t\t\t\tid: data.documentId,\n\t\t\t\tcollection_key: data.collectionKey,\n\t\t\t\tcollection_migration_id: migrationIdRes.data,\n\t\t\t\t//* only applied on insert - the conflict update does not touch tenant_key, so existing documents are never re-stamped\n\t\t\t\ttenant_key: context.request.tenantKey ?? null,\n\t\t\t\t//* only applied on insert; reorders use documentServices.updateOrder\n\t\t\t\torder: order ?? null,\n\t\t\t\tcreated_by: data.userId,\n\t\t\t\tupdated_by: data.userId,\n\t\t\t\tis_deleted: false,\n\t\t\t\tupdated_at: new Date().toISOString(),\n\t\t\t},\n\t\t\treturning: [\"id\"],\n\t\t\tvalidation: {\n\t\t\t\tenabled: true,\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\ttableName: tableNamesRes.data.document,\n\t\t},\n\t);\n\tif (upsertDocRes.error) return upsertDocRes;\n\n\t// ----------------------------------------------\n\t// Create and manage document versions\n\tconst [createVersionRes, workflowRes] = await Promise.all([\n\t\tdocumentVersionServices.createSingle(context, {\n\t\t\tdocumentId: upsertDocRes.data.id,\n\t\t\tuserId: data.userId,\n\t\t\tbricks: data.bricks,\n\t\t\tfields: data.fields,\n\t\t\tcollection: collectionRes.data,\n\t\t}),\n\t\tdata.documentId === undefined\n\t\t\t? documentWorkflowServices.createInitial(context, {\n\t\t\t\t\tcollectionKey: data.collectionKey,\n\t\t\t\t\tdocumentId: upsertDocRes.data.id,\n\t\t\t\t\tuserId: data.userId,\n\t\t\t\t})\n\t\t\t: undefined,\n\t]);\n\n\tif (createVersionRes.error) {\n\t\tif (data.documentId === undefined) {\n\t\t\tawait cleanupFailedCreate(context, {\n\t\t\t\tcollectionKey: data.collectionKey,\n\t\t\t\tdocumentId: upsertDocRes.data.id,\n\t\t\t\ttableName: tableNamesRes.data.document,\n\t\t\t});\n\t\t}\n\t\treturn createVersionRes;\n\t}\n\tif (workflowRes?.error) {\n\t\tif (data.documentId === undefined) {\n\t\t\tawait cleanupFailedCreate(context, {\n\t\t\t\tcollectionKey: data.collectionKey,\n\t\t\t\tdocumentId: upsertDocRes.data.id,\n\t\t\t\ttableName: tableNamesRes.data.document,\n\t\t\t});\n\t\t}\n\t\treturn workflowRes;\n\t}\n\n\tawait invalidateClientDocumentCache(context, data.collectionKey);\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: upsertDocRes.data.id,\n\t};\n};\n\nexport default upsertSingle;\n"],"mappings":"w0BAoBA,MAAM,EAYF,MAAO,EAAS,IAAS,CAC5B,IAAM,EAAW,IAAI,EACpB,EAAQ,GAAG,OACX,EAAQ,OAAO,EAChB,EAMM,EAAgB,MAAMA,EAAwC,EAAS,CAC5E,IAAK,EAAK,aACX,CAAC,EACD,GAAI,EAAc,MAAO,OAAO,EAEhC,IAAM,EAAgB,MAAM,EAAc,EAAS,EAAK,aAAa,EACrE,GAAI,EAAc,MAAO,OAAO,EAGhC,GAAI,EAAc,KAAK,QAAQ,OAC9B,MAAO,CACN,MAAO,CACN,KAAM,QACN,KAAM,EAAK,0CAA0C,EACrD,QAAS,EAAK,6CAA6C,EAC3D,OAAQ,GACT,EACA,KAAM,IAAA,EACP,EAID,IAAM,EAAqB,MAAM,EAAmB,EAAS,CAC5D,WAAY,EAAc,IAC3B,CAAC,EACD,GAAI,E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