{"version":3,"file":"connection.mjs","sources":["../src/connection.ts"],"sourcesContent":["import knex from 'knex';\nimport type { Knex } from 'knex';\n\nconst clientMap = {\n  sqlite: 'better-sqlite3',\n  mysql: 'mysql2',\n  postgres: 'pg',\n};\n\ntype ClientKind = keyof typeof clientMap;\n\nexport function isDatabaseClientKind(client: unknown): client is ClientKind {\n  return (client as string) in clientMap;\n}\n\nexport const createConnection = (userConfig: Knex.Config, strapiConfig?: Partial<Knex.Config>) => {\n  if (!isDatabaseClientKind(userConfig.client)) {\n    throw new Error(`Unsupported database client ${userConfig.client}`);\n  }\n\n  const knexConfig: Knex.Config = { ...userConfig, client: clientMap[userConfig.client] };\n\n  // initialization code to run upon opening a new connection\n  if (strapiConfig?.pool?.afterCreate) {\n    // copy so the wrapper below is not written into the caller's pool config (shared with strapi.config)\n    knexConfig.pool = { ...knexConfig.pool };\n    // if the user has set their own afterCreate in config, we will replace it and call it\n    const userAfterCreate = knexConfig.pool?.afterCreate;\n    const strapiAfterCreate = strapiConfig.pool.afterCreate;\n    knexConfig.pool.afterCreate = (\n      conn: unknown,\n      done: (err: Error | null | undefined, connection: any) => void\n    ) => {\n      strapiAfterCreate(conn, (err: Error | null | undefined, nativeConn: any) => {\n        if (err) {\n          return done(err, nativeConn);\n        }\n        if (userAfterCreate) {\n          return userAfterCreate(nativeConn, done);\n        }\n        return done(null, nativeConn);\n      });\n    };\n  }\n\n  const instance = knex(knexConfig);\n\n  // knex up to 3.2.6 made `password` non-enumerable on the connection object it was given, which\n  // is the live config object, so it stayed out of JSON.stringify and Object.keys. 3.2.7 clones the\n  // config instead and hides it on the clone only, so do it here. The value is unchanged.\n  const connection = userConfig.connection;\n  if (\n    typeof connection === 'object' &&\n    connection !== null &&\n    Object.prototype.hasOwnProperty.call(connection, 'password')\n  ) {\n    Object.defineProperty(connection, 'password', { enumerable: false });\n  }\n\n  return instance;\n};\n"],"names":["clientMap","sqlite","mysql","postgres","isDatabaseClientKind","client","createConnection","userConfig","strapiConfig","Error","knexConfig","pool","afterCreate","userAfterCreate","strapiAfterCreate","conn","done","err","nativeConn","instance","knex","connection","Object","prototype","hasOwnProperty","call","defineProperty","enumerable"],"mappings":";;AAGA,MAAMA,SAAAA,GAAY;IAChBC,MAAAA,EAAQ,gBAAA;IACRC,KAAAA,EAAO,QAAA;IACPC,QAAAA,EAAU;AACZ,CAAA;AAIO,SAASC,qBAAqBC,MAAe,EAAA;AAClD,IAAA,OAAO,MAACA,IAAqBL,SAAAA;AAC/B;AAEO,MAAMM,gBAAAA,GAAmB,CAACC,UAAAA,EAAyBC,YAAAA,GAAAA;AACxD,IAAA,IAAI,CAACJ,oBAAAA,CAAqBG,UAAAA,CAAWF,MAAM,CAAA,EAAG;AAC5C,QAAA,MAAM,IAAII,KAAAA,CAAM,CAAC,4BAA4B,EAAEF,UAAAA,CAAWF,MAAM,CAAA,CAAE,CAAA;AACpE,IAAA;AAEA,IAAA,MAAMK,UAAAA,GAA0B;AAAE,QAAA,GAAGH,UAAU;AAAEF,QAAAA,MAAAA,EAAQL,SAAS,CAACO,UAAAA,CAAWF,MAAM;AAAE,KAAA;;IAGtF,IAAIG,YAAAA,EAAcG,MAAMC,WAAAA,EAAa;;AAEnCF,QAAAA,UAAAA,CAAWC,IAAI,GAAG;AAAE,YAAA,GAAGD,WAAWC;AAAK,SAAA;;QAEvC,MAAME,eAAAA,GAAkBH,UAAAA,CAAWC,IAAI,EAAEC,WAAAA;AACzC,QAAA,MAAME,iBAAAA,GAAoBN,YAAAA,CAAaG,IAAI,CAACC,WAAW;AACvDF,QAAAA,UAAAA,CAAWC,IAAI,CAACC,WAAW,GAAG,CAC5BG,IAAAA,EACAC,IAAAA,GAAAA;YAEAF,iBAAAA,CAAkBC,IAAAA,EAAM,CAACE,GAAAA,EAA+BC,UAAAA,GAAAA;AACtD,gBAAA,IAAID,GAAAA,EAAK;AACP,oBAAA,OAAOD,KAAKC,GAAAA,EAAKC,UAAAA,CAAAA;AACnB,gBAAA;AACA,gBAAA,IAA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