import type { JSONValue } from "grafast"; import { exportAs, inspect, isDev, Step } from "grafast"; import type { SQL, SQLRawValue } from "pg-sql2"; import sql from "pg-sql2"; import { parse as rangeParse } from "postgres-range"; import type { CustomInspectFunction } from "util"; import type { PgBox, PgCircle, PgHStore, PgInterval, PgLine, PgLseg, PgPath, PgPoint, PgPolygon, } from "./codecUtils/index.ts"; import { parseBox, parseCircle, parseHstore, parseLine, parseLseg, parsePath, parsePoint, parsePolygon, stringifyBox, stringifyCircle, stringifyHstore, stringifyInterval, stringifyLine, stringifyLseg, stringifyPath, stringifyPoint, stringifyPolygon, } from "./codecUtils/index.ts"; import type { PgExecutor } from "./executor.ts"; import type { PgCodec, PgCodecPolymorphism, PgDecode, PgEncode, PgEnumCodec, PgEnumValue, } from "./interfaces.ts"; import { makeParseArrayWithTransform, parseArray } from "./parseArray.ts"; // PERF: `identity` can be shortcut const identity = (value: T): T => value; export type PgCodecAttributeViaExplicit = { relation: string; attribute: string; }; export type PgCodecAttributeVia = string | PgCodecAttributeViaExplicit; /** @deprecated Use DataplanPg.PgCodecAttributeExtensions instead */ export type PgCodecAttributeExtensions = DataplanPg.PgCodecAttributeExtensions; export interface PgCodecAttribute< TCodec extends PgCodec = PgCodec, TNotNull extends boolean = boolean, > { /** * How to translate to/from PG and how to cast. */ codec: TCodec; /** * Is the column/attribute guaranteed to not be null? */ notNull?: TNotNull; hasDefault?: boolean; /** * The SQL expression for a derivative attributes, e.g.: * * ```js * expression: (alias) => sql`${alias}.first_name || ' ' || ${alias}.last_name` * ``` */ expression?: (alias: SQL) => SQL; /** * If this attribute actually exists on a relation rather than locally, the name * of the (unique) relation this attribute belongs to. */ via?: PgCodecAttributeVia; /** * If the attribute exists identically on a relation and locally (e.g. * `posts.author_id` and `users.id` have exactly the same value due to a * foreign key reference) then the plans can choose which one to grab. * * @remarks * * ``` * create table users (id serial primary key); * create table posts (id serial primary key, author_id int references users); * create table comments (id serial primary key, user_id int references users); * create table pets (id serial primary key, owner_id int references users); * ``` * * Here: * - posts.author_id *identical via* 'author.id' * - comments.user_id *identical via* 'user.id' * - pets.owner_id *identical via* 'owner.id' * * Note however that `users.id` is not *identical via* anything, because * these are all plural relationships. So identicalVia is generally one-way * (except in 1-to-1 relationships). */ identicalVia?: PgCodecAttributeVia; // ENHANCE: can identicalVia be plural? Is that useful? Maybe a attribute that has // multiple foreign key references? // Answer: yes it can be plural - consider `parent.id` is identical to // `child.parent_id` via the `parent` relation. This is in addition to the // existing `item.id` being identical to `topic.id` used by polymorphism. /** * Set this true if you're using column-level select privileges and there are * roles accessible that do not have permission to select it. This will tell * us not to auto-select it to more efficiently resolve row nullability * questions - we'll only try when the user explicitly tells us to. */ restrictedAccess?: boolean; description?: string; 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: * * - castFromPg: how to wrap the SQL fragment that represents this type so that * it's cast to a suitable type for us to receive via the relevant Postgres * driver * - listCastFromPg: as castFromPg, but for usage when the expression type is a * list of this type * - fromPg: parse the value from Postgres into JS format * - toPg: serialize the value from JS into a format Postgres will understand * * param type - the name of the Postgres type - see the `pg_type` table * param options - the configuration options described above */ function t(): < const TName extends string, >( oid: string | undefined, type: TName, options?: CodecOptions, ) => PgCodec< TName, undefined, TFromPostgres, TFromJavaScript, undefined, undefined, undefined > { return (oid, type, options = {}) => { const { castFromPg, listCastFromPg, fromPg, toPg, isBinary, isEnum, hasNaturalOrdering = false, hasNaturalEquality = false, } = options; return { name: type, sqlType: sql.identifier(...type.split(".")), fromPg: fromPg ?? (identity as any), toPg: toPg ?? (identity as any), attributes: undefined, extensions: { oid: oid }, castFromPg, listCastFromPg, executor: null, isBinary, isEnum, hasNaturalOrdering, hasNaturalEquality, [inspect.custom]: codecInspect, }; }; } /** * As `t`, but for simple types (primitives, i.e. things that make sense to be * sorted/ordered) */ function s(): < const TName extends string, >( oid: string | undefined, type: TName, options?: CodecOptions, ) => PgCodec< TName, undefined, TFromPostgres, TFromJavaScript, undefined, undefined, undefined > { return (oid, type, options) => t()(oid, type, { hasNaturalOrdering: true, hasNaturalEquality: true, ...options, }); } /** * | To put a double quote or backslash in a quoted composite field value, * | precede it with a backslash. */ function pgWrapQuotesInCompositeValue(str: string): string { return `"${str.replace(/["\\]/g, "\\$&")}"`; } function toRecordString(val: SQLRawValue): string { if (val == null) { return ""; } else if (typeof val === "boolean") { return val ? "t" : "f"; } else if (typeof val === "number") { return "" + val; } else if ( // essentially Array.isArray in this context typeof val === "object" ) { const parts = val.map((v) => toListString(v)); return `{${parts.join(",")}}`; } else if (/[(),"\\]/.test(val) || val.length === 0) { /* * The Postgres manual states: * * > You can put double quotes around any field value, and must do so if * > it contains commas or parentheses. * * Also: * * > In particular, fields containing parentheses, commas, double quotes, * > or backslashes must be double-quoted. [...] Alternatively, you can * > avoid quoting and use backslash-escaping to protect all data * > characters that would otherwise be taken as composite syntax. * * We're going to go with double quoting. */ return pgWrapQuotesInCompositeValue(val); } else { return "" + val; } } function pgWrapQuotesInArray(str: string): string { return `"${str.replace(/["\\]/g, "\\$&")}"`; } function toListString(val: SQLRawValue): string { if (val == null) { return "NULL"; } else if (typeof val === "boolean") { return val ? "t" : "f"; } else if (typeof val === "number") { return "" + val; } else if ( // essentially Array.isArray in this context typeof val === "object" ) { const parts = val.map((v) => toListString(v)); return `{${parts.join(",")}}`; } else { return pgWrapQuotesInArray(val); } } // TESTS: this needs unit tests! /** * Parses a PostgreSQL record string (e.g. `(1,2, hi)`) into a tuple (e.g. * `["1", "2", " hi"]`). * * Postgres says: * * | The composite output routine will put double quotes around field values if * | they are empty strings or contain parentheses, commas, double quotes, * | backslashes, or white space. (Doing so for white space is not essential, * | but aids legibility.) Double quotes and backslashes embedded in field * | values will be doubled. * * @see {@link https://www.postgresql.org/docs/current/rowtypes.html#id-1.5.7.24.6} */ function recordStringToTuple(value: string): Array { if (!value.startsWith("(") || !value.endsWith(")")) { throw new Error(`Unsupported record string '${value}'`); } let inQuotes = false; let current: string | null = null; const tuple: Array = []; // We only need to loop inside the parenthesis. Whitespace is significant in here. for (let i = 1, l = value.length - 1; i < l; i++) { const char = value[i]; if (inQuotes) { if (current === null) { throw new Error("Impossible?"); } if (char === '"') { // '""' is an escape for '"' if (value[i + 1] === '"') { current += value[++i]; } else { inQuotes = false; // Expect comma or end } } else if (char === "\\") { // Backslash is literal escape current += value[++i]; } else { current += char; } } else if (char === '"') { if (current !== null) { throw new Error( `Invalid record string attempts to open quotes when value already exists '${value}'`, ); } inQuotes = true; current = ""; } else if (char === ",") { tuple.push(current); current = null; } else if (current !== null) { current += char; } else { current = char; } } if (inQuotes) { throw new Error( `Invalid record string; exits whilst still in quote marks '${value}'`, ); } tuple.push(current); return tuple; } function realAttributeDefs( attributes: TAttributes, ): Array<[string, TAttributes[keyof TAttributes]]> { const attributeDefs = Object.entries(attributes) as Array< [string, TAttributes extends infer U ? U[keyof U] : never] >; return attributeDefs.filter( ([_attributeName, spec]) => !spec.expression && !spec.via, ); } /** * Takes a list of attributes and returns a mapping function that takes a * composite value and turns it into a string that PostgreSQL could process as * the composite value. * * @see {@link https://www.postgresql.org/docs/current/rowtypes.html#id-1.5.7.24.6} */ function makeRecordToSQLRawValue( attributes: TAttributes, ): PgEncode> { const attributeDefs = realAttributeDefs(attributes); return (value) => { const values = attributeDefs.map(([attributeName, spec]) => { const v = value[attributeName]; const val = v == null ? null : spec.codec.toPg(v); return toRecordString(val); }); return `(${values.join(",")})`; }; } 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; }; /** * Takes a list of attributes and returns a mapping function that takes a * PostgreSQL record string value (e.g. `(1,2,"hi")`) and turns it into a * JavaScript object. If `asJSON` is true, then instead of a record string value, * we expect a JSON array value (typically due to casting). * * @see {@link https://www.postgresql.org/docs/current/rowtypes.html#id-1.5.7.24.6} */ function makeSQLValueToRecord( attributes: TAttributes, asJSON = false, ): (value: string) => ObjectFromPgCodecAttributes { const attributeDefs = realAttributeDefs(attributes); const attributeCount = attributeDefs.length; return (value) => { const tuple = asJSON ? JSON.parse(value) : recordStringToTuple(value); const record = Object.create(null); for (let i = 0; i < attributeCount; i++) { const [attributeName, spec] = attributeDefs[i]; const entry = tuple[i]; record[attributeName] = entry == null ? null : spec.codec.fromPg(entry); } return record; }; } export type PgRecordTypeCodecSpec< TName extends string, TAttributes extends PgCodecAttributes, > = { name: TName; executor: PgExecutor; identifier: SQL; attributes: TAttributes; polymorphism?: PgCodecPolymorphism; description?: string; extensions?: Partial; isAnonymous?: boolean; }; const codecInspect: CustomInspectFunction = function (this: PgCodec) { const type = this.domainOfCodec ? `DomainCodec<${this.domainOfCodec.name}>` : this.arrayOfCodec ? `ListCodec<${this.arrayOfCodec.name}[]>` : this.rangeOfCodec ? `RangeCodec<${this.rangeOfCodec.name}>` : this.baseCodec ? `ModifiedCodec<${this.baseCodec.name}>` : this.attributes ? `RecordCodec` : "Codec"; return `${type}(${this.name})`; }; /** * Returns a PgCodec that represents a composite type (a type with * attributes). * * name - the name of this type * identifier - a pg-sql2 fragment that uniquely identifies this type, suitable to be fed after `::` into an SQL query. * attributes - the attributes this composite type has * extensions - an optional object that you can use to associate arbitrary data with this type * isAnonymous - if true, this represents an "anonymous" type, typically the return value of a function or something like that. If this is true, then name and identifier are ignored. */ export function recordCodec< const TName extends string, const TAttributes extends PgCodecAttributes, >( config: PgRecordTypeCodecSpec, ): PgCodec< TName, TAttributes, string, ObjectFromPgCodecAttributes, undefined, undefined, undefined > { const { name, identifier, attributes, polymorphism, description, extensions, isAnonymous = false, executor, } = config; return { name, sqlType: identifier, isAnonymous, ...makeRecordCodecToFrom(name, attributes), attributes, polymorphism, description, extensions, executor, [inspect.custom]: codecInspect, }; } exportAs("@dataplan/pg", recordCodec, "recordCodec"); function listCastViaUnnest( name: string, frag: SQL, castFromPg: (identifier: SQL) => SQL, guaranteedNotNull?: boolean, ) { const identifier = sql.identifier(Symbol(name)); const arraySql = sql`array(${sql.indent( sql`select ${castFromPg(identifier)}\nfrom unnest(${frag}) ${identifier}`, )})::text`; if (guaranteedNotNull) { return arraySql; } else { return sql`(case when (${frag}) is not distinct from null then null::text else ${arraySql} end)`; } } function makeRecordExpression( fragment: SQL, attributeDefs: ReadonlyArray<[string, PgCodecAttribute]>, ) { /** comma-separated list */ const csl = sql.join( attributeDefs.map(([attrName, attr]) => { const expr = sql`((${fragment}).${sql.identifier(attrName)})`; if (attr.codec.castFromPg) { return attr.codec.castFromPg(expr, attr.codec.notNull); } else { return sql`(${expr})::text`; } }), ",\n", ); if (attributeDefs.length <= 100) { return sql`json_build_array(${sql.indent(csl)})`; } else { // if (attributeDefs.length <= 16383) { return sql`to_json(array[${sql.indent(csl)}])`; } } function makeRecordCodecToFrom( name: string, attributes: TAttributes, ): Pick { const attributeDefs = realAttributeDefs(attributes); if (attributeDefs.some(([_attrName, attr]) => attr.codec.castFromPg)) { const castFromPg = (fragment: SQL) => sql`case when (${fragment}) is not distinct from null then null::text else ${makeRecordExpression(fragment, attributeDefs)}::text end`; return { castFromPg, listCastFromPg(frag, guaranteedNotNull) { return listCastViaUnnest(name, frag, castFromPg, guaranteedNotNull); }, fromPg: makeSQLValueToRecord(attributes, true), toPg: makeRecordToSQLRawValue(attributes), }; } else { return { fromPg: makeSQLValueToRecord(attributes), toPg: makeRecordToSQLRawValue(attributes), }; } } export type PgEnumCodecSpec = { name: TName; identifier: SQL; values: Array | TValue>; description?: string; extensions?: Partial; }; /** * Returns a PgCodec that represents a Postgres enum type. * * - name - the name of the enum * - identifier - a pg-sql2 fragment that uniquely identifies this type, suitable to be fed after `::` into an SQL query. * - values - a list of the values that this enum can represent * - extensions - an optional object that you can use to associate arbitrary data with this type */ export function enumCodec< const TName extends string, const TValue extends string, >(config: PgEnumCodecSpec): PgEnumCodec { const { name, identifier, values, description, extensions } = config; return { name, sqlType: identifier, fromPg: identity as (val: string) => TValue, toPg: identity, values: values.map((value) => typeof value === "string" ? { value } : value, ), attributes: undefined, description, extensions, executor: null, hasNaturalOrdering: false, hasNaturalEquality: true, isEnum: true, }; } exportAs("@dataplan/pg", enumCodec, "enumCodec"); export function isEnumCodec< TName extends string, TValue extends string = string, >( t: PgCodec, ): t is PgEnumCodec { return "values" in t; } const $$listCodec = Symbol("listCodec"); type CodecWithListCodec< TCodec extends PgCodec, > = TCodec & { [$$listCodec]?: PgCodec< `${TCodec extends PgCodec ? UName : never}[]`, undefined, string, TCodec extends PgCodec ? UFromJs[] : any[], TCodec, undefined, undefined >; }; type PgCodecTFromJavaScript< TInnerCodec extends PgCodec, > = TInnerCodec extends PgCodec ? UFromJs : any; function listEncode( innerCodecToPg: PgEncode, typeDelim: string, value: readonly unknown[], ) { let result = "{"; for (let i = 0, l = value.length; i < l; i++) { if (i > 0) { result += typeDelim; } const v = value[i]; if (v == null) { result += "NULL"; continue; } const str = innerCodecToPg(v); if (str == null) { result += "NULL"; continue; } if (typeof str !== "string" && typeof str !== "number") { throw new Error( `Do not know how to encode ${inspect(str)} to an array (send a PR!)`, ); } // > To put a double quote or backslash in a quoted array element // > value, precede it with a backslash. // -- https://www.postgresql.org/docs/current/arrays.html#ARRAYS-IO result += `"${String(str).replace(/[\\"]/g, "\\$&")}"`; } result += "}"; return result; } /** * Given a PgCodec, this returns a new PgCodec that represents a list * of the former. * * List codecs CANNOT BE NESTED - Postgres array types don't have defined * dimensionality, so an array of an array of a type doesn't really make sense * to Postgres, it being the same as an array of the type. * * @param listedCodec - the codec that represents the "inner type" of the array * @param config - optional configuration for the list codec */ export function listOfCodec< TInnerCodec extends PgCodec, TName extends string = `${TInnerCodec extends PgCodec< infer UName, any, any, any, any, any, any > ? UName : never}[]`, >( listedCodec: TInnerCodec, config?: { /** Description for this list type. */ description?: string; /** Metadata to associate with this list type. */ extensions?: Partial; /** Delimiter used to separate entries when Postgres stringifies it. */ typeDelim?: string; /** pg-sql2 fragment that represents the name of this type. */ identifier?: SQL; /** Name for this list type. */ name?: TName; }, ): PgCodec< TName, undefined, // Array has no attributes string, readonly PgCodecTFromJavaScript[], TInnerCodec, undefined, undefined > { const innerCodec: CodecWithListCodec = listedCodec; if (!config && innerCodec[$$listCodec]) { return innerCodec[$$listCodec] as any; } const { description, extensions, identifier = sql`${listedCodec.sqlType}[]`, typeDelim = `,`, name = `${innerCodec.name}[]` as TName, } = config ?? ({} as Record); const { fromPg: innerCodecFromPg, toPg: innerCodecToPg, listCastFromPg: innerCodecListCastFromPg, notNull: innerCodecNotNull, executor, } = innerCodec; const listCodec: PgCodec< TName, undefined, // Array has no attributes string, readonly PgCodecTFromJavaScript[], TInnerCodec, undefined, undefined > = { name, sqlType: identifier, fromPg: innerCodecFromPg === identity && typeDelim === "," ? parseArray : makeParseArrayWithTransform(innerCodecFromPg, typeDelim), toPg: (value) => listEncode(innerCodecToPg, typeDelim, value), attributes: undefined, description, extensions, arrayOfCodec: innerCodec, ...(innerCodecListCastFromPg ? { castFromPg: innerCodecListCastFromPg, listCastFromPg(frag, guaranteedNotNull) { return listCastViaUnnest( `${name}_item`, frag, (identifier) => innerCodecListCastFromPg.call( this, identifier, innerCodecNotNull, ), guaranteedNotNull, ); }, } : null), executor: executor, [inspect.custom]: codecInspect, }; if (!config) { // Memoize such that every `listOfCodec(foo)` returns the same object. Object.defineProperty(innerCodec, $$listCodec, { value: listCodec }); } return listCodec; } exportAs("@dataplan/pg", listOfCodec, "listOfCodec"); /** * Represents a PostgreSQL `DOMAIN` over the given codec * * @param innerCodec - the codec that represents the "inner type" of the domain * @param name - the name of the domain * @param identifier - a pg-sql2 fragment that represents the name of this type * @param config - extra details about this domain */ export function domainOfCodec< TName extends string, TInnerCodec extends PgCodec, >( innerCodec: TInnerCodec, name: TName, identifier: SQL, config: { /** Description for this domain. */ description?: string; /** Metadata to associate with this domain. */ extensions?: Partial; /** Whether this domain is not nullable. */ notNull?: boolean | null; /** Whether this domain has a default. */ hasDefault?: boolean | null; } = {}, ): PgCodec< TName, TInnerCodec extends PgCodec ? U : any, TInnerCodec extends PgCodec ? U : any, undefined, TInnerCodec, undefined > { const { description, extensions, notNull, hasDefault } = config; return { // Generally same as underlying type: ...innerCodec, // Overriding: name, sqlType: identifier, description, extensions, domainOfCodec: innerCodec.arrayOfCodec ? undefined : innerCodec, notNull: Boolean(notNull), hasDefault: Boolean(hasDefault), [inspect.custom]: codecInspect, }; } exportAs("@dataplan/pg", domainOfCodec, "domainOfCodec"); /** * @see {@link https://www.postgresql.org/docs/14/rangetypes.html#RANGETYPES-IO} * * @internal */ function escapeRangeValue< TInnerCodec extends PgCodec, >(value: null | any, innerCodec: TInnerCodec): string { if (value == null) { return ""; } const encoded = "" + (innerCodec.toPg(value) ?? ""); // PERF: we don't always need to do this return `"${encoded.replace(/"/g, '""')}"`; } /** The value of a 'range' type from Postgres */ export type PgRangeValue = | { empty: true } | { empty: false; /** The lower bound; inclusive or exclusive. If null, there is no lower bound */ start: { value: T; inclusive: boolean } | null; /** The upper bound; inclusive or exclusive. If null, there is no upper bound */ end: { value: T; inclusive: boolean } | null; }; export type PgCodecPGDatatype = TCodec extends PgCodec ? TValue : never; export type PgCodecJSDatatype = TCodec extends PgCodec ? TValue : never; /** * Returns a PgCodec that represents a range of the given inner PgCodec * type. * * @param innerCodec - the PgCodec that represents the bounds of this range * @param name - the name of the range * @param identifier - a pg-sql2 fragment that represents the name of this type * @param config - extra details about this range */ export function rangeOfCodec< TName extends string, TInnerCodec extends PgCodec< any, undefined, any, any, undefined, any, undefined >, >( innerCodec: TInnerCodec, name: TName, identifier: SQL, config: { /** Description for this range. */ description?: string; /** Metadata to associate with this range. */ extensions?: Partial; } = {}, ): PgCodec< TName, undefined, string, PgRangeValue>, undefined, undefined, TInnerCodec > { const { description, extensions } = config; const needsCast = innerCodec.castFromPg; const castFromPg = needsCast ? function castFromPg(frag: SQL) { return sql`json_build_array(${sql.indent( sql`lower_inc(${frag}),\n${innerCodec.castFromPg!( sql`lower(${frag})`, innerCodec.notNull, )},\n${innerCodec.castFromPg!( sql`upper(${frag})`, innerCodec.notNull, )},\nupper_inc(${frag}),\nisempty(${frag})`, )})::text`; } : null; return { name, sqlType: identifier, description, extensions, rangeOfCodec: innerCodec, ...(castFromPg ? { castFromPg, listCastFromPg(frag, guaranteedNotNull) { return listCastViaUnnest(name, frag, castFromPg, guaranteedNotNull); }, } : null), fromPg: needsCast ? function (value) { const [lowerInc, lower, upper, upperInc, empty] = JSON.parse(value); return empty ? { empty: true } : { empty: false, start: lower != null ? { value: innerCodec.fromPg(lower), inclusive: !!lowerInc } : null, end: upper != null ? { value: innerCodec.fromPg(upper), inclusive: !!upperInc } : null, }; } : function (value) { const parsed = rangeParse(value); return parsed.isEmpty() ? { empty: true } : { empty: false, start: parsed.lower != null ? { value: innerCodec.fromPg(parsed.lower), inclusive: parsed.isLowerBoundClosed(), } : null, end: parsed.upper != null ? { value: innerCodec.fromPg(parsed.upper), inclusive: parsed.isUpperBoundClosed(), } : null, }; }, toPg(value) { if (value.empty) { return "empty"; } let str = ""; if (value.start == null) { str += "("; } else { str += `${value.start.inclusive ? "[" : "("}${escapeRangeValue( value.start.value, innerCodec, )}`; } str += ","; if (value.end == null) { str += ")"; } else { str += `${escapeRangeValue(value.end.value, innerCodec)}${ value.end.inclusive ? "]" : ")" }`; } return str; }, attributes: undefined, executor: innerCodec.executor, [inspect.custom]: codecInspect, }; } exportAs("@dataplan/pg", rangeOfCodec, "rangeOfCodec"); /** * Helper type for common casting methods. */ type CodecOptions = { castFromPg?(frag: SQL, guaranteedNotNull?: boolean): SQL; listCastFromPg?(frag: SQL, guaranteedNotNull?: boolean): SQL; toPg?: PgEncode; fromPg?: PgDecode; isBinary?: boolean; isEnum?: boolean; /** * True if doing an equality check for this value would have intuitive * results for a human. E.g. `3.0` and `3.0000` when encoded as `float` are * the same as a human would expect, so `float` has natural equality. On the * other hand Postgres sees the `json` `{"a":1}` as different to * `{ "a": 1 }`), whereas a human would see these as the same JSON objects, * so `json` does not have natural equality. * * Typically true primitives will set this true. */ hasNaturalEquality?: boolean; /** * True if this type has a natural ordering that would be intuitive for a human. * For example numbers and text have natural ordering, whereas `{"a":1}` and * `{ "a": 2 }` are not so obvious. Similarly, a `point` could be ordered in many * ways relative to another point (x-first, then y; y-first, then x; distance * from origin first, then angle; etc) so do not have natural order. * * Typically true primitives will set this true. */ hasNaturalOrdering?: boolean; }; /** * When we can use the raw representation directly, typically suitable for * text, varchar, char, etc */ const verbatim: CodecOptions = { castFromPg: (frag: SQL): SQL => frag, }; /** * For tsquery/tsvector, natural equality is present (comparisons are done * after parsing, so minor whitespace differences don't matter), but there's no * real natural order for them. */ const textsearch: CodecOptions = { // Use built in `castFromPg` (::text) because we don't know the various // drivers won't parse it. hasNaturalEquality: true, hasNaturalOrdering: false, }; /** * Casts to something else before casting to text; e.g. `${expression}::numeric::text` */ const castVia = (via: SQL): CodecOptions => ({ castFromPg(frag) { return sql`${sql.parens(frag)}::${via}::text`; }, listCastFromPg(frag) { return sql`${sql.parens(frag)}::${via}[]::text[]::text`; }, }); const viaNumeric = castVia(sql`numeric`); // const viaJson = castVia(sql`json`); /** * Casts using to_char to format dates; also handles arrays via unnest. */ const viaDateFormat = ( format: string, prefix: SQL = sql.blank, ): CodecOptions => { const sqlFormat = sql.literal(format); function castFromPg(frag: SQL) { return sql`to_char(${prefix}${frag}, ${sqlFormat}::text)`; } return { castFromPg, listCastFromPg(frag, guaranteedNotNull) { return listCastViaUnnest("entry", frag, castFromPg, guaranteedNotNull); }, }; }; const parseAsTrustedInt = (n: string) => +n; const jsonParse = (s: string) => JSON.parse(s); const jsonStringify = (o: JSONValue) => JSON.stringify(o); const stripSubnet32 = { fromPg(value: string) { return value.replace(/\/(32|128)$/, ""); }, }; const reg = { hasNaturalOrdering: false }; // Rough emulation of what I think the `pg` module does function encodeUnknown(value: unknown): string | null { if (value == null) { return null; } const t = typeof value; if (t === "string") { return value as string; } else if (t === "boolean") { return String(value); } else if (t === "number") { return String(value); } else if (Array.isArray(value)) { return listEncode(encodeUnknown, ",", value); } else if (value instanceof Date) { return value.toISOString(); } else if (Buffer.isBuffer(value)) { throw new Error("Encoding buffer not supported for unknown"); } else if (t === "object") { return JSON.stringify(value); } else { return String(value); } } /** * Built in PostgreSQL types that we support; note the keys are the "ergonomic" * names (like 'bigint'), but the values use the underlying PostgreSQL true * names (those that would be found in the `pg_type` table). */ export const TYPES = { /** Experimental support for "unknown" data on input. @experimental */ unknown: t()("705", "unknown", { toPg(value) { return encodeUnknown(value); }, fromPg(value) { // TODO: warning? return String(value); }, }), // unknown: 705 void: t()("2278", "void"), // void: 2278 boolean: s()("16", "bool", { fromPg: (value) => value[0] === "t", toPg: (v) => { if (v === true) { return "t"; } else if (v === false) { return "f"; } else { throw new Error( `${v} isn't a boolean; cowardly refusing to cast it to postgres`, ); } }, }), int2: s()("21", "int2", { fromPg: parseAsTrustedInt }), int: s()("23", "int4", { fromPg: parseAsTrustedInt }), oid: s()("26", "oid", { fromPg: parseAsTrustedInt }), bigint: s()("20", "int8"), float4: s()("700", "float4", { fromPg: parseFloat }), float: s()("701", "float8", { fromPg: parseFloat }), money: s()("790", "money", viaNumeric), numeric: s()("1700", "numeric"), char: s()("18", "char", verbatim), bpchar: s()("1042", "bpchar", verbatim), varchar: s()("1043", "varchar", verbatim), text: s()("25", "text", verbatim), name: s()("19", "name", verbatim), json: t()("114", "json", { fromPg: jsonParse, toPg: jsonStringify, }), jsonb: t()("3802", "jsonb", { fromPg: jsonParse, toPg: jsonStringify, // We could totally add the following if someone wanted it: // hasNaturalEquality: true, }), jsonpath: t()("4072", "jsonpath"), xml: t()("142", "xml"), tsvector: t()("3614", "tsvector", textsearch), tsquery: t()("3615", "tsquery", textsearch), citext: s()(undefined, "citext", verbatim), uuid: s()("2950", "uuid", verbatim), timestamp: s()( "1114", "timestamp", viaDateFormat('YYYY-MM-DD"T"HH24:MI:SS.US'), ), timestamptz: s()( "1184", "timestamptz", viaDateFormat('YYYY-MM-DD"T"HH24:MI:SS.USTZH:TZM'), ), date: s()("1082", "date", viaDateFormat("YYYY-MM-DD")), time: s()( "1083", "time", viaDateFormat("HH24:MI:SS.US", sql`date '1970-01-01' + `), ), timetz: s()( "1266", "timetz", viaDateFormat("HH24:MI:SS.USTZH:TZM", sql`date '1970-01-01' + `), ), inet: s()("869", "inet", stripSubnet32), regproc: s()("24", "regproc", reg), regprocedure: s()("2202", "regprocedure", reg), regoper: s()("2203", "regoper", reg), regoperator: s()("2204", "regoperator", reg), regclass: s()("2205", "regclass", reg), regtype: s()("2206", "regtype", reg), regrole: s()("4096", "regrole", reg), regnamespace: s()("4089", "regnamespace", reg), regconfig: s()("3734", "regconfig", reg), regdictionary: s()("3769", "regdictionary", reg), cidr: s()("650", "cidr"), macaddr: s()("829", "macaddr"), macaddr8: s()("774", "macaddr8"), interval: s()("1186", "interval", { ...viaDateFormat(`YYYY_MM_DD_HH24_MI_SS.US`), fromPg(value: string): PgInterval { const parts = value.split("_").map(parseFloat); // Note these are actually all integers except for `seconds`. const [years, months, days, hours, minutes, seconds] = parts; return { years, months, days, hours, minutes, seconds }; }, toPg: stringifyInterval, }), bit: s()("1560", "bit", { hasNaturalOrdering: false }), varbit: s()("1562", "varbit", { hasNaturalOrdering: false }), point: t()("600", "point", { fromPg: parsePoint, toPg: stringifyPoint, hasNaturalEquality: true, }), line: t()("628", "line", { fromPg: parseLine, toPg: stringifyLine }), lseg: t()("601", "lseg", { fromPg: parseLseg, toPg: stringifyLseg }), box: t()("603", "box", { fromPg: parseBox, toPg: stringifyBox }), path: t()("602", "path", { fromPg: parsePath, toPg: stringifyPath, hasNaturalEquality: true, }), polygon: t()("604", "polygon", { fromPg: parsePolygon, toPg: stringifyPolygon, }), circle: t()("718", "circle", { fromPg: parseCircle, toPg: stringifyCircle, }), hstore: t()(undefined, "hstore", { fromPg: parseHstore, toPg: stringifyHstore, }), bytea: t()("17", "bytea", { fromPg(str) { // The bytea type supports two formats for input and output: “hex” // format and PostgreSQL's historical “escape” format. Both of these // are always accepted on input. The output format depends on the // configuration parameter bytea_output; the default is hex. // -- https://www.postgresql.org/docs/current/datatype-binary.html if (str.startsWith("\\x")) { // Hex format return Buffer.from(str.substring(2), "hex"); } else { // ENHANCE: consider supporting this throw new Error( `PostgreSQL bytea escape format is currently unsupported, please use \`bytea_output = 'hex'\` in your PostgreSQL configuration.`, ); } }, toPg(data: Buffer) { return `\\x${data.toString("hex")}`; }, isBinary: true, }), } as const; exportAs("@dataplan/pg", TYPES, "TYPES"); for (const [name, codec] of Object.entries(TYPES)) { exportAs("@dataplan/pg", codec, ["TYPES", name]); } function builtinListOfCodec( oid: string, codec: TCodec, typeDelim = ",", ) { return listOfCodec(codec, { name: `${codec.name}Array`, typeDelim, extensions: { oid }, }); } export const LIST_TYPES = { /* ```sql with names as ( select name, idx from unnest(array[ 'boolean', 'int2', 'int', 'oid' 'bigint', 'float4', 'float', 'money', 'numeric', 'char', 'bpchar', 'varchar', 'text', 'name', 'json', 'jsonb', 'jsonpath', 'xml', 'citext', 'uuid', 'timestamp', 'timestamptz', 'date', 'time', 'timetz', 'inet', 'regproc', 'regprocedure', 'regoper', 'regoperator', 'regclass', 'regtype', 'regrole', 'regnamespace', 'regconfig', 'regdictionary', 'cidr', 'macaddr', 'macaddr8', 'interval', 'bit', 'varbit', 'point', 'line', 'lseg', 'box', 'path', 'polygon', 'circle', 'hstore', 'bytea' ]) with ordinality as names(name, idx) ) select name || ': builtinListOfCodec("' || oid || '", TYPES.' || name || (case when typdelim = ',' then '' else ', "' || typdelim::text || '"' end) || '),' from names inner join pg_type t on t.oid = (select typarray from pg_type t2 where t2.oid = name::regtype) order by idx asc; ``` */ boolean: builtinListOfCodec("1000", TYPES.boolean), int2: builtinListOfCodec("1005", TYPES.int2), int: builtinListOfCodec("1007", TYPES.int), oid: builtinListOfCodec("1028", TYPES.oid), bigint: builtinListOfCodec("1016", TYPES.bigint), float4: builtinListOfCodec("1021", TYPES.float4), float: builtinListOfCodec("1022", TYPES.float), money: builtinListOfCodec("791", TYPES.money), numeric: builtinListOfCodec("1231", TYPES.numeric), char: builtinListOfCodec("1014", TYPES.char), bpchar: builtinListOfCodec("1014", TYPES.bpchar), varchar: builtinListOfCodec("1015", TYPES.varchar), text: builtinListOfCodec("1009", TYPES.text), name: builtinListOfCodec("1003", TYPES.name), json: builtinListOfCodec("199", TYPES.json), jsonb: builtinListOfCodec("3807", TYPES.jsonb), jsonpath: builtinListOfCodec("4073", TYPES.jsonpath), xml: builtinListOfCodec("143", TYPES.xml), tsvector: builtinListOfCodec("3643", TYPES.tsvector), tsquery: builtinListOfCodec("3645", TYPES.tsquery), citext: builtinListOfCodec("20277428", TYPES.citext), uuid: builtinListOfCodec("2951", TYPES.uuid), timestamp: builtinListOfCodec("1115", TYPES.timestamp), timestamptz: builtinListOfCodec("1185", TYPES.timestamptz), date: builtinListOfCodec("1182", TYPES.date), time: builtinListOfCodec("1183", TYPES.time), timetz: builtinListOfCodec("1270", TYPES.timetz), inet: builtinListOfCodec("1041", TYPES.inet), regproc: builtinListOfCodec("1008", TYPES.regproc), regprocedure: builtinListOfCodec("2207", TYPES.regprocedure), regoper: builtinListOfCodec("2208", TYPES.regoper), regoperator: builtinListOfCodec("2209", TYPES.regoperator), regclass: builtinListOfCodec("2210", TYPES.regclass), regtype: builtinListOfCodec("2211", TYPES.regtype), regrole: builtinListOfCodec("4097", TYPES.regrole), regnamespace: builtinListOfCodec("4090", TYPES.regnamespace), regconfig: builtinListOfCodec("3735", TYPES.regconfig), regdictionary: builtinListOfCodec("3770", TYPES.regdictionary), cidr: builtinListOfCodec("651", TYPES.cidr), macaddr: builtinListOfCodec("1040", TYPES.macaddr), macaddr8: builtinListOfCodec("775", TYPES.macaddr8), interval: builtinListOfCodec("1187", TYPES.interval), bit: builtinListOfCodec("1561", TYPES.bit), varbit: builtinListOfCodec("1563", TYPES.varbit), point: builtinListOfCodec("1017", TYPES.point), line: builtinListOfCodec("629", TYPES.line), lseg: builtinListOfCodec("1018", TYPES.lseg), box: builtinListOfCodec("1020", TYPES.box, ";"), path: builtinListOfCodec("1019", TYPES.path), polygon: builtinListOfCodec("1027", TYPES.polygon), circle: builtinListOfCodec("719", TYPES.circle), hstore: builtinListOfCodec("20278122", TYPES.hstore), bytea: builtinListOfCodec("1001", TYPES.bytea), } satisfies { [name in Exclude< keyof typeof TYPES, "unknown" | "void" >]: (typeof TYPES)[name] extends PgCodec< infer UName, any, any, any, any, any, any > ? PgCodec< `${UName}Array`, undefined, string, readonly PgCodecTFromJavaScript<(typeof TYPES)[name]>[], (typeof TYPES)[name], undefined, undefined > : never; }; exportAs("@dataplan/pg", LIST_TYPES, "LIST_TYPES"); for (const [name, codec] of Object.entries(LIST_TYPES)) { exportAs("@dataplan/pg", codec, ["LIST_TYPES", name]); } /** * For supported builtin type names ('void', 'bool', etc) that will be found in * the `pg_catalog` table this will return a PgCodec. */ export function getCodecByPgCatalogTypeName(pgCatalogTypeName: string) { switch (pgCatalogTypeName) { case "unknown": return TYPES.unknown; case "void": return TYPES.void; case "bool": return TYPES.boolean; case "_bool": return LIST_TYPES.boolean; case "bytea": return TYPES.bytea; // oid: 17 case "_bytea": return LIST_TYPES.bytea; // oid: 17 case "char": return TYPES.char; case "_char": return LIST_TYPES.char; case "bpchar": return TYPES.bpchar; case "_bpchar": return LIST_TYPES.bpchar; case "varchar": return TYPES.varchar; case "_varchar": return LIST_TYPES.varchar; case "text": return TYPES.text; case "_text": return LIST_TYPES.text; case "name": return TYPES.name; case "_name": return LIST_TYPES.name; case "uuid": return TYPES.uuid; case "_uuid": return LIST_TYPES.uuid; case "xml": return TYPES.xml; case "_xml": return LIST_TYPES.xml; case "tsvector": return TYPES.tsvector; case "_tsvector": return LIST_TYPES.tsvector; case "tsquery": return TYPES.tsquery; case "_tsquery": return LIST_TYPES.tsquery; case "json": return TYPES.json; case "_json": return LIST_TYPES.json; case "jsonb": return TYPES.jsonb; case "_jsonb": return LIST_TYPES.jsonb; case "jsonpath": return TYPES.jsonpath; case "_jsonpath": return LIST_TYPES.jsonpath; case "bit": return TYPES.bit; case "_bit": return LIST_TYPES.bit; case "varbit": return TYPES.varbit; case "_varbit": return LIST_TYPES.varbit; case "int2": return TYPES.int2; case "_int2": return LIST_TYPES.int2; case "int4": return TYPES.int; case "_int4": return LIST_TYPES.int; case "int8": return TYPES.bigint; case "_int8": return LIST_TYPES.bigint; case "float8": return TYPES.float; case "_float8": return LIST_TYPES.float; case "float4": return TYPES.float4; case "_float4": return LIST_TYPES.float4; case "numeric": return TYPES.numeric; case "_numeric": return LIST_TYPES.numeric; case "money": return TYPES.money; case "_money": return LIST_TYPES.money; case "box": return TYPES.box; case "_box": return LIST_TYPES.box; case "point": return TYPES.point; case "_point": return LIST_TYPES.point; case "path": return TYPES.path; case "_path": return LIST_TYPES.path; case "line": return TYPES.line; case "_line": return LIST_TYPES.line; case "lseg": return TYPES.lseg; case "_lseg": return LIST_TYPES.lseg; case "circle": return TYPES.circle; case "_circle": return LIST_TYPES.circle; case "polygon": return TYPES.polygon; case "_polygon": return LIST_TYPES.polygon; case "cidr": return TYPES.cidr; case "_cidr": return LIST_TYPES.cidr; case "inet": return TYPES.inet; case "_inet": return LIST_TYPES.inet; case "macaddr": return TYPES.macaddr; case "_macaddr": return LIST_TYPES.macaddr; case "macaddr8": return TYPES.macaddr8; case "_macaddr8": return LIST_TYPES.macaddr8; case "date": return TYPES.date; case "_date": return LIST_TYPES.date; case "timestamp": return TYPES.timestamp; case "_timestamp": return LIST_TYPES.timestamp; case "timestamptz": return TYPES.timestamptz; case "_timestamptz": return LIST_TYPES.timestamptz; case "time": return TYPES.time; case "_time": return LIST_TYPES.time; case "timetz": return TYPES.timetz; case "_timetz": return LIST_TYPES.timetz; case "interval": return TYPES.interval; case "_interval": return LIST_TYPES.interval; case "oid": return TYPES.oid; case "_oid": return LIST_TYPES.oid; case "regclass": return TYPES.regclass; case "_regclass": return LIST_TYPES.regclass; case "regconfig": return TYPES.regconfig; case "_regconfig": return LIST_TYPES.regconfig; case "regdictionary": return TYPES.regdictionary; case "_regdictionary": return LIST_TYPES.regdictionary; case "regnamespace": return TYPES.regnamespace; case "_regnamespace": return LIST_TYPES.regnamespace; case "regoper": return TYPES.regoper; case "_regoper": return LIST_TYPES.regoper; case "regoperator": return TYPES.regoperator; case "_regoperator": return LIST_TYPES.regoperator; case "regproc": return TYPES.regproc; case "_regproc": return LIST_TYPES.regproc; case "regprocedure": return TYPES.regprocedure; case "_regprocedure": return LIST_TYPES.regprocedure; case "regrole": return TYPES.regrole; case "_regrole": return LIST_TYPES.regrole; case "regtype": return TYPES.regtype; case "_regtype": return LIST_TYPES.regtype; } return null; } export function getInnerCodec< TCodec extends PgCodec, >( codec: TCodec, ): TCodec extends PgCodec< any, any, any, infer UArray, infer UDomain, infer URange > ? Exclude : TCodec { if (codec.domainOfCodec) { return getInnerCodec(codec.domainOfCodec) as any; } if (codec.arrayOfCodec) { return getInnerCodec(codec.arrayOfCodec) as any; } if (codec.rangeOfCodec) { return getInnerCodec(codec.rangeOfCodec) as any; } if (codec.baseCodec) { return getInnerCodec(codec.baseCodec) as any; } return codec as any; } exportAs("@dataplan/pg", getInnerCodec, "getInnerCodec"); export function sqlValueWithCodec(value: unknown, codec: PgCodec) { if (isDev && value instanceof Step) { throw new Error( `sqlValueWithCodec(value, codec) is meant to be called at _execution_ time with runtime values; you've called it with a Step indicating maybe you called it at planning time? You probably want to call something like \`$pgSelect.placeholder($value, codec)\` instead.`, ); } return sql`${sql.value(value == null ? null : codec.toPg(value))}::${ codec.sqlType }`; }