{"version":3,"sources":["../../src/utilities/transformWhereQuery.ts"],"sourcesContent":["import type { Where } from '../types/index.js'\n\n/**\n * Transforms a basic \"where\" query into a format in which the \"where builder\" can understand.\n * Even though basic queries are valid, we need to hoist them into the \"and\" / \"or\" format.\n * Use this function alongside `validateWhereQuery` to check that for valid queries before transforming.\n * @example\n * Inaccurate: [text][equals]=example%20post\n * Accurate: [or][0][and][0][text][equals]=example%20post\n */\nexport const transformWhereQuery = (whereQuery: Where): Where => {\n if (!whereQuery) {\n return {}\n }\n\n // Check if 'whereQuery' has 'or' field but no 'and'. This is the case for \"correct\" queries\n if (whereQuery.or && !whereQuery.and) {\n return {\n or: whereQuery.or.map((query) => {\n // ...but if the or query does not have an and, we need to add it\n if (!query.and) {\n return {\n and: [query],\n }\n }\n return query\n }),\n }\n }\n\n // Check if 'whereQuery' has 'and' field but no 'or'.\n if (whereQuery.and && !whereQuery.or) {\n return {\n or: [\n {\n and: whereQuery.and,\n },\n ],\n }\n }\n\n // Check if 'whereQuery' has neither 'or' nor 'and'.\n if (!whereQuery.or && !whereQuery.and) {\n return {\n or: [\n {\n and: [whereQuery], // top-level siblings are considered 'and'\n },\n ],\n }\n }\n\n // If 'whereQuery' has 'or' and 'and', just return it as it is.\n return whereQuery\n}\n"],"names":["transformWhereQuery","whereQuery","or","and","map","query"],"mappings":"AAEA;;;;;;;CAOC,GACD,OAAO,MAAMA,sBAAsB,CAACC;IAClC,IAAI,CAACA,YAAY;QACf,OAAO,CAAC;IACV;IAEA,4FAA4F;IAC5F,IAAIA,WAAWC,EAAE,IAAI,CAACD,WAAWE,GAAG,EAAE;QACpC,OAAO;YACLD,IAAID,WAAWC,EAAE,CAACE,GAAG,CAAC,CAACC;gBACrB,iEAAiE;gBACjE,IAAI,CAACA,MAAMF,GAAG,EAAE;oBACd,OAAO;wBACLA,KAAK;4BAACE;yBAAM;oBACd;gBACF;gBACA,OAAOA;YACT;QACF;IACF;IAEA,qDAAqD;IACrD,IAAIJ,WAAWE,GAAG,IAAI,CAACF,WAAWC,EAAE,EAAE;QACpC,OAAO;YACLA,IAAI;gBACF;oBACEC,KAAKF,WAAWE,GAAG;gBACrB;aACD;QACH;IACF;IAEA,oDAAoD;IACpD,IAAI,CAACF,WAAWC,EAAE,IAAI,CAACD,WAAWE,GAAG,EAAE;QACrC,OAAO;YACLD,IAAI;gBACF;oBACEC,KAAK;wBAACF;qBAAW;gBACnB;aACD;QACH;IACF;IAEA,+DAA+D;IAC/D,OAAOA;AACT,EAAC"}