From c3dbe672ab75d39490441c174f3fcea16d07f079 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 22 Dec 2025 18:56:59 +0000 Subject: [PATCH 1/3] Add type definitions for PostgreSQL types in parser Add a BuiltInPgType union type that lists all built-in PostgreSQL type names that can be used as parser keys. This provides IDE autocomplete and IntelliSense support when defining custom parsers. The Parser type now uses a mapped type with BuiltInPgType | (string & {}) which gives autocomplete for known PG types while still allowing custom types like enums, domains, and composite types. Also exports BuiltInPgType, Parser, ParseFunction, pgArrayParser, and defaultParser from the package index for easier use by consumers. --- packages/typescript-client/src/index.ts | 7 ++ packages/typescript-client/src/parser.ts | 96 +++++++++++++++++++++++- 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/packages/typescript-client/src/index.ts b/packages/typescript-client/src/index.ts index 39b6989e35..92fab50d38 100644 --- a/packages/typescript-client/src/index.ts +++ b/packages/typescript-client/src/index.ts @@ -16,3 +16,10 @@ export { snakeToCamel, camelToSnake, } from './column-mapper' +export { + type BuiltInPgType, + type Parser, + type ParseFunction, + pgArrayParser, + defaultParser, +} from './parser' diff --git a/packages/typescript-client/src/parser.ts b/packages/typescript-client/src/parser.ts index 4689848bb3..3d6c64c43b 100644 --- a/packages/typescript-client/src/parser.ts +++ b/packages/typescript-client/src/parser.ts @@ -11,12 +11,106 @@ type NullableParseFunction = ( value: NullableToken, additionalInfo?: Omit ) => Value + +/** + * Built-in PostgreSQL type names that can be used as parser keys. + * This provides autocomplete/IntelliSense support for common PostgreSQL types. + * + * Note: Custom types (enums, domains, composite types) can still be used as keys + * even though they are not listed here. + */ +export type BuiltInPgType = + // Numeric Types + | `int2` + | `int4` + | `int8` + | `float4` + | `float8` + | `numeric` + | `decimal` + | `money` + | `oid` + // Character Types + | `char` + | `bpchar` + | `varchar` + | `text` + | `citext` + | `name` + // Binary Type + | `bytea` + // Date/Time Types + | `date` + | `time` + | `timetz` + | `timestamp` + | `timestamptz` + | `interval` + // Boolean + | `bool` + // Geometric Types + | `point` + | `line` + | `lseg` + | `box` + | `path` + | `polygon` + | `circle` + // Network Types + | `cidr` + | `inet` + | `macaddr` + | `macaddr8` + // Bit String Types + | `bit` + | `varbit` + // Text Search Types + | `tsvector` + | `tsquery` + // UUID + | `uuid` + // XML + | `xml` + // JSON Types + | `json` + | `jsonb` + // Range Types + | `int4range` + | `int8range` + | `numrange` + | `tsrange` + | `tstzrange` + | `daterange` + // Multirange Types (PostgreSQL 14+) + | `int4multirange` + | `int8multirange` + | `nummultirange` + | `tsmultirange` + | `tstzmultirange` + | `datemultirange` + /** + * Parser type mapping PostgreSQL type names to parse functions. + * + * Provides autocomplete for built-in PostgreSQL types while still allowing + * custom types like enums, domains, and composite types. + * * @typeParam Extensions - Additional types that can be parsed by this parser beyond the standard SQL types. * Defaults to no additional types. + * + * @example + * ```ts + * const parser: Parser = { + * // Built-in types get autocomplete + * timestamptz: (value) => new Date(value), + * uuid: (value) => value, + * // Custom types (enums, domains) also work + * my_enum: (value) => value as MyEnum, + * } + * ``` */ export type Parser = { - [key: string]: ParseFunction + [key in BuiltInPgType | (string & {})]?: ParseFunction } export type TransformFunction = ( From ffdc68999df1c63bd6b35485be3053c34de7e9be Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 22 Dec 2025 19:10:36 +0000 Subject: [PATCH 2/3] Add changeset for parser type definitions --- .changeset/add-parser-pg-type-definitions.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .changeset/add-parser-pg-type-definitions.md diff --git a/.changeset/add-parser-pg-type-definitions.md b/.changeset/add-parser-pg-type-definitions.md new file mode 100644 index 0000000000..f983220556 --- /dev/null +++ b/.changeset/add-parser-pg-type-definitions.md @@ -0,0 +1,9 @@ +--- +'@electric-sql/client': minor +--- + +Add type definitions for PostgreSQL types in the parser option. + +Introduces a `BuiltInPgType` union type that lists all built-in PostgreSQL type names, providing IDE autocomplete and IntelliSense support when defining custom parsers. The `Parser` type now suggests known PostgreSQL types while still allowing custom types like enums, domains, and composite types. + +Also exports `BuiltInPgType`, `Parser`, `ParseFunction`, `pgArrayParser`, and `defaultParser` from the package for easier use by consumers. From 357989122124b16849b2416f27a67bd162b6702d Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 22 Dec 2025 20:43:45 +0000 Subject: [PATCH 3/3] Fix defaultParser type to use satisfies for exact typing --- packages/typescript-client/src/parser.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/typescript-client/src/parser.ts b/packages/typescript-client/src/parser.ts index 3d6c64c43b..6425488686 100644 --- a/packages/typescript-client/src/parser.ts +++ b/packages/typescript-client/src/parser.ts @@ -123,7 +123,7 @@ const parseBigInt = (value: string) => BigInt(value) const parseJson = (value: string) => JSON.parse(value) const identityParser: ParseFunction = (v: string) => v -export const defaultParser: Parser = { +export const defaultParser = { int2: parseNumber, int4: parseNumber, int8: parseBigInt, @@ -132,7 +132,7 @@ export const defaultParser: Parser = { float8: parseNumber, json: parseJson, jsonb: parseJson, -} +} satisfies Parser // Taken from: https://github.com/electric-sql/pglite/blob/main/packages/pglite/src/types.ts#L233-L279 export function pgArrayParser(