Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhoule committed Sep 7, 2023
1 parent ecc773b commit ecdb995
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 159 deletions.
187 changes: 81 additions & 106 deletions query-engine/driver-adapters/js/adapter-pg/src/pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,125 +9,100 @@ type StdClient = pg.Pool
type TransactionClient = pg.PoolClient

class PgQueryable<ClientT extends StdClient | TransactionClient>
implements Queryable {
readonly flavour = 'postgres'

constructor(protected readonly client: ClientT) {
}

/**
* Execute a query given as SQL, interpolating the given parameters.
*/
async queryRaw(query: Query): Promise<Result<ResultSet>> {
const tag = '[js::query_raw]'
debug(`${tag} %O`, query)

const { fields, rows: results } = await this.performIO(query)

const columns = fields.map((field) => field.name)
const resultSet: ResultSet = {
columnNames: columns,
columnTypes: fields.map((field) => fieldToColumnType(field.dataTypeID)),
rows: results.map((result) => columns.map((column) => result[column])),
implements Queryable {
readonly flavour = 'postgres'

constructor(protected readonly client: ClientT) {
}

/**
* Execute a query given as SQL, interpolating the given parameters.
*/
async queryRaw(query: Query): Promise<Result<ResultSet>> {
const tag = '[js::query_raw]'
debug(`${tag} %O`, query)

const { fields, rows: results } = await this.performIO(query)

const columns = fields.map((field) => field.name)
const resultSet: ResultSet = {
columnNames: columns,
columnTypes: fields.map((field) => fieldToColumnType(field.dataTypeID)),
rows: results.map((result) => columns.map((column) => result[column])),
}

return { ok: true, value: resultSet }
}

/**
* Execute a query given as SQL, interpolating the given parameters and
* returning the number of affected rows.
* Note: Queryable expects a u64, but napi.rs only supports u32.
*/
async executeRaw(query: Query): Promise<Result<number>> {
const tag = '[js::execute_raw]'
debug(`${tag} %O`, query)

const { rowCount: rowsAffected } = await this.performIO(query)

// Note: `rowsAffected` can sometimes be null (e.g., when executing `"BEGIN"`)
return { ok: true, value: rowsAffected ?? 0 }
}

return { ok: true, value: resultSet }
}

/**
* Execute a query given as SQL, interpolating the given parameters and
* returning the number of affected rows.
* Note: Queryable expects a u64, but napi.rs only supports u32.
*/
async executeRaw(query: Query): Promise<Result<number>> {
const tag = '[js::execute_raw]'
debug(`${tag} %O`, query)

const { rowCount: rowsAffected } = await this.performIO(query)

// Note: `rowsAffected` can sometimes be null (e.g., when executing `"BEGIN"`)
return { ok: true, value: rowsAffected ?? 0 }
}

/**
* Run a query against the database, returning the result set.
* Should the query fail due to a connection error, the connection is
* marked as unhealthy.
*/
private async performIO(query: Query) {
const { sql, args: values } = query

try {
const result = await this.client.query(sql, values)
return result
} catch (e) {
const error = e as Error
debug('Error in performIO: %O', error)
throw error
/**
* Run a query against the database, returning the result set.
* Should the query fail due to a connection error, the connection is
* marked as unhealthy.
*/
private async performIO(query: Query) {
const { sql, args: values } = query

try {
const result = await this.client.query(sql, values)
return result
} catch (e) {
const error = e as Error
debug('Error in performIO: %O', error)
throw error
}
}
}
}

class PgTransaction extends PgQueryable<TransactionClient>
implements Transaction {
constructor(client: pg.PoolClient, readonly options: TransactionOptions) {
super(client)
}
implements Transaction {
constructor(client: pg.PoolClient, readonly options: TransactionOptions) {
super(client)
}

async commit(): Promise<Result<void>> {
debug(`[js::commit]`)
async commit(): Promise<Result<void>> {
debug(`[js::commit]`)

this.client.release()
return Promise.resolve({ ok: true, value: undefined })
}
this.client.release()
return Promise.resolve({ ok: true, value: undefined })
}

async rollback(): Promise<Result<void>> {
debug(`[js::rollback]`)
async rollback(): Promise<Result<void>> {
debug(`[js::rollback]`)

this.client.release()
return Promise.resolve({ ok: true, value: undefined })
}
this.client.release()
return Promise.resolve({ ok: true, value: undefined })
}
}

<<<<<<< HEAD:query-engine/driver-adapters/js/adapter-pg/src/pg.ts
export class PrismaPg extends PgQueryable<StdClient> implements DriverAdapter {
constructor(client: pg.Pool) {
||||||| parent of 792f6fa29f (end-to-end baseline correctness on query()):query-engine/js-connectors/js/pg-js-connector/src/pg.ts
class PrismaPg extends PgQueryable<StdClient> implements Connector {
constructor(config: PrismaPgConfig) {
const { url: connectionString } = config

const client = new pg.Pool({
connectionString,
})

=======
class PrismaPg extends PgQueryable<StdClient> implements Connector {
constructor(config: PrismaPgConfig) {
const { url: connectionString } = config

console.error("Starting a postgres pool")
const client = new pg.Pool({
connectionString,
})

>>>>>>> 792f6fa29f (end-to-end baseline correctness on query()):query-engine/js-connectors/js/pg-js-connector/src/pg.ts
super(client)
}

async startTransaction(): Promise<Result<Transaction>> {
const options: TransactionOptions = {
usePhantomQuery: false,
}
export class PrismaPg extends PgQueryable<StdClient> implements Connector {
async startTransaction(): Promise<Result<Transaction>> {
const options: TransactionOptions = {
usePhantomQuery: false,
}

const tag = '[js::startTransaction]'
debug(`${tag} options: %O`, options)
const tag = '[js::startTransaction]'
debug(`${tag} options: %O`, options)

const connection = await this.client.connect()
return { ok: true, value: new PgTransaction(connection, options) }
}
const connection = await this.client.connect()
return { ok: true, value: new PgTransaction(connection, options) }
}

async close() {
return { ok: true as const, value: undefined }
}
async close() {
return { ok: true as const, value: undefined }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@
"author": "",
"sideEffects": false,
"license": "Apache-2.0",
"dependencies": {
"@jkomyno/prisma-pg-js-connector": "workspace:*",
"tempy": "^3"
}
"dependencies": { "@jkomyno/prisma-adapter-pg": "workspace:*" }
}
2 changes: 1 addition & 1 deletion query-engine/driver-adapters/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "",
"engines": {
"node": ">=16.13",
"pnpm": ">=8.6.7 <9"
"pnpm": ">=8.6.6 <9"
},
"license": "Apache-2.0",
"scripts": {
Expand Down
54 changes: 6 additions & 48 deletions query-engine/driver-adapters/js/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ecdb995

Please sign in to comment.