{"version":3,"file":"morph-relations.mjs","sources":["../../src/entity-manager/morph-relations.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/naming-convention */ // allow __type\nimport { curry, groupBy, pipe, mapValues, map, isEmpty } from 'lodash/fp';\nimport type { Knex } from 'knex';\n\nimport { createQueryBuilder } from '../query';\nimport type { Database } from '..';\nimport type { MorphJoinTable, Relation } from '../types';\n\ntype Rows = Record[];\n\nconst getMorphToManyRowsLinkedToMorphOne = (\n rows: Rows,\n {\n uid,\n attributeName,\n typeColumn,\n db,\n }: {\n uid: string;\n attributeName: string;\n typeColumn: { name: string };\n db: Database;\n }\n) =>\n rows.filter((row) => {\n const relatedType = row[typeColumn.name] as string;\n const field = row.field as any;\n\n const targetAttribute = db.metadata.get(relatedType).attributes[field] as Relation.MorphOne;\n\n // ensure targeted field is the right one + check if it is a morphOne\n return (\n targetAttribute?.target === uid &&\n targetAttribute?.morphBy === attributeName &&\n targetAttribute?.relation === 'morphOne'\n );\n });\n\nexport const deleteRelatedMorphOneRelationsAfterMorphToManyUpdate = async (\n rows: Rows,\n {\n uid,\n attributeName,\n joinTable,\n db,\n transaction: trx,\n }: {\n uid: string;\n attributeName: string;\n joinTable: MorphJoinTable;\n db: Database;\n transaction?: Knex.Transaction;\n }\n) => {\n const { morphColumn } = joinTable;\n const { idColumn, typeColumn } = morphColumn;\n\n const morphOneRows = getMorphToManyRowsLinkedToMorphOne(rows, {\n uid,\n attributeName,\n typeColumn,\n db,\n });\n\n const groupByType = groupBy(typeColumn.name);\n const groupByField = groupBy('field');\n\n const typeAndFieldIdsGrouped = pipe(groupByType, mapValues(groupByField))(morphOneRows);\n\n const orWhere: object[] = [];\n\n for (const [type, v] of Object.entries(typeAndFieldIdsGrouped)) {\n for (const [field, arr] of Object.entries(v)) {\n orWhere.push({\n [typeColumn.name]: type,\n field,\n [idColumn.name]: { $in: map(idColumn.name, arr) },\n });\n }\n }\n\n if (!isEmpty(orWhere)) {\n await createQueryBuilder(joinTable.name, db)\n .delete()\n .where({ $or: orWhere })\n .transacting(trx)\n .execute();\n }\n};\n\n/**\n * Encoding utilities for polymorphic relations.\n *\n * In some scenarios is useful to encode both the id & __type of the relation\n * to have a unique identifier for the relation. (e.g. relations reordering)\n */\n\nexport const encodePolymorphicId = (id: number | string, __type: string) => {\n return `${id}:::${__type}`;\n};\n\nexport const encodePolymorphicRelation = curry(({ idColumn, typeColumn }, relation?: any): any => {\n // Encode the id of the relation and the positional argument if it exist\n const newRelation = {\n ...relation,\n [idColumn]: encodePolymorphicId(relation[idColumn], relation[typeColumn]),\n };\n\n if (relation.position) {\n const { before, after } = relation.position;\n const __type = relation.position.__type || relation.__type;\n\n newRelation.position = { ...relation.position };\n\n if (before) newRelation.position.before = encodePolymorphicId(before, __type);\n if (after) newRelation.position.after = encodePolymorphicId(after, __type);\n }\n\n return newRelation;\n});\n"],"names":["getMorphToManyRowsLinkedToMorphOne","rows","uid","attributeName","typeColumn","db","filter","row","relatedType","name","field","targetAttribute","metadata","get","attributes","target","morphBy","relation","deleteRelatedMorphOneRelationsAfterMorphToManyUpdate","joinTable","transaction","trx","morphColumn","idColumn","morphOneRows","groupByType","groupBy","groupByField","typeAndFieldIdsGrouped","pipe","mapValues","orWhere","type","v","Object","entries","arr","push","$in","map","isEmpty","createQueryBuilder","delete","where","$or","transacting","execute","encodePolymorphicId","id","__type","encodePolymorphicRelation","curry","newRelation","position","before","after"],"mappings":";;;AAAA;AAUA,MAAMA,qCAAqC,CACzCC,IAAAA,EACA,EACEC,GAAG,EACHC,aAAa,EACbC,UAAU,EACVC,EAAE,EAMH,GAEDJ,IAAKK,CAAAA,MAAM,CAAC,CAACC,GAAAA,GAAAA;AACX,QAAA,MAAMC,WAAcD,GAAAA,GAAG,CAACH,UAAAA,CAAWK,IAAI,CAAC;QACxC,MAAMC,KAAAA,GAAQH,IAAIG,KAAK;QAEvB,MAAMC,eAAAA,GAAkBN,GAAGO,QAAQ,CAACC,GAAG,CAACL,WAAAA,CAAAA,CAAaM,UAA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