Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesj committed Apr 23, 2024
1 parent cd9b654 commit eec829b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 32 deletions.
10 changes: 5 additions & 5 deletions grafast/dataplan-pg/src/adaptors/pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ export function createWithPgClient(
}

// This is here as a TypeScript assertion, to ensure we conform to PgAdaptor
const _testValidAdaptor: PgAdaptor<PgAdaptorSettings, NodePostgresPgClient>["createWithPgClient"] =
const _testValidAdaptor: PgAdaptor<"@dataplan/pg/adaptors/pg">["createWithPgClient"] =
createWithPgClient;

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
Expand Down Expand Up @@ -718,8 +718,8 @@ declare global {
}

export function makePgService(
options: MakePgServiceOptions<PgAdaptorSettings, NodePostgresPgClient> & { pool?: pg.Pool },
): GraphileConfig.PgServiceConfiguration<PgAdaptorSettings, NodePostgresPgClient> {
options: MakePgServiceOptions & { pool?: pg.Pool },
): GraphileConfig.PgServiceConfiguration {
const {
name = "main",
connectionString,
Expand Down Expand Up @@ -760,7 +760,7 @@ export function makePgService(
pgSubscriber = new PgSubscriber(pool);
releasers.push(() => pgSubscriber!.release?.());
}
const service: GraphileConfig.PgServiceConfiguration<PgAdaptorSettings, NodePostgresPgClient> = {
const service: GraphileConfig.PgServiceConfiguration = {
name,
schemas: Array.isArray(schemas) ? schemas : [schemas ?? "public"],
withPgClientKey: withPgClientKey as any,
Expand All @@ -769,7 +769,7 @@ export function makePgService(
pgSettings,
pgSettingsForIntrospection,
pgSubscriber,
adaptor: { createWithPgClient },
adaptor: { createWithPgClient, makePgService },
adaptorSettings: {
pool,
superuserConnectionString,
Expand Down
2 changes: 1 addition & 1 deletion grafast/dataplan-pg/src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export interface PgClient {
withTransaction<T>(callback: (client: this) => Promise<T>): Promise<T>;
}

export interface WithPgClient<TPgClient extends PgClient> {
export interface WithPgClient<TPgClient extends PgClient = PgClient> {
<T>(
pgSettings: { [key: string]: string } | null,
callback: (client: TPgClient) => T | Promise<T>,
Expand Down
20 changes: 15 additions & 5 deletions grafast/dataplan-pg/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { GrafastSubscriber } from "grafast";
import { exportAsMany } from "grafast";

import type {NodePostgresPgClient, PgAdaptorSettings} from "./adaptors/pg.js";
import {
domainOfCodec,
enumCodec,
Expand Down Expand Up @@ -426,15 +427,15 @@ export { version } from "./version.js";

declare global {
namespace GraphileConfig {
interface PgServiceConfiguration<TAdaptorSettings, TPgClient extends PgClient> {
interface PgServiceConfiguration<TAdaptor extends keyof DataplanPg.PgClientByAdaptor = keyof DataplanPg.PgClientByAdaptor> {
name: string;
schemas?: string[];

adaptor: PgAdaptor<TAdaptorSettings, TPgClient>;
adaptorSettings?: TAdaptorSettings;
adaptor: DataplanPg.PgClientByAdaptor[TAdaptor]["adaptor"];
adaptorSettings?: DataplanPg.PgClientByAdaptor[TAdaptor]["adaptorSettings"];

/** The key on 'context' where the withPgClient function will be sourced */
withPgClientKey: KeysOfType<Grafast.Context & object, WithPgClient<TPgClient>>;
withPgClientKey: KeysOfType<Grafast.Context & object, WithPgClient<DataplanPg.PgClientByAdaptor[TAdaptor]["client"]>>;

/** Return settings to set in the session */
pgSettings?: (
Expand Down Expand Up @@ -467,7 +468,7 @@ declare global {
}

interface Preset {
pgServices?: ReadonlyArray<PgServiceConfiguration<unknown, PgClient>>;
pgServices?: ReadonlyArray<PgServiceConfiguration>;
}
}
namespace DataplanPg {
Expand Down Expand Up @@ -504,5 +505,14 @@ declare global {
interface PgCodecAttributeExtensions {}
interface PgRefDefinitionExtensions {}
interface PgCodecRelationExtensions {}

interface PgClientByAdaptor {
"@dataplan/pg/adaptors/pg": {
adaptor: PgAdaptor;
adaptorSettings: PgAdaptorSettings,
client: NodePostgresPgClient;
};
// Add more here via declaration merging
}
}
}
6 changes: 3 additions & 3 deletions grafast/dataplan-pg/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
PgResourceParameter,
PgResourceUnique,
} from "./datasource.js";
import type { PgClient, PgExecutor} from "./executor.js";
import type { PgExecutor} from "./executor.js";
import type { PgDeleteSingleStep } from "./steps/pgDeleteSingle.js";
import type { PgInsertSingleStep } from "./steps/pgInsertSingle.js";
import type { PgSelectSingleStep } from "./steps/pgSelectSingle.js";
Expand Down Expand Up @@ -415,10 +415,10 @@ export type KeysOfType<TObject, TValueType> = {
[key in keyof TObject]: TObject[key] extends TValueType ? key : never;
}[keyof TObject];

export interface MakePgServiceOptions<TAdaptorSettings, TPgClient extends PgClient>
export interface MakePgServiceOptions
extends Partial<
Pick<
GraphileConfig.PgServiceConfiguration<TAdaptorSettings, TPgClient>,
GraphileConfig.PgServiceConfiguration,
| "name"
| "pgSettings"
| "pgSettingsForIntrospection"
Expand Down
34 changes: 20 additions & 14 deletions grafast/dataplan-pg/src/pgServices.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import type { PgClient, WithPgClient } from "./executor.ts";
import type * as pg from "pg";

import type { PgClient, WithPgClient } from "./executor";
import type {MakePgServiceOptions} from "./interfaces";

type PromiseOrDirect<T> = T | PromiseLike<T>;

/** @experimental */
export interface PgAdaptor<TAdaptorSettings, TPgClient extends PgClient> {
export interface PgAdaptor<TAdaptor extends keyof DataplanPg.PgClientByAdaptor = keyof DataplanPg.PgClientByAdaptor> {
createWithPgClient: (
adaptorSettings: TAdaptorSettings | undefined,
adaptorSettings: DataplanPg.PgClientByAdaptor[TAdaptor]["adaptorSettings"] | undefined,
variant?: "SUPERUSER" | null,
) => PromiseOrDirect<WithPgClient<TPgClient>>;
) => PromiseOrDirect<WithPgClient<DataplanPg.PgClientByAdaptor[TAdaptor]["client"]>>;
makePgService: (
options: MakePgServiceOptions & { pool?: pg.Pool },
) => GraphileConfig.PgServiceConfiguration
}

/**
Expand All @@ -27,16 +33,16 @@ interface PgClientBySourceCacheValue {
}

const withPgClientDetailsByConfigCache = new Map<
GraphileConfig.PgServiceConfiguration<any, any>,
GraphileConfig.PgServiceConfiguration,
PromiseOrDirect<PgClientBySourceCacheValue>
>();

/**
* Get or build the 'withPgClient' callback function for a given database
* config, caching it to make future lookups faster.
*/
export function getWithPgClientFromPgService<TAdaptorOptions, TPgClient extends PgClient>(
config: GraphileConfig.PgServiceConfiguration<TAdaptorOptions, TPgClient>,
export function getWithPgClientFromPgService<TPgClient extends PgClient>(
config: GraphileConfig.PgServiceConfiguration,
): PromiseOrDirect<WithPgClient<TPgClient>> {
const existing = withPgClientDetailsByConfigCache.get(config);
if (existing) {
Expand All @@ -60,7 +66,7 @@ export function getWithPgClientFromPgService<TAdaptorOptions, TPgClient extends

const originalWithPgClient = await factory(config.adaptorSettings);
const withPgClient = ((...args) =>
originalWithPgClient.apply(null, args)) as WithPgClient<TPgClient>;
originalWithPgClient.apply(null, args)) as WithPgClient;
const cachedValue: PgClientBySourceCacheValue = {
withPgClient,
retainers: 1,
Expand Down Expand Up @@ -97,10 +103,10 @@ export function getWithPgClientFromPgService<TAdaptorOptions, TPgClient extends
}
}

export async function withPgClientFromPgService<T, TAdaptorSettings, TPgClient extends PgClient>(
config: GraphileConfig.PgServiceConfiguration<TAdaptorSettings, TPgClient>,
export async function withPgClientFromPgService<T>(
config: GraphileConfig.PgServiceConfiguration,
pgSettings: { [key: string]: string } | null,
callback: (client: TPgClient) => T | Promise<T>,
callback: (client: PgClient) => T | Promise<T>,
): Promise<T> {
const withPgClientFromPgService = getWithPgClientFromPgService(config);
const withPgClient = isPromiseLike(withPgClientFromPgService)
Expand All @@ -114,10 +120,10 @@ export async function withPgClientFromPgService<T, TAdaptorSettings, TPgClient e
}

// We don't cache superuser withPgClients
export async function withSuperuserPgClientFromPgService<T, TAdaptorSettings, TPgClient extends PgClient>(
config: GraphileConfig.PgServiceConfiguration<TAdaptorSettings, TPgClient>,
export async function withSuperuserPgClientFromPgService<T>(
config: GraphileConfig.PgServiceConfiguration,
pgSettings: { [key: string]: string } | null,
callback: (client: TPgClient) => T | Promise<T>,
callback: (client: PgClient) => T | Promise<T>,
): Promise<T> {
const withPgClient = await config.adaptor.createWithPgClient(
config.adaptorSettings,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { KeysOfType, PgClient, WithPgClient } from "@dataplan/pg";
import type { KeysOfType, WithPgClient } from "@dataplan/pg";
import {
PgExecutor,
withPgClientFromPgService,
Expand Down Expand Up @@ -66,7 +66,7 @@ declare global {
getIntrospection(): PromiseOrDirect<IntrospectionResults>;
getService(serviceName: string): Promise<{
introspection: Introspection;
pgService: GraphileConfig.PgServiceConfiguration<unknown, PgClient>;
pgService: GraphileConfig.PgServiceConfiguration;
}>;
getExecutorForService(serviceName: string): PgExecutor;

Expand Down Expand Up @@ -229,7 +229,7 @@ declare global {
}

type IntrospectionResults = Array<{
pgService: GraphileConfig.PgServiceConfiguration<unknown>;
pgService: GraphileConfig.PgServiceConfiguration;
introspection: Introspection;
}>;

Expand Down Expand Up @@ -706,7 +706,7 @@ export const PgIntrospectionPlugin: GraphileConfig.Plugin = {

function introspectPgServices(
pgServices:
| ReadonlyArray<GraphileConfig.PgServiceConfiguration<unknown>>
| ReadonlyArray<GraphileConfig.PgServiceConfiguration>
| undefined,
): Promise<IntrospectionResults> {
if (!pgServices) {
Expand Down

0 comments on commit eec829b

Please sign in to comment.