@@ -38,8 +38,12 @@ import {
38
38
import type { PgExecutor } from "./executor.js" ;
39
39
import { inspect } from "./inspect.js" ;
40
40
import type {
41
+ AnyPgCodec ,
41
42
PgCodec ,
43
+ PgCodecAttributes ,
44
+ PgCodecAttributesRecord ,
42
45
PgCodecExtensions ,
46
+ PgCodecFromPostgres ,
43
47
PgCodecPolymorphism ,
44
48
PgDecode ,
45
49
PgEncode ,
@@ -59,10 +63,22 @@ export type PgCodecAttributeVia = string | PgCodecAttributeViaExplicit;
59
63
/** @deprecated Use DataplanPg.PgCodecAttributeExtensions instead */
60
64
export type PgCodecAttributeExtensions = DataplanPg . PgCodecAttributeExtensions ;
61
65
66
+ export type PgCodecAttributeName < U > = U extends PgCodecAttribute <
67
+ infer TName ,
68
+ any ,
69
+ any
70
+ >
71
+ ? TName
72
+ : never ;
73
+
74
+ export interface AnyPgCodecAttribute extends PgCodecAttribute { }
75
+
62
76
export interface PgCodecAttribute <
63
- TCodec extends PgCodec = PgCodec ,
77
+ TName extends string = string ,
78
+ TCodec extends PgCodec = AnyPgCodec ,
64
79
TNotNull extends boolean = boolean ,
65
80
> {
81
+ name : TName ;
66
82
/**
67
83
* How to translate to/from PG and how to cast.
68
84
*/
@@ -129,14 +145,6 @@ export interface PgCodecAttribute<
129
145
extensions ?: Partial < PgCodecAttributeExtensions > ;
130
146
}
131
147
132
- export type PgCodecAttributes <
133
- TCodecMap extends {
134
- [ attributeName in string ] : PgCodecAttribute ;
135
- } = {
136
- [ attributeName in string ] : PgCodecAttribute ;
137
- } ,
138
- > = TCodecMap ;
139
-
140
148
/**
141
149
* Returns a PgCodec for the given builtin Postgres scalar type, optionally
142
150
* pass the following config:
@@ -160,12 +168,12 @@ function t<TFromJavaScript = any, TFromPostgres = string>(): <
160
168
options ?: Cast < TFromJavaScript , TFromPostgres > ,
161
169
) => PgCodec <
162
170
TName ,
163
- undefined ,
171
+ never ,
164
172
TFromPostgres ,
165
173
TFromJavaScript ,
166
- undefined ,
167
- undefined ,
168
- undefined
174
+ never ,
175
+ never ,
176
+ never
169
177
> {
170
178
return ( oid , type , options = { } ) => {
171
179
const { castFromPg, listCastFromPg, fromPg, toPg, isBinary } = options ;
@@ -174,7 +182,6 @@ function t<TFromJavaScript = any, TFromPostgres = string>(): <
174
182
sqlType : sql . identifier ( ...type . split ( "." ) ) ,
175
183
fromPg : fromPg ?? ( identity as any ) ,
176
184
toPg : toPg ?? ( identity as any ) ,
177
- attributes : undefined ,
178
185
extensions : { oid : oid } ,
179
186
castFromPg,
180
187
listCastFromPg,
@@ -318,11 +325,11 @@ function recordStringToTuple(value: string): Array<string | null> {
318
325
return tuple ;
319
326
}
320
327
321
- function realAttributeDefs < TAttributes extends PgCodecAttributes > (
322
- attributes : TAttributes ,
323
- ) : Array < [ string , TAttributes [ keyof TAttributes ] ] > {
328
+ function realAttributeDefs < TAttributes extends ReadonlyArray < PgCodecAttribute > > (
329
+ attributes : PgCodecAttributesRecord < TAttributes > ,
330
+ ) : Array < [ PgCodecAttributeName < TAttributes [ number ] > , TAttributes [ number ] ] > {
324
331
const attributeDefs = Object . entries ( attributes ) as Array <
325
- [ string , TAttributes extends infer U ? U [ keyof U ] : never ]
332
+ [ PgCodecAttributeName < TAttributes [ number ] > , TAttributes [ number ] ]
326
333
> ;
327
334
return attributeDefs . filter (
328
335
( [ _attributeName , spec ] ) => ! spec . expression && ! spec . via ,
@@ -336,8 +343,10 @@ function realAttributeDefs<TAttributes extends PgCodecAttributes>(
336
343
*
337
344
* @see {@link https://www.postgresql.org/docs/current/rowtypes.html#id-1.5.7.24.6 }
338
345
*/
339
- function makeRecordToSQLRawValue < TAttributes extends PgCodecAttributes > (
340
- attributes : TAttributes ,
346
+ function makeRecordToSQLRawValue <
347
+ TAttributes extends ReadonlyArray < PgCodecAttribute > ,
348
+ > (
349
+ attributes : PgCodecAttributesRecord < TAttributes > ,
341
350
) : PgEncode < ObjectFromPgCodecAttributes < TAttributes > > {
342
351
const attributeDefs = realAttributeDefs ( attributes ) ;
343
352
return ( value ) => {
@@ -350,19 +359,22 @@ function makeRecordToSQLRawValue<TAttributes extends PgCodecAttributes>(
350
359
} ;
351
360
}
352
361
353
- export type ObjectFromPgCodecAttributes < TAttributes extends PgCodecAttributes > =
354
- {
355
- [ attributeName in keyof TAttributes ] : TAttributes [ attributeName ] extends PgCodecAttribute <
356
- infer UCodec ,
357
- infer UNonNull
358
- >
359
- ? UCodec extends PgCodec < any , any , any , infer UFromJs , any , any , any >
360
- ? UNonNull extends true
361
- ? Exclude < UFromJs , null | undefined >
362
- : UFromJs | null
363
- : never
364
- : never ;
365
- } ;
362
+ export type ObjectFromPgCodecAttributes <
363
+ TAttributes extends
364
+ ReadonlyArray < AnyPgCodecAttribute > = ReadonlyArray < AnyPgCodecAttribute > ,
365
+ > = {
366
+ [ TCodecAttribute in TAttributes [ number ] as PgCodecAttributeName < TCodecAttribute > ] : TCodecAttribute extends PgCodecAttribute <
367
+ any ,
368
+ infer UCodec ,
369
+ infer UNonNull
370
+ >
371
+ ? UCodec extends PgCodec < any , any , any , infer UFromJs , any , any , any >
372
+ ? UNonNull extends true
373
+ ? Exclude < UFromJs , null | undefined >
374
+ : UFromJs | null
375
+ : never
376
+ : never ;
377
+ } ;
366
378
367
379
/**
368
380
* Takes a list of attributes and returns a mapping function that takes a
@@ -371,8 +383,10 @@ export type ObjectFromPgCodecAttributes<TAttributes extends PgCodecAttributes> =
371
383
*
372
384
* @see {@link https://www.postgresql.org/docs/current/rowtypes.html#id-1.5.7.24.6 }
373
385
*/
374
- function makeSQLValueToRecord < TAttributes extends PgCodecAttributes > (
375
- attributes : TAttributes ,
386
+ function makeSQLValueToRecord <
387
+ TAttributes extends ReadonlyArray < PgCodecAttribute > ,
388
+ > (
389
+ attributes : PgCodecAttributesRecord < TAttributes > ,
376
390
) : ( value : string ) => ObjectFromPgCodecAttributes < TAttributes > {
377
391
const attributeDefs = realAttributeDefs ( attributes ) ;
378
392
const attributeCount = attributeDefs . length ;
@@ -390,7 +404,7 @@ function makeSQLValueToRecord<TAttributes extends PgCodecAttributes>(
390
404
391
405
export type PgRecordTypeCodecSpec <
392
406
TName extends string ,
393
- TAttributes extends PgCodecAttributes ,
407
+ TAttributes extends PgCodecAttributesRecord < ReadonlyArray < AnyPgCodecAttribute > > ,
394
408
> = {
395
409
name : TName ;
396
410
executor : PgExecutor ;
@@ -413,18 +427,18 @@ export type PgRecordTypeCodecSpec<
413
427
* 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.
414
428
*/
415
429
export function recordCodec <
416
- const TName extends string ,
417
- const TAttributes extends PgCodecAttributes ,
430
+ TName extends string ,
431
+ TAttributes extends PgCodecAttributesRecord < ReadonlyArray < AnyPgCodecAttribute > > ,
418
432
> (
419
433
config : PgRecordTypeCodecSpec < TName , TAttributes > ,
420
434
) : PgCodec <
421
435
TName ,
422
436
TAttributes ,
423
437
string ,
424
438
ObjectFromPgCodecAttributes < TAttributes > ,
425
- undefined ,
426
- undefined ,
427
- undefined
439
+ never ,
440
+ never ,
441
+ never
428
442
> {
429
443
const {
430
444
name,
@@ -504,14 +518,14 @@ type CodecWithListCodec<
504
518
`${TCodec extends PgCodec < infer UName , any , any , any , any , any , any >
505
519
? UName
506
520
: never } []`,
507
- undefined ,
521
+ never ,
508
522
string ,
509
- TCodec extends PgCodec < any , any , any , infer UFromJs , undefined , any , any >
523
+ TCodec extends PgCodec < any , any , any , infer UFromJs , never , any , any >
510
524
? UFromJs [ ]
511
525
: any [ ] ,
512
526
TCodec ,
513
- undefined ,
514
- undefined
527
+ never ,
528
+ never
515
529
> ;
516
530
} ;
517
531
@@ -528,9 +542,7 @@ type CodecWithListCodec<
528
542
* @param typeDelim - the delimeter used to separate entries in this list when Postgres stringifies it
529
543
* @param identifier - a pg-sql2 fragment that represents the name of this type
530
544
*/
531
- export function listOfCodec <
532
- TInnerCodec extends PgCodec < string , any , any , any , undefined , any , any > ,
533
- > (
545
+ export function listOfCodec < TInnerCodec extends AnyPgCodec = AnyPgCodec > (
534
546
listedCodec : TInnerCodec ,
535
547
config ?: {
536
548
description ?: string ;
@@ -542,14 +554,14 @@ export function listOfCodec<
542
554
`${TInnerCodec extends PgCodec < infer UName , any , any , any , any , any , any >
543
555
? UName
544
556
: never } []`,
545
- undefined , // Array has no attributes
557
+ never , // Array has no attributes
546
558
string ,
547
- TInnerCodec extends PgCodec < any , any , any , infer UFromJs , undefined , any , any >
559
+ TInnerCodec extends PgCodec < any , any , any , infer UFromJs , never , any , any >
548
560
? UFromJs [ ]
549
561
: any [ ] ,
550
562
TInnerCodec ,
551
- undefined ,
552
- undefined
563
+ never ,
564
+ never
553
565
> {
554
566
const innerCodec : CodecWithListCodec < TInnerCodec > = listedCodec ;
555
567
@@ -568,22 +580,14 @@ export function listOfCodec<
568
580
`${TInnerCodec extends PgCodec < infer UName , any , any , any , any , any , any >
569
581
? UName
570
582
: never } []`,
571
- undefined , // Array has no attributes
583
+ never , // Array has no attributes
572
584
string ,
573
- TInnerCodec extends PgCodec <
574
- any ,
575
- any ,
576
- any ,
577
- infer UFromJs ,
578
- undefined ,
579
- any ,
580
- any
581
- >
585
+ TInnerCodec extends PgCodec < any , any , any , infer UFromJs , never , any , any >
582
586
? UFromJs [ ]
583
587
: any [ ] ,
584
588
TInnerCodec ,
585
- undefined ,
586
- undefined
589
+ never ,
590
+ never
587
591
> = {
588
592
name : `${
589
593
innerCodec . name as TInnerCodec extends PgCodec <
@@ -661,7 +665,7 @@ exportAs("@dataplan/pg", listOfCodec, "listOfCodec");
661
665
*/
662
666
export function domainOfCodec <
663
667
TName extends string ,
664
- TInnerCodec extends PgCodec < any , any , any , any , any , any > ,
668
+ TInnerCodec extends AnyPgCodec ,
665
669
> (
666
670
innerCodec : TInnerCodec ,
667
671
name : TName ,
@@ -673,13 +677,15 @@ export function domainOfCodec<
673
677
} = { } ,
674
678
) : PgCodec <
675
679
TName ,
676
- TInnerCodec extends PgCodec < any , infer U , any , any , any , any > ? U : any ,
677
- TInnerCodec extends PgCodec < any , any , infer U , any , any , any > ? U : any ,
678
- undefined ,
679
- TInnerCodec ,
680
- undefined
680
+ PgCodecAttributes < TInnerCodec > ,
681
+ PgCodecFromPostgres < TInnerCodec > ,
682
+ any ,
683
+ AnyPgCodec , // <-- weird types
684
+ TInnerCodec , // <-- weird types
685
+ any
681
686
> {
682
687
const { description, extensions, notNull } = config ;
688
+
683
689
return {
684
690
// Generally same as underlying type:
685
691
...innerCodec ,
@@ -689,7 +695,7 @@ export function domainOfCodec<
689
695
sqlType : identifier ,
690
696
description,
691
697
extensions,
692
- domainOfCodec : innerCodec . arrayOfCodec ? undefined : innerCodec ,
698
+ ... ( innerCodec . arrayOfCodec ? { } : { domainOfCodec : innerCodec } ) ,
693
699
notNull : Boolean ( notNull ) ,
694
700
} ;
695
701
}
@@ -701,7 +707,7 @@ exportAs("@dataplan/pg", domainOfCodec, "domainOfCodec");
701
707
* @internal
702
708
*/
703
709
function escapeRangeValue <
704
- TInnerCodec extends PgCodec < any , undefined , any , any , undefined , any , any > ,
710
+ TInnerCodec extends PgCodec < any , never , any , any , never , any , any > ,
705
711
> ( value : null | any , innerCodec : TInnerCodec ) : string {
706
712
if ( value == null ) {
707
713
return "" ;
@@ -727,15 +733,7 @@ interface PgRange<T> {
727
733
*/
728
734
export function rangeOfCodec <
729
735
TName extends string ,
730
- TInnerCodec extends PgCodec <
731
- any ,
732
- undefined ,
733
- any ,
734
- any ,
735
- undefined ,
736
- any ,
737
- undefined
738
- > ,
736
+ TInnerCodec extends PgCodec < any , never , any , any , never , any , never > ,
739
737
> (
740
738
innerCodec : TInnerCodec ,
741
739
name : TName ,
@@ -744,15 +742,7 @@ export function rangeOfCodec<
744
742
description ?: string ;
745
743
extensions ?: Partial < PgCodecExtensions > ;
746
744
} = { } ,
747
- ) : PgCodec <
748
- TName ,
749
- undefined ,
750
- string ,
751
- PgRange < unknown > ,
752
- undefined ,
753
- undefined ,
754
- TInnerCodec
755
- > {
745
+ ) : PgCodec < TName , never , string , PgRange < unknown > , never , never , TInnerCodec > {
756
746
const { description, extensions } = config ;
757
747
const needsCast = innerCodec . castFromPg ;
758
748
@@ -1169,17 +1159,16 @@ export function getCodecByPgCatalogTypeName(pgCatalogTypeName: string) {
1169
1159
return null ;
1170
1160
}
1171
1161
1172
- export function getInnerCodec <
1173
- TCodec extends PgCodec < any , any , any , any , any , any > ,
1174
- > (
1162
+ export function getInnerCodec < TCodec extends PgCodec > (
1175
1163
codec : TCodec ,
1176
1164
) : TCodec extends PgCodec <
1177
1165
any ,
1178
1166
any ,
1179
1167
any ,
1180
1168
infer UArray ,
1181
1169
infer UDomain ,
1182
- infer URange
1170
+ infer URange ,
1171
+ any
1183
1172
>
1184
1173
? Exclude < UDomain | UArray | URange , undefined >
1185
1174
: TCodec {
0 commit comments