Skip to content

Commit

Permalink
Add rawClient to new NodePostgresPgClient subtype (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored Aug 10, 2023
2 parents 8a345d2 + 3783f8c commit 4116b60
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 64 deletions.
10 changes: 10 additions & 0 deletions .changeset/tidy-crabs-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"graphile-build-pg": patch
"@dataplan/pg": patch
"postgraphile": patch
---

`@dataplan/pg/adaptors/pg` now adds `rawClient` property which is the underlying
Postgres client for use with `pgTyped`, `zapatos`, and other libraries that can
use a raw postgres client. This is exposed via `NodePostgresPgClient` interface
which is a subtype of `PgClient`.
27 changes: 19 additions & 8 deletions grafast/dataplan-pg/src/adaptors/pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ const $$isSetup = Symbol("isConfiguredForDataplanPg");
*/
const DONT_DISABLE_JIT = process.env.DATAPLAN_PG_DONT_DISABLE_JIT === "1";

export interface NodePostgresPgClient extends PgClient {
rawClient: PoolClient;
}

function newNodePostgresPgClient(
pgClient: pg.PoolClient,
txLevel: number,
alwaysQueue: boolean,
alreadyInTransaction: boolean,
): PgClient {
): NodePostgresPgClient {
let queue: Promise<void> | null = null;
const addToQueue = <T>(callback: () => Promise<T>): Promise<T> => {
const result = queue ? queue.then(callback) : callback();
Expand All @@ -82,6 +86,7 @@ function newNodePostgresPgClient(
return result;
};
return {
rawClient: pgClient,
withTransaction(callback) {
// Transactions always queue; creating queue if need be
return addToQueue(async () => {
Expand Down Expand Up @@ -191,7 +196,7 @@ declare module "pg" {
async function makeNodePostgresWithPgClient_inner<T>(
pgClient: pg.PoolClient,
pgSettings: { [key: string]: string } | null,
callback: (client: PgClient) => T | Promise<T>,
callback: (client: NodePostgresPgClient) => T | Promise<T>,
alwaysQueue: boolean,
alreadyInTransaction: boolean,
) {
Expand Down Expand Up @@ -265,8 +270,11 @@ async function makeNodePostgresWithPgClient_inner<T>(
export function makePgAdaptorWithPgClient(
pool: Pool,
release: () => PromiseOrDirect<void> = () => {},
): WithPgClient {
const withPgClient: WithPgClient = async (pgSettings, callback) => {
): WithPgClient<NodePostgresPgClient> {
const withPgClient: WithPgClient<NodePostgresPgClient> = async (
pgSettings,
callback,
) => {
const pgClient = await pool.connect();
if (!pgClient[$$isSetup]) {
pgClient[$$isSetup] = true;
Expand Down Expand Up @@ -315,9 +323,12 @@ export function makePgAdaptorWithPgClient(
export function makeWithPgClientViaPgClientAlreadyInTransaction(
pgClient: pg.PoolClient,
alreadyInTransaction = false,
): WithPgClient {
): WithPgClient<NodePostgresPgClient> {
const release = () => {};
const withPgClient: WithPgClient = async (pgSettings, callback) => {
const withPgClient: WithPgClient<NodePostgresPgClient> = async (
pgSettings,
callback,
) => {
return makeNodePostgresWithPgClient_inner(
pgClient,
pgSettings,
Expand Down Expand Up @@ -365,7 +376,7 @@ export interface PgAdaptorOptions {
export function createWithPgClient(
options: PgAdaptorOptions,
variant?: "SUPERUSER" | string | null,
): WithPgClient {
): WithPgClient<NodePostgresPgClient> {
if (variant === "SUPERUSER") {
if (options.superuserPool) {
return makePgAdaptorWithPgClient(options.superuserPool);
Expand Down Expand Up @@ -692,7 +703,7 @@ declare global {
pgSettings: {
[key: string]: string;
} | null;
withPgClient: WithPgClient;
withPgClient: WithPgClient<NodePostgresPgClient>;
pgSubscriber: PgSubscriber | null;
}
}
Expand Down
4 changes: 2 additions & 2 deletions grafast/dataplan-pg/src/examples/exampleSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ import type {
PgSelectStep,
WithPgClient,
} from "../";
import type { PgSubscriber } from "../adaptors/pg.js";
import type { NodePostgresPgClient, PgSubscriber } from "../adaptors/pg.js";
import { listOfCodec } from "../codecs.js";
import {
makePgResourceOptions,
Expand Down Expand Up @@ -143,7 +143,7 @@ export function EXPORTABLE<T, TScope extends any[]>(
// This is the actual runtime context; we should not use a global for this.
export interface OurGraphQLContext extends Grafast.Context {
pgSettings: { [key: string]: string };
withPgClient: WithPgClient;
withPgClient: WithPgClient<NodePostgresPgClient>;
pgSubscriber: PgSubscriber;
}

Expand Down
18 changes: 12 additions & 6 deletions grafast/dataplan-pg/src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,29 @@ export interface PgClient {
withTransaction<T>(callback: (client: PgClient) => Promise<T>): Promise<T>;
}

export interface WithPgClient {
export interface WithPgClient<TPgClient extends PgClient = PgClient> {
<T>(
pgSettings: { [key: string]: string } | null,
callback: (client: PgClient) => T | Promise<T>,
callback: (client: TPgClient) => T | Promise<T>,
): Promise<T>;

release?(): PromiseOrDirect<void>;
}

export type PgExecutorContext<TSettings = any> = {
export type PgExecutorContext<
TSettings = any,
TPgClient extends PgClient = PgClient,
> = {
pgSettings: TSettings;
withPgClient: WithPgClient;
withPgClient: WithPgClient<TPgClient>;
};

export type PgExecutorContextPlans<TSettings = any> = {
export type PgExecutorContextPlans<
TSettings = any,
TPgClient extends PgClient = PgClient,
> = {
pgSettings: ExecutableStep<TSettings>;
withPgClient: ExecutableStep<WithPgClient>;
withPgClient: ExecutableStep<WithPgClient<TPgClient>>;
};

export type PgExecutorInput<TInput> = {
Expand Down
11 changes: 0 additions & 11 deletions graphile-build/graphile-build-pg/src/examples/NO_DATA_GATHERING.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@ import sql from "pg-sql2";

import { defaultPreset as graphileBuildPgPreset } from "../index.js";

declare global {
namespace Grafast {
interface Context {
pgSettings: {
[key: string]: string;
} | null;
withPgClient: WithPgClient;
}
}
}

const pool = new Pool({
connectionString: "pggql_test",
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* query.
*/

import type { WithPgClient } from "@dataplan/pg";
import { envelop, useExtendContext, useSchema } from "@envelop/core";
import { useParserCache } from "@envelop/parser-cache";
import { useValidationCache } from "@envelop/validation-cache";
Expand Down Expand Up @@ -42,17 +41,6 @@ import * as ws from "ws";
import { defaultPreset as graphileBuildPgPreset } from "../index.js";
import { getWithPgClientFromPgService } from "../pgServices.js";

declare global {
namespace Grafast {
interface Context {
pgSettings: {
[key: string]: string;
} | null;
withPgClient: WithPgClient;
}
}
}

const pool = new Pool({
connectionString: "pggql_test",
});
Expand Down
12 changes: 0 additions & 12 deletions graphile-build/graphile-build-pg/src/examples/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import "graphile-config";

import type { WithPgClient } from "@dataplan/pg";
import {
defaultPreset as graphileBuildPreset,
QueryQueryPlugin,
Expand All @@ -27,17 +26,6 @@ const DATABASE_SCHEMAS: string[] = ["public", "app_public"];
/* ** ** */
/* ************************************************************************** */

declare global {
namespace Grafast {
interface Context {
pgSettings: {
[key: string]: string;
} | null;
withPgClient: WithPgClient;
}
}
}

export function getPool() {
const pool = new Pool({
connectionString: DATABASE_CONNECTION_STRING,
Expand Down
13 changes: 0 additions & 13 deletions graphile-build/graphile-build-pg/src/examples/webpack.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-restricted-syntax */

import type { WithPgClient } from "@dataplan/pg";
import {
buildInflection,
buildSchema,
Expand All @@ -21,18 +20,6 @@ import { defaultPreset as graphileBuildPgPreset } from "../index.js";
*/
const DEBUG_MODE = true;

// Our server will supply pgSettings/withPgClient on the GraphQL context
declare global {
namespace Grafast {
interface Context {
pgSettings: {
[key: string]: string;
} | null;
withPgClient: WithPgClient;
}
}
}

// Create a pool and add the error handler
const pool = new Pool({
connectionString: "postgres://postgres:unsecured@localhost:6432/chinook",
Expand Down

0 comments on commit 4116b60

Please sign in to comment.