From 5589d141b1c767ec8f0de770c5b64a2af341039b Mon Sep 17 00:00:00 2001 From: Wessel van der Veen Date: Wed, 4 Oct 2023 12:01:08 +0200 Subject: [PATCH 1/8] rework types --- .gitignore | 2 +- grafast/dataplan-pg/src/codecs.ts | 265 ++++--- grafast/dataplan-pg/src/datasource.ts | 636 ++++++++++------- .../dataplan-pg/src/examples/exampleSchema.ts | 413 +++++++---- .../src/filters/pgBooleanFilter.ts | 7 +- .../dataplan-pg/src/filters/pgClassFilter.ts | 7 +- grafast/dataplan-pg/src/filters/pgOrFilter.ts | 4 +- grafast/dataplan-pg/src/index.ts | 153 +++- grafast/dataplan-pg/src/interfaces.ts | 673 ++++++++++-------- grafast/dataplan-pg/src/pgLocker.ts | 15 +- .../src/steps/pgClassExpression.ts | 29 +- grafast/dataplan-pg/src/steps/pgCondition.ts | 12 +- .../dataplan-pg/src/steps/pgDeleteSingle.ts | 58 +- .../dataplan-pg/src/steps/pgInsertSingle.ts | 80 ++- grafast/dataplan-pg/src/steps/pgSelect.ts | 161 ++--- .../dataplan-pg/src/steps/pgSelectSingle.ts | 149 ++-- grafast/dataplan-pg/src/steps/pgTempTable.ts | 13 +- grafast/dataplan-pg/src/steps/pgUnionAll.ts | 87 +-- .../dataplan-pg/src/steps/pgUpdateSingle.ts | 94 +-- grafast/dataplan-pg/src/steps/toPg.ts | 6 +- .../src/examples/NO_DATA_GATHERING.ts | 2 +- .../graphile-build-pg/src/inputUtils.ts | 18 +- .../graphile-build-pg/src/interfaces.ts | 4 +- .../src/plugins/PgAttributesPlugin.ts | 47 +- .../src/plugins/PgBasicsPlugin.ts | 69 +- .../src/plugins/PgCodecsPlugin.ts | 84 +-- .../src/plugins/PgConditionArgumentPlugin.ts | 10 +- .../plugins/PgConditionCustomFieldsPlugin.ts | 135 ++-- .../plugins/PgConnectionArgOrderByPlugin.ts | 3 +- .../src/plugins/PgCustomTypeFieldPlugin.ts | 84 +-- .../PgFirstLastBeforeAfterArgsPlugin.ts | 24 +- .../PgInterfaceModeUnionAllRowsPlugin.ts | 25 +- .../src/plugins/PgJWTPlugin.ts | 11 +- .../plugins/PgMutationPayloadEdgePlugin.ts | 12 +- .../plugins/PgMutationUpdateDeletePlugin.ts | 98 ++- .../src/plugins/PgNodeIdAttributesPlugin.ts | 22 +- .../src/plugins/PgOrderAllAttributesPlugin.ts | 10 +- .../src/plugins/PgOrderByPrimaryKeyPlugin.ts | 20 +- .../src/plugins/PgOrderCustomFieldsPlugin.ts | 23 +- .../src/plugins/PgPolymorphismPlugin.ts | 63 +- .../src/plugins/PgProceduresPlugin.ts | 192 ++--- .../src/plugins/PgRefsPlugin.ts | 15 +- .../src/plugins/PgRegistryPlugin.ts | 24 +- .../src/plugins/PgRelationsPlugin.ts | 86 +-- .../src/plugins/PgRowByUniquePlugin.ts | 242 +++---- .../src/plugins/PgTableNodePlugin.ts | 18 +- .../src/plugins/PgTablesPlugin.ts | 49 +- graphile-build/graphile-build-pg/src/utils.ts | 4 +- .../graphile-simplify-inflection/src/index.ts | 13 +- .../src/makeAddPgTableConditionPlugin.ts | 10 +- .../src/makeAddPgTableOrderByPlugin.ts | 8 +- .../src/plugins/PgV4BehaviorPlugin.ts | 4 +- .../src/plugins/PgV4InflectionPlugin.ts | 4 +- .../postgraphile/src/presets/relay.ts | 12 +- postgraphile/postgraphile/src/presets/v4.ts | 2 +- .../src/commands/behavior/debug/main.ts | 2 +- 56 files changed, 2406 insertions(+), 1907 deletions(-) diff --git a/.gitignore b/.gitignore index aae85fdc63..1492be3d39 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,4 @@ grafast/dataplan-pg/__tests__/**/*.mermaid.png /Session.vim /WHAT_ARE_YOU_DOING.md contrib -/env +/env \ No newline at end of file diff --git a/grafast/dataplan-pg/src/codecs.ts b/grafast/dataplan-pg/src/codecs.ts index cc35724d9e..3bcdce9c40 100644 --- a/grafast/dataplan-pg/src/codecs.ts +++ b/grafast/dataplan-pg/src/codecs.ts @@ -38,8 +38,14 @@ import { import type { PgExecutor } from "./executor.js"; import { inspect } from "./inspect.js"; import type { + AnyPgCodec, + DefaultPgCodec, PgCodec, + PgCodecAttributeMap, + PgCodecAttributes, + PgCodecAttributesRecord, PgCodecExtensions, + PgCodecFromPostgres, PgCodecPolymorphism, PgDecode, PgEncode, @@ -50,19 +56,59 @@ import type { // PERF: `identity` can be shortcut const identity = (value: T): T => value; -export type PgCodecAttributeViaExplicit = { - relation: string; - attribute: string; +export type PgCodecAttributeViaExplicit< + TRelationName extends string, + TAttribute extends string, +> = { + relation: TRelationName; + attribute: TAttribute; }; -export type PgCodecAttributeVia = string | PgCodecAttributeViaExplicit; +export interface AnyPgCodecAttributeVia extends PgCodecAttributeVia {} +export type DefaultPgCodecAttributeVia = PgCodecAttributeVia; +export type AnyPgCodecAttributeViaRelationName = + U extends PgCodecAttributeVia + ? TRelationName + : never; +export type AnyPgCodecAttributeViaAttribute = U extends PgCodecAttributeVia< + any, + infer TAttributeName +> + ? TAttributeName + : never; +export type PgCodecAttributeVia< + TRelationName extends string, + TAttribute extends string, +> = TRelationName | PgCodecAttributeViaExplicit; /** @deprecated Use DataplanPg.PgCodecAttributeExtensions instead */ export type PgCodecAttributeExtensions = DataplanPg.PgCodecAttributeExtensions; +export type PgCodecAttributeName = U extends PgCodecAttribute< + infer TName, + any, + any +> + ? TName + : never; + +export type PgCodecAttributeCodec = U extends PgCodecAttribute< + any, + infer TCodec, + any +> + ? TCodec + : never; + +export interface DefaultPgCodecAttribute + extends PgCodecAttribute {} +export interface AnyPgCodecAttribute extends PgCodecAttribute {} + export interface PgCodecAttribute< - TCodec extends PgCodec = PgCodec, - TNotNull extends boolean = boolean, + TName extends string, + TCodec extends AnyPgCodec, + TNotNull extends boolean, > { + name: TName; /** * How to translate to/from PG and how to cast. */ @@ -87,7 +133,7 @@ export interface PgCodecAttribute< * If this attribute actually exists on a relation rather than locally, the name * of the (unique) relation this attribute belongs to. */ - via?: PgCodecAttributeVia; + via?: AnyPgCodecAttributeVia; /** * If the attribute exists identically on a relation and locally (e.g. @@ -112,7 +158,7 @@ export interface PgCodecAttribute< * these are all plural relationships. So identicalVia is generally one-way * (except in 1-to-1 relationships). */ - identicalVia?: PgCodecAttributeVia; + identicalVia?: AnyPgCodecAttributeVia; // ENHANCE: can identicalVia be plural? Is that useful? Maybe a attribute that has // multiple foreign key references? @@ -129,14 +175,6 @@ export interface PgCodecAttribute< extensions?: Partial; } -export type PgCodecAttributes< - TCodecMap extends { - [attributeName in string]: PgCodecAttribute; - } = { - [attributeName in string]: PgCodecAttribute; - }, -> = TCodecMap; - /** * Returns a PgCodec for the given builtin Postgres scalar type, optionally * pass the following config: @@ -160,12 +198,12 @@ function t(): < options?: Cast, ) => PgCodec< TName, - undefined, + never, TFromPostgres, TFromJavaScript, - undefined, - undefined, - undefined + never, + never, + never > { return (oid, type, options = {}) => { const { castFromPg, listCastFromPg, fromPg, toPg, isBinary } = options; @@ -174,7 +212,6 @@ function t(): < sqlType: sql.identifier(...type.split(".")), fromPg: fromPg ?? (identity as any), toPg: toPg ?? (identity as any), - attributes: undefined, extensions: { oid: oid }, castFromPg, listCastFromPg, @@ -318,11 +355,21 @@ function recordStringToTuple(value: string): Array { return tuple; } -function realAttributeDefs( +function realAttributeDefs< + TAttributes extends Record, +>( attributes: TAttributes, -): Array<[string, TAttributes[keyof TAttributes]]> { +): Array< + [ + PgCodecAttributeName, + TAttributes[keyof TAttributes], + ] +> { const attributeDefs = Object.entries(attributes) as Array< - [string, TAttributes extends infer U ? U[keyof U] : never] + [ + PgCodecAttributeName, + TAttributes[keyof TAttributes], + ] >; return attributeDefs.filter( ([_attributeName, spec]) => !spec.expression && !spec.via, @@ -336,9 +383,9 @@ function realAttributeDefs( * * @see {@link https://www.postgresql.org/docs/current/rowtypes.html#id-1.5.7.24.6} */ -function makeRecordToSQLRawValue( - attributes: TAttributes, -): PgEncode> { +function makeRecordToSQLRawValue< + TAttributes extends Record, +>(attributes: TAttributes): PgEncode> { const attributeDefs = realAttributeDefs(attributes); return (value) => { const values = attributeDefs.map(([attributeName, spec]) => { @@ -350,19 +397,23 @@ function makeRecordToSQLRawValue( }; } -export type ObjectFromPgCodecAttributes = - { - [attributeName in keyof TAttributes]: TAttributes[attributeName] extends PgCodecAttribute< - infer UCodec, - infer UNonNull - > - ? UCodec extends PgCodec - ? UNonNull extends true - ? Exclude - : UFromJs | null - : never - : never; - }; +export type ObjectFromPgCodecAttributes< + TAttributes extends Record, +> = { + [TCodecAttribute in keyof TAttributes as PgCodecAttributeName< + TAttributes[TCodecAttribute] + >]: TAttributes[TCodecAttribute] extends PgCodecAttribute< + any, + infer UCodec, + infer UNonNull + > + ? UCodec extends PgCodec + ? UNonNull extends true + ? Exclude + : UFromJs | null + : never + : never; +}; /** * Takes a list of attributes and returns a mapping function that takes a @@ -371,7 +422,9 @@ export type ObjectFromPgCodecAttributes = * * @see {@link https://www.postgresql.org/docs/current/rowtypes.html#id-1.5.7.24.6} */ -function makeSQLValueToRecord( +function makeSQLValueToRecord< + TAttributes extends Record, +>( attributes: TAttributes, ): (value: string) => ObjectFromPgCodecAttributes { const attributeDefs = realAttributeDefs(attributes); @@ -390,7 +443,9 @@ function makeSQLValueToRecord( export type PgRecordTypeCodecSpec< TName extends string, - TAttributes extends PgCodecAttributes, + TAttributes extends { + [TAttribute in keyof TAttributes]: Omit; + }, > = { name: TName; executor: PgExecutor; @@ -414,17 +469,28 @@ export type PgRecordTypeCodecSpec< */ export function recordCodec< const TName extends string, - const TAttributes extends PgCodecAttributes, + const TAttributes extends { + [TAttribute in keyof TAttributes]: Omit; + }, + const TCodecAttributes extends { + [TAttribute in keyof TAttributes]: PgCodecAttribute< + TAttribute extends string ? TAttribute : never, + TAttributes[TAttribute]["codec"], + TAttributes[TAttribute]["notNull"] extends boolean + ? TAttributes[TAttribute]["notNull"] + : never + >; + }, >( config: PgRecordTypeCodecSpec, ): PgCodec< TName, - TAttributes, + TCodecAttributes, string, - ObjectFromPgCodecAttributes, - undefined, - undefined, - undefined + ObjectFromPgCodecAttributes, + never, + never, + never > { const { name, @@ -436,13 +502,26 @@ export function recordCodec< isAnonymous = false, executor, } = config; + + ( + Object.entries(attributes) as Array< + [keyof TAttributes, TAttributes[keyof TAttributes]] + > + ).forEach( + ([name, value]) => + (attributes[name] = { + ...value, + name, + }), + ); + return { name, sqlType: identifier, isAnonymous, - fromPg: makeSQLValueToRecord(attributes), - toPg: makeRecordToSQLRawValue(attributes), - attributes, + fromPg: makeSQLValueToRecord(attributes as unknown as TCodecAttributes), + toPg: makeRecordToSQLRawValue(attributes as unknown as TCodecAttributes), + attributes: attributes as unknown as TCodecAttributes, polymorphism, description, extensions, @@ -506,14 +585,14 @@ type CodecWithListCodec< `${TCodec extends PgCodec ? UName : never}[]`, - undefined, + never, string, - TCodec extends PgCodec + TCodec extends PgCodec ? UFromJs[] : any[], TCodec, - undefined, - undefined + never, + never >; }; @@ -530,9 +609,7 @@ type CodecWithListCodec< * @param typeDelim - the delimeter used to separate entries in this list when Postgres stringifies it * @param identifier - a pg-sql2 fragment that represents the name of this type */ -export function listOfCodec< - TInnerCodec extends PgCodec, ->( +export function listOfCodec( listedCodec: TInnerCodec, config?: { description?: string; @@ -544,14 +621,14 @@ export function listOfCodec< `${TInnerCodec extends PgCodec ? UName : never}[]`, - undefined, // Array has no attributes + never, // Array has no attributes string, - TInnerCodec extends PgCodec + TInnerCodec extends PgCodec ? UFromJs[] : any[], TInnerCodec, - undefined, - undefined + never, + never > { const innerCodec: CodecWithListCodec = listedCodec; @@ -570,22 +647,14 @@ export function listOfCodec< `${TInnerCodec extends PgCodec ? UName : never}[]`, - undefined, // Array has no attributes + never, // Array has no attributes string, - TInnerCodec extends PgCodec< - any, - any, - any, - infer UFromJs, - undefined, - any, - any - > + TInnerCodec extends PgCodec ? UFromJs[] : any[], TInnerCodec, - undefined, - undefined + never, + never > = { name: `${ innerCodec.name as TInnerCodec extends PgCodec< @@ -663,7 +732,7 @@ exportAs("@dataplan/pg", listOfCodec, "listOfCodec"); */ export function domainOfCodec< TName extends string, - TInnerCodec extends PgCodec, + TInnerCodec extends AnyPgCodec, >( innerCodec: TInnerCodec, name: TName, @@ -675,13 +744,15 @@ export function domainOfCodec< } = {}, ): PgCodec< TName, - TInnerCodec extends PgCodec ? U : any, - TInnerCodec extends PgCodec ? U : any, - undefined, + PgCodecAttributeMap, + PgCodecFromPostgres, + any, + AnyPgCodec, TInnerCodec, - undefined + any > { const { description, extensions, notNull } = config; + return { // Generally same as underlying type: ...innerCodec, @@ -691,7 +762,7 @@ export function domainOfCodec< sqlType: identifier, description, extensions, - domainOfCodec: innerCodec.arrayOfCodec ? undefined : innerCodec, + ...(innerCodec.arrayOfCodec ? {} : { domainOfCodec: innerCodec }), notNull: Boolean(notNull), }; } @@ -702,9 +773,10 @@ exportAs("@dataplan/pg", domainOfCodec, "domainOfCodec"); * * @internal */ -function escapeRangeValue< - TInnerCodec extends PgCodec, ->(value: null | any, innerCodec: TInnerCodec): string { +function escapeRangeValue( + value: null | any, + innerCodec: TInnerCodec, +): string { if (value == null) { return ""; } @@ -729,15 +801,7 @@ interface PgRange { */ export function rangeOfCodec< TName extends string, - TInnerCodec extends PgCodec< - any, - undefined, - any, - any, - undefined, - any, - undefined - >, + TInnerCodec extends AnyPgCodec, >( innerCodec: TInnerCodec, name: TName, @@ -746,15 +810,7 @@ export function rangeOfCodec< description?: string; extensions?: Partial; } = {}, -): PgCodec< - TName, - undefined, - string, - PgRange, - undefined, - undefined, - TInnerCodec -> { +): PgCodec, never, never, TInnerCodec> { const { description, extensions } = config; const needsCast = innerCodec.castFromPg; @@ -1171,9 +1227,7 @@ export function getCodecByPgCatalogTypeName(pgCatalogTypeName: string) { return null; } -export function getInnerCodec< - TCodec extends PgCodec, ->( +export function getInnerCodec( codec: TCodec, ): TCodec extends PgCodec< any, @@ -1181,7 +1235,8 @@ export function getInnerCodec< any, infer UArray, infer UDomain, - infer URange + infer URange, + any > ? Exclude : TCodec { diff --git a/grafast/dataplan-pg/src/datasource.ts b/grafast/dataplan-pg/src/datasource.ts index c61b7034c2..3d772caf2f 100644 --- a/grafast/dataplan-pg/src/datasource.ts +++ b/grafast/dataplan-pg/src/datasource.ts @@ -1,5 +1,3 @@ -/* eslint-disable graphile-export/export-instances */ -import chalk from "chalk"; import type { GrafastValuesList, ObjectStep } from "grafast"; import { __ValueStep, @@ -13,7 +11,9 @@ import type { SQL } from "pg-sql2"; import sql from "pg-sql2"; import type { - PgCodecAttributes, + AnyPgCodecAttribute, + DefaultPgCodecAttribute, + PgCodecAttributeName, PgCodecAttributeVia, PgCodecAttributeViaExplicit, } from "./codecs.js"; @@ -28,18 +28,29 @@ import type { } from "./executor.js"; import { inspect } from "./inspect.js"; import type { - Expand, - GetPgCodecAttributes, + AnyPgCodecRelationConfig, + PgCodecAttributes, GetPgRegistryCodecRelations, - GetPgRegistryCodecs, PgCodec, - PgCodecRelation, + PgCodecName, PgCodecRelationConfig, - PgCodecWithAttributes, PgRefDefinition, PgRegistry, PgRegistryConfig, PlanByUniques, + AnyPgCodec, + AnyPgRegistry, + GetPgRegistryCodecRelationConfigs, + GetPgRegistryCodecs, + AnyPgRegistryConfig, + AnyScalarPgCodec, + PgCodecRelationConfigName, + PgRegistryConfigCodecs, + PgRegistryConfigRelationConfigs, + PgRegistryConfigResourceOptions, + Expand, + DefaultPgCodec, + DefaultPgCodecRelationConfig, } from "./interfaces.js"; import type { PgClassExpressionStep } from "./steps/pgClassExpression.js"; import type { @@ -54,6 +65,7 @@ import type { PgSelectSinglePlanOptions, PgSelectSingleStep, } from "./steps/pgSelectSingle.js"; +import chalk from "chalk"; export function EXPORTABLE( factory: (...args: TScope) => T, @@ -82,13 +94,17 @@ export type PgResourceExtensions = DataplanPg.PgResourceExtensions; export type PgResourceParameterExtensions = DataplanPg.PgResourceParameterExtensions; +export interface AnyPgResourceParameter extends PgResourceParameter {} +export interface DefaultPgResourceParameter + extends PgResourceParameter {} + /** * If this is a functional (rather than static) resource, this describes one of * the parameters it accepts. */ export interface PgResourceParameter< - TName extends string | null = string | null, - TCodec extends PgCodec = PgCodec, + TName extends string | null, + TCodec extends AnyPgCodec, > { /** * Name of the parameter, if null then we must use positional rather than @@ -111,16 +127,17 @@ export interface PgResourceParameter< extensions?: PgResourceParameterExtensions; } +export interface AnyPgResourceUnique extends PgResourceUnique {} +export interface DefaultPgResourceUnique + extends PgResourceUnique {} /** * Description of a unique constraint on a PgResource. */ -export interface PgResourceUnique< - TAttributes extends PgCodecAttributes = PgCodecAttributes, -> { +export interface PgResourceUnique { /** * The attributes that are unique */ - attributes: ReadonlyArray; + attributes: Array>; /** * If this is true, this represents the "primary key" of the resource. */ @@ -151,19 +168,59 @@ export interface PgCodecRef { export interface PgCodecRefs { [refName: string]: PgCodecRef; } +export interface DefaultPgResourceOptions< + TCodec extends DefaultPgCodec = DefaultPgCodec, +> extends PgResourceOptions< + string, + TCodec, + DefaultPgResourceUnique, + DefaultPgResourceParameter + > {} + +export interface AnyPgResourceOptions + extends PgResourceOptions {} + +export type PgResourceOptionName = U extends PgResourceOptions< + infer TName, + any, + any, + any +> + ? TName + : never; +export type PgResourceOptionCodec = U extends PgResourceOptions< + any, + infer TCodec, + any, + any +> + ? TCodec + : never; +export type PgResourceOptionUniques = U extends PgResourceOptions< + any, + any, + infer TUniques, + any +> + ? TUniques + : never; +export type PgResourceOptionParameters = U extends PgResourceOptions< + any, + any, + any, + infer TParameters +> + ? TParameters + : never; /** * Configuration options for your PgResource */ export interface PgResourceOptions< - TName extends string = string, - TCodec extends PgCodec = PgCodec, - TUniques extends ReadonlyArray< - PgResourceUnique> - > = ReadonlyArray>>, - TParameters extends readonly PgResourceParameter[] | undefined = - | readonly PgResourceParameter[] - | undefined, + TName extends string, + TCodec extends AnyPgCodec, + TUniques extends PgResourceUnique>, + TParameters extends AnyPgResourceParameter, > { /** * The associated codec for this resource @@ -177,18 +234,17 @@ export interface PgResourceOptions< executor: PgExecutor; // TODO: auth should also apply to insert, update and delete, maybe via insertAuth, updateAuth, etc - selectAuth?: ( - $step: PgSelectStep>, - ) => void; + selectAuth?: ($step: PgSelectStep) => void; name: TName; identifier?: string; - from: TParameters extends readonly PgResourceParameter[] - ? (...args: PgSelectArgumentDigest[]) => SQL - : SQL; - uniques?: TUniques; + // see https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types + from: SQL | ((...args: PgSelectArgumentDigest[]) => SQL); + uniques?: [TUniques] extends [never] ? never : ReadonlyArray; extensions?: PgResourceExtensions; - parameters?: TParameters; + parameters?: [TParameters] extends [never] + ? never + : ReadonlyArray; description?: string; /** * Set true if this resource will only return at most one record - this is @@ -213,44 +269,104 @@ export interface PgResourceOptions< isVirtual?: boolean; } +export interface AnyPgFunctionResourceOptions + extends PgFunctionResourceOptions {} +export interface DefaultPgFunctionResourceOptions + extends PgFunctionResourceOptions< + string, + DefaultPgCodec, + DefaultPgResourceUnique, + DefaultPgResourceParameter + > {} + export interface PgFunctionResourceOptions< - TNewName extends string = string, - TCodec extends PgCodec = PgCodec, - TUniques extends ReadonlyArray< - PgResourceUnique> - > = ReadonlyArray>>, - TNewParameters extends - readonly PgResourceParameter[] = readonly PgResourceParameter[], + TNewName extends string, + TCodec extends AnyPgCodec, + TUniques extends PgResourceUnique>, + TNewParameters extends AnyPgResourceParameter, > { name: TNewName; identifier?: string; from: (...args: PgSelectArgumentDigest[]) => SQL; - parameters: TNewParameters; + parameters?: [TNewParameters] extends [never] + ? never + : ReadonlyArray; returnsSetof: boolean; returnsArray: boolean; - uniques?: TUniques; + uniques?: [TUniques] extends [never] ? never : ReadonlyArray; extensions?: PgResourceExtensions; isMutation?: boolean; - selectAuth?: ( - $step: PgSelectStep>, - ) => void; + selectAuth?: ($step: PgSelectStep) => void; description?: string; } +export type PgResourceName = U extends PgResource< + infer TName, + any, + any, + any, + any +> + ? TName + : never; +export type PgResourceCodec = U extends PgResource< + any, + infer TCodec, + any, + any, + any +> + ? TCodec + : never; + +export type PgResourceUniques = U extends PgResource< + any, + any, + infer TUniques, + any, + any +> + ? TUniques + : never; +export type PgResourceParameters = U extends PgResource< + any, + any, + any, + infer TParameters, + any +> + ? TParameters + : never; +export type PgResourceRegistry = U extends PgResource< + any, + any, + any, + any, + infer TRegistry +> + ? TRegistry + : never; +export interface DefaultPgResource + extends PgResource< + string, + DefaultPgCodec, + DefaultPgResourceUnique, + DefaultPgResourceParameter, + any + > {} +export interface AnyPgResource extends PgResource {} +export interface AnyScalarPgResource + extends PgResource {} /** * PgResource represents any resource of SELECT-able data in Postgres: tables, * views, functions, etc. */ export class PgResource< - TName extends string = string, - TCodec extends PgCodec = PgCodec, - TUniques extends ReadonlyArray< - PgResourceUnique> - > = ReadonlyArray>>, - TParameters extends readonly PgResourceParameter[] | undefined = - | readonly PgResourceParameter[] - | undefined, - TRegistry extends PgRegistry = PgRegistry, + TName extends string, + TCodec extends AnyPgCodec, + TUniques extends PgResourceUnique>, + TParameters extends AnyPgResourceParameter, + TRegistry extends AnyPgRegistry, > { public readonly registry: TRegistry; public readonly codec: TCodec; @@ -258,10 +374,10 @@ export class PgResource< public readonly name: TName; public readonly identifier: string; public readonly from: SQL | ((...args: PgSelectArgumentDigest[]) => SQL); - public readonly uniques: TUniques; - private selectAuth?: ( - $step: PgSelectStep>, - ) => void; + public readonly uniques: [TUniques] extends [never] + ? never + : ReadonlyArray; + private selectAuth?: ($step: PgSelectStep) => void; // TODO: make a public interface for this information /** @@ -273,7 +389,9 @@ export class PgResource< */ public sqlPartitionByIndex: SQL | null = null; - public readonly parameters: TParameters; + public readonly parameters: [TParameters] extends [never] + ? never + : ReadonlyArray; public readonly description: string | undefined; public readonly isUnique: boolean; public readonly isMutation: boolean; @@ -291,7 +409,7 @@ export class PgResource< */ public readonly isVirtual: boolean; - public extensions: Partial | undefined; + public extensions: Partial | undefined; /** * @param from - the SQL for the `FROM` clause (without any @@ -329,7 +447,7 @@ export class PgResource< this.identifier = identifier ?? name; this.from = from; this.uniques = uniques ?? ([] as never); - this.parameters = parameters as TParameters; + this.parameters = parameters ?? ([] as never); this.description = description; this.isUnique = !!isUnique; this.sqlPartitionByIndex = sqlPartitionByIndex ?? null; @@ -370,21 +488,21 @@ export class PgResource< * type/relations/etc. */ static alternativeResourceOptions< - TCodec extends PgCodec, - const TNewUniques extends ReadonlyArray< - PgResourceUnique> - >, + TCodec extends AnyPgCodec, + const TNewUniques extends PgResourceUnique>, const TNewName extends string, >( - baseOptions: PgResourceOptions, + baseOptions: PgResourceOptions, overrideOptions: { name: TNewName; identifier?: string; from: SQL; - uniques?: TNewUniques; - extensions?: PgResourceExtensions; + uniques?: [TNewUniques] extends [never] + ? never + : ReadonlyArray; + extensions?: DataplanPg.PgResourceExtensions; }, - ): PgResourceOptions { + ): PgResourceOptions { const { name, identifier, from, uniques, extensions } = overrideOptions; const { codec, executor, selectAuth } = baseOptions; return { @@ -394,7 +512,6 @@ export class PgResource< identifier, from, uniques, - parameters: undefined, extensions, selectAuth, }; @@ -407,11 +524,9 @@ export class PgResource< * type/relations/etc but pull their rows from functions. */ static functionResourceOptions< - TCodec extends PgCodec, - const TNewParameters extends readonly PgResourceParameter[], - const TNewUniques extends ReadonlyArray< - PgResourceUnique> - >, + TCodec extends AnyPgCodec, + const TNewParameters extends AnyPgResourceParameter, + const TNewUniques extends PgResourceUnique>, const TNewName extends string, >( baseOptions: Pick< @@ -518,25 +633,30 @@ export class PgResource< } public getRelation< - TRelationName extends keyof GetPgRegistryCodecRelations, + TRelationName extends PgCodecRelationConfigName< + GetPgRegistryCodecRelationConfigs + >, >( name: TRelationName, ): GetPgRegistryCodecRelations[TRelationName] { return this.getRelations()[name]; } - public resolveVia( - via: PgCodecAttributeVia, - attr: string, - ): PgCodecAttributeViaExplicit { + public resolveVia< + TRelationName extends PgCodecRelationConfigName< + GetPgRegistryCodecRelationConfigs + >, + TAttribute extends string, + >( + via: PgCodecAttributeVia, + attr: TAttribute, + ): PgCodecAttributeViaExplicit { if (!via) { throw new Error("No via to resolve"); } if (typeof via === "string") { // Check - const relation = this.getRelation(via) as unknown as - | PgCodecRelation - | undefined; + const relation = this.getRelation(via); if (!relation) { throw new Error(`Unknown relation '${via}' in ${this}`); } @@ -554,20 +674,20 @@ export class PgResource< // PERF: this needs optimization. public getReciprocal< TOtherCodec extends GetPgRegistryCodecs, - TOtherRelationName extends keyof GetPgRegistryCodecRelations< + TOtherRelationName extends GetPgRegistryCodecRelationConfigs< TRegistry, TOtherCodec - >, + >["name"], >( otherCodec: TOtherCodec, otherRelationName: TOtherRelationName, ): | [ - relationName: keyof GetPgRegistryCodecRelations, - relation: GetPgRegistryCodecRelations< + relationName: GetPgRegistryCodecRelationConfigs< TRegistry, TCodec - >[keyof GetPgRegistryCodecRelations], + >["name"], + relation: GetPgRegistryCodecRelationConfigs, ] | null { if (this.parameters) { @@ -577,10 +697,7 @@ export class PgResource< } const otherRelation = this.registry.pgRelations[otherCodec.name]?.[otherRelationName]; - const relations = this.getRelations() as unknown as Record< - string, - PgCodecRelation - >; + const relations = this.getRelations(); const reciprocal = Object.entries(relations).find( ([_relationName, relation]) => { if (relation.remoteResource.codec !== otherCodec) { @@ -599,16 +716,35 @@ export class PgResource< return true; }, ); + + // TODO: this is still a bit tricky, :-(, but we're almost here! return (reciprocal as [any, any]) || null; } - public get( - spec: PlanByUniques, TUniques>, + public get< + TSelectSinglePlanOptions extends PgSelectSinglePlanOptions, + TSpec extends Expand, TUniques>>, + >( + spec: TSpec, // This is internal, it's an optimisation we can use but you shouldn't. - _internalOptionsDoNotPass?: PgSelectSinglePlanOptions, - ): GetPgCodecAttributes extends PgCodecAttributes - ? PgSelectSingleStep - : PgClassExpressionStep { + _internalOptionsDoNotPass?: TSelectSinglePlanOptions, + ): PgSelectSingleStep; + public get< + TSelectSinglePlanOptions extends PgSelectSinglePlanOptions, + TSpec extends Expand, TUniques>>, + >( + spec: TSpec, + // This is internal, it's an optimisation we can use but you shouldn't. + _internalOptionsDoNotPass?: TSelectSinglePlanOptions, + ): PgClassExpressionStep; + public get< + TSelectSinglePlanOptions extends PgSelectSinglePlanOptions, + TSpec extends Expand, TUniques>>, + >( + spec: TSpec, + // This is internal, it's an optimisation we can use but you shouldn't. + _internalOptionsDoNotPass?: TSelectSinglePlanOptions, + ): PgClassExpressionStep | PgSelectSingleStep { if (this.parameters) { throw new Error( ".get() cannot be used with functional resources; please use .execute()", @@ -617,12 +753,10 @@ export class PgResource< if (!spec) { throw new Error(`Cannot ${this}.get without a valid spec`); } - const keys = Object.keys(spec) as ReadonlyArray as ReadonlyArray< - keyof GetPgCodecAttributes - >; + const keys = Object.keys(spec); if ( - !this.uniques.some((uniq) => - uniq.attributes.every((key) => keys.includes(key as any)), + !this.uniques.some((uniq: AnyPgResourceUnique) => + uniq.attributes.every((key) => keys.includes(key)), ) ) { throw new Error( @@ -633,12 +767,13 @@ export class PgResource< )}). Did you mean to call .find() instead?`, ); } - return this.find(spec).single(_internalOptionsDoNotPass) as any; + + return this.find(spec).single(_internalOptionsDoNotPass); } public find( spec: { - [key in keyof GetPgCodecAttributes]?: + [attribute in PgCodecAttributes as PgCodecAttributeName]?: | ExecutableStep | string | number; @@ -652,10 +787,10 @@ export class PgResource< if (!this.codec.attributes) { throw new Error("Cannot call find if there's no attributes"); } - const attributes = this.codec.attributes as NonNullable< - GetPgCodecAttributes + const attributes = this.codec.attributes; + const keys = Object.keys(spec) as Array< + PgCodecAttributeName> >; - const keys = Object.keys(spec); /* as Array*/ const invalidKeys = keys.filter((key) => attributes[key] == null); if (invalidKeys.length > 0) { throw new Error( @@ -696,7 +831,7 @@ export class PgResource< matches: (alias: SQL) => typeof attribute.expression === "function" ? attribute.expression(alias) - : sql`${alias}.${sql.identifier(key as string)}`, + : sql`${alias}.${sql.identifier(key)}`, }; }); return pgSelect({ resource: this, identifiers }); @@ -797,7 +932,9 @@ export class PgResource< // Every column in a primary key is non-nullable; so just see if one is null const pk = this.uniques.find((u) => u.isPrimary); const nonNullableAttribute = this.codec.attributes - ? Object.entries(this.codec.attributes).find( + ? Object.entries( + this.codec.attributes as Record, + ).find( ([_attributeName, spec]) => !spec.via && !spec.expression && spec.notNull, )?.[0] @@ -822,69 +959,46 @@ export class PgResource< } exportAs("@dataplan/pg", PgResource, "PgResource"); +export interface AnyPgRegistryBuilder + extends PgRegistryBuilder {} +export interface EmptyRegistryBuilder + extends PgRegistryBuilder {} +export interface DefaultRegistryBuilder + extends PgRegistryBuilder< + DefaultPgCodec, + DefaultPgResourceOptions, + DefaultPgCodecRelationConfig + > {} + export interface PgRegistryBuilder< - TCodecs extends { - [name in string]: PgCodec< - name, - PgCodecAttributes | undefined, - any, - any, - any, - any, - any - >; - }, - TResources extends { - [name in string]: PgResourceOptions< - name, - PgCodec, - ReadonlyArray>, - readonly PgResourceParameter[] | undefined - >; - }, - TRelations extends { - [codecName in keyof TCodecs]?: { - [relationName in string]: PgCodecRelationConfig< - PgCodec, - PgResourceOptions - >; - }; - }, + TCodecs extends AnyPgCodec, + TResourceOptions extends AnyPgResourceOptions, + TRelationConfigs extends AnyPgCodecRelationConfig, > { getRegistryConfig(): PgRegistryConfig< - Expand, - Expand, - Expand + TCodecs, + TResourceOptions, + TRelationConfigs >; - addCodec( + addCodec( codec: TCodec, - ): PgRegistryBuilder< - TCodecs & { - [name in TCodec["name"]]: TCodec; - }, - TResources, - TRelations - >; + ): PgRegistryBuilder; - addResource>( - resource: TResource, + addResource( + resource: TResourceOption, ): PgRegistryBuilder< - TCodecs & { - [name in TResource["codec"]["name"]]: TResource["codec"]; - }, - TResources & { - [name in TResource["name"]]: TResource; - }, - TRelations + TCodecs | PgResourceOptionCodec, + TResourceOptions | TResourceOption, + TRelationConfigs >; addRelation< - TCodec extends PgCodec, + TCodec extends AnyPgCodec, const TCodecRelationName extends string, - const TRemoteResource extends PgResourceOptions, + const TRemoteResource extends AnyPgResourceOptions, const TCodecRelation extends Omit< - PgCodecRelationConfig, - "localCodec" | "remoteResourceOptions" + PgCodecRelationConfig, + "localCodec" | "remoteResourceOptions" | "name" >, >( codec: TCodec, @@ -893,52 +1007,32 @@ export interface PgRegistryBuilder< relation: TCodecRelation, ): PgRegistryBuilder< TCodecs, - TResources, - TRelations & { - [codecName in TCodec["name"]]: { - [relationName in TCodecRelationName]: TCodecRelation & { - localCodec: TCodec; - remoteResourceOptions: TRemoteResource; - }; - }; - } + TResourceOptions, + | TRelationConfigs + | (TCodecRelation & { + name: TCodecRelationName; + localCodec: TCodec; + remoteResourceOptions: TRemoteResource; + }) >; - build(): PgRegistry, Expand, Expand>; + build(): PgRegistry; } export function makeRegistry< - TCodecs extends { - [name in string]: PgCodec< - name, - PgCodecAttributes | undefined, - any, - any, - any, - any, - any - >; - }, - TResourceOptions extends { - [name in string]: PgResourceOptions< - name, - PgCodec, - ReadonlyArray>>, - readonly PgResourceParameter[] | undefined - >; - }, - TRelations extends { - [codecName in keyof TCodecs]?: { - [relationName in string]: PgCodecRelationConfig< - PgCodec, - PgResourceOptions - >; - }; - }, + TConfig extends PgRegistryConfig< + any, + PgResourceOptions, + AnyPgCodecRelationConfig + >, >( - config: PgRegistryConfig, -): PgRegistry { - const registry: PgRegistry = { + config: TConfig, +): PgRegistry< + PgRegistryConfigCodecs, + PgRegistryConfigResourceOptions, + PgRegistryConfigRelationConfigs +> { + const registry: AnyPgRegistry = { pgCodecs: Object.create(null) as any, pgResources: Object.create(null) as any, pgRelations: Object.create(null) as any, @@ -948,24 +1042,24 @@ export function makeRegistry< Object.defineProperties(registry.pgCodecs, { $exporter$args: { value: [registry] }, $exporter$factory: { - value: (registry: PgRegistry) => registry.pgCodecs, + value: (registry: AnyPgRegistry) => registry.pgCodecs, }, }); Object.defineProperties(registry.pgResources, { $exporter$args: { value: [registry] }, $exporter$factory: { - value: (registry: PgRegistry) => registry.pgResources, + value: (registry: AnyPgRegistry) => registry.pgResources, }, }); Object.defineProperties(registry.pgRelations, { $exporter$args: { value: [registry] }, $exporter$factory: { - value: (registry: PgRegistry) => registry.pgRelations, + value: (registry: AnyPgRegistry) => registry.pgRelations, }, }); let addCodecForbidden = false; - function addCodec(codec: PgCodec): PgCodec { + function addCodec(codec: TCodec): TCodec { if (addCodecForbidden) { throw new Error(`It's too late to call addCodec now`); } @@ -977,19 +1071,22 @@ export function makeRegistry< new: codec, }); throw new Error( - `Codec named '${codecName}' is already registsred; you cannot have two codecs with the same name`, + `Codec named '${codecName}' is already registered; you cannot have two codecs with the same name`, ); } return codec; } else if ((codec as any).$$export || (codec as any).$exporter$factory) { - registry.pgCodecs[codecName as keyof TCodecs] = codec as any; + registry.pgCodecs[codecName] = codec; return codec; } else { // Custom spec, pin it back to the registry - registry.pgCodecs[codecName as keyof TCodecs] = codec as any; + registry.pgCodecs[codecName] = codec; if (codec.attributes) { - const prevCols = codec.attributes as PgCodecAttributes; + const prevCols = codec.attributes as Record< + string, + AnyPgCodecAttribute + >; for (const col of Object.values(prevCols)) { addCodec(col.codec); } @@ -1008,7 +1105,7 @@ export function makeRegistry< Object.defineProperties(codec, { $exporter$args: { value: [registry, codecName] }, $exporter$factory: { - value: (registry: PgRegistry, codecName: string) => + value: (registry: AnyPgRegistry, codecName: string) => registry.pgCodecs[codecName], }, }); @@ -1024,20 +1121,18 @@ export function makeRegistry< addCodec(codecSpec); } - for (const [resourceName, rawConfig] of Object.entries( - config.pgResources, - ) as [keyof TResourceOptions, PgResourceOptions][]) { - const resourceConfig = { + for (const [resourceName, rawConfig] of Object.entries(config.pgResources)) { + const resourceConfig: AnyPgResourceOptions = { ...rawConfig, codec: addCodec(rawConfig.codec), parameters: rawConfig.parameters - ? (rawConfig.parameters as readonly PgResourceParameter[]).map((p) => ({ + ? rawConfig.parameters.map((p) => ({ ...p, codec: addCodec(p.codec), })) : rawConfig.parameters, }; - const resource = new PgResource(registry, resourceConfig) as any; + const resource = new PgResource(registry, resourceConfig); // This is the magic that breaks the circular reference: rather than // building PgResource via a factory we tell the system to just retrieve it @@ -1045,7 +1140,7 @@ export function makeRegistry< Object.defineProperties(resource, { $exporter$args: { value: [registry, resourceName] }, $exporter$factory: { - value: (registry: PgRegistry, resourceName: string) => + value: (registry: AnyPgRegistry, resourceName: string) => registry.pgResources[resourceName], }, }); @@ -1054,19 +1149,13 @@ export function makeRegistry< } // Ensure all the relation codecs are also added - for (const codecName of Object.keys( - config.pgRelations, - ) as (keyof typeof config.pgRelations)[]) { + for (const codecName of Object.keys(config.pgRelations)) { const relations = config.pgRelations[codecName]; if (!relations) { continue; } - for (const relationName of Object.keys( - relations, - ) as (keyof typeof relations)[]) { - const relationConfig = relations![ - relationName - ] as unknown as PgCodecRelationConfig; + for (const relationName of Object.keys(relations)) { + const relationConfig = relations![relationName]; if (relationConfig) { addCodec(relationConfig.localCodec); } @@ -1083,11 +1172,11 @@ export function makeRegistry< * remove the ones that already have resources, then we build resources for the * remainder. */ - const tableLikeCodecsWithoutTableLikeResources = new Set(); - const walkCodec = ( - codec: PgCodec, + const tableLikeCodecsWithoutTableLikeResources = new Set(); + const walkCodec = ( + codec: TCodec, isAccessibleViaAttribute = false, - seen = new Set(), + seen = new Set(), ) => { if (seen.has(codec)) { return; @@ -1102,7 +1191,9 @@ export function makeRegistry< tableLikeCodecsWithoutTableLikeResources.add(codec); } if (codec.attributes) { - for (const col of Object.values(codec.attributes)) { + for (const col of Object.values( + codec.attributes as Record, + )) { if (isAccessibleViaAttribute) { walkCodec(col.codec, isAccessibleViaAttribute, seen); } else { @@ -1136,12 +1227,11 @@ export function makeRegistry< // Now add resources for the table-like codecs that don't have them already for (const codec of tableLikeCodecsWithoutTableLikeResources) { if (codec.executor) { - const resourceName = `frmcdc_${codec.name}` as keyof TResourceOptions & - string; + const resourceName = `frmcdc_${codec.name}`; const resource = new PgResource(registry, { name: resourceName, executor: codec.executor, - from: sql`(select 1/0 /* codec-only resource; should not select directly */)`, + from: sql`(select 1/0 /* codec-only resource; should not select directly */)` as any, codec, identifier: resourceName, isVirtual: true, @@ -1150,12 +1240,12 @@ export function makeRegistry< behavior: "-*", }, }, - }) as any; + }); Object.defineProperties(resource, { $exporter$args: { value: [registry, resourceName] }, $exporter$factory: { - value: (registry: PgRegistry, resourceName: string) => + value: (registry: AnyPgRegistry, resourceName: string) => registry.pgResources[resourceName], }, }); @@ -1164,9 +1254,7 @@ export function makeRegistry< } } - for (const codecName of Object.keys( - config.pgRelations, - ) as (keyof typeof config.pgRelations)[]) { + for (const codecName of Object.keys(config.pgRelations)) { const relations = config.pgRelations[codecName]; if (!relations) { continue; @@ -1178,34 +1266,30 @@ export function makeRegistry< Object.defineProperties(builtRelations, { $exporter$args: { value: [registry, codecName] }, $exporter$factory: { - value: (registry: PgRegistry, codecName: string) => + value: (registry: AnyPgRegistry, codecName: string) => registry.pgRelations[codecName], }, }); - for (const relationName of Object.keys( - relations, - ) as (keyof typeof relations)[]) { - const relationConfig = relations![ - relationName - ] as unknown as PgCodecRelationConfig; + for (const relationName of Object.keys(relations)) { + const relationConfig = relations[relationName]; if (!relationConfig) { continue; } const { localCodec, remoteResourceOptions, ...rest } = relationConfig; const builtRelation = { - ...(rest as any), + ...rest, localCodec, remoteResource: registry.pgResources[remoteResourceOptions.name], - } as PgCodecRelation; + }; // Tell the system to read the built relation from the registry Object.defineProperties(builtRelation, { $exporter$args: { value: [registry, codecName, relationName] }, $exporter$factory: { value: ( - registry: PgRegistry, + registry: AnyPgRegistry, codecName: string, relationName: string, ) => registry.pgRelations[codecName][relationName], @@ -1224,16 +1308,24 @@ export function makeRegistry< } exportAs("@dataplan/pg", makeRegistry, "makeRegistry"); -function validateRelations(registry: PgRegistry): void { +function validateRelations< + TCodecs extends AnyPgCodec, + TResourceOptions extends AnyPgResourceOptions, + TRelationConfigs extends AnyPgCodecRelationConfig, +>(registry: PgRegistry): void { // PERF: skip this if not isDev? - const reg = registry as PgRegistry; + const reg = registry; - for (const codec of Object.values(reg.pgCodecs)) { + for (const codec of Object.values(reg.pgCodecs) as Array) { // Check that all the `via` and `identicalVia` match actual relations. - const relationKeys = Object.keys(reg.pgRelations[codec.name] ?? {}); + const relationKeys = Object.keys( + reg.pgRelations[codec.name as PgCodecName] ?? {}, + ); if (codec.attributes) { - Object.entries(codec.attributes).forEach(([attributeName, col]) => { + Object.entries( + codec.attributes as Record, + ).forEach(([attributeName, col]) => { const { via, identicalVia } = col; if (via != null) { if (typeof via === "string") { @@ -1270,15 +1362,14 @@ function validateRelations(registry: PgRegistry): void { } } -// eslint-disable-next-line @typescript-eslint/ban-types -export function makeRegistryBuilder(): PgRegistryBuilder<{}, {}, {}> { - const registryConfig: PgRegistryConfig = { +export function makeRegistryBuilder(): EmptyRegistryBuilder { + const registryConfig: AnyPgRegistryConfig = { pgCodecs: Object.create(null), pgResources: Object.create(null), pgRelations: Object.create(null), }; - const builder: PgRegistryBuilder = { + const builder: AnyPgRegistryBuilder = { getRegistryConfig() { return registryConfig; }, @@ -1306,7 +1397,9 @@ export function makeRegistryBuilder(): PgRegistryBuilder<{}, {}, {}> { this.addCodec(codec.rangeOfCodec); } if (codec.attributes) { - for (const col of Object.values(codec.attributes)) { + for (const col of Object.values( + codec.attributes as Record, + )) { this.addCodec(col.codec); } } @@ -1314,9 +1407,7 @@ export function makeRegistryBuilder(): PgRegistryBuilder<{}, {}, {}> { }, addResource(resource) { - const existing = registryConfig.pgResources[resource.name] as - | PgResourceOptions - | undefined; + const existing = registryConfig.pgResources[resource.name]; if (existing) { if (existing !== resource) { throw new Error( @@ -1353,11 +1444,9 @@ export function makeRegistryBuilder(): PgRegistryBuilder<{}, {}, {}> { registryConfig.pgRelations[localCodec.name][relationName] = { localCodec, remoteResourceOptions, + name: relationName, ...relation, - } as PgCodecRelationConfig< - PgCodecWithAttributes, - PgResourceOptions - >; + }; return builder; }, @@ -1374,14 +1463,29 @@ export function makeRegistryBuilder(): PgRegistryBuilder<{}, {}, {}> { exportAs("@dataplan/pg", makeRegistryBuilder, "makeRegistryBuilder"); export function makePgResourceOptions< - const TResourceOptions extends PgResourceOptions, ->(options: TResourceOptions) { + const TName extends string, + const TCodec extends AnyPgCodec, + const TAttributes extends PgCodecAttributes, + const TUniques extends PgResourceUnique< + TAttributes[keyof TAttributes] + > = never, + const TParameters extends AnyPgResourceParameter = never, +>( + options: PgResourceOptions, +): PgResourceOptions { return options; } exportAs("@dataplan/pg", makePgResourceOptions, "makePgResourceOptions"); -function printResourceFrom(resource: PgResourceOptions): string { +function printResourceFrom< + TResource extends PgResourceOptions< + any, + AnyPgCodec, + any, + AnyPgResourceParameter + >, +>(resource: TResource): string { if (typeof resource.from === "function") { return `a function accepting ${resource.parameters ?.length} parameters and returning SQL type '${ diff --git a/grafast/dataplan-pg/src/examples/exampleSchema.ts b/grafast/dataplan-pg/src/examples/exampleSchema.ts index 742f209105..1187d18abd 100644 --- a/grafast/dataplan-pg/src/examples/exampleSchema.ts +++ b/grafast/dataplan-pg/src/examples/exampleSchema.ts @@ -69,7 +69,6 @@ import sql from "pg-sql2"; import { inspect } from "util"; import type { - GetPgResourceRelations, PgCodecAttribute, PgCodecAttributeVia, PgConditionStep, @@ -79,11 +78,15 @@ import type { WithPgClient, } from "../"; import type { NodePostgresPgClient, PgSubscriber } from "../adaptors/pg.js"; -import { listOfCodec } from "../codecs.js"; +import { listOfCodec, PgCodecAttributeName } from "../codecs.js"; import { + AnyPgResource, makePgResourceOptions, makeRegistry, makeRegistryBuilder, + PgResourceCodec, + PgResourceName, + PgResourceUniques, } from "../datasource.js"; import { enumCodec, @@ -107,7 +110,16 @@ import { recordCodec, TYPES, } from "../index.js"; -import type { GetPgResourceAttributes, PgCodec } from "../interfaces"; +import type { + AnyPgCodec, + GetPgResourceAttributeMap, + GetPgResourceAttributes, + GetPgResourceRelationConfigs, + PgCodec, + PgCodecAttributeMap, + PgCodecAttributes, + PgCodecRelationConfigName, +} from "../interfaces"; import { PgPageInfoStep } from "../steps/pgPageInfo.js"; import type { PgPolymorphicTypeMap } from "../steps/pgPolymorphic.js"; import type { PgSelectParsedCursorStep } from "../steps/pgSelect.js"; @@ -118,6 +130,10 @@ import { WithPgClientStep, withPgClientTransaction, } from "../steps/withPgClient.js"; +import { + AnyPgSelectSingleStep, + PgSelectSingleStepResource, +} from "../steps/pgSelectSingle"; const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); @@ -197,19 +213,28 @@ export function makeExampleSchema( ) => { const col = < TOptions extends { - codec: PgCodec; + codec: AnyPgCodec; notNull?: boolean; - expression?: PgCodecAttribute["expression"]; - via?: PgCodecAttributeVia; - identicalVia?: PgCodecAttributeVia; + expression?: PgCodecAttribute["expression"]; + via?: PgCodecAttributeVia; + identicalVia?: PgCodecAttributeVia; }, >( options: TOptions, - ): PgCodecAttribute => { + ): PgCodecAttribute< + any, + TOptions extends { codec: infer U } ? U : never, + TOptions extends { notNull: infer U } + ? U extends boolean + ? U + : false + : false + > => { const { notNull, codec, expression, via, identicalVia } = options; return { + name: undefined, codec: codec as TOptions extends { codec: infer U } ? U : never, - notNull: !!notNull, + notNull: !!notNull as any, expression, via, identicalVia, @@ -294,7 +319,6 @@ export function makeExampleSchema( from: (...args) => sql`app_public.forum_names_array(${sqlFromArgDigests(args)})`, name: "forum_names_array", - parameters: [], isUnique: true, // No setof }); @@ -305,7 +329,6 @@ export function makeExampleSchema( from: (...args) => sql`app_public.forum_names_cases(${sqlFromArgDigests(args)})`, name: "forum_names_cases", - parameters: [], }); const forumsUniqueAuthorCountResourceOptions = makePgResourceOptions({ @@ -394,7 +417,6 @@ export function makeExampleSchema( sql`app_public.featured_messages(${sqlFromArgDigests(args)})`, returnsSetof: true, returnsArray: false, - parameters: [], }); const forumsFeaturedMessagesResourceOptions = @@ -423,7 +445,6 @@ export function makeExampleSchema( sql`app_public.random_user_array(${sqlFromArgDigests(args)})`, returnsArray: true, returnsSetof: false, - parameters: [], }, ); @@ -434,7 +455,6 @@ export function makeExampleSchema( sql`app_public.random_user_array_set(${sqlFromArgDigests(args)})`, returnsSetof: true, returnsArray: true, - parameters: [], }); const forumsMessagesListSetResourceOptions = @@ -444,7 +464,6 @@ export function makeExampleSchema( sql`app_public.forums_messages_list_set(${sqlFromArgDigests( args, )})`, - parameters: [], returnsArray: true, returnsSetof: true, extensions: { @@ -726,7 +745,7 @@ export function makeExampleSchema( const itemAttributes = { id: col({ codec: TYPES.int, notNull: true, identicalVia: "item" }), - type: col({ codec: TYPES.text, notNull: true, via: "item" }), + type: col({ codec: itemTypeEnumCodec, notNull: true, via: "item" }), type2: col({ codec: enumTableItemTypeEnumCodec, notNull: true, @@ -1291,8 +1310,8 @@ export function makeExampleSchema( "item", relationalItemsResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, }, ) @@ -1301,14 +1320,14 @@ export function makeExampleSchema( "parent", relationalItemsResourceOptions, { - localAttributes: [`parent_id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`parent_id`], + remoteAttributes: [`id`], isUnique: true, }, ) .addRelation(relationalTopicsCodec, "author", personResourceOptions, { - localAttributes: [`author_id`] as const, - remoteAttributes: [`person_id`] as const, + localAttributes: [`author_id`], + remoteAttributes: [`person_id`], isUnique: true, }) @@ -1317,8 +1336,8 @@ export function makeExampleSchema( "item", relationalItemsResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, }, ) @@ -1327,14 +1346,14 @@ export function makeExampleSchema( "parent", relationalItemsResourceOptions, { - localAttributes: [`parent_id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`parent_id`], + remoteAttributes: [`id`], isUnique: true, }, ) .addRelation(relationalPostsCodec, "author", personResourceOptions, { - localAttributes: [`author_id`] as const, - remoteAttributes: [`person_id`] as const, + localAttributes: [`author_id`], + remoteAttributes: [`person_id`], isUnique: true, }) .addRelation( @@ -1342,8 +1361,8 @@ export function makeExampleSchema( "commentable", relationalCommentableResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, }, ) @@ -1353,8 +1372,8 @@ export function makeExampleSchema( "item", relationalItemsResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, }, ) @@ -1363,14 +1382,14 @@ export function makeExampleSchema( "parent", relationalItemsResourceOptions, { - localAttributes: [`parent_id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`parent_id`], + remoteAttributes: [`id`], isUnique: true, }, ) .addRelation(relationalDividersCodec, "author", personResourceOptions, { - localAttributes: [`author_id`] as const, - remoteAttributes: [`person_id`] as const, + localAttributes: [`author_id`], + remoteAttributes: [`person_id`], isUnique: true, }) .addRelation( @@ -1378,8 +1397,8 @@ export function makeExampleSchema( "item", relationalItemsResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, }, ) @@ -1388,8 +1407,8 @@ export function makeExampleSchema( "parent", relationalItemsResourceOptions, { - localAttributes: [`parent_id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`parent_id`], + remoteAttributes: [`id`], isUnique: true, }, ) @@ -1398,8 +1417,8 @@ export function makeExampleSchema( "author", personResourceOptions, { - localAttributes: [`author_id`] as const, - remoteAttributes: [`person_id`] as const, + localAttributes: [`author_id`], + remoteAttributes: [`person_id`], isUnique: true, }, ) @@ -1408,8 +1427,8 @@ export function makeExampleSchema( "commentable", relationalCommentableResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, }, ) @@ -1418,8 +1437,8 @@ export function makeExampleSchema( "item", relationalItemsResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, }, ) @@ -1428,8 +1447,8 @@ export function makeExampleSchema( "parent", relationalItemsResourceOptions, { - localAttributes: [`parent_id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`parent_id`], + remoteAttributes: [`id`], isUnique: true, }, ) @@ -1438,8 +1457,8 @@ export function makeExampleSchema( "author", personResourceOptions, { - localAttributes: [`author_id`] as const, - remoteAttributes: [`person_id`] as const, + localAttributes: [`author_id`], + remoteAttributes: [`person_id`], isUnique: true, }, ) @@ -1448,8 +1467,8 @@ export function makeExampleSchema( "commentable", relationalCommentableResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, }, ) @@ -1460,8 +1479,8 @@ export function makeExampleSchema( relationalItemsResourceOptions, { isUnique: true, - localAttributes: ["parent_id"] as const, - remoteAttributes: ["id"] as const, + localAttributes: ["parent_id"], + remoteAttributes: ["id"], }, ) .addRelation( @@ -1470,22 +1489,22 @@ export function makeExampleSchema( relationalItemsResourceOptions, { isUnique: false, - localAttributes: ["id"] as const, - remoteAttributes: ["parent_id"] as const, + localAttributes: ["id"], + remoteAttributes: ["parent_id"], }, ) .addRelation(relationalItemsCodec, "author", personResourceOptions, { isUnique: true, - localAttributes: ["author_id"] as const, - remoteAttributes: ["person_id"] as const, + localAttributes: ["author_id"], + remoteAttributes: ["person_id"], }) .addRelation( relationalItemsCodec, "topic", relationalTopicsResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, // reciprocal: 'item', }, @@ -1495,8 +1514,8 @@ export function makeExampleSchema( "post", relationalPostsResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, // reciprocal: 'item', }, @@ -1506,8 +1525,8 @@ export function makeExampleSchema( "divider", relationalDividersResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, // reciprocal: 'item', }, @@ -1517,8 +1536,8 @@ export function makeExampleSchema( "checklist", relationalChecklistsResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, // reciprocal: 'item', }, @@ -1528,8 +1547,8 @@ export function makeExampleSchema( "checklistItem", relationalChecklistItemsResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, // reciprocal: 'item', }, @@ -1540,8 +1559,8 @@ export function makeExampleSchema( "post", relationalPostsResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, // reciprocal: 'item', }, @@ -1551,8 +1570,8 @@ export function makeExampleSchema( "checklist", relationalChecklistsResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, // reciprocal: 'item', }, @@ -1562,26 +1581,26 @@ export function makeExampleSchema( "checklistItem", relationalChecklistItemsResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, // reciprocal: 'item', }, ) .addRelation(unionItemsCodec, "topic", unionTopicsResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, }) .addRelation(unionItemsCodec, "post", unionPostsResource, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, }) .addRelation(unionItemsCodec, "divider", unionDividersResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, }) .addRelation( @@ -1589,8 +1608,8 @@ export function makeExampleSchema( "checklist", unionChecklistsResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, }, ) @@ -1599,8 +1618,8 @@ export function makeExampleSchema( "checklistItem", unionChecklistItemsResourceOptions, { - localAttributes: [`id`] as const, - remoteAttributes: [`id`] as const, + localAttributes: [`id`], + remoteAttributes: [`id`], isUnique: true, }, ) @@ -1621,11 +1640,17 @@ export function makeExampleSchema( ], ); - const registry = EXPORTABLE( + const rawRegistry = EXPORTABLE( (makeRegistry, registryConfig) => makeRegistry(registryConfig), [makeRegistry, registryConfig], ); + type RawRegistry = typeof rawRegistry; + + interface Registry extends RawRegistry {} + + const registry: Registry = rawRegistry; + if (Math.random() > 2) { /* * This block includes a rudimentary TypeScript types test - we get a @@ -1665,7 +1690,7 @@ export function makeExampleSchema( const innerPlan = step instanceof __ListTransformStep ? step.getListStep() - : (step as PgSelectStep | PgSelectSingleStep); + : (step as PgSelectStep | PgSelectSingleStep); if ("getClassStep" in innerPlan) { innerPlan.getClassStep().setInliningForbidden(); } else if ("setInliningForbidden" in innerPlan) { @@ -1677,9 +1702,7 @@ export function makeExampleSchema( [__ListTransformStep, options], ); - type ResourceConnectionPlan< - TResource extends PgResource, - > = ConnectionStep< + type ResourceConnectionPlan = ConnectionStep< PgSelectSingleStep, PgSelectParsedCursorStep, PgSelectStep, @@ -1727,48 +1750,103 @@ export function makeExampleSchema( }, } = registry; - type MessageConnectionStep = ResourceConnectionPlan; - type MessageStep = PgSelectSingleStep; - type UserStep = PgSelectSingleStep; - type ForumStep = PgSelectSingleStep; - type PersonStep = PgSelectSingleStep; - type PersonBookmarkStep = PgSelectSingleStep; - type PostStep = PgSelectSingleStep; - type CommentStep = PgSelectSingleStep; - type SingleTableItemsStep = PgSelectStep; - type SingleTableItemStep = PgSelectSingleStep< - typeof singleTableItemsResource - >; - type RelationalItemsStep = PgSelectStep; - type RelationalItemStep = PgSelectSingleStep; - type RelationalTopicStep = PgSelectSingleStep< - typeof relationalTopicsResource - >; - type RelationalPostStep = PgSelectSingleStep; - type RelationalDividerStep = PgSelectSingleStep< - typeof relationalDividersResource - >; - type RelationalChecklistStep = PgSelectSingleStep< - typeof relationalChecklistsResource - >; - type RelationalChecklistItemStep = PgSelectSingleStep< - typeof relationalChecklistItemsResource - >; - type UnionItemsStep = PgSelectStep; - type UnionItemStep = PgSelectSingleStep; - type UnionTopicStep = PgSelectSingleStep; - type UnionPostStep = PgSelectSingleStep; - type UnionDividerStep = PgSelectSingleStep; - type UnionChecklistStep = PgSelectSingleStep; - type UnionChecklistItemStep = PgSelectSingleStep< - typeof unionChecklistItemsResource - >; - type RelationalCommentablesStep = PgSelectStep< - typeof relationalCommentableResource - >; - type RelationalCommentableStep = PgSelectSingleStep< - typeof relationalCommentableResource - >; + type unionEntityCodec = typeof unionEntityCodec; + interface UnionEntityCodec extends unionEntityCodec {} + + type messages = typeof messageResource; + interface MessageResource extends messages {} + + type MessageConnectionStep = ResourceConnectionPlan; + + type users = typeof userResource; + interface UserResource extends users {} + + type forums = typeof forumResource; + interface ForumResource extends forums {} + + type people = typeof personResource; + interface PersonResource extends people {} + + type person_bookmarks = typeof personBookmarksResource; + interface PersonBookmarkResource extends person_bookmarks {} + + type posts = typeof postResource; + interface PostResource extends posts {} + + type comments = typeof commentResource; + interface CommentResource extends comments {} + + type single_table_items = typeof singleTableItemsResource; + interface SingleTableItemResource extends single_table_items {} + + type relational_items = typeof relationalItemsResource; + interface RelationalItemResource extends relational_items {} + + type relational_topics = typeof relationalTopicsResource; + interface RelationalTopicResource extends relational_topics {} + + type relational_posts = typeof relationalPostsResource; + interface RelationalPostResource extends relational_posts {} + + type relational_dividers = typeof relationalDividersResource; + interface RelationalDividerResource extends relational_dividers {} + + type relational_checklists = typeof relationalChecklistsResource; + interface RelationalChecklistResource extends relational_checklists {} + + type relational_checklist_items = typeof relationalChecklistItemsResource; + interface RelationalChecklistItemResource + extends relational_checklist_items {} + + type union_items = typeof unionItemsResource; + interface UnionItemResource extends union_items {} + + type union_topics = typeof unionTopicsResource; + interface UnionTopicResource extends union_topics {} + + type union_posts = typeof unionPostsResource; + interface UnionPostResource extends union_posts {} + + type union_dividers = typeof unionDividersResource; + interface UnionDividerResource extends union_dividers {} + + type union_checklists = typeof unionChecklistsResource; + interface UnionChecklistResource extends union_checklists {} + + type union_checklist_items = typeof unionChecklistItemsResource; + interface UnionChecklistItemResource extends union_checklist_items {} + + type relational_commentables = typeof relationalCommentableResource; + interface RelationalCommentableResource extends relational_commentables {} + + type MessageStep = PgSelectSingleStep; + type UserStep = PgSelectSingleStep; + type ForumStep = PgSelectSingleStep; + type PersonStep = PgSelectSingleStep; + type PersonBookmarkStep = PgSelectSingleStep; + type PostStep = PgSelectSingleStep; + type CommentStep = PgSelectSingleStep; + type SingleTableItemsStep = PgSelectStep; + type SingleTableItemStep = PgSelectSingleStep; + type RelationalItemsStep = PgSelectStep; + type RelationalItemStep = PgSelectSingleStep; + type RelationalTopicStep = PgSelectSingleStep; + type RelationalPostStep = PgSelectSingleStep; + type RelationalDividerStep = PgSelectSingleStep; + type RelationalChecklistStep = + PgSelectSingleStep; + type RelationalChecklistItemStep = + PgSelectSingleStep; + type UnionItemsStep = PgSelectStep; + type UnionItemStep = PgSelectSingleStep; + type UnionTopicStep = PgSelectSingleStep; + type UnionPostStep = PgSelectSingleStep; + type UnionDividerStep = PgSelectSingleStep; + type UnionChecklistStep = PgSelectSingleStep; + type UnionChecklistItemStep = PgSelectSingleStep; + type RelationalCommentablesStep = PgSelectStep; + type RelationalCommentableStep = + PgSelectSingleStep; //////////////////////////////////////// @@ -1784,8 +1862,10 @@ export function makeExampleSchema( }); function attrField< - TMyResource extends PgResource, - TAttrName extends keyof GetPgResourceAttributes, + TMyResource extends AnyPgResource, + TAttrName extends PgCodecAttributeName< + GetPgResourceAttributes + >, >(attrName: TAttrName, type: GraphQLOutputType) { return { type, @@ -1800,8 +1880,10 @@ export function makeExampleSchema( } function singleRelationField< - TMyResource extends PgResource, - TRelationName extends keyof GetPgResourceRelations, + TMyResource extends AnyPgResource, + TRelationName extends PgCodecRelationConfigName< + GetPgResourceRelationConfigs + >, >(relation: TRelationName, type: GraphQLOutputType) { return { type, @@ -2827,7 +2909,7 @@ export function makeExampleSchema( const relationalItemPolymorphicTypeMap = EXPORTABLE( ( deoptimizeIfAppropriate, - ): PgPolymorphicTypeMap => ({ + ): PgPolymorphicTypeMap => ({ RelationalTopic: { match: (t) => t === "TOPIC", plan: (_, $item) => @@ -2859,7 +2941,7 @@ export function makeExampleSchema( const relationalItemInterface = EXPORTABLE( (pgPolymorphic, relationalItemPolymorphicTypeMap) => - ($item: RelationalItemStep) => + ($item: TStep) => pgPolymorphic( $item, $item.get("type"), @@ -2945,7 +3027,8 @@ export function makeExampleSchema( personResource, postResource, ): PgPolymorphicTypeMap< - PgSelectSingleStep | PgClassExpressionStep, + | PgSelectSingleStep + | PgClassExpressionStep, any>, readonly number[], ListStep > => ({ @@ -2977,10 +3060,10 @@ export function makeExampleSchema( */ const entityUnion = EXPORTABLE( (entityPolymorphicTypeMap, list, pgPolymorphic) => - ( + ( $item: - | PgSelectSingleStep> - | PgClassExpressionStep>, + | PgSelectSingleStep> + | PgClassExpressionStep, ) => pgPolymorphic( $item, @@ -3033,10 +3116,9 @@ export function makeExampleSchema( ) => function plan($person) { const $personId = $person.get("person_id"); - const $items: SingleTableItemsStep = - singleTableItemsResource.find({ - author_id: $personId, - }); + const $items = singleTableItemsResource.find({ + author_id: $personId, + }); deoptimizeIfAppropriate($items); return each($items, singleTableItemInterface); }, @@ -3156,9 +3238,7 @@ export function makeExampleSchema( [fieldName: string]: GrafastFieldConfig< any, any, - PgSelectSingleStep< - PgResource - >, + SingleTableItemStep, any, any >; @@ -3255,8 +3335,27 @@ export function makeExampleSchema( }), }); + interface RelationalItemsLikeStep + extends PgSelectSingleStep< + PgResource< + any, + PgCodec< + any, + PgCodecAttributeMap>, + any, + any, + any, + any, + any + >, + any, + any, + any + > + > {} + const commonRelationalItemFields = < - TStep extends PgSelectSingleStep, + TStep extends RelationalItemsLikeStep, >() => ({ id: attrField("id", GraphQLInt), @@ -3638,7 +3737,7 @@ export function makeExampleSchema( extensions: { grafast: { applyPlan: EXPORTABLE( - () => (step: PgUnionAllStep) => { + () => (step: PgUnionAllStep) => { step.orderBy({ attribute: "cvss_score", direction: "ASC", @@ -3653,7 +3752,7 @@ export function makeExampleSchema( extensions: { grafast: { applyPlan: EXPORTABLE( - () => (step: PgUnionAllStep) => { + () => (step: PgUnionAllStep) => { step.orderBy({ attribute: "cvss_score", direction: "DESC", @@ -4310,7 +4409,7 @@ export function makeExampleSchema( pgCodec: TYPES.text, name: "query", }, - ]) as PgSelectStep; + ]) as PgSelectStep; deoptimizeIfAppropriate($step); return each($step, ($item) => entityUnion($item as any)); }, @@ -4708,7 +4807,15 @@ export function makeExampleSchema( type PgRecord> = PgClassExpressionStep< - PgCodec, any, any, any, any, any>, + PgCodec< + any, + GetPgResourceAttributeMap, + any, + any, + any, + any, + any + >, TResource >; diff --git a/grafast/dataplan-pg/src/filters/pgBooleanFilter.ts b/grafast/dataplan-pg/src/filters/pgBooleanFilter.ts index a43f62419d..17579d4bf0 100644 --- a/grafast/dataplan-pg/src/filters/pgBooleanFilter.ts +++ b/grafast/dataplan-pg/src/filters/pgBooleanFilter.ts @@ -2,7 +2,7 @@ import type { ExecutableStep } from "grafast"; import { ModifierStep } from "grafast"; import type { SQL } from "pg-sql2"; -import type { PgCodec, PgConditionLikeStep } from "../interfaces.js"; +import type { AnyPgCodec, PgCodec, PgConditionLikeStep } from "../interfaces.js"; export class PgBooleanFilterStep extends ModifierStep { static $$export = { @@ -22,7 +22,10 @@ export class PgBooleanFilterStep extends ModifierStep { this.alias = $classFilterPlan.alias; } - placeholder($step: ExecutableStep, codec: PgCodec): SQL { + placeholder( + $step: ExecutableStep, + codec: TCodec, + ): SQL { return this.$parent.placeholder($step, codec); } diff --git a/grafast/dataplan-pg/src/filters/pgClassFilter.ts b/grafast/dataplan-pg/src/filters/pgClassFilter.ts index 6db1af2c93..0e9eec8be7 100644 --- a/grafast/dataplan-pg/src/filters/pgClassFilter.ts +++ b/grafast/dataplan-pg/src/filters/pgClassFilter.ts @@ -2,7 +2,7 @@ import type { ExecutableStep } from "grafast"; import { ModifierStep } from "grafast"; import type { SQL } from "pg-sql2"; -import type { PgCodec } from "../interfaces.js"; +import type { AnyPgCodec, PgCodec } from "../interfaces.js"; import type { PgConditionCapableParentStep, PgConditionStep, @@ -30,7 +30,10 @@ export class PgClassFilterStep< this.conditions.push(condition); } - placeholder($step: ExecutableStep, codec: PgCodec): SQL { + placeholder( + $step: ExecutableStep, + codec: TCodec, + ): SQL { return this.$parent.placeholder($step, codec); } diff --git a/grafast/dataplan-pg/src/filters/pgOrFilter.ts b/grafast/dataplan-pg/src/filters/pgOrFilter.ts index 06c930c24f..c7b6f04e71 100644 --- a/grafast/dataplan-pg/src/filters/pgOrFilter.ts +++ b/grafast/dataplan-pg/src/filters/pgOrFilter.ts @@ -3,7 +3,7 @@ import { ModifierStep } from "grafast"; import type { SQL } from "pg-sql2"; import { sql } from "pg-sql2"; -import type { PgCodec, PgConditionLikeStep } from "../interfaces.js"; +import type { AnyPgCodec, PgCodec, PgConditionLikeStep } from "../interfaces.js"; export class PgOrFilterStep extends ModifierStep { static $$export = { @@ -20,7 +20,7 @@ export class PgOrFilterStep extends ModifierStep { this.alias = $classFilterPlan.alias; } - placeholder($step: ExecutableStep, codec: PgCodec): SQL { + placeholder($step: ExecutableStep, codec: TCodec): SQL { return this.$parent.placeholder($step, codec); } diff --git a/grafast/dataplan-pg/src/index.ts b/grafast/dataplan-pg/src/index.ts index e1c7c9b16f..6674d7a18e 100644 --- a/grafast/dataplan-pg/src/index.ts +++ b/grafast/dataplan-pg/src/index.ts @@ -10,7 +10,6 @@ import { ObjectFromPgCodecAttributes, PgCodecAttribute, PgCodecAttributeExtensions, - PgCodecAttributes, PgCodecAttributeVia, PgCodecAttributeViaExplicit, PgEnumCodecSpec, @@ -18,6 +17,12 @@ import { rangeOfCodec, recordCodec, TYPES, + AnyPgCodecAttribute, + DefaultPgCodecAttribute, + DefaultPgCodecAttributeVia, + AnyPgCodecAttributeVia, + AnyPgCodecAttributeViaAttribute, + AnyPgCodecAttributeViaRelationName, } from "./codecs.js"; import { PgBox, @@ -47,6 +52,20 @@ import { PgResourceParameter, PgResourceUnique, PgResourceUniqueExtensions, + AnyPgRegistryBuilder, + AnyPgResource, + AnyPgResourceOptions, + AnyPgResourceParameter, + AnyPgResourceUnique, + AnyScalarPgResource, + DefaultPgResourceOptions, + DefaultPgResource, + DefaultPgResourceParameter, + DefaultPgResourceUnique, + DefaultRegistryBuilder, + EmptyRegistryBuilder, + DefaultPgFunctionResourceOptions, + AnyPgFunctionResourceOptions, } from "./datasource.js"; import { PgClient, @@ -65,7 +84,7 @@ import { PgClassFilterStep } from "./filters/pgClassFilter.js"; import { PgManyFilterStep } from "./filters/pgManyFilter.js"; import { PgOrFilterStep } from "./filters/pgOrFilter.js"; import { - GetPgCodecAttributes, + PgCodecAttributes, GetPgRegistryCodecRelations, GetPgRegistryCodecs, GetPgRegistrySources, @@ -88,7 +107,7 @@ import { PgCodecPolymorphismSingleTypeAttributeSpec, PgCodecPolymorphismSingleTypeSpec, PgCodecPolymorphismUnion, - PgCodecRelation, + PgRelation, PgCodecRelationConfig, PgCodecRelationExtensions, PgCodecWithAttributes, @@ -106,6 +125,43 @@ import { PgTypedExecutableStep, PlanByUniques, TuplePlanMap, + AnyPgCodec, + AnyPgCodecAttributesRecord, + AnyPgCodecRelationConfig, + AnyPgRegistry, + AnyPgRegistryConfig, + AnyScalarPgCodec, + PgCodecAttributesRecord, + PgCodecFromJavaScript, + PgCodecFromPg, + PgCodecFromPostgres, + PgCodecName, + PgCodecRelationBase, + PgCodecRelationConfigLocalCodec, + PgCodecRelationConfigName, + PgCodecRelationConfigRemoteResourceOptions, + PgOrderAttributeSpec, + PgOrderFragmentSpec, + AnyPgRangeItemCodec, + PgRegistryCodecRelations, + PgRegistryCodecs, + PgRegistryConfig, + PgRegistryConfigCodecs, + PgRegistryConfigRelationConfigs, + PgRegistryConfigResourceOptions, + PgRegistryRelationConfigs, + PgRegistryResourceOptions, + PgRegistryResources, + DefaultPgRelation, + AnyPgRelation, + DefaultPgCodec, + DefaultScalarPgCodec, + DefaultPgCodecRelationConfig, + DefaultPgCodecAttributesRecord, + DefaultPgRangeItemCodec, + DefaultPgRegistry, + DefaultPgCodecWithAttributes, + DefaultPgRegistryConfig, } from "./interfaces.js"; import { PgLockableParameter, PgLockCallback } from "./pgLocker.js"; import { @@ -121,7 +177,12 @@ import { pgWhereConditionSpecListToSQL, } from "./steps/pgCondition.js"; import { PgCursorStep } from "./steps/pgCursor.js"; -import { pgDeleteSingle, PgDeleteSingleStep } from "./steps/pgDeleteSingle.js"; +import { + pgDeleteSingle, + PgDeleteSingleStep, + DefaultPgDeleteSingleStep, + AnyPgDeleteSingleStep, +} from "./steps/pgDeleteSingle.js"; import { pgInsertSingle, PgInsertSingleStep } from "./steps/pgInsertSingle.js"; import { pgPageInfo, PgPageInfoStep } from "./steps/pgPageInfo.js"; import { @@ -141,12 +202,17 @@ import { PgSelectParsedCursorStep, PgSelectStep, sqlFromArgDigests, + DefaultPgSelectStep, + AnyPgSelectStep, } from "./steps/pgSelect.js"; import { pgSelectFromRecord, pgSelectSingleFromRecord, PgSelectSinglePlanOptions, PgSelectSingleStep, + AnyPgSelectSinglePlanOptions, + AnyPgSelectSingleStep, + DefaultPgSelectSingleStep, } from "./steps/pgSelectSingle.js"; import { pgSingleTablePolymorphic, @@ -163,7 +229,12 @@ import { PgUnionAllStepMember, PgUnionAllStepOrder, } from "./steps/pgUnionAll.js"; -import { pgUpdateSingle, PgUpdateSingleStep } from "./steps/pgUpdateSingle.js"; +import { + pgUpdateSingle, + PgUpdateSingleStep, + DefaultPgUpdateSingleStep, + AnyPgUpdateSingleStep, +} from "./steps/pgUpdateSingle.js"; import { pgValidateParsedCursor, PgValidateParsedCursorStep, @@ -178,13 +249,72 @@ import { import { assertPgClassSingleStep } from "./utils.js"; export { + DefaultPgResourceOptions, + AnyPgSelectSinglePlanOptions, + DefaultPgCodecAttribute, + DefaultPgCodecAttributeVia, + AnyPgSelectSingleStep, + DefaultPgResource, + DefaultPgResourceParameter, + DefaultPgSelectSingleStep, + DefaultPgRelation, + AnyPgRelation, + DefaultPgCodecWithAttributes, + DefaultPgResourceUnique, + DefaultRegistryBuilder, + EmptyRegistryBuilder, + DefaultPgUpdateSingleStep, + AnyPgUpdateSingleStep, + DefaultPgDeleteSingleStep, + AnyPgDeleteSingleStep, + DefaultPgCodec, + AnyPgRegistryBuilder, + AnyPgResource, + AnyPgResourceOptions, + AnyPgResourceParameter, + AnyPgResourceUnique, + AnyScalarPgResource, + AnyPgCodecAttribute, + AnyPgCodecAttributeVia, + AnyPgCodecAttributeViaAttribute, + AnyPgCodecAttributeViaRelationName, + AnyPgCodec, + AnyPgCodecAttributesRecord, + AnyPgCodecRelationConfig, + AnyPgRegistry, + AnyPgRegistryConfig, + AnyScalarPgCodec, + PgCodecAttributes, + PgCodecAttributesRecord, + PgCodecFromJavaScript, + PgCodecFromPg, + PgCodecFromPostgres, + PgCodecName, + PgCodecRelationBase, + PgCodecRelationConfigLocalCodec, + PgCodecRelationConfigName, + PgCodecRelationConfigRemoteResourceOptions, + PgOrderAttributeSpec, + PgOrderFragmentSpec, + AnyPgRangeItemCodec as PgRangeItemCodec, + PgRegistryCodecRelations, + PgRegistryCodecs, + PgRegistryConfig, + PgRegistryConfigCodecs, + PgRegistryConfigRelationConfigs, + PgRegistryConfigResourceOptions, + PgRegistryRelationConfigs, + PgRegistryResourceOptions, + PgRegistryResources, + PgRelation, assertPgClassSingleStep, digestsFromArgumentSpecs, domainOfCodec, enumCodec, + DefaultPgFunctionResourceOptions, + AnyPgFunctionResourceOptions, getCodecByPgCatalogTypeName, getInnerCodec, - GetPgCodecAttributes, GetPgRegistryCodecRelations, GetPgRegistryCodecs, GetPgRegistrySources, @@ -193,6 +323,12 @@ export { GetPgResourceRegistry, GetPgResourceRelations, GetPgResourceUniques, + DefaultScalarPgCodec, + DefaultPgCodecRelationConfig, + DefaultPgCodecAttributesRecord, + DefaultPgRangeItemCodec, + DefaultPgRegistry, + DefaultPgRegistryConfig, isEnumCodec, KeysOfType, listOfCodec, @@ -215,7 +351,6 @@ export { PgCodecAnyScalar, PgCodecAttribute, PgCodecAttributeExtensions, - PgCodecAttributes, PgCodecAttributeVia, PgCodecAttributeViaExplicit, PgCodecExtensions, @@ -232,7 +367,7 @@ export { PgCodecRefPath, PgCodecRefPathEntry, PgCodecRefs, - PgCodecRelation, + PgRelation as PgCodecRelation, PgCodecRelationConfig, PgCodecRelationExtensions, PgCodecWithAttributes, @@ -250,6 +385,8 @@ export { PgEnumValue, PgExecutor, PgExecutorContext, + AnyPgSelectStep, + DefaultPgSelectStep, PgExecutorContextPlans, PgExecutorInput, PgExecutorMutationOptions, diff --git a/grafast/dataplan-pg/src/interfaces.ts b/grafast/dataplan-pg/src/interfaces.ts index bc100e469e..35ee9ce1d4 100644 --- a/grafast/dataplan-pg/src/interfaces.ts +++ b/grafast/dataplan-pg/src/interfaces.ts @@ -2,12 +2,27 @@ import type { ExecutableStep, GrafastSubscriber, ModifierStep } from "grafast"; import type { SQL, SQLRawValue } from "pg-sql2"; import type { PgAdaptorOptions } from "./adaptors/pg.js"; -import type { PgCodecAttributes } from "./codecs.js"; import type { + AnyPgCodecAttribute, + DefaultPgCodecAttribute, + PgCodecAttribute, + PgCodecAttributeCodec, + PgCodecAttributeName, +} from "./codecs.js"; +import type { + AnyPgResource, + AnyPgResourceOptions, + AnyPgResourceParameter, + AnyPgResourceUnique, + DefaultPgResource, + DefaultPgResourceOptions, PgCodecRefs, PgResource, - PgResourceOptions, - PgResourceParameter, + PgResourceCodec, + PgResourceOptionCodec, + PgResourceOptionName, + PgResourceOptionParameters, + PgResourceOptionUniques, PgResourceUnique, } from "./datasource.js"; import type { PgExecutor, WithPgClient } from "./executor.js"; @@ -20,13 +35,12 @@ import type { PgUpdateSingleStep } from "./steps/pgUpdateSingle.js"; * A class-like source of information - could be from `SELECT`-ing a row, or * `INSERT...RETURNING` or similar. *ALWAYS* represents a single row (or null). */ -export type PgClassSingleStep< - TResource extends PgResource = PgResource, -> = - | PgSelectSingleStep - | PgInsertSingleStep - | PgUpdateSingleStep - | PgDeleteSingleStep; +export type PgClassSingleStep = + + | PgSelectSingleStep + | PgInsertSingleStep + | PgUpdateSingleStep + | PgDeleteSingleStep; /** * Given a value of type TInput, returns an `SQL` value to insert into an SQL @@ -116,29 +130,119 @@ export type PgCodecPolymorphism = | PgCodecPolymorphismRelational | PgCodecPolymorphismUnion; +export interface DefaultPgRangeItemCodec + extends PgCodec {} +export interface AnyPgRangeItemCodec + extends PgCodec {} + +export type PgCodecName = U extends PgCodec< + infer TName, + any, + any, + any, + any, + any, + any +> + ? TName + : never; + +export type PgCodecAttributes = U extends PgCodec< + any, + infer TAttributes, + any, + any, + any, + any, + any +> + ? TAttributes[keyof TAttributes] + : never; +export type PgCodecAttributeMap = U extends PgCodec< + any, + infer TAttributes, + any, + any, + any, + any, + any +> + ? TAttributes + : never; +export type PgCodecFromJavaScript = U extends PgCodec< + any, + any, + any, + infer TFromJavaScript, + any, + any, + any +> + ? TFromJavaScript + : never; +export type PgCodecFromPostgres = U extends PgCodec< + any, + any, + infer TFromPostgres, + any, + any, + any, + any +> + ? TFromPostgres + : never; +export type PgCodecFromPg = PgDecode< + PgCodecFromJavaScript, + PgCodecFromPostgres +>; +export interface DefaultPgCodecAttributesRecord + extends PgCodecAttributesRecord {} +export interface AnyPgCodecAttributesRecord + extends PgCodecAttributesRecord {} + +export type PgCodecAttributesRecord< + TCodecAttributes extends AnyPgCodecAttribute, +> = { + [TCodecAttribute in TCodecAttributes as PgCodecAttributeName]: TCodecAttribute; +}; +export interface DefaultPgCodec + extends PgCodec< + string, + PgCodecAttributesRecord, + any, + any, + DefaultPgCodec, + DefaultPgCodec, + DefaultPgCodec + > {} +export interface DefaultScalarPgCodec + extends PgCodec< + string, + never, + any, + any, + DefaultPgCodec, + DefaultPgCodec, + DefaultPgCodec + > {} +export interface AnyScalarPgCodec + extends PgCodec {} +export interface AnyPgCodec + extends PgCodec {} + /** * A codec for a Postgres type, tells us how to convert to-and-from Postgres * (including changes to the SQL statement itself). Also includes metadata * about the type, for example any of the attributes it has. */ export interface PgCodec< - TName extends string = string, - TAttributes extends PgCodecAttributes | undefined = - | PgCodecAttributes - | undefined, - TFromPostgres = any, - TFromJavaScript = TFromPostgres, - TArrayItemCodec extends - | PgCodec - | undefined = PgCodec | undefined, - TDomainItemCodec extends - | PgCodec - | undefined = PgCodec | undefined, - TRangeItemCodec extends - | PgCodec - | undefined = - | PgCodec - | undefined, + TName extends string, + TCodecAttributes extends Record, + TFromPostgres, + TFromJavaScript, + TArrayItemCodec extends AnyPgCodec, + TDomainItemCodec extends AnyPgCodec, + TRangeItemCodec extends AnyPgCodec, > { /** * Unique name to identify this codec. @@ -197,7 +301,7 @@ export interface PgCodec< /** * If this is a composite type, the attributes it supports. */ - attributes: TAttributes; + attributes?: TCodecAttributes; /** * A callback to return `'true'` (text string) if the composite type @@ -239,7 +343,7 @@ export interface PgCodec< */ rangeOfCodec?: TRangeItemCodec; - polymorphism?: PgCodecPolymorphism; + polymorphism?: PgCodecPolymorphism; description?: string; @@ -262,20 +366,29 @@ export interface PgCodec< */ executor: PgExecutor | null; } - -export type PgCodecWithAttributes< - TAttributes extends PgCodecAttributes = PgCodecAttributes, -> = PgCodec; - -export type PgCodecAnyScalar = PgCodec< - string, - undefined, +export interface DefaultPgCodecWithAttributes + extends PgCodec< + any, + Record, + any, + any, + never, + any, + never + > {} +export interface PgCodecWithAttributes< + TAttributes extends Record, +> extends PgCodec {} + +export interface PgCodecAnyScalar + extends PgCodec; +> {} export type PgCodecList< TInnerCodec extends PgCodec = PgCodec< @@ -287,7 +400,7 @@ export type PgCodecList< any, any >, -> = PgCodec; +> = PgCodec; export type PgEnumValue = { value: TValue; @@ -300,15 +413,7 @@ export type PgEnumValue = { export interface PgEnumCodec< TName extends string = string, TValue extends string = string, -> extends PgCodec< - TName, - undefined, - string, - TValue, - undefined, - undefined, - undefined - > { +> extends PgCodec { values: PgEnumValue[]; } @@ -316,7 +421,7 @@ export interface PgEnumCodec< * A PgTypedExecutableStep has a 'pgCodec' property which means we don't need * to also state the pgCodec to use, this can be an added convenience. */ -export interface PgTypedExecutableStep +export interface PgTypedExecutableStep extends ExecutableStep { pgCodec: TCodec; } @@ -331,7 +436,7 @@ export type PgOrderFragmentSpec = { /** The expression we're ordering by. */ fragment: SQL; /** The codec of the expression that we're ordering by, this is useful when constructing a cursor for it. */ - codec: PgCodec; + codec: AnyPgCodec; attribute?: never; callback?: never; @@ -345,9 +450,9 @@ export type PgOrderAttributeSpec = { /** An optional expression to wrap this attribute with, and the type that expression returns */ callback?: ( attributeExpression: SQL, - attributeCodec: PgCodec, + attributeCodec: AnyPgCodec, nullable: boolean, - ) => [fragment: SQL, codec: PgCodec, nullable?: boolean]; + ) => [fragment: SQL, codec: AnyPgCodec, nullable?: boolean]; fragment?: never; codec?: never; @@ -369,19 +474,25 @@ export interface PgGroupSpec { } export type TuplePlanMap< - TAttributes extends PgCodecAttributes, - TTuple extends ReadonlyArray, + TAttributes extends AnyPgCodecAttribute, + TTuple extends ReadonlyArray>, > = { [Index in keyof TTuple]: { // Optional attributes - [key in keyof TAttributes as Exclude< - key, - keyof TTuple[number] - >]?: ExecutableStep>; + [attribute in Exclude< + TAttributes, + { name: TTuple[number] } + > as PgCodecAttributeName]?: ExecutableStep< + ReturnType>> + >; } & { // Required unique combination of attributes [key in TTuple[number]]: ExecutableStep< - ReturnType + ReturnType< + PgCodecFromPg< + PgCodecAttributeCodec> + > + > >; }; }; @@ -395,18 +506,13 @@ export type TuplePlanMap< * to. */ export type PlanByUniques< - TAttributes extends PgCodecAttributes, - TUniqueAttributes extends ReadonlyArray>, -> = TAttributes extends PgCodecAttributes - ? TuplePlanMap< - TAttributes, - TUniqueAttributes[number]["attributes"] & string[] - >[number] - : undefined; + TAttributes extends AnyPgCodecAttribute, + TUniqueAttributes extends PgResourceUnique, +> = TuplePlanMap[number]; export type PgConditionLikeStep = (ModifierStep | ExecutableStep) & { alias: SQL; - placeholder($step: ExecutableStep, codec: PgCodec): SQL; + placeholder($step: ExecutableStep, codec: AnyPgCodec): SQL; where(condition: SQL): void; having(condition: SQL): void; }; @@ -523,9 +629,11 @@ export interface MakePgServiceOptions export type PgCodecRelationExtensions = DataplanPg.PgCodecRelationExtensions; export interface PgCodecRelationBase< - TLocalCodec extends PgCodec = PgCodec, + TName extends string = string, + TLocalCodec extends AnyPgCodec = AnyPgCodec, TRemoteAttributes extends string = string, > { + name: TName; /** Where the relationship starts */ localCodec: TLocalCodec; /** If localCodec is polymorphic, which of the concrete subtypes should this relationship apply to? */ @@ -534,7 +642,7 @@ export interface PgCodecRelationBase< /** * The attributes locally used in this relationship. */ - localAttributes: readonly (keyof TLocalCodec["attributes"])[]; + localAttributes: Array>>; /** * The remote attributes that are joined against. @@ -560,91 +668,79 @@ export interface PgCodecRelationBase< description?: string; } - +export interface DefaultPgCodecRelationConfig + extends PgCodecRelationConfig< + string, + DefaultPgCodec, + DefaultPgResourceOptions + > {} +export interface AnyPgCodecRelationConfig + extends PgCodecRelationConfig {} + +export type PgCodecRelationConfigName = U extends PgCodecRelationConfig< + infer TName, + any, + any +> + ? TName + : never; +export type PgCodecRelationConfigLocalCodec = + U extends PgCodecRelationConfig + ? TLocalCodec + : never; +export type PgCodecRelationConfigRemoteResourceOptions = + U extends PgCodecRelationConfig + ? TRemoteResourceOptions + : never; export interface PgCodecRelationConfig< - TLocalCodec extends PgCodec = PgCodecWithAttributes, - TRemoteResourceOptions extends PgResourceOptions = PgResourceOptions< - any, - PgCodecWithAttributes, - any, - any - >, + TName extends string = string, + TLocalCodec extends AnyPgCodec = AnyPgCodec, + TRemoteResourceOptions extends AnyPgResourceOptions = AnyPgResourceOptions, > extends PgCodecRelationBase< + TName, TLocalCodec, - TRemoteResourceOptions extends PgResourceOptions< - any, - PgCodec, - any, - any - > - ? keyof UAttributes - : never + PgCodecAttributeName> > { remoteResourceOptions: TRemoteResourceOptions; } -/** - * Describes a relation from a codec to a resource - */ -export interface PgCodecRelation< - TLocalCodec extends PgCodecWithAttributes = PgCodecWithAttributes, - TRemoteResource extends PgResource< - any, - PgCodecWithAttributes, - any, - any, - any - > = PgResource, -> extends PgCodecRelationBase< - TLocalCodec, - TRemoteResource extends PgResource< - any, - PgCodec, - any, - any, - any - > - ? keyof UAttributes - : never - > { - /** - * The remote resource this relation relates to. - */ - remoteResource: TRemoteResource; -} +export interface DefaultPgRegistryConfig + extends PgRegistryConfig< + DefaultPgCodec, + DefaultPgResourceOptions, + DefaultPgCodecRelationConfig + > {} +export interface AnyPgRegistryConfig extends PgRegistryConfig {} +export type PgRegistryConfigCodecs = U extends PgRegistryConfig< + infer TCodecs, + any, + any +> + ? TCodecs + : never; +export type PgRegistryConfigResourceOptions = U extends PgRegistryConfig< + any, + infer TResourceOptions, + any +> + ? TResourceOptions + : never; +export type PgRegistryConfigRelationConfigs = U extends PgRegistryConfig< + any, + any, + infer TRelationConfigs +> + ? TRelationConfigs + : never; export interface PgRegistryConfig< - TCodecs extends { - [name in string]: PgCodec< - name, - PgCodecAttributes | undefined, - any, - any, - any, - any, - any - >; - }, - TResourceOptions extends { - [name in string]: PgResourceOptions< - name, - PgCodec, - ReadonlyArray>>, - readonly PgResourceParameter[] | undefined - >; - }, - TRelations extends { - [codecName in keyof TCodecs]?: { - [relationName in string]: PgCodecRelationConfig< - PgCodec, - PgResourceOptions - >; - }; - }, + TCodecs extends AnyPgCodec, + TResourceOptions extends AnyPgResourceOptions, + TRelationConfigs extends AnyPgCodecRelationConfig, > { - pgCodecs: TCodecs; - pgResources: TResourceOptions; - pgRelations: TRelations; + pgCodecs: PgRegistryCodecs; + pgResources: PgRegistryResourceOptions; + pgRelations: PgRegistryRelationConfigs; } // https://github.com/microsoft/TypeScript/issues/47980#issuecomment-1049304607 @@ -652,150 +748,159 @@ export type Expand = T extends unknown ? { [TKey in keyof T]: T[TKey] } : never; -export interface PgRegistry< - TCodecs extends { - [name in string]: PgCodec< - name, - PgCodecAttributes | undefined, - any, - any, - any, - any, - any - >; - } = Record< +export type PgRegistryCodecs = { + [TCodec in TCodecs as PgCodecName]: TCodec; +}; + +export type PgRegistryResourceOptions< + TResourceOptions extends AnyPgResourceOptions, +> = { + [TResourceOption in TResourceOptions as PgResourceOptionName]: TResourceOption; +}; + +export type PgRegistryResources< + TResourceOptions extends AnyPgResourceOptions, + TRegistry extends AnyPgRegistry, +> = { + [TResourceOption in TResourceOptions as PgResourceOptionName]: PgResource< + PgResourceOptionName, + PgResourceOptionCodec, + PgResourceOptionUniques, + PgResourceOptionParameters, + TRegistry + >; +}; +export type PgRegistryRelationConfigs< + TRelationConfigs extends AnyPgCodecRelationConfig, +> = { + [TRelationConfig in TRelationConfigs as PgCodecName< + PgCodecRelationConfigLocalCodec + >]: Record, TRelationConfig>; +}; + +export interface AnyPgRelation extends PgRelation {} +export interface DefaultPgRelation + extends PgRelation< string, - PgCodec - >, - TResourceOptions extends { - [name in string]: PgResourceOptions< - name, - PgCodec, // TCodecs[keyof TCodecs], - ReadonlyArray>, - readonly PgResourceParameter[] | undefined + DefaultPgCodec, + DefaultPgResourceOptions, + DefaultPgRegistry + > {} + +export interface PgRelation< + TName extends string, + TLocalCodec extends AnyPgCodec, + TRemoteResourceOptions extends AnyPgResourceOptions, + TRegistry extends AnyPgRegistry, +> extends Omit< + PgCodecRelationConfig, + "remoteResourceOptions" + > { + remoteResource: PgResource< + PgResourceOptionName, + PgResourceOptionCodec, + PgResourceOptionUniques, + PgResourceOptionParameters, + TRegistry + >; +} + +export type PgRegistryCodecRelations< + TRelationConfigs extends AnyPgCodecRelationConfig, + TRegistry extends AnyPgRegistry, +> = { + [TRelationConfig in TRelationConfigs as PgCodecName< + PgCodecRelationConfigLocalCodec + >]: { + [TRelationName in PgCodecRelationConfigName]: PgRelation< + PgCodecRelationConfigName< + Extract + >, + PgCodecRelationConfigLocalCodec< + Extract + >, + PgCodecRelationConfigRemoteResourceOptions< + Extract + >, + TRegistry >; - } = Record< - string, - PgResourceOptions< - string, - // TYPES: This maybe shouldn't be PgCodecWithAttributes, but PgCodec instead? - PgCodecWithAttributes, // TCodecs[keyof TCodecs], - ReadonlyArray>, - readonly PgResourceParameter[] | undefined - > - >, - TRelations extends { - [codecName in keyof TCodecs]?: { - [relationName in string]: PgCodecRelationConfig< - // TCodecs[keyof TCodecs] & - PgCodec, - // TResourceOptions[keyof TResourceOptions] & - PgResourceOptions< - any, - // TCodecs[keyof TCodecs] & - PgCodecWithAttributes, - any, - any - > - >; - }; - } = Record< - string, - Record< - string, - PgCodecRelationConfig< - // TCodecs[keyof TCodecs] & - PgCodec, - // TResourceOptions[keyof TResourceOptions] & - PgResourceOptions< - any, - // TCodecs[keyof TCodecs] & - PgCodecWithAttributes, - any, - any - > - > - > - >, -> { - pgCodecs: TCodecs; - pgResources: { - [name in keyof TResourceOptions]: TResourceOptions[name] extends PgResourceOptions< - infer UName, - infer UCodec, - infer UUniques, - infer UParameters - > - ? PgResource< - UName, - UCodec, - UUniques, - UParameters, - PgRegistry - > - : never; - }; - pgRelations: { - [codecName in keyof TRelations]: { - [relationName in keyof TRelations[codecName]]: Expand< - Omit & { - remoteResource: TRelations[codecName][relationName] extends { - remoteResourceOptions: PgResourceOptions< - infer UName, - infer UCodec, - infer UUniques, - infer UParameters - >; - } - ? PgResource< - UName, - UCodec, - UUniques, - UParameters, - PgRegistry - > - : never; - } - >; - }; }; +}; +export interface AnyPgRegistry extends PgRegistry {} +export interface DefaultPgRegistry + extends PgRegistry< + DefaultPgCodec, + DefaultPgResourceOptions, + DefaultPgCodecRelationConfig + > {} +export interface EmptyPgRegistry extends PgRegistry {} + +export interface PgRegistry< + TCodecs extends AnyPgCodec, + TResourceOptions extends AnyPgResourceOptions, + TRelationConfigs extends AnyPgCodecRelationConfig, +> { + pgCodecs: PgRegistryCodecs; + pgResources: PgRegistryResources; + pgRelations: PgRegistryCodecRelations; } -export type GetPgRegistryCodecs> = - TRegistry["pgCodecs"]; +export type GetPgRegistryCodecs = U extends PgRegistry< + infer TCodecs, + any, + any +> + ? TCodecs + : never; -export type GetPgRegistrySources> = +export type GetPgRegistryRelations = U extends PgRegistry< + any, + any, + infer TRelationConfigs +> + ? PgRegistryCodecRelations + : never; + +export type GetPgRegistrySources = TRegistry["pgResources"]; +export type GetPgRegistryCodecRelationConfigs< + TRegistry extends AnyPgRegistry, + TCodec extends AnyPgCodec, +> = TRegistry extends PgRegistry + ? Extract } }> + : never; + export type GetPgRegistryCodecRelations< - TRegistry extends PgRegistry, - TCodec extends PgCodec, -> = TRegistry["pgRelations"][TCodec["name"]]; - -export type GetPgCodecAttributes< - TCodec extends PgCodec, -> = TCodec extends PgCodec - ? UAttributes extends undefined - ? never - : UAttributes - : PgCodecAttributes; - -export type GetPgResourceRegistry< - TResource extends PgResource, -> = TResource["registry"]; - -export type GetPgResourceCodec< - TResource extends PgResource, -> = TResource["codec"]; - -export type GetPgResourceAttributes< - TResource extends PgResource, -> = GetPgCodecAttributes; - -export type GetPgResourceRelations< - TResource extends PgResource, -> = TResource["registry"]["pgRelations"][TResource["codec"]["name"]]; - -export type GetPgResourceUniques< - TResource extends PgResource, -> = TResource["uniques"]; + TRegistry extends AnyPgRegistry, + TCodec extends AnyPgCodec, +> = GetPgRegistryRelations[PgCodecName]; + +export type GetPgResourceRegistry = + TResource["registry"]; + +export type GetPgResourceCodec = + TResource["codec"]; + +export type GetPgResourceAttributes = + PgCodecAttributes>; +export type GetPgResourceAttributeMap = + PgCodecAttributeMap>; +export type GetPgResourceRelationConfigs = + GetPgRegistryCodecRelationConfigs; + +export type GetPgResourceRelations = + GetPgRegistryCodecRelations< + GetPgResourceRegistry, + PgResourceCodec + >; + +export type GetPgResourceUniques = U extends PgResource< + any, + any, + infer TUniques, + any, + any +> + ? TUniques + : never; diff --git a/grafast/dataplan-pg/src/pgLocker.ts b/grafast/dataplan-pg/src/pgLocker.ts index 0c6e4f4c73..d7e0f8a4c1 100644 --- a/grafast/dataplan-pg/src/pgLocker.ts +++ b/grafast/dataplan-pg/src/pgLocker.ts @@ -1,6 +1,7 @@ import { isDev } from "grafast"; -import type { PgSelectStep, PgUnionAllStep } from "./index"; +import { AnyPgSelectStep } from "./steps/pgSelect"; +import { AnyPgUnionAllStep } from "./steps/pgUnionAll"; export type PgLockableParameter = | "orderBy" @@ -8,13 +9,13 @@ export type PgLockableParameter = | "last" | "offset" | "groupBy"; -export type PgLockCallback< - TStep extends PgSelectStep | PgUnionAllStep, -> = (step: TStep) => void; +export type PgLockCallback = (step: TStep) => void; -export class PgLocker< - TStep extends PgSelectStep | PgUnionAllStep, -> { +export type AnyPgStep = AnyPgSelectStep | AnyPgUnionAllStep; + +export interface AnyPgLocker extends PgLocker {} + +export class PgLocker { /** * Determines if the PgSelectStep is "locked" - i.e. its * FROM,JOINs,WHERE,ORDER BY,LIMIT,OFFSET cannot be changed. Note this does diff --git a/grafast/dataplan-pg/src/steps/pgClassExpression.ts b/grafast/dataplan-pg/src/steps/pgClassExpression.ts index 9ace5a877f..d522327b47 100644 --- a/grafast/dataplan-pg/src/steps/pgClassExpression.ts +++ b/grafast/dataplan-pg/src/steps/pgClassExpression.ts @@ -3,11 +3,11 @@ import { access, exportAs, UnbatchedExecutableStep } from "grafast"; import type { SQL } from "pg-sql2"; import sql from "pg-sql2"; -import type { PgResource } from "../datasource.js"; +import type { AnyPgResource, PgResource } from "../datasource.js"; import type { - GetPgCodecAttributes, + AnyPgCodec, PgClassSingleStep, - PgCodec, + PgCodecAttributes, PgTypedExecutableStep, } from "../interfaces.js"; import { PgDeleteSingleStep } from "./pgDeleteSingle.js"; @@ -15,6 +15,7 @@ import { PgInsertSingleStep } from "./pgInsertSingle.js"; import { PgSelectSingleStep } from "./pgSelectSingle.js"; import { PgUnionAllSingleStep } from "./pgUnionAll.js"; import { PgUpdateSingleStep } from "./pgUpdateSingle.js"; +import { PgCodecAttributeCodec, PgCodecAttributeName } from "../codecs.js"; // const debugPlan = debugFactory("@dataplan/pg:PgClassExpressionStep:plan"); // const debugExecute = debugFactory( "@dataplan/pg:PgClassExpressionStep:execute",); @@ -28,8 +29,8 @@ import { PgUpdateSingleStep } from "./pgUpdateSingle.js"; * of another layer of plan. */ export class PgClassExpressionStep< - TExpressionCodec extends PgCodec, - TResource extends PgResource, + TExpressionCodec extends AnyPgCodec, + TResource extends AnyPgResource, > extends UnbatchedExecutableStep implements PgTypedExecutableStep @@ -160,10 +161,14 @@ export class PgClassExpressionStep< Instead, we'll lie and ignore the `AccessStep` case */ - public get>( + public get< + TAttr extends PgCodecAttributeName>, + >( attributeName: TAttr, ): PgClassExpressionStep< - GetPgCodecAttributes[TAttr]["codec"], + PgCodecAttributeCodec< + Extract, { name: TAttr }> + >, TResource > { const attributes = this.pgCodec.attributes; @@ -171,7 +176,7 @@ export class PgClassExpressionStep< // Fall back to access, since this could be a 'point' or similar type that doesn't have attributes in Postgres but does in JS. return access(this, attributeName) as any; } - const attribute = attributes[attributeName as string]; + const attribute = attributes[attributeName]; if (!attribute) { throw new Error( `Cannot call ${this}.get('${String( @@ -197,8 +202,8 @@ export class PgClassExpressionStep< } const sqlExpr = pgClassExpression(this.getParentStep(), attribute.codec); return sqlExpr`${sql.parens(this.expression, true)}.${sql.identifier( - attributeName as string, - )}` as any; + attributeName, + )}`; } public getParentStep(): PgClassSingleStep | PgUnionAllSingleStep { @@ -265,8 +270,8 @@ export class PgClassExpressionStep< * that will be selected. */ function pgClassExpression< - TExpressionCodec extends PgCodec, - TResource extends PgResource, + TExpressionCodec extends AnyPgCodec, + TResource extends AnyPgResource, >( table: PgClassSingleStep | PgUnionAllSingleStep, codec: TExpressionCodec, diff --git a/grafast/dataplan-pg/src/steps/pgCondition.ts b/grafast/dataplan-pg/src/steps/pgCondition.ts index 1f4d656d03..76a5d647bd 100644 --- a/grafast/dataplan-pg/src/steps/pgCondition.ts +++ b/grafast/dataplan-pg/src/steps/pgCondition.ts @@ -4,7 +4,7 @@ import type { SQL } from "pg-sql2"; import { sql } from "pg-sql2"; import { TYPES } from "../index.js"; -import type { PgCodec } from "../interfaces.js"; +import type { AnyPgCodec, PgCodec } from "../interfaces.js"; export type PgWhereConditionSpec = | SQL @@ -22,7 +22,10 @@ export type PgConditionStepExtensions = DataplanPg.PgConditionStepExtensions; export interface PgConditionCapableParentStep extends BaseStep { alias: SQL; - placeholder($step: ExecutableStep, codec: PgCodec): SQL; + placeholder( + $step: ExecutableStep, + codec: TCodec, + ): SQL; where(condition: PgWhereConditionSpec): void; having?(condition: PgHavingConditionSpec): void; } @@ -135,7 +138,10 @@ export class PgConditionStep< this.havingConditions.push(condition); } - placeholder($step: ExecutableStep, codec: PgCodec): SQL { + placeholder( + $step: ExecutableStep, + codec: TCodec, + ): SQL { return this.$parent.placeholder($step, codec); } diff --git a/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts b/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts index 6792b584de..7a3b07b1bd 100644 --- a/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts @@ -7,18 +7,22 @@ import { ExecutableStep, exportAs, isDev, SafeError } from "grafast"; import type { SQL, SQLRawValue } from "pg-sql2"; import sql from "pg-sql2"; -import type { PgCodecAttribute } from "../codecs.js"; -import type { PgResource, PgResourceUnique } from "../index.js"; import { inspect } from "../inspect.js"; import type { + AnyPgCodec, GetPgResourceAttributes, GetPgResourceCodec, GetPgResourceUniques, - PgCodec, PlanByUniques, } from "../interfaces.js"; import type { PgClassExpressionStep } from "./pgClassExpression.js"; import { pgClassExpression } from "./pgClassExpression.js"; +import { + AnyPgResource, + DefaultPgResource, + PgResourceUnique, +} from "../datasource.js"; +import { PgCodecAttributeCodec, PgCodecAttributeName } from "../codecs.js"; type QueryValueDetailsBySymbol = Map< symbol, @@ -36,11 +40,14 @@ interface PgDeletePlanFinalizeResults { queryValueDetailsBySymbol: QueryValueDetailsBySymbol; } +export interface AnyPgDeleteSingleStep extends PgDeleteSingleStep {} +export interface DefaultPgDeleteSingleStep + extends PgDeleteSingleStep {} /** * Deletes a row in the database, can return columns from the deleted row. */ export class PgDeleteSingleStep< - TResource extends PgResource = PgResource, + TResource extends AnyPgResource, > extends ExecutableStep { static $$export = { moduleName: "@dataplan/pg", @@ -75,9 +82,9 @@ export class PgDeleteSingleStep< * The attributes and their dependency ids for us to find the record by. */ private getBys: Array<{ - name: keyof GetPgResourceAttributes; + name: PgCodecAttributeName>; depId: number; - pgCodec: PgCodec; + pgCodec: AnyPgCodec; }> = []; /** @@ -117,14 +124,18 @@ export class PgDeleteSingleStep< this.alias = sql.identifier(this.symbol); this.contextId = this.addDependency(this.resource.executor.context()); - const keys: ReadonlyArray> = getBy - ? (Object.keys(getBy) as Array>) + const keys = getBy + ? (Object.keys(getBy) as Array< + PgCodecAttributeName> + >) : []; if ( - !(this.resource.uniques as PgResourceUnique[]).some((uniq) => - uniq.attributes.every((key) => keys.includes(key as any)), - ) + !( + this.resource.uniques as Array< + PgResourceUnique> + > + ).some((uniq) => uniq.attributes.every((key) => keys.includes(key))) ) { throw new Error( `Attempted to build 'PgDeleteSingleStep' with a non-unique getBy keys ('${keys.join( @@ -145,13 +156,13 @@ export class PgDeleteSingleStep< ); } } - const value = (getBy as any)![name as any]; + const value = getBy[name]; const depId = this.addDependency(value); - const attribute = ( - this.resource.codec.attributes as GetPgResourceAttributes - )[name]; - const pgCodec = attribute.codec; - this.getBys.push({ name, depId, pgCodec }); + const attribute = this.resource.codec.attributes?.[name]; + if (attribute) { + const pgCodec = attribute.codec; + this.getBys.push({ name, depId, pgCodec }); + } }); } @@ -163,14 +174,15 @@ export class PgDeleteSingleStep< * Returns a plan representing a named attribute (e.g. column) from the newly * deleteed row. */ - get>( + get>>( attr: TAttr, ): PgClassExpressionStep< - GetPgResourceAttributes[TAttr]["codec"], + PgCodecAttributeCodec< + Extract, { name: TAttr }> + >, TResource > { - const resourceAttribute: PgCodecAttribute = - this.resource.codec.attributes![attr as string]; + const resourceAttribute = this.resource.codec.attributes![attr]; if (!resourceAttribute) { throw new Error( `${this.resource} does not define an attribute named '${String(attr)}'`, @@ -381,9 +393,7 @@ export class PgDeleteSingleStep< /** * Delete a row in `resource` identified by the `getBy` unique condition. */ -export function pgDeleteSingle< - TResource extends PgResource, ->( +export function pgDeleteSingle( resource: TResource, getBy: PlanByUniques< GetPgResourceAttributes, diff --git a/grafast/dataplan-pg/src/steps/pgInsertSingle.ts b/grafast/dataplan-pg/src/steps/pgInsertSingle.ts index 3881d6d468..cefe882c71 100644 --- a/grafast/dataplan-pg/src/steps/pgInsertSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgInsertSingle.ts @@ -9,17 +9,22 @@ import { ExecutableStep, exportAs, isDev, setter } from "grafast"; import type { SQL, SQLRawValue } from "pg-sql2"; import sql from "pg-sql2"; -import type { PgCodecAttribute } from "../codecs.js"; -import type { PgResource } from "../index.js"; +import type { + AnyPgCodecAttribute, + PgCodecAttribute, + PgCodecAttributeCodec, + PgCodecAttributeName, +} from "../codecs.js"; import { inspect } from "../inspect.js"; import type { + AnyPgCodec, GetPgResourceAttributes, GetPgResourceCodec, - PgCodec, PgTypedExecutableStep, } from "../interfaces.js"; import type { PgClassExpressionStep } from "./pgClassExpression.js"; import { pgClassExpression } from "./pgClassExpression.js"; +import { AnyPgResource } from "../datasource.js"; const EMPTY_MAP = new Map(); @@ -42,16 +47,13 @@ interface PgInsertSinglePlanFinalizeResults { /** * Inserts a row into resource with the given specified attribute values. */ -export class PgInsertSingleStep< - TResource extends PgResource = PgResource, - > +export class PgInsertSingleStep extends ExecutableStep< unknown[] // tuple depending on what's selected > implements SetterCapableStep<{ - [key in keyof GetPgResourceAttributes & - string]: ExecutableStep; + [attribute in GetPgResourceAttributes as PgCodecAttributeName]: ExecutableStep; }> { static $$export = { @@ -87,10 +89,10 @@ export class PgInsertSingleStep< * The attributes and their dependency ids for us to insert. */ private attributes: Array<{ - name: keyof GetPgResourceAttributes; + name: PgCodecAttributeName>; depId: number; // This isn't really needed, we can look it up in the codec, but it acts as a quick reference. - pgCodec: PgCodec; + pgCodec: AnyPgCodec; }> = []; /** @@ -117,9 +119,14 @@ export class PgInsertSingleStep< constructor( resource: TResource, attributes?: { - [key in keyof GetPgResourceAttributes]?: + [attribute in GetPgResourceAttributes as PgCodecAttributeName]?: | PgTypedExecutableStep< - GetPgResourceAttributes[key]["codec"] + PgCodecAttributeCodec< + Extract< + GetPgResourceAttributes, + { name: PgCodecAttributeName } + > + > > | ExecutableStep; }, @@ -131,12 +138,16 @@ export class PgInsertSingleStep< this.alias = sql.identifier(this.symbol); this.contextId = this.addDependency(this.resource.executor.context()); if (attributes) { - Object.entries(attributes).forEach(([key, value]) => { + ( + Object.entries(attributes) as Array< + [ + PgCodecAttributeName>, + ExecutableStep, + ] + > + ).forEach(([key, value]) => { if (value) { - this.set( - key as keyof GetPgResourceAttributes, - value as ExecutableStep, - ); + this.set(key, value); } }); } @@ -146,7 +157,7 @@ export class PgInsertSingleStep< return `${this.resource.name}(${this.attributes.map((a) => a.name)})`; } - set>( + set>>( name: TKey, value: ExecutableStep, // | PgTypedExecutableStep ): void { @@ -160,9 +171,7 @@ export class PgInsertSingleStep< ); } } - const attribute = ( - this.resource.codec.attributes as GetPgResourceAttributes - )?.[name]; + const attribute = this.resource.codec.attributes?.[name]; if (!attribute) { throw new Error( `Attribute ${String(name)} not found in ${this.resource.codec}`, @@ -175,8 +184,7 @@ export class PgInsertSingleStep< setPlan(): SetterStep< { - [key in keyof GetPgResourceAttributes & - string]: ExecutableStep; + [attribute in GetPgResourceAttributes as PgCodecAttributeName]: ExecutableStep; }, this > { @@ -192,12 +200,13 @@ export class PgInsertSingleStep< * Returns a plan representing a named attribute (e.g. column) from the newly * inserted row. */ - get>( + get>>( attr: TAttr, ): PgClassExpressionStep< - GetPgResourceAttributes[TAttr] extends PgCodecAttribute< - infer UCodec - > + Extract< + GetPgResourceAttributes, + { name: TAttr } + > extends PgCodecAttribute ? UCodec : never, TResource @@ -205,7 +214,7 @@ export class PgInsertSingleStep< if (!this.resource.codec.attributes) { throw new Error(`Cannot call .get() when there's no attributes.`); } - const resourceAttribute: PgCodecAttribute = + const resourceAttribute: AnyPgCodecAttribute = this.resource.codec.attributes[attr as string]; if (!resourceAttribute) { throw new Error( @@ -411,13 +420,18 @@ export class PgInsertSingleStep< /** * Inserts a row into resource with the given specified attribute values. */ -export function pgInsertSingle< - TResource extends PgResource, ->( +export function pgInsertSingle( resource: TResource, attributes?: { - [key in keyof GetPgResourceAttributes]?: - | PgTypedExecutableStep[key]["codec"]> + [attribute in GetPgResourceAttributes as PgCodecAttributeName]?: + | PgTypedExecutableStep< + PgCodecAttributeCodec< + Extract< + GetPgResourceAttributes, + { name: PgCodecAttributeName } + > + > + > | ExecutableStep; }, ): PgInsertSingleStep { diff --git a/grafast/dataplan-pg/src/steps/pgSelect.ts b/grafast/dataplan-pg/src/steps/pgSelect.ts index 04bdca06fc..66ec8bd017 100644 --- a/grafast/dataplan-pg/src/steps/pgSelect.ts +++ b/grafast/dataplan-pg/src/steps/pgSelect.ts @@ -42,15 +42,22 @@ import { import type { SQL, SQLRawValue } from "pg-sql2"; import sql, { $$symbolToIdentifier, arraysMatch } from "pg-sql2"; -import type { PgCodecAttributes } from "../codecs.js"; import { listOfCodec, TYPES } from "../codecs.js"; -import type { PgResource, PgResourceUnique } from "../datasource.js"; import type { + AnyPgResource, + AnyPgResourceUnique, + AnyScalarPgResource, + DefaultPgResource, + PgResource, + PgResourceCodec, +} from "../datasource.js"; +import type { + AnyPgCodec, GetPgResourceAttributes, GetPgResourceCodec, + GetPgResourceRelationConfigs, GetPgResourceRelations, PgCodec, - PgCodecRelation, PgGroupSpec, PgOrderSpec, PgTypedExecutableStep, @@ -157,26 +164,26 @@ type PgSelectPlanJoin = */ type PgSelectPlaceholder = { dependencyIndex: number; - codec: PgCodec; + codec: AnyPgCodec; symbol: symbol; }; export type PgSelectIdentifierSpec = | { step: ExecutableStep; - codec: PgCodec; + codec: AnyPgCodec; matches: (alias: SQL) => SQL; } | { step: PgTypedExecutableStep; - codec?: PgCodec; + codec?: AnyPgCodec; matches: (alias: SQL) => SQL; }; export type PgSelectArgumentSpec = | { step: ExecutableStep; - pgCodec: PgCodec; + pgCodec: AnyPgCodec; name?: string; } | { @@ -192,7 +199,7 @@ export interface PgSelectArgumentDigest { interface QueryValue { dependencyIndex: number; - codec: PgCodec; + codec: AnyPgCodec; } function assertSensible(step: ExecutableStep): void { @@ -210,9 +217,7 @@ function assertSensible(step: ExecutableStep): void { export type PgSelectMode = "normal" | "aggregate" | "mutation"; -export interface PgSelectOptions< - TResource extends PgResource = PgResource, -> { +export interface PgSelectOptions { /** * Tells us what we're dealing with - data type, columns, where to get it * from, what it's called, etc. Many of these details can be overridden @@ -260,6 +265,9 @@ export interface PgSelectOptions< joinAsLateral?: boolean; } +export interface DefaultPgSelectStep extends PgSelectStep {} +export interface AnyPgSelectStep extends PgSelectStep {} + /** * This represents selecting from a class-like entity (table, view, etc); i.e. * it represents `SELECT , FROM `. You can also add @@ -269,9 +277,7 @@ export interface PgSelectOptions< * don't allow `UNION`/`INTERSECT`/`EXCEPT`/`FOR UPDATE`/etc at this time, * purely because it hasn't been sufficiently considered. */ -export class PgSelectStep< - TResource extends PgResource = PgResource, - > +export class PgSelectStep extends ExecutableStep< ReadonlyArray > @@ -322,7 +328,10 @@ export class PgSelectStep< // JOIN - private relationJoins: Map, SQL>; + private relationJoins: Map< + GetPgResourceRelationConfigs["name"], + SQL + >; private joins: Array; // WHERE @@ -483,8 +492,9 @@ export class PgSelectStep< // -------------------- public readonly mode: PgSelectMode; - - private locker: PgLocker = new PgLocker(this); + // DO NOT REMOVE THE `any` here. It'll give massive hard to track type errors if you do. + // I suspect it's due to circular type inference issues. + private locker = new PgLocker(this); constructor(options: PgSelectOptions); constructor(cloneFrom: PgSelectStep, mode?: PgSelectMode); @@ -763,11 +773,11 @@ export class PgSelectStep< return this.isUnique; } - public placeholder($step: PgTypedExecutableStep): SQL; - public placeholder($step: ExecutableStep, codec: PgCodec): SQL; + public placeholder($step: PgTypedExecutableStep): SQL; + public placeholder($step: ExecutableStep, codec: AnyPgCodec): SQL; public placeholder( - $step: ExecutableStep | PgTypedExecutableStep, - overrideCodec?: PgCodec, + $step: ExecutableStep | PgTypedExecutableStep, + overrideCodec?: AnyPgCodec, ): SQL { if (this.locker.locked) { throw new Error(`${this}: cannot add placeholders once plan is locked`); @@ -809,9 +819,7 @@ export class PgSelectStep< public singleRelation< TRelationName extends keyof GetPgResourceRelations & string, >(relationIdentifier: TRelationName): SQL { - const relation = this.resource.getRelation( - relationIdentifier, - ) as PgCodecRelation; + const relation = this.resource.getRelation(relationIdentifier); if (!relation) { throw new Error( `${this.resource} does not have a relation named '${String( @@ -2196,7 +2204,7 @@ ${lateralText};`; if ($p === this) { return true; } - const p = $p as PgSelectStep; + const p = $p as PgSelectStep; // If SELECT, FROM, JOIN, WHERE, ORDER, GROUP BY, HAVING, LIMIT, OFFSET // all match with one of our peers then we can replace ourself with one // of our peers. NOTE: we do _not_ merge SELECTs at this stage because @@ -2376,7 +2384,7 @@ ${lateralText};`; } } - private mergeSelectsWith>( + private mergeSelectsWith>( otherPlan: TOtherStep, ): { [desiredIndex: string]: string; @@ -2400,7 +2408,7 @@ ${lateralText};`; return actualKeyByDesiredKey; } - private mergePlaceholdersInto>( + private mergePlaceholdersInto>( otherPlan: TOtherStep, ): void { for (const placeholder of this.placeholders) { @@ -2479,7 +2487,7 @@ ${lateralText};`; !this.joins.some((j) => j.type !== "left") ) { // Inline ourself into our parent if we can. - let t: PgSelectStep | null | undefined = undefined; + let t: PgSelectStep | null | undefined = undefined; let p: ExecutableStep | undefined = undefined; for ( let dependencyIndex = 0, l = this.dependencies.length; @@ -2782,9 +2790,9 @@ ${lateralText};`; * Beware: if you call this and the database might actually return more than * one record then you're potentially in for a Bad Time. */ - singleAsRecord( - options?: PgSelectSinglePlanOptions, - ): PgSelectSingleStep { + singleAsRecord< + TSelectSinglePlanOptions extends PgSelectSinglePlanOptions, + >(options?: TSelectSinglePlanOptions): PgSelectSingleStep { this.setUnique(true); return new PgSelectSingleStep(this, first(this), options); } @@ -2798,28 +2806,26 @@ ${lateralText};`; * Beware: if you call this and the database might actually return more than * one record then you're potentially in for a Bad Time. */ - single( - options?: PgSelectSinglePlanOptions, - ): TResource extends PgResource< - any, - PgCodec, - any, - any, - any - > - ? UAttributes extends PgCodecAttributes - ? PgSelectSingleStep - : PgClassExpressionStep< - PgCodec, - TResource - > - : never { + single>( + options?: TSelectSinglePlanOptions, + ): [GetPgResourceAttributes] extends [never] + ? PgClassExpressionStep, TResource> + : PgSelectSingleStep; + single>( + options?: TSelectSinglePlanOptions, + ): + | PgClassExpressionStep, TResource> + | PgSelectSingleStep { const $single = this.singleAsRecord(options); const isScalar = !this.resource.codec.attributes; - return (isScalar ? $single.getSelfNamed() : $single) as any; + + return isScalar ? $single.getSelfNamed() : $single; } - row($row: ExecutableStep, options?: PgSelectSinglePlanOptions) { + row>( + $row: ExecutableStep, + options?: TSelectSinglePlanOptions, + ) { return new PgSelectSingleStep(this, $row, options); } @@ -2838,23 +2844,18 @@ ${lateralText};`; */ listItem( itemPlan: ExecutableStep, - ): TResource extends PgResource< - any, - PgCodec, - any, - any, - any - > - ? UAttributes extends PgCodecAttributes - ? PgSelectSingleStep - : PgClassExpressionStep< - PgCodec, - TResource - > - : never { + ): TResource extends AnyScalarPgResource + ? PgClassExpressionStep, TResource> + : PgSelectSingleStep; + listItem( + itemPlan: ExecutableStep, + ): + | PgClassExpressionStep, TResource> + | PgSelectSingleStep { const $single = new PgSelectSingleStep(this, itemPlan); const isScalar = !this.resource.codec.attributes; - return (isScalar ? $single.getSelfNamed() : $single) as any; + + return isScalar ? $single.getSelfNamed() : $single; } // -------------------- @@ -2896,16 +2897,18 @@ function joinMatches( /** * Apply a default order in case our default is not unique. */ -function ensureOrderIsUnique(step: PgSelectStep) { +function ensureOrderIsUnique< + TResource extends PgResource, +>(step: PgSelectStep) { // No need to order a unique record if (step.unique()) return; - const unique = (step.resource.uniques as PgResourceUnique[])[0]; + const unique = step.resource.uniques[0]; if (unique !== undefined) { const ordersIsUnique = step.orderIsUnique(); if (!ordersIsUnique) { unique.attributes.forEach((c) => { step.orderBy({ - fragment: sql`${step.alias}.${sql.identifier(c as string)}`, + fragment: sql`${step.alias}.${sql.identifier(c)}`, codec: step.resource.codec.attributes![c].codec, direction: "ASC", }); @@ -2915,7 +2918,7 @@ function ensureOrderIsUnique(step: PgSelectStep) { } } -export function pgSelect>( +export function pgSelect( options: PgSelectOptions, ): PgSelectStep { return new PgSelectStep(options); @@ -2925,21 +2928,11 @@ exportAs("@dataplan/pg", pgSelect, "pgSelect"); /** * Turns a list of records (e.g. from PgSelectSingleStep.record()) back into a PgSelect. */ -export function pgSelectFromRecords< - TResource extends PgResource, ->( +export function pgSelectFromRecords( resource: TResource, records: | PgClassExpressionStep< - PgCodec< - any, - undefined, - any, - any, - GetPgResourceCodec, - any, - any - >, + PgCodec, any, any>, TResource > | ExecutableStep, @@ -2949,7 +2942,7 @@ export function pgSelectFromRecords< identifiers: [], from: (records) => sql`unnest(${records.placeholder})`, args: [{ step: records, pgCodec: listOfCodec(resource.codec) }], - }) as PgSelectStep; + }); } exportAs("@dataplan/pg", pgSelectFromRecords, "pgSelectFromRecords"); @@ -2972,7 +2965,7 @@ exportAs("@dataplan/pg", sqlFromArgDigests, "sqlFromArgDigests"); export function digestsFromArgumentSpecs( $placeholderable: { - placeholder(step: ExecutableStep, codec: PgCodec): SQL; + placeholder(step: ExecutableStep, codec: AnyPgCodec): SQL; }, specs: PgSelectArgumentSpec[], digests: PgSelectArgumentDigest[] = [], @@ -3010,8 +3003,8 @@ exportAs("@dataplan/pg", digestsFromArgumentSpecs, "digestsFromArgumentSpecs"); export function getFragmentAndCodecFromOrder( alias: SQL, order: PgOrderSpec, - codecOrCodecs: PgCodec | PgCodec[], -): [fragment: SQL, codec: PgCodec, isNullable?: boolean] { + codecOrCodecs: AnyPgCodec | AnyPgCodec[], +): [fragment: SQL, codec: AnyPgCodec, isNullable?: boolean] { if (order.attribute != null) { const colFrag = sql`${alias}.${sql.identifier(order.attribute)}`; const isArray = Array.isArray(codecOrCodecs); diff --git a/grafast/dataplan-pg/src/steps/pgSelectSingle.ts b/grafast/dataplan-pg/src/steps/pgSelectSingle.ts index 6e2f4b454a..61c4912138 100644 --- a/grafast/dataplan-pg/src/steps/pgSelectSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgSelectSingle.ts @@ -10,17 +10,31 @@ import type { SQL } from "pg-sql2"; import sql from "pg-sql2"; import type { + AnyPgCodecAttribute, ObjectFromPgCodecAttributes, - PgCodecAttribute, + PgCodecAttributeCodec, + PgCodecAttributeName, } from "../codecs.js"; import { TYPES } from "../codecs.js"; -import type { PgResource } from "../datasource.js"; import type { + AnyPgResource, + DefaultPgResource, + PgResource, + PgResourceCodec, + PgResourceRegistry, +} from "../datasource.js"; +import type { + AnyPgCodec, + GetPgRegistryCodecRelationConfigs, + GetPgResourceAttributeMap, GetPgResourceAttributes, GetPgResourceCodec, + GetPgResourceRelationConfigs, GetPgResourceRelations, PgCodec, - PgCodecRelation, + PgCodecAttributes, + PgCodecPolymorphism, + PgCodecRelationConfigName, PgRegistry, PgTypedExecutableStep, } from "../interfaces.js"; @@ -36,13 +50,23 @@ import { getFragmentAndCodecFromOrder, PgSelectStep } from "./pgSelect.js"; // const debugPlanVerbose = debugPlan.extend("verbose"); // const debugExecuteVerbose = debugExecute.extend("verbose"); -export interface PgSelectSinglePlanOptions { - fromRelation?: [PgSelectSingleStep, string]; +export interface AnyPgSelectSinglePlanOptions + extends PgSelectSinglePlanOptions {} +export interface PgSelectSinglePlanOptions { + fromRelation?: readonly [ + PgSelectSingleStep, + PgCodecRelationConfigName< + GetPgRegistryCodecRelationConfigs< + PgResourceRegistry, + PgResourceCodec + > + >, + ]; } // Types that only take a few bytes so adding them to the selection would be // cheap to do. -const CHEAP_ATTRIBUTE_TYPES = new Set([ +const CHEAP_ATTRIBUTE_TYPES = new Set([ TYPES.int2, TYPES.int, TYPES.bigint, @@ -55,6 +79,14 @@ const CHEAP_ATTRIBUTE_TYPES = new Set([ TYPES.timestamptz, ]); +export interface AnyPgSelectSingleStep extends PgSelectSingleStep {} +export interface DefaultPgSelectSingleStep extends PgSelectSingleStep {} +export type PgSelectSingleStepResource = U extends PgSelectSingleStep< + infer TResource +> + ? TResource + : never; + /** * Represents the single result of a unique PgSelectStep. This might be * retrieved explicitly by PgSelectStep.single(), or implicitly (via Grafast) @@ -63,18 +95,12 @@ const CHEAP_ATTRIBUTE_TYPES = new Set([ * such as `.get` and `.cursor` which can receive specific properties by * telling the PgSelectStep to select the relevant expressions. */ -export class PgSelectSingleStep< - TResource extends PgResource = PgResource, - > +export class PgSelectSingleStep extends UnbatchedExecutableStep< unknown[] /* What we return will be a tuple based on the values selected */ > implements - PgTypedExecutableStep< - TResource extends PgResource - ? UCodec - : never - >, + PgTypedExecutableStep>, EdgeCapableStep { static $$export = { @@ -95,12 +121,12 @@ export class PgSelectSingleStep< constructor( $class: PgSelectStep, $item: ExecutableStep, - private options: PgSelectSinglePlanOptions = Object.create(null), + private options: PgSelectSinglePlanOptions = Object.create(null), ) { super(); this.itemStepId = this.addDependency($item); this.resource = $class.resource; - this.pgCodec = this.resource.codec as GetPgResourceCodec; + this.pgCodec = this.resource.codec; this.mode = $class.mode; this.classStepId = $class.id; } @@ -151,15 +177,16 @@ export class PgSelectSingleStep< * Returns a plan representing a named attribute (e.g. column) from the class * (e.g. table). */ - get>( + public get< + TAttr extends PgCodecAttributeName< + PgCodecAttributes> + >, + >( attr: TAttr, ): PgClassExpressionStep< - GetPgResourceAttributes[TAttr] extends PgCodecAttribute< - infer UCodec, - any - > - ? UCodec - : never, + PgCodecAttributeCodec< + Extract, { name: TAttr }> + >, TResource > { if (this.mode === "aggregate") { @@ -171,8 +198,7 @@ export class PgSelectSingleStep< ); } const classPlan = this.getClassStep(); - const resourceAttribute: PgCodecAttribute | undefined = - this.resource.codec.attributes?.[attr as string]; + const resourceAttribute = this.resource.codec.attributes?.[attr]; if (!resourceAttribute && attr !== "") { throw new Error( `${this.resource} does not define an attribute named '${String(attr)}'`, @@ -182,18 +208,18 @@ export class PgSelectSingleStep< if (resourceAttribute?.via) { const { relation, attribute } = this.resource.resolveVia( resourceAttribute.via, - attr as string, + attr, ); - return this.singleRelation(relation as any).get(attribute) as any; + return this.singleRelation(relation).get(attribute) as any; } if (resourceAttribute?.identicalVia) { const { relation, attribute } = this.resource.resolveVia( resourceAttribute.identicalVia, - attr as string, + attr, ); - const $existingPlan = this.existingSingleRelation(relation as any); + const $existingPlan = this.existingSingleRelation(relation); if ($existingPlan) { // Relation exists already; load it from there for efficiency return $existingPlan.get(attribute) as any; @@ -206,7 +232,10 @@ export class PgSelectSingleStep< const [$fromPlan, fromRelationName] = this.options.fromRelation; const matchingAttribute = ( Object.entries($fromPlan.resource.codec.attributes!) as Array< - [string, PgCodecAttribute] + [ + PgCodecAttributeName>, + AnyPgCodecAttribute, + ] > ).find(([name, col]) => { if (col.identicalVia) { @@ -221,7 +250,7 @@ export class PgSelectSingleStep< return false; }); if (matchingAttribute) { - return $fromPlan.get(matchingAttribute[0]) as any; + return $fromPlan.get(matchingAttribute[0]); } } @@ -236,7 +265,7 @@ export class PgSelectSingleStep< * decoding these string values. */ - const sqlExpr = pgClassExpression( + const sqlExpr = pgClassExpression( this, attr === "" ? this.resource.codec @@ -260,13 +289,13 @@ export class PgSelectSingleStep< this.nonNullAttribute = { attribute: resourceAttribute, attr }; } - return colPlan as any; + return colPlan; } /** * Returns a plan representing the result of an expression. */ - public select( + public select( fragment: SQL, codec: TExpressionCodec, ): PgClassExpressionStep { @@ -285,10 +314,13 @@ export class PgSelectSingleStep< } public placeholder($step: PgTypedExecutableStep): SQL; - public placeholder($step: ExecutableStep, codec: PgCodec): SQL; - public placeholder( + public placeholder( + $step: ExecutableStep, + codec: TCodec, + ): SQL; + public placeholder( $step: ExecutableStep | PgTypedExecutableStep, - overrideCodec?: PgCodec, + overrideCodec?: TCodec, ): SQL { return overrideCodec ? this.getClassStep().placeholder($step, overrideCodec) @@ -323,7 +355,9 @@ export class PgSelectSingleStep< } public singleRelation< - TRelationName extends keyof GetPgResourceRelations, + TRelationName extends PgCodecRelationConfigName< + GetPgResourceRelationConfigs + >, >( relationIdentifier: TRelationName, ): PgSelectSingleStep< @@ -333,9 +367,7 @@ export class PgSelectSingleStep< if ($existingPlan) { return $existingPlan; } - const relation = this.resource.getRelation( - relationIdentifier, - ) as PgCodecRelation; + const relation = this.resource.getRelation(relationIdentifier); if (!relation || !relation.isUnique) { throw new Error( `${String(relationIdentifier)} is not a unique relation on ${ @@ -345,11 +377,8 @@ export class PgSelectSingleStep< } const { remoteResource, remoteAttributes, localAttributes } = relation; - const options: PgSelectSinglePlanOptions = { - fromRelation: [ - this as PgSelectSingleStep, - relationIdentifier as string, - ], + const options = { + fromRelation: [this, relationIdentifier] as const, }; return remoteResource.get( remoteAttributes.reduce((memo, remoteAttribute, attributeIndex) => { @@ -357,19 +386,17 @@ export class PgSelectSingleStep< return memo; }, Object.create(null)), options, - ) as PgSelectSingleStep; + ); } public manyRelation< - TRelationName extends keyof GetPgResourceRelations, + TRelationName extends GetPgResourceRelationConfigs["name"], >( relationIdentifier: TRelationName, ): PgSelectStep< GetPgResourceRelations[TRelationName]["remoteResource"] > { - const relation = this.resource.getRelation( - relationIdentifier, - ) as PgCodecRelation; + const relation = this.resource.getRelation(relationIdentifier); if (!relation) { throw new Error( `${String(relationIdentifier)} is not a relation on ${this.resource}`, @@ -377,12 +404,12 @@ export class PgSelectSingleStep< } const { remoteResource, remoteAttributes, localAttributes } = relation; - return (remoteResource as PgResource).find( + return remoteResource.find( remoteAttributes.reduce((memo, remoteAttribute, attributeIndex) => { memo[remoteAttribute] = this.get(localAttributes[attributeIndex]); return memo; }, Object.create(null)), - ) as any; + ); } public record(): PgClassExpressionStep< @@ -456,7 +483,7 @@ export class PgSelectSingleStep< } planForType(type: GraphQLObjectType): ExecutableStep { - const poly = (this.resource.codec as PgCodec).polymorphism; + const poly: PgCodecPolymorphism = this.resource.codec.polymorphism; if (poly?.mode === "single") { return this; } else if (poly?.mode === "relational") { @@ -476,12 +503,12 @@ export class PgSelectSingleStep< } private nonNullAttribute: { - attribute: PgCodecAttribute; + attribute: AnyPgCodecAttribute; attr: string; } | null = null; private nullCheckAttributeIndex: number | null = null; optimize() { - const poly = (this.resource.codec as PgCodec).polymorphism; + const poly: PgCodecPolymorphism = this.resource.codec.polymorphism; if (poly?.mode === "single" || poly?.mode === "relational") { const $class = this.getClassStep(); this.typeStepIndexList = poly.typeAttributes.map((col) => { @@ -565,16 +592,18 @@ export class PgSelectSingleStep< unbatchedExecute( _extra: ExecutionExtra, - result: ObjectFromPgCodecAttributes>, + result: ObjectFromPgCodecAttributes>, ): unknown[] { if (result == null) { return this._coalesceToEmptyObject ? Object.create(null) : null; } else if (this.nullCheckAttributeIndex != null) { + // @ts-expect-error const nullIfAttributeNull = result[this.nullCheckAttributeIndex]; if (nullIfAttributeNull == null) { return this._coalesceToEmptyObject ? Object.create(null) : null; } } else if (this.nullCheckId != null) { + // @ts-expect-error const nullIfExpressionNotTrue = result[this.nullCheckId]; if ( nullIfExpressionNotTrue == null || @@ -597,7 +626,7 @@ export function pgSelectFromRecord< PgCodec, any, any, - PgRegistry + PgRegistry >, >( resource: TResource, @@ -616,9 +645,7 @@ export function pgSelectFromRecord< * Given a plan that represents a single record (via * PgSelectSingleStep.record()) this turns it back into a PgSelectSingleStep */ -export function pgSelectSingleFromRecord< - TResource extends PgResource, ->( +export function pgSelectSingleFromRecord( resource: TResource, $record: PgClassExpressionStep, TResource>, ): PgSelectSingleStep { diff --git a/grafast/dataplan-pg/src/steps/pgTempTable.ts b/grafast/dataplan-pg/src/steps/pgTempTable.ts index 7427af7ee9..35ee8d10c6 100644 --- a/grafast/dataplan-pg/src/steps/pgTempTable.ts +++ b/grafast/dataplan-pg/src/steps/pgTempTable.ts @@ -3,15 +3,13 @@ import { BaseStep } from "grafast"; import type { SQL } from "pg-sql2"; import { sql } from "pg-sql2"; -import type { PgResource } from "../datasource.js"; +import type { AnyPgResource } from "../datasource.js"; import type { PgClassFilterStep } from "../filters/pgClassFilter.js"; -import type { PgCodec } from "../interfaces.js"; +import type { AnyPgCodec } from "../interfaces.js"; import type { PgConditionCapableParentStep } from "./pgCondition.js"; import { PgConditionStep } from "./pgCondition.js"; -export class PgTempTableStep< - TResource extends PgResource, - > +export class PgTempTableStep extends BaseStep implements PgConditionCapableParentStep { @@ -30,7 +28,10 @@ export class PgTempTableStep< this.alias = sql.identifier(Symbol(`${resource.name}_filter`)); } - placeholder($step: ExecutableStep, codec: PgCodec): SQL { + placeholder( + $step: ExecutableStep, + codec: TCodec, + ): SQL { return this.$parent.placeholder($step, codec); } diff --git a/grafast/dataplan-pg/src/steps/pgUnionAll.ts b/grafast/dataplan-pg/src/steps/pgUnionAll.ts index 2ff0e8df35..aaf33300f7 100644 --- a/grafast/dataplan-pg/src/steps/pgUnionAll.ts +++ b/grafast/dataplan-pg/src/steps/pgUnionAll.ts @@ -30,13 +30,21 @@ import type { GraphQLObjectType } from "grafast/graphql"; import type { SQL, SQLRawValue } from "pg-sql2"; import { $$symbolToIdentifier, sql } from "pg-sql2"; -import type { PgCodecAttributes } from "../codecs.js"; import { TYPES } from "../codecs.js"; -import type { PgResource, PgResourceUnique } from "../datasource.js"; +import type { + AnyPgResource, + AnyPgResourceUnique, + PgResource, + PgResourceUnique, +} from "../datasource.js"; import type { PgExecutor } from "../executor.js"; -import type { PgCodecRefPath, PgCodecRelation, PgGroupSpec } from "../index.js"; import type { - PgCodec, + GetPgResourceAttributes, + PgCodecRefPath, + PgGroupSpec, +} from "../index.js"; +import type { + AnyPgCodec, PgOrderFragmentSpec, PgOrderSpec, PgTypedExecutableStep, @@ -121,7 +129,7 @@ type PgUnionAllStepSelect = | { type: "expression"; expression: SQL; - codec: PgCodec; + codec: AnyPgCodec; } | { type: "outerExpression"; @@ -130,19 +138,13 @@ type PgUnionAllStepSelect = export type PgUnionAllStepConfigAttributes = { [attributeName in TAttributes]: { - codec: PgCodec; + codec: AnyPgCodec; }; }; export interface PgUnionAllStepMember { typeName: TTypeNames; - resource: PgResource< - any, - any, - ReadonlyArray>, - any, - any - >; + resource: PgResource; match?: { [resourceAttributeName: string]: | { @@ -151,7 +153,7 @@ export interface PgUnionAllStepMember { } | { step: ExecutableStep; - codec: PgCodec; + codec: AnyPgCodec; }; }; path?: PgCodecRefPath; @@ -162,7 +164,7 @@ export interface PgUnionAllStepConfig< TTypeNames extends string, > { resourceByTypeName: { - [typeName in TTypeNames]: PgResource; + [typeName in TTypeNames]: AnyPgResource; }; attributes?: PgUnionAllStepConfigAttributes; members?: PgUnionAllStepMember[]; @@ -182,7 +184,7 @@ export interface PgUnionAllStepOrder { interface QueryValue { dependencyIndex: number; - codec: PgCodec; + codec: AnyPgCodec; alreadyEncoded: boolean; } @@ -198,7 +200,7 @@ interface QueryValue { */ type PgUnionAllPlaceholder = { dependencyIndex: number; - codec: PgCodec; + codec: AnyPgCodec; symbol: symbol; alreadyEncoded: boolean; }; @@ -239,9 +241,11 @@ export class PgUnionAllSingleStep // This type isn't handled; so it should never occur return constant(null); } - const pk = (resource.uniques as PgResourceUnique[] | undefined)?.find( - (u) => u.isPrimary === true, - ); + const pk = ( + resource.uniques as Array< + PgResourceUnique> + > + )?.find((u) => u.isPrimary === true); if (!pk) { throw new Error( `No PK found for ${objectType.name}; this should have been caught earlier?!`, @@ -298,7 +302,7 @@ export class PgUnionAllSingleStep /** * Returns a plan representing the result of an expression. */ - expression( + expression( expression: SQL, codec: TExpressionCodec, ): PgClassExpressionStep { @@ -315,7 +319,7 @@ export class PgUnionAllSingleStep return this.getClassStep().selectAndReturnIndex(fragment); } - public select( + public select( fragment: SQL, codec: TExpressionCodec, ): PgClassExpressionStep { @@ -341,13 +345,7 @@ export class PgUnionAllSingleStep interface MemberDigest { member: PgUnionAllStepMember; - finalResource: PgResource< - any, - any, - ReadonlyArray>, - any, - any - >; + finalResource: PgResource; sqlSource: SQL; symbol: symbol; alias: SQL; @@ -377,18 +375,24 @@ function cloneDigests( return digests.map(cloneDigest); } +export interface AnyPgUnionAllStep extends PgUnionAllStep {} + /** * Represents a `UNION ALL` statement, which can have multiple table-like * resources, but must return a consistent data shape. */ export class PgUnionAllStep< - TAttributes extends string = string, - TTypeNames extends string = string, + TAttributes extends string, + TTypeNames extends string, > extends ExecutableStep implements - ConnectionCapableStep, PgSelectParsedCursorStep> + ConnectionCapableStep< + PgSelectSingleStep, + PgSelectParsedCursorStep + > { + _PgUnionAllStep: "PgUnionAllStep" = "PgUnionAllStep"; static $$export = { moduleName: "@dataplan/pg", exportName: "PgUnionAllStep", @@ -675,9 +679,7 @@ export class PgUnionAllStep< let sqlSource = sql`${currentResource.from} as ${currentAlias}`; for (const pathEntry of path) { - const relation = currentResource.getRelation( - pathEntry.relationName, - ) as PgCodecRelation; + const relation = currentResource.getRelation(pathEntry.relationName); const nextResource = relation.remoteResource; const nextSymbol = Symbol(nextResource.name); const nextAlias = sql.identifier(nextSymbol); @@ -820,7 +822,10 @@ on (${sql.indent( return index; } - selectExpression(expression: SQL, codec: PgCodec): number { + selectExpression( + expression: SQL, + codec: TCodec, + ): number { const existingIndex = this.selects.findIndex( (s) => s.type === "expression" && sql.isEquivalent(s.expression, expression), @@ -1010,12 +1015,12 @@ on (${sql.indent( public placeholder($step: PgTypedExecutableStep): SQL; public placeholder( $step: ExecutableStep, - codec: PgCodec, + codec: AnyPgCodec, alreadyEncoded?: boolean, ): SQL; public placeholder( $step: ExecutableStep | PgTypedExecutableStep, - overrideCodec?: PgCodec, + overrideCodec?: AnyPgCodec, alreadyEncoded = false, ): SQL { if (this.locker.locked) { @@ -1128,7 +1133,7 @@ on (${sql.indent( ); identifierPlaceholders[i] = this.placeholder( toPg(access($parsedCursorPlan, [i + 1]), codec), - codec as PgCodec, + codec, ); } else { // No implementations?! @@ -1143,7 +1148,7 @@ on (${sql.indent( } const max = orderCount - 1 + pk.attributes.length; const pkPlaceholder = identifierPlaceholders[orderCount - 1]; - const pkAttributes = finalResource.codec.attributes as PgCodecAttributes; + const pkAttributes = finalResource.codec.attributes; const condition = (i = 0): SQL => { const order = digest.orders[i]; const [ @@ -1562,7 +1567,7 @@ and ${condition(i + 1)}`} const midSelects: SQL[] = []; const innerSelects = this.selects .map((s, selectIndex) => { - const r = ((): [SQL, PgCodec] | null => { + const r = ((): [SQL, AnyPgCodec] | null => { switch (s.type) { case "attribute": { if (!this.spec.attributes) { diff --git a/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts b/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts index d2b58662ce..31f59f77b4 100644 --- a/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts @@ -7,18 +7,19 @@ import { ExecutableStep, exportAs, isDev, SafeError, setter } from "grafast"; import type { SQL, SQLRawValue } from "pg-sql2"; import sql from "pg-sql2"; -import type { PgCodecAttribute } from "../codecs.js"; -import type { PgResource, PgResourceUnique } from "../index.js"; +import type { AnyPgCodecAttribute, PgCodecAttributeName } from "../codecs.js"; +import type { DefaultPgResource, PgResourceUnique } from "../index.js"; import { inspect } from "../inspect.js"; import type { + AnyPgCodec, GetPgResourceAttributes, GetPgResourceCodec, GetPgResourceUniques, - PgCodec, PlanByUniques, } from "../interfaces.js"; import type { PgClassExpressionStep } from "./pgClassExpression.js"; import { pgClassExpression } from "./pgClassExpression.js"; +import { AnyPgResource, PgResourceUniques } from "../datasource.js"; type QueryValueDetailsBySymbol = Map< symbol, @@ -35,12 +36,14 @@ interface PgUpdatePlanFinalizeResults { /** When we see the given symbol in the SQL values, what dependency do we replace it with? */ queryValueDetailsBySymbol: QueryValueDetailsBySymbol; } - +export interface AnyPgUpdateSingleStep extends PgUpdateSingleStep {} +export interface DefaultPgUpdateSingleStep + extends PgUpdateSingleStep {} /** * Update a single row identified by the 'getBy' argument. */ export class PgUpdateSingleStep< - TResource extends PgResource = PgResource, + TResource extends AnyPgResource, > extends ExecutableStep { static $$export = { moduleName: "@dataplan/pg", @@ -75,18 +78,18 @@ export class PgUpdateSingleStep< * The attributes and their dependency ids for us to find the record by. */ private getBys: Array<{ - name: keyof GetPgResourceAttributes; + name: PgCodecAttributeName>; depId: number; - pgCodec: PgCodec; + pgCodec: AnyPgCodec; }> = []; /** * The attributes and their dependency ids for us to update. */ private attributes: Array<{ - name: keyof GetPgResourceAttributes; + name: PgCodecAttributeName>; depId: number; - pgCodec: PgCodec; + pgCodec: AnyPgCodec; }> = []; /** @@ -114,10 +117,10 @@ export class PgUpdateSingleStep< resource: TResource, getBy: PlanByUniques< GetPgResourceAttributes, - GetPgResourceUniques + PgResourceUniques >, attributes?: { - [key in keyof GetPgResourceAttributes]?: ExecutableStep; // | PgTypedExecutableStep + [attribute in GetPgResourceAttributes as PgCodecAttributeName]?: ExecutableStep; // | PgTypedExecutableStep }, ) { super(); @@ -127,14 +130,18 @@ export class PgUpdateSingleStep< this.alias = sql.identifier(this.symbol); this.contextId = this.addDependency(this.resource.executor.context()); - const keys: ReadonlyArray> = getBy - ? (Object.keys(getBy) as Array>) + const keys = getBy + ? (Object.keys(getBy) as Array< + PgCodecAttributeName> + >) : []; if ( - !(this.resource.uniques as PgResourceUnique[]).some((uniq) => - uniq.attributes.every((key) => keys.includes(key as any)), - ) + !( + this.resource.uniques as Array< + PgResourceUnique> + > + ).some((uniq) => uniq.attributes.every((key) => keys.includes(key))) ) { throw new Error( `Attempted to build 'PgUpdateSingleStep' with a non-unique getBy keys ('${keys.join( @@ -155,22 +162,26 @@ export class PgUpdateSingleStep< ); } } - const value = (getBy as any)![name as any]; + const value = getBy[name]; const depId = this.addDependency(value); - const attribute = ( - this.resource.codec.attributes as GetPgResourceAttributes - )[name]; - const pgCodec = attribute.codec; - this.getBys.push({ name, depId, pgCodec }); + const attribute = this.resource.codec.attributes?.[name]; + if (attribute) { + const pgCodec = attribute.codec; + this.getBys.push({ name, depId, pgCodec }); + } }); if (attributes) { - Object.entries(attributes).forEach(([key, value]) => { + ( + Object.entries(attributes) as Array< + [ + PgCodecAttributeName>, + ExecutableStep, + ] + > + ).forEach(([key, value]) => { if (value) { - this.set( - key as keyof GetPgResourceAttributes, - value as ExecutableStep, - ); + this.set(key, value as ExecutableStep); } }); } @@ -182,9 +193,9 @@ export class PgUpdateSingleStep< )};${this.attributes.map((a) => a.name)})`; } - set>( + set>>( name: TKey, - value: ExecutableStep, // | PgTypedExecutableStep + value: ExecutableStep, // PgTypedExecutableStep[number]["codec"]>, ): void { if (this.locked) { throw new Error("Cannot set after plan is locked."); @@ -196,17 +207,16 @@ export class PgUpdateSingleStep< ); } } - const { codec: pgCodec } = ( - this.resource.codec.attributes as GetPgResourceAttributes - )[name]; - const depId = this.addDependency(value); - this.attributes.push({ name, depId, pgCodec }); + const attribute = this.resource.codec.attributes?.[name]; + if (attribute) { + const depId = this.addDependency(value); + this.attributes.push({ name, depId, pgCodec: attribute.codec }); + } } setPlan(): SetterStep< { - [key in keyof GetPgResourceAttributes & - string]: ExecutableStep; + [attribute in GetPgResourceAttributes as PgCodecAttributeName]: ExecutableStep; }, this > { @@ -222,13 +232,13 @@ export class PgUpdateSingleStep< * Returns a plan representing a named attribute (e.g. column) from the newly * updateed row. */ - get>( + get>>( attr: TAttr, ): PgClassExpressionStep< - GetPgResourceAttributes[TAttr]["codec"], + Extract, { name: TAttr }>["codec"], TResource > { - const resourceAttribute: PgCodecAttribute = + const resourceAttribute: AnyPgCodecAttribute = this.resource.codec.attributes![attr as string]; if (!resourceAttribute) { throw new Error( @@ -450,16 +460,14 @@ export class PgUpdateSingleStep< /** * Update a single row identified by the 'getBy' argument. */ -export function pgUpdateSingle< - TResource extends PgResource, ->( +export function pgUpdateSingle( resource: TResource, getBy: PlanByUniques< GetPgResourceAttributes, GetPgResourceUniques >, attributes?: { - [key in keyof GetPgResourceAttributes]?: ExecutableStep; // | PgTypedExecutableStep + [attribute in GetPgResourceAttributes as PgCodecAttributeName]?: ExecutableStep; // | PgTypedExecutableStep }, ): PgUpdateSingleStep { return new PgUpdateSingleStep(resource, getBy, attributes); diff --git a/grafast/dataplan-pg/src/steps/toPg.ts b/grafast/dataplan-pg/src/steps/toPg.ts index 2f002d460a..b970306191 100644 --- a/grafast/dataplan-pg/src/steps/toPg.ts +++ b/grafast/dataplan-pg/src/steps/toPg.ts @@ -1,7 +1,7 @@ import type { ExecutableStep, ExecutionExtra } from "grafast"; import { UnbatchedExecutableStep } from "grafast"; -import type { PgCodec } from "../interfaces.js"; +import type { AnyPgCodec, PgCodec } from "../interfaces.js"; /** * Converts the given value to the representation suitable for feeding into the @@ -19,7 +19,7 @@ export class ToPgStep extends UnbatchedExecutableStep { isSyncAndSafe = true; constructor( $value: ExecutableStep, - private codec: PgCodec, + private codec: AnyPgCodec, ) { super(); this.addDependency($value); @@ -39,6 +39,6 @@ export class ToPgStep extends UnbatchedExecutableStep { * * @internal */ -export function toPg($value: ExecutableStep, codec: PgCodec) { +export function toPg($value: ExecutableStep, codec: AnyPgCodec) { return new ToPgStep($value, codec); } diff --git a/graphile-build/graphile-build-pg/src/examples/NO_DATA_GATHERING.ts b/graphile-build/graphile-build-pg/src/examples/NO_DATA_GATHERING.ts index 240c05e68e..b3ead311ae 100644 --- a/graphile-build/graphile-build-pg/src/examples/NO_DATA_GATHERING.ts +++ b/graphile-build/graphile-build-pg/src/examples/NO_DATA_GATHERING.ts @@ -395,7 +395,7 @@ async function main() { // We're crafting our own input const input: GraphileBuild.BuildInput = { - pgRegistry: pgRegistry as unknown as PgRegistry, + pgRegistry, }; const schema = buildSchema(config, input); diff --git a/graphile-build/graphile-build-pg/src/inputUtils.ts b/graphile-build/graphile-build-pg/src/inputUtils.ts index 81589c891e..8d2a53f704 100644 --- a/graphile-build/graphile-build-pg/src/inputUtils.ts +++ b/graphile-build/graphile-build-pg/src/inputUtils.ts @@ -1,6 +1,6 @@ import "graphile-build"; -import type { PgCodec, PgCodecRelation, PgResource } from "@dataplan/pg"; +import type { DefaultPgCodec, DefaultPgResource } from "@dataplan/pg"; /** * Metadata for a specific PgCodec @@ -18,12 +18,12 @@ export interface PgCodecMeta { /** * A map from PgCodec to its associated metadata. */ -export type PgCodecMetaLookup = Map; +export type PgCodecMetaLookup = Map; /** * Creates an empty meta object for the given codec. */ -export function makePgCodecMeta(_codec: PgCodec): PgCodecMeta { +export function makePgCodecMeta(_codec: DefaultPgCodec): PgCodecMeta { return { typeNameBySituation: Object.create(null), }; @@ -40,8 +40,8 @@ export function getCodecMetaLookupFromInput( input: GraphileBuild.BuildInput, ): PgCodecMetaLookup { const metaLookup: PgCodecMetaLookup = new Map(); - const seenResources = new Set(); - for (const codec of Object.values(input.pgRegistry.pgCodecs) as PgCodec[]) { + const seenResources = new Set(); + for (const codec of Object.values(input.pgRegistry.pgCodecs)) { walkCodec(codec, metaLookup); } for (const resource of Object.values(input.pgRegistry.pgResources)) { @@ -57,9 +57,9 @@ export function getCodecMetaLookupFromInput( * @internal */ function walkResource( - resource: PgResource, + resource: DefaultPgResource, metaLookup: PgCodecMetaLookup, - seenResources: Set, + seenResources: Set, ): void { if (seenResources.has(resource)) { return; @@ -68,7 +68,7 @@ function walkResource( if (!metaLookup.has(resource.codec)) { walkCodec(resource.codec, metaLookup); } - const relations = resource.getRelations() as Record; + const relations = resource.getRelations(); if (relations) { for (const relationshipName in relations) { walkResource( @@ -86,7 +86,7 @@ function walkResource( * * @internal */ -function walkCodec(codec: PgCodec, metaLookup: PgCodecMetaLookup): void { +function walkCodec(codec: DefaultPgCodec, metaLookup: PgCodecMetaLookup): void { if (metaLookup.has(codec)) { return; } diff --git a/graphile-build/graphile-build-pg/src/interfaces.ts b/graphile-build/graphile-build-pg/src/interfaces.ts index 0796780f1c..5d64224b5b 100644 --- a/graphile-build/graphile-build-pg/src/interfaces.ts +++ b/graphile-build/graphile-build-pg/src/interfaces.ts @@ -1,4 +1,4 @@ -import type { PgRegistry, WithPgClient } from "@dataplan/pg"; +import type { DefaultPgRegistry, PgRegistry, WithPgClient } from "@dataplan/pg"; import type { PromiseOrDirect } from "grafast"; declare global { namespace GraphileBuild { @@ -107,7 +107,7 @@ declare global { namespace GraphileBuild { interface BuildInput { - pgRegistry: PgRegistry; + pgRegistry: DefaultPgRegistry; } } } diff --git a/graphile-build/graphile-build-pg/src/plugins/PgAttributesPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgAttributesPlugin.ts index 83ec4ea39a..3eafa0d535 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgAttributesPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgAttributesPlugin.ts @@ -3,12 +3,11 @@ import "../interfaces.js"; import "graphile-config"; import type { + DefaultPgCodec, + DefaultPgCodecAttribute, + DefaultPgSelectSingleStep, PgClassExpressionStep, - PgCodec, - PgCodecAttribute, - PgCodecAttributes, PgCodecList, - PgCodecWithAttributes, PgConditionStep, PgSelectSingleStep, PgSelectStep, @@ -25,9 +24,9 @@ declare global { namespace GraphileBuild { interface Build { pgResolveOutputType( - codec: PgCodec, + codec: DefaultPgCodec, notNull?: boolean, - ): [baseCodec: PgCodec, resolvedType: GraphQLOutputType] | null; + ): [baseCodec: DefaultPgCodec, resolvedType: GraphQLOutputType] | null; } interface Inflection { @@ -42,7 +41,7 @@ declare global { _attributeName( this: GraphileBuild.Inflection, details: { - codec: PgCodecWithAttributes; + codec: DefaultPgCodec; attributeName: string; skipRowId?: boolean; }, @@ -54,7 +53,7 @@ declare global { */ _joinAttributeNames( this: GraphileBuild.Inflection, - codec: PgCodecWithAttributes, + codec: DefaultPgCodec, names: readonly string[], ): string; @@ -67,7 +66,7 @@ declare global { this: GraphileBuild.Inflection, details: { attributeName: string; - codec: PgCodecWithAttributes; + codec: DefaultPgCodec; }, ): string; } @@ -78,7 +77,7 @@ declare global { isPgBaseInput?: boolean; isPgRowType?: boolean; isPgCompoundType?: boolean; - pgAttribute?: PgCodecAttribute; + pgAttribute?: DefaultPgCodecAttribute; } } } @@ -102,11 +101,11 @@ function processAttribute( return; } - const pgCodec = rawPgCodec as PgCodecWithAttributes; + const pgCodec = rawPgCodec; const isInterface = context.type === "GraphQLInterfaceType"; - const attribute = pgCodec.attributes[attributeName]; + const attribute = pgCodec.attributes![attributeName]; if ( !build.behavior.pgCodecAttributeMatches( @@ -147,7 +146,7 @@ function processAttribute( }; const resource = baseCodec.attributes - ? build.pgTableResource(baseCodec as PgCodecWithAttributes, false) + ? build.pgTableResource(baseCodec, false) : null; if (baseCodec.attributes && !resource) { // We can't load codecs with attributes unless we know the executor. @@ -160,7 +159,7 @@ function processAttribute( if (!baseCodec.attributes) { // Simply get the value return EXPORTABLE( - (attributeName) => ($record: PgSelectSingleStep) => { + (attributeName) => ($record: DefaultPgSelectSingleStep) => { return $record.get(attributeName); }, [attributeName], @@ -257,7 +256,7 @@ export const PgAttributesPlugin: GraphileConfig.Plugin = { inflection: { add: { _attributeName(options, { attributeName, codec }) { - const attribute = codec.attributes[attributeName]; + const attribute = codec.attributes![attributeName]; return this.coerceToGraphQLName( attribute.extensions?.tags?.name || attributeName, ); @@ -290,8 +289,8 @@ export const PgAttributesPlugin: GraphileConfig.Plugin = { const behaviors = new Set([ "select base update insert filterBy orderBy", ]); - const attribute = codec.attributes[attributeName]; - function walk(codec: PgCodec) { + const attribute = codec.attributes![attributeName]; + function walk(codec: DefaultPgCodec) { if (codec.arrayOfCodec) { behaviors.add("-condition:attribute:filterBy"); behaviors.add(`-attribute:orderBy`); @@ -443,8 +442,8 @@ export const PgAttributesPlugin: GraphileConfig.Plugin = { ) { return fields; } - const pgCodec = rawPgCodec as PgCodecWithAttributes; - const allAttributes = pgCodec.attributes; + const pgCodec = rawPgCodec; + const allAttributes = pgCodec.attributes!; const allowedAttributes = pgCodec.polymorphism?.mode === "single" ? [ @@ -463,12 +462,12 @@ export const PgAttributesPlugin: GraphileConfig.Plugin = { */ ] : null; - const attributes = allowedAttributes - ? (Object.fromEntries( + const attributes = allowedAttributes! + ? Object.fromEntries( Object.entries(allAttributes).filter(([attrName, _attr]) => allowedAttributes.includes(attrName), ), - ) as PgCodecAttributes) + ) : allAttributes; return Object.entries(attributes).reduce( @@ -597,9 +596,9 @@ export const PgAttributesPlugin: GraphileConfig.Plugin = { function resolveOutputType( build: GraphileBuild.Build, - codec: PgCodec, + codec: DefaultPgCodec, notNull?: boolean, -): [baseCodec: PgCodec, resolvedType: GraphQLOutputType] | null { +): [baseCodec: DefaultPgCodec, resolvedType: GraphQLOutputType] | null { const { getGraphQLTypeByPgCodec, graphql: { GraphQLList, GraphQLNonNull, getNullableType, isOutputType }, diff --git a/graphile-build/graphile-build-pg/src/plugins/PgBasicsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgBasicsPlugin.ts index 134325096f..bc6691f20a 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgBasicsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgBasicsPlugin.ts @@ -2,15 +2,7 @@ import "./PgTablesPlugin.js"; import "../interfaces.js"; import "graphile-config"; -import type { - PgCodec, - PgCodecRef, - PgCodecRelation, - PgCodecWithAttributes, - PgRefDefinition, - PgResource, - PgResourceUnique, -} from "@dataplan/pg"; +import type { PgCodecRef, PgRefDefinition, PgResource } from "@dataplan/pg"; import * as dataplanPg from "@dataplan/pg"; import type { GraphQLType } from "grafast/graphql"; import { EXPORTABLE, gatherConfig } from "graphile-build"; @@ -29,19 +21,19 @@ declare global { "@dataplan/pg": string; } type HasGraphQLTypeForPgCodec = ( - codec: PgCodec, + codec: dataplanPg.DefaultPgCodec, situation?: string, ) => boolean; type GetGraphQLTypeByPgCodec = ( - codec: PgCodec, + codec: dataplanPg.DefaultPgCodec, situation: string, ) => GraphQLType | null; type GetGraphQLTypeNameByPgCodec = ( - codec: PgCodec, + codec: dataplanPg.DefaultPgCodec, situation: string, ) => string | null; type SetGraphQLTypeForPgCodec = ( - codec: PgCodec, + codec: dataplanPg.DefaultPgCodec, situations: string | string[], typeName: string, ) => void; @@ -92,26 +84,30 @@ declare global { /** * Get a table-like resource for the given codec, assuming exactly one exists. */ - pgTableResource( + pgTableResource( codec: TCodec, strict?: boolean, ): PgResource< string, TCodec, - ReadonlyArray, - undefined + dataplanPg.DefaultPgResourceUnique, + never, + dataplanPg.DefaultPgRegistry > | null; } interface BehaviorEntities { - pgCodec: PgCodec; - pgCodecAttribute: [codec: PgCodecWithAttributes, attributeName: string]; - pgResource: PgResource; + pgCodec: dataplanPg.DefaultPgCodec; + pgCodecAttribute: [ + codec: dataplanPg.DefaultPgCodec, + attributeName: string, + ]; + pgResource: dataplanPg.DefaultPgResource; pgResourceUnique: [ - resource: PgResource, - unique: PgResourceUnique, + resource: dataplanPg.DefaultPgResource, + unique: dataplanPg.DefaultPgResourceUnique, ]; - pgCodecRelation: PgCodecRelation; + pgCodecRelation: dataplanPg.DefaultPgRelation; pgCodecRef: PgCodecRef; pgRefDefinition: PgRefDefinition; } @@ -190,7 +186,7 @@ export const PgBasicsPlugin: GraphileConfig.Plugin = { `pgCodecAttribute no longer accepts (codec, attribute) - it now accepts (codec, attributeName). Please update your code. Sorry! (Changed in PostGraphile V5 alpha 13.)`, ); } - const attribute = codec.attributes[attributeName]; + const attribute = codec.attributes![attributeName]; return [ behavior, getBehavior([codec.extensions, attribute.extensions]), @@ -334,34 +330,41 @@ export const PgBasicsPlugin: GraphileConfig.Plugin = { } }; const resourceByCodecCacheUnstrict = new Map< - PgCodecWithAttributes, - PgResource | null + dataplanPg.DefaultPgCodec, + dataplanPg.DefaultPgResource | null >(); const resourceByCodecCacheStrict = new Map< - PgCodecWithAttributes, - PgResource | null + dataplanPg.DefaultPgCodec, + dataplanPg.DefaultPgResource | null >(); - const pgTableResource = ( + const pgTableResource = ( codec: TCodec, strict = true, ): PgResource< string, TCodec, - ReadonlyArray, - undefined + dataplanPg.DefaultPgResourceUnique, + never, + dataplanPg.DefaultPgRegistry > | null => { const resourceByCodecCache = strict ? resourceByCodecCacheStrict : resourceByCodecCacheUnstrict; if (resourceByCodecCache.has(codec)) { - return resourceByCodecCache.get(codec)!; + return resourceByCodecCache.get(codec)! as any; } const resources = Object.values( build.input.pgRegistry.pgResources, ).filter( ( - r: PgResource, - ): r is PgResource => + r, + ): r is PgResource< + string, + TCodec, + any, + never, + dataplanPg.DefaultPgRegistry + > => r.codec === codec && !r.parameters && r.executor === codec.executor && diff --git a/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts index 958823516b..8c685db40b 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts @@ -1,7 +1,9 @@ import type { + AnyPgCodecAttributesRecord, + DefaultPgCodec, + DefaultPgCodecAttribute, PgCodec, PgCodecAnyScalar, - PgCodecAttribute, PgCodecAttributes, PgCodecExtensions, PgEnumCodec, @@ -27,11 +29,11 @@ import { version } from "../version.js"; interface State { codecByTypeIdByDatabaseName: Map< string, - Map> + Map> >; codecByClassIdByDatabaseName: Map< string, - Map> + Map> >; } @@ -63,7 +65,7 @@ declare global { } interface Build { - allPgCodecs: Set; + allPgCodecs: Set; } interface ScopeObject { @@ -74,7 +76,7 @@ declare global { interface ScopeInputObject { isPgRangeInputType?: boolean; isPgRangeBoundInputType?: boolean; - pgCodec?: PgCodec; + pgCodec?: DefaultPgCodec; } } @@ -84,18 +86,18 @@ declare global { getCodecFromClass( serviceName: string, pgClassId: string, - ): Promise; + ): Promise; getCodecFromType( serviceName: string, pgTypeId: string, pgTypeModifier?: string | number | null, - ): Promise; + ): Promise; }; } interface GatherHooks { pgCodecs_PgCodec(event: { serviceName: string; - pgCodec: PgCodec; + pgCodec: DefaultPgCodec; pgClass?: PgClass; pgType: PgType; }): Promise | void; @@ -104,7 +106,7 @@ declare global { serviceName: string; pgClass: PgClass; pgAttribute: PgAttribute; - attribute: PgCodecAttribute; + attribute: DefaultPgCodecAttribute; }): Promise | void; pgCodecs_recordType_spec(event: { @@ -122,21 +124,21 @@ declare global { pgCodecs_rangeOfCodec_extensions(event: { serviceName: string; pgType: PgType; - innerCodec: PgCodec; + innerCodec: DefaultPgCodec; extensions: any; }): Promise | void; pgCodecs_domainOfCodec_extensions(event: { serviceName: string; pgType: PgType; - innerCodec: PgCodec; + innerCodec: DefaultPgCodec; extensions: any; }): Promise | void; pgCodecs_listOfCodec_extensions(event: { serviceName: string; pgType: PgType; - innerCodec: PgCodec; + innerCodec: DefaultPgCodec; extensions: any; }): Promise | void; } @@ -308,7 +310,7 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { ); } - const attributes: PgCodecAttributes = Object.create(null); + const attributes: AnyPgCodecAttributesRecord = Object.create(null); const allAttributes = await info.helpers.pgIntrospection.getAttributesForClass( serviceName, @@ -465,7 +467,7 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { return map.get(typeId)!; } - const promise = (async (): Promise => { + const promise = (async (): Promise => { const type = await info.helpers.pgIntrospection.getType( serviceName, typeId, @@ -502,7 +504,7 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { ); } - const codec = await (async (): Promise => { + const codec = await (async (): Promise => { const namespace = await info.helpers.pgIntrospection.getNamespace( serviceName, type.typnamespace, @@ -603,12 +605,10 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { `Failed to get range entry related to '${type._id}'`, ); } - const innerCodec = (await info.helpers.pgCodecs.getCodecFromType( + const innerCodec = await info.helpers.pgCodecs.getCodecFromType( serviceName, range.rngsubtype!, - )) as - | PgCodec - | undefined; + ); const namespaceName = namespace.nspname; const typeName = type.typname; if (!innerCodec) { @@ -726,7 +726,7 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { description, extensions, notNull, - }) as PgCodec, + }), [ codecName, description, @@ -749,14 +749,11 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { ); if (innerType) { - const innerCodec = - (await info.helpers.pgCodecs.getCodecFromType( - serviceName, - innerType._id, - typeModifier, // TODO: is it correct to pass this through? - )) as - | PgCodec - | undefined; + const innerCodec = await info.helpers.pgCodecs.getCodecFromType( + serviceName, + innerType._id, + typeModifier, // TODO: is it correct to pass this through? + ); if (innerCodec) { const typeDelim = innerType.typdelim!; const { tags, description } = type.getTagsAndDescription(); @@ -830,7 +827,7 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { hooks: { async pgRegistry_PgRegistryBuilder_pgCodecs(info, event) { const { registryBuilder } = event; - const codecs = new Set(); + const codecs = new Set(); // If we get errors from the frozen object then clearly we need to // ensure more work has completed before continuing - call other plugin @@ -865,8 +862,8 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { schema: { hooks: { build(build) { - build.allPgCodecs = new Set(); - function walkCodec(codec: PgCodec): void { + build.allPgCodecs = new Set(); + function walkCodec(codec: DefaultPgCodec): void { if (build.allPgCodecs!.has(codec)) { return; } @@ -878,9 +875,7 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { if (codec.attributes) { for (const attributeName in codec.attributes) { - const attributeCodec = (codec.attributes as PgCodecAttributes)[ - attributeName - ].codec; + const attributeCodec = codec.attributes[attributeName].codec; walkCodec(attributeCodec); } } @@ -906,15 +901,13 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { } } if (build.input.pgRegistry.pgCodecs) { - for (const codec of Object.values( - build.input.pgRegistry.pgCodecs, - ) as PgCodec[]) { + for (const codec of Object.values(build.input.pgRegistry.pgCodecs)) { walkCodec(codec); } } // Ensure all sources are uniquely named - const knownCodecByName = new Map(); + const knownCodecByName = new Map(); for (const codec of build.allPgCodecs) { const known = knownCodecByName.get(codec.name); if (known === codec) { @@ -1090,8 +1083,8 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { // Now walk over all the codecs and ensure that each on has an associated type function prepareTypeForCodec( - codec: PgCodec, - visited: Set, + codec: DefaultPgCodec, + visited: Set, ): void { if (visited.has(codec)) { return; @@ -1109,9 +1102,7 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { // Process all the attributes (if any), then exit. if (codec.attributes) { for (const attributeName in codec.attributes) { - const attributeCodec = (codec.attributes as PgCodecAttributes)[ - attributeName - ].codec; + const attributeCodec = codec.attributes[attributeName].codec; prepareTypeForCodec(attributeCodec, visited); } @@ -1163,11 +1154,10 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { typeName, ); } else if (codec.rangeOfCodec || codec.domainOfCodec) { - const underlyingType = - codec.rangeOfCodec || + const underlyingType = (codec.rangeOfCodec || codec.domainOfCodec?.arrayOfCodec || codec.domainOfCodec?.rangeOfCodec || - codec.domainOfCodec; + codec.domainOfCodec)!; // This type is a "domain", so we can mimic the underlying type const underlyingOutputTypeName = build.getGraphQLTypeNameByPgCodec(underlyingType, "output"); @@ -1462,7 +1452,7 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { } } - const visited: Set = new Set(); + const visited: Set = new Set(); for (const codec of build.allPgCodecs) { prepareTypeForCodec(codec, visited); } diff --git a/graphile-build/graphile-build-pg/src/plugins/PgConditionArgumentPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgConditionArgumentPlugin.ts index 730a07def5..7cd8878334 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgConditionArgumentPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgConditionArgumentPlugin.ts @@ -2,6 +2,8 @@ import "./PgTablesPlugin.js"; import "graphile-config"; import type { + DefaultPgSelectSingleStep, + DefaultPgSelectStep, PgCodecWithAttributes, PgSelectParsedCursorStep, PgSelectSingleStep, @@ -59,7 +61,7 @@ export const PgConditionArgumentPlugin: GraphileConfig.Plugin = { if (!rawCodec.attributes || rawCodec.isAnonymous) { return; } - const codec = rawCodec as PgCodecWithAttributes; + const codec = rawCodec; const tableTypeName = inflection.tableType(codec); const conditionName = inflection.conditionType(tableTypeName); @@ -156,15 +158,15 @@ export const PgConditionArgumentPlugin: GraphileConfig.Plugin = { ? ( _condition, $connection: ConnectionStep< - PgSelectSingleStep, + DefaultPgSelectSingleStep, PgSelectParsedCursorStep, - PgSelectStep + DefaultPgSelectStep >, ) => { const $select = $connection.getSubplan(); return $select.wherePlan(); } - : (_condition, $select: PgSelectStep) => { + : (_condition, $select: DefaultPgSelectStep) => { return $select.wherePlan(); }, }, diff --git a/graphile-build/graphile-build-pg/src/plugins/PgConditionCustomFieldsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgConditionCustomFieldsPlugin.ts index fcad8ee74f..06f5765aae 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgConditionCustomFieldsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgConditionCustomFieldsPlugin.ts @@ -1,6 +1,7 @@ import "graphile-config"; import type { + DefaultPgResource, PgConditionStep, PgResource, PgResourceParameter, @@ -24,12 +25,11 @@ declare global { * arguments except the first are nullable, the first argument is a composite * type, and the result is a simple scalar type. */ -export function isSimpleScalarComputedColumnLike(resource: PgResource) { +export function isSimpleScalarComputedColumnLike(resource: DefaultPgResource) { if (resource.codec.attributes) return false; if (resource.codec.arrayOfCodec) return false; if (resource.codec.rangeOfCodec) return false; - const parameters: readonly PgResourceParameter[] | undefined = - resource.parameters; + const parameters = resource.parameters; if (!parameters || parameters.length < 1) return false; if (parameters.some((p, i) => i > 0 && p.required)) return false; if (!parameters[0].codec.attributes) return false; @@ -85,72 +85,69 @@ export const PgConditionCustomFieldsPlugin: GraphileConfig.Plugin = { return build.extend( fields, - functionSources.reduce((memo, rawPgFieldSource) => { - const pgFieldSource = rawPgFieldSource as PgResource< - any, - any, - any, - readonly PgResourceParameter[], - any - >; - const fieldName = inflection.computedAttributeField({ - resource: pgFieldSource, - }); - const type = build.getGraphQLTypeByPgCodec( - pgFieldSource.codec, - "input", - ); - if (!type) return memo; - memo = build.extend( - memo, - { - [fieldName]: fieldWithHooks( - { - fieldName, - fieldBehaviorScope: "proc:filterBy", - isPgConnectionConditionInputField: true, - pgFieldSource, - }, - { - description: build.wrapDescription( - `Checks for equality with the object’s \`${fieldName}\` field.`, - "field", - ), - type, - applyPlan: EXPORTABLE( - (pgFieldSource, sql) => - function plan( - $condition: PgConditionStep>, - val, - ) { - if (typeof pgFieldSource.from !== "function") { - throw new Error( - "Invalid computed attribute 'from'", - ); - } - const expression = sql`${pgFieldSource.from({ - placeholder: $condition.alias, - })}`; - if (val.getRaw().evalIs(null)) { - $condition.where(sql`${expression} is null`); - } else { - $condition.where( - sql`${expression} = ${$condition.placeholder( - val.get(), - pgFieldSource.codec, - )}`, - ); - } - }, - [pgFieldSource, sql], - ), - }, - ), - }, - `Adding computed attribute condition argument for ${pgCodec.name}`, - ); - return memo; - }, Object.create(null)), + functionSources.reduce( + (memo, rawPgFieldSource: DefaultPgResource) => { + const pgFieldSource = rawPgFieldSource; + const fieldName = inflection.computedAttributeField({ + resource: pgFieldSource, + }); + const type = build.getGraphQLTypeByPgCodec( + pgFieldSource.codec, + "input", + ); + if (!type) return memo; + memo = build.extend( + memo, + { + [fieldName]: fieldWithHooks( + { + fieldName, + fieldBehaviorScope: "proc:filterBy", + isPgConnectionConditionInputField: true, + pgFieldSource, + }, + { + description: build.wrapDescription( + `Checks for equality with the object’s \`${fieldName}\` field.`, + "field", + ), + type, + applyPlan: EXPORTABLE( + (pgFieldSource, sql) => + function plan( + $condition: PgConditionStep>, + val, + ) { + if (typeof pgFieldSource.from !== "function") { + throw new Error( + "Invalid computed attribute 'from'", + ); + } + const expression = sql`${pgFieldSource.from({ + placeholder: $condition.alias, + })}`; + if (val.getRaw().evalIs(null)) { + $condition.where(sql`${expression} is null`); + } else { + $condition.where( + sql`${expression} = ${$condition.placeholder( + val.get(), + pgFieldSource.codec, + )}`, + ); + } + }, + [pgFieldSource, sql], + ), + }, + ), + }, + `Adding computed attribute condition argument for ${pgCodec.name}`, + ); + return memo; + }, + Object.create(null), + ), `Adding computed attribute filterable functions to condition for '${pgCodec.name}'`, ); }, diff --git a/graphile-build/graphile-build-pg/src/plugins/PgConnectionArgOrderByPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgConnectionArgOrderByPlugin.ts index f633710bf8..2e23264c49 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgConnectionArgOrderByPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgConnectionArgOrderByPlugin.ts @@ -2,6 +2,7 @@ import "./PgTablesPlugin.js"; import "graphile-config"; import type { + DefaultPgCodec, PgCodec, PgSelectParsedCursorStep, PgSelectSingleStep, @@ -25,7 +26,7 @@ declare global { orderByType(this: Inflection, typeName: string): string; } interface ScopeEnum { - pgCodec?: PgCodec; + pgCodec?: DefaultPgCodec; isPgRowSortEnum?: boolean; } } diff --git a/graphile-build/graphile-build-pg/src/plugins/PgCustomTypeFieldPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgCustomTypeFieldPlugin.ts index 1f5a874040..870d5402a5 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgCustomTypeFieldPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgCustomTypeFieldPlugin.ts @@ -6,6 +6,13 @@ import "./PgProceduresPlugin.js"; import "graphile-config"; import type { + DefaultPgCodec, + DefaultPgDeleteSingleStep, + DefaultPgResource, + DefaultPgResourceParameter, + DefaultPgSelectSingleStep, + DefaultPgSelectStep, + DefaultPgUpdateSingleStep, PgClassSingleStep, PgCodec, PgDeleteSingleStep, @@ -57,8 +64,8 @@ declare global { namespace GraphileBuild { interface Build { pgGetArgDetailsFromParameters( - resource: PgResource, - parameters?: readonly PgResourceParameter[], + resource: DefaultPgResource, + parameters?: readonly DefaultPgResourceParameter[], ): { makeFieldArgs(): { [graphqlArgName: string]: { @@ -70,13 +77,13 @@ declare global { argDetails: Array<{ graphqlArgName: string; postgresArgName: string | null; - pgCodec: PgCodec; + pgCodec: DefaultPgCodec; inputType: GraphQLInputType; required: boolean; }>; makeExpression(opts: { $placeholderable: { - placeholder($step: ExecutableStep, codec: PgCodec): SQL; + placeholder($step: ExecutableStep, codec: DefaultPgCodec): SQL; }; resource: PgResource; fieldArgs: FieldArgs; @@ -87,15 +94,15 @@ declare global { } interface InflectionCustomFieldProcedureDetails { - resource: PgResource; + resource: DefaultPgResource; } interface InflectionCustomFieldArgumentDetails { - resource: PgResource; - param: PgResourceParameter; + resource: DefaultPgResource; + param: DefaultPgResourceParameter; index: number; } interface InflectionCustomFieldMutationResult { - resource: PgResource; + resource: DefaultPgResource; returnGraphQLTypeName: string; } @@ -172,7 +179,7 @@ declare global { } function shouldUseCustomConnection( - pgResource: PgResource, + pgResource: DefaultPgResource, ): boolean { const { codec } = pgResource; // 'setof ' functions should use a connection based on the function name, not a generic connection @@ -181,13 +188,9 @@ function shouldUseCustomConnection( return setOrArray && scalarOrAnonymous; } -function defaultProcSourceBehavior( - s: PgResource, -): string { +function defaultProcSourceBehavior(s: DefaultPgResource): string { const behavior = []; - const firstParameter = ( - s as PgResource - ).parameters[0]; + const firstParameter = s.parameters[0]; if ( !s.isMutation && s.parameters && @@ -232,22 +235,19 @@ function defaultProcSourceBehavior( function hasRecord( $row: ExecutableStep, ): $row is - | PgSelectSingleStep + | DefaultPgSelectSingleStep | PgInsertSingleStep - | PgUpdateSingleStep - | PgDeleteSingleStep { + | DefaultPgUpdateSingleStep + | DefaultPgDeleteSingleStep { return "record" in $row && typeof ($row as any).record === "function"; } declare global { namespace GraphileBuild { interface Build { - [$$rootQuery]: Array>; - [$$rootMutation]: Array>; - [$$computed]: Map< - PgCodec, - Array> - >; + [$$rootQuery]: Array; + [$$rootMutation]: Array; + [$$computed]: Map>; } } } @@ -531,10 +531,8 @@ export const PgCustomTypeFieldPlugin: GraphileConfig.Plugin = { step = constant(null); } } else if (fetcher) { - step = ( - fetcher( - args.get([...path, graphqlArgName]), - ) as PgSelectSingleStep + step = fetcher( + args.get([...path, graphqlArgName]), ).record(); } else { step = args.get([...path, graphqlArgName]); @@ -632,13 +630,7 @@ export const PgCustomTypeFieldPlugin: GraphileConfig.Plugin = { shouldUseCustomConnection(someSource); if (isFunctionSourceRequiringConnection) { - const resource = someSource as PgResource< - any, - any, - any, - readonly PgResourceParameter[], - any - >; + const resource = someSource; const connectionTypeName = resource.codec.attributes ? inflection.recordFunctionConnectionType({ resource, @@ -694,13 +686,7 @@ export const PgCustomTypeFieldPlugin: GraphileConfig.Plugin = { build.behavior.pgResourceMatches(someSource, "mutationField"); // Add payload type for mutation functions if (isMutationProcSource) { - const resource = someSource as PgResource< - any, - any, - any, - readonly PgResourceParameter[], - any - >; + const resource = someSource; build.recoverable(null, () => { const inputTypeName = inflection.customMutationInput({ resource, @@ -913,11 +899,10 @@ export const PgCustomTypeFieldPlugin: GraphileConfig.Plugin = { (memo, resource) => build.recoverable(memo, () => { // "Computed attributes" skip a parameter - const remainingParameters = ( + const remainingParameters = isRootMutation || isRootQuery ? resource.parameters - : resource.parameters.slice(1) - ) as PgResourceParameter[]; + : resource.parameters.slice(1); const { makeArgs, makeFieldArgs } = pgGetArgDetailsFromParameters( resource, @@ -963,7 +948,7 @@ export const PgCustomTypeFieldPlugin: GraphileConfig.Plugin = { ($in, args, _info) => { if (!hasRecord($in)) { throw new Error( - `Invalid plan, exepcted 'PgSelectSingleStep', 'PgInsertSingleStep', 'PgUpdateSingleStep' or 'PgDeleteSingleStep', but found ${$in}`, + `Invalid plan, expected 'PgSelectSingleStep', 'PgInsertSingleStep', 'PgUpdateSingleStep' or 'PgDeleteSingleStep', but found ${$in}`, ); } const extraSelectArgs = makeArgs(args); @@ -1201,7 +1186,7 @@ export const PgCustomTypeFieldPlugin: GraphileConfig.Plugin = { $parent, args, info, - ) as PgSelectStep; + ) as DefaultPgSelectStep; return connection( $select, ($item) => $item, @@ -1287,10 +1272,9 @@ export const PgCustomTypeFieldPlugin: GraphileConfig.Plugin = { function getFunctionSourceReturnGraphQLType( build: GraphileBuild.Build, - resource: PgResource, + resource: DefaultPgResource, ): GraphQLOutputType | null { - const resourceInnerCodec: PgCodec = - resource.codec.arrayOfCodec ?? resource.codec; + const resourceInnerCodec = resource.codec.arrayOfCodec ?? resource.codec; if (!resourceInnerCodec) { return null; } diff --git a/graphile-build/graphile-build-pg/src/plugins/PgFirstLastBeforeAfterArgsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgFirstLastBeforeAfterArgsPlugin.ts index 75e68ec884..c67f71f2ed 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgFirstLastBeforeAfterArgsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgFirstLastBeforeAfterArgsPlugin.ts @@ -2,9 +2,9 @@ import "./PgTablesPlugin.js"; import "graphile-config"; import type { + DefaultPgSelectSingleStep, + DefaultPgSelectStep, PgSelectParsedCursorStep, - PgSelectSingleStep, - PgSelectStep, } from "@dataplan/pg"; import type { ConnectionStep, GrafastFieldConfigArgumentMap } from "grafast"; import { EXPORTABLE } from "graphile-build"; @@ -93,9 +93,9 @@ function commonFn( function plan( _: any, $connection: ConnectionStep< - PgSelectSingleStep, + DefaultPgSelectSingleStep, PgSelectParsedCursorStep, - PgSelectStep + DefaultPgSelectStep >, arg, ) { @@ -118,9 +118,9 @@ function commonFn( function plan( _: any, $connection: ConnectionStep< - PgSelectSingleStep, + DefaultPgSelectSingleStep, PgSelectParsedCursorStep, - PgSelectStep + DefaultPgSelectStep >, val, ) { @@ -145,9 +145,9 @@ function commonFn( function plan( _: any, $connection: ConnectionStep< - PgSelectSingleStep, + DefaultPgSelectSingleStep, PgSelectParsedCursorStep, - PgSelectStep + DefaultPgSelectStep >, val, ) { @@ -170,9 +170,9 @@ function commonFn( function plan( _: any, $connection: ConnectionStep< - PgSelectSingleStep, + DefaultPgSelectSingleStep, PgSelectParsedCursorStep, - PgSelectStep + DefaultPgSelectStep >, val, ) { @@ -193,9 +193,9 @@ function commonFn( function plan( _: any, $connection: ConnectionStep< - PgSelectSingleStep, + DefaultPgSelectSingleStep, PgSelectParsedCursorStep, - PgSelectStep + DefaultPgSelectStep >, val, ) { diff --git a/graphile-build/graphile-build-pg/src/plugins/PgInterfaceModeUnionAllRowsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgInterfaceModeUnionAllRowsPlugin.ts index 461369e44f..f677aee712 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgInterfaceModeUnionAllRowsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgInterfaceModeUnionAllRowsPlugin.ts @@ -1,9 +1,9 @@ import "graphile-config"; import type { + DefaultPgCodec, + DefaultPgResource, PgCodec, - PgRegistry, - PgResource, PgUnionAllStepConfigAttributes, PgUnionAllStepMember, } from "@dataplan/pg"; @@ -21,7 +21,7 @@ declare global { * The base inflector used by allInterfaceModeUnionRowsConnection and * allInterfaceModeUnionRowsList. */ - _allInterfaceModeUnionRows(this: Inflection, codec: PgCodec): string; + _allInterfaceModeUnionRows(this: Inflection, codec: DefaultPgCodec): string; /** * The field name for a Cursor Connection field that returns all rows @@ -29,14 +29,14 @@ declare global { */ allInterfaceModeUnionRowsConnection( this: Inflection, - codec: PgCodec, + codec: DefaultPgCodec, ): string; /** * The field name for a List field that returns all rows from the given * `@interface mode:union` codec. */ - allInterfaceModeUnionRowsList(this: Inflection, codec: PgCodec): string; + allInterfaceModeUnionRowsList(this: Inflection, codec: DefaultPgCodec): string; } } } @@ -90,11 +90,11 @@ export const PgInterfaceModeUnionAllRowsPlugin: GraphileConfig.Plugin = { return fields; } - const pgRegistry = input.pgRegistry as PgRegistry; + const pgRegistry = input.pgRegistry; const resourcesByPolymorphicTypeName: { [polymorphicTypeName: string]: { - resources: PgResource[]; + resources: DefaultPgResource[]; type: "union" | "interface"; }; } = Object.create(null); @@ -135,7 +135,7 @@ export const PgInterfaceModeUnionAllRowsPlugin: GraphileConfig.Plugin = { for (const interfaceName of interfaces) { if (!resourcesByPolymorphicTypeName[interfaceName]) { resourcesByPolymorphicTypeName[interfaceName] = { - resources: [resource as PgResource], + resources: [resource], type: "interface", }; } else { @@ -146,15 +146,16 @@ export const PgInterfaceModeUnionAllRowsPlugin: GraphileConfig.Plugin = { throw new Error(`Inconsistent polymorphism`); } resourcesByPolymorphicTypeName[interfaceName].resources.push( - resource as PgResource, + resource, ); } } } } - const interfaceCodecs: { [polymorphicTypeName: string]: PgCodec } = - Object.create(null); + const interfaceCodecs: { + [polymorphicTypeName: string]: DefaultPgCodec; + } = Object.create(null); for (const codec of Object.values(pgRegistry.pgCodecs)) { if (!codec.polymorphism) continue; if (codec.polymorphism.mode !== "union") continue; @@ -216,7 +217,7 @@ export const PgInterfaceModeUnionAllRowsPlugin: GraphileConfig.Plugin = { if (!interfaceCodec.attributes) return; const attributes: PgUnionAllStepConfigAttributes = interfaceCodec.attributes; - const resourceByTypeName: Record = + const resourceByTypeName: Record = Object.create(null); const members: PgUnionAllStepMember[] = []; for (const resource of spec.resources) { diff --git a/graphile-build/graphile-build-pg/src/plugins/PgJWTPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgJWTPlugin.ts index b0e55b8f6f..6f32f8fe02 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgJWTPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgJWTPlugin.ts @@ -1,6 +1,11 @@ import "graphile-config"; -import type { PgCodec, PgSelectSingleStep } from "@dataplan/pg"; +import type { + DefaultPgCodec, + DefaultPgSelectSingleStep, + PgCodec, + PgSelectSingleStep, +} from "@dataplan/pg"; import { EXPORTABLE, gatherConfig } from "graphile-build"; import type { Secret, SignOptions } from "jsonwebtoken"; import { sign as signJwt } from "jsonwebtoken"; @@ -35,7 +40,7 @@ declare global { interface ScopeScalar { isPgJwtType?: boolean; - pgCodec?: PgCodec; + pgCodec?: DefaultPgCodec; } } } @@ -185,7 +190,7 @@ export const PgJWTPlugin: GraphileConfig.Plugin = { plan: EXPORTABLE( () => function plan($in) { - const $record = $in as PgSelectSingleStep; + const $record = $in as DefaultPgSelectSingleStep; return $record.record(); }, [], diff --git a/graphile-build/graphile-build-pg/src/plugins/PgMutationPayloadEdgePlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgMutationPayloadEdgePlugin.ts index 68a8caf9bd..7f9950d8ef 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgMutationPayloadEdgePlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgMutationPayloadEdgePlugin.ts @@ -1,10 +1,6 @@ import "graphile-config"; -import type { - PgClassSingleStep, - PgCodecWithAttributes, - PgResourceUnique, -} from "@dataplan/pg"; +import type { DefaultPgCodec, PgClassSingleStep } from "@dataplan/pg"; import { PgDeleteSingleStep, pgSelectFromRecord } from "@dataplan/pg"; import type { FieldArgs, FieldInfo, ObjectStep } from "grafast"; import { connection, constant, EdgeStep, first } from "grafast"; @@ -18,7 +14,7 @@ import { applyOrderToPlan } from "./PgConnectionArgOrderByPlugin.js"; declare global { namespace GraphileBuild { interface Inflection { - tableEdgeField(this: Inflection, codec: PgCodecWithAttributes): string; + tableEdgeField(this: Inflection, codec: DefaultPgCodec): string; } interface ScopeObjectFieldsField { @@ -84,9 +80,7 @@ export const PgMutationPayloadEdgePlugin: GraphileConfig.Plugin = { return fields; } - const pk = (resource.uniques as PgResourceUnique[])?.find( - (u) => u.isPrimary, - ); + const pk = resource.uniques?.find((u) => u.isPrimary); if (!pk) { return fields; } diff --git a/graphile-build/graphile-build-pg/src/plugins/PgMutationUpdateDeletePlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgMutationUpdateDeletePlugin.ts index 98fe243399..6f13f71e5e 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgMutationUpdateDeletePlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgMutationUpdateDeletePlugin.ts @@ -1,6 +1,10 @@ import "graphile-config"; import type { + DefaultPgDeleteSingleStep, + DefaultPgResource, + DefaultPgResourceUnique, + DefaultPgUpdateSingleStep, PgClassSingleStep, PgCodecWithAttributes, PgDeleteSingleStep, @@ -31,7 +35,7 @@ declare global { interface ScopeObject { isPgUpdatePayloadType?: boolean; isPgDeletePayloadType?: boolean; - pgTypeResource?: PgResource; + pgTypeResource?: DefaultPgResource; } interface ScopeObjectFieldsField { @@ -45,88 +49,88 @@ declare global { isPgDeleteInputType?: boolean; isPgDeleteByKeysInputType?: boolean; isPgDeleteNodeInputType?: boolean; - pgResource?: PgResource; - pgResourceUnique?: PgResourceUnique; + pgResource?: DefaultPgResource; + pgResourceUnique?: DefaultPgResourceUnique; } interface Inflection { updatePayloadType( this: Inflection, details: { - resource: PgResource; + resource: DefaultPgResource; }, ): string; deletePayloadType( this: Inflection, details: { - resource: PgResource; + resource: DefaultPgResource; }, ): string; updateNodeField( this: Inflection, details: { - resource: PgResource; - unique: PgResourceUnique; + resource: DefaultPgResource; + unique: DefaultPgResourceUnique; }, ): string; updateNodeInputType( this: Inflection, details: { - resource: PgResource; - unique: PgResourceUnique; + resource: DefaultPgResource; + unique: DefaultPgResourceUnique; }, ): string; deletedNodeId( this: Inflection, details: { - resource: PgResource; + resource: DefaultPgResource; }, ): string; deleteNodeField( this: Inflection, details: { - resource: PgResource; - unique: PgResourceUnique; + resource: DefaultPgResource; + unique: DefaultPgResourceUnique; }, ): string; deleteNodeInputType( this: Inflection, details: { - resource: PgResource; - unique: PgResourceUnique; + resource: DefaultPgResource; + unique: DefaultPgResourceUnique; }, ): string; updateByKeysField( this: Inflection, details: { - resource: PgResource; - unique: PgResourceUnique; + resource: DefaultPgResource; + unique: DefaultPgResourceUnique; }, ): string; updateByKeysInputType( this: Inflection, details: { - resource: PgResource; - unique: PgResourceUnique; + resource: DefaultPgResource; + unique: DefaultPgResourceUnique; }, ): string; deleteByKeysField( this: Inflection, details: { - resource: PgResource; - unique: PgResourceUnique; + resource: DefaultPgResource; + unique: DefaultPgResourceUnique; }, ): string; deleteByKeysInputType( this: Inflection, details: { - resource: PgResource; - unique: PgResourceUnique; + resource: DefaultPgResource; + unique: DefaultPgResourceUnique; }, ): string; @@ -247,7 +251,7 @@ export const PgMutationUpdateDeletePlugin: GraphileConfig.Plugin = { } = build; const process = ( - resource: PgResource, + resource: DefaultPgResource, mode: "resource:update" | "resource:delete", ) => { const modeText = mode === "resource:update" ? "update" : "delete"; @@ -337,8 +341,8 @@ export const PgMutationUpdateDeletePlugin: GraphileConfig.Plugin = { function plan( $object: ObjectStep<{ result: - | PgUpdateSingleStep - | PgDeleteSingleStep; + | DefaultPgUpdateSingleStep + | DefaultPgDeleteSingleStep; }>, ) { return $object.get("result"); @@ -497,7 +501,7 @@ export const PgMutationUpdateDeletePlugin: GraphileConfig.Plugin = { : (unique.attributes as string[]).reduce( (memo, attributeName) => { const attribute = - resource.codec.attributes[attributeName]; + resource.codec.attributes![attributeName]; memo[ inflection.attribute({ attributeName, @@ -531,7 +535,7 @@ export const PgMutationUpdateDeletePlugin: GraphileConfig.Plugin = { () => function plan( $object: ObjectStep<{ - result: PgUpdateSingleStep; + result: DefaultPgUpdateSingleStep; }>, ) { const $record = @@ -555,30 +559,12 @@ export const PgMutationUpdateDeletePlugin: GraphileConfig.Plugin = { } }; - const allResources = Object.values( - build.input.pgRegistry.pgResources, - ) as PgResource[]; - const updatableResources = allResources.filter( - ( - resource, - ): resource is PgResource< - any, - PgCodecWithAttributes, - any, - any, - any - > => isUpdatable(build, resource), + const allResources = Object.values(build.input.pgRegistry.pgResources); + const updatableResources = allResources.filter((resource) => + isUpdatable(build, resource), ); - const deletableResources = allResources.filter( - ( - resource, - ): resource is PgResource< - any, - PgCodecWithAttributes, - any, - any, - any - > => isDeletable(build, resource), + const deletableResources = allResources.filter((resource) => + isDeletable(build, resource), ); updatableResources.forEach((resource) => { @@ -764,8 +750,8 @@ export const PgMutationUpdateDeletePlugin: GraphileConfig.Plugin = { _: any, $object: ObjectStep<{ result: - | PgUpdateSingleStep - | PgDeleteSingleStep; + | DefaultPgUpdateSingleStep + | DefaultPgDeleteSingleStep; }>, ) { return $object; @@ -892,11 +878,11 @@ return (_$root, args) => { function getSpecs( build: GraphileBuild.Build, - resource: PgResource, + resource: DefaultPgResource, mode: "resource:update" | "resource:delete", ) { const primaryUnique = resource.uniques.find( - (u: PgResourceUnique) => u.isPrimary, + (u: DefaultPgResourceUnique) => u.isPrimary, ); const constraintMode = `constraint:${mode}`; const specs = [ @@ -906,13 +892,13 @@ function getSpecs( ? [{ unique: primaryUnique, uniqueMode: "node" }] : []), ...resource.uniques - .filter((unique: PgResourceUnique) => { + .filter((unique: DefaultPgResourceUnique) => { return build.behavior.pgResourceUniqueMatches( [resource, unique], constraintMode, ); }) - .map((unique: PgResourceUnique) => ({ + .map((unique: DefaultPgResourceUnique) => ({ unique, uniqueMode: "keys", })), diff --git a/graphile-build/graphile-build-pg/src/plugins/PgNodeIdAttributesPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgNodeIdAttributesPlugin.ts index e6976b3ede..631bebf136 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgNodeIdAttributesPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgNodeIdAttributesPlugin.ts @@ -3,10 +3,9 @@ import "../interfaces.js"; import "graphile-config"; import type { - PgCodecRelation, - PgCodecWithAttributes, + DefaultPgCodec, + DefaultPgRegistry, PgConditionStep, - PgRegistry, PgSelectStep, } from "@dataplan/pg"; import type { SetterStep } from "grafast"; @@ -24,8 +23,8 @@ declare global { nodeIdAttribute( this: Inflection, details: { - registry: PgRegistry; - codec: PgCodecWithAttributes; + registry: DefaultPgRegistry; + codec: DefaultPgCodec; relationName: string; }, ): string; @@ -82,13 +81,10 @@ export const PgNodeIdAttributesPlugin: GraphileConfig.Plugin = { ) { return fields; } - const pgCodec = rawPgCodec as PgCodecWithAttributes; + const pgCodec = rawPgCodec; - const relationEntries = ( - Object.entries(pgRegistry.pgRelations[pgCodec.name] ?? {}) as [ - string, - PgCodecRelation, - ][] + const relationEntries = Object.entries( + pgRegistry.pgRelations[pgCodec.name] ?? {}, ).filter( ([_name, relation]) => !relation.isReferencee && relation.isUnique, ); @@ -134,14 +130,14 @@ export const PgNodeIdAttributesPlugin: GraphileConfig.Plugin = { relationName, }); const attributes = relation.localAttributes.map( - (name) => pgCodec.attributes[name], + (name) => pgCodec.attributes![name], ); const anAttributeIsNotNull = attributes.some( (attr) => attr.notNull || attr.extensions?.tags?.notNull, ); const { localAttributes, remoteAttributes } = relation; const localAttributeCodecs = localAttributes.map( - (name) => pgCodec.attributes[name].codec, + (name) => pgCodec.attributes![name].codec, ); return extend( memo, diff --git a/graphile-build/graphile-build-pg/src/plugins/PgOrderAllAttributesPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgOrderAllAttributesPlugin.ts index d2699054e4..c9c5a3d2fa 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgOrderAllAttributesPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgOrderAllAttributesPlugin.ts @@ -1,7 +1,7 @@ import "./PgTablesPlugin.js"; import "graphile-config"; -import type { PgCodecAttribute, PgCodecWithAttributes } from "@dataplan/pg"; +import type { DefaultPgCodec, DefaultPgCodecAttribute } from "@dataplan/pg"; import { PgSelectStep, PgUnionAllStep } from "@dataplan/pg"; import type { ExecutableStep, ModifierStep } from "grafast"; import type { GraphQLEnumValueConfigMap } from "grafast/graphql"; @@ -15,9 +15,9 @@ declare global { orderByAttributeEnum( this: Inflection, details: { - codec: PgCodecWithAttributes; + codec: DefaultPgCodec; attributeName: string; - attribute: PgCodecAttribute; + attribute: DefaultPgCodecAttribute; variant: "asc" | "desc" | "asc_nulls_last" | "desc_nulls_last"; }, ): string; @@ -59,8 +59,8 @@ export const PgOrderAllAttributesPlugin: GraphileConfig.Plugin = { ) { return values; } - const pgCodec = rawPgCodec as PgCodecWithAttributes; - const allAttributes = pgCodec.attributes; + const pgCodec = rawPgCodec; + const allAttributes = pgCodec.attributes!; const allowedAttributes = pgCodec.polymorphism?.mode === "single" ? [ diff --git a/graphile-build/graphile-build-pg/src/plugins/PgOrderByPrimaryKeyPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgOrderByPrimaryKeyPlugin.ts index 42da22c064..84b346144e 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgOrderByPrimaryKeyPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgOrderByPrimaryKeyPlugin.ts @@ -1,11 +1,7 @@ import "./PgTablesPlugin.js"; import "graphile-config"; -import type { - PgCodecWithAttributes, - PgResourceUnique, - PgSelectStep, -} from "@dataplan/pg"; +import type { DefaultPgSelectStep } from "@dataplan/pg"; import { EXPORTABLE } from "graphile-build"; import { version } from "../version.js"; @@ -41,16 +37,14 @@ export const PgOrderByPrimaryKeyPlugin: GraphileConfig.Plugin = { return values; } - const pgCodec = rawPgCodec as PgCodecWithAttributes; + const pgCodec = rawPgCodec; const resource = build.pgTableResource(pgCodec); if (!resource) { return values; } - const primaryKey = (resource.uniques as PgResourceUnique[]).find( - (unique) => unique.isPrimary, - ); + const primaryKey = resource.uniques.find((unique) => unique.isPrimary); if (!primaryKey) { return values; } @@ -64,9 +58,9 @@ export const PgOrderByPrimaryKeyPlugin: GraphileConfig.Plugin = { grafast: { applyPlan: EXPORTABLE( (pgCodec, pgOrderByNullsLast, primaryKeyAttributes, sql) => - (step: PgSelectStep) => { + (step: DefaultPgSelectStep) => { primaryKeyAttributes.forEach((attributeName) => { - const attribute = pgCodec.attributes[attributeName]; + const attribute = pgCodec.attributes![attributeName]; step.orderBy({ codec: attribute.codec, fragment: sql`${step.alias}.${sql.identifier( @@ -92,9 +86,9 @@ export const PgOrderByPrimaryKeyPlugin: GraphileConfig.Plugin = { grafast: { applyPlan: EXPORTABLE( (pgCodec, pgOrderByNullsLast, primaryKeyAttributes, sql) => - (step: PgSelectStep) => { + (step: DefaultPgSelectStep) => { primaryKeyAttributes.forEach((attributeName) => { - const attribute = pgCodec.attributes[attributeName]; + const attribute = pgCodec.attributes![attributeName]; step.orderBy({ codec: attribute.codec, fragment: sql`${step.alias}.${sql.identifier( diff --git a/graphile-build/graphile-build-pg/src/plugins/PgOrderCustomFieldsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgOrderCustomFieldsPlugin.ts index 18126e7ab0..7a4ca67b08 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgOrderCustomFieldsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgOrderCustomFieldsPlugin.ts @@ -2,9 +2,8 @@ import "./PgTablesPlugin.js"; import "graphile-config"; import type { - PgResource, - PgResourceParameter, - PgSelectStep, + DefaultPgResource, + DefaultPgSelectStep, } from "@dataplan/pg"; import { EXPORTABLE } from "graphile-build"; @@ -17,13 +16,7 @@ declare global { computedAttributeOrder( this: Inflection, details: { - resource: PgResource< - any, - any, - any, - readonly PgResourceParameter[], - any - >; + resource: DefaultPgResource; variant: "asc" | "desc" | "asc_nulls_last" | "desc_nulls_last"; }, ): string; @@ -90,13 +83,7 @@ export const PgOrderCustomFieldsPlugin: GraphileConfig.Plugin = { functionSources.reduce((memo, pgFieldSource) => { for (const ascDesc of ["asc" as const, "desc" as const]) { const valueName = inflection.computedAttributeOrder({ - resource: pgFieldSource as PgResource< - any, - any, - any, - readonly PgResourceParameter[], - any - >, + resource: pgFieldSource, variant: ascDesc, }); @@ -108,7 +95,7 @@ export const PgOrderCustomFieldsPlugin: GraphileConfig.Plugin = { grafast: { applyPlan: EXPORTABLE( (ascDesc, pgFieldSource, sql) => - (step: PgSelectStep) => { + (step: DefaultPgSelectStep) => { if (typeof pgFieldSource.from !== "function") { throw new Error( "Invalid computed attribute 'from'", diff --git a/graphile-build/graphile-build-pg/src/plugins/PgPolymorphismPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgPolymorphismPlugin.ts index 6ed9889ac9..9dcbd0deef 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgPolymorphismPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgPolymorphismPlugin.ts @@ -5,7 +5,9 @@ import "./PgRelationsPlugin.js"; import "./PgTablesPlugin.js"; import type { - PgCodec, + DefaultPgCodec, + DefaultPgRelation, + DefaultPgSelectSingleStep, PgCodecExtensions, PgCodecPolymorphism, PgCodecPolymorphismRelational, @@ -14,14 +16,7 @@ import type { PgCodecPolymorphismSingleTypeAttributeSpec, PgCodecPolymorphismSingleTypeSpec, PgCodecRef, - PgCodecRelation, - PgCodecWithAttributes, PgRefDefinition, - PgRegistry, - PgResource, - PgResourceOptions, - PgResourceUnique, - PgSelectSingleStep, } from "@dataplan/pg"; import { assertPgClassSingleStep } from "@dataplan/pg"; import type { ExecutableStep, ListStep, NodeIdHandler } from "grafast"; @@ -61,14 +56,14 @@ declare global { } namespace GraphileBuild { interface Build { - nodeIdSpecForCodec(codec: PgCodec): + nodeIdSpecForCodec(codec: DefaultPgCodec): | (($nodeId: ExecutableStep) => { [key: string]: ExecutableStep; }) | null; } interface ScopeInterface { - pgCodec?: PgCodec; + pgCodec?: DefaultPgCodec; isPgPolymorphicTableType?: boolean; pgPolymorphism?: PgCodecPolymorphism; } @@ -335,9 +330,7 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = { async pgRegistry_PgRegistryBuilder_finalize(info, event) { const { registryBuilder } = event; const registryConfig = registryBuilder.getRegistryConfig(); - for (const resource of Object.values( - registryConfig.pgResources, - ) as PgResourceOptions[]) { + for (const resource of Object.values(registryConfig.pgResources)) { if (resource.parameters || !resource.codec.attributes) { continue; } @@ -359,7 +352,7 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = { continue; } - const poly = (resource.codec as PgCodec).polymorphism; + const poly = resource.codec.polymorphism; if (poly?.mode === "relational") { // Copy common attributes to implementations for (const spec of Object.values(poly.types)) { @@ -438,6 +431,7 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = { sharedRelationName; } else { otherCodec.attributes[colName] = { + name: colName, codec: colSpec.codec, notNull: colSpec.notNull, hasDefault: colSpec.hasDefault, @@ -463,13 +457,7 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = { if (rawResource.parameters || !rawResource.codec.attributes) { continue; } - const resource = rawResource as PgResource< - string, - PgCodecWithAttributes, - any, - undefined, - PgRegistry - >; + const resource = rawResource; if (!resource.extensions?.pg) { continue; } @@ -488,11 +476,8 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = { continue; } - const relations = registry.pgRelations[resource.codec.name] as Record< - string, - PgCodecRelation - >; - const poly = (resource.codec as PgCodec).polymorphism; + const relations = registry.pgRelations[resource.codec.name]; + const poly = resource.codec.polymorphism; if (poly?.mode === "relational") { // Copy common attributes to implementations for (const spec of Object.values(poly.types)) { @@ -738,7 +723,7 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = { !resource.isUnique && !resource.isVirtual ) { - if ((resource.codec as PgCodec).polymorphism) { + if (resource.codec.polymorphism) { // This is a polymorphic type newBehavior.push( "-resource:insert -resource:update -resource:delete", @@ -747,11 +732,9 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = { const resourceTypeName = build.inflection.tableType( resource.codec, ); - const relations: Record = + const relations: Record = resource.getRelations(); - const pk = ( - resource.uniques as PgResourceUnique[] | undefined - )?.find((u) => u.isPrimary); + const pk = resource.uniques?.find((u) => u.isPrimary); if (pk) { const pkAttributes = pk.attributes; const pkRelations = Object.values(relations).filter((r) => @@ -808,9 +791,9 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = { if (!handler) { return null; } - const pk = ( - relation.remoteResource.uniques as PgResourceUnique[] - ).find((u) => u.isPrimary); + const pk = relation.remoteResource.uniques.find( + (u) => u.isPrimary, + ); if (!pk) { return null; } @@ -987,7 +970,7 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = { setGraphQLTypeForPgCodec, grafast: { list, constant, access }, } = build; - const unionsToRegister = new Map(); + const unionsToRegister = new Map(); for (const codec of build.pgCodecMetaLookup.keys()) { if (!codec.attributes) { // Only apply to codecs that define attributes @@ -1048,13 +1031,9 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = { }, nonNullNode: pgForbidSetofFunctionsToReturnNull, }); - const resource = build.pgTableResource( - codec as PgCodecWithAttributes, - ); + const resource = build.pgTableResource(codec); const primaryKey = resource - ? (resource.uniques as PgResourceUnique[]).find( - (u) => u.isPrimary === true, - ) + ? resource.uniques.find((u) => u.isPrimary === true) : undefined; const pk = primaryKey?.attributes; for (const [typeIdentifier, spec] of Object.entries( @@ -1136,7 +1115,7 @@ return function (list, constant) { ) : EXPORTABLE( (constant, list, pk, tableTypeName) => - ($record: PgSelectSingleStep) => { + ($record: DefaultPgSelectSingleStep) => { return list([ constant(tableTypeName, false), ...pk.map((attribute) => diff --git a/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts index 0d51d019da..5153009794 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts @@ -3,12 +3,12 @@ // (e.g. they can be relations to other tables), so we've renamed them. import type { - PgCodec, - PgCodecAttributes, - PgFunctionResourceOptions, + DefaultPgCodec, + DefaultPgCodecAttributesRecord, + DefaultPgFunctionResourceOptions, + DefaultPgResourceOptions, + DefaultPgResourceParameter, PgResourceExtensions, - PgResourceOptions, - PgResourceParameter, PgSelectArgumentDigest, } from "@dataplan/pg"; import { @@ -64,7 +64,7 @@ declare global { getResourceOptions( serviceName: string, pgProc: PgProc, - ): Promise; + ): Promise; }; } @@ -72,15 +72,18 @@ declare global { pgProcedures_functionResourceOptions(event: { serviceName: string; pgProc: PgProc; - baseResourceOptions: Pick & - Partial>; - functionResourceOptions: PgFunctionResourceOptions; + baseResourceOptions: Pick< + DefaultPgResourceOptions, + "codec" | "executor" + > & + Partial>; + functionResourceOptions: DefaultPgFunctionResourceOptions; }): void | Promise; pgProcedures_PgResourceOptions(event: { serviceName: string; pgProc: PgProc; - resourceOptions: PgResourceOptions; + resourceOptions: DefaultPgResourceOptions; }): void | Promise; } } @@ -89,7 +92,7 @@ declare global { interface State { resourceOptionsByPgProcByService: Map< string, - Map> + Map> >; } const EMPTY_OBJECT = Object.freeze({}); @@ -213,89 +216,92 @@ export const PgProceduresPlugin: GraphileConfig.Plugin = { const { tags: rawTags, description } = pgProc.getTagsAndDescription(); const tags = JSON.parse(JSON.stringify(rawTags)); - const makeCodecFromReturn = async (): Promise => { - // We're building a PgCodec to represent specifically the - // return type of this function. - - const numberOfArguments = allArgTypes.length ?? 0; - const attributes: PgCodecAttributes = Object.create(null); - for (let i = 0, l = numberOfArguments; i < l; i++) { - // i for IN arguments, o for OUT arguments, b for INOUT arguments, - // v for VARIADIC arguments, t for TABLE arguments - const argMode = (pgProc.proargmodes?.[i] ?? "i") as - | "i" - | "o" - | "b" - | "v" - | "t"; - - if (argMode === "o" || argMode === "b" || argMode === "t") { - const argType = allArgTypes[i]; - const trueArgName = pgProc.proargnames?.[i]; - const argName = trueArgName || `column${i + 1}`; - - const tag = tags[`arg${i}modifier`]; - const typeModifier = - typeof tag === "string" - ? /^[0-9]+$/.test(tag) - ? parseInt(tag, 10) - : tag - : undefined; - - // This argument exists on the record type output - // NOTE: we treat `OUT foo`, `INOUT foo` and - // `RETURNS TABLE (foo ...)` as the same. - const attributeCodec = - await info.helpers.pgCodecs.getCodecFromType( - serviceName, - argType, - typeModifier, - ); - if (!attributeCodec) { - console.warn( - `Could not make codec for '${debugProcName}' argument '${argName}' which has type ${argType} (${ - (await info.helpers.pgIntrospection.getType( - serviceName, - argType, - ))!.typname - }); skipping function`, - ); - return null; + const makeCodecFromReturn = + async (): Promise => { + // We're building a PgCodec to represent specifically the + // return type of this function. + + const numberOfArguments = allArgTypes.length ?? 0; + const attributes: DefaultPgCodecAttributesRecord = + Object.create(null); + for (let i = 0, l = numberOfArguments; i < l; i++) { + // i for IN arguments, o for OUT arguments, b for INOUT arguments, + // v for VARIADIC arguments, t for TABLE arguments + const argMode = (pgProc.proargmodes?.[i] ?? "i") as + | "i" + | "o" + | "b" + | "v" + | "t"; + + if (argMode === "o" || argMode === "b" || argMode === "t") { + const argType = allArgTypes[i]; + const trueArgName = pgProc.proargnames?.[i]; + const argName = trueArgName || `column${i + 1}`; + + const tag = tags[`arg${i}modifier`]; + const typeModifier = + typeof tag === "string" + ? /^[0-9]+$/.test(tag) + ? parseInt(tag, 10) + : tag + : undefined; + + // This argument exists on the record type output + // NOTE: we treat `OUT foo`, `INOUT foo` and + // `RETURNS TABLE (foo ...)` as the same. + const attributeCodec = + await info.helpers.pgCodecs.getCodecFromType( + serviceName, + argType, + typeModifier, + ); + if (!attributeCodec) { + console.warn( + `Could not make codec for '${debugProcName}' argument '${argName}' which has type ${argType} (${ + (await info.helpers.pgIntrospection.getType( + serviceName, + argType, + ))!.typname + }); skipping function`, + ); + return null; + } + attributes[argName] = { + name: argName, + notNull: false, + codec: attributeCodec, + extensions: { + argIndex: i, + argName: trueArgName, + }, + // ENHANCE: could use "param" smart tag in function to add extensions here? + }; } - attributes[argName] = { - notNull: false, - codec: attributeCodec, - extensions: { - argIndex: i, - argName: trueArgName, - }, - // ENHANCE: could use "param" smart tag in function to add extensions here? - }; } - } - const recordCodecName = - info.inflection.functionRecordReturnCodecName({ - pgProc, - serviceName, - }); - return EXPORTABLE( - (attributes, executor, recordCodec, recordCodecName, sql) => - recordCodec({ - name: recordCodecName, - identifier: sql`ANONYMOUS_TYPE_DO_NOT_REFERENCE`, - attributes, - description: undefined, - extensions: { - /* `The return type of our \`${name}\` ${ + const recordCodecName = + info.inflection.functionRecordReturnCodecName({ + pgProc, + serviceName, + }); + return EXPORTABLE( + (attributes, executor, recordCodec, recordCodecName, sql) => + recordCodec({ + name: recordCodecName, + identifier: sql`ANONYMOUS_TYPE_DO_NOT_REFERENCE`, + attributes, + description: undefined, + extensions: { + /* `The return type of our \`${name}\` ${ pgProc.provolatile === "v" ? "mutation" : "query" }.`, */ - }, - executor, - isAnonymous: true, - }), - [attributes, executor, recordCodec, recordCodecName, sql], - ); - }; + }, + executor, + isAnonymous: true, + }), + [attributes, executor, recordCodec, recordCodecName, sql], + ); + }; const returnCodec = needsPayloadCodecToBeGenerated ? await makeCodecFromReturn() @@ -311,7 +317,7 @@ export const PgProceduresPlugin: GraphileConfig.Plugin = { return null; } - const parameters: PgResourceParameter[] = []; + const parameters: DefaultPgResourceParameter[] = []; // const processedFirstInputArg = false; @@ -498,7 +504,7 @@ export const PgProceduresPlugin: GraphileConfig.Plugin = { return null; } - const options: PgFunctionResourceOptions = { + const options: DefaultPgFunctionResourceOptions = { name, identifier, from: fromCallback, @@ -535,7 +541,7 @@ export const PgProceduresPlugin: GraphileConfig.Plugin = { [finalResourceOptions, makePgResourceOptions], ); } else { - const options: PgResourceOptions = EXPORTABLE( + const options: DefaultPgResourceOptions = EXPORTABLE( ( description, executor, diff --git a/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts index 0ea9e513bc..22d7ed6aca 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts @@ -1,4 +1,8 @@ import type { + AnyPgCodec, + AnyPgCodecRelationConfig, + AnyPgResourceOptions, + DefaultPgCodecRelationConfig, PgCodec, PgCodecExtensions, PgCodecRefPath, @@ -202,7 +206,7 @@ export const PgRefsPlugin: GraphileConfig.Plugin = { ? [tags.refVia] : null; - const refDefinitions = (resourceOptions.codec as PgCodec).extensions + const refDefinitions = resourceOptions.codec.extensions ?.refDefinitions; if (!refDefinitions) { if (rawRefVias) { @@ -252,16 +256,13 @@ export const PgRefsPlugin: GraphileConfig.Plugin = { outerLoop: for (const via of vias) { const path: PgCodecRefPath = []; const parts = via.split(";"); - let currentResourceOptions: PgResourceOptions = resourceOptions; + let currentResourceOptions = resourceOptions; for (const rawPart of parts) { - type RelationEntry = [ - string, - PgCodecRelationConfig, - ]; + type RelationEntry = [string, DefaultPgCodecRelationConfig]; const relations = registryConfig.pgRelations[currentResourceOptions.codec.name]; const relationEntries = relations - ? (Object.entries(relations) as Array) + ? Object.entries(relations) : []; const part = rawPart.trim(); // ENHANCE: allow whitespace diff --git a/graphile-build/graphile-build-pg/src/plugins/PgRegistryPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgRegistryPlugin.ts index 2ba98c26a9..a0313d00da 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgRegistryPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgRegistryPlugin.ts @@ -1,5 +1,11 @@ /* eslint-disable graphile-export/export-instances */ -import type { PgRegistry, PgRegistryBuilder } from "@dataplan/pg"; +import type { + DefaultPgRegistry, + PgRegistry, + PgRegistryBuilder, + DefaultRegistryBuilder, + EmptyRegistryBuilder, +} from "@dataplan/pg"; import { makeRegistryBuilder } from "@dataplan/pg"; import type { PromiseOrDirect } from "grafast"; import { gatherConfig } from "graphile-build"; @@ -10,29 +16,29 @@ declare global { namespace GraphileConfig { interface GatherHelpers { pgRegistry: { - getRegistryBuilder(): PromiseOrDirect>; - getRegistry(): PromiseOrDirect>; + getRegistryBuilder(): PromiseOrDirect; + getRegistry(): PromiseOrDirect; }; } interface GatherHooks { pgRegistry_PgRegistryBuilder_init(event: { - registryBuilder: PgRegistryBuilder; + registryBuilder: EmptyRegistryBuilder; }): PromiseOrDirect; pgRegistry_PgRegistryBuilder_pgCodecs(event: { - registryBuilder: PgRegistryBuilder; + registryBuilder: DefaultRegistryBuilder; }): PromiseOrDirect; pgRegistry_PgRegistryBuilder_pgResources(event: { - registryBuilder: PgRegistryBuilder; + registryBuilder: DefaultRegistryBuilder; }): PromiseOrDirect; pgRegistry_PgRegistryBuilder_pgRelations(event: { - registryBuilder: PgRegistryBuilder; + registryBuilder: DefaultRegistryBuilder; }): PromiseOrDirect; pgRegistry_PgRegistryBuilder_finalize(event: { - registryBuilder: PgRegistryBuilder; + registryBuilder: DefaultRegistryBuilder; }): PromiseOrDirect; pgRegistry_PgRegistry(event: { - registry: PgRegistry; + registry: DefaultPgRegistry; }): PromiseOrDirect; } } diff --git a/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts index a7851af264..1330350aa5 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts @@ -2,17 +2,18 @@ import "./PgTablesPlugin.js"; import "graphile-config"; import type { - PgCodec, + DefaultPgCodec, + DefaultPgCodecRelationConfig, + DefaultPgRegistry, + DefaultPgResource, + DefaultPgResourceOptions, + DefaultPgSelectSingleStep, PgCodecRef, PgCodecRefPath, - PgCodecRelation, PgCodecRelationConfig, PgCodecWithAttributes, PgRefDefinition, - PgRegistry, - PgResource, PgResourceOptions, - PgSelectSingleStep, PgUnionAllStepConfigAttributes, PgUnionAllStepMember, } from "@dataplan/pg"; @@ -38,8 +39,8 @@ declare global { pgMutationPayloadRelations?: boolean; } interface PgRelationsPluginRelationDetails { - registry: PgRegistry; - codec: PgCodecWithAttributes; + registry: DefaultPgRegistry; + codec: DefaultPgCodec; relationName: string; } @@ -106,7 +107,7 @@ declare global { event: { pgClass: PgClass; serviceName: string; - resourceOptions: PgResourceOptions; + resourceOptions: DefaultPgResourceOptions; }, pgConstraint: PgConstraint, isReferencee?: boolean, @@ -118,7 +119,7 @@ declare global { serviceName: string; pgClass: PgClass; pgConstraint: PgConstraint; - relation: PgCodecRelationConfig; + relation: DefaultPgCodecRelationConfig; }): Promise | void; } } @@ -321,10 +322,10 @@ export const PgRelationsPlugin: GraphileConfig.Plugin = { ), ); const foreignResourceOptions = - (await info.helpers.pgTables.getResourceOptions( + await info.helpers.pgTables.getResourceOptions( serviceName, foreignClass, - )) as PgResourceOptions; + ); if ( !localCodec || !foreignResourceOptions || @@ -402,7 +403,8 @@ export const PgRelationsPlugin: GraphileConfig.Plugin = { : tags.forwardBehavior; const behavior = combineBehaviors(baseBehavior, specificBehavior); const newRelation: PgCodecRelationConfig = { - localCodec: localCodec as PgCodecWithAttributes, + name: relationName, + localCodec, localCodecPolymorphicTypes, localAttributes: localAttributes.map((c) => c!.attname), remoteAttributes: foreignAttributes.map((c) => c!.attname), @@ -455,7 +457,7 @@ export const PgRelationsPlugin: GraphileConfig.Plugin = { } } registryBuilder.addRelation( - codec as PgCodecWithAttributes, + codec, relationName, newRelation.remoteResourceOptions, newRelation, @@ -565,7 +567,7 @@ function makeSpecString( function makeRelationPlans( localAttributes: readonly string[], remoteAttributes: readonly string[], - otherSource: PgResource, + otherSource: DefaultPgResource, isMutationPayload: boolean, ) { const recordOrResult = isMutationPayload @@ -588,16 +590,17 @@ function makeRelationPlans( : null; const specFromRecord = EXPORTABLE( - (localAttributes, remoteAttributes) => ($record: PgSelectSingleStep) => { - return remoteAttributes.reduce((memo, remoteAttributeName, i) => { - memo[remoteAttributeName] = $record.get(localAttributes[i] as string); - return memo; - }, Object.create(null)); - }, + (localAttributes, remoteAttributes) => + ($record: DefaultPgSelectSingleStep) => { + return remoteAttributes.reduce((memo, remoteAttributeName, i) => { + memo[remoteAttributeName] = $record.get(localAttributes[i] as string); + return memo; + }, Object.create(null)); + }, [localAttributes, remoteAttributes], ); type MutationPayload = ObjectStep<{ - result: PgSelectSingleStep; + result: DefaultPgSelectSingleStep; }>; const singleRecordPlan = @@ -622,7 +625,7 @@ return function (otherSource) { ) : EXPORTABLE( (otherSource, specFromRecord) => - function plan($record: PgSelectSingleStep) { + function plan($record: DefaultPgSelectSingleStep) { return otherSource.get(specFromRecord($record)); }, [otherSource, specFromRecord], @@ -649,7 +652,7 @@ return function (otherSource) { ) : EXPORTABLE( (otherSource, specFromRecord) => - function plan($record: PgSelectSingleStep) { + function plan($record: DefaultPgSelectSingleStep) { return otherSource.find(specFromRecord($record)); }, [otherSource, specFromRecord], @@ -679,7 +682,7 @@ return function (otherSource, connection) { ) : EXPORTABLE( (connection, otherSource, specFromRecord) => - function plan($record: PgSelectSingleStep) { + function plan($record: DefaultPgSelectSingleStep) { return connection(otherSource.find(specFromRecord($record))); }, [connection, otherSource, specFromRecord], @@ -722,7 +725,7 @@ function addRelations( ? scope.pgPolymorphicSingleTableType : undefined; - const codec = (pgTypeResource?.codec ?? pgCodec) as PgCodecWithAttributes; + const codec = pgTypeResource?.codec ?? pgCodec; if ( (isMutationPayload && !build.options.pgMutationPayloadRelations) || !(isPgClassType || isMutationPayload || pgPolymorphism) || @@ -732,11 +735,8 @@ function addRelations( return fields; } const resource = pgTypeResource ?? build.pgTableResource(codec); - const relations: Record = (build.input.pgRegistry - .pgRelations[codec.name] ?? Object.create(null)) as Record< - string, - PgCodecRelation - >; + const relations = + build.input.pgRegistry.pgRelations[codec.name] ?? Object.create(null); if (resource && resource.parameters && !resource.isUnique) { return fields; @@ -747,7 +747,7 @@ function addRelations( refName: string; refDefinition: PgRefDefinition; ref?: PgCodecRef; - codec?: PgCodec; + codec?: DefaultPgCodec; }> = isMutationPayload ? [] : codec.refs @@ -783,8 +783,8 @@ function addRelations( listFieldName: string; connectionFieldName: string; description?: string; - pgResource?: PgResource; - pgCodec: PgCodec | undefined; + pgResource?: DefaultPgResource; + pgCodec: DefaultPgCodec | undefined; pgRelationDetails?: GraphileBuild.PgRelationsPluginRelationDetails; relatedTypeName: string; isNonNull: boolean | undefined; @@ -847,7 +847,7 @@ function addRelations( const { singleRecordPlan, listPlan, connectionPlan } = makeRelationPlans( localAttributes as string[], remoteAttributes as string[], - remoteResource as PgResource, + remoteResource, isMutationPayload ?? false, ); const singleRecordFieldName = relation.isReferencee @@ -909,8 +909,8 @@ function addRelations( } of refDefinitionList) { const isUnique = !!refSpec.singular; let hasReferencee; - let sharedCodec: PgCodec | undefined = undefined; - let sharedSource: PgResource | undefined = undefined; + let sharedCodec: DefaultPgCodec | undefined = undefined; + let sharedSource: DefaultPgResource | undefined = undefined; let behavior: string; let typeName: string | null | undefined; let singleRecordPlan; @@ -996,14 +996,12 @@ function addRelations( // Add forbidden names here if (ref.paths.length === 1 && ref.paths[0].length === 1) { - const relation: PgCodecRelation = resource.getRelation( - ref.paths[0][0].relationName, - ); + const relation = resource.getRelation(ref.paths[0][0].relationName); const remoteResource = relation.remoteResource; return makeRelationPlans( relation.localAttributes as string[], relation.remoteAttributes as string[], - remoteResource as PgResource, + remoteResource, isMutationPayload ?? false, ); } else if (!needsPgUnionAll) { @@ -1134,7 +1132,7 @@ function addRelations( const isConnection = mode === "connection"; const attributes: PgUnionAllStepConfigAttributes = unionAttributes ?? {}; - const resourceByTypeName: Record = + const resourceByTypeName: Record = Object.create(null); const members: PgUnionAllStepMember[] = []; for (const path of paths) { @@ -1173,9 +1171,11 @@ function addRelations( ($parent: ExecutableStep) => { const $record = isMutationPayload ? ( - $parent as ObjectStep<{ result: PgSelectSingleStep }> + $parent as ObjectStep<{ + result: DefaultPgSelectSingleStep; + }> ).get("result") - : ($parent as PgSelectSingleStep); + : ($parent as DefaultPgSelectSingleStep); for (let i = 0, l = paths.length; i < l; i++) { const path = paths[i]; const firstLayer = path.layers[0]; diff --git a/graphile-build/graphile-build-pg/src/plugins/PgRowByUniquePlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgRowByUniquePlugin.ts index c57db45798..1072973eec 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgRowByUniquePlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgRowByUniquePlugin.ts @@ -1,10 +1,9 @@ import "graphile-config"; import type { - PgCodec, - PgCodecWithAttributes, - PgResource, - PgResourceUnique, + DefaultPgCodec, + DefaultPgResource, + DefaultPgResourceUnique, } from "@dataplan/pg"; import type { FieldArgs } from "grafast"; import { EXPORTABLE } from "graphile-build"; @@ -19,8 +18,8 @@ declare global { rowByUnique( this: Inflection, details: { - unique: PgResourceUnique; - resource: PgResource; + unique: DefaultPgResourceUnique; + resource: DefaultPgResource; }, ): string; } @@ -84,65 +83,58 @@ export const PgRowByUniquePlugin: GraphileConfig.Plugin = { return resources.reduce( (outerMemo, rawResource) => build.recoverable(outerMemo, () => - (rawResource.uniques as PgResourceUnique[]).reduce( - (memo, unique) => { - const resource = rawResource as PgResource< - any, - PgCodecWithAttributes, - any, - any, - any - >; - const uniqueKeys = unique.attributes as string[]; - const fieldName = build.inflection.rowByUnique({ - unique, - resource, - }); + rawResource.uniques.reduce((memo, unique) => { + const resource = rawResource; + const uniqueKeys = unique.attributes; + const fieldName = build.inflection.rowByUnique({ + unique, + resource, + }); - const type = build.getTypeByName( - build.inflection.tableType(resource.codec), - ); - if (!type || !(type instanceof GraphQLObjectType)) { - return memo; - } + const type = build.getTypeByName( + build.inflection.tableType(resource.codec), + ); + if (!type || !(type instanceof GraphQLObjectType)) { + return memo; + } - const detailsByAttributeName: { - [attributeName: string]: { - graphqlName: string; - codec: PgCodec; - }; - } = Object.create(null); - uniqueKeys.forEach((attributeName) => { - const attribute = resource.codec.attributes![attributeName]; - const attributeArgName = build.inflection.attribute({ - attributeName, - codec: resource.codec, - }); - detailsByAttributeName[attributeName] = { - graphqlName: attributeArgName, - codec: attribute.codec, - }; + const detailsByAttributeName: { + [attributeName: string]: { + graphqlName: string; + codec: DefaultPgCodec; + }; + } = Object.create(null); + uniqueKeys.forEach((attributeName) => { + const attribute = resource.codec.attributes![attributeName]; + const attributeArgName = build.inflection.attribute({ + attributeName, + codec: resource.codec, }); + detailsByAttributeName[attributeName] = { + graphqlName: attributeArgName, + codec: attribute.codec, + }; + }); - const attributeNames = Object.keys(detailsByAttributeName); - const clean = attributeNames.every( - (key) => - isSafeObjectPropertyName(key) && - isSafeObjectPropertyName( - detailsByAttributeName[key].graphqlName, - ), - ); - const plan = clean - ? /* - * Since all the identifiers are nice and clean we can use - * an optimized function that doesn't loop over the - * attributes and just builds the object directly. This is - * more performant, but it also makes the code nicer to - * read in the exported code. - */ - // eslint-disable-next-line graphile-export/exhaustive-deps - EXPORTABLE( - te.run`\ + const attributeNames = Object.keys(detailsByAttributeName); + const clean = attributeNames.every( + (key) => + isSafeObjectPropertyName(key) && + isSafeObjectPropertyName( + detailsByAttributeName[key].graphqlName, + ), + ); + const plan = clean + ? /* + * Since all the identifiers are nice and clean we can use + * an optimized function that doesn't loop over the + * attributes and just builds the object directly. This is + * more performant, but it also makes the code nicer to + * read in the exported code. + */ + // eslint-disable-next-line graphile-export/exhaustive-deps + EXPORTABLE( + te.run`\ return function (resource) { return (_$root, args) => resource.get({ ${te.join( attributeNames.map( @@ -154,76 +146,72 @@ return function (resource) { ", ", )} }); }` as any, - [resource], - ) - : EXPORTABLE( - (detailsByAttributeName, resource) => - function plan(_$root: any, args: FieldArgs) { - const spec = Object.create(null); - for (const attributeName in detailsByAttributeName) { - spec[attributeName] = args.get( - detailsByAttributeName[attributeName] - .graphqlName, - ); - } - return resource.get(spec); - }, - [detailsByAttributeName, resource], - ); - - const fieldBehaviorScope = "query:resource:single"; - if ( - !build.behavior.pgResourceUniqueMatches( - [resource, unique], - fieldBehaviorScope, + [resource], ) - ) { - return memo; - } - - return build.extend( - memo, - { - [fieldName]: fieldWithHooks( - { - fieldName, - fieldBehaviorScope, + : EXPORTABLE( + (detailsByAttributeName, resource) => + function plan(_$root: any, args: FieldArgs) { + const spec = Object.create(null); + for (const attributeName in detailsByAttributeName) { + spec[attributeName] = args.get( + detailsByAttributeName[attributeName].graphqlName, + ); + } + return resource.get(spec); }, - () => ({ - description: `Get a single \`${type.name}\`.`, - deprecationReason: tagToString( - resource.extensions?.tags?.deprecated, - ), - type, - args: uniqueKeys.reduce((args, attributeName) => { - const details = - detailsByAttributeName[attributeName]; - const attributeType = build.getGraphQLTypeByPgCodec( - details.codec, - "input", + [detailsByAttributeName, resource], + ); + + const fieldBehaviorScope = "query:resource:single"; + if ( + !build.behavior.pgResourceUniqueMatches( + [resource, unique], + fieldBehaviorScope, + ) + ) { + return memo; + } + + return build.extend( + memo, + { + [fieldName]: fieldWithHooks( + { + fieldName, + fieldBehaviorScope, + }, + () => ({ + description: `Get a single \`${type.name}\`.`, + deprecationReason: tagToString( + resource.extensions?.tags?.deprecated, + ), + type, + args: uniqueKeys.reduce((args, attributeName) => { + const details = detailsByAttributeName[attributeName]; + const attributeType = build.getGraphQLTypeByPgCodec( + details.codec, + "input", + ); + if (!attributeType) { + throw new Error( + `Could not determine type for attribute`, ); - if (!attributeType) { - throw new Error( - `Could not determine type for attribute`, - ); - } - args[details.graphqlName] = { - type: new GraphQLNonNull(attributeType), - }; - return args; - }, Object.create(null)), + } + args[details.graphqlName] = { + type: new GraphQLNonNull(attributeType), + }; + return args; + }, Object.create(null)), - plan: plan as any, - }), - ), - }, - `Adding row accessor for ${resource} by unique attributes ${uniqueKeys.join( - ",", - )}`, - ); - }, - outerMemo, - ), + plan: plan as any, + }), + ), + }, + `Adding row accessor for ${resource} by unique attributes ${uniqueKeys.join( + ",", + )}`, + ); + }, outerMemo), ), fields, ); diff --git a/graphile-build/graphile-build-pg/src/plugins/PgTableNodePlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgTableNodePlugin.ts index 54928bdf5e..50f7a55c6d 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgTableNodePlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgTableNodePlugin.ts @@ -1,11 +1,9 @@ import "graphile-config"; import type { - PgCodec, - PgCodecWithAttributes, - PgResource, - PgResourceUnique, - PgSelectSingleStep, + AnyPgSelectSingleStep, + DefaultPgCodec, + DefaultPgResource, } from "@dataplan/pg"; import type { ListStep } from "grafast"; import { access, constant, list } from "grafast"; @@ -43,9 +41,7 @@ export const PgTableNodePlugin: GraphileConfig.Plugin = { codec.polymorphism.mode === "single" || codec.polymorphism.mode === "relational") ) { - const resource = build.pgTableResource( - codec as PgCodecWithAttributes, - ); + const resource = build.pgTableResource(codec); if (resource && resource.uniques?.length >= 1) { if (codec.polymorphism) { newBehavior.push("interface:node"); @@ -80,7 +76,7 @@ export const PgTableNodePlugin: GraphileConfig.Plugin = { ); }); - const resourcesByCodec = new Map(); + const resourcesByCodec = new Map(); for (const resource of tableResources) { let list = resourcesByCodec.get(resource.codec); if (!list) { @@ -110,7 +106,7 @@ export const PgTableNodePlugin: GraphileConfig.Plugin = { continue; } const pgResource = resources[0]; - const primaryKey = (pgResource.uniques as PgResourceUnique[]).find( + const primaryKey = pgResource.uniques.find( (u) => u.isPrimary === true, ); if (!primaryKey) { @@ -154,7 +150,7 @@ return function (list, constant) { ) : EXPORTABLE( (constant, identifier, list, pk) => - ($record: PgSelectSingleStep) => { + ($record: AnyPgSelectSingleStep) => { return list([ constant(identifier, false), ...pk.map((attribute) => $record.get(attribute)), diff --git a/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts index 5c22360cdd..c82c2ec55b 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts @@ -1,9 +1,12 @@ import type { + AnyPgCodecAttribute, + AnyPgResource, + DefaultPgResourceOptions, + AnyPgResourceUnique, PgCodec, - PgCodecAttribute, PgResource, - PgResourceOptions, - PgResourceUnique, + AnyPgResourceOptions, + DefaultPgCodec, } from "@dataplan/pg"; import { assertPgClassSingleStep, makePgResourceOptions } from "@dataplan/pg"; import { object } from "grafast"; @@ -87,7 +90,7 @@ declare global { */ _codecName( this: Inflection, - codec: PgCodec, + codec: DefaultPgCodec, ): string; /** @@ -103,7 +106,7 @@ declare global { */ _singularizedCodecName( this: Inflection, - codec: PgCodec, + codec: DefaultPgCodec, ): string; /** @@ -119,17 +122,17 @@ declare global { */ tableType( this: GraphileBuild.Inflection, - codec: PgCodec, + codec: DefaultPgCodec, ): string; tableConnectionType( this: GraphileBuild.Inflection, - codec: PgCodec, + codec: DefaultPgCodec, ): string; tableEdgeType( this: GraphileBuild.Inflection, - codec: PgCodec, + codec: DefaultPgCodec, ): string; patchType(this: GraphileBuild.Inflection, typeName: string): string; @@ -137,21 +140,21 @@ declare global { } interface ScopeObject { - pgCodec?: PgCodec; + pgCodec?: DefaultPgCodec; isPgClassType?: boolean; isPgConnectionRelated?: true; } interface ScopeObjectFieldsField { pgFieldResource?: PgResource; - pgFieldCodec?: PgCodec; - pgFieldAttribute?: PgCodecAttribute; + pgFieldCodec?: DefaultPgCodec; + pgFieldAttribute?: AnyPgCodecAttribute; isPgFieldConnection?: boolean; isPgFieldSimpleCollection?: boolean; } interface ScopeInterfaceFieldsField { pgFieldResource?: PgResource; - pgFieldCodec?: PgCodec; - pgFieldAttribute?: PgCodecAttribute; + pgFieldCodec?: DefaultPgCodec; + pgFieldAttribute?: AnyPgCodecAttribute; isPgFieldConnection?: boolean; isPgFieldSimpleCollection?: boolean; } @@ -165,7 +168,7 @@ declare global { getResourceOptions( serviceName: string, pgClass: PgClass, - ): Promise; + ): Promise; }; } @@ -177,7 +180,7 @@ declare global { serviceName: string; pgClass: PgClass; pgConstraint: PgConstraint; - unique: PgResourceUnique; + unique: AnyPgResourceUnique; }): void | Promise; /** * Passed the PgResourceOptions before it's added to the PgRegistryBuilder. @@ -185,17 +188,17 @@ declare global { pgTables_PgResourceOptions(event: { serviceName: string; pgClass: PgClass; - resourceOptions: PgResourceOptions; + resourceOptions: DefaultPgResourceOptions; }): void | Promise; pgTables_PgResourceOptions_relations(event: { serviceName: string; pgClass: PgClass; - resourceOptions: PgResourceOptions; + resourceOptions: DefaultPgResourceOptions; }): Promise | void; pgTables_PgResourceOptions_relations_post(event: { serviceName: string; pgClass: PgClass; - resourceOptions: PgResourceOptions; + resourceOptions: DefaultPgResourceOptions; }): Promise | void; } } @@ -204,11 +207,11 @@ declare global { interface State { resourceOptionsByPgClassByService: Map< string, - Map> + Map> >; - resourceByResourceOptions: Map>; + resourceByResourceOptions: Map>; detailsByResourceOptions: Map< - PgResourceOptions, + DefaultPgResourceOptions, { serviceName: string; pgClass: PgClass } >; } @@ -406,7 +409,7 @@ export const PgTablesPlugin: GraphileConfig.Plugin = { uniqueAttributeOnlyConstraints.map(async (pgConstraint) => { const { tags, description } = pgConstraint.getTagsAndDescription(); - const unique: PgResourceUnique = { + const unique: AnyPgResourceUnique = { isPrimary: pgConstraint.contype === "p", attributes: pgConstraint.conkey!.map( (k) => attributes.find((att) => att.attnum === k)!.attname, @@ -543,7 +546,7 @@ export const PgTablesPlugin: GraphileConfig.Plugin = { await info.helpers.pgIntrospection.getIntrospection(); const toProcess: Array<{ - resourceOptions: PgResourceOptions; + resourceOptions: AnyPgResourceOptions; pgClass: PgClass; serviceName: string; }> = []; diff --git a/graphile-build/graphile-build-pg/src/utils.ts b/graphile-build/graphile-build-pg/src/utils.ts index 273293efff..f382c701a5 100644 --- a/graphile-build/graphile-build-pg/src/utils.ts +++ b/graphile-build/graphile-build-pg/src/utils.ts @@ -385,9 +385,7 @@ export const resolveResourceRefPath = ( layers: [] as Layer[], }; for (const pathEntry of path) { - const relation = result.resource.getRelation( - pathEntry.relationName, - ) as PgCodecRelation; + const relation = result.resource.getRelation(pathEntry.relationName); const { isReferencee, localAttributes, diff --git a/graphile-build/graphile-simplify-inflection/src/index.ts b/graphile-build/graphile-simplify-inflection/src/index.ts index ac3653ed46..9504da3024 100644 --- a/graphile-build/graphile-simplify-inflection/src/index.ts +++ b/graphile-build/graphile-simplify-inflection/src/index.ts @@ -320,9 +320,9 @@ const PgSimplifyInflectionPlugin: GraphileConfig.Plugin = { if (baseName) { return this.camelCase(baseName); } - const foreignPk = ( - relation.remoteResource.uniques as PgResourceUnique[] - ).find((u) => u.isPrimary); + const foreignPk = relation.remoteResource.uniques.find( + (u) => u.isPrimary, + ); if ( foreignPk && arraysMatch(foreignPk.attributes, relation.remoteAttributes) @@ -375,8 +375,7 @@ const PgSimplifyInflectionPlugin: GraphileConfig.Plugin = { `singleRelationBackwards inflector check failed: multiple table-like resources for codec '${codec.name}', so we cannot determine the primary key - please override this inflector for this table.`, ); } - const uniques = (possibleResources[0]?.uniques ?? - []) as PgResourceUnique[]; + const uniques = possibleResources[0]?.uniques ?? []; const pk = uniques.find((u) => u.isPrimary); if (pk && arraysMatch(pk.attributes, relation.localAttributes)) { return this.camelCase( @@ -417,9 +416,7 @@ const PgSimplifyInflectionPlugin: GraphileConfig.Plugin = { ); } } - const pk = (relation.remoteResource.uniques as PgResourceUnique[]).find( - (u) => u.isPrimary, - ); + const pk = relation.remoteResource.uniques.find((u) => u.isPrimary); if (pk && arraysMatch(pk.attributes, relation.remoteAttributes)) { return this.camelCase( `${this.distinctPluralize( diff --git a/graphile-build/graphile-utils/src/makeAddPgTableConditionPlugin.ts b/graphile-build/graphile-utils/src/makeAddPgTableConditionPlugin.ts index 02b5ee4c7d..375e070f87 100644 --- a/graphile-build/graphile-utils/src/makeAddPgTableConditionPlugin.ts +++ b/graphile-build/graphile-utils/src/makeAddPgTableConditionPlugin.ts @@ -1,4 +1,8 @@ -import type { PgConditionStep, PgSelectStep } from "@dataplan/pg"; +import type { + DefaultPgSelectStep, + PgConditionStep, + PgSelectStep, +} from "@dataplan/pg"; import type { FieldArgs, GrafastInputFieldConfig } from "grafast"; import type { SQL, sql } from "pg-sql2"; @@ -13,7 +17,7 @@ export function makeAddPgTableConditionPlugin( conditionGenerator?: ( value: FieldArgs, helpers: { - $condition: PgConditionStep; + $condition: PgConditionStep; sql: typeof sql; sqlTableAlias: SQL; build: GraphileBuild.Build; @@ -85,7 +89,7 @@ export function makeAddPgTableConditionPlugin( conditionFieldSpec.applyPlan = EXPORTABLE( (build, conditionGenerator, sql) => function applyPlan( - $condition: PgConditionStep, + $condition: PgConditionStep, val, ) { const expression = conditionGenerator!(val, { diff --git a/graphile-build/graphile-utils/src/makeAddPgTableOrderByPlugin.ts b/graphile-build/graphile-utils/src/makeAddPgTableOrderByPlugin.ts index ee3e9bee19..95638be761 100644 --- a/graphile-build/graphile-utils/src/makeAddPgTableOrderByPlugin.ts +++ b/graphile-build/graphile-utils/src/makeAddPgTableOrderByPlugin.ts @@ -1,17 +1,17 @@ -import type { PgOrderSpec, PgSelectStep } from "@dataplan/pg"; +import type { DefaultPgSelectStep, PgOrderSpec, PgSelectStep } from "@dataplan/pg"; import { EXPORTABLE } from "./exportable.js"; type OrderBySpecIdentity = | string // Attribute name | Omit // Expression - | (($select: PgSelectStep) => Omit); // Callback, allows for joins/etc + | (($select: DefaultPgSelectStep) => Omit); // Callback, allows for joins/etc export interface MakeAddPgTableOrderByPluginOrders { [orderByEnumValue: string]: { extensions: { grafast: { - applyPlan($select: PgSelectStep): void; + applyPlan($select: DefaultPgSelectStep): void; }; }; }; @@ -127,7 +127,7 @@ export function orderByAscDesc( ? "FIRST" : "LAST"; - type Plan = ($select: PgSelectStep) => void; + type Plan = ($select: DefaultPgSelectStep) => void; let spec: PgOrderSpec; const ascendingPlan: Plan = diff --git a/postgraphile/postgraphile/src/plugins/PgV4BehaviorPlugin.ts b/postgraphile/postgraphile/src/plugins/PgV4BehaviorPlugin.ts index 0f67ee06c2..62373de680 100644 --- a/postgraphile/postgraphile/src/plugins/PgV4BehaviorPlugin.ts +++ b/postgraphile/postgraphile/src/plugins/PgV4BehaviorPlugin.ts @@ -1,7 +1,7 @@ import "graphile-config"; import "graphile-build-pg"; -import type { PgResourceOptions } from "@dataplan/pg"; +import type { DefaultPgResourceOptions, PgResourceOptions } from "@dataplan/pg"; import type { PgProc } from "graphile-build-pg/pg-introspection"; import { inspect } from "util"; @@ -14,7 +14,7 @@ declare global { } const v4ComputedAttributeChecks = ( - _s: PgResourceOptions, + _s: DefaultPgResourceOptions, pgProc: PgProc, ): boolean => { const args = pgProc.getArguments(); diff --git a/postgraphile/postgraphile/src/plugins/PgV4InflectionPlugin.ts b/postgraphile/postgraphile/src/plugins/PgV4InflectionPlugin.ts index 90633efcb4..c2107f5aa6 100644 --- a/postgraphile/postgraphile/src/plugins/PgV4InflectionPlugin.ts +++ b/postgraphile/postgraphile/src/plugins/PgV4InflectionPlugin.ts @@ -27,13 +27,13 @@ export const PgV4InflectionPlugin: GraphileConfig.Plugin = { }, _attributeName(previous, options, details) { const { codec, attributeName } = details; - const attribute = codec.attributes[attributeName]; + const attribute = codec.attributes![attributeName]; if (!attribute) { throw new Error( `Attempted to access attribute '${attributeName}' of codec '${ codec.name }', but it doesn't have that attribute (known attributes: ${Object.keys( - codec.attributes, + codec.attributes!, ).join(", ")})`, ); } diff --git a/postgraphile/postgraphile/src/presets/relay.ts b/postgraphile/postgraphile/src/presets/relay.ts index a13a79e02a..83cee34b3d 100644 --- a/postgraphile/postgraphile/src/presets/relay.ts +++ b/postgraphile/postgraphile/src/presets/relay.ts @@ -51,7 +51,7 @@ export const PgRelayPlugin: GraphileConfig.Plugin = { entityBehavior: { pgCodecAttribute(behavior, [codec, attributeName], build) { const newBehavior = [behavior]; - const attr = codec.attributes[attributeName]; + const attr = codec.attributes![attributeName]; const resource = codec.polymorphism?.mode === "union" @@ -88,9 +88,9 @@ export const PgRelayPlugin: GraphileConfig.Plugin = { // If the column is available via a singular relation, don't include the column itself const relationsMap = build.input.pgRegistry.pgRelations[codec.name]; const relations = relationsMap - ? (Object.values( + ? Object.values( build.input.pgRegistry.pgRelations[codec.name] ?? {}, - ) as PgCodecRelation[]) + ) : []; const singularRelationsUsingThisColumn = relations.filter((r) => { // NOTE: We do this even if the end table is not visible, because @@ -110,10 +110,8 @@ export const PgRelayPlugin: GraphileConfig.Plugin = { newBehavior.push(...RELAY_HIDDEN_COLUMN_BEHAVIORS); } } - const relations = ( - Object.values( - build.input.pgRegistry.pgRelations[codec.name] ?? {}, - ) as PgCodecRelation[] + const relations = Object.values( + build.input.pgRegistry.pgRelations[codec.name] ?? {}, ).filter((r) => !r.isReferencee && r.isUnique); const isPartOfRelation = !attr.codec.extensions?.isEnumTableEnum && diff --git a/postgraphile/postgraphile/src/presets/v4.ts b/postgraphile/postgraphile/src/presets/v4.ts index b626db9f7a..08c8d26c59 100644 --- a/postgraphile/postgraphile/src/presets/v4.ts +++ b/postgraphile/postgraphile/src/presets/v4.ts @@ -154,7 +154,7 @@ const makeV4Plugin = (options: V4Options): GraphileConfig.Plugin => { entityBehavior: { pgResource: "+delete:resource:select", pgCodecAttribute(behavior, [codec, attributeName]) { - const attribute = codec.attributes[attributeName]; + const attribute = codec.attributes![attributeName]; const underlyingCodec = attribute.codec.domainOfCodec ?? attribute.codec; const newBehavior = [behavior]; diff --git a/utils/graphile/src/commands/behavior/debug/main.ts b/utils/graphile/src/commands/behavior/debug/main.ts index 04daf71d3c..a0ba62ff25 100644 --- a/utils/graphile/src/commands/behavior/debug/main.ts +++ b/utils/graphile/src/commands/behavior/debug/main.ts @@ -185,7 +185,7 @@ function getEntities( build: GraphileBuild.Build, entityType: keyof GraphileBuild.BehaviorEntities, ) { - const registry = build.input.pgRegistry as PgRegistry; + const registry = build.input.pgRegistry; switch (entityType) { case "pgCodec": { return registry.pgCodecs; From e0e319fe7b149d0893be24673f67f2493e61b024 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Wed, 22 Nov 2023 14:10:36 +0000 Subject: [PATCH 2/8] Trailing newline --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1492be3d39..aae85fdc63 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,4 @@ grafast/dataplan-pg/__tests__/**/*.mermaid.png /Session.vim /WHAT_ARE_YOU_DOING.md contrib -/env \ No newline at end of file +/env From e10dce00823d7af9f49524656b904d489a47cc75 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Wed, 22 Nov 2023 14:15:18 +0000 Subject: [PATCH 3/8] Any -> _Any --- grafast/dataplan-pg/src/codecs.ts | 40 +++--- grafast/dataplan-pg/src/datasource.ts | 134 +++++++++--------- .../dataplan-pg/src/examples/exampleSchema.ts | 14 +- .../src/filters/pgBooleanFilter.ts | 4 +- .../dataplan-pg/src/filters/pgClassFilter.ts | 4 +- grafast/dataplan-pg/src/filters/pgOrFilter.ts | 4 +- grafast/dataplan-pg/src/index.ts | 96 ++++++------- grafast/dataplan-pg/src/interfaces.ts | 114 +++++++-------- grafast/dataplan-pg/src/pgLocker.ts | 12 +- .../src/steps/pgClassExpression.ts | 12 +- grafast/dataplan-pg/src/steps/pgCondition.ts | 6 +- .../dataplan-pg/src/steps/pgDeleteSingle.ts | 12 +- .../dataplan-pg/src/steps/pgInsertSingle.ts | 14 +- grafast/dataplan-pg/src/steps/pgSelect.ts | 54 +++---- .../dataplan-pg/src/steps/pgSelectSingle.ts | 28 ++-- grafast/dataplan-pg/src/steps/pgTempTable.ts | 8 +- grafast/dataplan-pg/src/steps/pgUnionAll.ts | 38 ++--- .../dataplan-pg/src/steps/pgUpdateSingle.ts | 18 +-- grafast/dataplan-pg/src/steps/toPg.ts | 6 +- grafast/grafast/src/input.ts | 12 +- grafast/grafast/src/interfaces.ts | 18 +-- grafast/grafast/src/steps/node.ts | 4 +- .../step-library/standard-steps/node.md | 2 +- .../src/plugins/PgCodecsPlugin.ts | 4 +- .../src/plugins/PgRefsPlugin.ts | 6 +- .../src/plugins/PgTableNodePlugin.ts | 4 +- .../src/plugins/PgTablesPlugin.ts | 20 +-- utils/graphile-export/src/exportSchema.ts | 10 +- 28 files changed, 349 insertions(+), 349 deletions(-) diff --git a/grafast/dataplan-pg/src/codecs.ts b/grafast/dataplan-pg/src/codecs.ts index 3bcdce9c40..9d562b5104 100644 --- a/grafast/dataplan-pg/src/codecs.ts +++ b/grafast/dataplan-pg/src/codecs.ts @@ -38,7 +38,7 @@ import { import type { PgExecutor } from "./executor.js"; import { inspect } from "./inspect.js"; import type { - AnyPgCodec, + _AnyPgCodec, DefaultPgCodec, PgCodec, PgCodecAttributeMap, @@ -63,13 +63,13 @@ export type PgCodecAttributeViaExplicit< relation: TRelationName; attribute: TAttribute; }; -export interface AnyPgCodecAttributeVia extends PgCodecAttributeVia {} +export interface _AnyPgCodecAttributeVia extends PgCodecAttributeVia {} export type DefaultPgCodecAttributeVia = PgCodecAttributeVia; -export type AnyPgCodecAttributeViaRelationName = +export type _AnyPgCodecAttributeViaRelationName = U extends PgCodecAttributeVia ? TRelationName : never; -export type AnyPgCodecAttributeViaAttribute = U extends PgCodecAttributeVia< +export type _AnyPgCodecAttributeViaAttribute = U extends PgCodecAttributeVia< any, infer TAttributeName > @@ -101,11 +101,11 @@ export type PgCodecAttributeCodec = U extends PgCodecAttribute< export interface DefaultPgCodecAttribute extends PgCodecAttribute {} -export interface AnyPgCodecAttribute extends PgCodecAttribute {} +export interface _AnyPgCodecAttribute extends PgCodecAttribute {} export interface PgCodecAttribute< TName extends string, - TCodec extends AnyPgCodec, + TCodec extends _AnyPgCodec, TNotNull extends boolean, > { name: TName; @@ -133,7 +133,7 @@ export interface PgCodecAttribute< * If this attribute actually exists on a relation rather than locally, the name * of the (unique) relation this attribute belongs to. */ - via?: AnyPgCodecAttributeVia; + via?: _AnyPgCodecAttributeVia; /** * If the attribute exists identically on a relation and locally (e.g. @@ -158,7 +158,7 @@ export interface PgCodecAttribute< * these are all plural relationships. So identicalVia is generally one-way * (except in 1-to-1 relationships). */ - identicalVia?: AnyPgCodecAttributeVia; + identicalVia?: _AnyPgCodecAttributeVia; // ENHANCE: can identicalVia be plural? Is that useful? Maybe a attribute that has // multiple foreign key references? @@ -356,7 +356,7 @@ function recordStringToTuple(value: string): Array { } function realAttributeDefs< - TAttributes extends Record, + TAttributes extends Record, >( attributes: TAttributes, ): Array< @@ -384,7 +384,7 @@ function realAttributeDefs< * @see {@link https://www.postgresql.org/docs/current/rowtypes.html#id-1.5.7.24.6} */ function makeRecordToSQLRawValue< - TAttributes extends Record, + TAttributes extends Record, >(attributes: TAttributes): PgEncode> { const attributeDefs = realAttributeDefs(attributes); return (value) => { @@ -398,7 +398,7 @@ function makeRecordToSQLRawValue< } export type ObjectFromPgCodecAttributes< - TAttributes extends Record, + TAttributes extends Record, > = { [TCodecAttribute in keyof TAttributes as PgCodecAttributeName< TAttributes[TCodecAttribute] @@ -423,7 +423,7 @@ export type ObjectFromPgCodecAttributes< * @see {@link https://www.postgresql.org/docs/current/rowtypes.html#id-1.5.7.24.6} */ function makeSQLValueToRecord< - TAttributes extends Record, + TAttributes extends Record, >( attributes: TAttributes, ): (value: string) => ObjectFromPgCodecAttributes { @@ -444,7 +444,7 @@ function makeSQLValueToRecord< export type PgRecordTypeCodecSpec< TName extends string, TAttributes extends { - [TAttribute in keyof TAttributes]: Omit; + [TAttribute in keyof TAttributes]: Omit<_AnyPgCodecAttribute, "name">; }, > = { name: TName; @@ -470,7 +470,7 @@ export type PgRecordTypeCodecSpec< export function recordCodec< const TName extends string, const TAttributes extends { - [TAttribute in keyof TAttributes]: Omit; + [TAttribute in keyof TAttributes]: Omit<_AnyPgCodecAttribute, "name">; }, const TCodecAttributes extends { [TAttribute in keyof TAttributes]: PgCodecAttribute< @@ -609,7 +609,7 @@ type CodecWithListCodec< * @param typeDelim - the delimeter used to separate entries in this list when Postgres stringifies it * @param identifier - a pg-sql2 fragment that represents the name of this type */ -export function listOfCodec( +export function listOfCodec( listedCodec: TInnerCodec, config?: { description?: string; @@ -732,7 +732,7 @@ exportAs("@dataplan/pg", listOfCodec, "listOfCodec"); */ export function domainOfCodec< TName extends string, - TInnerCodec extends AnyPgCodec, + TInnerCodec extends _AnyPgCodec, >( innerCodec: TInnerCodec, name: TName, @@ -747,7 +747,7 @@ export function domainOfCodec< PgCodecAttributeMap, PgCodecFromPostgres, any, - AnyPgCodec, + _AnyPgCodec, TInnerCodec, any > { @@ -773,7 +773,7 @@ exportAs("@dataplan/pg", domainOfCodec, "domainOfCodec"); * * @internal */ -function escapeRangeValue( +function escapeRangeValue( value: null | any, innerCodec: TInnerCodec, ): string { @@ -801,7 +801,7 @@ interface PgRange { */ export function rangeOfCodec< TName extends string, - TInnerCodec extends AnyPgCodec, + TInnerCodec extends _AnyPgCodec, >( innerCodec: TInnerCodec, name: TName, @@ -1227,7 +1227,7 @@ export function getCodecByPgCatalogTypeName(pgCatalogTypeName: string) { return null; } -export function getInnerCodec( +export function getInnerCodec( codec: TCodec, ): TCodec extends PgCodec< any, diff --git a/grafast/dataplan-pg/src/datasource.ts b/grafast/dataplan-pg/src/datasource.ts index 3d772caf2f..e1973726cd 100644 --- a/grafast/dataplan-pg/src/datasource.ts +++ b/grafast/dataplan-pg/src/datasource.ts @@ -11,7 +11,7 @@ import type { SQL } from "pg-sql2"; import sql from "pg-sql2"; import type { - AnyPgCodecAttribute, + _AnyPgCodecAttribute, DefaultPgCodecAttribute, PgCodecAttributeName, PgCodecAttributeVia, @@ -28,7 +28,7 @@ import type { } from "./executor.js"; import { inspect } from "./inspect.js"; import type { - AnyPgCodecRelationConfig, + _AnyPgCodecRelationConfig, PgCodecAttributes, GetPgRegistryCodecRelations, PgCodec, @@ -38,12 +38,12 @@ import type { PgRegistry, PgRegistryConfig, PlanByUniques, - AnyPgCodec, - AnyPgRegistry, + _AnyPgCodec, + _AnyPgRegistry, GetPgRegistryCodecRelationConfigs, GetPgRegistryCodecs, - AnyPgRegistryConfig, - AnyScalarPgCodec, + _AnyPgRegistryConfig, + _AnyScalarPgCodec, PgCodecRelationConfigName, PgRegistryConfigCodecs, PgRegistryConfigRelationConfigs, @@ -94,7 +94,7 @@ export type PgResourceExtensions = DataplanPg.PgResourceExtensions; export type PgResourceParameterExtensions = DataplanPg.PgResourceParameterExtensions; -export interface AnyPgResourceParameter extends PgResourceParameter {} +export interface _AnyPgResourceParameter extends PgResourceParameter {} export interface DefaultPgResourceParameter extends PgResourceParameter {} @@ -104,7 +104,7 @@ export interface DefaultPgResourceParameter */ export interface PgResourceParameter< TName extends string | null, - TCodec extends AnyPgCodec, + TCodec extends _AnyPgCodec, > { /** * Name of the parameter, if null then we must use positional rather than @@ -127,13 +127,13 @@ export interface PgResourceParameter< extensions?: PgResourceParameterExtensions; } -export interface AnyPgResourceUnique extends PgResourceUnique {} +export interface _AnyPgResourceUnique extends PgResourceUnique {} export interface DefaultPgResourceUnique extends PgResourceUnique {} /** * Description of a unique constraint on a PgResource. */ -export interface PgResourceUnique { +export interface PgResourceUnique { /** * The attributes that are unique */ @@ -177,7 +177,7 @@ export interface DefaultPgResourceOptions< DefaultPgResourceParameter > {} -export interface AnyPgResourceOptions +export interface _AnyPgResourceOptions extends PgResourceOptions {} export type PgResourceOptionName = U extends PgResourceOptions< @@ -218,9 +218,9 @@ export type PgResourceOptionParameters = U extends PgResourceOptions< */ export interface PgResourceOptions< TName extends string, - TCodec extends AnyPgCodec, + TCodec extends _AnyPgCodec, TUniques extends PgResourceUnique>, - TParameters extends AnyPgResourceParameter, + TParameters extends _AnyPgResourceParameter, > { /** * The associated codec for this resource @@ -234,7 +234,7 @@ export interface PgResourceOptions< executor: PgExecutor; // TODO: auth should also apply to insert, update and delete, maybe via insertAuth, updateAuth, etc - selectAuth?: ($step: PgSelectStep) => void; + selectAuth?: ($step: PgSelectStep<_AnyPgResource>) => void; name: TName; identifier?: string; @@ -269,7 +269,7 @@ export interface PgResourceOptions< isVirtual?: boolean; } -export interface AnyPgFunctionResourceOptions +export interface _AnyPgFunctionResourceOptions extends PgFunctionResourceOptions {} export interface DefaultPgFunctionResourceOptions extends PgFunctionResourceOptions< @@ -281,9 +281,9 @@ export interface DefaultPgFunctionResourceOptions export interface PgFunctionResourceOptions< TNewName extends string, - TCodec extends AnyPgCodec, + TCodec extends _AnyPgCodec, TUniques extends PgResourceUnique>, - TNewParameters extends AnyPgResourceParameter, + TNewParameters extends _AnyPgResourceParameter, > { name: TNewName; identifier?: string; @@ -296,7 +296,7 @@ export interface PgFunctionResourceOptions< uniques?: [TUniques] extends [never] ? never : ReadonlyArray; extensions?: PgResourceExtensions; isMutation?: boolean; - selectAuth?: ($step: PgSelectStep) => void; + selectAuth?: ($step: PgSelectStep<_AnyPgResource>) => void; description?: string; } export type PgResourceName = U extends PgResource< @@ -353,9 +353,9 @@ export interface DefaultPgResource DefaultPgResourceParameter, any > {} -export interface AnyPgResource extends PgResource {} -export interface AnyScalarPgResource - extends PgResource {} +export interface _AnyPgResource extends PgResource {} +export interface _AnyScalarPgResource + extends PgResource {} /** * PgResource represents any resource of SELECT-able data in Postgres: tables, @@ -363,10 +363,10 @@ export interface AnyScalarPgResource */ export class PgResource< TName extends string, - TCodec extends AnyPgCodec, + TCodec extends _AnyPgCodec, TUniques extends PgResourceUnique>, - TParameters extends AnyPgResourceParameter, - TRegistry extends AnyPgRegistry, + TParameters extends _AnyPgResourceParameter, + TRegistry extends _AnyPgRegistry, > { public readonly registry: TRegistry; public readonly codec: TCodec; @@ -377,7 +377,7 @@ export class PgResource< public readonly uniques: [TUniques] extends [never] ? never : ReadonlyArray; - private selectAuth?: ($step: PgSelectStep) => void; + private selectAuth?: ($step: PgSelectStep<_AnyPgResource>) => void; // TODO: make a public interface for this information /** @@ -488,7 +488,7 @@ export class PgResource< * type/relations/etc. */ static alternativeResourceOptions< - TCodec extends AnyPgCodec, + TCodec extends _AnyPgCodec, const TNewUniques extends PgResourceUnique>, const TNewName extends string, >( @@ -524,8 +524,8 @@ export class PgResource< * type/relations/etc but pull their rows from functions. */ static functionResourceOptions< - TCodec extends AnyPgCodec, - const TNewParameters extends AnyPgResourceParameter, + TCodec extends _AnyPgCodec, + const TNewParameters extends _AnyPgResourceParameter, const TNewUniques extends PgResourceUnique>, const TNewName extends string, >( @@ -755,7 +755,7 @@ export class PgResource< } const keys = Object.keys(spec); if ( - !this.uniques.some((uniq: AnyPgResourceUnique) => + !this.uniques.some((uniq: _AnyPgResourceUnique) => uniq.attributes.every((key) => keys.includes(key)), ) ) { @@ -933,7 +933,7 @@ export class PgResource< const pk = this.uniques.find((u) => u.isPrimary); const nonNullableAttribute = this.codec.attributes ? Object.entries( - this.codec.attributes as Record, + this.codec.attributes as Record, ).find( ([_attributeName, spec]) => !spec.via && !spec.expression && spec.notNull, @@ -959,7 +959,7 @@ export class PgResource< } exportAs("@dataplan/pg", PgResource, "PgResource"); -export interface AnyPgRegistryBuilder +export interface _AnyPgRegistryBuilder extends PgRegistryBuilder {} export interface EmptyRegistryBuilder extends PgRegistryBuilder {} @@ -971,20 +971,20 @@ export interface DefaultRegistryBuilder > {} export interface PgRegistryBuilder< - TCodecs extends AnyPgCodec, - TResourceOptions extends AnyPgResourceOptions, - TRelationConfigs extends AnyPgCodecRelationConfig, + TCodecs extends _AnyPgCodec, + TResourceOptions extends _AnyPgResourceOptions, + TRelationConfigs extends _AnyPgCodecRelationConfig, > { getRegistryConfig(): PgRegistryConfig< TCodecs, TResourceOptions, TRelationConfigs >; - addCodec( + addCodec( codec: TCodec, ): PgRegistryBuilder; - addResource( + addResource( resource: TResourceOption, ): PgRegistryBuilder< TCodecs | PgResourceOptionCodec, @@ -993,9 +993,9 @@ export interface PgRegistryBuilder< >; addRelation< - TCodec extends AnyPgCodec, + TCodec extends _AnyPgCodec, const TCodecRelationName extends string, - const TRemoteResource extends AnyPgResourceOptions, + const TRemoteResource extends _AnyPgResourceOptions, const TCodecRelation extends Omit< PgCodecRelationConfig, "localCodec" | "remoteResourceOptions" | "name" @@ -1022,8 +1022,8 @@ export interface PgRegistryBuilder< export function makeRegistry< TConfig extends PgRegistryConfig< any, - PgResourceOptions, - AnyPgCodecRelationConfig + PgResourceOptions, + _AnyPgCodecRelationConfig >, >( config: TConfig, @@ -1032,7 +1032,7 @@ export function makeRegistry< PgRegistryConfigResourceOptions, PgRegistryConfigRelationConfigs > { - const registry: AnyPgRegistry = { + const registry: _AnyPgRegistry = { pgCodecs: Object.create(null) as any, pgResources: Object.create(null) as any, pgRelations: Object.create(null) as any, @@ -1042,24 +1042,24 @@ export function makeRegistry< Object.defineProperties(registry.pgCodecs, { $exporter$args: { value: [registry] }, $exporter$factory: { - value: (registry: AnyPgRegistry) => registry.pgCodecs, + value: (registry: _AnyPgRegistry) => registry.pgCodecs, }, }); Object.defineProperties(registry.pgResources, { $exporter$args: { value: [registry] }, $exporter$factory: { - value: (registry: AnyPgRegistry) => registry.pgResources, + value: (registry: _AnyPgRegistry) => registry.pgResources, }, }); Object.defineProperties(registry.pgRelations, { $exporter$args: { value: [registry] }, $exporter$factory: { - value: (registry: AnyPgRegistry) => registry.pgRelations, + value: (registry: _AnyPgRegistry) => registry.pgRelations, }, }); let addCodecForbidden = false; - function addCodec(codec: TCodec): TCodec { + function addCodec(codec: TCodec): TCodec { if (addCodecForbidden) { throw new Error(`It's too late to call addCodec now`); } @@ -1085,7 +1085,7 @@ export function makeRegistry< if (codec.attributes) { const prevCols = codec.attributes as Record< string, - AnyPgCodecAttribute + _AnyPgCodecAttribute >; for (const col of Object.values(prevCols)) { addCodec(col.codec); @@ -1105,7 +1105,7 @@ export function makeRegistry< Object.defineProperties(codec, { $exporter$args: { value: [registry, codecName] }, $exporter$factory: { - value: (registry: AnyPgRegistry, codecName: string) => + value: (registry: _AnyPgRegistry, codecName: string) => registry.pgCodecs[codecName], }, }); @@ -1122,7 +1122,7 @@ export function makeRegistry< } for (const [resourceName, rawConfig] of Object.entries(config.pgResources)) { - const resourceConfig: AnyPgResourceOptions = { + const resourceConfig: _AnyPgResourceOptions = { ...rawConfig, codec: addCodec(rawConfig.codec), parameters: rawConfig.parameters @@ -1140,7 +1140,7 @@ export function makeRegistry< Object.defineProperties(resource, { $exporter$args: { value: [registry, resourceName] }, $exporter$factory: { - value: (registry: AnyPgRegistry, resourceName: string) => + value: (registry: _AnyPgRegistry, resourceName: string) => registry.pgResources[resourceName], }, }); @@ -1172,11 +1172,11 @@ export function makeRegistry< * remove the ones that already have resources, then we build resources for the * remainder. */ - const tableLikeCodecsWithoutTableLikeResources = new Set(); - const walkCodec = ( + const tableLikeCodecsWithoutTableLikeResources = new Set<_AnyPgCodec>(); + const walkCodec = ( codec: TCodec, isAccessibleViaAttribute = false, - seen = new Set(), + seen = new Set<_AnyPgCodec>(), ) => { if (seen.has(codec)) { return; @@ -1192,7 +1192,7 @@ export function makeRegistry< } if (codec.attributes) { for (const col of Object.values( - codec.attributes as Record, + codec.attributes as Record, )) { if (isAccessibleViaAttribute) { walkCodec(col.codec, isAccessibleViaAttribute, seen); @@ -1245,7 +1245,7 @@ export function makeRegistry< Object.defineProperties(resource, { $exporter$args: { value: [registry, resourceName] }, $exporter$factory: { - value: (registry: AnyPgRegistry, resourceName: string) => + value: (registry: _AnyPgRegistry, resourceName: string) => registry.pgResources[resourceName], }, }); @@ -1266,7 +1266,7 @@ export function makeRegistry< Object.defineProperties(builtRelations, { $exporter$args: { value: [registry, codecName] }, $exporter$factory: { - value: (registry: AnyPgRegistry, codecName: string) => + value: (registry: _AnyPgRegistry, codecName: string) => registry.pgRelations[codecName], }, }); @@ -1289,7 +1289,7 @@ export function makeRegistry< $exporter$args: { value: [registry, codecName, relationName] }, $exporter$factory: { value: ( - registry: AnyPgRegistry, + registry: _AnyPgRegistry, codecName: string, relationName: string, ) => registry.pgRelations[codecName][relationName], @@ -1309,9 +1309,9 @@ export function makeRegistry< exportAs("@dataplan/pg", makeRegistry, "makeRegistry"); function validateRelations< - TCodecs extends AnyPgCodec, - TResourceOptions extends AnyPgResourceOptions, - TRelationConfigs extends AnyPgCodecRelationConfig, + TCodecs extends _AnyPgCodec, + TResourceOptions extends _AnyPgResourceOptions, + TRelationConfigs extends _AnyPgCodecRelationConfig, >(registry: PgRegistry): void { // PERF: skip this if not isDev? @@ -1324,7 +1324,7 @@ function validateRelations< ); if (codec.attributes) { Object.entries( - codec.attributes as Record, + codec.attributes as Record, ).forEach(([attributeName, col]) => { const { via, identicalVia } = col; if (via != null) { @@ -1363,13 +1363,13 @@ function validateRelations< } export function makeRegistryBuilder(): EmptyRegistryBuilder { - const registryConfig: AnyPgRegistryConfig = { + const registryConfig: _AnyPgRegistryConfig = { pgCodecs: Object.create(null), pgResources: Object.create(null), pgRelations: Object.create(null), }; - const builder: AnyPgRegistryBuilder = { + const builder: _AnyPgRegistryBuilder = { getRegistryConfig() { return registryConfig; }, @@ -1398,7 +1398,7 @@ export function makeRegistryBuilder(): EmptyRegistryBuilder { } if (codec.attributes) { for (const col of Object.values( - codec.attributes as Record, + codec.attributes as Record, )) { this.addCodec(col.codec); } @@ -1464,12 +1464,12 @@ exportAs("@dataplan/pg", makeRegistryBuilder, "makeRegistryBuilder"); export function makePgResourceOptions< const TName extends string, - const TCodec extends AnyPgCodec, + const TCodec extends _AnyPgCodec, const TAttributes extends PgCodecAttributes, const TUniques extends PgResourceUnique< TAttributes[keyof TAttributes] > = never, - const TParameters extends AnyPgResourceParameter = never, + const TParameters extends _AnyPgResourceParameter = never, >( options: PgResourceOptions, ): PgResourceOptions { @@ -1481,9 +1481,9 @@ exportAs("@dataplan/pg", makePgResourceOptions, "makePgResourceOptions"); function printResourceFrom< TResource extends PgResourceOptions< any, - AnyPgCodec, + _AnyPgCodec, any, - AnyPgResourceParameter + _AnyPgResourceParameter >, >(resource: TResource): string { if (typeof resource.from === "function") { diff --git a/grafast/dataplan-pg/src/examples/exampleSchema.ts b/grafast/dataplan-pg/src/examples/exampleSchema.ts index 1187d18abd..f1c74cf436 100644 --- a/grafast/dataplan-pg/src/examples/exampleSchema.ts +++ b/grafast/dataplan-pg/src/examples/exampleSchema.ts @@ -80,7 +80,7 @@ import type { import type { NodePostgresPgClient, PgSubscriber } from "../adaptors/pg.js"; import { listOfCodec, PgCodecAttributeName } from "../codecs.js"; import { - AnyPgResource, + _AnyPgResource, makePgResourceOptions, makeRegistry, makeRegistryBuilder, @@ -111,7 +111,7 @@ import { TYPES, } from "../index.js"; import type { - AnyPgCodec, + _AnyPgCodec, GetPgResourceAttributeMap, GetPgResourceAttributes, GetPgResourceRelationConfigs, @@ -131,7 +131,7 @@ import { withPgClientTransaction, } from "../steps/withPgClient.js"; import { - AnyPgSelectSingleStep, + _AnyPgSelectSingleStep, PgSelectSingleStepResource, } from "../steps/pgSelectSingle"; @@ -213,7 +213,7 @@ export function makeExampleSchema( ) => { const col = < TOptions extends { - codec: AnyPgCodec; + codec: _AnyPgCodec; notNull?: boolean; expression?: PgCodecAttribute["expression"]; via?: PgCodecAttributeVia; @@ -1702,7 +1702,7 @@ export function makeExampleSchema( [__ListTransformStep, options], ); - type ResourceConnectionPlan = ConnectionStep< + type ResourceConnectionPlan = ConnectionStep< PgSelectSingleStep, PgSelectParsedCursorStep, PgSelectStep, @@ -1862,7 +1862,7 @@ export function makeExampleSchema( }); function attrField< - TMyResource extends AnyPgResource, + TMyResource extends _AnyPgResource, TAttrName extends PgCodecAttributeName< GetPgResourceAttributes >, @@ -1880,7 +1880,7 @@ export function makeExampleSchema( } function singleRelationField< - TMyResource extends AnyPgResource, + TMyResource extends _AnyPgResource, TRelationName extends PgCodecRelationConfigName< GetPgResourceRelationConfigs >, diff --git a/grafast/dataplan-pg/src/filters/pgBooleanFilter.ts b/grafast/dataplan-pg/src/filters/pgBooleanFilter.ts index 17579d4bf0..a51f0c9c32 100644 --- a/grafast/dataplan-pg/src/filters/pgBooleanFilter.ts +++ b/grafast/dataplan-pg/src/filters/pgBooleanFilter.ts @@ -2,7 +2,7 @@ import type { ExecutableStep } from "grafast"; import { ModifierStep } from "grafast"; import type { SQL } from "pg-sql2"; -import type { AnyPgCodec, PgCodec, PgConditionLikeStep } from "../interfaces.js"; +import type { _AnyPgCodec, PgCodec, PgConditionLikeStep } from "../interfaces.js"; export class PgBooleanFilterStep extends ModifierStep { static $$export = { @@ -22,7 +22,7 @@ export class PgBooleanFilterStep extends ModifierStep { this.alias = $classFilterPlan.alias; } - placeholder( + placeholder( $step: ExecutableStep, codec: TCodec, ): SQL { diff --git a/grafast/dataplan-pg/src/filters/pgClassFilter.ts b/grafast/dataplan-pg/src/filters/pgClassFilter.ts index 0e9eec8be7..7b066c8876 100644 --- a/grafast/dataplan-pg/src/filters/pgClassFilter.ts +++ b/grafast/dataplan-pg/src/filters/pgClassFilter.ts @@ -2,7 +2,7 @@ import type { ExecutableStep } from "grafast"; import { ModifierStep } from "grafast"; import type { SQL } from "pg-sql2"; -import type { AnyPgCodec, PgCodec } from "../interfaces.js"; +import type { _AnyPgCodec, PgCodec } from "../interfaces.js"; import type { PgConditionCapableParentStep, PgConditionStep, @@ -30,7 +30,7 @@ export class PgClassFilterStep< this.conditions.push(condition); } - placeholder( + placeholder( $step: ExecutableStep, codec: TCodec, ): SQL { diff --git a/grafast/dataplan-pg/src/filters/pgOrFilter.ts b/grafast/dataplan-pg/src/filters/pgOrFilter.ts index c7b6f04e71..575f4a01c3 100644 --- a/grafast/dataplan-pg/src/filters/pgOrFilter.ts +++ b/grafast/dataplan-pg/src/filters/pgOrFilter.ts @@ -3,7 +3,7 @@ import { ModifierStep } from "grafast"; import type { SQL } from "pg-sql2"; import { sql } from "pg-sql2"; -import type { AnyPgCodec, PgCodec, PgConditionLikeStep } from "../interfaces.js"; +import type { _AnyPgCodec, PgCodec, PgConditionLikeStep } from "../interfaces.js"; export class PgOrFilterStep extends ModifierStep { static $$export = { @@ -20,7 +20,7 @@ export class PgOrFilterStep extends ModifierStep { this.alias = $classFilterPlan.alias; } - placeholder($step: ExecutableStep, codec: TCodec): SQL { + placeholder($step: ExecutableStep, codec: TCodec): SQL { return this.$parent.placeholder($step, codec); } diff --git a/grafast/dataplan-pg/src/index.ts b/grafast/dataplan-pg/src/index.ts index 6674d7a18e..201cd85767 100644 --- a/grafast/dataplan-pg/src/index.ts +++ b/grafast/dataplan-pg/src/index.ts @@ -17,12 +17,12 @@ import { rangeOfCodec, recordCodec, TYPES, - AnyPgCodecAttribute, + _AnyPgCodecAttribute, DefaultPgCodecAttribute, DefaultPgCodecAttributeVia, - AnyPgCodecAttributeVia, - AnyPgCodecAttributeViaAttribute, - AnyPgCodecAttributeViaRelationName, + _AnyPgCodecAttributeVia, + _AnyPgCodecAttributeViaAttribute, + _AnyPgCodecAttributeViaRelationName, } from "./codecs.js"; import { PgBox, @@ -52,12 +52,12 @@ import { PgResourceParameter, PgResourceUnique, PgResourceUniqueExtensions, - AnyPgRegistryBuilder, - AnyPgResource, - AnyPgResourceOptions, - AnyPgResourceParameter, - AnyPgResourceUnique, - AnyScalarPgResource, + _AnyPgRegistryBuilder, + _AnyPgResource, + _AnyPgResourceOptions, + _AnyPgResourceParameter, + _AnyPgResourceUnique, + _AnyScalarPgResource, DefaultPgResourceOptions, DefaultPgResource, DefaultPgResourceParameter, @@ -65,7 +65,7 @@ import { DefaultRegistryBuilder, EmptyRegistryBuilder, DefaultPgFunctionResourceOptions, - AnyPgFunctionResourceOptions, + _AnyPgFunctionResourceOptions, } from "./datasource.js"; import { PgClient, @@ -125,12 +125,12 @@ import { PgTypedExecutableStep, PlanByUniques, TuplePlanMap, - AnyPgCodec, - AnyPgCodecAttributesRecord, - AnyPgCodecRelationConfig, - AnyPgRegistry, - AnyPgRegistryConfig, - AnyScalarPgCodec, + _AnyPgCodec, + _AnyPgCodecAttributesRecord, + _AnyPgCodecRelationConfig, + _AnyPgRegistry, + _AnyPgRegistryConfig, + _AnyScalarPgCodec, PgCodecAttributesRecord, PgCodecFromJavaScript, PgCodecFromPg, @@ -142,7 +142,7 @@ import { PgCodecRelationConfigRemoteResourceOptions, PgOrderAttributeSpec, PgOrderFragmentSpec, - AnyPgRangeItemCodec, + _AnyPgRangeItemCodec, PgRegistryCodecRelations, PgRegistryCodecs, PgRegistryConfig, @@ -153,7 +153,7 @@ import { PgRegistryResourceOptions, PgRegistryResources, DefaultPgRelation, - AnyPgRelation, + _AnyPgRelation, DefaultPgCodec, DefaultScalarPgCodec, DefaultPgCodecRelationConfig, @@ -181,7 +181,7 @@ import { pgDeleteSingle, PgDeleteSingleStep, DefaultPgDeleteSingleStep, - AnyPgDeleteSingleStep, + _AnyPgDeleteSingleStep, } from "./steps/pgDeleteSingle.js"; import { pgInsertSingle, PgInsertSingleStep } from "./steps/pgInsertSingle.js"; import { pgPageInfo, PgPageInfoStep } from "./steps/pgPageInfo.js"; @@ -203,15 +203,15 @@ import { PgSelectStep, sqlFromArgDigests, DefaultPgSelectStep, - AnyPgSelectStep, + _AnyPgSelectStep, } from "./steps/pgSelect.js"; import { pgSelectFromRecord, pgSelectSingleFromRecord, PgSelectSinglePlanOptions, PgSelectSingleStep, - AnyPgSelectSinglePlanOptions, - AnyPgSelectSingleStep, + _AnyPgSelectSinglePlanOptions, + _AnyPgSelectSingleStep, DefaultPgSelectSingleStep, } from "./steps/pgSelectSingle.js"; import { @@ -233,7 +233,7 @@ import { pgUpdateSingle, PgUpdateSingleStep, DefaultPgUpdateSingleStep, - AnyPgUpdateSingleStep, + _AnyPgUpdateSingleStep, } from "./steps/pgUpdateSingle.js"; import { pgValidateParsedCursor, @@ -250,40 +250,40 @@ import { assertPgClassSingleStep } from "./utils.js"; export { DefaultPgResourceOptions, - AnyPgSelectSinglePlanOptions, + _AnyPgSelectSinglePlanOptions, DefaultPgCodecAttribute, DefaultPgCodecAttributeVia, - AnyPgSelectSingleStep, + _AnyPgSelectSingleStep, DefaultPgResource, DefaultPgResourceParameter, DefaultPgSelectSingleStep, DefaultPgRelation, - AnyPgRelation, + _AnyPgRelation, DefaultPgCodecWithAttributes, DefaultPgResourceUnique, DefaultRegistryBuilder, EmptyRegistryBuilder, DefaultPgUpdateSingleStep, - AnyPgUpdateSingleStep, + _AnyPgUpdateSingleStep, DefaultPgDeleteSingleStep, - AnyPgDeleteSingleStep, + _AnyPgDeleteSingleStep, DefaultPgCodec, - AnyPgRegistryBuilder, - AnyPgResource, - AnyPgResourceOptions, - AnyPgResourceParameter, - AnyPgResourceUnique, - AnyScalarPgResource, - AnyPgCodecAttribute, - AnyPgCodecAttributeVia, - AnyPgCodecAttributeViaAttribute, - AnyPgCodecAttributeViaRelationName, - AnyPgCodec, - AnyPgCodecAttributesRecord, - AnyPgCodecRelationConfig, - AnyPgRegistry, - AnyPgRegistryConfig, - AnyScalarPgCodec, + _AnyPgRegistryBuilder, + _AnyPgResource, + _AnyPgResourceOptions, + _AnyPgResourceParameter, + _AnyPgResourceUnique, + _AnyScalarPgResource, + _AnyPgCodecAttribute, + _AnyPgCodecAttributeVia, + _AnyPgCodecAttributeViaAttribute, + _AnyPgCodecAttributeViaRelationName, + _AnyPgCodec, + _AnyPgCodecAttributesRecord, + _AnyPgCodecRelationConfig, + _AnyPgRegistry, + _AnyPgRegistryConfig, + _AnyScalarPgCodec, PgCodecAttributes, PgCodecAttributesRecord, PgCodecFromJavaScript, @@ -296,7 +296,7 @@ export { PgCodecRelationConfigRemoteResourceOptions, PgOrderAttributeSpec, PgOrderFragmentSpec, - AnyPgRangeItemCodec as PgRangeItemCodec, + _AnyPgRangeItemCodec as PgRangeItemCodec, PgRegistryCodecRelations, PgRegistryCodecs, PgRegistryConfig, @@ -312,7 +312,7 @@ export { domainOfCodec, enumCodec, DefaultPgFunctionResourceOptions, - AnyPgFunctionResourceOptions, + _AnyPgFunctionResourceOptions, getCodecByPgCatalogTypeName, getInnerCodec, GetPgRegistryCodecRelations, @@ -385,7 +385,7 @@ export { PgEnumValue, PgExecutor, PgExecutorContext, - AnyPgSelectStep, + _AnyPgSelectStep, DefaultPgSelectStep, PgExecutorContextPlans, PgExecutorInput, diff --git a/grafast/dataplan-pg/src/interfaces.ts b/grafast/dataplan-pg/src/interfaces.ts index 35ee9ce1d4..28c560060d 100644 --- a/grafast/dataplan-pg/src/interfaces.ts +++ b/grafast/dataplan-pg/src/interfaces.ts @@ -3,17 +3,17 @@ import type { SQL, SQLRawValue } from "pg-sql2"; import type { PgAdaptorOptions } from "./adaptors/pg.js"; import type { - AnyPgCodecAttribute, + _AnyPgCodecAttribute, DefaultPgCodecAttribute, PgCodecAttribute, PgCodecAttributeCodec, PgCodecAttributeName, } from "./codecs.js"; import type { - AnyPgResource, - AnyPgResourceOptions, - AnyPgResourceParameter, - AnyPgResourceUnique, + _AnyPgResource, + _AnyPgResourceOptions, + _AnyPgResourceParameter, + _AnyPgResourceUnique, DefaultPgResource, DefaultPgResourceOptions, PgCodecRefs, @@ -35,7 +35,7 @@ import type { PgUpdateSingleStep } from "./steps/pgUpdateSingle.js"; * A class-like source of information - could be from `SELECT`-ing a row, or * `INSERT...RETURNING` or similar. *ALWAYS* represents a single row (or null). */ -export type PgClassSingleStep = +export type PgClassSingleStep = | PgSelectSingleStep | PgInsertSingleStep @@ -132,7 +132,7 @@ export type PgCodecPolymorphism = export interface DefaultPgRangeItemCodec extends PgCodec {} -export interface AnyPgRangeItemCodec +export interface _AnyPgRangeItemCodec extends PgCodec {} export type PgCodecName = U extends PgCodec< @@ -197,11 +197,11 @@ export type PgCodecFromPg = PgDecode< >; export interface DefaultPgCodecAttributesRecord extends PgCodecAttributesRecord {} -export interface AnyPgCodecAttributesRecord +export interface _AnyPgCodecAttributesRecord extends PgCodecAttributesRecord {} export type PgCodecAttributesRecord< - TCodecAttributes extends AnyPgCodecAttribute, + TCodecAttributes extends _AnyPgCodecAttribute, > = { [TCodecAttribute in TCodecAttributes as PgCodecAttributeName]: TCodecAttribute; }; @@ -225,9 +225,9 @@ export interface DefaultScalarPgCodec DefaultPgCodec, DefaultPgCodec > {} -export interface AnyScalarPgCodec +export interface _AnyScalarPgCodec extends PgCodec {} -export interface AnyPgCodec +export interface _AnyPgCodec extends PgCodec {} /** @@ -237,12 +237,12 @@ export interface AnyPgCodec */ export interface PgCodec< TName extends string, - TCodecAttributes extends Record, + TCodecAttributes extends Record, TFromPostgres, TFromJavaScript, - TArrayItemCodec extends AnyPgCodec, - TDomainItemCodec extends AnyPgCodec, - TRangeItemCodec extends AnyPgCodec, + TArrayItemCodec extends _AnyPgCodec, + TDomainItemCodec extends _AnyPgCodec, + TRangeItemCodec extends _AnyPgCodec, > { /** * Unique name to identify this codec. @@ -377,7 +377,7 @@ export interface DefaultPgCodecWithAttributes never > {} export interface PgCodecWithAttributes< - TAttributes extends Record, + TAttributes extends Record, > extends PgCodec {} export interface PgCodecAnyScalar @@ -421,7 +421,7 @@ export interface PgEnumCodec< * A PgTypedExecutableStep has a 'pgCodec' property which means we don't need * to also state the pgCodec to use, this can be an added convenience. */ -export interface PgTypedExecutableStep +export interface PgTypedExecutableStep extends ExecutableStep { pgCodec: TCodec; } @@ -436,7 +436,7 @@ export type PgOrderFragmentSpec = { /** The expression we're ordering by. */ fragment: SQL; /** The codec of the expression that we're ordering by, this is useful when constructing a cursor for it. */ - codec: AnyPgCodec; + codec: _AnyPgCodec; attribute?: never; callback?: never; @@ -450,9 +450,9 @@ export type PgOrderAttributeSpec = { /** An optional expression to wrap this attribute with, and the type that expression returns */ callback?: ( attributeExpression: SQL, - attributeCodec: AnyPgCodec, + attributeCodec: _AnyPgCodec, nullable: boolean, - ) => [fragment: SQL, codec: AnyPgCodec, nullable?: boolean]; + ) => [fragment: SQL, codec: _AnyPgCodec, nullable?: boolean]; fragment?: never; codec?: never; @@ -474,7 +474,7 @@ export interface PgGroupSpec { } export type TuplePlanMap< - TAttributes extends AnyPgCodecAttribute, + TAttributes extends _AnyPgCodecAttribute, TTuple extends ReadonlyArray>, > = { [Index in keyof TTuple]: { @@ -506,13 +506,13 @@ export type TuplePlanMap< * to. */ export type PlanByUniques< - TAttributes extends AnyPgCodecAttribute, + TAttributes extends _AnyPgCodecAttribute, TUniqueAttributes extends PgResourceUnique, > = TuplePlanMap[number]; export type PgConditionLikeStep = (ModifierStep | ExecutableStep) & { alias: SQL; - placeholder($step: ExecutableStep, codec: AnyPgCodec): SQL; + placeholder($step: ExecutableStep, codec: _AnyPgCodec): SQL; where(condition: SQL): void; having(condition: SQL): void; }; @@ -630,7 +630,7 @@ export type PgCodecRelationExtensions = DataplanPg.PgCodecRelationExtensions; export interface PgCodecRelationBase< TName extends string = string, - TLocalCodec extends AnyPgCodec = AnyPgCodec, + TLocalCodec extends _AnyPgCodec = _AnyPgCodec, TRemoteAttributes extends string = string, > { name: TName; @@ -674,7 +674,7 @@ export interface DefaultPgCodecRelationConfig DefaultPgCodec, DefaultPgResourceOptions > {} -export interface AnyPgCodecRelationConfig +export interface _AnyPgCodecRelationConfig extends PgCodecRelationConfig {} export type PgCodecRelationConfigName = U extends PgCodecRelationConfig< @@ -694,8 +694,8 @@ export type PgCodecRelationConfigRemoteResourceOptions = : never; export interface PgCodecRelationConfig< TName extends string = string, - TLocalCodec extends AnyPgCodec = AnyPgCodec, - TRemoteResourceOptions extends AnyPgResourceOptions = AnyPgResourceOptions, + TLocalCodec extends _AnyPgCodec = _AnyPgCodec, + TRemoteResourceOptions extends _AnyPgResourceOptions = _AnyPgResourceOptions, > extends PgCodecRelationBase< TName, TLocalCodec, @@ -710,7 +710,7 @@ export interface DefaultPgRegistryConfig DefaultPgResourceOptions, DefaultPgCodecRelationConfig > {} -export interface AnyPgRegistryConfig extends PgRegistryConfig {} +export interface _AnyPgRegistryConfig extends PgRegistryConfig {} export type PgRegistryConfigCodecs = U extends PgRegistryConfig< infer TCodecs, any, @@ -734,9 +734,9 @@ export type PgRegistryConfigRelationConfigs = U extends PgRegistryConfig< : never; export interface PgRegistryConfig< - TCodecs extends AnyPgCodec, - TResourceOptions extends AnyPgResourceOptions, - TRelationConfigs extends AnyPgCodecRelationConfig, + TCodecs extends _AnyPgCodec, + TResourceOptions extends _AnyPgResourceOptions, + TRelationConfigs extends _AnyPgCodecRelationConfig, > { pgCodecs: PgRegistryCodecs; pgResources: PgRegistryResourceOptions; @@ -748,19 +748,19 @@ export type Expand = T extends unknown ? { [TKey in keyof T]: T[TKey] } : never; -export type PgRegistryCodecs = { +export type PgRegistryCodecs = { [TCodec in TCodecs as PgCodecName]: TCodec; }; export type PgRegistryResourceOptions< - TResourceOptions extends AnyPgResourceOptions, + TResourceOptions extends _AnyPgResourceOptions, > = { [TResourceOption in TResourceOptions as PgResourceOptionName]: TResourceOption; }; export type PgRegistryResources< - TResourceOptions extends AnyPgResourceOptions, - TRegistry extends AnyPgRegistry, + TResourceOptions extends _AnyPgResourceOptions, + TRegistry extends _AnyPgRegistry, > = { [TResourceOption in TResourceOptions as PgResourceOptionName]: PgResource< PgResourceOptionName, @@ -771,14 +771,14 @@ export type PgRegistryResources< >; }; export type PgRegistryRelationConfigs< - TRelationConfigs extends AnyPgCodecRelationConfig, + TRelationConfigs extends _AnyPgCodecRelationConfig, > = { [TRelationConfig in TRelationConfigs as PgCodecName< PgCodecRelationConfigLocalCodec >]: Record, TRelationConfig>; }; -export interface AnyPgRelation extends PgRelation {} +export interface _AnyPgRelation extends PgRelation {} export interface DefaultPgRelation extends PgRelation< string, @@ -789,9 +789,9 @@ export interface DefaultPgRelation export interface PgRelation< TName extends string, - TLocalCodec extends AnyPgCodec, - TRemoteResourceOptions extends AnyPgResourceOptions, - TRegistry extends AnyPgRegistry, + TLocalCodec extends _AnyPgCodec, + TRemoteResourceOptions extends _AnyPgResourceOptions, + TRegistry extends _AnyPgRegistry, > extends Omit< PgCodecRelationConfig, "remoteResourceOptions" @@ -806,8 +806,8 @@ export interface PgRelation< } export type PgRegistryCodecRelations< - TRelationConfigs extends AnyPgCodecRelationConfig, - TRegistry extends AnyPgRegistry, + TRelationConfigs extends _AnyPgCodecRelationConfig, + TRegistry extends _AnyPgRegistry, > = { [TRelationConfig in TRelationConfigs as PgCodecName< PgCodecRelationConfigLocalCodec @@ -826,7 +826,7 @@ export type PgRegistryCodecRelations< >; }; }; -export interface AnyPgRegistry extends PgRegistry {} +export interface _AnyPgRegistry extends PgRegistry {} export interface DefaultPgRegistry extends PgRegistry< DefaultPgCodec, @@ -836,9 +836,9 @@ export interface DefaultPgRegistry export interface EmptyPgRegistry extends PgRegistry {} export interface PgRegistry< - TCodecs extends AnyPgCodec, - TResourceOptions extends AnyPgResourceOptions, - TRelationConfigs extends AnyPgCodecRelationConfig, + TCodecs extends _AnyPgCodec, + TResourceOptions extends _AnyPgResourceOptions, + TRelationConfigs extends _AnyPgCodecRelationConfig, > { pgCodecs: PgRegistryCodecs; pgResources: PgRegistryResources; @@ -861,35 +861,35 @@ export type GetPgRegistryRelations = U extends PgRegistry< ? PgRegistryCodecRelations : never; -export type GetPgRegistrySources = +export type GetPgRegistrySources = TRegistry["pgResources"]; export type GetPgRegistryCodecRelationConfigs< - TRegistry extends AnyPgRegistry, - TCodec extends AnyPgCodec, + TRegistry extends _AnyPgRegistry, + TCodec extends _AnyPgCodec, > = TRegistry extends PgRegistry ? Extract } }> : never; export type GetPgRegistryCodecRelations< - TRegistry extends AnyPgRegistry, - TCodec extends AnyPgCodec, + TRegistry extends _AnyPgRegistry, + TCodec extends _AnyPgCodec, > = GetPgRegistryRelations[PgCodecName]; -export type GetPgResourceRegistry = +export type GetPgResourceRegistry = TResource["registry"]; -export type GetPgResourceCodec = +export type GetPgResourceCodec = TResource["codec"]; -export type GetPgResourceAttributes = +export type GetPgResourceAttributes = PgCodecAttributes>; -export type GetPgResourceAttributeMap = +export type GetPgResourceAttributeMap = PgCodecAttributeMap>; -export type GetPgResourceRelationConfigs = +export type GetPgResourceRelationConfigs = GetPgRegistryCodecRelationConfigs; -export type GetPgResourceRelations = +export type GetPgResourceRelations = GetPgRegistryCodecRelations< GetPgResourceRegistry, PgResourceCodec diff --git a/grafast/dataplan-pg/src/pgLocker.ts b/grafast/dataplan-pg/src/pgLocker.ts index d7e0f8a4c1..11e104952c 100644 --- a/grafast/dataplan-pg/src/pgLocker.ts +++ b/grafast/dataplan-pg/src/pgLocker.ts @@ -1,7 +1,7 @@ import { isDev } from "grafast"; -import { AnyPgSelectStep } from "./steps/pgSelect"; -import { AnyPgUnionAllStep } from "./steps/pgUnionAll"; +import { _AnyPgSelectStep } from "./steps/pgSelect"; +import { _AnyPgUnionAllStep } from "./steps/pgUnionAll"; export type PgLockableParameter = | "orderBy" @@ -9,13 +9,13 @@ export type PgLockableParameter = | "last" | "offset" | "groupBy"; -export type PgLockCallback = (step: TStep) => void; +export type PgLockCallback = (step: TStep) => void; -export type AnyPgStep = AnyPgSelectStep | AnyPgUnionAllStep; +export type _AnyPgStep = _AnyPgSelectStep | _AnyPgUnionAllStep; -export interface AnyPgLocker extends PgLocker {} +export interface _AnyPgLocker extends PgLocker {} -export class PgLocker { +export class PgLocker { /** * Determines if the PgSelectStep is "locked" - i.e. its * FROM,JOINs,WHERE,ORDER BY,LIMIT,OFFSET cannot be changed. Note this does diff --git a/grafast/dataplan-pg/src/steps/pgClassExpression.ts b/grafast/dataplan-pg/src/steps/pgClassExpression.ts index d522327b47..e464fea130 100644 --- a/grafast/dataplan-pg/src/steps/pgClassExpression.ts +++ b/grafast/dataplan-pg/src/steps/pgClassExpression.ts @@ -3,9 +3,9 @@ import { access, exportAs, UnbatchedExecutableStep } from "grafast"; import type { SQL } from "pg-sql2"; import sql from "pg-sql2"; -import type { AnyPgResource, PgResource } from "../datasource.js"; +import type { _AnyPgResource, PgResource } from "../datasource.js"; import type { - AnyPgCodec, + _AnyPgCodec, PgClassSingleStep, PgCodecAttributes, PgTypedExecutableStep, @@ -29,8 +29,8 @@ import { PgCodecAttributeCodec, PgCodecAttributeName } from "../codecs.js"; * of another layer of plan. */ export class PgClassExpressionStep< - TExpressionCodec extends AnyPgCodec, - TResource extends AnyPgResource, + TExpressionCodec extends _AnyPgCodec, + TResource extends _AnyPgResource, > extends UnbatchedExecutableStep implements PgTypedExecutableStep @@ -270,8 +270,8 @@ export class PgClassExpressionStep< * that will be selected. */ function pgClassExpression< - TExpressionCodec extends AnyPgCodec, - TResource extends AnyPgResource, + TExpressionCodec extends _AnyPgCodec, + TResource extends _AnyPgResource, >( table: PgClassSingleStep | PgUnionAllSingleStep, codec: TExpressionCodec, diff --git a/grafast/dataplan-pg/src/steps/pgCondition.ts b/grafast/dataplan-pg/src/steps/pgCondition.ts index 76a5d647bd..c0f9a032ad 100644 --- a/grafast/dataplan-pg/src/steps/pgCondition.ts +++ b/grafast/dataplan-pg/src/steps/pgCondition.ts @@ -4,7 +4,7 @@ import type { SQL } from "pg-sql2"; import { sql } from "pg-sql2"; import { TYPES } from "../index.js"; -import type { AnyPgCodec, PgCodec } from "../interfaces.js"; +import type { _AnyPgCodec, PgCodec } from "../interfaces.js"; export type PgWhereConditionSpec = | SQL @@ -22,7 +22,7 @@ export type PgConditionStepExtensions = DataplanPg.PgConditionStepExtensions; export interface PgConditionCapableParentStep extends BaseStep { alias: SQL; - placeholder( + placeholder( $step: ExecutableStep, codec: TCodec, ): SQL; @@ -138,7 +138,7 @@ export class PgConditionStep< this.havingConditions.push(condition); } - placeholder( + placeholder( $step: ExecutableStep, codec: TCodec, ): SQL { diff --git a/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts b/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts index 7a3b07b1bd..d983bf79bc 100644 --- a/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts @@ -9,7 +9,7 @@ import sql from "pg-sql2"; import { inspect } from "../inspect.js"; import type { - AnyPgCodec, + _AnyPgCodec, GetPgResourceAttributes, GetPgResourceCodec, GetPgResourceUniques, @@ -18,7 +18,7 @@ import type { import type { PgClassExpressionStep } from "./pgClassExpression.js"; import { pgClassExpression } from "./pgClassExpression.js"; import { - AnyPgResource, + _AnyPgResource, DefaultPgResource, PgResourceUnique, } from "../datasource.js"; @@ -40,14 +40,14 @@ interface PgDeletePlanFinalizeResults { queryValueDetailsBySymbol: QueryValueDetailsBySymbol; } -export interface AnyPgDeleteSingleStep extends PgDeleteSingleStep {} +export interface _AnyPgDeleteSingleStep extends PgDeleteSingleStep {} export interface DefaultPgDeleteSingleStep extends PgDeleteSingleStep {} /** * Deletes a row in the database, can return columns from the deleted row. */ export class PgDeleteSingleStep< - TResource extends AnyPgResource, + TResource extends _AnyPgResource, > extends ExecutableStep { static $$export = { moduleName: "@dataplan/pg", @@ -84,7 +84,7 @@ export class PgDeleteSingleStep< private getBys: Array<{ name: PgCodecAttributeName>; depId: number; - pgCodec: AnyPgCodec; + pgCodec: _AnyPgCodec; }> = []; /** @@ -393,7 +393,7 @@ export class PgDeleteSingleStep< /** * Delete a row in `resource` identified by the `getBy` unique condition. */ -export function pgDeleteSingle( +export function pgDeleteSingle( resource: TResource, getBy: PlanByUniques< GetPgResourceAttributes, diff --git a/grafast/dataplan-pg/src/steps/pgInsertSingle.ts b/grafast/dataplan-pg/src/steps/pgInsertSingle.ts index cefe882c71..c92ca68629 100644 --- a/grafast/dataplan-pg/src/steps/pgInsertSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgInsertSingle.ts @@ -10,21 +10,21 @@ import type { SQL, SQLRawValue } from "pg-sql2"; import sql from "pg-sql2"; import type { - AnyPgCodecAttribute, + _AnyPgCodecAttribute, PgCodecAttribute, PgCodecAttributeCodec, PgCodecAttributeName, } from "../codecs.js"; import { inspect } from "../inspect.js"; import type { - AnyPgCodec, + _AnyPgCodec, GetPgResourceAttributes, GetPgResourceCodec, PgTypedExecutableStep, } from "../interfaces.js"; import type { PgClassExpressionStep } from "./pgClassExpression.js"; import { pgClassExpression } from "./pgClassExpression.js"; -import { AnyPgResource } from "../datasource.js"; +import { _AnyPgResource } from "../datasource.js"; const EMPTY_MAP = new Map(); @@ -47,7 +47,7 @@ interface PgInsertSinglePlanFinalizeResults { /** * Inserts a row into resource with the given specified attribute values. */ -export class PgInsertSingleStep +export class PgInsertSingleStep extends ExecutableStep< unknown[] // tuple depending on what's selected > @@ -92,7 +92,7 @@ export class PgInsertSingleStep name: PgCodecAttributeName>; depId: number; // This isn't really needed, we can look it up in the codec, but it acts as a quick reference. - pgCodec: AnyPgCodec; + pgCodec: _AnyPgCodec; }> = []; /** @@ -214,7 +214,7 @@ export class PgInsertSingleStep if (!this.resource.codec.attributes) { throw new Error(`Cannot call .get() when there's no attributes.`); } - const resourceAttribute: AnyPgCodecAttribute = + const resourceAttribute: _AnyPgCodecAttribute = this.resource.codec.attributes[attr as string]; if (!resourceAttribute) { throw new Error( @@ -420,7 +420,7 @@ export class PgInsertSingleStep /** * Inserts a row into resource with the given specified attribute values. */ -export function pgInsertSingle( +export function pgInsertSingle( resource: TResource, attributes?: { [attribute in GetPgResourceAttributes as PgCodecAttributeName]?: diff --git a/grafast/dataplan-pg/src/steps/pgSelect.ts b/grafast/dataplan-pg/src/steps/pgSelect.ts index 66ec8bd017..fca557e01a 100644 --- a/grafast/dataplan-pg/src/steps/pgSelect.ts +++ b/grafast/dataplan-pg/src/steps/pgSelect.ts @@ -44,15 +44,15 @@ import sql, { $$symbolToIdentifier, arraysMatch } from "pg-sql2"; import { listOfCodec, TYPES } from "../codecs.js"; import type { - AnyPgResource, - AnyPgResourceUnique, - AnyScalarPgResource, + _AnyPgResource, + _AnyPgResourceUnique, + _AnyScalarPgResource, DefaultPgResource, PgResource, PgResourceCodec, } from "../datasource.js"; import type { - AnyPgCodec, + _AnyPgCodec, GetPgResourceAttributes, GetPgResourceCodec, GetPgResourceRelationConfigs, @@ -164,26 +164,26 @@ type PgSelectPlanJoin = */ type PgSelectPlaceholder = { dependencyIndex: number; - codec: AnyPgCodec; + codec: _AnyPgCodec; symbol: symbol; }; export type PgSelectIdentifierSpec = | { step: ExecutableStep; - codec: AnyPgCodec; + codec: _AnyPgCodec; matches: (alias: SQL) => SQL; } | { step: PgTypedExecutableStep; - codec?: AnyPgCodec; + codec?: _AnyPgCodec; matches: (alias: SQL) => SQL; }; export type PgSelectArgumentSpec = | { step: ExecutableStep; - pgCodec: AnyPgCodec; + pgCodec: _AnyPgCodec; name?: string; } | { @@ -199,7 +199,7 @@ export interface PgSelectArgumentDigest { interface QueryValue { dependencyIndex: number; - codec: AnyPgCodec; + codec: _AnyPgCodec; } function assertSensible(step: ExecutableStep): void { @@ -217,7 +217,7 @@ function assertSensible(step: ExecutableStep): void { export type PgSelectMode = "normal" | "aggregate" | "mutation"; -export interface PgSelectOptions { +export interface PgSelectOptions { /** * Tells us what we're dealing with - data type, columns, where to get it * from, what it's called, etc. Many of these details can be overridden @@ -266,7 +266,7 @@ export interface PgSelectOptions { } export interface DefaultPgSelectStep extends PgSelectStep {} -export interface AnyPgSelectStep extends PgSelectStep {} +export interface _AnyPgSelectStep extends PgSelectStep {} /** * This represents selecting from a class-like entity (table, view, etc); i.e. @@ -277,7 +277,7 @@ export interface AnyPgSelectStep extends PgSelectStep {} * don't allow `UNION`/`INTERSECT`/`EXCEPT`/`FOR UPDATE`/etc at this time, * purely because it hasn't been sufficiently considered. */ -export class PgSelectStep +export class PgSelectStep extends ExecutableStep< ReadonlyArray > @@ -773,11 +773,11 @@ export class PgSelectStep return this.isUnique; } - public placeholder($step: PgTypedExecutableStep): SQL; - public placeholder($step: ExecutableStep, codec: AnyPgCodec): SQL; + public placeholder($step: PgTypedExecutableStep<_AnyPgCodec>): SQL; + public placeholder($step: ExecutableStep, codec: _AnyPgCodec): SQL; public placeholder( - $step: ExecutableStep | PgTypedExecutableStep, - overrideCodec?: AnyPgCodec, + $step: ExecutableStep | PgTypedExecutableStep<_AnyPgCodec>, + overrideCodec?: _AnyPgCodec, ): SQL { if (this.locker.locked) { throw new Error(`${this}: cannot add placeholders once plan is locked`); @@ -2204,7 +2204,7 @@ ${lateralText};`; if ($p === this) { return true; } - const p = $p as PgSelectStep; + const p = $p as PgSelectStep<_AnyPgResource>; // If SELECT, FROM, JOIN, WHERE, ORDER, GROUP BY, HAVING, LIMIT, OFFSET // all match with one of our peers then we can replace ourself with one // of our peers. NOTE: we do _not_ merge SELECTs at this stage because @@ -2384,7 +2384,7 @@ ${lateralText};`; } } - private mergeSelectsWith>( + private mergeSelectsWith>( otherPlan: TOtherStep, ): { [desiredIndex: string]: string; @@ -2408,7 +2408,7 @@ ${lateralText};`; return actualKeyByDesiredKey; } - private mergePlaceholdersInto>( + private mergePlaceholdersInto>( otherPlan: TOtherStep, ): void { for (const placeholder of this.placeholders) { @@ -2487,7 +2487,7 @@ ${lateralText};`; !this.joins.some((j) => j.type !== "left") ) { // Inline ourself into our parent if we can. - let t: PgSelectStep | null | undefined = undefined; + let t: PgSelectStep<_AnyPgResource> | null | undefined = undefined; let p: ExecutableStep | undefined = undefined; for ( let dependencyIndex = 0, l = this.dependencies.length; @@ -2844,7 +2844,7 @@ ${lateralText};`; */ listItem( itemPlan: ExecutableStep, - ): TResource extends AnyScalarPgResource + ): TResource extends _AnyScalarPgResource ? PgClassExpressionStep, TResource> : PgSelectSingleStep; listItem( @@ -2898,7 +2898,7 @@ function joinMatches( * Apply a default order in case our default is not unique. */ function ensureOrderIsUnique< - TResource extends PgResource, + TResource extends PgResource, >(step: PgSelectStep) { // No need to order a unique record if (step.unique()) return; @@ -2918,7 +2918,7 @@ function ensureOrderIsUnique< } } -export function pgSelect( +export function pgSelect( options: PgSelectOptions, ): PgSelectStep { return new PgSelectStep(options); @@ -2928,7 +2928,7 @@ exportAs("@dataplan/pg", pgSelect, "pgSelect"); /** * Turns a list of records (e.g. from PgSelectSingleStep.record()) back into a PgSelect. */ -export function pgSelectFromRecords( +export function pgSelectFromRecords( resource: TResource, records: | PgClassExpressionStep< @@ -2965,7 +2965,7 @@ exportAs("@dataplan/pg", sqlFromArgDigests, "sqlFromArgDigests"); export function digestsFromArgumentSpecs( $placeholderable: { - placeholder(step: ExecutableStep, codec: AnyPgCodec): SQL; + placeholder(step: ExecutableStep, codec: _AnyPgCodec): SQL; }, specs: PgSelectArgumentSpec[], digests: PgSelectArgumentDigest[] = [], @@ -3003,8 +3003,8 @@ exportAs("@dataplan/pg", digestsFromArgumentSpecs, "digestsFromArgumentSpecs"); export function getFragmentAndCodecFromOrder( alias: SQL, order: PgOrderSpec, - codecOrCodecs: AnyPgCodec | AnyPgCodec[], -): [fragment: SQL, codec: AnyPgCodec, isNullable?: boolean] { + codecOrCodecs: _AnyPgCodec | _AnyPgCodec[], +): [fragment: SQL, codec: _AnyPgCodec, isNullable?: boolean] { if (order.attribute != null) { const colFrag = sql`${alias}.${sql.identifier(order.attribute)}`; const isArray = Array.isArray(codecOrCodecs); diff --git a/grafast/dataplan-pg/src/steps/pgSelectSingle.ts b/grafast/dataplan-pg/src/steps/pgSelectSingle.ts index 61c4912138..3ccff64df9 100644 --- a/grafast/dataplan-pg/src/steps/pgSelectSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgSelectSingle.ts @@ -10,21 +10,21 @@ import type { SQL } from "pg-sql2"; import sql from "pg-sql2"; import type { - AnyPgCodecAttribute, + _AnyPgCodecAttribute, ObjectFromPgCodecAttributes, PgCodecAttributeCodec, PgCodecAttributeName, } from "../codecs.js"; import { TYPES } from "../codecs.js"; import type { - AnyPgResource, + _AnyPgResource, DefaultPgResource, PgResource, PgResourceCodec, PgResourceRegistry, } from "../datasource.js"; import type { - AnyPgCodec, + _AnyPgCodec, GetPgRegistryCodecRelationConfigs, GetPgResourceAttributeMap, GetPgResourceAttributes, @@ -50,9 +50,9 @@ import { getFragmentAndCodecFromOrder, PgSelectStep } from "./pgSelect.js"; // const debugPlanVerbose = debugPlan.extend("verbose"); // const debugExecuteVerbose = debugExecute.extend("verbose"); -export interface AnyPgSelectSinglePlanOptions +export interface _AnyPgSelectSinglePlanOptions extends PgSelectSinglePlanOptions {} -export interface PgSelectSinglePlanOptions { +export interface PgSelectSinglePlanOptions { fromRelation?: readonly [ PgSelectSingleStep, PgCodecRelationConfigName< @@ -66,7 +66,7 @@ export interface PgSelectSinglePlanOptions { // Types that only take a few bytes so adding them to the selection would be // cheap to do. -const CHEAP_ATTRIBUTE_TYPES = new Set([ +const CHEAP_ATTRIBUTE_TYPES = new Set<_AnyPgCodec>([ TYPES.int2, TYPES.int, TYPES.bigint, @@ -79,7 +79,7 @@ const CHEAP_ATTRIBUTE_TYPES = new Set([ TYPES.timestamptz, ]); -export interface AnyPgSelectSingleStep extends PgSelectSingleStep {} +export interface _AnyPgSelectSingleStep extends PgSelectSingleStep {} export interface DefaultPgSelectSingleStep extends PgSelectSingleStep {} export type PgSelectSingleStepResource = U extends PgSelectSingleStep< infer TResource @@ -95,7 +95,7 @@ export type PgSelectSingleStepResource = U extends PgSelectSingleStep< * such as `.get` and `.cursor` which can receive specific properties by * telling the PgSelectStep to select the relevant expressions. */ -export class PgSelectSingleStep +export class PgSelectSingleStep extends UnbatchedExecutableStep< unknown[] /* What we return will be a tuple based on the values selected */ > @@ -234,7 +234,7 @@ export class PgSelectSingleStep Object.entries($fromPlan.resource.codec.attributes!) as Array< [ PgCodecAttributeName>, - AnyPgCodecAttribute, + _AnyPgCodecAttribute, ] > ).find(([name, col]) => { @@ -295,7 +295,7 @@ export class PgSelectSingleStep /** * Returns a plan representing the result of an expression. */ - public select( + public select( fragment: SQL, codec: TExpressionCodec, ): PgClassExpressionStep { @@ -314,11 +314,11 @@ export class PgSelectSingleStep } public placeholder($step: PgTypedExecutableStep): SQL; - public placeholder( + public placeholder( $step: ExecutableStep, codec: TCodec, ): SQL; - public placeholder( + public placeholder( $step: ExecutableStep | PgTypedExecutableStep, overrideCodec?: TCodec, ): SQL { @@ -503,7 +503,7 @@ export class PgSelectSingleStep } private nonNullAttribute: { - attribute: AnyPgCodecAttribute; + attribute: _AnyPgCodecAttribute; attr: string; } | null = null; private nullCheckAttributeIndex: number | null = null; @@ -645,7 +645,7 @@ export function pgSelectFromRecord< * Given a plan that represents a single record (via * PgSelectSingleStep.record()) this turns it back into a PgSelectSingleStep */ -export function pgSelectSingleFromRecord( +export function pgSelectSingleFromRecord( resource: TResource, $record: PgClassExpressionStep, TResource>, ): PgSelectSingleStep { diff --git a/grafast/dataplan-pg/src/steps/pgTempTable.ts b/grafast/dataplan-pg/src/steps/pgTempTable.ts index 35ee8d10c6..073f86ccba 100644 --- a/grafast/dataplan-pg/src/steps/pgTempTable.ts +++ b/grafast/dataplan-pg/src/steps/pgTempTable.ts @@ -3,13 +3,13 @@ import { BaseStep } from "grafast"; import type { SQL } from "pg-sql2"; import { sql } from "pg-sql2"; -import type { AnyPgResource } from "../datasource.js"; +import type { _AnyPgResource } from "../datasource.js"; import type { PgClassFilterStep } from "../filters/pgClassFilter.js"; -import type { AnyPgCodec } from "../interfaces.js"; +import type { _AnyPgCodec } from "../interfaces.js"; import type { PgConditionCapableParentStep } from "./pgCondition.js"; import { PgConditionStep } from "./pgCondition.js"; -export class PgTempTableStep +export class PgTempTableStep extends BaseStep implements PgConditionCapableParentStep { @@ -28,7 +28,7 @@ export class PgTempTableStep this.alias = sql.identifier(Symbol(`${resource.name}_filter`)); } - placeholder( + placeholder( $step: ExecutableStep, codec: TCodec, ): SQL { diff --git a/grafast/dataplan-pg/src/steps/pgUnionAll.ts b/grafast/dataplan-pg/src/steps/pgUnionAll.ts index aaf33300f7..53078e1491 100644 --- a/grafast/dataplan-pg/src/steps/pgUnionAll.ts +++ b/grafast/dataplan-pg/src/steps/pgUnionAll.ts @@ -32,8 +32,8 @@ import { $$symbolToIdentifier, sql } from "pg-sql2"; import { TYPES } from "../codecs.js"; import type { - AnyPgResource, - AnyPgResourceUnique, + _AnyPgResource, + _AnyPgResourceUnique, PgResource, PgResourceUnique, } from "../datasource.js"; @@ -44,7 +44,7 @@ import type { PgGroupSpec, } from "../index.js"; import type { - AnyPgCodec, + _AnyPgCodec, PgOrderFragmentSpec, PgOrderSpec, PgTypedExecutableStep, @@ -129,7 +129,7 @@ type PgUnionAllStepSelect = | { type: "expression"; expression: SQL; - codec: AnyPgCodec; + codec: _AnyPgCodec; } | { type: "outerExpression"; @@ -138,13 +138,13 @@ type PgUnionAllStepSelect = export type PgUnionAllStepConfigAttributes = { [attributeName in TAttributes]: { - codec: AnyPgCodec; + codec: _AnyPgCodec; }; }; export interface PgUnionAllStepMember { typeName: TTypeNames; - resource: PgResource; + resource: PgResource; match?: { [resourceAttributeName: string]: | { @@ -153,7 +153,7 @@ export interface PgUnionAllStepMember { } | { step: ExecutableStep; - codec: AnyPgCodec; + codec: _AnyPgCodec; }; }; path?: PgCodecRefPath; @@ -164,7 +164,7 @@ export interface PgUnionAllStepConfig< TTypeNames extends string, > { resourceByTypeName: { - [typeName in TTypeNames]: AnyPgResource; + [typeName in TTypeNames]: _AnyPgResource; }; attributes?: PgUnionAllStepConfigAttributes; members?: PgUnionAllStepMember[]; @@ -184,7 +184,7 @@ export interface PgUnionAllStepOrder { interface QueryValue { dependencyIndex: number; - codec: AnyPgCodec; + codec: _AnyPgCodec; alreadyEncoded: boolean; } @@ -200,7 +200,7 @@ interface QueryValue { */ type PgUnionAllPlaceholder = { dependencyIndex: number; - codec: AnyPgCodec; + codec: _AnyPgCodec; symbol: symbol; alreadyEncoded: boolean; }; @@ -302,7 +302,7 @@ export class PgUnionAllSingleStep /** * Returns a plan representing the result of an expression. */ - expression( + expression( expression: SQL, codec: TExpressionCodec, ): PgClassExpressionStep { @@ -319,7 +319,7 @@ export class PgUnionAllSingleStep return this.getClassStep().selectAndReturnIndex(fragment); } - public select( + public select( fragment: SQL, codec: TExpressionCodec, ): PgClassExpressionStep { @@ -345,7 +345,7 @@ export class PgUnionAllSingleStep interface MemberDigest { member: PgUnionAllStepMember; - finalResource: PgResource; + finalResource: PgResource; sqlSource: SQL; symbol: symbol; alias: SQL; @@ -375,7 +375,7 @@ function cloneDigests( return digests.map(cloneDigest); } -export interface AnyPgUnionAllStep extends PgUnionAllStep {} +export interface _AnyPgUnionAllStep extends PgUnionAllStep {} /** * Represents a `UNION ALL` statement, which can have multiple table-like @@ -388,7 +388,7 @@ export class PgUnionAllStep< extends ExecutableStep implements ConnectionCapableStep< - PgSelectSingleStep, + PgSelectSingleStep<_AnyPgResource>, PgSelectParsedCursorStep > { @@ -822,7 +822,7 @@ on (${sql.indent( return index; } - selectExpression( + selectExpression( expression: SQL, codec: TCodec, ): number { @@ -1015,12 +1015,12 @@ on (${sql.indent( public placeholder($step: PgTypedExecutableStep): SQL; public placeholder( $step: ExecutableStep, - codec: AnyPgCodec, + codec: _AnyPgCodec, alreadyEncoded?: boolean, ): SQL; public placeholder( $step: ExecutableStep | PgTypedExecutableStep, - overrideCodec?: AnyPgCodec, + overrideCodec?: _AnyPgCodec, alreadyEncoded = false, ): SQL { if (this.locker.locked) { @@ -1567,7 +1567,7 @@ and ${condition(i + 1)}`} const midSelects: SQL[] = []; const innerSelects = this.selects .map((s, selectIndex) => { - const r = ((): [SQL, AnyPgCodec] | null => { + const r = ((): [SQL, _AnyPgCodec] | null => { switch (s.type) { case "attribute": { if (!this.spec.attributes) { diff --git a/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts b/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts index 31f59f77b4..acc2d64ffc 100644 --- a/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts @@ -7,11 +7,11 @@ import { ExecutableStep, exportAs, isDev, SafeError, setter } from "grafast"; import type { SQL, SQLRawValue } from "pg-sql2"; import sql from "pg-sql2"; -import type { AnyPgCodecAttribute, PgCodecAttributeName } from "../codecs.js"; +import type { _AnyPgCodecAttribute, PgCodecAttributeName } from "../codecs.js"; import type { DefaultPgResource, PgResourceUnique } from "../index.js"; import { inspect } from "../inspect.js"; import type { - AnyPgCodec, + _AnyPgCodec, GetPgResourceAttributes, GetPgResourceCodec, GetPgResourceUniques, @@ -19,7 +19,7 @@ import type { } from "../interfaces.js"; import type { PgClassExpressionStep } from "./pgClassExpression.js"; import { pgClassExpression } from "./pgClassExpression.js"; -import { AnyPgResource, PgResourceUniques } from "../datasource.js"; +import { _AnyPgResource, PgResourceUniques } from "../datasource.js"; type QueryValueDetailsBySymbol = Map< symbol, @@ -36,14 +36,14 @@ interface PgUpdatePlanFinalizeResults { /** When we see the given symbol in the SQL values, what dependency do we replace it with? */ queryValueDetailsBySymbol: QueryValueDetailsBySymbol; } -export interface AnyPgUpdateSingleStep extends PgUpdateSingleStep {} +export interface _AnyPgUpdateSingleStep extends PgUpdateSingleStep {} export interface DefaultPgUpdateSingleStep extends PgUpdateSingleStep {} /** * Update a single row identified by the 'getBy' argument. */ export class PgUpdateSingleStep< - TResource extends AnyPgResource, + TResource extends _AnyPgResource, > extends ExecutableStep { static $$export = { moduleName: "@dataplan/pg", @@ -80,7 +80,7 @@ export class PgUpdateSingleStep< private getBys: Array<{ name: PgCodecAttributeName>; depId: number; - pgCodec: AnyPgCodec; + pgCodec: _AnyPgCodec; }> = []; /** @@ -89,7 +89,7 @@ export class PgUpdateSingleStep< private attributes: Array<{ name: PgCodecAttributeName>; depId: number; - pgCodec: AnyPgCodec; + pgCodec: _AnyPgCodec; }> = []; /** @@ -238,7 +238,7 @@ export class PgUpdateSingleStep< Extract, { name: TAttr }>["codec"], TResource > { - const resourceAttribute: AnyPgCodecAttribute = + const resourceAttribute: _AnyPgCodecAttribute = this.resource.codec.attributes![attr as string]; if (!resourceAttribute) { throw new Error( @@ -460,7 +460,7 @@ export class PgUpdateSingleStep< /** * Update a single row identified by the 'getBy' argument. */ -export function pgUpdateSingle( +export function pgUpdateSingle( resource: TResource, getBy: PlanByUniques< GetPgResourceAttributes, diff --git a/grafast/dataplan-pg/src/steps/toPg.ts b/grafast/dataplan-pg/src/steps/toPg.ts index b970306191..3aad9586f8 100644 --- a/grafast/dataplan-pg/src/steps/toPg.ts +++ b/grafast/dataplan-pg/src/steps/toPg.ts @@ -1,7 +1,7 @@ import type { ExecutableStep, ExecutionExtra } from "grafast"; import { UnbatchedExecutableStep } from "grafast"; -import type { AnyPgCodec, PgCodec } from "../interfaces.js"; +import type { _AnyPgCodec, PgCodec } from "../interfaces.js"; /** * Converts the given value to the representation suitable for feeding into the @@ -19,7 +19,7 @@ export class ToPgStep extends UnbatchedExecutableStep { isSyncAndSafe = true; constructor( $value: ExecutableStep, - private codec: AnyPgCodec, + private codec: _AnyPgCodec, ) { super(); this.addDependency($value); @@ -39,6 +39,6 @@ export class ToPgStep extends UnbatchedExecutableStep { * * @internal */ -export function toPg($value: ExecutableStep, codec: AnyPgCodec) { +export function toPg($value: ExecutableStep, codec: _AnyPgCodec) { return new ToPgStep($value, codec); } diff --git a/grafast/grafast/src/input.ts b/grafast/grafast/src/input.ts index db62222fe7..02fe4073be 100644 --- a/grafast/grafast/src/input.ts +++ b/grafast/grafast/src/input.ts @@ -11,7 +11,7 @@ import * as graphql from "graphql"; import type { OperationPlan } from "./engine/OperationPlan.js"; import { inspect } from "./inspect.js"; -import type { AnyInputStep } from "./interfaces.js"; +import type { _AnyInputStep } from "./interfaces.js"; import { __InputDynamicScalarStep } from "./steps/__inputDynamicScalar.js"; import type { __InputObjectStepWithDollars } from "./steps/__inputObject.js"; import { __InputObjectStep } from "./steps/__inputObject.js"; @@ -36,7 +36,7 @@ const { export function assertInputStep( itemPlan: unknown, -): asserts itemPlan is AnyInputStep { +): asserts itemPlan is _AnyInputStep { if (itemPlan instanceof __TrackedValueStep) return; if (itemPlan instanceof __InputListStep) return; if (itemPlan instanceof __InputStaticLeafStep) return; @@ -85,7 +85,7 @@ export function inputStep( inputType: GraphQLInputType, rawInputValue: ValueNode | undefined, defaultValue: ConstValueNode | undefined = undefined, -): AnyInputStep { +): _AnyInputStep { // This prevents recursion if (rawInputValue === undefined && defaultValue === undefined) { return constant(undefined); @@ -182,7 +182,7 @@ function inputVariablePlan( variableType: GraphQLInputType, inputType: GraphQLInputType, defaultValue: ConstValueNode | undefined = undefined, -): AnyInputStep { +): _AnyInputStep { if ( variableType instanceof GraphQLNonNull && !(inputType instanceof GraphQLNonNull) @@ -239,7 +239,7 @@ function inputVariablePlan( */ function inputNonNullPlan( _operationPlan: OperationPlan, - innerPlan: AnyInputStep, -): AnyInputStep { + innerPlan: _AnyInputStep, +): _AnyInputStep { return innerPlan; } diff --git a/grafast/grafast/src/interfaces.ts b/grafast/grafast/src/interfaces.ts index f60bc5f323..da8d0e3f72 100644 --- a/grafast/grafast/src/interfaces.ts +++ b/grafast/grafast/src/interfaces.ts @@ -411,17 +411,17 @@ export type FieldArgs = { /** Gets the value, evaluating the `inputPlan` at each field if appropriate */ get(path?: string | ReadonlyArray): ExecutableStep; /** Gets the value *without* calling any `inputPlan`s */ - getRaw(path?: string | ReadonlyArray): AnyInputStep; + getRaw(path?: string | ReadonlyArray): _AnyInputStep; /** This also works (without path) to apply each list entry against $target */ apply( $target: ExecutableStep | ModifierStep | (() => ModifierStep), path?: string | ReadonlyArray, ): void; -} & AnyInputStepDollars; +} & _AnyInputStepDollars; export type InputStep = GraphQLInputType extends TInputType - ? AnyInputStep + ? _AnyInputStep : TInputType extends GraphQLNonNull ? Exclude, ConstantStep> : TInputType extends GraphQLList @@ -435,9 +435,9 @@ export type InputStep = | __InputObjectStepWithDollars // .get(), .eval(), .evalHas(), .evalIs(null), .evalIsEmpty() | ConstantStep // .eval(), .evalIs(), .evalIsEmpty() : // TYPES: handle the other types - AnyInputStep; + _AnyInputStep; -export type AnyInputStep = +export type _AnyInputStep = | __TrackedValueStepWithDollars // .get(), .eval(), .evalIs(), .evalHas(), .at(), .evalLength(), .evalIsEmpty() | __InputListStep // .at(), .eval(), .evalLength(), .evalIs(null) | __InputStaticLeafStep // .eval(), .evalIs() @@ -445,7 +445,7 @@ export type AnyInputStep = | __InputObjectStepWithDollars // .get(), .eval(), .evalHas(), .evalIs(null), .evalIsEmpty() | ConstantStep; // .eval(), .evalIs(), .evalIsEmpty() -export type AnyInputStepWithDollars = AnyInputStep & AnyInputStepDollars; +export type _AnyInputStepWithDollars = _AnyInputStep & _AnyInputStepDollars; // TYPES: solve these lies /** @@ -453,8 +453,8 @@ export type AnyInputStepWithDollars = AnyInputStep & AnyInputStepDollars; * `{ $input: { $user: { $username } } }` without having to pass loads of * generics. */ -export type AnyInputStepDollars = { - [key in string as `$${key}`]: AnyInputStepWithDollars; +export type _AnyInputStepDollars = { + [key in string as `$${key}`]: _AnyInputStepWithDollars; }; export interface FieldInfo { @@ -809,7 +809,7 @@ export type GrafastInputFieldConfig< export type TrackedArguments< TArgs extends BaseGraphQLArguments = BaseGraphQLArguments, > = { - get(key: TKey): AnyInputStep; + get(key: TKey): _AnyInputStep; }; /** diff --git a/grafast/grafast/src/steps/node.ts b/grafast/grafast/src/steps/node.ts index 58cd999a6d..6ed938693a 100644 --- a/grafast/grafast/src/steps/node.ts +++ b/grafast/grafast/src/steps/node.ts @@ -3,7 +3,7 @@ import type { GraphQLObjectType } from "graphql"; import { isDev } from "../dev.js"; import { inspect } from "../inspect.js"; import type { - AnyInputStep, + _AnyInputStep, ExecutionExtra, NodeIdHandler, PolymorphicData, @@ -101,7 +101,7 @@ export function node( export function specFromNodeId( handler: NodeIdHandler, - $id: ExecutableStep | AnyInputStep, + $id: ExecutableStep | _AnyInputStep, ) { function decodeWithCodecAndHandler(raw: string) { try { diff --git a/grafast/website/grafast/step-library/standard-steps/node.md b/grafast/website/grafast/step-library/standard-steps/node.md index a2491c55dc..04e8e259c5 100644 --- a/grafast/website/grafast/step-library/standard-steps/node.md +++ b/grafast/website/grafast/step-library/standard-steps/node.md @@ -138,7 +138,7 @@ spec will resolve to null-ish values (or maybe raise an error). ```ts function specFromNodeId( handler: NodeIdHandler, - $id: ExecutableStep | AnyInputStep, + $id: ExecutableStep | _AnyInputStep, ): any; ``` diff --git a/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts index 8c685db40b..c349e5e0d0 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts @@ -1,5 +1,5 @@ import type { - AnyPgCodecAttributesRecord, + _AnyPgCodecAttributesRecord, DefaultPgCodec, DefaultPgCodecAttribute, PgCodec, @@ -310,7 +310,7 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { ); } - const attributes: AnyPgCodecAttributesRecord = Object.create(null); + const attributes: _AnyPgCodecAttributesRecord = Object.create(null); const allAttributes = await info.helpers.pgIntrospection.getAttributesForClass( serviceName, diff --git a/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts index 22d7ed6aca..ee23679ade 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts @@ -1,7 +1,7 @@ import type { - AnyPgCodec, - AnyPgCodecRelationConfig, - AnyPgResourceOptions, + _AnyPgCodec, + _AnyPgCodecRelationConfig, + _AnyPgResourceOptions, DefaultPgCodecRelationConfig, PgCodec, PgCodecExtensions, diff --git a/graphile-build/graphile-build-pg/src/plugins/PgTableNodePlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgTableNodePlugin.ts index 50f7a55c6d..bd44055d99 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgTableNodePlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgTableNodePlugin.ts @@ -1,7 +1,7 @@ import "graphile-config"; import type { - AnyPgSelectSingleStep, + _AnyPgSelectSingleStep, DefaultPgCodec, DefaultPgResource, } from "@dataplan/pg"; @@ -150,7 +150,7 @@ return function (list, constant) { ) : EXPORTABLE( (constant, identifier, list, pk) => - ($record: AnyPgSelectSingleStep) => { + ($record: _AnyPgSelectSingleStep) => { return list([ constant(identifier, false), ...pk.map((attribute) => $record.get(attribute)), diff --git a/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts index c82c2ec55b..0055e3c0c9 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts @@ -1,11 +1,11 @@ import type { - AnyPgCodecAttribute, - AnyPgResource, + _AnyPgCodecAttribute, + _AnyPgResource, DefaultPgResourceOptions, - AnyPgResourceUnique, + _AnyPgResourceUnique, PgCodec, PgResource, - AnyPgResourceOptions, + _AnyPgResourceOptions, DefaultPgCodec, } from "@dataplan/pg"; import { assertPgClassSingleStep, makePgResourceOptions } from "@dataplan/pg"; @@ -147,14 +147,14 @@ declare global { interface ScopeObjectFieldsField { pgFieldResource?: PgResource; pgFieldCodec?: DefaultPgCodec; - pgFieldAttribute?: AnyPgCodecAttribute; + pgFieldAttribute?: _AnyPgCodecAttribute; isPgFieldConnection?: boolean; isPgFieldSimpleCollection?: boolean; } interface ScopeInterfaceFieldsField { pgFieldResource?: PgResource; pgFieldCodec?: DefaultPgCodec; - pgFieldAttribute?: AnyPgCodecAttribute; + pgFieldAttribute?: _AnyPgCodecAttribute; isPgFieldConnection?: boolean; isPgFieldSimpleCollection?: boolean; } @@ -180,7 +180,7 @@ declare global { serviceName: string; pgClass: PgClass; pgConstraint: PgConstraint; - unique: AnyPgResourceUnique; + unique: _AnyPgResourceUnique; }): void | Promise; /** * Passed the PgResourceOptions before it's added to the PgRegistryBuilder. @@ -209,7 +209,7 @@ interface State { string, Map> >; - resourceByResourceOptions: Map>; + resourceByResourceOptions: Map>; detailsByResourceOptions: Map< DefaultPgResourceOptions, { serviceName: string; pgClass: PgClass } @@ -409,7 +409,7 @@ export const PgTablesPlugin: GraphileConfig.Plugin = { uniqueAttributeOnlyConstraints.map(async (pgConstraint) => { const { tags, description } = pgConstraint.getTagsAndDescription(); - const unique: AnyPgResourceUnique = { + const unique: _AnyPgResourceUnique = { isPrimary: pgConstraint.contype === "p", attributes: pgConstraint.conkey!.map( (k) => attributes.find((att) => att.attnum === k)!.attname, @@ -546,7 +546,7 @@ export const PgTablesPlugin: GraphileConfig.Plugin = { await info.helpers.pgIntrospection.getIntrospection(); const toProcess: Array<{ - resourceOptions: AnyPgResourceOptions; + resourceOptions: _AnyPgResourceOptions; pgClass: PgClass; serviceName: string; }> = []; diff --git a/utils/graphile-export/src/exportSchema.ts b/utils/graphile-export/src/exportSchema.ts index c6800f9e5f..920bf8ae0d 100644 --- a/utils/graphile-export/src/exportSchema.ts +++ b/utils/graphile-export/src/exportSchema.ts @@ -169,11 +169,11 @@ function isImportable( return ( (typeof thing === "object" || typeof thing === "function") && thing !== null && - "$$export" in (thing as object | AnyFunction) + "$$export" in (thing as object | _AnyFunction) ); } -type AnyFunction = { +type _AnyFunction = { (...args: any[]): any; displayName?: string; }; @@ -1003,7 +1003,7 @@ function _convertToAST( }), ); } else if (typeof thing === "function") { - return func(file, thing as AnyFunction, locationHint, nameHint); + return func(file, thing as _AnyFunction, locationHint, nameHint); } else if (isSchema(thing)) { throw new Error( "Attempted to export GraphQLSchema directly from `_convertToAST`; this is currently unsupported.", @@ -1208,7 +1208,7 @@ function iife(statements: t.Statement[]): t.Expression { function func( file: CodegenFile, - fn: AnyFunction, + fn: _AnyFunction, locationHint: string, nameHint: string, ): t.Expression { @@ -1300,7 +1300,7 @@ function factoryAst( } function funcToAst( - fn: AnyFunction, + fn: _AnyFunction, locationHint: string, _nameHint: string, ): t.FunctionExpression | t.ArrowFunctionExpression { From 83f9b170ea91ca5149b7ee86bca239c961d31290 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Wed, 22 Nov 2023 14:26:39 +0000 Subject: [PATCH 4/8] Mark all _Any types as internal --- grafast/dataplan-pg/src/codecs.ts | 4 ++++ grafast/dataplan-pg/src/datasource.ts | 7 +++++++ grafast/dataplan-pg/src/interfaces.ts | 8 ++++++++ grafast/dataplan-pg/src/pgLocker.ts | 2 ++ grafast/dataplan-pg/src/steps/pgDeleteSingle.ts | 1 + grafast/dataplan-pg/src/steps/pgSelect.ts | 1 + grafast/dataplan-pg/src/steps/pgSelectSingle.ts | 2 ++ grafast/dataplan-pg/src/steps/pgUnionAll.ts | 1 + grafast/dataplan-pg/src/steps/pgUpdateSingle.ts | 1 + grafast/grafast/src/interfaces.ts | 3 +++ utils/graphile-export/src/exportSchema.ts | 1 + 11 files changed, 31 insertions(+) diff --git a/grafast/dataplan-pg/src/codecs.ts b/grafast/dataplan-pg/src/codecs.ts index 9d562b5104..c41eb94ac0 100644 --- a/grafast/dataplan-pg/src/codecs.ts +++ b/grafast/dataplan-pg/src/codecs.ts @@ -63,12 +63,15 @@ export type PgCodecAttributeViaExplicit< relation: TRelationName; attribute: TAttribute; }; +/** @internal */ export interface _AnyPgCodecAttributeVia extends PgCodecAttributeVia {} export type DefaultPgCodecAttributeVia = PgCodecAttributeVia; +/** @internal */ export type _AnyPgCodecAttributeViaRelationName = U extends PgCodecAttributeVia ? TRelationName : never; +/** @internal */ export type _AnyPgCodecAttributeViaAttribute = U extends PgCodecAttributeVia< any, infer TAttributeName @@ -101,6 +104,7 @@ export type PgCodecAttributeCodec = U extends PgCodecAttribute< export interface DefaultPgCodecAttribute extends PgCodecAttribute {} +/** @internal */ export interface _AnyPgCodecAttribute extends PgCodecAttribute {} export interface PgCodecAttribute< diff --git a/grafast/dataplan-pg/src/datasource.ts b/grafast/dataplan-pg/src/datasource.ts index e1973726cd..f7d4983717 100644 --- a/grafast/dataplan-pg/src/datasource.ts +++ b/grafast/dataplan-pg/src/datasource.ts @@ -94,6 +94,7 @@ export type PgResourceExtensions = DataplanPg.PgResourceExtensions; export type PgResourceParameterExtensions = DataplanPg.PgResourceParameterExtensions; +/** @internal */ export interface _AnyPgResourceParameter extends PgResourceParameter {} export interface DefaultPgResourceParameter extends PgResourceParameter {} @@ -127,6 +128,7 @@ export interface PgResourceParameter< extensions?: PgResourceParameterExtensions; } +/** @internal */ export interface _AnyPgResourceUnique extends PgResourceUnique {} export interface DefaultPgResourceUnique extends PgResourceUnique {} @@ -177,6 +179,7 @@ export interface DefaultPgResourceOptions< DefaultPgResourceParameter > {} +/** @internal */ export interface _AnyPgResourceOptions extends PgResourceOptions {} @@ -269,6 +272,7 @@ export interface PgResourceOptions< isVirtual?: boolean; } +/** @internal */ export interface _AnyPgFunctionResourceOptions extends PgFunctionResourceOptions {} export interface DefaultPgFunctionResourceOptions @@ -353,7 +357,9 @@ export interface DefaultPgResource DefaultPgResourceParameter, any > {} +/** @internal */ export interface _AnyPgResource extends PgResource {} +/** @internal */ export interface _AnyScalarPgResource extends PgResource {} @@ -959,6 +965,7 @@ export class PgResource< } exportAs("@dataplan/pg", PgResource, "PgResource"); +/** @internal */ export interface _AnyPgRegistryBuilder extends PgRegistryBuilder {} export interface EmptyRegistryBuilder diff --git a/grafast/dataplan-pg/src/interfaces.ts b/grafast/dataplan-pg/src/interfaces.ts index 28c560060d..8d4b2b73cb 100644 --- a/grafast/dataplan-pg/src/interfaces.ts +++ b/grafast/dataplan-pg/src/interfaces.ts @@ -132,6 +132,7 @@ export type PgCodecPolymorphism = export interface DefaultPgRangeItemCodec extends PgCodec {} +/** @internal */ export interface _AnyPgRangeItemCodec extends PgCodec {} @@ -197,6 +198,7 @@ export type PgCodecFromPg = PgDecode< >; export interface DefaultPgCodecAttributesRecord extends PgCodecAttributesRecord {} +/** @internal */ export interface _AnyPgCodecAttributesRecord extends PgCodecAttributesRecord {} @@ -225,8 +227,10 @@ export interface DefaultScalarPgCodec DefaultPgCodec, DefaultPgCodec > {} +/** @internal */ export interface _AnyScalarPgCodec extends PgCodec {} +/** @internal */ export interface _AnyPgCodec extends PgCodec {} @@ -674,6 +678,7 @@ export interface DefaultPgCodecRelationConfig DefaultPgCodec, DefaultPgResourceOptions > {} +/** @internal */ export interface _AnyPgCodecRelationConfig extends PgCodecRelationConfig {} @@ -710,6 +715,7 @@ export interface DefaultPgRegistryConfig DefaultPgResourceOptions, DefaultPgCodecRelationConfig > {} +/** @internal */ export interface _AnyPgRegistryConfig extends PgRegistryConfig {} export type PgRegistryConfigCodecs = U extends PgRegistryConfig< infer TCodecs, @@ -778,6 +784,7 @@ export type PgRegistryRelationConfigs< >]: Record, TRelationConfig>; }; +/** @internal */ export interface _AnyPgRelation extends PgRelation {} export interface DefaultPgRelation extends PgRelation< @@ -826,6 +833,7 @@ export type PgRegistryCodecRelations< >; }; }; +/** @internal */ export interface _AnyPgRegistry extends PgRegistry {} export interface DefaultPgRegistry extends PgRegistry< diff --git a/grafast/dataplan-pg/src/pgLocker.ts b/grafast/dataplan-pg/src/pgLocker.ts index 11e104952c..53c7aac973 100644 --- a/grafast/dataplan-pg/src/pgLocker.ts +++ b/grafast/dataplan-pg/src/pgLocker.ts @@ -11,8 +11,10 @@ export type PgLockableParameter = | "groupBy"; export type PgLockCallback = (step: TStep) => void; +/** @internal */ export type _AnyPgStep = _AnyPgSelectStep | _AnyPgUnionAllStep; +/** @internal */ export interface _AnyPgLocker extends PgLocker {} export class PgLocker { diff --git a/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts b/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts index d983bf79bc..bdb315626c 100644 --- a/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts @@ -40,6 +40,7 @@ interface PgDeletePlanFinalizeResults { queryValueDetailsBySymbol: QueryValueDetailsBySymbol; } +/** @internal */ export interface _AnyPgDeleteSingleStep extends PgDeleteSingleStep {} export interface DefaultPgDeleteSingleStep extends PgDeleteSingleStep {} diff --git a/grafast/dataplan-pg/src/steps/pgSelect.ts b/grafast/dataplan-pg/src/steps/pgSelect.ts index fca557e01a..a63991eb61 100644 --- a/grafast/dataplan-pg/src/steps/pgSelect.ts +++ b/grafast/dataplan-pg/src/steps/pgSelect.ts @@ -266,6 +266,7 @@ export interface PgSelectOptions { } export interface DefaultPgSelectStep extends PgSelectStep {} +/** @internal */ export interface _AnyPgSelectStep extends PgSelectStep {} /** diff --git a/grafast/dataplan-pg/src/steps/pgSelectSingle.ts b/grafast/dataplan-pg/src/steps/pgSelectSingle.ts index 3ccff64df9..f4b7f0c248 100644 --- a/grafast/dataplan-pg/src/steps/pgSelectSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgSelectSingle.ts @@ -50,6 +50,7 @@ import { getFragmentAndCodecFromOrder, PgSelectStep } from "./pgSelect.js"; // const debugPlanVerbose = debugPlan.extend("verbose"); // const debugExecuteVerbose = debugExecute.extend("verbose"); +/** @internal */ export interface _AnyPgSelectSinglePlanOptions extends PgSelectSinglePlanOptions {} export interface PgSelectSinglePlanOptions { @@ -79,6 +80,7 @@ const CHEAP_ATTRIBUTE_TYPES = new Set<_AnyPgCodec>([ TYPES.timestamptz, ]); +/** @internal */ export interface _AnyPgSelectSingleStep extends PgSelectSingleStep {} export interface DefaultPgSelectSingleStep extends PgSelectSingleStep {} export type PgSelectSingleStepResource = U extends PgSelectSingleStep< diff --git a/grafast/dataplan-pg/src/steps/pgUnionAll.ts b/grafast/dataplan-pg/src/steps/pgUnionAll.ts index 53078e1491..43b981e449 100644 --- a/grafast/dataplan-pg/src/steps/pgUnionAll.ts +++ b/grafast/dataplan-pg/src/steps/pgUnionAll.ts @@ -375,6 +375,7 @@ function cloneDigests( return digests.map(cloneDigest); } +/** @internal */ export interface _AnyPgUnionAllStep extends PgUnionAllStep {} /** diff --git a/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts b/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts index acc2d64ffc..efc492b17e 100644 --- a/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts @@ -36,6 +36,7 @@ interface PgUpdatePlanFinalizeResults { /** When we see the given symbol in the SQL values, what dependency do we replace it with? */ queryValueDetailsBySymbol: QueryValueDetailsBySymbol; } +/** @internal */ export interface _AnyPgUpdateSingleStep extends PgUpdateSingleStep {} export interface DefaultPgUpdateSingleStep extends PgUpdateSingleStep {} diff --git a/grafast/grafast/src/interfaces.ts b/grafast/grafast/src/interfaces.ts index da8d0e3f72..8888efdcba 100644 --- a/grafast/grafast/src/interfaces.ts +++ b/grafast/grafast/src/interfaces.ts @@ -437,6 +437,7 @@ export type InputStep = : // TYPES: handle the other types _AnyInputStep; +/** @internal */ export type _AnyInputStep = | __TrackedValueStepWithDollars // .get(), .eval(), .evalIs(), .evalHas(), .at(), .evalLength(), .evalIsEmpty() | __InputListStep // .at(), .eval(), .evalLength(), .evalIs(null) @@ -445,6 +446,7 @@ export type _AnyInputStep = | __InputObjectStepWithDollars // .get(), .eval(), .evalHas(), .evalIs(null), .evalIsEmpty() | ConstantStep; // .eval(), .evalIs(), .evalIsEmpty() +/** @internal */ export type _AnyInputStepWithDollars = _AnyInputStep & _AnyInputStepDollars; // TYPES: solve these lies @@ -453,6 +455,7 @@ export type _AnyInputStepWithDollars = _AnyInputStep & _AnyInputStepDollars; * `{ $input: { $user: { $username } } }` without having to pass loads of * generics. */ +/** @internal */ export type _AnyInputStepDollars = { [key in string as `$${key}`]: _AnyInputStepWithDollars; }; diff --git a/utils/graphile-export/src/exportSchema.ts b/utils/graphile-export/src/exportSchema.ts index 920bf8ae0d..bc3e78949f 100644 --- a/utils/graphile-export/src/exportSchema.ts +++ b/utils/graphile-export/src/exportSchema.ts @@ -173,6 +173,7 @@ function isImportable( ); } +/** @internal */ type _AnyFunction = { (...args: any[]): any; displayName?: string; From b6a85da60296aa9d68555cf75e9638631da18762 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Wed, 22 Nov 2023 14:43:11 +0000 Subject: [PATCH 5/8] Default->Generic --- grafast/dataplan-pg/src/codecs.ts | 8 +- grafast/dataplan-pg/src/datasource.ts | 44 +++++------ grafast/dataplan-pg/src/index.ts | 76 +++++++++---------- grafast/dataplan-pg/src/interfaces.ts | 64 ++++++++-------- .../dataplan-pg/src/steps/pgDeleteSingle.ts | 6 +- grafast/dataplan-pg/src/steps/pgSelect.ts | 4 +- .../dataplan-pg/src/steps/pgSelectSingle.ts | 4 +- .../dataplan-pg/src/steps/pgUpdateSingle.ts | 6 +- .../graphile-build-pg/src/inputUtils.ts | 14 ++-- .../graphile-build-pg/src/interfaces.ts | 4 +- .../src/plugins/PgAttributesPlugin.ts | 26 +++---- .../src/plugins/PgBasicsPlugin.ts | 42 +++++----- .../src/plugins/PgCodecsPlugin.ts | 44 +++++------ .../src/plugins/PgConditionArgumentPlugin.ts | 10 +-- .../plugins/PgConditionCustomFieldsPlugin.ts | 6 +- .../plugins/PgConnectionArgOrderByPlugin.ts | 4 +- .../src/plugins/PgCustomTypeFieldPlugin.ts | 50 ++++++------ .../PgFirstLastBeforeAfterArgsPlugin.ts | 24 +++--- .../PgInterfaceModeUnionAllRowsPlugin.ts | 16 ++-- .../src/plugins/PgJWTPlugin.ts | 8 +- .../plugins/PgMutationPayloadEdgePlugin.ts | 4 +- .../plugins/PgMutationUpdateDeletePlugin.ts | 72 +++++++++--------- .../src/plugins/PgNodeIdAttributesPlugin.ts | 8 +- .../src/plugins/PgOrderAllAttributesPlugin.ts | 6 +- .../src/plugins/PgOrderByPrimaryKeyPlugin.ts | 6 +- .../src/plugins/PgOrderCustomFieldsPlugin.ts | 8 +- .../src/plugins/PgPolymorphismPlugin.ts | 16 ++-- .../src/plugins/PgProceduresPlugin.ts | 32 ++++---- .../src/plugins/PgRefsPlugin.ts | 4 +- .../src/plugins/PgRegistryPlugin.ts | 6 +- .../src/plugins/PgRelationsPlugin.ts | 48 ++++++------ .../src/plugins/PgRowByUniquePlugin.ts | 12 +-- .../src/plugins/PgTableNodePlugin.ts | 6 +- .../src/plugins/PgTablesPlugin.ts | 34 ++++----- .../src/makeAddPgTableConditionPlugin.ts | 6 +- .../src/makeAddPgTableOrderByPlugin.ts | 8 +- .../src/plugins/PgV4BehaviorPlugin.ts | 4 +- 37 files changed, 370 insertions(+), 370 deletions(-) diff --git a/grafast/dataplan-pg/src/codecs.ts b/grafast/dataplan-pg/src/codecs.ts index c41eb94ac0..dd68511079 100644 --- a/grafast/dataplan-pg/src/codecs.ts +++ b/grafast/dataplan-pg/src/codecs.ts @@ -39,7 +39,7 @@ import type { PgExecutor } from "./executor.js"; import { inspect } from "./inspect.js"; import type { _AnyPgCodec, - DefaultPgCodec, + GenericPgCodec, PgCodec, PgCodecAttributeMap, PgCodecAttributes, @@ -65,7 +65,7 @@ export type PgCodecAttributeViaExplicit< }; /** @internal */ export interface _AnyPgCodecAttributeVia extends PgCodecAttributeVia {} -export type DefaultPgCodecAttributeVia = PgCodecAttributeVia; +export type GenericPgCodecAttributeVia = PgCodecAttributeVia; /** @internal */ export type _AnyPgCodecAttributeViaRelationName = U extends PgCodecAttributeVia @@ -102,8 +102,8 @@ export type PgCodecAttributeCodec = U extends PgCodecAttribute< ? TCodec : never; -export interface DefaultPgCodecAttribute - extends PgCodecAttribute {} +export interface GenericPgCodecAttribute + extends PgCodecAttribute {} /** @internal */ export interface _AnyPgCodecAttribute extends PgCodecAttribute {} diff --git a/grafast/dataplan-pg/src/datasource.ts b/grafast/dataplan-pg/src/datasource.ts index f7d4983717..9697f96621 100644 --- a/grafast/dataplan-pg/src/datasource.ts +++ b/grafast/dataplan-pg/src/datasource.ts @@ -12,7 +12,7 @@ import sql from "pg-sql2"; import type { _AnyPgCodecAttribute, - DefaultPgCodecAttribute, + GenericPgCodecAttribute, PgCodecAttributeName, PgCodecAttributeVia, PgCodecAttributeViaExplicit, @@ -49,8 +49,8 @@ import type { PgRegistryConfigRelationConfigs, PgRegistryConfigResourceOptions, Expand, - DefaultPgCodec, - DefaultPgCodecRelationConfig, + GenericPgCodec, + GenericPgCodecRelationConfig, } from "./interfaces.js"; import type { PgClassExpressionStep } from "./steps/pgClassExpression.js"; import type { @@ -96,8 +96,8 @@ export type PgResourceParameterExtensions = /** @internal */ export interface _AnyPgResourceParameter extends PgResourceParameter {} -export interface DefaultPgResourceParameter - extends PgResourceParameter {} +export interface GenericPgResourceParameter + extends PgResourceParameter {} /** * If this is a functional (rather than static) resource, this describes one of @@ -130,8 +130,8 @@ export interface PgResourceParameter< /** @internal */ export interface _AnyPgResourceUnique extends PgResourceUnique {} -export interface DefaultPgResourceUnique - extends PgResourceUnique {} +export interface GenericPgResourceUnique + extends PgResourceUnique {} /** * Description of a unique constraint on a PgResource. */ @@ -170,13 +170,13 @@ export interface PgCodecRef { export interface PgCodecRefs { [refName: string]: PgCodecRef; } -export interface DefaultPgResourceOptions< - TCodec extends DefaultPgCodec = DefaultPgCodec, +export interface GenericPgResourceOptions< + TCodec extends GenericPgCodec = GenericPgCodec, > extends PgResourceOptions< string, TCodec, - DefaultPgResourceUnique, - DefaultPgResourceParameter + GenericPgResourceUnique, + GenericPgResourceParameter > {} /** @internal */ @@ -275,12 +275,12 @@ export interface PgResourceOptions< /** @internal */ export interface _AnyPgFunctionResourceOptions extends PgFunctionResourceOptions {} -export interface DefaultPgFunctionResourceOptions +export interface GenericPgFunctionResourceOptions extends PgFunctionResourceOptions< string, - DefaultPgCodec, - DefaultPgResourceUnique, - DefaultPgResourceParameter + GenericPgCodec, + GenericPgResourceUnique, + GenericPgResourceParameter > {} export interface PgFunctionResourceOptions< @@ -349,12 +349,12 @@ export type PgResourceRegistry = U extends PgResource< > ? TRegistry : never; -export interface DefaultPgResource +export interface GenericPgResource extends PgResource< string, - DefaultPgCodec, - DefaultPgResourceUnique, - DefaultPgResourceParameter, + GenericPgCodec, + GenericPgResourceUnique, + GenericPgResourceParameter, any > {} /** @internal */ @@ -972,9 +972,9 @@ export interface EmptyRegistryBuilder extends PgRegistryBuilder {} export interface DefaultRegistryBuilder extends PgRegistryBuilder< - DefaultPgCodec, - DefaultPgResourceOptions, - DefaultPgCodecRelationConfig + GenericPgCodec, + GenericPgResourceOptions, + GenericPgCodecRelationConfig > {} export interface PgRegistryBuilder< diff --git a/grafast/dataplan-pg/src/index.ts b/grafast/dataplan-pg/src/index.ts index 201cd85767..2166a4d15e 100644 --- a/grafast/dataplan-pg/src/index.ts +++ b/grafast/dataplan-pg/src/index.ts @@ -18,8 +18,8 @@ import { recordCodec, TYPES, _AnyPgCodecAttribute, - DefaultPgCodecAttribute, - DefaultPgCodecAttributeVia, + GenericPgCodecAttribute, + GenericPgCodecAttributeVia, _AnyPgCodecAttributeVia, _AnyPgCodecAttributeViaAttribute, _AnyPgCodecAttributeViaRelationName, @@ -58,13 +58,13 @@ import { _AnyPgResourceParameter, _AnyPgResourceUnique, _AnyScalarPgResource, - DefaultPgResourceOptions, - DefaultPgResource, - DefaultPgResourceParameter, - DefaultPgResourceUnique, + GenericPgResourceOptions, + GenericPgResource, + GenericPgResourceParameter, + GenericPgResourceUnique, DefaultRegistryBuilder, EmptyRegistryBuilder, - DefaultPgFunctionResourceOptions, + GenericPgFunctionResourceOptions, _AnyPgFunctionResourceOptions, } from "./datasource.js"; import { @@ -152,16 +152,16 @@ import { PgRegistryRelationConfigs, PgRegistryResourceOptions, PgRegistryResources, - DefaultPgRelation, + GenericPgRelation, _AnyPgRelation, - DefaultPgCodec, + GenericPgCodec, DefaultScalarPgCodec, - DefaultPgCodecRelationConfig, - DefaultPgCodecAttributesRecord, - DefaultPgRangeItemCodec, - DefaultPgRegistry, - DefaultPgCodecWithAttributes, - DefaultPgRegistryConfig, + GenericPgCodecRelationConfig, + GenericPgCodecAttributesRecord, + GenericPgRangeItemCodec, + GenericPgRegistry, + GenericPgCodecWithAttributes, + GenericPgRegistryConfig, } from "./interfaces.js"; import { PgLockableParameter, PgLockCallback } from "./pgLocker.js"; import { @@ -180,7 +180,7 @@ import { PgCursorStep } from "./steps/pgCursor.js"; import { pgDeleteSingle, PgDeleteSingleStep, - DefaultPgDeleteSingleStep, + GenericPgDeleteSingleStep, _AnyPgDeleteSingleStep, } from "./steps/pgDeleteSingle.js"; import { pgInsertSingle, PgInsertSingleStep } from "./steps/pgInsertSingle.js"; @@ -202,7 +202,7 @@ import { PgSelectParsedCursorStep, PgSelectStep, sqlFromArgDigests, - DefaultPgSelectStep, + GenericPgSelectStep, _AnyPgSelectStep, } from "./steps/pgSelect.js"; import { @@ -212,7 +212,7 @@ import { PgSelectSingleStep, _AnyPgSelectSinglePlanOptions, _AnyPgSelectSingleStep, - DefaultPgSelectSingleStep, + GenericPgSelectSingleStep, } from "./steps/pgSelectSingle.js"; import { pgSingleTablePolymorphic, @@ -232,7 +232,7 @@ import { import { pgUpdateSingle, PgUpdateSingleStep, - DefaultPgUpdateSingleStep, + GenericPgUpdateSingleStep, _AnyPgUpdateSingleStep, } from "./steps/pgUpdateSingle.js"; import { @@ -249,25 +249,25 @@ import { import { assertPgClassSingleStep } from "./utils.js"; export { - DefaultPgResourceOptions, + GenericPgResourceOptions, _AnyPgSelectSinglePlanOptions, - DefaultPgCodecAttribute, - DefaultPgCodecAttributeVia, + GenericPgCodecAttribute, + GenericPgCodecAttributeVia, _AnyPgSelectSingleStep, - DefaultPgResource, - DefaultPgResourceParameter, - DefaultPgSelectSingleStep, - DefaultPgRelation, + GenericPgResource, + GenericPgResourceParameter, + GenericPgSelectSingleStep, + GenericPgRelation, _AnyPgRelation, - DefaultPgCodecWithAttributes, - DefaultPgResourceUnique, + GenericPgCodecWithAttributes, + GenericPgResourceUnique, DefaultRegistryBuilder, EmptyRegistryBuilder, - DefaultPgUpdateSingleStep, + GenericPgUpdateSingleStep, _AnyPgUpdateSingleStep, - DefaultPgDeleteSingleStep, + GenericPgDeleteSingleStep, _AnyPgDeleteSingleStep, - DefaultPgCodec, + GenericPgCodec, _AnyPgRegistryBuilder, _AnyPgResource, _AnyPgResourceOptions, @@ -311,7 +311,7 @@ export { digestsFromArgumentSpecs, domainOfCodec, enumCodec, - DefaultPgFunctionResourceOptions, + GenericPgFunctionResourceOptions, _AnyPgFunctionResourceOptions, getCodecByPgCatalogTypeName, getInnerCodec, @@ -324,11 +324,11 @@ export { GetPgResourceRelations, GetPgResourceUniques, DefaultScalarPgCodec, - DefaultPgCodecRelationConfig, - DefaultPgCodecAttributesRecord, - DefaultPgRangeItemCodec, - DefaultPgRegistry, - DefaultPgRegistryConfig, + GenericPgCodecRelationConfig, + GenericPgCodecAttributesRecord, + GenericPgRangeItemCodec, + GenericPgRegistry, + GenericPgRegistryConfig, isEnumCodec, KeysOfType, listOfCodec, @@ -386,7 +386,7 @@ export { PgExecutor, PgExecutorContext, _AnyPgSelectStep, - DefaultPgSelectStep, + GenericPgSelectStep, PgExecutorContextPlans, PgExecutorInput, PgExecutorMutationOptions, diff --git a/grafast/dataplan-pg/src/interfaces.ts b/grafast/dataplan-pg/src/interfaces.ts index 8d4b2b73cb..8a114c729f 100644 --- a/grafast/dataplan-pg/src/interfaces.ts +++ b/grafast/dataplan-pg/src/interfaces.ts @@ -4,7 +4,7 @@ import type { SQL, SQLRawValue } from "pg-sql2"; import type { PgAdaptorOptions } from "./adaptors/pg.js"; import type { _AnyPgCodecAttribute, - DefaultPgCodecAttribute, + GenericPgCodecAttribute, PgCodecAttribute, PgCodecAttributeCodec, PgCodecAttributeName, @@ -14,8 +14,8 @@ import type { _AnyPgResourceOptions, _AnyPgResourceParameter, _AnyPgResourceUnique, - DefaultPgResource, - DefaultPgResourceOptions, + GenericPgResource, + GenericPgResourceOptions, PgCodecRefs, PgResource, PgResourceCodec, @@ -130,8 +130,8 @@ export type PgCodecPolymorphism = | PgCodecPolymorphismRelational | PgCodecPolymorphismUnion; -export interface DefaultPgRangeItemCodec - extends PgCodec {} +export interface GenericPgRangeItemCodec + extends PgCodec {} /** @internal */ export interface _AnyPgRangeItemCodec extends PgCodec {} @@ -196,8 +196,8 @@ export type PgCodecFromPg = PgDecode< PgCodecFromJavaScript, PgCodecFromPostgres >; -export interface DefaultPgCodecAttributesRecord - extends PgCodecAttributesRecord {} +export interface GenericPgCodecAttributesRecord + extends PgCodecAttributesRecord {} /** @internal */ export interface _AnyPgCodecAttributesRecord extends PgCodecAttributesRecord {} @@ -207,15 +207,15 @@ export type PgCodecAttributesRecord< > = { [TCodecAttribute in TCodecAttributes as PgCodecAttributeName]: TCodecAttribute; }; -export interface DefaultPgCodec +export interface GenericPgCodec extends PgCodec< string, - PgCodecAttributesRecord, + PgCodecAttributesRecord, any, any, - DefaultPgCodec, - DefaultPgCodec, - DefaultPgCodec + GenericPgCodec, + GenericPgCodec, + GenericPgCodec > {} export interface DefaultScalarPgCodec extends PgCodec< @@ -223,9 +223,9 @@ export interface DefaultScalarPgCodec never, any, any, - DefaultPgCodec, - DefaultPgCodec, - DefaultPgCodec + GenericPgCodec, + GenericPgCodec, + GenericPgCodec > {} /** @internal */ export interface _AnyScalarPgCodec @@ -370,10 +370,10 @@ export interface PgCodec< */ executor: PgExecutor | null; } -export interface DefaultPgCodecWithAttributes +export interface GenericPgCodecWithAttributes extends PgCodec< any, - Record, + Record, any, any, never, @@ -672,11 +672,11 @@ export interface PgCodecRelationBase< description?: string; } -export interface DefaultPgCodecRelationConfig +export interface GenericPgCodecRelationConfig extends PgCodecRelationConfig< string, - DefaultPgCodec, - DefaultPgResourceOptions + GenericPgCodec, + GenericPgResourceOptions > {} /** @internal */ export interface _AnyPgCodecRelationConfig @@ -709,11 +709,11 @@ export interface PgCodecRelationConfig< remoteResourceOptions: TRemoteResourceOptions; } -export interface DefaultPgRegistryConfig +export interface GenericPgRegistryConfig extends PgRegistryConfig< - DefaultPgCodec, - DefaultPgResourceOptions, - DefaultPgCodecRelationConfig + GenericPgCodec, + GenericPgResourceOptions, + GenericPgCodecRelationConfig > {} /** @internal */ export interface _AnyPgRegistryConfig extends PgRegistryConfig {} @@ -786,12 +786,12 @@ export type PgRegistryRelationConfigs< /** @internal */ export interface _AnyPgRelation extends PgRelation {} -export interface DefaultPgRelation +export interface GenericPgRelation extends PgRelation< string, - DefaultPgCodec, - DefaultPgResourceOptions, - DefaultPgRegistry + GenericPgCodec, + GenericPgResourceOptions, + GenericPgRegistry > {} export interface PgRelation< @@ -835,11 +835,11 @@ export type PgRegistryCodecRelations< }; /** @internal */ export interface _AnyPgRegistry extends PgRegistry {} -export interface DefaultPgRegistry +export interface GenericPgRegistry extends PgRegistry< - DefaultPgCodec, - DefaultPgResourceOptions, - DefaultPgCodecRelationConfig + GenericPgCodec, + GenericPgResourceOptions, + GenericPgCodecRelationConfig > {} export interface EmptyPgRegistry extends PgRegistry {} diff --git a/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts b/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts index bdb315626c..8c67b5754e 100644 --- a/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts @@ -19,7 +19,7 @@ import type { PgClassExpressionStep } from "./pgClassExpression.js"; import { pgClassExpression } from "./pgClassExpression.js"; import { _AnyPgResource, - DefaultPgResource, + GenericPgResource, PgResourceUnique, } from "../datasource.js"; import { PgCodecAttributeCodec, PgCodecAttributeName } from "../codecs.js"; @@ -42,8 +42,8 @@ interface PgDeletePlanFinalizeResults { /** @internal */ export interface _AnyPgDeleteSingleStep extends PgDeleteSingleStep {} -export interface DefaultPgDeleteSingleStep - extends PgDeleteSingleStep {} +export interface GenericPgDeleteSingleStep + extends PgDeleteSingleStep {} /** * Deletes a row in the database, can return columns from the deleted row. */ diff --git a/grafast/dataplan-pg/src/steps/pgSelect.ts b/grafast/dataplan-pg/src/steps/pgSelect.ts index a63991eb61..0b1d890233 100644 --- a/grafast/dataplan-pg/src/steps/pgSelect.ts +++ b/grafast/dataplan-pg/src/steps/pgSelect.ts @@ -47,7 +47,7 @@ import type { _AnyPgResource, _AnyPgResourceUnique, _AnyScalarPgResource, - DefaultPgResource, + GenericPgResource, PgResource, PgResourceCodec, } from "../datasource.js"; @@ -265,7 +265,7 @@ export interface PgSelectOptions { joinAsLateral?: boolean; } -export interface DefaultPgSelectStep extends PgSelectStep {} +export interface GenericPgSelectStep extends PgSelectStep {} /** @internal */ export interface _AnyPgSelectStep extends PgSelectStep {} diff --git a/grafast/dataplan-pg/src/steps/pgSelectSingle.ts b/grafast/dataplan-pg/src/steps/pgSelectSingle.ts index f4b7f0c248..ee46814164 100644 --- a/grafast/dataplan-pg/src/steps/pgSelectSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgSelectSingle.ts @@ -18,7 +18,7 @@ import type { import { TYPES } from "../codecs.js"; import type { _AnyPgResource, - DefaultPgResource, + GenericPgResource, PgResource, PgResourceCodec, PgResourceRegistry, @@ -82,7 +82,7 @@ const CHEAP_ATTRIBUTE_TYPES = new Set<_AnyPgCodec>([ /** @internal */ export interface _AnyPgSelectSingleStep extends PgSelectSingleStep {} -export interface DefaultPgSelectSingleStep extends PgSelectSingleStep {} +export interface GenericPgSelectSingleStep extends PgSelectSingleStep {} export type PgSelectSingleStepResource = U extends PgSelectSingleStep< infer TResource > diff --git a/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts b/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts index efc492b17e..7865c1dab6 100644 --- a/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts @@ -8,7 +8,7 @@ import type { SQL, SQLRawValue } from "pg-sql2"; import sql from "pg-sql2"; import type { _AnyPgCodecAttribute, PgCodecAttributeName } from "../codecs.js"; -import type { DefaultPgResource, PgResourceUnique } from "../index.js"; +import type { GenericPgResource, PgResourceUnique } from "../index.js"; import { inspect } from "../inspect.js"; import type { _AnyPgCodec, @@ -38,8 +38,8 @@ interface PgUpdatePlanFinalizeResults { } /** @internal */ export interface _AnyPgUpdateSingleStep extends PgUpdateSingleStep {} -export interface DefaultPgUpdateSingleStep - extends PgUpdateSingleStep {} +export interface GenericPgUpdateSingleStep + extends PgUpdateSingleStep {} /** * Update a single row identified by the 'getBy' argument. */ diff --git a/graphile-build/graphile-build-pg/src/inputUtils.ts b/graphile-build/graphile-build-pg/src/inputUtils.ts index 8d2a53f704..c03e5678a0 100644 --- a/graphile-build/graphile-build-pg/src/inputUtils.ts +++ b/graphile-build/graphile-build-pg/src/inputUtils.ts @@ -1,6 +1,6 @@ import "graphile-build"; -import type { DefaultPgCodec, DefaultPgResource } from "@dataplan/pg"; +import type { GenericPgCodec, GenericPgResource } from "@dataplan/pg"; /** * Metadata for a specific PgCodec @@ -18,12 +18,12 @@ export interface PgCodecMeta { /** * A map from PgCodec to its associated metadata. */ -export type PgCodecMetaLookup = Map; +export type PgCodecMetaLookup = Map; /** * Creates an empty meta object for the given codec. */ -export function makePgCodecMeta(_codec: DefaultPgCodec): PgCodecMeta { +export function makePgCodecMeta(_codec: GenericPgCodec): PgCodecMeta { return { typeNameBySituation: Object.create(null), }; @@ -40,7 +40,7 @@ export function getCodecMetaLookupFromInput( input: GraphileBuild.BuildInput, ): PgCodecMetaLookup { const metaLookup: PgCodecMetaLookup = new Map(); - const seenResources = new Set(); + const seenResources = new Set(); for (const codec of Object.values(input.pgRegistry.pgCodecs)) { walkCodec(codec, metaLookup); } @@ -57,9 +57,9 @@ export function getCodecMetaLookupFromInput( * @internal */ function walkResource( - resource: DefaultPgResource, + resource: GenericPgResource, metaLookup: PgCodecMetaLookup, - seenResources: Set, + seenResources: Set, ): void { if (seenResources.has(resource)) { return; @@ -86,7 +86,7 @@ function walkResource( * * @internal */ -function walkCodec(codec: DefaultPgCodec, metaLookup: PgCodecMetaLookup): void { +function walkCodec(codec: GenericPgCodec, metaLookup: PgCodecMetaLookup): void { if (metaLookup.has(codec)) { return; } diff --git a/graphile-build/graphile-build-pg/src/interfaces.ts b/graphile-build/graphile-build-pg/src/interfaces.ts index 5d64224b5b..d9f4a9a554 100644 --- a/graphile-build/graphile-build-pg/src/interfaces.ts +++ b/graphile-build/graphile-build-pg/src/interfaces.ts @@ -1,4 +1,4 @@ -import type { DefaultPgRegistry, PgRegistry, WithPgClient } from "@dataplan/pg"; +import type { GenericPgRegistry, PgRegistry, WithPgClient } from "@dataplan/pg"; import type { PromiseOrDirect } from "grafast"; declare global { namespace GraphileBuild { @@ -107,7 +107,7 @@ declare global { namespace GraphileBuild { interface BuildInput { - pgRegistry: DefaultPgRegistry; + pgRegistry: GenericPgRegistry; } } } diff --git a/graphile-build/graphile-build-pg/src/plugins/PgAttributesPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgAttributesPlugin.ts index 3eafa0d535..4943d6035c 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgAttributesPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgAttributesPlugin.ts @@ -3,9 +3,9 @@ import "../interfaces.js"; import "graphile-config"; import type { - DefaultPgCodec, - DefaultPgCodecAttribute, - DefaultPgSelectSingleStep, + GenericPgCodec, + GenericPgCodecAttribute, + GenericPgSelectSingleStep, PgClassExpressionStep, PgCodecList, PgConditionStep, @@ -24,9 +24,9 @@ declare global { namespace GraphileBuild { interface Build { pgResolveOutputType( - codec: DefaultPgCodec, + codec: GenericPgCodec, notNull?: boolean, - ): [baseCodec: DefaultPgCodec, resolvedType: GraphQLOutputType] | null; + ): [baseCodec: GenericPgCodec, resolvedType: GraphQLOutputType] | null; } interface Inflection { @@ -41,7 +41,7 @@ declare global { _attributeName( this: GraphileBuild.Inflection, details: { - codec: DefaultPgCodec; + codec: GenericPgCodec; attributeName: string; skipRowId?: boolean; }, @@ -53,7 +53,7 @@ declare global { */ _joinAttributeNames( this: GraphileBuild.Inflection, - codec: DefaultPgCodec, + codec: GenericPgCodec, names: readonly string[], ): string; @@ -66,7 +66,7 @@ declare global { this: GraphileBuild.Inflection, details: { attributeName: string; - codec: DefaultPgCodec; + codec: GenericPgCodec; }, ): string; } @@ -77,7 +77,7 @@ declare global { isPgBaseInput?: boolean; isPgRowType?: boolean; isPgCompoundType?: boolean; - pgAttribute?: DefaultPgCodecAttribute; + pgAttribute?: GenericPgCodecAttribute; } } } @@ -159,7 +159,7 @@ function processAttribute( if (!baseCodec.attributes) { // Simply get the value return EXPORTABLE( - (attributeName) => ($record: DefaultPgSelectSingleStep) => { + (attributeName) => ($record: GenericPgSelectSingleStep) => { return $record.get(attributeName); }, [attributeName], @@ -290,7 +290,7 @@ export const PgAttributesPlugin: GraphileConfig.Plugin = { "select base update insert filterBy orderBy", ]); const attribute = codec.attributes![attributeName]; - function walk(codec: DefaultPgCodec) { + function walk(codec: GenericPgCodec) { if (codec.arrayOfCodec) { behaviors.add("-condition:attribute:filterBy"); behaviors.add(`-attribute:orderBy`); @@ -596,9 +596,9 @@ export const PgAttributesPlugin: GraphileConfig.Plugin = { function resolveOutputType( build: GraphileBuild.Build, - codec: DefaultPgCodec, + codec: GenericPgCodec, notNull?: boolean, -): [baseCodec: DefaultPgCodec, resolvedType: GraphQLOutputType] | null { +): [baseCodec: GenericPgCodec, resolvedType: GraphQLOutputType] | null { const { getGraphQLTypeByPgCodec, graphql: { GraphQLList, GraphQLNonNull, getNullableType, isOutputType }, diff --git a/graphile-build/graphile-build-pg/src/plugins/PgBasicsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgBasicsPlugin.ts index bc6691f20a..12596a6cd1 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgBasicsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgBasicsPlugin.ts @@ -21,19 +21,19 @@ declare global { "@dataplan/pg": string; } type HasGraphQLTypeForPgCodec = ( - codec: dataplanPg.DefaultPgCodec, + codec: dataplanPg.GenericPgCodec, situation?: string, ) => boolean; type GetGraphQLTypeByPgCodec = ( - codec: dataplanPg.DefaultPgCodec, + codec: dataplanPg.GenericPgCodec, situation: string, ) => GraphQLType | null; type GetGraphQLTypeNameByPgCodec = ( - codec: dataplanPg.DefaultPgCodec, + codec: dataplanPg.GenericPgCodec, situation: string, ) => string | null; type SetGraphQLTypeForPgCodec = ( - codec: dataplanPg.DefaultPgCodec, + codec: dataplanPg.GenericPgCodec, situations: string | string[], typeName: string, ) => void; @@ -84,30 +84,30 @@ declare global { /** * Get a table-like resource for the given codec, assuming exactly one exists. */ - pgTableResource( + pgTableResource( codec: TCodec, strict?: boolean, ): PgResource< string, TCodec, - dataplanPg.DefaultPgResourceUnique, + dataplanPg.GenericPgResourceUnique, never, - dataplanPg.DefaultPgRegistry + dataplanPg.GenericPgRegistry > | null; } interface BehaviorEntities { - pgCodec: dataplanPg.DefaultPgCodec; + pgCodec: dataplanPg.GenericPgCodec; pgCodecAttribute: [ - codec: dataplanPg.DefaultPgCodec, + codec: dataplanPg.GenericPgCodec, attributeName: string, ]; - pgResource: dataplanPg.DefaultPgResource; + pgResource: dataplanPg.GenericPgResource; pgResourceUnique: [ - resource: dataplanPg.DefaultPgResource, - unique: dataplanPg.DefaultPgResourceUnique, + resource: dataplanPg.GenericPgResource, + unique: dataplanPg.GenericPgResourceUnique, ]; - pgCodecRelation: dataplanPg.DefaultPgRelation; + pgCodecRelation: dataplanPg.GenericPgRelation; pgCodecRef: PgCodecRef; pgRefDefinition: PgRefDefinition; } @@ -330,22 +330,22 @@ export const PgBasicsPlugin: GraphileConfig.Plugin = { } }; const resourceByCodecCacheUnstrict = new Map< - dataplanPg.DefaultPgCodec, - dataplanPg.DefaultPgResource | null + dataplanPg.GenericPgCodec, + dataplanPg.GenericPgResource | null >(); const resourceByCodecCacheStrict = new Map< - dataplanPg.DefaultPgCodec, - dataplanPg.DefaultPgResource | null + dataplanPg.GenericPgCodec, + dataplanPg.GenericPgResource | null >(); - const pgTableResource = ( + const pgTableResource = ( codec: TCodec, strict = true, ): PgResource< string, TCodec, - dataplanPg.DefaultPgResourceUnique, + dataplanPg.GenericPgResourceUnique, never, - dataplanPg.DefaultPgRegistry + dataplanPg.GenericPgRegistry > | null => { const resourceByCodecCache = strict ? resourceByCodecCacheStrict @@ -363,7 +363,7 @@ export const PgBasicsPlugin: GraphileConfig.Plugin = { TCodec, any, never, - dataplanPg.DefaultPgRegistry + dataplanPg.GenericPgRegistry > => r.codec === codec && !r.parameters && diff --git a/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts index c349e5e0d0..083929f710 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts @@ -1,7 +1,7 @@ import type { _AnyPgCodecAttributesRecord, - DefaultPgCodec, - DefaultPgCodecAttribute, + GenericPgCodec, + GenericPgCodecAttribute, PgCodec, PgCodecAnyScalar, PgCodecAttributes, @@ -29,11 +29,11 @@ import { version } from "../version.js"; interface State { codecByTypeIdByDatabaseName: Map< string, - Map> + Map> >; codecByClassIdByDatabaseName: Map< string, - Map> + Map> >; } @@ -65,7 +65,7 @@ declare global { } interface Build { - allPgCodecs: Set; + allPgCodecs: Set; } interface ScopeObject { @@ -76,7 +76,7 @@ declare global { interface ScopeInputObject { isPgRangeInputType?: boolean; isPgRangeBoundInputType?: boolean; - pgCodec?: DefaultPgCodec; + pgCodec?: GenericPgCodec; } } @@ -86,18 +86,18 @@ declare global { getCodecFromClass( serviceName: string, pgClassId: string, - ): Promise; + ): Promise; getCodecFromType( serviceName: string, pgTypeId: string, pgTypeModifier?: string | number | null, - ): Promise; + ): Promise; }; } interface GatherHooks { pgCodecs_PgCodec(event: { serviceName: string; - pgCodec: DefaultPgCodec; + pgCodec: GenericPgCodec; pgClass?: PgClass; pgType: PgType; }): Promise | void; @@ -106,7 +106,7 @@ declare global { serviceName: string; pgClass: PgClass; pgAttribute: PgAttribute; - attribute: DefaultPgCodecAttribute; + attribute: GenericPgCodecAttribute; }): Promise | void; pgCodecs_recordType_spec(event: { @@ -124,21 +124,21 @@ declare global { pgCodecs_rangeOfCodec_extensions(event: { serviceName: string; pgType: PgType; - innerCodec: DefaultPgCodec; + innerCodec: GenericPgCodec; extensions: any; }): Promise | void; pgCodecs_domainOfCodec_extensions(event: { serviceName: string; pgType: PgType; - innerCodec: DefaultPgCodec; + innerCodec: GenericPgCodec; extensions: any; }): Promise | void; pgCodecs_listOfCodec_extensions(event: { serviceName: string; pgType: PgType; - innerCodec: DefaultPgCodec; + innerCodec: GenericPgCodec; extensions: any; }): Promise | void; } @@ -467,7 +467,7 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { return map.get(typeId)!; } - const promise = (async (): Promise => { + const promise = (async (): Promise => { const type = await info.helpers.pgIntrospection.getType( serviceName, typeId, @@ -504,7 +504,7 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { ); } - const codec = await (async (): Promise => { + const codec = await (async (): Promise => { const namespace = await info.helpers.pgIntrospection.getNamespace( serviceName, type.typnamespace, @@ -827,7 +827,7 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { hooks: { async pgRegistry_PgRegistryBuilder_pgCodecs(info, event) { const { registryBuilder } = event; - const codecs = new Set(); + const codecs = new Set(); // If we get errors from the frozen object then clearly we need to // ensure more work has completed before continuing - call other plugin @@ -862,8 +862,8 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { schema: { hooks: { build(build) { - build.allPgCodecs = new Set(); - function walkCodec(codec: DefaultPgCodec): void { + build.allPgCodecs = new Set(); + function walkCodec(codec: GenericPgCodec): void { if (build.allPgCodecs!.has(codec)) { return; } @@ -907,7 +907,7 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { } // Ensure all sources are uniquely named - const knownCodecByName = new Map(); + const knownCodecByName = new Map(); for (const codec of build.allPgCodecs) { const known = knownCodecByName.get(codec.name); if (known === codec) { @@ -1083,8 +1083,8 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { // Now walk over all the codecs and ensure that each on has an associated type function prepareTypeForCodec( - codec: DefaultPgCodec, - visited: Set, + codec: GenericPgCodec, + visited: Set, ): void { if (visited.has(codec)) { return; @@ -1452,7 +1452,7 @@ export const PgCodecsPlugin: GraphileConfig.Plugin = { } } - const visited: Set = new Set(); + const visited: Set = new Set(); for (const codec of build.allPgCodecs) { prepareTypeForCodec(codec, visited); } diff --git a/graphile-build/graphile-build-pg/src/plugins/PgConditionArgumentPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgConditionArgumentPlugin.ts index 7cd8878334..837d9a9ab7 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgConditionArgumentPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgConditionArgumentPlugin.ts @@ -2,8 +2,8 @@ import "./PgTablesPlugin.js"; import "graphile-config"; import type { - DefaultPgSelectSingleStep, - DefaultPgSelectStep, + GenericPgSelectSingleStep, + GenericPgSelectStep, PgCodecWithAttributes, PgSelectParsedCursorStep, PgSelectSingleStep, @@ -158,15 +158,15 @@ export const PgConditionArgumentPlugin: GraphileConfig.Plugin = { ? ( _condition, $connection: ConnectionStep< - DefaultPgSelectSingleStep, + GenericPgSelectSingleStep, PgSelectParsedCursorStep, - DefaultPgSelectStep + GenericPgSelectStep >, ) => { const $select = $connection.getSubplan(); return $select.wherePlan(); } - : (_condition, $select: DefaultPgSelectStep) => { + : (_condition, $select: GenericPgSelectStep) => { return $select.wherePlan(); }, }, diff --git a/graphile-build/graphile-build-pg/src/plugins/PgConditionCustomFieldsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgConditionCustomFieldsPlugin.ts index 06f5765aae..6b2dc1ce92 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgConditionCustomFieldsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgConditionCustomFieldsPlugin.ts @@ -1,7 +1,7 @@ import "graphile-config"; import type { - DefaultPgResource, + GenericPgResource, PgConditionStep, PgResource, PgResourceParameter, @@ -25,7 +25,7 @@ declare global { * arguments except the first are nullable, the first argument is a composite * type, and the result is a simple scalar type. */ -export function isSimpleScalarComputedColumnLike(resource: DefaultPgResource) { +export function isSimpleScalarComputedColumnLike(resource: GenericPgResource) { if (resource.codec.attributes) return false; if (resource.codec.arrayOfCodec) return false; if (resource.codec.rangeOfCodec) return false; @@ -86,7 +86,7 @@ export const PgConditionCustomFieldsPlugin: GraphileConfig.Plugin = { return build.extend( fields, functionSources.reduce( - (memo, rawPgFieldSource: DefaultPgResource) => { + (memo, rawPgFieldSource: GenericPgResource) => { const pgFieldSource = rawPgFieldSource; const fieldName = inflection.computedAttributeField({ resource: pgFieldSource, diff --git a/graphile-build/graphile-build-pg/src/plugins/PgConnectionArgOrderByPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgConnectionArgOrderByPlugin.ts index 2e23264c49..00c223dff1 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgConnectionArgOrderByPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgConnectionArgOrderByPlugin.ts @@ -2,7 +2,7 @@ import "./PgTablesPlugin.js"; import "graphile-config"; import type { - DefaultPgCodec, + GenericPgCodec, PgCodec, PgSelectParsedCursorStep, PgSelectSingleStep, @@ -26,7 +26,7 @@ declare global { orderByType(this: Inflection, typeName: string): string; } interface ScopeEnum { - pgCodec?: DefaultPgCodec; + pgCodec?: GenericPgCodec; isPgRowSortEnum?: boolean; } } diff --git a/graphile-build/graphile-build-pg/src/plugins/PgCustomTypeFieldPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgCustomTypeFieldPlugin.ts index 870d5402a5..8b3c87e639 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgCustomTypeFieldPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgCustomTypeFieldPlugin.ts @@ -6,13 +6,13 @@ import "./PgProceduresPlugin.js"; import "graphile-config"; import type { - DefaultPgCodec, - DefaultPgDeleteSingleStep, - DefaultPgResource, - DefaultPgResourceParameter, - DefaultPgSelectSingleStep, - DefaultPgSelectStep, - DefaultPgUpdateSingleStep, + GenericPgCodec, + GenericPgDeleteSingleStep, + GenericPgResource, + GenericPgResourceParameter, + GenericPgSelectSingleStep, + GenericPgSelectStep, + GenericPgUpdateSingleStep, PgClassSingleStep, PgCodec, PgDeleteSingleStep, @@ -64,8 +64,8 @@ declare global { namespace GraphileBuild { interface Build { pgGetArgDetailsFromParameters( - resource: DefaultPgResource, - parameters?: readonly DefaultPgResourceParameter[], + resource: GenericPgResource, + parameters?: readonly GenericPgResourceParameter[], ): { makeFieldArgs(): { [graphqlArgName: string]: { @@ -77,13 +77,13 @@ declare global { argDetails: Array<{ graphqlArgName: string; postgresArgName: string | null; - pgCodec: DefaultPgCodec; + pgCodec: GenericPgCodec; inputType: GraphQLInputType; required: boolean; }>; makeExpression(opts: { $placeholderable: { - placeholder($step: ExecutableStep, codec: DefaultPgCodec): SQL; + placeholder($step: ExecutableStep, codec: GenericPgCodec): SQL; }; resource: PgResource; fieldArgs: FieldArgs; @@ -94,15 +94,15 @@ declare global { } interface InflectionCustomFieldProcedureDetails { - resource: DefaultPgResource; + resource: GenericPgResource; } interface InflectionCustomFieldArgumentDetails { - resource: DefaultPgResource; - param: DefaultPgResourceParameter; + resource: GenericPgResource; + param: GenericPgResourceParameter; index: number; } interface InflectionCustomFieldMutationResult { - resource: DefaultPgResource; + resource: GenericPgResource; returnGraphQLTypeName: string; } @@ -179,7 +179,7 @@ declare global { } function shouldUseCustomConnection( - pgResource: DefaultPgResource, + pgResource: GenericPgResource, ): boolean { const { codec } = pgResource; // 'setof ' functions should use a connection based on the function name, not a generic connection @@ -188,7 +188,7 @@ function shouldUseCustomConnection( return setOrArray && scalarOrAnonymous; } -function defaultProcSourceBehavior(s: DefaultPgResource): string { +function defaultProcSourceBehavior(s: GenericPgResource): string { const behavior = []; const firstParameter = s.parameters[0]; if ( @@ -235,19 +235,19 @@ function defaultProcSourceBehavior(s: DefaultPgResource): string { function hasRecord( $row: ExecutableStep, ): $row is - | DefaultPgSelectSingleStep + | GenericPgSelectSingleStep | PgInsertSingleStep - | DefaultPgUpdateSingleStep - | DefaultPgDeleteSingleStep { + | GenericPgUpdateSingleStep + | GenericPgDeleteSingleStep { return "record" in $row && typeof ($row as any).record === "function"; } declare global { namespace GraphileBuild { interface Build { - [$$rootQuery]: Array; - [$$rootMutation]: Array; - [$$computed]: Map>; + [$$rootQuery]: Array; + [$$rootMutation]: Array; + [$$computed]: Map>; } } } @@ -1186,7 +1186,7 @@ export const PgCustomTypeFieldPlugin: GraphileConfig.Plugin = { $parent, args, info, - ) as DefaultPgSelectStep; + ) as GenericPgSelectStep; return connection( $select, ($item) => $item, @@ -1272,7 +1272,7 @@ export const PgCustomTypeFieldPlugin: GraphileConfig.Plugin = { function getFunctionSourceReturnGraphQLType( build: GraphileBuild.Build, - resource: DefaultPgResource, + resource: GenericPgResource, ): GraphQLOutputType | null { const resourceInnerCodec = resource.codec.arrayOfCodec ?? resource.codec; if (!resourceInnerCodec) { diff --git a/graphile-build/graphile-build-pg/src/plugins/PgFirstLastBeforeAfterArgsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgFirstLastBeforeAfterArgsPlugin.ts index c67f71f2ed..e8421714f5 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgFirstLastBeforeAfterArgsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgFirstLastBeforeAfterArgsPlugin.ts @@ -2,8 +2,8 @@ import "./PgTablesPlugin.js"; import "graphile-config"; import type { - DefaultPgSelectSingleStep, - DefaultPgSelectStep, + GenericPgSelectSingleStep, + GenericPgSelectStep, PgSelectParsedCursorStep, } from "@dataplan/pg"; import type { ConnectionStep, GrafastFieldConfigArgumentMap } from "grafast"; @@ -93,9 +93,9 @@ function commonFn( function plan( _: any, $connection: ConnectionStep< - DefaultPgSelectSingleStep, + GenericPgSelectSingleStep, PgSelectParsedCursorStep, - DefaultPgSelectStep + GenericPgSelectStep >, arg, ) { @@ -118,9 +118,9 @@ function commonFn( function plan( _: any, $connection: ConnectionStep< - DefaultPgSelectSingleStep, + GenericPgSelectSingleStep, PgSelectParsedCursorStep, - DefaultPgSelectStep + GenericPgSelectStep >, val, ) { @@ -145,9 +145,9 @@ function commonFn( function plan( _: any, $connection: ConnectionStep< - DefaultPgSelectSingleStep, + GenericPgSelectSingleStep, PgSelectParsedCursorStep, - DefaultPgSelectStep + GenericPgSelectStep >, val, ) { @@ -170,9 +170,9 @@ function commonFn( function plan( _: any, $connection: ConnectionStep< - DefaultPgSelectSingleStep, + GenericPgSelectSingleStep, PgSelectParsedCursorStep, - DefaultPgSelectStep + GenericPgSelectStep >, val, ) { @@ -193,9 +193,9 @@ function commonFn( function plan( _: any, $connection: ConnectionStep< - DefaultPgSelectSingleStep, + GenericPgSelectSingleStep, PgSelectParsedCursorStep, - DefaultPgSelectStep + GenericPgSelectStep >, val, ) { diff --git a/graphile-build/graphile-build-pg/src/plugins/PgInterfaceModeUnionAllRowsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgInterfaceModeUnionAllRowsPlugin.ts index f677aee712..e4094e1a28 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgInterfaceModeUnionAllRowsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgInterfaceModeUnionAllRowsPlugin.ts @@ -1,8 +1,8 @@ import "graphile-config"; import type { - DefaultPgCodec, - DefaultPgResource, + GenericPgCodec, + GenericPgResource, PgCodec, PgUnionAllStepConfigAttributes, PgUnionAllStepMember, @@ -21,7 +21,7 @@ declare global { * The base inflector used by allInterfaceModeUnionRowsConnection and * allInterfaceModeUnionRowsList. */ - _allInterfaceModeUnionRows(this: Inflection, codec: DefaultPgCodec): string; + _allInterfaceModeUnionRows(this: Inflection, codec: GenericPgCodec): string; /** * The field name for a Cursor Connection field that returns all rows @@ -29,14 +29,14 @@ declare global { */ allInterfaceModeUnionRowsConnection( this: Inflection, - codec: DefaultPgCodec, + codec: GenericPgCodec, ): string; /** * The field name for a List field that returns all rows from the given * `@interface mode:union` codec. */ - allInterfaceModeUnionRowsList(this: Inflection, codec: DefaultPgCodec): string; + allInterfaceModeUnionRowsList(this: Inflection, codec: GenericPgCodec): string; } } } @@ -94,7 +94,7 @@ export const PgInterfaceModeUnionAllRowsPlugin: GraphileConfig.Plugin = { const resourcesByPolymorphicTypeName: { [polymorphicTypeName: string]: { - resources: DefaultPgResource[]; + resources: GenericPgResource[]; type: "union" | "interface"; }; } = Object.create(null); @@ -154,7 +154,7 @@ export const PgInterfaceModeUnionAllRowsPlugin: GraphileConfig.Plugin = { } const interfaceCodecs: { - [polymorphicTypeName: string]: DefaultPgCodec; + [polymorphicTypeName: string]: GenericPgCodec; } = Object.create(null); for (const codec of Object.values(pgRegistry.pgCodecs)) { if (!codec.polymorphism) continue; @@ -217,7 +217,7 @@ export const PgInterfaceModeUnionAllRowsPlugin: GraphileConfig.Plugin = { if (!interfaceCodec.attributes) return; const attributes: PgUnionAllStepConfigAttributes = interfaceCodec.attributes; - const resourceByTypeName: Record = + const resourceByTypeName: Record = Object.create(null); const members: PgUnionAllStepMember[] = []; for (const resource of spec.resources) { diff --git a/graphile-build/graphile-build-pg/src/plugins/PgJWTPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgJWTPlugin.ts index 6f32f8fe02..21ea6b5e91 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgJWTPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgJWTPlugin.ts @@ -1,8 +1,8 @@ import "graphile-config"; import type { - DefaultPgCodec, - DefaultPgSelectSingleStep, + GenericPgCodec, + GenericPgSelectSingleStep, PgCodec, PgSelectSingleStep, } from "@dataplan/pg"; @@ -40,7 +40,7 @@ declare global { interface ScopeScalar { isPgJwtType?: boolean; - pgCodec?: DefaultPgCodec; + pgCodec?: GenericPgCodec; } } } @@ -190,7 +190,7 @@ export const PgJWTPlugin: GraphileConfig.Plugin = { plan: EXPORTABLE( () => function plan($in) { - const $record = $in as DefaultPgSelectSingleStep; + const $record = $in as GenericPgSelectSingleStep; return $record.record(); }, [], diff --git a/graphile-build/graphile-build-pg/src/plugins/PgMutationPayloadEdgePlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgMutationPayloadEdgePlugin.ts index 7f9950d8ef..f93945c0c3 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgMutationPayloadEdgePlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgMutationPayloadEdgePlugin.ts @@ -1,6 +1,6 @@ import "graphile-config"; -import type { DefaultPgCodec, PgClassSingleStep } from "@dataplan/pg"; +import type { GenericPgCodec, PgClassSingleStep } from "@dataplan/pg"; import { PgDeleteSingleStep, pgSelectFromRecord } from "@dataplan/pg"; import type { FieldArgs, FieldInfo, ObjectStep } from "grafast"; import { connection, constant, EdgeStep, first } from "grafast"; @@ -14,7 +14,7 @@ import { applyOrderToPlan } from "./PgConnectionArgOrderByPlugin.js"; declare global { namespace GraphileBuild { interface Inflection { - tableEdgeField(this: Inflection, codec: DefaultPgCodec): string; + tableEdgeField(this: Inflection, codec: GenericPgCodec): string; } interface ScopeObjectFieldsField { diff --git a/graphile-build/graphile-build-pg/src/plugins/PgMutationUpdateDeletePlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgMutationUpdateDeletePlugin.ts index 6f13f71e5e..f52e654593 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgMutationUpdateDeletePlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgMutationUpdateDeletePlugin.ts @@ -1,10 +1,10 @@ import "graphile-config"; import type { - DefaultPgDeleteSingleStep, - DefaultPgResource, - DefaultPgResourceUnique, - DefaultPgUpdateSingleStep, + GenericPgDeleteSingleStep, + GenericPgResource, + GenericPgResourceUnique, + GenericPgUpdateSingleStep, PgClassSingleStep, PgCodecWithAttributes, PgDeleteSingleStep, @@ -35,7 +35,7 @@ declare global { interface ScopeObject { isPgUpdatePayloadType?: boolean; isPgDeletePayloadType?: boolean; - pgTypeResource?: DefaultPgResource; + pgTypeResource?: GenericPgResource; } interface ScopeObjectFieldsField { @@ -49,88 +49,88 @@ declare global { isPgDeleteInputType?: boolean; isPgDeleteByKeysInputType?: boolean; isPgDeleteNodeInputType?: boolean; - pgResource?: DefaultPgResource; - pgResourceUnique?: DefaultPgResourceUnique; + pgResource?: GenericPgResource; + pgResourceUnique?: GenericPgResourceUnique; } interface Inflection { updatePayloadType( this: Inflection, details: { - resource: DefaultPgResource; + resource: GenericPgResource; }, ): string; deletePayloadType( this: Inflection, details: { - resource: DefaultPgResource; + resource: GenericPgResource; }, ): string; updateNodeField( this: Inflection, details: { - resource: DefaultPgResource; - unique: DefaultPgResourceUnique; + resource: GenericPgResource; + unique: GenericPgResourceUnique; }, ): string; updateNodeInputType( this: Inflection, details: { - resource: DefaultPgResource; - unique: DefaultPgResourceUnique; + resource: GenericPgResource; + unique: GenericPgResourceUnique; }, ): string; deletedNodeId( this: Inflection, details: { - resource: DefaultPgResource; + resource: GenericPgResource; }, ): string; deleteNodeField( this: Inflection, details: { - resource: DefaultPgResource; - unique: DefaultPgResourceUnique; + resource: GenericPgResource; + unique: GenericPgResourceUnique; }, ): string; deleteNodeInputType( this: Inflection, details: { - resource: DefaultPgResource; - unique: DefaultPgResourceUnique; + resource: GenericPgResource; + unique: GenericPgResourceUnique; }, ): string; updateByKeysField( this: Inflection, details: { - resource: DefaultPgResource; - unique: DefaultPgResourceUnique; + resource: GenericPgResource; + unique: GenericPgResourceUnique; }, ): string; updateByKeysInputType( this: Inflection, details: { - resource: DefaultPgResource; - unique: DefaultPgResourceUnique; + resource: GenericPgResource; + unique: GenericPgResourceUnique; }, ): string; deleteByKeysField( this: Inflection, details: { - resource: DefaultPgResource; - unique: DefaultPgResourceUnique; + resource: GenericPgResource; + unique: GenericPgResourceUnique; }, ): string; deleteByKeysInputType( this: Inflection, details: { - resource: DefaultPgResource; - unique: DefaultPgResourceUnique; + resource: GenericPgResource; + unique: GenericPgResourceUnique; }, ): string; @@ -251,7 +251,7 @@ export const PgMutationUpdateDeletePlugin: GraphileConfig.Plugin = { } = build; const process = ( - resource: DefaultPgResource, + resource: GenericPgResource, mode: "resource:update" | "resource:delete", ) => { const modeText = mode === "resource:update" ? "update" : "delete"; @@ -341,8 +341,8 @@ export const PgMutationUpdateDeletePlugin: GraphileConfig.Plugin = { function plan( $object: ObjectStep<{ result: - | DefaultPgUpdateSingleStep - | DefaultPgDeleteSingleStep; + | GenericPgUpdateSingleStep + | GenericPgDeleteSingleStep; }>, ) { return $object.get("result"); @@ -535,7 +535,7 @@ export const PgMutationUpdateDeletePlugin: GraphileConfig.Plugin = { () => function plan( $object: ObjectStep<{ - result: DefaultPgUpdateSingleStep; + result: GenericPgUpdateSingleStep; }>, ) { const $record = @@ -750,8 +750,8 @@ export const PgMutationUpdateDeletePlugin: GraphileConfig.Plugin = { _: any, $object: ObjectStep<{ result: - | DefaultPgUpdateSingleStep - | DefaultPgDeleteSingleStep; + | GenericPgUpdateSingleStep + | GenericPgDeleteSingleStep; }>, ) { return $object; @@ -878,11 +878,11 @@ return (_$root, args) => { function getSpecs( build: GraphileBuild.Build, - resource: DefaultPgResource, + resource: GenericPgResource, mode: "resource:update" | "resource:delete", ) { const primaryUnique = resource.uniques.find( - (u: DefaultPgResourceUnique) => u.isPrimary, + (u: GenericPgResourceUnique) => u.isPrimary, ); const constraintMode = `constraint:${mode}`; const specs = [ @@ -892,13 +892,13 @@ function getSpecs( ? [{ unique: primaryUnique, uniqueMode: "node" }] : []), ...resource.uniques - .filter((unique: DefaultPgResourceUnique) => { + .filter((unique: GenericPgResourceUnique) => { return build.behavior.pgResourceUniqueMatches( [resource, unique], constraintMode, ); }) - .map((unique: DefaultPgResourceUnique) => ({ + .map((unique: GenericPgResourceUnique) => ({ unique, uniqueMode: "keys", })), diff --git a/graphile-build/graphile-build-pg/src/plugins/PgNodeIdAttributesPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgNodeIdAttributesPlugin.ts index 631bebf136..481e7aea62 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgNodeIdAttributesPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgNodeIdAttributesPlugin.ts @@ -3,8 +3,8 @@ import "../interfaces.js"; import "graphile-config"; import type { - DefaultPgCodec, - DefaultPgRegistry, + GenericPgCodec, + GenericPgRegistry, PgConditionStep, PgSelectStep, } from "@dataplan/pg"; @@ -23,8 +23,8 @@ declare global { nodeIdAttribute( this: Inflection, details: { - registry: DefaultPgRegistry; - codec: DefaultPgCodec; + registry: GenericPgRegistry; + codec: GenericPgCodec; relationName: string; }, ): string; diff --git a/graphile-build/graphile-build-pg/src/plugins/PgOrderAllAttributesPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgOrderAllAttributesPlugin.ts index c9c5a3d2fa..b9d54df9e0 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgOrderAllAttributesPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgOrderAllAttributesPlugin.ts @@ -1,7 +1,7 @@ import "./PgTablesPlugin.js"; import "graphile-config"; -import type { DefaultPgCodec, DefaultPgCodecAttribute } from "@dataplan/pg"; +import type { GenericPgCodec, GenericPgCodecAttribute } from "@dataplan/pg"; import { PgSelectStep, PgUnionAllStep } from "@dataplan/pg"; import type { ExecutableStep, ModifierStep } from "grafast"; import type { GraphQLEnumValueConfigMap } from "grafast/graphql"; @@ -15,9 +15,9 @@ declare global { orderByAttributeEnum( this: Inflection, details: { - codec: DefaultPgCodec; + codec: GenericPgCodec; attributeName: string; - attribute: DefaultPgCodecAttribute; + attribute: GenericPgCodecAttribute; variant: "asc" | "desc" | "asc_nulls_last" | "desc_nulls_last"; }, ): string; diff --git a/graphile-build/graphile-build-pg/src/plugins/PgOrderByPrimaryKeyPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgOrderByPrimaryKeyPlugin.ts index 84b346144e..5cda4cd13a 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgOrderByPrimaryKeyPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgOrderByPrimaryKeyPlugin.ts @@ -1,7 +1,7 @@ import "./PgTablesPlugin.js"; import "graphile-config"; -import type { DefaultPgSelectStep } from "@dataplan/pg"; +import type { GenericPgSelectStep } from "@dataplan/pg"; import { EXPORTABLE } from "graphile-build"; import { version } from "../version.js"; @@ -58,7 +58,7 @@ export const PgOrderByPrimaryKeyPlugin: GraphileConfig.Plugin = { grafast: { applyPlan: EXPORTABLE( (pgCodec, pgOrderByNullsLast, primaryKeyAttributes, sql) => - (step: DefaultPgSelectStep) => { + (step: GenericPgSelectStep) => { primaryKeyAttributes.forEach((attributeName) => { const attribute = pgCodec.attributes![attributeName]; step.orderBy({ @@ -86,7 +86,7 @@ export const PgOrderByPrimaryKeyPlugin: GraphileConfig.Plugin = { grafast: { applyPlan: EXPORTABLE( (pgCodec, pgOrderByNullsLast, primaryKeyAttributes, sql) => - (step: DefaultPgSelectStep) => { + (step: GenericPgSelectStep) => { primaryKeyAttributes.forEach((attributeName) => { const attribute = pgCodec.attributes![attributeName]; step.orderBy({ diff --git a/graphile-build/graphile-build-pg/src/plugins/PgOrderCustomFieldsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgOrderCustomFieldsPlugin.ts index 7a4ca67b08..4f9bb5826d 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgOrderCustomFieldsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgOrderCustomFieldsPlugin.ts @@ -2,8 +2,8 @@ import "./PgTablesPlugin.js"; import "graphile-config"; import type { - DefaultPgResource, - DefaultPgSelectStep, + GenericPgResource, + GenericPgSelectStep, } from "@dataplan/pg"; import { EXPORTABLE } from "graphile-build"; @@ -16,7 +16,7 @@ declare global { computedAttributeOrder( this: Inflection, details: { - resource: DefaultPgResource; + resource: GenericPgResource; variant: "asc" | "desc" | "asc_nulls_last" | "desc_nulls_last"; }, ): string; @@ -95,7 +95,7 @@ export const PgOrderCustomFieldsPlugin: GraphileConfig.Plugin = { grafast: { applyPlan: EXPORTABLE( (ascDesc, pgFieldSource, sql) => - (step: DefaultPgSelectStep) => { + (step: GenericPgSelectStep) => { if (typeof pgFieldSource.from !== "function") { throw new Error( "Invalid computed attribute 'from'", diff --git a/graphile-build/graphile-build-pg/src/plugins/PgPolymorphismPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgPolymorphismPlugin.ts index 9dcbd0deef..44df73da57 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgPolymorphismPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgPolymorphismPlugin.ts @@ -5,9 +5,9 @@ import "./PgRelationsPlugin.js"; import "./PgTablesPlugin.js"; import type { - DefaultPgCodec, - DefaultPgRelation, - DefaultPgSelectSingleStep, + GenericPgCodec, + GenericPgRelation, + GenericPgSelectSingleStep, PgCodecExtensions, PgCodecPolymorphism, PgCodecPolymorphismRelational, @@ -56,14 +56,14 @@ declare global { } namespace GraphileBuild { interface Build { - nodeIdSpecForCodec(codec: DefaultPgCodec): + nodeIdSpecForCodec(codec: GenericPgCodec): | (($nodeId: ExecutableStep) => { [key: string]: ExecutableStep; }) | null; } interface ScopeInterface { - pgCodec?: DefaultPgCodec; + pgCodec?: GenericPgCodec; isPgPolymorphicTableType?: boolean; pgPolymorphism?: PgCodecPolymorphism; } @@ -732,7 +732,7 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = { const resourceTypeName = build.inflection.tableType( resource.codec, ); - const relations: Record = + const relations: Record = resource.getRelations(); const pk = resource.uniques?.find((u) => u.isPrimary); if (pk) { @@ -970,7 +970,7 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = { setGraphQLTypeForPgCodec, grafast: { list, constant, access }, } = build; - const unionsToRegister = new Map(); + const unionsToRegister = new Map(); for (const codec of build.pgCodecMetaLookup.keys()) { if (!codec.attributes) { // Only apply to codecs that define attributes @@ -1115,7 +1115,7 @@ return function (list, constant) { ) : EXPORTABLE( (constant, list, pk, tableTypeName) => - ($record: DefaultPgSelectSingleStep) => { + ($record: GenericPgSelectSingleStep) => { return list([ constant(tableTypeName, false), ...pk.map((attribute) => diff --git a/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts index 5153009794..269e37bd12 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts @@ -3,11 +3,11 @@ // (e.g. they can be relations to other tables), so we've renamed them. import type { - DefaultPgCodec, - DefaultPgCodecAttributesRecord, - DefaultPgFunctionResourceOptions, - DefaultPgResourceOptions, - DefaultPgResourceParameter, + GenericPgCodec, + GenericPgCodecAttributesRecord, + GenericPgFunctionResourceOptions, + GenericPgResourceOptions, + GenericPgResourceParameter, PgResourceExtensions, PgSelectArgumentDigest, } from "@dataplan/pg"; @@ -64,7 +64,7 @@ declare global { getResourceOptions( serviceName: string, pgProc: PgProc, - ): Promise; + ): Promise; }; } @@ -73,17 +73,17 @@ declare global { serviceName: string; pgProc: PgProc; baseResourceOptions: Pick< - DefaultPgResourceOptions, + GenericPgResourceOptions, "codec" | "executor" > & - Partial>; - functionResourceOptions: DefaultPgFunctionResourceOptions; + Partial>; + functionResourceOptions: GenericPgFunctionResourceOptions; }): void | Promise; pgProcedures_PgResourceOptions(event: { serviceName: string; pgProc: PgProc; - resourceOptions: DefaultPgResourceOptions; + resourceOptions: GenericPgResourceOptions; }): void | Promise; } } @@ -92,7 +92,7 @@ declare global { interface State { resourceOptionsByPgProcByService: Map< string, - Map> + Map> >; } const EMPTY_OBJECT = Object.freeze({}); @@ -217,12 +217,12 @@ export const PgProceduresPlugin: GraphileConfig.Plugin = { const tags = JSON.parse(JSON.stringify(rawTags)); const makeCodecFromReturn = - async (): Promise => { + async (): Promise => { // We're building a PgCodec to represent specifically the // return type of this function. const numberOfArguments = allArgTypes.length ?? 0; - const attributes: DefaultPgCodecAttributesRecord = + const attributes: GenericPgCodecAttributesRecord = Object.create(null); for (let i = 0, l = numberOfArguments; i < l; i++) { // i for IN arguments, o for OUT arguments, b for INOUT arguments, @@ -317,7 +317,7 @@ export const PgProceduresPlugin: GraphileConfig.Plugin = { return null; } - const parameters: DefaultPgResourceParameter[] = []; + const parameters: GenericPgResourceParameter[] = []; // const processedFirstInputArg = false; @@ -504,7 +504,7 @@ export const PgProceduresPlugin: GraphileConfig.Plugin = { return null; } - const options: DefaultPgFunctionResourceOptions = { + const options: GenericPgFunctionResourceOptions = { name, identifier, from: fromCallback, @@ -541,7 +541,7 @@ export const PgProceduresPlugin: GraphileConfig.Plugin = { [finalResourceOptions, makePgResourceOptions], ); } else { - const options: DefaultPgResourceOptions = EXPORTABLE( + const options: GenericPgResourceOptions = EXPORTABLE( ( description, executor, diff --git a/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts index ee23679ade..302c35a683 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts @@ -2,7 +2,7 @@ import type { _AnyPgCodec, _AnyPgCodecRelationConfig, _AnyPgResourceOptions, - DefaultPgCodecRelationConfig, + GenericPgCodecRelationConfig, PgCodec, PgCodecExtensions, PgCodecRefPath, @@ -258,7 +258,7 @@ export const PgRefsPlugin: GraphileConfig.Plugin = { const parts = via.split(";"); let currentResourceOptions = resourceOptions; for (const rawPart of parts) { - type RelationEntry = [string, DefaultPgCodecRelationConfig]; + type RelationEntry = [string, GenericPgCodecRelationConfig]; const relations = registryConfig.pgRelations[currentResourceOptions.codec.name]; const relationEntries = relations diff --git a/graphile-build/graphile-build-pg/src/plugins/PgRegistryPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgRegistryPlugin.ts index a0313d00da..ca994643e3 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgRegistryPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgRegistryPlugin.ts @@ -1,6 +1,6 @@ /* eslint-disable graphile-export/export-instances */ import type { - DefaultPgRegistry, + GenericPgRegistry, PgRegistry, PgRegistryBuilder, DefaultRegistryBuilder, @@ -17,7 +17,7 @@ declare global { interface GatherHelpers { pgRegistry: { getRegistryBuilder(): PromiseOrDirect; - getRegistry(): PromiseOrDirect; + getRegistry(): PromiseOrDirect; }; } @@ -38,7 +38,7 @@ declare global { registryBuilder: DefaultRegistryBuilder; }): PromiseOrDirect; pgRegistry_PgRegistry(event: { - registry: DefaultPgRegistry; + registry: GenericPgRegistry; }): PromiseOrDirect; } } diff --git a/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts index 1330350aa5..205301ccbf 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts @@ -2,12 +2,12 @@ import "./PgTablesPlugin.js"; import "graphile-config"; import type { - DefaultPgCodec, - DefaultPgCodecRelationConfig, - DefaultPgRegistry, - DefaultPgResource, - DefaultPgResourceOptions, - DefaultPgSelectSingleStep, + GenericPgCodec, + GenericPgCodecRelationConfig, + GenericPgRegistry, + GenericPgResource, + GenericPgResourceOptions, + GenericPgSelectSingleStep, PgCodecRef, PgCodecRefPath, PgCodecRelationConfig, @@ -39,8 +39,8 @@ declare global { pgMutationPayloadRelations?: boolean; } interface PgRelationsPluginRelationDetails { - registry: DefaultPgRegistry; - codec: DefaultPgCodec; + registry: GenericPgRegistry; + codec: GenericPgCodec; relationName: string; } @@ -107,7 +107,7 @@ declare global { event: { pgClass: PgClass; serviceName: string; - resourceOptions: DefaultPgResourceOptions; + resourceOptions: GenericPgResourceOptions; }, pgConstraint: PgConstraint, isReferencee?: boolean, @@ -119,7 +119,7 @@ declare global { serviceName: string; pgClass: PgClass; pgConstraint: PgConstraint; - relation: DefaultPgCodecRelationConfig; + relation: GenericPgCodecRelationConfig; }): Promise | void; } } @@ -567,7 +567,7 @@ function makeSpecString( function makeRelationPlans( localAttributes: readonly string[], remoteAttributes: readonly string[], - otherSource: DefaultPgResource, + otherSource: GenericPgResource, isMutationPayload: boolean, ) { const recordOrResult = isMutationPayload @@ -591,7 +591,7 @@ function makeRelationPlans( const specFromRecord = EXPORTABLE( (localAttributes, remoteAttributes) => - ($record: DefaultPgSelectSingleStep) => { + ($record: GenericPgSelectSingleStep) => { return remoteAttributes.reduce((memo, remoteAttributeName, i) => { memo[remoteAttributeName] = $record.get(localAttributes[i] as string); return memo; @@ -600,7 +600,7 @@ function makeRelationPlans( [localAttributes, remoteAttributes], ); type MutationPayload = ObjectStep<{ - result: DefaultPgSelectSingleStep; + result: GenericPgSelectSingleStep; }>; const singleRecordPlan = @@ -625,7 +625,7 @@ return function (otherSource) { ) : EXPORTABLE( (otherSource, specFromRecord) => - function plan($record: DefaultPgSelectSingleStep) { + function plan($record: GenericPgSelectSingleStep) { return otherSource.get(specFromRecord($record)); }, [otherSource, specFromRecord], @@ -652,7 +652,7 @@ return function (otherSource) { ) : EXPORTABLE( (otherSource, specFromRecord) => - function plan($record: DefaultPgSelectSingleStep) { + function plan($record: GenericPgSelectSingleStep) { return otherSource.find(specFromRecord($record)); }, [otherSource, specFromRecord], @@ -682,7 +682,7 @@ return function (otherSource, connection) { ) : EXPORTABLE( (connection, otherSource, specFromRecord) => - function plan($record: DefaultPgSelectSingleStep) { + function plan($record: GenericPgSelectSingleStep) { return connection(otherSource.find(specFromRecord($record))); }, [connection, otherSource, specFromRecord], @@ -747,7 +747,7 @@ function addRelations( refName: string; refDefinition: PgRefDefinition; ref?: PgCodecRef; - codec?: DefaultPgCodec; + codec?: GenericPgCodec; }> = isMutationPayload ? [] : codec.refs @@ -783,8 +783,8 @@ function addRelations( listFieldName: string; connectionFieldName: string; description?: string; - pgResource?: DefaultPgResource; - pgCodec: DefaultPgCodec | undefined; + pgResource?: GenericPgResource; + pgCodec: GenericPgCodec | undefined; pgRelationDetails?: GraphileBuild.PgRelationsPluginRelationDetails; relatedTypeName: string; isNonNull: boolean | undefined; @@ -909,8 +909,8 @@ function addRelations( } of refDefinitionList) { const isUnique = !!refSpec.singular; let hasReferencee; - let sharedCodec: DefaultPgCodec | undefined = undefined; - let sharedSource: DefaultPgResource | undefined = undefined; + let sharedCodec: GenericPgCodec | undefined = undefined; + let sharedSource: GenericPgResource | undefined = undefined; let behavior: string; let typeName: string | null | undefined; let singleRecordPlan; @@ -1132,7 +1132,7 @@ function addRelations( const isConnection = mode === "connection"; const attributes: PgUnionAllStepConfigAttributes = unionAttributes ?? {}; - const resourceByTypeName: Record = + const resourceByTypeName: Record = Object.create(null); const members: PgUnionAllStepMember[] = []; for (const path of paths) { @@ -1172,10 +1172,10 @@ function addRelations( const $record = isMutationPayload ? ( $parent as ObjectStep<{ - result: DefaultPgSelectSingleStep; + result: GenericPgSelectSingleStep; }> ).get("result") - : ($parent as DefaultPgSelectSingleStep); + : ($parent as GenericPgSelectSingleStep); for (let i = 0, l = paths.length; i < l; i++) { const path = paths[i]; const firstLayer = path.layers[0]; diff --git a/graphile-build/graphile-build-pg/src/plugins/PgRowByUniquePlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgRowByUniquePlugin.ts index 1072973eec..70cab5441a 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgRowByUniquePlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgRowByUniquePlugin.ts @@ -1,9 +1,9 @@ import "graphile-config"; import type { - DefaultPgCodec, - DefaultPgResource, - DefaultPgResourceUnique, + GenericPgCodec, + GenericPgResource, + GenericPgResourceUnique, } from "@dataplan/pg"; import type { FieldArgs } from "grafast"; import { EXPORTABLE } from "graphile-build"; @@ -18,8 +18,8 @@ declare global { rowByUnique( this: Inflection, details: { - unique: DefaultPgResourceUnique; - resource: DefaultPgResource; + unique: GenericPgResourceUnique; + resource: GenericPgResource; }, ): string; } @@ -101,7 +101,7 @@ export const PgRowByUniquePlugin: GraphileConfig.Plugin = { const detailsByAttributeName: { [attributeName: string]: { graphqlName: string; - codec: DefaultPgCodec; + codec: GenericPgCodec; }; } = Object.create(null); uniqueKeys.forEach((attributeName) => { diff --git a/graphile-build/graphile-build-pg/src/plugins/PgTableNodePlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgTableNodePlugin.ts index bd44055d99..3ad05875ff 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgTableNodePlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgTableNodePlugin.ts @@ -2,8 +2,8 @@ import "graphile-config"; import type { _AnyPgSelectSingleStep, - DefaultPgCodec, - DefaultPgResource, + GenericPgCodec, + GenericPgResource, } from "@dataplan/pg"; import type { ListStep } from "grafast"; import { access, constant, list } from "grafast"; @@ -76,7 +76,7 @@ export const PgTableNodePlugin: GraphileConfig.Plugin = { ); }); - const resourcesByCodec = new Map(); + const resourcesByCodec = new Map(); for (const resource of tableResources) { let list = resourcesByCodec.get(resource.codec); if (!list) { diff --git a/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts index 0055e3c0c9..6545cf7498 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts @@ -1,12 +1,12 @@ import type { _AnyPgCodecAttribute, _AnyPgResource, - DefaultPgResourceOptions, + GenericPgResourceOptions, _AnyPgResourceUnique, PgCodec, PgResource, _AnyPgResourceOptions, - DefaultPgCodec, + GenericPgCodec, } from "@dataplan/pg"; import { assertPgClassSingleStep, makePgResourceOptions } from "@dataplan/pg"; import { object } from "grafast"; @@ -90,7 +90,7 @@ declare global { */ _codecName( this: Inflection, - codec: DefaultPgCodec, + codec: GenericPgCodec, ): string; /** @@ -106,7 +106,7 @@ declare global { */ _singularizedCodecName( this: Inflection, - codec: DefaultPgCodec, + codec: GenericPgCodec, ): string; /** @@ -122,17 +122,17 @@ declare global { */ tableType( this: GraphileBuild.Inflection, - codec: DefaultPgCodec, + codec: GenericPgCodec, ): string; tableConnectionType( this: GraphileBuild.Inflection, - codec: DefaultPgCodec, + codec: GenericPgCodec, ): string; tableEdgeType( this: GraphileBuild.Inflection, - codec: DefaultPgCodec, + codec: GenericPgCodec, ): string; patchType(this: GraphileBuild.Inflection, typeName: string): string; @@ -140,20 +140,20 @@ declare global { } interface ScopeObject { - pgCodec?: DefaultPgCodec; + pgCodec?: GenericPgCodec; isPgClassType?: boolean; isPgConnectionRelated?: true; } interface ScopeObjectFieldsField { pgFieldResource?: PgResource; - pgFieldCodec?: DefaultPgCodec; + pgFieldCodec?: GenericPgCodec; pgFieldAttribute?: _AnyPgCodecAttribute; isPgFieldConnection?: boolean; isPgFieldSimpleCollection?: boolean; } interface ScopeInterfaceFieldsField { pgFieldResource?: PgResource; - pgFieldCodec?: DefaultPgCodec; + pgFieldCodec?: GenericPgCodec; pgFieldAttribute?: _AnyPgCodecAttribute; isPgFieldConnection?: boolean; isPgFieldSimpleCollection?: boolean; @@ -168,7 +168,7 @@ declare global { getResourceOptions( serviceName: string, pgClass: PgClass, - ): Promise; + ): Promise; }; } @@ -188,17 +188,17 @@ declare global { pgTables_PgResourceOptions(event: { serviceName: string; pgClass: PgClass; - resourceOptions: DefaultPgResourceOptions; + resourceOptions: GenericPgResourceOptions; }): void | Promise; pgTables_PgResourceOptions_relations(event: { serviceName: string; pgClass: PgClass; - resourceOptions: DefaultPgResourceOptions; + resourceOptions: GenericPgResourceOptions; }): Promise | void; pgTables_PgResourceOptions_relations_post(event: { serviceName: string; pgClass: PgClass; - resourceOptions: DefaultPgResourceOptions; + resourceOptions: GenericPgResourceOptions; }): Promise | void; } } @@ -207,11 +207,11 @@ declare global { interface State { resourceOptionsByPgClassByService: Map< string, - Map> + Map> >; - resourceByResourceOptions: Map>; + resourceByResourceOptions: Map>; detailsByResourceOptions: Map< - DefaultPgResourceOptions, + GenericPgResourceOptions, { serviceName: string; pgClass: PgClass } >; } diff --git a/graphile-build/graphile-utils/src/makeAddPgTableConditionPlugin.ts b/graphile-build/graphile-utils/src/makeAddPgTableConditionPlugin.ts index 375e070f87..66de9fa882 100644 --- a/graphile-build/graphile-utils/src/makeAddPgTableConditionPlugin.ts +++ b/graphile-build/graphile-utils/src/makeAddPgTableConditionPlugin.ts @@ -1,5 +1,5 @@ import type { - DefaultPgSelectStep, + GenericPgSelectStep, PgConditionStep, PgSelectStep, } from "@dataplan/pg"; @@ -17,7 +17,7 @@ export function makeAddPgTableConditionPlugin( conditionGenerator?: ( value: FieldArgs, helpers: { - $condition: PgConditionStep; + $condition: PgConditionStep; sql: typeof sql; sqlTableAlias: SQL; build: GraphileBuild.Build; @@ -89,7 +89,7 @@ export function makeAddPgTableConditionPlugin( conditionFieldSpec.applyPlan = EXPORTABLE( (build, conditionGenerator, sql) => function applyPlan( - $condition: PgConditionStep, + $condition: PgConditionStep, val, ) { const expression = conditionGenerator!(val, { diff --git a/graphile-build/graphile-utils/src/makeAddPgTableOrderByPlugin.ts b/graphile-build/graphile-utils/src/makeAddPgTableOrderByPlugin.ts index 95638be761..fb5898165c 100644 --- a/graphile-build/graphile-utils/src/makeAddPgTableOrderByPlugin.ts +++ b/graphile-build/graphile-utils/src/makeAddPgTableOrderByPlugin.ts @@ -1,17 +1,17 @@ -import type { DefaultPgSelectStep, PgOrderSpec, PgSelectStep } from "@dataplan/pg"; +import type { GenericPgSelectStep, PgOrderSpec, PgSelectStep } from "@dataplan/pg"; import { EXPORTABLE } from "./exportable.js"; type OrderBySpecIdentity = | string // Attribute name | Omit // Expression - | (($select: DefaultPgSelectStep) => Omit); // Callback, allows for joins/etc + | (($select: GenericPgSelectStep) => Omit); // Callback, allows for joins/etc export interface MakeAddPgTableOrderByPluginOrders { [orderByEnumValue: string]: { extensions: { grafast: { - applyPlan($select: DefaultPgSelectStep): void; + applyPlan($select: GenericPgSelectStep): void; }; }; }; @@ -127,7 +127,7 @@ export function orderByAscDesc( ? "FIRST" : "LAST"; - type Plan = ($select: DefaultPgSelectStep) => void; + type Plan = ($select: GenericPgSelectStep) => void; let spec: PgOrderSpec; const ascendingPlan: Plan = diff --git a/postgraphile/postgraphile/src/plugins/PgV4BehaviorPlugin.ts b/postgraphile/postgraphile/src/plugins/PgV4BehaviorPlugin.ts index 62373de680..9214615470 100644 --- a/postgraphile/postgraphile/src/plugins/PgV4BehaviorPlugin.ts +++ b/postgraphile/postgraphile/src/plugins/PgV4BehaviorPlugin.ts @@ -1,7 +1,7 @@ import "graphile-config"; import "graphile-build-pg"; -import type { DefaultPgResourceOptions, PgResourceOptions } from "@dataplan/pg"; +import type { GenericPgResourceOptions, PgResourceOptions } from "@dataplan/pg"; import type { PgProc } from "graphile-build-pg/pg-introspection"; import { inspect } from "util"; @@ -14,7 +14,7 @@ declare global { } const v4ComputedAttributeChecks = ( - _s: DefaultPgResourceOptions, + _s: GenericPgResourceOptions, pgProc: PgProc, ): boolean => { const args = pgProc.getArguments(); From 35dc2e464b2de94770533baf689f9de68088ae31 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Thu, 23 Nov 2023 11:17:21 +0000 Subject: [PATCH 6/8] Linting --- grafast/dataplan-pg/src/codecs.ts | 5 +- grafast/dataplan-pg/src/datasource.ts | 66 ++--- .../dataplan-pg/src/examples/exampleSchema.ts | 28 +- .../src/filters/pgBooleanFilter.ts | 2 +- .../dataplan-pg/src/filters/pgClassFilter.ts | 2 +- grafast/dataplan-pg/src/filters/pgOrFilter.ts | 7 +- grafast/dataplan-pg/src/index.ts | 242 +++++++++--------- grafast/dataplan-pg/src/interfaces.ts | 24 +- grafast/dataplan-pg/src/pgLocker.ts | 4 +- .../src/steps/pgClassExpression.ts | 4 +- grafast/dataplan-pg/src/steps/pgCondition.ts | 2 +- .../dataplan-pg/src/steps/pgDeleteSingle.ts | 12 +- .../dataplan-pg/src/steps/pgInsertSingle.ts | 6 +- grafast/dataplan-pg/src/steps/pgSelect.ts | 6 +- .../dataplan-pg/src/steps/pgSelectSingle.ts | 3 +- grafast/dataplan-pg/src/steps/pgUnionAll.ts | 2 +- .../dataplan-pg/src/steps/pgUpdateSingle.ts | 2 +- grafast/dataplan-pg/src/steps/toPg.ts | 2 +- .../src/examples/NO_DATA_GATHERING.ts | 6 +- .../graphile-build-pg/src/interfaces.ts | 2 +- .../src/plugins/PgCodecsPlugin.ts | 1 - .../src/plugins/PgConditionArgumentPlugin.ts | 3 - .../plugins/PgConditionCustomFieldsPlugin.ts | 1 - .../plugins/PgConnectionArgOrderByPlugin.ts | 1 - .../src/plugins/PgCustomTypeFieldPlugin.ts | 9 +- .../PgInterfaceModeUnionAllRowsPlugin.ts | 11 +- .../src/plugins/PgJWTPlugin.ts | 7 +- .../plugins/PgMutationUpdateDeletePlugin.ts | 4 - .../src/plugins/PgOrderCustomFieldsPlugin.ts | 5 +- .../src/plugins/PgRefsPlugin.ts | 7 +- .../src/plugins/PgRegistryPlugin.ts | 4 +- .../src/plugins/PgRelationsPlugin.ts | 2 - .../src/plugins/PgTablesPlugin.ts | 29 +-- graphile-build/graphile-build-pg/src/utils.ts | 2 +- .../graphile-simplify-inflection/src/index.ts | 2 +- .../src/makeAddPgTableConditionPlugin.ts | 6 +- .../src/makeAddPgTableOrderByPlugin.ts | 2 +- .../src/plugins/PgV4BehaviorPlugin.ts | 2 +- .../postgraphile/src/presets/relay.ts | 2 - .../src/commands/behavior/debug/main.ts | 1 - 40 files changed, 241 insertions(+), 287 deletions(-) diff --git a/grafast/dataplan-pg/src/codecs.ts b/grafast/dataplan-pg/src/codecs.ts index dd68511079..3e923535f5 100644 --- a/grafast/dataplan-pg/src/codecs.ts +++ b/grafast/dataplan-pg/src/codecs.ts @@ -42,8 +42,6 @@ import type { GenericPgCodec, PgCodec, PgCodecAttributeMap, - PgCodecAttributes, - PgCodecAttributesRecord, PgCodecExtensions, PgCodecFromPostgres, PgCodecPolymorphism, @@ -64,7 +62,8 @@ export type PgCodecAttributeViaExplicit< attribute: TAttribute; }; /** @internal */ -export interface _AnyPgCodecAttributeVia extends PgCodecAttributeVia {} +export interface _AnyPgCodecAttributeVia + extends PgCodecAttributeVia {} export type GenericPgCodecAttributeVia = PgCodecAttributeVia; /** @internal */ export type _AnyPgCodecAttributeViaRelationName = diff --git a/grafast/dataplan-pg/src/datasource.ts b/grafast/dataplan-pg/src/datasource.ts index 9697f96621..d66e0d3570 100644 --- a/grafast/dataplan-pg/src/datasource.ts +++ b/grafast/dataplan-pg/src/datasource.ts @@ -1,3 +1,4 @@ +import chalk from "chalk"; import type { GrafastValuesList, ObjectStep } from "grafast"; import { __ValueStep, @@ -28,29 +29,28 @@ import type { } from "./executor.js"; import { inspect } from "./inspect.js"; import type { + _AnyPgCodec, _AnyPgCodecRelationConfig, - PgCodecAttributes, + _AnyPgRegistry, + _AnyPgRegistryConfig, + _AnyScalarPgCodec, + Expand, + GenericPgCodec, + GenericPgCodecRelationConfig, + GetPgRegistryCodecRelationConfigs, GetPgRegistryCodecRelations, - PgCodec, + GetPgRegistryCodecs, + PgCodecAttributes, PgCodecName, PgCodecRelationConfig, + PgCodecRelationConfigName, PgRefDefinition, PgRegistry, PgRegistryConfig, - PlanByUniques, - _AnyPgCodec, - _AnyPgRegistry, - GetPgRegistryCodecRelationConfigs, - GetPgRegistryCodecs, - _AnyPgRegistryConfig, - _AnyScalarPgCodec, - PgCodecRelationConfigName, PgRegistryConfigCodecs, PgRegistryConfigRelationConfigs, PgRegistryConfigResourceOptions, - Expand, - GenericPgCodec, - GenericPgCodecRelationConfig, + PlanByUniques, } from "./interfaces.js"; import type { PgClassExpressionStep } from "./steps/pgClassExpression.js"; import type { @@ -65,7 +65,6 @@ import type { PgSelectSinglePlanOptions, PgSelectSingleStep, } from "./steps/pgSelectSingle.js"; -import chalk from "chalk"; export function EXPORTABLE( factory: (...args: TScope) => T, @@ -95,7 +94,8 @@ export type PgResourceParameterExtensions = DataplanPg.PgResourceParameterExtensions; /** @internal */ -export interface _AnyPgResourceParameter extends PgResourceParameter {} +export interface _AnyPgResourceParameter + extends PgResourceParameter {} export interface GenericPgResourceParameter extends PgResourceParameter {} @@ -1139,7 +1139,11 @@ export function makeRegistry< })) : rawConfig.parameters, }; - const resource = new PgResource(registry, resourceConfig); + const resource = EXPORTABLE( + (PgResource, registry, resourceConfig) => + new PgResource(registry, resourceConfig), + [PgResource, registry, resourceConfig], + ); // This is the magic that breaks the circular reference: rather than // building PgResource via a factory we tell the system to just retrieve it @@ -1235,19 +1239,23 @@ export function makeRegistry< for (const codec of tableLikeCodecsWithoutTableLikeResources) { if (codec.executor) { const resourceName = `frmcdc_${codec.name}`; - const resource = new PgResource(registry, { - name: resourceName, - executor: codec.executor, - from: sql`(select 1/0 /* codec-only resource; should not select directly */)` as any, - codec, - identifier: resourceName, - isVirtual: true, - extensions: { - tags: { - behavior: "-*", - }, - }, - }); + const resource = EXPORTABLE( + (PgResource, codec, registry, resourceName, sql) => + new PgResource(registry, { + name: resourceName, + executor: codec.executor!, + from: sql`(select 1/0 /* codec-only resource; should not select directly */)` as any, + codec, + identifier: resourceName, + isVirtual: true, + extensions: { + tags: { + behavior: "-*", + }, + }, + }), + [PgResource, codec, registry, resourceName, sql], + ); Object.defineProperties(resource, { $exporter$args: { value: [registry, resourceName] }, diff --git a/grafast/dataplan-pg/src/examples/exampleSchema.ts b/grafast/dataplan-pg/src/examples/exampleSchema.ts index f1c74cf436..3eebd60b6d 100644 --- a/grafast/dataplan-pg/src/examples/exampleSchema.ts +++ b/grafast/dataplan-pg/src/examples/exampleSchema.ts @@ -78,15 +78,13 @@ import type { WithPgClient, } from "../"; import type { NodePostgresPgClient, PgSubscriber } from "../adaptors/pg.js"; -import { listOfCodec, PgCodecAttributeName } from "../codecs.js"; +import type { PgCodecAttributeName } from "../codecs.js"; +import { listOfCodec } from "../codecs.js"; +import type { _AnyPgResource, PgResourceCodec } from "../datasource.js"; import { - _AnyPgResource, makePgResourceOptions, makeRegistry, makeRegistryBuilder, - PgResourceCodec, - PgResourceName, - PgResourceUniques, } from "../datasource.js"; import { enumCodec, @@ -117,23 +115,19 @@ import type { GetPgResourceRelationConfigs, PgCodec, PgCodecAttributeMap, - PgCodecAttributes, PgCodecRelationConfigName, } from "../interfaces"; import { PgPageInfoStep } from "../steps/pgPageInfo.js"; import type { PgPolymorphicTypeMap } from "../steps/pgPolymorphic.js"; import type { PgSelectParsedCursorStep } from "../steps/pgSelect.js"; import { sqlFromArgDigests } from "../steps/pgSelect.js"; +import { _AnyPgSelectSingleStep } from "../steps/pgSelectSingle.js"; import type { PgUnionAllStep } from "../steps/pgUnionAll.js"; import { pgUnionAll, PgUnionAllSingleStep } from "../steps/pgUnionAll.js"; import { WithPgClientStep, withPgClientTransaction, } from "../steps/withPgClient.js"; -import { - _AnyPgSelectSingleStep, - PgSelectSingleStepResource, -} from "../steps/pgSelectSingle"; const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); @@ -1702,12 +1696,13 @@ export function makeExampleSchema( [__ListTransformStep, options], ); - type ResourceConnectionPlan = ConnectionStep< - PgSelectSingleStep, - PgSelectParsedCursorStep, - PgSelectStep, - PgSelectSingleStep - >; + type ResourceConnectionPlan = + ConnectionStep< + PgSelectSingleStep, + PgSelectParsedCursorStep, + PgSelectStep, + PgSelectSingleStep + >; const { pgCodecs: { union__entity: unionEntityCodec }, @@ -1826,7 +1821,6 @@ export function makeExampleSchema( type PersonBookmarkStep = PgSelectSingleStep; type PostStep = PgSelectSingleStep; type CommentStep = PgSelectSingleStep; - type SingleTableItemsStep = PgSelectStep; type SingleTableItemStep = PgSelectSingleStep; type RelationalItemsStep = PgSelectStep; type RelationalItemStep = PgSelectSingleStep; diff --git a/grafast/dataplan-pg/src/filters/pgBooleanFilter.ts b/grafast/dataplan-pg/src/filters/pgBooleanFilter.ts index a51f0c9c32..37bbecf90a 100644 --- a/grafast/dataplan-pg/src/filters/pgBooleanFilter.ts +++ b/grafast/dataplan-pg/src/filters/pgBooleanFilter.ts @@ -2,7 +2,7 @@ import type { ExecutableStep } from "grafast"; import { ModifierStep } from "grafast"; import type { SQL } from "pg-sql2"; -import type { _AnyPgCodec, PgCodec, PgConditionLikeStep } from "../interfaces.js"; +import type { _AnyPgCodec, PgConditionLikeStep } from "../interfaces.js"; export class PgBooleanFilterStep extends ModifierStep { static $$export = { diff --git a/grafast/dataplan-pg/src/filters/pgClassFilter.ts b/grafast/dataplan-pg/src/filters/pgClassFilter.ts index 7b066c8876..15a520ea35 100644 --- a/grafast/dataplan-pg/src/filters/pgClassFilter.ts +++ b/grafast/dataplan-pg/src/filters/pgClassFilter.ts @@ -2,7 +2,7 @@ import type { ExecutableStep } from "grafast"; import { ModifierStep } from "grafast"; import type { SQL } from "pg-sql2"; -import type { _AnyPgCodec, PgCodec } from "../interfaces.js"; +import type { _AnyPgCodec } from "../interfaces.js"; import type { PgConditionCapableParentStep, PgConditionStep, diff --git a/grafast/dataplan-pg/src/filters/pgOrFilter.ts b/grafast/dataplan-pg/src/filters/pgOrFilter.ts index 575f4a01c3..892df12f99 100644 --- a/grafast/dataplan-pg/src/filters/pgOrFilter.ts +++ b/grafast/dataplan-pg/src/filters/pgOrFilter.ts @@ -3,7 +3,7 @@ import { ModifierStep } from "grafast"; import type { SQL } from "pg-sql2"; import { sql } from "pg-sql2"; -import type { _AnyPgCodec, PgCodec, PgConditionLikeStep } from "../interfaces.js"; +import type { _AnyPgCodec, PgConditionLikeStep } from "../interfaces.js"; export class PgOrFilterStep extends ModifierStep { static $$export = { @@ -20,7 +20,10 @@ export class PgOrFilterStep extends ModifierStep { this.alias = $classFilterPlan.alias; } - placeholder($step: ExecutableStep, codec: TCodec): SQL { + placeholder( + $step: ExecutableStep, + codec: TCodec, + ): SQL { return this.$parent.placeholder($step, codec); } diff --git a/grafast/dataplan-pg/src/index.ts b/grafast/dataplan-pg/src/index.ts index 2166a4d15e..603eb2337b 100644 --- a/grafast/dataplan-pg/src/index.ts +++ b/grafast/dataplan-pg/src/index.ts @@ -1,8 +1,14 @@ import { exportAsMany } from "grafast"; import { + _AnyPgCodecAttribute, + _AnyPgCodecAttributeVia, + _AnyPgCodecAttributeViaAttribute, + _AnyPgCodecAttributeViaRelationName, domainOfCodec, enumCodec, + GenericPgCodecAttribute, + GenericPgCodecAttributeVia, getCodecByPgCatalogTypeName, getInnerCodec, isEnumCodec, @@ -17,12 +23,6 @@ import { rangeOfCodec, recordCodec, TYPES, - _AnyPgCodecAttribute, - GenericPgCodecAttribute, - GenericPgCodecAttributeVia, - _AnyPgCodecAttributeVia, - _AnyPgCodecAttributeViaAttribute, - _AnyPgCodecAttributeViaRelationName, } from "./codecs.js"; import { PgBox, @@ -36,6 +36,20 @@ import { PgPolygon, } from "./codecUtils/index.js"; import { + _AnyPgFunctionResourceOptions, + _AnyPgRegistryBuilder, + _AnyPgResource, + _AnyPgResourceOptions, + _AnyPgResourceParameter, + _AnyPgResourceUnique, + _AnyScalarPgResource, + DefaultRegistryBuilder, + EmptyRegistryBuilder, + GenericPgFunctionResourceOptions, + GenericPgResource, + GenericPgResourceOptions, + GenericPgResourceParameter, + GenericPgResourceUnique, makePgResourceOptions, makeRegistry, makeRegistryBuilder, @@ -52,20 +66,6 @@ import { PgResourceParameter, PgResourceUnique, PgResourceUniqueExtensions, - _AnyPgRegistryBuilder, - _AnyPgResource, - _AnyPgResourceOptions, - _AnyPgResourceParameter, - _AnyPgResourceUnique, - _AnyScalarPgResource, - GenericPgResourceOptions, - GenericPgResource, - GenericPgResourceParameter, - GenericPgResourceUnique, - DefaultRegistryBuilder, - EmptyRegistryBuilder, - GenericPgFunctionResourceOptions, - _AnyPgFunctionResourceOptions, } from "./datasource.js"; import { PgClient, @@ -84,7 +84,23 @@ import { PgClassFilterStep } from "./filters/pgClassFilter.js"; import { PgManyFilterStep } from "./filters/pgManyFilter.js"; import { PgOrFilterStep } from "./filters/pgOrFilter.js"; import { - PgCodecAttributes, + _AnyPgCodec, + _AnyPgCodecAttributesRecord, + _AnyPgCodecRelationConfig, + _AnyPgRangeItemCodec, + _AnyPgRegistry, + _AnyPgRegistryConfig, + _AnyPgRelation, + _AnyScalarPgCodec, + DefaultScalarPgCodec, + GenericPgCodec, + GenericPgCodecAttributesRecord, + GenericPgCodecRelationConfig, + GenericPgCodecWithAttributes, + GenericPgRangeItemCodec, + GenericPgRegistry, + GenericPgRegistryConfig, + GenericPgRelation, GetPgRegistryCodecRelations, GetPgRegistryCodecs, GetPgRegistrySources, @@ -98,8 +114,14 @@ import { PgClassSingleStep, PgCodec, PgCodecAnyScalar, + PgCodecAttributes, + PgCodecAttributesRecord, PgCodecExtensions, + PgCodecFromJavaScript, + PgCodecFromPg, + PgCodecFromPostgres, PgCodecList, + PgCodecName, PgCodecPolymorphism, PgCodecPolymorphismRelational, PgCodecPolymorphismRelationalTypeSpec, @@ -107,8 +129,11 @@ import { PgCodecPolymorphismSingleTypeAttributeSpec, PgCodecPolymorphismSingleTypeSpec, PgCodecPolymorphismUnion, - PgRelation, + PgCodecRelationBase, PgCodecRelationConfig, + PgCodecRelationConfigLocalCodec, + PgCodecRelationConfigName, + PgCodecRelationConfigRemoteResourceOptions, PgCodecRelationExtensions, PgCodecWithAttributes, PgConditionLikeStep, @@ -117,32 +142,13 @@ import { PgEnumCodec, PgEnumValue, PgGroupSpec, + PgOrderAttributeSpec, + PgOrderFragmentSpec, PgOrderSpec, PgRefDefinition, PgRefDefinitionExtensions, PgRefDefinitions, PgRegistry, - PgTypedExecutableStep, - PlanByUniques, - TuplePlanMap, - _AnyPgCodec, - _AnyPgCodecAttributesRecord, - _AnyPgCodecRelationConfig, - _AnyPgRegistry, - _AnyPgRegistryConfig, - _AnyScalarPgCodec, - PgCodecAttributesRecord, - PgCodecFromJavaScript, - PgCodecFromPg, - PgCodecFromPostgres, - PgCodecName, - PgCodecRelationBase, - PgCodecRelationConfigLocalCodec, - PgCodecRelationConfigName, - PgCodecRelationConfigRemoteResourceOptions, - PgOrderAttributeSpec, - PgOrderFragmentSpec, - _AnyPgRangeItemCodec, PgRegistryCodecRelations, PgRegistryCodecs, PgRegistryConfig, @@ -152,16 +158,10 @@ import { PgRegistryRelationConfigs, PgRegistryResourceOptions, PgRegistryResources, - GenericPgRelation, - _AnyPgRelation, - GenericPgCodec, - DefaultScalarPgCodec, - GenericPgCodecRelationConfig, - GenericPgCodecAttributesRecord, - GenericPgRangeItemCodec, - GenericPgRegistry, - GenericPgCodecWithAttributes, - GenericPgRegistryConfig, + PgRelation, + PgTypedExecutableStep, + PlanByUniques, + TuplePlanMap, } from "./interfaces.js"; import { PgLockableParameter, PgLockCallback } from "./pgLocker.js"; import { @@ -178,10 +178,10 @@ import { } from "./steps/pgCondition.js"; import { PgCursorStep } from "./steps/pgCursor.js"; import { + _AnyPgDeleteSingleStep, + GenericPgDeleteSingleStep, pgDeleteSingle, PgDeleteSingleStep, - GenericPgDeleteSingleStep, - _AnyPgDeleteSingleStep, } from "./steps/pgDeleteSingle.js"; import { pgInsertSingle, PgInsertSingleStep } from "./steps/pgInsertSingle.js"; import { pgPageInfo, PgPageInfoStep } from "./steps/pgPageInfo.js"; @@ -191,7 +191,9 @@ import { PgPolymorphicTypeMap, } from "./steps/pgPolymorphic.js"; import { + _AnyPgSelectStep, digestsFromArgumentSpecs, + GenericPgSelectStep, pgSelect, PgSelectArgumentDigest, PgSelectArgumentSpec, @@ -202,17 +204,15 @@ import { PgSelectParsedCursorStep, PgSelectStep, sqlFromArgDigests, - GenericPgSelectStep, - _AnyPgSelectStep, } from "./steps/pgSelect.js"; import { + _AnyPgSelectSinglePlanOptions, + _AnyPgSelectSingleStep, + GenericPgSelectSingleStep, pgSelectFromRecord, pgSelectSingleFromRecord, PgSelectSinglePlanOptions, PgSelectSingleStep, - _AnyPgSelectSinglePlanOptions, - _AnyPgSelectSingleStep, - GenericPgSelectSingleStep, } from "./steps/pgSelectSingle.js"; import { pgSingleTablePolymorphic, @@ -230,10 +230,10 @@ import { PgUnionAllStepOrder, } from "./steps/pgUnionAll.js"; import { + _AnyPgUpdateSingleStep, + GenericPgUpdateSingleStep, pgUpdateSingle, PgUpdateSingleStep, - GenericPgUpdateSingleStep, - _AnyPgUpdateSingleStep, } from "./steps/pgUpdateSingle.js"; import { pgValidateParsedCursor, @@ -249,70 +249,55 @@ import { import { assertPgClassSingleStep } from "./utils.js"; export { - GenericPgResourceOptions, - _AnyPgSelectSinglePlanOptions, - GenericPgCodecAttribute, - GenericPgCodecAttributeVia, - _AnyPgSelectSingleStep, - GenericPgResource, - GenericPgResourceParameter, - GenericPgSelectSingleStep, - GenericPgRelation, - _AnyPgRelation, - GenericPgCodecWithAttributes, - GenericPgResourceUnique, - DefaultRegistryBuilder, - EmptyRegistryBuilder, - GenericPgUpdateSingleStep, - _AnyPgUpdateSingleStep, - GenericPgDeleteSingleStep, - _AnyPgDeleteSingleStep, - GenericPgCodec, - _AnyPgRegistryBuilder, - _AnyPgResource, - _AnyPgResourceOptions, - _AnyPgResourceParameter, - _AnyPgResourceUnique, - _AnyScalarPgResource, + _AnyPgCodec, _AnyPgCodecAttribute, + _AnyPgCodecAttributesRecord, _AnyPgCodecAttributeVia, _AnyPgCodecAttributeViaAttribute, _AnyPgCodecAttributeViaRelationName, - _AnyPgCodec, - _AnyPgCodecAttributesRecord, _AnyPgCodecRelationConfig, + _AnyPgDeleteSingleStep, + _AnyPgFunctionResourceOptions, _AnyPgRegistry, + _AnyPgRegistryBuilder, _AnyPgRegistryConfig, + _AnyPgRelation, + _AnyPgResource, + _AnyPgResourceOptions, + _AnyPgResourceParameter, + _AnyPgResourceUnique, + _AnyPgSelectSinglePlanOptions, + _AnyPgSelectSingleStep, + _AnyPgSelectStep, + _AnyPgUpdateSingleStep, _AnyScalarPgCodec, - PgCodecAttributes, - PgCodecAttributesRecord, - PgCodecFromJavaScript, - PgCodecFromPg, - PgCodecFromPostgres, - PgCodecName, - PgCodecRelationBase, - PgCodecRelationConfigLocalCodec, - PgCodecRelationConfigName, - PgCodecRelationConfigRemoteResourceOptions, - PgOrderAttributeSpec, - PgOrderFragmentSpec, - _AnyPgRangeItemCodec as PgRangeItemCodec, - PgRegistryCodecRelations, - PgRegistryCodecs, - PgRegistryConfig, - PgRegistryConfigCodecs, - PgRegistryConfigRelationConfigs, - PgRegistryConfigResourceOptions, - PgRegistryRelationConfigs, - PgRegistryResourceOptions, - PgRegistryResources, - PgRelation, + _AnyScalarPgResource, assertPgClassSingleStep, + DefaultRegistryBuilder, + DefaultScalarPgCodec, digestsFromArgumentSpecs, domainOfCodec, + EmptyRegistryBuilder, enumCodec, + GenericPgCodec, + GenericPgCodecAttribute, + GenericPgCodecAttributesRecord, + GenericPgCodecAttributeVia, + GenericPgCodecRelationConfig, + GenericPgCodecWithAttributes, + GenericPgDeleteSingleStep, GenericPgFunctionResourceOptions, - _AnyPgFunctionResourceOptions, + GenericPgRangeItemCodec, + GenericPgRegistry, + GenericPgRegistryConfig, + GenericPgRelation, + GenericPgResource, + GenericPgResourceOptions, + GenericPgResourceParameter, + GenericPgResourceUnique, + GenericPgSelectSingleStep, + GenericPgSelectStep, + GenericPgUpdateSingleStep, getCodecByPgCatalogTypeName, getInnerCodec, GetPgRegistryCodecRelations, @@ -323,12 +308,6 @@ export { GetPgResourceRegistry, GetPgResourceRelations, GetPgResourceUniques, - DefaultScalarPgCodec, - GenericPgCodecRelationConfig, - GenericPgCodecAttributesRecord, - GenericPgRangeItemCodec, - GenericPgRegistry, - GenericPgRegistryConfig, isEnumCodec, KeysOfType, listOfCodec, @@ -351,10 +330,16 @@ export { PgCodecAnyScalar, PgCodecAttribute, PgCodecAttributeExtensions, + PgCodecAttributes, + PgCodecAttributesRecord, PgCodecAttributeVia, PgCodecAttributeViaExplicit, PgCodecExtensions, + PgCodecFromJavaScript, + PgCodecFromPg, + PgCodecFromPostgres, PgCodecList, + PgCodecName, PgCodecPolymorphism, PgCodecPolymorphismRelational, PgCodecPolymorphismRelationalTypeSpec, @@ -368,7 +353,11 @@ export { PgCodecRefPathEntry, PgCodecRefs, PgRelation as PgCodecRelation, + PgCodecRelationBase, PgCodecRelationConfig, + PgCodecRelationConfigLocalCodec, + PgCodecRelationConfigName, + PgCodecRelationConfigRemoteResourceOptions, PgCodecRelationExtensions, PgCodecWithAttributes, PgConditionCapableParentStep, @@ -385,8 +374,6 @@ export { PgEnumValue, PgExecutor, PgExecutorContext, - _AnyPgSelectStep, - GenericPgSelectStep, PgExecutorContextPlans, PgExecutorInput, PgExecutorMutationOptions, @@ -403,6 +390,8 @@ export { PgLockCallback, PgLseg, PgManyFilterStep, + PgOrderAttributeSpec, + PgOrderFragmentSpec, PgOrderSpec, PgOrFilterStep, pgPageInfo, @@ -413,12 +402,23 @@ export { pgPolymorphic, PgPolymorphicStep, PgPolymorphicTypeMap, + _AnyPgRangeItemCodec as PgRangeItemCodec, PgRecordTypeCodecSpec, PgRefDefinition, PgRefDefinitionExtensions, PgRefDefinitions, PgRegistry, PgRegistryBuilder, + PgRegistryCodecRelations, + PgRegistryCodecs, + PgRegistryConfig, + PgRegistryConfigCodecs, + PgRegistryConfigRelationConfigs, + PgRegistryConfigResourceOptions, + PgRegistryRelationConfigs, + PgRegistryResourceOptions, + PgRegistryResources, + PgRelation, PgResource, PgResourceExtensions, PgResourceOptions, diff --git a/grafast/dataplan-pg/src/interfaces.ts b/grafast/dataplan-pg/src/interfaces.ts index 8a114c729f..26603e52cd 100644 --- a/grafast/dataplan-pg/src/interfaces.ts +++ b/grafast/dataplan-pg/src/interfaces.ts @@ -5,7 +5,6 @@ import type { PgAdaptorOptions } from "./adaptors/pg.js"; import type { _AnyPgCodecAttribute, GenericPgCodecAttribute, - PgCodecAttribute, PgCodecAttributeCodec, PgCodecAttributeName, } from "./codecs.js"; @@ -14,7 +13,6 @@ import type { _AnyPgResourceOptions, _AnyPgResourceParameter, _AnyPgResourceUnique, - GenericPgResource, GenericPgResourceOptions, PgCodecRefs, PgResource, @@ -35,12 +33,13 @@ import type { PgUpdateSingleStep } from "./steps/pgUpdateSingle.js"; * A class-like source of information - could be from `SELECT`-ing a row, or * `INSERT...RETURNING` or similar. *ALWAYS* represents a single row (or null). */ -export type PgClassSingleStep = - - | PgSelectSingleStep - | PgInsertSingleStep - | PgUpdateSingleStep - | PgDeleteSingleStep; +export type PgClassSingleStep< + TResource extends _AnyPgResource = _AnyPgResource, +> = + | PgSelectSingleStep + | PgInsertSingleStep + | PgUpdateSingleStep + | PgDeleteSingleStep; /** * Given a value of type TInput, returns an `SQL` value to insert into an SQL @@ -385,14 +384,7 @@ export interface PgCodecWithAttributes< > extends PgCodec {} export interface PgCodecAnyScalar - extends PgCodec {} + extends PgCodec {} export type PgCodecList< TInnerCodec extends PgCodec = PgCodec< diff --git a/grafast/dataplan-pg/src/pgLocker.ts b/grafast/dataplan-pg/src/pgLocker.ts index 53c7aac973..93c3099532 100644 --- a/grafast/dataplan-pg/src/pgLocker.ts +++ b/grafast/dataplan-pg/src/pgLocker.ts @@ -1,7 +1,7 @@ import { isDev } from "grafast"; -import { _AnyPgSelectStep } from "./steps/pgSelect"; -import { _AnyPgUnionAllStep } from "./steps/pgUnionAll"; +import type { _AnyPgSelectStep } from "./steps/pgSelect"; +import type { _AnyPgUnionAllStep } from "./steps/pgUnionAll"; export type PgLockableParameter = | "orderBy" diff --git a/grafast/dataplan-pg/src/steps/pgClassExpression.ts b/grafast/dataplan-pg/src/steps/pgClassExpression.ts index e464fea130..a78a3a061e 100644 --- a/grafast/dataplan-pg/src/steps/pgClassExpression.ts +++ b/grafast/dataplan-pg/src/steps/pgClassExpression.ts @@ -3,7 +3,8 @@ import { access, exportAs, UnbatchedExecutableStep } from "grafast"; import type { SQL } from "pg-sql2"; import sql from "pg-sql2"; -import type { _AnyPgResource, PgResource } from "../datasource.js"; +import type { PgCodecAttributeCodec, PgCodecAttributeName } from "../codecs.js"; +import type { _AnyPgResource } from "../datasource.js"; import type { _AnyPgCodec, PgClassSingleStep, @@ -15,7 +16,6 @@ import { PgInsertSingleStep } from "./pgInsertSingle.js"; import { PgSelectSingleStep } from "./pgSelectSingle.js"; import { PgUnionAllSingleStep } from "./pgUnionAll.js"; import { PgUpdateSingleStep } from "./pgUpdateSingle.js"; -import { PgCodecAttributeCodec, PgCodecAttributeName } from "../codecs.js"; // const debugPlan = debugFactory("@dataplan/pg:PgClassExpressionStep:plan"); // const debugExecute = debugFactory( "@dataplan/pg:PgClassExpressionStep:execute",); diff --git a/grafast/dataplan-pg/src/steps/pgCondition.ts b/grafast/dataplan-pg/src/steps/pgCondition.ts index c0f9a032ad..a144e11cfe 100644 --- a/grafast/dataplan-pg/src/steps/pgCondition.ts +++ b/grafast/dataplan-pg/src/steps/pgCondition.ts @@ -4,7 +4,7 @@ import type { SQL } from "pg-sql2"; import { sql } from "pg-sql2"; import { TYPES } from "../index.js"; -import type { _AnyPgCodec, PgCodec } from "../interfaces.js"; +import type { _AnyPgCodec } from "../interfaces.js"; export type PgWhereConditionSpec = | SQL diff --git a/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts b/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts index 8c67b5754e..ec997890c3 100644 --- a/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgDeleteSingle.ts @@ -7,6 +7,12 @@ import { ExecutableStep, exportAs, isDev, SafeError } from "grafast"; import type { SQL, SQLRawValue } from "pg-sql2"; import sql from "pg-sql2"; +import type { PgCodecAttributeCodec, PgCodecAttributeName } from "../codecs.js"; +import type { + _AnyPgResource, + GenericPgResource, + PgResourceUnique, +} from "../datasource.js"; import { inspect } from "../inspect.js"; import type { _AnyPgCodec, @@ -17,12 +23,6 @@ import type { } from "../interfaces.js"; import type { PgClassExpressionStep } from "./pgClassExpression.js"; import { pgClassExpression } from "./pgClassExpression.js"; -import { - _AnyPgResource, - GenericPgResource, - PgResourceUnique, -} from "../datasource.js"; -import { PgCodecAttributeCodec, PgCodecAttributeName } from "../codecs.js"; type QueryValueDetailsBySymbol = Map< symbol, diff --git a/grafast/dataplan-pg/src/steps/pgInsertSingle.ts b/grafast/dataplan-pg/src/steps/pgInsertSingle.ts index c92ca68629..8016fb0ca2 100644 --- a/grafast/dataplan-pg/src/steps/pgInsertSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgInsertSingle.ts @@ -15,6 +15,7 @@ import type { PgCodecAttributeCodec, PgCodecAttributeName, } from "../codecs.js"; +import type { _AnyPgResource } from "../datasource.js"; import { inspect } from "../inspect.js"; import type { _AnyPgCodec, @@ -24,7 +25,6 @@ import type { } from "../interfaces.js"; import type { PgClassExpressionStep } from "./pgClassExpression.js"; import { pgClassExpression } from "./pgClassExpression.js"; -import { _AnyPgResource } from "../datasource.js"; const EMPTY_MAP = new Map(); @@ -47,7 +47,9 @@ interface PgInsertSinglePlanFinalizeResults { /** * Inserts a row into resource with the given specified attribute values. */ -export class PgInsertSingleStep +export class PgInsertSingleStep< + TResource extends _AnyPgResource = _AnyPgResource, + > extends ExecutableStep< unknown[] // tuple depending on what's selected > diff --git a/grafast/dataplan-pg/src/steps/pgSelect.ts b/grafast/dataplan-pg/src/steps/pgSelect.ts index 0b1d890233..008581b438 100644 --- a/grafast/dataplan-pg/src/steps/pgSelect.ts +++ b/grafast/dataplan-pg/src/steps/pgSelect.ts @@ -2409,9 +2409,9 @@ ${lateralText};`; return actualKeyByDesiredKey; } - private mergePlaceholdersInto>( - otherPlan: TOtherStep, - ): void { + private mergePlaceholdersInto< + TOtherStep extends PgSelectStep<_AnyPgResource>, + >(otherPlan: TOtherStep): void { for (const placeholder of this.placeholders) { const { dependencyIndex, symbol, codec } = placeholder; const dep = this.getDep(dependencyIndex); diff --git a/grafast/dataplan-pg/src/steps/pgSelectSingle.ts b/grafast/dataplan-pg/src/steps/pgSelectSingle.ts index ee46814164..89e76c2187 100644 --- a/grafast/dataplan-pg/src/steps/pgSelectSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgSelectSingle.ts @@ -82,7 +82,8 @@ const CHEAP_ATTRIBUTE_TYPES = new Set<_AnyPgCodec>([ /** @internal */ export interface _AnyPgSelectSingleStep extends PgSelectSingleStep {} -export interface GenericPgSelectSingleStep extends PgSelectSingleStep {} +export interface GenericPgSelectSingleStep + extends PgSelectSingleStep {} export type PgSelectSingleStepResource = U extends PgSelectSingleStep< infer TResource > diff --git a/grafast/dataplan-pg/src/steps/pgUnionAll.ts b/grafast/dataplan-pg/src/steps/pgUnionAll.ts index 43b981e449..73aaafb71a 100644 --- a/grafast/dataplan-pg/src/steps/pgUnionAll.ts +++ b/grafast/dataplan-pg/src/steps/pgUnionAll.ts @@ -393,7 +393,7 @@ export class PgUnionAllStep< PgSelectParsedCursorStep > { - _PgUnionAllStep: "PgUnionAllStep" = "PgUnionAllStep"; + _PgUnionAllStep = "PgUnionAllStep" as const; static $$export = { moduleName: "@dataplan/pg", exportName: "PgUnionAllStep", diff --git a/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts b/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts index 7865c1dab6..45bbcc2df3 100644 --- a/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts +++ b/grafast/dataplan-pg/src/steps/pgUpdateSingle.ts @@ -8,6 +8,7 @@ import type { SQL, SQLRawValue } from "pg-sql2"; import sql from "pg-sql2"; import type { _AnyPgCodecAttribute, PgCodecAttributeName } from "../codecs.js"; +import type { _AnyPgResource, PgResourceUniques } from "../datasource.js"; import type { GenericPgResource, PgResourceUnique } from "../index.js"; import { inspect } from "../inspect.js"; import type { @@ -19,7 +20,6 @@ import type { } from "../interfaces.js"; import type { PgClassExpressionStep } from "./pgClassExpression.js"; import { pgClassExpression } from "./pgClassExpression.js"; -import { _AnyPgResource, PgResourceUniques } from "../datasource.js"; type QueryValueDetailsBySymbol = Map< symbol, diff --git a/grafast/dataplan-pg/src/steps/toPg.ts b/grafast/dataplan-pg/src/steps/toPg.ts index 3aad9586f8..f8f2db5df3 100644 --- a/grafast/dataplan-pg/src/steps/toPg.ts +++ b/grafast/dataplan-pg/src/steps/toPg.ts @@ -1,7 +1,7 @@ import type { ExecutableStep, ExecutionExtra } from "grafast"; import { UnbatchedExecutableStep } from "grafast"; -import type { _AnyPgCodec, PgCodec } from "../interfaces.js"; +import type { _AnyPgCodec } from "../interfaces.js"; /** * Converts the given value to the representation suitable for feeding into the diff --git a/graphile-build/graphile-build-pg/src/examples/NO_DATA_GATHERING.ts b/graphile-build/graphile-build-pg/src/examples/NO_DATA_GATHERING.ts index b3ead311ae..15f209eafc 100644 --- a/graphile-build/graphile-build-pg/src/examples/NO_DATA_GATHERING.ts +++ b/graphile-build/graphile-build-pg/src/examples/NO_DATA_GATHERING.ts @@ -9,11 +9,7 @@ import { readFile } from "node:fs/promises"; import { pathToFileURL } from "node:url"; import { inspect } from "node:util"; -import type { - PgExecutorContextPlans, - PgRegistry, - WithPgClient, -} from "@dataplan/pg"; +import type { PgExecutorContextPlans, WithPgClient } from "@dataplan/pg"; import { makePgResourceOptions, makeRegistryBuilder, diff --git a/graphile-build/graphile-build-pg/src/interfaces.ts b/graphile-build/graphile-build-pg/src/interfaces.ts index d9f4a9a554..576ed990b8 100644 --- a/graphile-build/graphile-build-pg/src/interfaces.ts +++ b/graphile-build/graphile-build-pg/src/interfaces.ts @@ -1,4 +1,4 @@ -import type { GenericPgRegistry, PgRegistry, WithPgClient } from "@dataplan/pg"; +import type { GenericPgRegistry, WithPgClient } from "@dataplan/pg"; import type { PromiseOrDirect } from "grafast"; declare global { namespace GraphileBuild { diff --git a/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts index 083929f710..9f45670ede 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgCodecsPlugin.ts @@ -4,7 +4,6 @@ import type { GenericPgCodecAttribute, PgCodec, PgCodecAnyScalar, - PgCodecAttributes, PgCodecExtensions, PgEnumCodec, PgRecordTypeCodecSpec, diff --git a/graphile-build/graphile-build-pg/src/plugins/PgConditionArgumentPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgConditionArgumentPlugin.ts index 837d9a9ab7..76b382a4aa 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgConditionArgumentPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgConditionArgumentPlugin.ts @@ -4,10 +4,7 @@ import "graphile-config"; import type { GenericPgSelectSingleStep, GenericPgSelectStep, - PgCodecWithAttributes, PgSelectParsedCursorStep, - PgSelectSingleStep, - PgSelectStep, } from "@dataplan/pg"; import type { ConnectionStep } from "grafast"; import type { GraphQLInputObjectType } from "grafast/graphql"; diff --git a/graphile-build/graphile-build-pg/src/plugins/PgConditionCustomFieldsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgConditionCustomFieldsPlugin.ts index 6b2dc1ce92..3e0c6fd1e5 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgConditionCustomFieldsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgConditionCustomFieldsPlugin.ts @@ -4,7 +4,6 @@ import type { GenericPgResource, PgConditionStep, PgResource, - PgResourceParameter, PgSelectStep, } from "@dataplan/pg"; import { EXPORTABLE } from "graphile-build"; diff --git a/graphile-build/graphile-build-pg/src/plugins/PgConnectionArgOrderByPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgConnectionArgOrderByPlugin.ts index 00c223dff1..76f93e1648 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgConnectionArgOrderByPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgConnectionArgOrderByPlugin.ts @@ -3,7 +3,6 @@ import "graphile-config"; import type { GenericPgCodec, - PgCodec, PgSelectParsedCursorStep, PgSelectSingleStep, PgSelectStep, diff --git a/graphile-build/graphile-build-pg/src/plugins/PgCustomTypeFieldPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgCustomTypeFieldPlugin.ts index 8b3c87e639..f1eec7f4fe 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgCustomTypeFieldPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgCustomTypeFieldPlugin.ts @@ -14,15 +14,10 @@ import type { GenericPgSelectStep, GenericPgUpdateSingleStep, PgClassSingleStep, - PgCodec, - PgDeleteSingleStep, PgInsertSingleStep, PgResource, - PgResourceParameter, PgSelectArgumentSpec, - PgSelectStep, PgTypedExecutableStep, - PgUpdateSingleStep, } from "@dataplan/pg"; import { digestsFromArgumentSpecs, @@ -178,9 +173,7 @@ declare global { } } -function shouldUseCustomConnection( - pgResource: GenericPgResource, -): boolean { +function shouldUseCustomConnection(pgResource: GenericPgResource): boolean { const { codec } = pgResource; // 'setof ' functions should use a connection based on the function name, not a generic connection const setOrArray = !pgResource.isUnique || !!codec.arrayOfCodec; diff --git a/graphile-build/graphile-build-pg/src/plugins/PgInterfaceModeUnionAllRowsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgInterfaceModeUnionAllRowsPlugin.ts index e4094e1a28..567978aaba 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgInterfaceModeUnionAllRowsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgInterfaceModeUnionAllRowsPlugin.ts @@ -3,7 +3,6 @@ import "graphile-config"; import type { GenericPgCodec, GenericPgResource, - PgCodec, PgUnionAllStepConfigAttributes, PgUnionAllStepMember, } from "@dataplan/pg"; @@ -21,7 +20,10 @@ declare global { * The base inflector used by allInterfaceModeUnionRowsConnection and * allInterfaceModeUnionRowsList. */ - _allInterfaceModeUnionRows(this: Inflection, codec: GenericPgCodec): string; + _allInterfaceModeUnionRows( + this: Inflection, + codec: GenericPgCodec, + ): string; /** * The field name for a Cursor Connection field that returns all rows @@ -36,7 +38,10 @@ declare global { * The field name for a List field that returns all rows from the given * `@interface mode:union` codec. */ - allInterfaceModeUnionRowsList(this: Inflection, codec: GenericPgCodec): string; + allInterfaceModeUnionRowsList( + this: Inflection, + codec: GenericPgCodec, + ): string; } } } diff --git a/graphile-build/graphile-build-pg/src/plugins/PgJWTPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgJWTPlugin.ts index 21ea6b5e91..3c95cdf993 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgJWTPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgJWTPlugin.ts @@ -1,11 +1,6 @@ import "graphile-config"; -import type { - GenericPgCodec, - GenericPgSelectSingleStep, - PgCodec, - PgSelectSingleStep, -} from "@dataplan/pg"; +import type { GenericPgCodec, GenericPgSelectSingleStep } from "@dataplan/pg"; import { EXPORTABLE, gatherConfig } from "graphile-build"; import type { Secret, SignOptions } from "jsonwebtoken"; import { sign as signJwt } from "jsonwebtoken"; diff --git a/graphile-build/graphile-build-pg/src/plugins/PgMutationUpdateDeletePlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgMutationUpdateDeletePlugin.ts index f52e654593..112363a566 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgMutationUpdateDeletePlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgMutationUpdateDeletePlugin.ts @@ -6,11 +6,7 @@ import type { GenericPgResourceUnique, GenericPgUpdateSingleStep, PgClassSingleStep, - PgCodecWithAttributes, - PgDeleteSingleStep, PgResource, - PgResourceUnique, - PgUpdateSingleStep, } from "@dataplan/pg"; import { pgDeleteSingle, pgUpdateSingle } from "@dataplan/pg"; import type { ExecutableStep, FieldArgs } from "grafast"; diff --git a/graphile-build/graphile-build-pg/src/plugins/PgOrderCustomFieldsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgOrderCustomFieldsPlugin.ts index 4f9bb5826d..bd6aab53ba 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgOrderCustomFieldsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgOrderCustomFieldsPlugin.ts @@ -1,10 +1,7 @@ import "./PgTablesPlugin.js"; import "graphile-config"; -import type { - GenericPgResource, - GenericPgSelectStep, -} from "@dataplan/pg"; +import type { GenericPgResource, GenericPgSelectStep } from "@dataplan/pg"; import { EXPORTABLE } from "graphile-build"; import { version } from "../version.js"; diff --git a/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts index 302c35a683..5ad6b545c4 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts @@ -3,14 +3,10 @@ import type { _AnyPgCodecRelationConfig, _AnyPgResourceOptions, GenericPgCodecRelationConfig, - PgCodec, PgCodecExtensions, PgCodecRefPath, - PgCodecRelationConfig, - PgCodecWithAttributes, PgRefDefinition, PgRefDefinitions, - PgResourceOptions, } from "@dataplan/pg"; import { arraysMatch } from "grafast"; import { gatherConfig } from "graphile-build"; @@ -206,8 +202,7 @@ export const PgRefsPlugin: GraphileConfig.Plugin = { ? [tags.refVia] : null; - const refDefinitions = resourceOptions.codec.extensions - ?.refDefinitions; + const refDefinitions = resourceOptions.codec.extensions?.refDefinitions; if (!refDefinitions) { if (rawRefVias) { throw new Error(`@refVia without matching @ref is invalid`); diff --git a/graphile-build/graphile-build-pg/src/plugins/PgRegistryPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgRegistryPlugin.ts index ca994643e3..980af3906a 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgRegistryPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgRegistryPlugin.ts @@ -1,10 +1,10 @@ /* eslint-disable graphile-export/export-instances */ import type { + DefaultRegistryBuilder, + EmptyRegistryBuilder, GenericPgRegistry, PgRegistry, PgRegistryBuilder, - DefaultRegistryBuilder, - EmptyRegistryBuilder, } from "@dataplan/pg"; import { makeRegistryBuilder } from "@dataplan/pg"; import type { PromiseOrDirect } from "grafast"; diff --git a/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts index 205301ccbf..790837dc9a 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts @@ -11,9 +11,7 @@ import type { PgCodecRef, PgCodecRefPath, PgCodecRelationConfig, - PgCodecWithAttributes, PgRefDefinition, - PgResourceOptions, PgUnionAllStepConfigAttributes, PgUnionAllStepMember, } from "@dataplan/pg"; diff --git a/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts index 6545cf7498..3807160dd2 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts @@ -1,12 +1,11 @@ import type { _AnyPgCodecAttribute, _AnyPgResource, - GenericPgResourceOptions, - _AnyPgResourceUnique, - PgCodec, - PgResource, _AnyPgResourceOptions, + _AnyPgResourceUnique, GenericPgCodec, + GenericPgResourceOptions, + PgResource, } from "@dataplan/pg"; import { assertPgClassSingleStep, makePgResourceOptions } from "@dataplan/pg"; import { object } from "grafast"; @@ -88,10 +87,7 @@ declare global { * be called directly, instead it's called from other inflectors to give * them common behavior. */ - _codecName( - this: Inflection, - codec: GenericPgCodec, - ): string; + _codecName(this: Inflection, codec: GenericPgCodec): string; /** * Takes a `_codecName` and singularizes it. This is also a good place to @@ -104,10 +100,7 @@ declare global { * be called directly, instead it's called from other inflectors to give * them common behavior. */ - _singularizedCodecName( - this: Inflection, - codec: GenericPgCodec, - ): string; + _singularizedCodecName(this: Inflection, codec: GenericPgCodec): string; /** * Appends '_record' to a name that ends in `_input`, `_patch`, `Input` @@ -120,10 +113,7 @@ declare global { * specific table (more specifically a PostgreSQL "pg_class" which is * represented as a certain PgCodec) */ - tableType( - this: GraphileBuild.Inflection, - codec: GenericPgCodec, - ): string; + tableType(this: GraphileBuild.Inflection, codec: GenericPgCodec): string; tableConnectionType( this: GraphileBuild.Inflection, @@ -209,9 +199,12 @@ interface State { string, Map> >; - resourceByResourceOptions: Map>; + resourceByResourceOptions: Map< + GenericPgResourceOptions, + Promise<_AnyPgResource | null> + >; detailsByResourceOptions: Map< - GenericPgResourceOptions, + GenericPgResourceOptions, { serviceName: string; pgClass: PgClass } >; } diff --git a/graphile-build/graphile-build-pg/src/utils.ts b/graphile-build/graphile-build-pg/src/utils.ts index 43076b0780..116224cdc7 100644 --- a/graphile-build/graphile-build-pg/src/utils.ts +++ b/graphile-build/graphile-build-pg/src/utils.ts @@ -1,4 +1,4 @@ -import type { PgCodecRefPath, PgCodecRelation, PgResource } from "@dataplan/pg"; +import type { PgCodecRefPath, PgResource } from "@dataplan/pg"; import type { PgSmartTagsDict } from "pg-introspection"; export function tagToString( diff --git a/graphile-build/graphile-simplify-inflection/src/index.ts b/graphile-build/graphile-simplify-inflection/src/index.ts index 9504da3024..bd53f44a14 100644 --- a/graphile-build/graphile-simplify-inflection/src/index.ts +++ b/graphile-build/graphile-simplify-inflection/src/index.ts @@ -1,4 +1,4 @@ -import type { PgCodec, PgResourceUnique } from "@dataplan/pg"; +import type { PgCodec } from "@dataplan/pg"; import type {} from "graphile-build"; import type {} from "graphile-build-pg"; import type { GraphileConfig } from "graphile-config"; diff --git a/graphile-build/graphile-utils/src/makeAddPgTableConditionPlugin.ts b/graphile-build/graphile-utils/src/makeAddPgTableConditionPlugin.ts index 66de9fa882..16ead295ba 100644 --- a/graphile-build/graphile-utils/src/makeAddPgTableConditionPlugin.ts +++ b/graphile-build/graphile-utils/src/makeAddPgTableConditionPlugin.ts @@ -1,8 +1,4 @@ -import type { - GenericPgSelectStep, - PgConditionStep, - PgSelectStep, -} from "@dataplan/pg"; +import type { GenericPgSelectStep, PgConditionStep } from "@dataplan/pg"; import type { FieldArgs, GrafastInputFieldConfig } from "grafast"; import type { SQL, sql } from "pg-sql2"; diff --git a/graphile-build/graphile-utils/src/makeAddPgTableOrderByPlugin.ts b/graphile-build/graphile-utils/src/makeAddPgTableOrderByPlugin.ts index fb5898165c..0c6e192707 100644 --- a/graphile-build/graphile-utils/src/makeAddPgTableOrderByPlugin.ts +++ b/graphile-build/graphile-utils/src/makeAddPgTableOrderByPlugin.ts @@ -1,4 +1,4 @@ -import type { GenericPgSelectStep, PgOrderSpec, PgSelectStep } from "@dataplan/pg"; +import type { GenericPgSelectStep, PgOrderSpec } from "@dataplan/pg"; import { EXPORTABLE } from "./exportable.js"; diff --git a/postgraphile/postgraphile/src/plugins/PgV4BehaviorPlugin.ts b/postgraphile/postgraphile/src/plugins/PgV4BehaviorPlugin.ts index 9214615470..8397f06a25 100644 --- a/postgraphile/postgraphile/src/plugins/PgV4BehaviorPlugin.ts +++ b/postgraphile/postgraphile/src/plugins/PgV4BehaviorPlugin.ts @@ -1,7 +1,7 @@ import "graphile-config"; import "graphile-build-pg"; -import type { GenericPgResourceOptions, PgResourceOptions } from "@dataplan/pg"; +import type { GenericPgResourceOptions } from "@dataplan/pg"; import type { PgProc } from "graphile-build-pg/pg-introspection"; import { inspect } from "util"; diff --git a/postgraphile/postgraphile/src/presets/relay.ts b/postgraphile/postgraphile/src/presets/relay.ts index 83cee34b3d..42027a57f2 100644 --- a/postgraphile/postgraphile/src/presets/relay.ts +++ b/postgraphile/postgraphile/src/presets/relay.ts @@ -1,8 +1,6 @@ import "graphile-config"; import "graphile-build-pg"; -import type { PgCodecRelation } from "@dataplan/pg"; - import { version } from "../version.js"; const RELAY_HIDDEN_COLUMN_BEHAVIORS = [ diff --git a/utils/graphile/src/commands/behavior/debug/main.ts b/utils/graphile/src/commands/behavior/debug/main.ts index a0ba62ff25..b3df8a7f09 100644 --- a/utils/graphile/src/commands/behavior/debug/main.ts +++ b/utils/graphile/src/commands/behavior/debug/main.ts @@ -1,4 +1,3 @@ -import type { PgRegistry } from "@dataplan/pg"; import chalk from "chalk"; import { resolvePresets } from "graphile-config"; import { loadConfig } from "graphile-config/load"; From c649b42de2a8fa453f334fa9fb61802a1a9cc1f4 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Thu, 23 Nov 2023 16:38:00 +0000 Subject: [PATCH 7/8] Experimental edits made on the phone --- grafast/dataplan-pg/src/codecs.ts | 21 +++++-- grafast/dataplan-pg/src/datasource.ts | 12 ++-- .../dataplan-pg/src/examples/exampleSchema.ts | 7 ++- grafast/dataplan-pg/src/interfaces.ts | 57 ++++++++++++++++--- .../src/plugins/PgProceduresPlugin.ts | 49 +++++++++++----- 5 files changed, 113 insertions(+), 33 deletions(-) diff --git a/grafast/dataplan-pg/src/codecs.ts b/grafast/dataplan-pg/src/codecs.ts index 3e923535f5..56cdd0e89a 100644 --- a/grafast/dataplan-pg/src/codecs.ts +++ b/grafast/dataplan-pg/src/codecs.ts @@ -88,6 +88,8 @@ export type PgCodecAttributeExtensions = DataplanPg.PgCodecAttributeExtensions; export type PgCodecAttributeName = U extends PgCodecAttribute< infer TName, any, + any, + any, any > ? TName @@ -96,20 +98,31 @@ export type PgCodecAttributeName = U extends PgCodecAttribute< export type PgCodecAttributeCodec = U extends PgCodecAttribute< any, infer TCodec, + any, + any, any > ? TCodec : never; export interface GenericPgCodecAttribute - extends PgCodecAttribute {} + extends PgCodecAttribute< + string, + GenericPgCodec, + boolean, + GenericPgCodecAttributeVia, + GenericPgCodecAttributeVia + > {} /** @internal */ -export interface _AnyPgCodecAttribute extends PgCodecAttribute {} +export interface _AnyPgCodecAttribute + extends PgCodecAttribute {} export interface PgCodecAttribute< TName extends string, TCodec extends _AnyPgCodec, TNotNull extends boolean, + TVia extends _AnyPgCodecAttributeVia = any, + TIdenticalVia extends _AnyPgCodecAttributeVia = any, > { name: TName; /** @@ -136,7 +149,7 @@ export interface PgCodecAttribute< * If this attribute actually exists on a relation rather than locally, the name * of the (unique) relation this attribute belongs to. */ - via?: _AnyPgCodecAttributeVia; + via?: TVia; /** * If the attribute exists identically on a relation and locally (e.g. @@ -161,7 +174,7 @@ export interface PgCodecAttribute< * these are all plural relationships. So identicalVia is generally one-way * (except in 1-to-1 relationships). */ - identicalVia?: _AnyPgCodecAttributeVia; + identicalVia?: TIdenticalVia; // ENHANCE: can identicalVia be plural? Is that useful? Maybe a attribute that has // multiple foreign key references? diff --git a/grafast/dataplan-pg/src/datasource.ts b/grafast/dataplan-pg/src/datasource.ts index d66e0d3570..f36b6b9c3c 100644 --- a/grafast/dataplan-pg/src/datasource.ts +++ b/grafast/dataplan-pg/src/datasource.ts @@ -14,6 +14,7 @@ import sql from "pg-sql2"; import type { _AnyPgCodecAttribute, GenericPgCodecAttribute, + PgCodecAttribute, PgCodecAttributeName, PgCodecAttributeVia, PgCodecAttributeViaExplicit, @@ -729,7 +730,7 @@ export class PgResource< public get< TSelectSinglePlanOptions extends PgSelectSinglePlanOptions, - TSpec extends Expand, TUniques>>, + TSpec extends PlanByUniques, TUniques>, >( spec: TSpec, // This is internal, it's an optimisation we can use but you shouldn't. @@ -1477,13 +1478,16 @@ export function makeRegistryBuilder(): EmptyRegistryBuilder { exportAs("@dataplan/pg", makeRegistryBuilder, "makeRegistryBuilder"); +type Foo< + TCodec extends _AnyPgCodec, + A extends PgCodecAttributes, +> = A extends any ? PgResourceUnique : never; + export function makePgResourceOptions< const TName extends string, const TCodec extends _AnyPgCodec, const TAttributes extends PgCodecAttributes, - const TUniques extends PgResourceUnique< - TAttributes[keyof TAttributes] - > = never, + const TUniques extends Foo = never, const TParameters extends _AnyPgResourceParameter = never, >( options: PgResourceOptions, diff --git a/grafast/dataplan-pg/src/examples/exampleSchema.ts b/grafast/dataplan-pg/src/examples/exampleSchema.ts index 3eebd60b6d..81dd620039 100644 --- a/grafast/dataplan-pg/src/examples/exampleSchema.ts +++ b/grafast/dataplan-pg/src/examples/exampleSchema.ts @@ -526,6 +526,7 @@ export function makeExampleSchema( { attributes: ["username"] }, ], }); + personResourceOptions.uniques; const postCodec = recordCodec({ executor, @@ -1657,8 +1658,12 @@ export function makeExampleSchema( * because it's not being ran as part of a Grafast planning context - hence * the `if`. */ + registry.pgResources.people.uniques; const $person = registry.pgResources.people.get({ - person_id: constant(1, false), + person_id: constant(1), + frog: constant("frog"), + + //person_id: constant(1, false), }); const $posts = $person.manyRelation("posts"); const $post = $posts.single(); diff --git a/grafast/dataplan-pg/src/interfaces.ts b/grafast/dataplan-pg/src/interfaces.ts index 26603e52cd..4ad4d32bb1 100644 --- a/grafast/dataplan-pg/src/interfaces.ts +++ b/grafast/dataplan-pg/src/interfaces.ts @@ -5,6 +5,7 @@ import type { PgAdaptorOptions } from "./adaptors/pg.js"; import type { _AnyPgCodecAttribute, GenericPgCodecAttribute, + PgCodecAttribute, PgCodecAttributeCodec, PgCodecAttributeName, } from "./codecs.js"; @@ -470,28 +471,63 @@ export interface PgGroupSpec { } export type TuplePlanMap< - TAttributes extends _AnyPgCodecAttribute, - TTuple extends ReadonlyArray>, -> = { - [Index in keyof TTuple]: { + TAllAttributes extends _AnyPgCodecAttribute, + TUniqueAttributeNamesUnion extends ReadonlyArray< + PgCodecAttributeName + >, +> = TUniqueAttributeNamesUnion extends any + ? //? TUniqueAttributeNamesUnion extends ReadonlyArray + { + [TKey in TUniqueAttributeNamesUnion[number]]: ExecutableStep; + } /*& { + [TKey in Exclude< + TAllAttributes["name"], + TUniqueAttributeNamesUnion[number] + >]?: ExecutableStep; + } + > */ + : //: never + never; +/* +type Attr = PgCodecAttribute; + +type A = TuplePlanMap< + Attr<"a"> | Attr<"b"> | Attr<"c"> | Attr<"d">, + ["a", "b"] | ["c"] +>; + +type F = (number | undefined) & number; + +// {a: 1, b: 1, c?: 1, d?:1} | {c: 1, a?: 1, b?:1, d?: 1} + +const s: ExecutableStep = null as any; +const a: A = { + d: s, +}; +*/ + +/* + // Optional attributes [attribute in Exclude< - TAttributes, - { name: TTuple[number] } + TAllAttributes, + { name: TUniqueAttributeNamesUnion[number] } > as PgCodecAttributeName]?: ExecutableStep< ReturnType>> >; } & { // Required unique combination of attributes - [key in TTuple[number]]: ExecutableStep< + [key in TUniqueAttributeNamesUnion[number]]: ExecutableStep< + ReturnType< PgCodecFromPg< - PgCodecAttributeCodec> + PgCodecAttributeCodec> > > >; }; }; +*/ /** * Represents a spec like `{user_id: ExecutableStep}` or @@ -504,7 +540,10 @@ export type TuplePlanMap< export type PlanByUniques< TAttributes extends _AnyPgCodecAttribute, TUniqueAttributes extends PgResourceUnique, -> = TuplePlanMap[number]; +> = TuplePlanMap< + TAttributes, + TUniqueAttributes extends { attributes: infer U } ? U : never +>; export type PgConditionLikeStep = (ModifierStep | ExecutableStep) & { alias: SQL; diff --git a/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts index 269e37bd12..cb42f0e233 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts @@ -4,10 +4,14 @@ import type { GenericPgCodec, + GenericPgCodecAttribute, GenericPgCodecAttributesRecord, GenericPgFunctionResourceOptions, GenericPgResourceOptions, GenericPgResourceParameter, + GetPgResourceCodec, + PgCodecAttributes, + PgCodecAttributesRecord, PgResourceExtensions, PgSelectArgumentDigest, } from "@dataplan/pg"; @@ -284,23 +288,25 @@ export const PgProceduresPlugin: GraphileConfig.Plugin = { pgProc, serviceName, }); - return EXPORTABLE( - (attributes, executor, recordCodec, recordCodecName, sql) => - recordCodec({ - name: recordCodecName, - identifier: sql`ANONYMOUS_TYPE_DO_NOT_REFERENCE`, - attributes, - description: undefined, - extensions: { - /* `The return type of our \`${name}\` ${ + const foo = + EXPORTABLE( + (attributes, executor, recordCodec, recordCodecName, sql) => + recordCodec({ + name: recordCodecName, + identifier: sql`ANONYMOUS_TYPE_DO_NOT_REFERENCE`, + attributes, + description: undefined, + extensions: { + /* `The return type of our \`${name}\` ${ pgProc.provolatile === "v" ? "mutation" : "query" }.`, */ - }, - executor, - isAnonymous: true, - }), - [attributes, executor, recordCodec, recordCodecName, sql], - ); + }, + executor, + isAnonymous: true, + }), + [attributes, executor, recordCodec, recordCodecName, sql], + ) ?? null; + return foo; }; const returnCodec = needsPayloadCodecToBeGenerated @@ -535,6 +541,19 @@ export const PgProceduresPlugin: GraphileConfig.Plugin = { resourceOptions: finalResourceOptions, }); + /* + type Foo = typeof finalResourceOptions; + type Codec = Foo["codec"]; + type Bar = GenericPgCodecAttribute; + type Bar2 = PgCodecAttributes; + type Bar2a = PgCodecAttributes; + // ISSUE: Bar2 = GenericPgCodecAttribute + // SHOULD BE: Bar3 = PgCodecAttributesRecord + type Bar3 = PgCodecAttributesRecord; + type Baz = keyof Bar2; + type Qux = Bar2[Baz]; + */ + return EXPORTABLE( (finalResourceOptions, makePgResourceOptions) => makePgResourceOptions(finalResourceOptions), From b36456879987877da57052dfaefe2b41e44310e2 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Thu, 23 Nov 2023 16:38:06 +0000 Subject: [PATCH 8/8] Revert "Experimental edits made on the phone" This reverts commit c649b42de2a8fa453f334fa9fb61802a1a9cc1f4. --- grafast/dataplan-pg/src/codecs.ts | 21 ++----- grafast/dataplan-pg/src/datasource.ts | 12 ++-- .../dataplan-pg/src/examples/exampleSchema.ts | 7 +-- grafast/dataplan-pg/src/interfaces.ts | 57 +++---------------- .../src/plugins/PgProceduresPlugin.ts | 49 +++++----------- 5 files changed, 33 insertions(+), 113 deletions(-) diff --git a/grafast/dataplan-pg/src/codecs.ts b/grafast/dataplan-pg/src/codecs.ts index 56cdd0e89a..3e923535f5 100644 --- a/grafast/dataplan-pg/src/codecs.ts +++ b/grafast/dataplan-pg/src/codecs.ts @@ -88,8 +88,6 @@ export type PgCodecAttributeExtensions = DataplanPg.PgCodecAttributeExtensions; export type PgCodecAttributeName = U extends PgCodecAttribute< infer TName, any, - any, - any, any > ? TName @@ -98,31 +96,20 @@ export type PgCodecAttributeName = U extends PgCodecAttribute< export type PgCodecAttributeCodec = U extends PgCodecAttribute< any, infer TCodec, - any, - any, any > ? TCodec : never; export interface GenericPgCodecAttribute - extends PgCodecAttribute< - string, - GenericPgCodec, - boolean, - GenericPgCodecAttributeVia, - GenericPgCodecAttributeVia - > {} + extends PgCodecAttribute {} /** @internal */ -export interface _AnyPgCodecAttribute - extends PgCodecAttribute {} +export interface _AnyPgCodecAttribute extends PgCodecAttribute {} export interface PgCodecAttribute< TName extends string, TCodec extends _AnyPgCodec, TNotNull extends boolean, - TVia extends _AnyPgCodecAttributeVia = any, - TIdenticalVia extends _AnyPgCodecAttributeVia = any, > { name: TName; /** @@ -149,7 +136,7 @@ export interface PgCodecAttribute< * If this attribute actually exists on a relation rather than locally, the name * of the (unique) relation this attribute belongs to. */ - via?: TVia; + via?: _AnyPgCodecAttributeVia; /** * If the attribute exists identically on a relation and locally (e.g. @@ -174,7 +161,7 @@ export interface PgCodecAttribute< * these are all plural relationships. So identicalVia is generally one-way * (except in 1-to-1 relationships). */ - identicalVia?: TIdenticalVia; + identicalVia?: _AnyPgCodecAttributeVia; // ENHANCE: can identicalVia be plural? Is that useful? Maybe a attribute that has // multiple foreign key references? diff --git a/grafast/dataplan-pg/src/datasource.ts b/grafast/dataplan-pg/src/datasource.ts index f36b6b9c3c..d66e0d3570 100644 --- a/grafast/dataplan-pg/src/datasource.ts +++ b/grafast/dataplan-pg/src/datasource.ts @@ -14,7 +14,6 @@ import sql from "pg-sql2"; import type { _AnyPgCodecAttribute, GenericPgCodecAttribute, - PgCodecAttribute, PgCodecAttributeName, PgCodecAttributeVia, PgCodecAttributeViaExplicit, @@ -730,7 +729,7 @@ export class PgResource< public get< TSelectSinglePlanOptions extends PgSelectSinglePlanOptions, - TSpec extends PlanByUniques, TUniques>, + TSpec extends Expand, TUniques>>, >( spec: TSpec, // This is internal, it's an optimisation we can use but you shouldn't. @@ -1478,16 +1477,13 @@ export function makeRegistryBuilder(): EmptyRegistryBuilder { exportAs("@dataplan/pg", makeRegistryBuilder, "makeRegistryBuilder"); -type Foo< - TCodec extends _AnyPgCodec, - A extends PgCodecAttributes, -> = A extends any ? PgResourceUnique : never; - export function makePgResourceOptions< const TName extends string, const TCodec extends _AnyPgCodec, const TAttributes extends PgCodecAttributes, - const TUniques extends Foo = never, + const TUniques extends PgResourceUnique< + TAttributes[keyof TAttributes] + > = never, const TParameters extends _AnyPgResourceParameter = never, >( options: PgResourceOptions, diff --git a/grafast/dataplan-pg/src/examples/exampleSchema.ts b/grafast/dataplan-pg/src/examples/exampleSchema.ts index 81dd620039..3eebd60b6d 100644 --- a/grafast/dataplan-pg/src/examples/exampleSchema.ts +++ b/grafast/dataplan-pg/src/examples/exampleSchema.ts @@ -526,7 +526,6 @@ export function makeExampleSchema( { attributes: ["username"] }, ], }); - personResourceOptions.uniques; const postCodec = recordCodec({ executor, @@ -1658,12 +1657,8 @@ export function makeExampleSchema( * because it's not being ran as part of a Grafast planning context - hence * the `if`. */ - registry.pgResources.people.uniques; const $person = registry.pgResources.people.get({ - person_id: constant(1), - frog: constant("frog"), - - //person_id: constant(1, false), + person_id: constant(1, false), }); const $posts = $person.manyRelation("posts"); const $post = $posts.single(); diff --git a/grafast/dataplan-pg/src/interfaces.ts b/grafast/dataplan-pg/src/interfaces.ts index 4ad4d32bb1..26603e52cd 100644 --- a/grafast/dataplan-pg/src/interfaces.ts +++ b/grafast/dataplan-pg/src/interfaces.ts @@ -5,7 +5,6 @@ import type { PgAdaptorOptions } from "./adaptors/pg.js"; import type { _AnyPgCodecAttribute, GenericPgCodecAttribute, - PgCodecAttribute, PgCodecAttributeCodec, PgCodecAttributeName, } from "./codecs.js"; @@ -471,63 +470,28 @@ export interface PgGroupSpec { } export type TuplePlanMap< - TAllAttributes extends _AnyPgCodecAttribute, - TUniqueAttributeNamesUnion extends ReadonlyArray< - PgCodecAttributeName - >, -> = TUniqueAttributeNamesUnion extends any - ? //? TUniqueAttributeNamesUnion extends ReadonlyArray - { - [TKey in TUniqueAttributeNamesUnion[number]]: ExecutableStep; - } /*& { - [TKey in Exclude< - TAllAttributes["name"], - TUniqueAttributeNamesUnion[number] - >]?: ExecutableStep; - } - > */ - : //: never - never; -/* -type Attr = PgCodecAttribute; - -type A = TuplePlanMap< - Attr<"a"> | Attr<"b"> | Attr<"c"> | Attr<"d">, - ["a", "b"] | ["c"] ->; - -type F = (number | undefined) & number; - -// {a: 1, b: 1, c?: 1, d?:1} | {c: 1, a?: 1, b?:1, d?: 1} - -const s: ExecutableStep = null as any; -const a: A = { - d: s, -}; -*/ - -/* - + TAttributes extends _AnyPgCodecAttribute, + TTuple extends ReadonlyArray>, +> = { + [Index in keyof TTuple]: { // Optional attributes [attribute in Exclude< - TAllAttributes, - { name: TUniqueAttributeNamesUnion[number] } + TAttributes, + { name: TTuple[number] } > as PgCodecAttributeName]?: ExecutableStep< ReturnType>> >; } & { // Required unique combination of attributes - [key in TUniqueAttributeNamesUnion[number]]: ExecutableStep< - + [key in TTuple[number]]: ExecutableStep< ReturnType< PgCodecFromPg< - PgCodecAttributeCodec> + PgCodecAttributeCodec> > > >; }; }; -*/ /** * Represents a spec like `{user_id: ExecutableStep}` or @@ -540,10 +504,7 @@ const a: A = { export type PlanByUniques< TAttributes extends _AnyPgCodecAttribute, TUniqueAttributes extends PgResourceUnique, -> = TuplePlanMap< - TAttributes, - TUniqueAttributes extends { attributes: infer U } ? U : never ->; +> = TuplePlanMap[number]; export type PgConditionLikeStep = (ModifierStep | ExecutableStep) & { alias: SQL; diff --git a/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts index cb42f0e233..269e37bd12 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgProceduresPlugin.ts @@ -4,14 +4,10 @@ import type { GenericPgCodec, - GenericPgCodecAttribute, GenericPgCodecAttributesRecord, GenericPgFunctionResourceOptions, GenericPgResourceOptions, GenericPgResourceParameter, - GetPgResourceCodec, - PgCodecAttributes, - PgCodecAttributesRecord, PgResourceExtensions, PgSelectArgumentDigest, } from "@dataplan/pg"; @@ -288,25 +284,23 @@ export const PgProceduresPlugin: GraphileConfig.Plugin = { pgProc, serviceName, }); - const foo = - EXPORTABLE( - (attributes, executor, recordCodec, recordCodecName, sql) => - recordCodec({ - name: recordCodecName, - identifier: sql`ANONYMOUS_TYPE_DO_NOT_REFERENCE`, - attributes, - description: undefined, - extensions: { - /* `The return type of our \`${name}\` ${ + return EXPORTABLE( + (attributes, executor, recordCodec, recordCodecName, sql) => + recordCodec({ + name: recordCodecName, + identifier: sql`ANONYMOUS_TYPE_DO_NOT_REFERENCE`, + attributes, + description: undefined, + extensions: { + /* `The return type of our \`${name}\` ${ pgProc.provolatile === "v" ? "mutation" : "query" }.`, */ - }, - executor, - isAnonymous: true, - }), - [attributes, executor, recordCodec, recordCodecName, sql], - ) ?? null; - return foo; + }, + executor, + isAnonymous: true, + }), + [attributes, executor, recordCodec, recordCodecName, sql], + ); }; const returnCodec = needsPayloadCodecToBeGenerated @@ -541,19 +535,6 @@ export const PgProceduresPlugin: GraphileConfig.Plugin = { resourceOptions: finalResourceOptions, }); - /* - type Foo = typeof finalResourceOptions; - type Codec = Foo["codec"]; - type Bar = GenericPgCodecAttribute; - type Bar2 = PgCodecAttributes; - type Bar2a = PgCodecAttributes; - // ISSUE: Bar2 = GenericPgCodecAttribute - // SHOULD BE: Bar3 = PgCodecAttributesRecord - type Bar3 = PgCodecAttributesRecord; - type Baz = keyof Bar2; - type Qux = Bar2[Baz]; - */ - return EXPORTABLE( (finalResourceOptions, makePgResourceOptions) => makePgResourceOptions(finalResourceOptions),