orange-orm 4.7.10-beta.1 → 4.7.10-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/map2.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- //map2.d.ts - Refactored Active Record Methods
1
+ //map2.d.ts - Extended Row Types with Relations Support
2
2
  import type { PGliteOptions } from './pglite.d.ts';
3
3
  import type { ConnectionConfiguration } from 'tedious';
4
4
  import type { D1Database } from '@cloudflare/workers-types';
@@ -90,6 +90,7 @@ export type ColumnFilterType<Val, ColumnType = any> = {
90
90
  ne(value: Val | null | undefined): Filter;
91
91
  lessThan(value: Val | null | undefined): Filter;
92
92
  lt(value: Val | null | undefined): Filter;
93
+ lessThanOrEqual(value: Val | null | undefined): Filter;
93
94
  le(value: Val | null | undefined): Filter;
94
95
  greaterThan(value: Val | null | undefined): Filter;
95
96
  gt(value: Val | null | undefined): Filter;
@@ -207,13 +208,18 @@ type ValidColumnFilterTypes<M extends Record<string, TableDefinition<M>>, K exte
207
208
 
208
209
  // Column selection refs without filter methods - only for getting values/references
209
210
  type ColumnSelectionRefs<M extends Record<string, TableDefinition<M>>, K extends keyof M> = {
210
- // Required columns (notNull = true)
211
- [C in keyof M[K]['columns'] as IsRequired<M[K]['columns'][C]> extends true ? C : never]: ColumnTypeToTS<M[K]['columns'][C]>;
212
- } & {
213
- // Optional columns (nullable)
214
- [C in keyof M[K]['columns'] as IsRequired<M[K]['columns'][C]> extends true ? never : C]?: ColumnTypeToTS<M[K]['columns'][C]> | null | undefined;
211
+ [C in keyof M[K]['columns']]: M[K]['columns'][C];
215
212
  };
216
213
 
214
+
215
+ // type ColumnSelectionRefs<M extends Record<string, TableDefinition<M>>, K extends keyof M> = {
216
+ // // Required columns (notNull = true)
217
+ // [C in keyof M[K]['columns'] as IsRequired<M[K]['columns'][C]> extends true ? C : never]: ColumnTypeToTS<M[K]['columns'][C]>;
218
+ // } & {
219
+ // // Optional columns (nullable)
220
+ // [C in keyof M[K]['columns'] as IsRequired<M[K]['columns'][C]> extends true ? never : C]?: ColumnTypeToTS<M[K]['columns'][C]> | null | undefined;
221
+ // };
222
+
217
223
  // Relation selection refs without filter methods - supports deep nesting
218
224
  // In selectors, all relation types just provide access to the target table structure
219
225
  // But WITHOUT aggregate functions (only available at root level)
@@ -544,6 +550,55 @@ type UpdateChangesRow<M extends Record<string, TableDefinition<M>>, K extends ke
544
550
  : ColumnTypeToTS<M[K]['columns'][C]> | null | undefined;
545
551
  };
546
552
 
553
+ // NEW: Extended row types with relations support
554
+
555
+ // Type for relation rows - only respects notNull, ignores notNullExceptInsert
556
+ type RelationRow<M extends Record<string, TableDefinition<M>>, K extends keyof M> = {
557
+ // Required columns (notNull = true, ignoring notNullExceptInsert)
558
+ [C in keyof M[K]['columns'] as IsRequired<M[K]['columns'][C]> extends true ? C : never]: ColumnTypeToTS<M[K]['columns'][C]>;
559
+ } & {
560
+ // Optional columns (all others)
561
+ [C in keyof M[K]['columns'] as IsRequired<M[K]['columns'][C]> extends true ? never : C]?: ColumnTypeToTS<M[K]['columns'][C]> | null | undefined;
562
+ };
563
+
564
+ // Helper type to create relation data for insert/update operations
565
+ type RelationData<M extends Record<string, TableDefinition<M>>, K extends keyof M> =
566
+ M[K] extends { relations: infer R }
567
+ ? {
568
+ [RName in keyof R]?: R[RName] extends RelationDefinition<M>
569
+ ? R[RName]['type'] extends 'hasMany'
570
+ ? R[RName]['target'] extends keyof M
571
+ ? Array<RelationRowWithRelations<M, R[RName]['target']>>
572
+ : never
573
+ : R[RName]['type'] extends 'hasOne' | 'references'
574
+ ? R[RName]['target'] extends keyof M
575
+ ? RelationRowWithRelations<M, R[RName]['target']> | null
576
+ : never
577
+ : never
578
+ : never;
579
+ }
580
+ : {};
581
+
582
+ // Relation row type with nested relations support
583
+ type RelationRowWithRelations<M extends Record<string, TableDefinition<M>>, K extends keyof M> =
584
+ RelationRow<M, K> & RelationData<M, K>;
585
+
586
+ // Extended insert row type with optional relations
587
+ type InsertRowWithRelations<M extends Record<string, TableDefinition<M>>, K extends keyof M> =
588
+ InsertRow<M, K> & RelationData<M, K>;
589
+
590
+ // Extended update/replace row type with optional relations
591
+ type UpdateChangesRowWithRelations<M extends Record<string, TableDefinition<M>>, K extends keyof M> =
592
+ UpdateChangesRow<M, K> & RelationData<M, K>;
593
+
594
+ // Extended type for bulk update operations with optional relations
595
+ type UpdateRowWithRelations<M extends Record<string, TableDefinition<M>>, K extends keyof M> =
596
+ Partial<{
597
+ [C in keyof M[K]['columns']]: IsRequired<M[K]['columns'][C]> extends true
598
+ ? ColumnTypeToTS<M[K]['columns'][C]>
599
+ : ColumnTypeToTS<M[K]['columns'][C]> | null | undefined;
600
+ }> & RelationData<M, K>;
601
+
547
602
  // REFACTORED: Separate Active Record Methods for Individual Rows vs Arrays
548
603
 
549
604
  // Active record methods for individual rows
@@ -578,6 +633,45 @@ type WithActiveRecord<T, M extends Record<string, TableDefinition<M>>, K extends
578
633
  type WithArrayActiveRecord<T extends Array<any>, M extends Record<string, TableDefinition<M>>, K extends keyof M> =
579
634
  T & ArrayActiveRecordMethods<M, K>;
580
635
 
636
+ // Separate CustomSelectorProperties for aggregate (allows column name overlap)
637
+ type AggregateCustomSelectorProperties<M extends Record<string, TableDefinition<M>>, K extends keyof M, FS extends Record<string, any>> = {
638
+ // Required properties (count, sum, avg) - no question mark, no null/undefined
639
+ [P in keyof FS as
640
+ P extends ReservedFetchStrategyProps ? never :
641
+ P extends (M[K] extends { relations: infer R } ? keyof R : never) ? never :
642
+ FS[P] extends (row: any) => any ?
643
+ (FS[P] extends (row: RootSelectionRefs<M, K>) => infer ReturnType
644
+ ? IsRequiredAggregate<ReturnType> extends true
645
+ ? P // Required aggregates
646
+ : never
647
+ : never)
648
+ : never
649
+ ]: FS[P] extends (row: RootSelectionRefs<M, K>) => infer ReturnType
650
+ ? number // Required aggregate functions (count, sum, avg) return required number
651
+ : never;
652
+ } & {
653
+ // Optional properties (max, min, plain columns) - with question mark AND null/undefined
654
+ [P in keyof FS as
655
+ P extends ReservedFetchStrategyProps ? never :
656
+ P extends (M[K] extends { relations: infer R } ? keyof R : never) ? never :
657
+ FS[P] extends (row: any) => any ?
658
+ (FS[P] extends (row: RootSelectionRefs<M, K>) => infer ReturnType
659
+ ? IsRequiredAggregate<ReturnType> extends true
660
+ ? never // Required aggregates are not optional
661
+ : P // Everything else is optional
662
+ : never)
663
+ : never
664
+ ]?: FS[P] extends (row: RootSelectionRefs<M, K>) => infer ReturnType
665
+ ? IsMinMaxAggregate<ReturnType> extends true
666
+ ? ExtractMinMaxColumnType<ReturnType> | null | undefined
667
+ : IsAggregateFunction<ReturnType> extends true
668
+ ? number | null | undefined
669
+ : ReturnType extends ORMColumnDefinition | ORMJsonColumnDefinition
670
+ ? ColumnDefinitionToTS<ReturnType> | null | undefined
671
+ : never
672
+ : never;
673
+ };
674
+
581
675
  export type TableClient<M extends Record<string, TableDefinition<M>>, K extends keyof M> = {
582
676
  // Array methods - return arrays with array-level active record methods, but individual items are plain
583
677
  getAll(): Promise<WithArrayActiveRecord<Array<DeepExpand<Selection<M, K, {}>>>, M, K>>;
@@ -586,7 +680,7 @@ export type TableClient<M extends Record<string, TableDefinition<M>>, K extends
586
680
  getMany<strategy extends FetchStrategy<M, K>>(filter: RawFilter | Array<PrimaryKeyObject<M, K>>, strategy: strategy): Promise<WithArrayActiveRecord<Array<DeepExpand<Selection<M, K, strategy>>>, M, K>>;
587
681
 
588
682
  // Aggregate methods - return plain objects (no active record methods)
589
- aggregate<strategy extends AggregateStrategy<M, K>>(strategy: strategy): Promise<Array<DeepExpand<CustomSelectorProperties<M, K, strategy>>>>;
683
+ aggregate<strategy extends AggregateStrategy<M, K>>(strategy: strategy): Promise<Array<DeepExpand<AggregateCustomSelectorProperties<M, K, strategy>>>>;
590
684
 
591
685
  // Single item methods - return individual objects with individual active record methods
592
686
  getOne<strategy extends FetchStrategy<M, K>>(
@@ -606,22 +700,14 @@ export type TableClient<M extends Record<string, TableDefinition<M>>, K extends
606
700
  ...args: [...PrimaryKeyArgs<M, K>]
607
701
  ): Promise<WithActiveRecord<DeepExpand<Selection<M, K, {}>>, M, K>>;
608
702
 
609
- // Bulk update methods
703
+ // UPDATED: Bulk update methods with relations support
610
704
  update(
611
- row: Partial<{
612
- [C in keyof M[K]['columns']]: IsRequired<M[K]['columns'][C]> extends true
613
- ? ColumnTypeToTS<M[K]['columns'][C]>
614
- : ColumnTypeToTS<M[K]['columns'][C]> | null | undefined;
615
- }>,
705
+ row: UpdateRowWithRelations<M, K>,
616
706
  opts: { where: (row: RootTableRefs<M, K>) => RawFilter }
617
707
  ): Promise<void>;
618
708
 
619
709
  update<strategy extends FetchStrategy<M, K>>(
620
- row: Partial<{
621
- [C in keyof M[K]['columns']]: IsRequired<M[K]['columns'][C]> extends true
622
- ? ColumnTypeToTS<M[K]['columns'][C]>
623
- : ColumnTypeToTS<M[K]['columns'][C]> | null | undefined;
624
- }>,
710
+ row: UpdateRowWithRelations<M, K>,
625
711
  opts: { where: (row: RootTableRefs<M, K>) => RawFilter },
626
712
  strategy: strategy
627
713
  ): Promise<WithArrayActiveRecord<Array<DeepExpand<Selection<M, K, strategy>>>, M, K>>;
@@ -631,87 +717,87 @@ export type TableClient<M extends Record<string, TableDefinition<M>>, K extends
631
717
  delete(filter: RawFilter | Array<PrimaryKeyObject<M, K>>,): Promise<void>;
632
718
  deleteCascade(filter: RawFilter | Array<PrimaryKeyObject<M, K>>,): Promise<void>;
633
719
 
634
- // Replace methods - can return single or array with appropriate active record methods
720
+ // UPDATED: Replace methods with relations support
635
721
  replace(
636
- row: Array<UpdateChangesRow<M, K>>
722
+ row: Array<UpdateChangesRowWithRelations<M, K>>
637
723
  ): Promise<void>;
638
724
 
639
725
  replace<strategy extends FetchStrategy<M, K>>(
640
- row: UpdateChangesRow<M, K>,
726
+ row: UpdateChangesRowWithRelations<M, K>,
641
727
  strategy: strategy
642
728
  ): Promise<WithActiveRecord<DeepExpand<Selection<M, K, strategy>>, M, K>>;
643
729
 
644
730
  replace<strategy extends FetchStrategy<M, K>>(
645
- rows: Array<UpdateChangesRow<M, K>>,
731
+ rows: Array<UpdateChangesRowWithRelations<M, K>>,
646
732
  strategy: strategy
647
733
  ): Promise<WithArrayActiveRecord<Array<DeepExpand<Selection<M, K, strategy>>>, M, K>>;
648
734
 
649
- // UpdateChanges methods - can return single or array with appropriate active record methods
735
+ // UPDATED: UpdateChanges methods with relations support
650
736
  updateChanges(
651
- row: UpdateChangesRow<M, K>,
652
- originalRow: UpdateChangesRow<M, K>
737
+ row: UpdateChangesRowWithRelations<M, K>,
738
+ originalRow: UpdateChangesRowWithRelations<M, K>
653
739
  ): Promise<WithActiveRecord<DeepExpand<Selection<M, K, {}>>, M, K>>;
654
740
 
655
741
  updateChanges(
656
- rows: Array<UpdateChangesRow<M, K>>,
657
- originalRows: Array<UpdateChangesRow<M, K>>
742
+ rows: Array<UpdateChangesRowWithRelations<M, K>>,
743
+ originalRows: Array<UpdateChangesRowWithRelations<M, K>>
658
744
  ): Promise<WithArrayActiveRecord<Array<DeepExpand<Selection<M, K, {}>>>, M, K>>;
659
745
 
660
746
  updateChanges<strategy extends FetchStrategy<M, K>>(
661
- row: UpdateChangesRow<M, K>,
662
- originalRow: UpdateChangesRow<M, K>,
747
+ row: UpdateChangesRowWithRelations<M, K>,
748
+ originalRow: UpdateChangesRowWithRelations<M, K>,
663
749
  strategy: strategy
664
750
  ): Promise<WithActiveRecord<DeepExpand<Selection<M, K, strategy>>, M, K>>;
665
751
 
666
752
  updateChanges<strategy extends FetchStrategy<M, K>>(
667
- rows: Array<UpdateChangesRow<M, K>>,
668
- originalRows: Array<UpdateChangesRow<M, K>>,
753
+ rows: Array<UpdateChangesRowWithRelations<M, K>>,
754
+ originalRows: Array<UpdateChangesRowWithRelations<M, K>>,
669
755
  strategy: strategy
670
756
  ): Promise<WithArrayActiveRecord<Array<DeepExpand<Selection<M, K, strategy>>>, M, K>>;
671
757
 
672
- // Insert methods - no active record methods for insertAndForget, appropriate methods for others
758
+ // UPDATED: Insert methods with relations support - no active record methods for insertAndForget, appropriate methods for others
673
759
  insertAndForget(
674
- row: InsertRow<M, K>
760
+ row: InsertRowWithRelations<M, K>
675
761
  ): Promise<void>;
676
762
 
677
763
  insertAndForget(
678
- rows: Array<InsertRow<M, K>>
764
+ rows: Array<InsertRowWithRelations<M, K>>
679
765
  ): Promise<void>;
680
766
 
681
767
  insert(
682
- row: InsertRow<M, K>
768
+ row: InsertRowWithRelations<M, K>
683
769
  ): Promise<WithActiveRecord<DeepExpand<Selection<M, K, {}>>, M, K>>;
684
770
 
685
771
  insert(
686
- rows: Array<InsertRow<M, K>>
772
+ rows: Array<InsertRowWithRelations<M, K>>
687
773
  ): Promise<WithArrayActiveRecord<Array<DeepExpand<Selection<M, K, {}>>>, M, K>>;
688
774
 
689
775
  insert<strategy extends FetchStrategy<M, K>>(
690
- row: InsertRow<M, K>,
776
+ row: InsertRowWithRelations<M, K>,
691
777
  strategy: strategy
692
778
  ): Promise<WithActiveRecord<DeepExpand<Selection<M, K, strategy>>, M, K>>;
693
779
 
694
780
  insert<strategy extends FetchStrategy<M, K>>(
695
- rows: Array<InsertRow<M, K>>,
781
+ rows: Array<InsertRowWithRelations<M, K>>,
696
782
  strategy: strategy
697
783
  ): Promise<WithArrayActiveRecord<Array<DeepExpand<Selection<M, K, strategy>>>, M, K>>;
698
784
 
699
- // Proxify methods - can return single or array with appropriate active record methods
785
+ // UPDATED: Proxify methods with relations support
700
786
  proxify(
701
- row: UpdateChangesRow<M, K>
787
+ row: UpdateChangesRowWithRelations<M, K>
702
788
  ): Promise<WithActiveRecord<DeepExpand<Selection<M, K, {}>>, M, K>>;
703
789
 
704
790
  proxify(
705
- rows: Array<UpdateChangesRow<M, K>>
791
+ rows: Array<UpdateChangesRowWithRelations<M, K>>
706
792
  ): Promise<WithArrayActiveRecord<Array<DeepExpand<Selection<M, K, {}>>>, M, K>>;
707
793
 
708
794
  proxify<strategy extends FetchStrategy<M, K>>(
709
- row: UpdateChangesRow<M, K>,
795
+ row: UpdateChangesRowWithRelations<M, K>,
710
796
  strategy: strategy
711
797
  ): Promise<WithActiveRecord<DeepExpand<Selection<M, K, strategy>>, M, K>>;
712
798
 
713
799
  proxify<strategy extends FetchStrategy<M, K>>(
714
- rows: Array<UpdateChangesRow<M, K>>,
800
+ rows: Array<UpdateChangesRowWithRelations<M, K>>,
715
801
  strategy: strategy
716
802
  ): Promise<WithArrayActiveRecord<Array<DeepExpand<Selection<M, K, strategy>>>, M, K>>;
717
803
 
package/src/cyclic.d.ts DELETED
@@ -1,132 +0,0 @@
1
- // Type definitions using a type mapping approach
2
- type EntityMap = {
3
- Order: {
4
- id: string;
5
- date: Date;
6
- total: number;
7
- };
8
- Line: {
9
- id: string;
10
- quantity: number;
11
- price: number;
12
- };
13
- Package: {
14
- id: string;
15
- trackingNumber: string;
16
- weight: number;
17
- };
18
- Customer: {
19
- id: string;
20
- name: string;
21
- email: string;
22
- };
23
- };
24
-
25
- // Enhanced relation mapping that includes cardinality information
26
- type RelationMap = {
27
- Order: {
28
- lines: { type: 'Line'; isArray: true };
29
- customer: { type: 'Customer'; isArray: false };
30
- };
31
- Line: {
32
- packages: { type: 'Package'; isArray: true };
33
- order: { type: 'Order'; isArray: false };
34
- };
35
- Customer: {
36
- orders: { type: 'Order'; isArray: true };
37
- };
38
- Package: {};
39
- };
40
-
41
- // Helper type to extract the entity type from a relation
42
- type RelationEntityType<R> = R extends { type: infer T extends keyof EntityMap } ? T : never;
43
-
44
- // Helper type to determine if a relation is an array
45
- type IsArray<R> = R extends { isArray: infer A extends boolean } ? A : false;
46
-
47
- // Generic deep type resolver
48
- type Depth = [never, 0, 1, 2, 3, 4, 5];
49
-
50
- // RecursiveType generic with improved depth handling
51
- type RecursiveType<
52
- T extends keyof EntityMap,
53
- D extends number = 5
54
- > = {
55
- // Always include base properties
56
- [K in keyof EntityMap[T]]: EntityMap[T][K];
57
- } & (D extends 0
58
- ? // At max depth, only include base properties (which are already included above)
59
- {}
60
- : // Otherwise include relations with appropriate depth reduction
61
- {
62
- [K in keyof RelationMap[T]]: IsArray<RelationMap[T][K]> extends true
63
- ? RecursiveType<
64
- RelationEntityType<RelationMap[T][K]>,
65
- Depth[D]
66
- >[]
67
- : RecursiveType<
68
- RelationEntityType<RelationMap[T][K]>,
69
- Depth[D]
70
- >;
71
- });
72
-
73
- // Generate the entity types with relationships resolved to the specified depth
74
- type Order = RecursiveType<'Order', 6>;
75
-
76
- type Line = RecursiveType<'Line'>;
77
- type Package = RecursiveType<'Package'>;
78
- type Customer = RecursiveType<'Customer'>;
79
-
80
- // Example usage
81
- function processOrder(order: Order, line: Line) {
82
- // Can access nested properties up to depth 5
83
- // const trackingNumber = order.customer.orders[0].lines[0].order.lines[0].packages[0].
84
- // const orderId = order.customer.orders[0].customer.orders[0].lines[0].
85
- line.order.customer.orders[0].lines[0].packages[0].
86
-
87
- console.log(trackingNumber, orderId);
88
- }
89
-
90
- // Allow custom depth control per entity type
91
- type OrderWithDepth1 = RecursiveType<'Order', 4>;
92
-
93
- // Example with custom depth
94
- function processOrderWithLimitedDepth(order: OrderWithDepth1) {
95
- // This would work (depth 1)
96
-
97
- console.log(order.customer.id); // Can access base properties at max depth
98
- console.log(order.customer.name); // Can access base properties at max depth
99
-
100
- // This would error - beyond depth 1
101
- // console.log(order.customer.orders[0].id);
102
- }
103
-
104
- // Example of creating an instance with this type system
105
- const sampleOrder: Order = {
106
- id: "ORD-123",
107
- date: new Date(),
108
- total: 299.99,
109
- customer: {
110
- id: "CUST-456",
111
- name: "John Doe",
112
- email: "john@example.com",
113
- orders: [
114
- /* would be recursive orders */
115
- ]
116
- },
117
- lines: [
118
- {
119
- id: "LINE-789",
120
- quantity: 2,
121
- price: 149.99,
122
- order: /* circular reference */,
123
- packages: [
124
- {
125
- id: "PKG-101",
126
- trackingNumber: "TRK123456789",
127
- weight: 1.5
128
- }
129
- ]
130
- }
131
- ]
132
- };