{"version":3,"file":"postgres-connection.mjs","sources":["../../../src/postgres-connection.ts"],"sourcesContent":["import pg from 'pg';\nimport {RdbmsConnection} from '@shopify/shopify-app-session-storage';\n\nexport class PostgresConnection implements RdbmsConnection {\n  sessionStorageIdentifier: string;\n  private ready: Promise<void>;\n  private pool: pg.Pool;\n  private dbUrl: URL;\n\n  constructor(dbUrl: string, sessionStorageIdentifier: string) {\n    this.dbUrl = new URL(dbUrl);\n    this.ready = this.init();\n    this.sessionStorageIdentifier = sessionStorageIdentifier;\n  }\n\n  async query(query: string, params: any[] = []): Promise<any[]> {\n    await this.ready;\n    return (await this.pool.query(query, params)).rows;\n  }\n\n  /**\n   * Runs a series of queries in a transaction - requires the use of a SINGLE client,\n   * hence we can't use the query method above.\n   *\n   * @param queries an array of SQL queries to execute in a transaction\n   */\n  async transaction(queries: string[]): Promise<void> {\n    await this.ready;\n\n    // check if the first and last queries are BEGIN and COMMIT, if not, add them\n    if (queries[0] !== 'BEGIN') {\n      queries.unshift('BEGIN');\n    }\n    if (queries[queries.length - 1] !== 'COMMIT') {\n      queries.push('COMMIT');\n    }\n    const client = await this.pool.connect();\n    try {\n      for (const query of queries) {\n        await client.query(query);\n      }\n    } catch (error) {\n      // rollback if any of the queries fail\n      await client.query(`ROLLBACK`);\n      throw error;\n    } finally {\n      client.release();\n    }\n  }\n\n  async disconnect(): Promise<void> {\n    // Since no longer using individual client, use disconnect to reset the pool.\n    await this.ready;\n    await this.pool.end();\n    this.ready = this.init();\n  }\n\n  async connect(): Promise<void> {\n    await this.ready;\n  }\n\n  public getDatabase(): string | undefined {\n    return decodeURIComponent(this.dbUrl.pathname.slice(1));\n  }\n\n  async hasTable(tablename: string): Promise<boolean> {\n    await this.ready;\n    const query = `\n      SELECT EXISTS (\n        SELECT tablename FROM pg_catalog.pg_tables\n          WHERE tablename = ${this.getArgumentPlaceholder(1)}\n      )\n  `;\n\n    // Allow multiple apps to be on the same host with separate DB and querying the right\n    // DB for the session table exisitence\n    const rows = await this.query(query, [tablename]);\n    return rows[0].exists;\n  }\n\n  getArgumentPlaceholder(position: number): string {\n    return `$${position}`;\n  }\n\n  private async init(): Promise<void> {\n    this.pool = new pg.Pool({\n      host: this.dbUrl.hostname,\n      user: decodeURIComponent(this.dbUrl.username),\n      password: decodeURIComponent(this.dbUrl.password),\n      database: this.getDatabase(),\n      port: Number(this.dbUrl.port),\n    });\n  }\n}\n"],"names":[],"mappings":";;MAGa,kBAAkB,CAAA;AAC7B,IAAA,wBAAwB;AAChB,IAAA,KAAK;AACL,IAAA,IAAI;AACJ,IAAA,KAAK;IAEb,WAAA,CAAY,KAAa,EAAE,wBAAgC,EAAA;QACzD,IAAI,CAAC,KAAK,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC;AAC3B,QAAA,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,IAAI,EAAE;AACxB,QAAA,IAAI,CAAC,wBAAwB,GAAG,wBAAwB;IAC1D;AAEA,IAAA,MAAM,KAAK,CAAC,KAAa,EAAE,SAAgB,EAAE,EAAA;QAC3C,MAAM,IAAI,CAAC,KAAK;AAChB,QAAA,OAAO,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,IAAI;IACpD;AAEA;;;;;AAKG;IACH,MAAM,WAAW,CAAC,OAAiB,EAAA;QACjC,MAAM,IAAI,CAAC,KAAK;;AAGhB,QAAA,IAAI,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,EAAE;AAC1B,YAAA,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC;QAC1B;QACA,IAAI,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,QAAQ,EAAE;AAC5C,YAAA,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC;QACxB;QACA,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;AACxC,QAAA,IAAI;AACF,YAAA,KAAK,MAAM,KAAK,I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