import { executePostgresQuery } from "../pg-query-executor.js";
import { ZQLDatabase } from "../zql-database.js";
class PrismaConnection {
  #client;
  constructor(client) {
    this.#client = client;
  }
  transaction(fn) {
    return this.#client.$transaction(
      (prismaTx) => fn(
        new PrismaInternalTransaction(prismaTx)
      )
    );
  }
}
class PrismaInternalTransaction {
  wrappedTransaction;
  constructor(prismaTx) {
    this.wrappedTransaction = prismaTx;
  }
  runQuery(ast, format, schema, serverSchema) {
    return executePostgresQuery(
      this,
      ast,
      format,
      schema,
      serverSchema
    );
  }
  async query(sql, params) {
    const result = await this.wrappedTransaction.$queryRawUnsafe(
      sql,
      ...params
    );
    return toIterableRows(result);
  }
}
function isIterable(value) {
  return (
    // oxlint-disable-next-line eqeqeq
    value != null && typeof value[Symbol.iterator] === "function"
  );
}
function toIterableRows(result) {
  if (result === null || result === void 0) {
    return [];
  }
  if (Array.isArray(result)) {
    return result;
  }
  if (isIterable(result)) {
    return result;
  }
  return [];
}
function zeroPrisma(schema, client) {
  return new ZQLDatabase(new PrismaConnection(client), schema);
}
export {
  PrismaConnection,
  zeroPrisma
};
//# sourceMappingURL=prisma.js.map
