{"version":3,"file":"knex.mjs","sources":["../../src/utils/knex.ts"],"sourcesContent":["import type { Knex } from 'knex';\n\nimport KnexBuilder from 'knex/lib/query/querybuilder';\nimport KnexRaw from 'knex/lib/raw';\n\nimport type { Database } from '..';\n\n/**\n * @internal\n */\nexport function isKnexQuery(value: unknown): value is Knex.Raw | Knex.QueryBuilder {\n  return value instanceof KnexBuilder || value instanceof KnexRaw;\n}\n\n/**\n * Applies the configured `settings.queryTimeout` (milliseconds) to a query so that the database\n * cancels it once the timeout elapses, instead of letting it run to completion while it holds a\n * pooled connection.\n *\n * A no-op when the setting is unset, or when the dialect cannot cancel queries (only the pg and\n * mysql knex clients can; knex throws when `{ cancel: true }` is used on any other client, and the\n * synchronous better-sqlite3 driver would never fire the timer anyway).\n *\n * @internal\n */\nexport function applyQueryTimeout<T extends Knex.Raw | Knex.QueryBuilder>(\n  query: T,\n  db: Database\n): T {\n  const { queryTimeout } = db.config.settings;\n\n  if (queryTimeout === undefined || !canCancelQuery(db)) {\n    return query;\n  }\n\n  return query.timeout(queryTimeout, { cancel: true }) as T;\n}\n\nconst canCancelQuery = (db: Database) =>\n  (db.connection.client as { canCancelQuery?: boolean }).canCancelQuery === true;\n\n/**\n * Adds the name of the schema to the table name if the schema was defined by the user.\n * Users can set the db schema only for Postgres in strapi database config.\n */\nexport const addSchema = (db: Database, tableName: string): string => {\n  const schemaName = db.getSchemaName();\n  return schemaName ? `${schemaName}.${tableName}` : tableName;\n};\n"],"names":["isKnexQuery","value","KnexBuilder","KnexRaw","applyQueryTimeout","query","db","queryTimeout","config","settings","undefined","canCancelQuery","timeout","cancel","connection","client","addSchema","tableName","schemaName","getSchemaName"],"mappings":";;;AAOA;;IAGO,SAASA,WAAAA,CAAYC,KAAc,EAAA;IACxC,OAAOA,KAAAA,YAAiBC,eAAeD,KAAAA,YAAiBE,OAAAA;AAC1D;AAEA;;;;;;;;;;AAUC,IACM,SAASC,iBAAAA,CACdC,KAAQ,EACRC,EAAY,EAAA;AAEZ,IAAA,MAAM,EAAEC,YAAY,EAAE,GAAGD,EAAAA,CAAGE,MAAM,CAACC,QAAQ;AAE3C,IAAA,IAAIF,YAAAA,KAAiBG,SAAAA,IAAa,CAACC,cAAAA,CAAeL,EAAAA,CAAAA,EAAK;QACrD,OAAOD,KAAAA;AACT,IAAA;IAEA,OAAOA,KAAAA,CAAMO,OAAO,CAACL,YAAAA,EAAc;QAAEM,MAAAA,EAAQ;AAAK,KAAA,CAAA;AACpD;AAEA,MAAMF,cAAAA,GAAiB,CAACL,EAAAA,GACtB,EAACA,CAAGQ,UAAU,CAACC,MAAM,CAAkCJ,cAAc,KAAK,IAAA;AAE5E;;;AAGC,IACM,MAAMK,SAAAA,GAAY,CAACV,EAAAA,EAAcW,SAAAA,GAAAA;IACtC,MAAMC,UAAAA,GAAaZ,GAAGa,aAAa,EAAA;AACnC,IAAA,OAAOD,aAAa,CAAA,EAAGA,UAAAA,CAAW,CAAC,EAAED,WAAW,GAAGA,SAAAA;AACrD;;;;"}