{"version":3,"file":"delete-multiple.mjs","names":[],"sources":["../../../src/services/documents-bricks/delete-multiple.ts"],"sourcesContent":["import { getBricksTableSchema } from \"../../libs/collection/schema/runtime/runtime-schema-selectors.js\";\nimport { DocumentBricksRepository } from \"../../libs/repositories/index.js\";\nimport type { ServiceFn } from \"../../utils/services/types.js\";\n\nconst deleteMultiple: ServiceFn<\n\t[\n\t\t{\n\t\t\tversionId: number;\n\t\t\tdocumentId: number;\n\t\t\tcollectionKey: string;\n\t\t},\n\t],\n\tundefined\n> = async (context, data) => {\n\tconst Bricks = new DocumentBricksRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\n\tconst brickTableSchema = await getBricksTableSchema(\n\t\tcontext,\n\t\tdata.collectionKey,\n\t);\n\tif (brickTableSchema.error) return brickTableSchema;\n\n\t/**\n\t * Child storage tables are linked to root document-fields/brick rows via foreign\n\t * keys, so deleting the roots is enough to cascade repeater and relation rows.\n\t */\n\tconst rootTables = brickTableSchema.data.filter(\n\t\t(table) => table.type === \"document-fields\" || table.type === \"brick\",\n\t);\n\n\tconst deleteBricksPromises = [];\n\tfor (const brickTable of rootTables) {\n\t\tdeleteBricksPromises.push(\n\t\t\tBricks.deleteMultiple(\n\t\t\t\t{\n\t\t\t\t\twhere: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tkey: \"document_id\",\n\t\t\t\t\t\t\toperator: \"=\",\n\t\t\t\t\t\t\tvalue: data.documentId,\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tkey: \"document_version_id\",\n\t\t\t\t\t\t\toperator: \"=\",\n\t\t\t\t\t\t\tvalue: data.versionId,\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\ttableName: brickTable.name,\n\t\t\t\t},\n\t\t\t),\n\t\t);\n\t}\n\tconst deleteResults = await Promise.all(deleteBricksPromises);\n\tfor (const result of deleteResults) {\n\t\tif (result.error) return result;\n\t}\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: undefined,\n\t};\n};\n\nexport default deleteMultiple;\n"],"mappings":"oKAIA,MAAM,EASF,MAAO,EAAS,IAAS,CAC5B,IAAM,EAAS,IAAI,EAClB,EAAQ,GAAG,OACX,EAAQ,OAAO,EAChB,EAEM,EAAmB,MAAM,EAC9B,EACA,EAAK,aACN,EACA,GAAI,EAAiB,MAAO,OAAO,EAMnC,IAAM,EAAa,EAAiB,KAAK,OACvC,GAAU,EAAM,OAAS,mBAAqB,EAAM,OAAS,OAC/D,EAEM,EAAuB,CAAC,EAC9B,IAAK,IAAM,KAAc,EACxB,EAAqB,KACpB,EAAO,eACN,CACC,MAAO,CACN,CACC,IAAK,cACL,SAAU,IACV,MAAO,EAAK,UACb,EACA,CACC,IAAK,sBACL,SAAU,IACV,MAAO,EAAK,SACb,CACD,CACD,EACA,CACC,UAAW,EAAW,IACvB,CACD,CACD,EAED,IAAM,EAAgB,MAAM,QAAQ,IAAI,CAAoB,EAC5D,IAAK,IAAM,KAAU,EACpB,GAAI,EAAO,MAAO,OAAO,EAG1B,MAAO,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,CACD"}