cogsbox-shape 0.5.58 → 0.5.60

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.
@@ -398,15 +398,111 @@ declare const schemas: {
398
398
  } & {
399
399
  type: "manyToMany";
400
400
  });
401
- zodSqlSchema: import("zod").ZodArray<import("zod").ZodAny, "many">;
402
- zodNewSchema: import("zod").ZodArray<import("zod").ZodAny, "many">;
401
+ zodSqlSchema: import("zod").ZodArray<import("zod").ZodObject<{
402
+ id: import("zod").ZodNumber;
403
+ name: import("zod").ZodString;
404
+ userId: import("zod").ZodAny;
405
+ fluffynessScale: import("zod").ZodString;
406
+ favourite: import("zod").ZodNumber;
407
+ }, import("zod").UnknownKeysParam, import("zod").ZodTypeAny, {
408
+ name: string;
409
+ id: number;
410
+ fluffynessScale: string;
411
+ favourite: number;
412
+ userId?: any;
413
+ }, {
414
+ name: string;
415
+ id: number;
416
+ fluffynessScale: string;
417
+ favourite: number;
418
+ userId?: any;
419
+ }>, "many">;
420
+ zodNewSchema: import("zod").ZodArray<import("zod").ZodObject<{
421
+ id: import("zod").ZodUnion<[import("zod").ZodNumber, import("zod").ZodNumber]>;
422
+ name: import("zod").ZodString;
423
+ userId: import("zod").ZodAny;
424
+ fluffynessScale: import("zod").ZodArray<import("zod").ZodEnum<["bald", "fuzzy", "fluffy", "poof"]>, "many">;
425
+ favourite: import("zod").ZodBoolean;
426
+ }, import("zod").UnknownKeysParam, import("zod").ZodTypeAny, {
427
+ name: string;
428
+ id: number;
429
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
430
+ favourite: boolean;
431
+ userId?: any;
432
+ }, {
433
+ name: string;
434
+ id: number;
435
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
436
+ favourite: boolean;
437
+ userId?: any;
438
+ }>, "many">;
403
439
  initialValue: any[];
404
- zodClientSchema: import("zod").ZodArray<import("zod").ZodAny, "many">;
405
- zodValidationSchema: import("zod").ZodArray<import("zod").ZodAny, "many">;
440
+ zodClientSchema: import("zod").ZodArray<import("zod").ZodObject<{
441
+ id: import("zod").ZodUnion<[import("zod").ZodNumber, import("zod").ZodNumber]>;
442
+ name: import("zod").ZodString;
443
+ userId: import("zod").ZodAny;
444
+ fluffynessScale: import("zod").ZodArray<import("zod").ZodEnum<["bald", "fuzzy", "fluffy", "poof"]>, "many">;
445
+ favourite: import("zod").ZodBoolean;
446
+ }, import("zod").UnknownKeysParam, import("zod").ZodTypeAny, {
447
+ name: string;
448
+ id: number;
449
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
450
+ favourite: boolean;
451
+ userId?: any;
452
+ }, {
453
+ name: string;
454
+ id: number;
455
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
456
+ favourite: boolean;
457
+ userId?: any;
458
+ }>, "many">;
459
+ zodValidationSchema: import("zod").ZodArray<import("zod").ZodObject<{
460
+ id: import("zod").ZodNumber;
461
+ name: import("zod").ZodString;
462
+ userId: import("zod").ZodAny;
463
+ fluffynessScale: import("zod").ZodString;
464
+ favourite: import("zod").ZodNumber;
465
+ }, import("zod").UnknownKeysParam, import("zod").ZodTypeAny, {
466
+ name: string;
467
+ id: number;
468
+ fluffynessScale: string;
469
+ favourite: number;
470
+ userId?: any;
471
+ }, {
472
+ name: string;
473
+ id: number;
474
+ fluffynessScale: string;
475
+ favourite: number;
476
+ userId?: any;
477
+ }>, "many">;
406
478
  };
407
479
  transform: (transforms: {
408
- toClient: (dbValue: any[]) => any[];
409
- toDb: (clientValue: any[]) => any[];
480
+ toClient: (dbValue: {
481
+ name: string;
482
+ id: number;
483
+ fluffynessScale: string;
484
+ favourite: number;
485
+ userId?: any;
486
+ }[]) => {
487
+ name: string;
488
+ id: number;
489
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
490
+ favourite: boolean;
491
+ userId?: any;
492
+ }[];
493
+ toDb: (clientValue: {
494
+ name: string;
495
+ id: number;
496
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
497
+ favourite: boolean;
498
+ userId?: any;
499
+ }[]) => {
500
+ name: string;
501
+ id: number;
502
+ fluffynessScale: string;
503
+ favourite: number;
504
+ userId?: any;
505
+ }[];
410
506
  }) => {
411
507
  config: {
412
508
  sql: ({
@@ -698,15 +794,111 @@ declare const schemas: {
698
794
  } & {
699
795
  type: "manyToMany";
700
796
  });
701
- zodSqlSchema: import("zod").ZodArray<import("zod").ZodAny, "many">;
702
- zodNewSchema: import("zod").ZodArray<import("zod").ZodAny, "many">;
797
+ zodSqlSchema: import("zod").ZodArray<import("zod").ZodObject<{
798
+ id: import("zod").ZodNumber;
799
+ name: import("zod").ZodString;
800
+ userId: import("zod").ZodAny;
801
+ fluffynessScale: import("zod").ZodString;
802
+ favourite: import("zod").ZodNumber;
803
+ }, import("zod").UnknownKeysParam, import("zod").ZodTypeAny, {
804
+ name: string;
805
+ id: number;
806
+ fluffynessScale: string;
807
+ favourite: number;
808
+ userId?: any;
809
+ }, {
810
+ name: string;
811
+ id: number;
812
+ fluffynessScale: string;
813
+ favourite: number;
814
+ userId?: any;
815
+ }>, "many">;
816
+ zodNewSchema: import("zod").ZodArray<import("zod").ZodObject<{
817
+ id: import("zod").ZodUnion<[import("zod").ZodNumber, import("zod").ZodNumber]>;
818
+ name: import("zod").ZodString;
819
+ userId: import("zod").ZodAny;
820
+ fluffynessScale: import("zod").ZodArray<import("zod").ZodEnum<["bald", "fuzzy", "fluffy", "poof"]>, "many">;
821
+ favourite: import("zod").ZodBoolean;
822
+ }, import("zod").UnknownKeysParam, import("zod").ZodTypeAny, {
823
+ name: string;
824
+ id: number;
825
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
826
+ favourite: boolean;
827
+ userId?: any;
828
+ }, {
829
+ name: string;
830
+ id: number;
831
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
832
+ favourite: boolean;
833
+ userId?: any;
834
+ }>, "many">;
703
835
  initialValue: any[];
704
- zodClientSchema: import("zod").ZodArray<import("zod").ZodAny, "many">;
705
- zodValidationSchema: import("zod").ZodArray<import("zod").ZodAny, "many">;
836
+ zodClientSchema: import("zod").ZodArray<import("zod").ZodObject<{
837
+ id: import("zod").ZodUnion<[import("zod").ZodNumber, import("zod").ZodNumber]>;
838
+ name: import("zod").ZodString;
839
+ userId: import("zod").ZodAny;
840
+ fluffynessScale: import("zod").ZodArray<import("zod").ZodEnum<["bald", "fuzzy", "fluffy", "poof"]>, "many">;
841
+ favourite: import("zod").ZodBoolean;
842
+ }, import("zod").UnknownKeysParam, import("zod").ZodTypeAny, {
843
+ name: string;
844
+ id: number;
845
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
846
+ favourite: boolean;
847
+ userId?: any;
848
+ }, {
849
+ name: string;
850
+ id: number;
851
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
852
+ favourite: boolean;
853
+ userId?: any;
854
+ }>, "many">;
855
+ zodValidationSchema: import("zod").ZodArray<import("zod").ZodObject<{
856
+ id: import("zod").ZodNumber;
857
+ name: import("zod").ZodString;
858
+ userId: import("zod").ZodAny;
859
+ fluffynessScale: import("zod").ZodString;
860
+ favourite: import("zod").ZodNumber;
861
+ }, import("zod").UnknownKeysParam, import("zod").ZodTypeAny, {
862
+ name: string;
863
+ id: number;
864
+ fluffynessScale: string;
865
+ favourite: number;
866
+ userId?: any;
867
+ }, {
868
+ name: string;
869
+ id: number;
870
+ fluffynessScale: string;
871
+ favourite: number;
872
+ userId?: any;
873
+ }>, "many">;
706
874
  } & {
707
875
  transforms: {
708
- toClient: (dbValue: any[]) => any[];
709
- toDb: (clientValue: any[]) => any[];
876
+ toClient: (dbValue: {
877
+ name: string;
878
+ id: number;
879
+ fluffynessScale: string;
880
+ favourite: number;
881
+ userId?: any;
882
+ }[]) => {
883
+ name: string;
884
+ id: number;
885
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
886
+ favourite: boolean;
887
+ userId?: any;
888
+ }[];
889
+ toDb: (clientValue: {
890
+ name: string;
891
+ id: number;
892
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
893
+ favourite: boolean;
894
+ userId?: any;
895
+ }[]) => {
896
+ name: string;
897
+ id: number;
898
+ fluffynessScale: string;
899
+ favourite: number;
900
+ userId?: any;
901
+ }[];
710
902
  };
711
903
  };
712
904
  };
@@ -464,15 +464,111 @@ export declare const userSchema: {
464
464
  } & {
465
465
  type: "manyToMany";
466
466
  });
467
- zodSqlSchema: z.ZodArray<z.ZodAny, "many">;
468
- zodNewSchema: z.ZodArray<z.ZodAny, "many">;
467
+ zodSqlSchema: z.ZodArray<z.ZodObject<{
468
+ id: z.ZodNumber;
469
+ name: z.ZodString;
470
+ userId: z.ZodAny;
471
+ fluffynessScale: z.ZodString;
472
+ favourite: z.ZodNumber;
473
+ }, z.UnknownKeysParam, z.ZodTypeAny, {
474
+ name: string;
475
+ id: number;
476
+ fluffynessScale: string;
477
+ favourite: number;
478
+ userId?: any;
479
+ }, {
480
+ name: string;
481
+ id: number;
482
+ fluffynessScale: string;
483
+ favourite: number;
484
+ userId?: any;
485
+ }>, "many">;
486
+ zodNewSchema: z.ZodArray<z.ZodObject<{
487
+ id: z.ZodUnion<[z.ZodNumber, z.ZodNumber]>;
488
+ name: z.ZodString;
489
+ userId: z.ZodAny;
490
+ fluffynessScale: z.ZodArray<z.ZodEnum<["bald", "fuzzy", "fluffy", "poof"]>, "many">;
491
+ favourite: z.ZodBoolean;
492
+ }, z.UnknownKeysParam, z.ZodTypeAny, {
493
+ name: string;
494
+ id: number;
495
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
496
+ favourite: boolean;
497
+ userId?: any;
498
+ }, {
499
+ name: string;
500
+ id: number;
501
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
502
+ favourite: boolean;
503
+ userId?: any;
504
+ }>, "many">;
469
505
  initialValue: any[];
470
- zodClientSchema: z.ZodArray<z.ZodAny, "many">;
471
- zodValidationSchema: z.ZodArray<z.ZodAny, "many">;
506
+ zodClientSchema: z.ZodArray<z.ZodObject<{
507
+ id: z.ZodUnion<[z.ZodNumber, z.ZodNumber]>;
508
+ name: z.ZodString;
509
+ userId: z.ZodAny;
510
+ fluffynessScale: z.ZodArray<z.ZodEnum<["bald", "fuzzy", "fluffy", "poof"]>, "many">;
511
+ favourite: z.ZodBoolean;
512
+ }, z.UnknownKeysParam, z.ZodTypeAny, {
513
+ name: string;
514
+ id: number;
515
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
516
+ favourite: boolean;
517
+ userId?: any;
518
+ }, {
519
+ name: string;
520
+ id: number;
521
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
522
+ favourite: boolean;
523
+ userId?: any;
524
+ }>, "many">;
525
+ zodValidationSchema: z.ZodArray<z.ZodObject<{
526
+ id: z.ZodNumber;
527
+ name: z.ZodString;
528
+ userId: z.ZodAny;
529
+ fluffynessScale: z.ZodString;
530
+ favourite: z.ZodNumber;
531
+ }, z.UnknownKeysParam, z.ZodTypeAny, {
532
+ name: string;
533
+ id: number;
534
+ fluffynessScale: string;
535
+ favourite: number;
536
+ userId?: any;
537
+ }, {
538
+ name: string;
539
+ id: number;
540
+ fluffynessScale: string;
541
+ favourite: number;
542
+ userId?: any;
543
+ }>, "many">;
472
544
  };
473
545
  transform: (transforms: {
474
- toClient: (dbValue: any[]) => any[];
475
- toDb: (clientValue: any[]) => any[];
546
+ toClient: (dbValue: {
547
+ name: string;
548
+ id: number;
549
+ fluffynessScale: string;
550
+ favourite: number;
551
+ userId?: any;
552
+ }[]) => {
553
+ name: string;
554
+ id: number;
555
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
556
+ favourite: boolean;
557
+ userId?: any;
558
+ }[];
559
+ toDb: (clientValue: {
560
+ name: string;
561
+ id: number;
562
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
563
+ favourite: boolean;
564
+ userId?: any;
565
+ }[]) => {
566
+ name: string;
567
+ id: number;
568
+ fluffynessScale: string;
569
+ favourite: number;
570
+ userId?: any;
571
+ }[];
476
572
  }) => {
477
573
  config: {
478
574
  sql: ({
@@ -764,15 +860,111 @@ export declare const userSchema: {
764
860
  } & {
765
861
  type: "manyToMany";
766
862
  });
767
- zodSqlSchema: z.ZodArray<z.ZodAny, "many">;
768
- zodNewSchema: z.ZodArray<z.ZodAny, "many">;
863
+ zodSqlSchema: z.ZodArray<z.ZodObject<{
864
+ id: z.ZodNumber;
865
+ name: z.ZodString;
866
+ userId: z.ZodAny;
867
+ fluffynessScale: z.ZodString;
868
+ favourite: z.ZodNumber;
869
+ }, z.UnknownKeysParam, z.ZodTypeAny, {
870
+ name: string;
871
+ id: number;
872
+ fluffynessScale: string;
873
+ favourite: number;
874
+ userId?: any;
875
+ }, {
876
+ name: string;
877
+ id: number;
878
+ fluffynessScale: string;
879
+ favourite: number;
880
+ userId?: any;
881
+ }>, "many">;
882
+ zodNewSchema: z.ZodArray<z.ZodObject<{
883
+ id: z.ZodUnion<[z.ZodNumber, z.ZodNumber]>;
884
+ name: z.ZodString;
885
+ userId: z.ZodAny;
886
+ fluffynessScale: z.ZodArray<z.ZodEnum<["bald", "fuzzy", "fluffy", "poof"]>, "many">;
887
+ favourite: z.ZodBoolean;
888
+ }, z.UnknownKeysParam, z.ZodTypeAny, {
889
+ name: string;
890
+ id: number;
891
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
892
+ favourite: boolean;
893
+ userId?: any;
894
+ }, {
895
+ name: string;
896
+ id: number;
897
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
898
+ favourite: boolean;
899
+ userId?: any;
900
+ }>, "many">;
769
901
  initialValue: any[];
770
- zodClientSchema: z.ZodArray<z.ZodAny, "many">;
771
- zodValidationSchema: z.ZodArray<z.ZodAny, "many">;
902
+ zodClientSchema: z.ZodArray<z.ZodObject<{
903
+ id: z.ZodUnion<[z.ZodNumber, z.ZodNumber]>;
904
+ name: z.ZodString;
905
+ userId: z.ZodAny;
906
+ fluffynessScale: z.ZodArray<z.ZodEnum<["bald", "fuzzy", "fluffy", "poof"]>, "many">;
907
+ favourite: z.ZodBoolean;
908
+ }, z.UnknownKeysParam, z.ZodTypeAny, {
909
+ name: string;
910
+ id: number;
911
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
912
+ favourite: boolean;
913
+ userId?: any;
914
+ }, {
915
+ name: string;
916
+ id: number;
917
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
918
+ favourite: boolean;
919
+ userId?: any;
920
+ }>, "many">;
921
+ zodValidationSchema: z.ZodArray<z.ZodObject<{
922
+ id: z.ZodNumber;
923
+ name: z.ZodString;
924
+ userId: z.ZodAny;
925
+ fluffynessScale: z.ZodString;
926
+ favourite: z.ZodNumber;
927
+ }, z.UnknownKeysParam, z.ZodTypeAny, {
928
+ name: string;
929
+ id: number;
930
+ fluffynessScale: string;
931
+ favourite: number;
932
+ userId?: any;
933
+ }, {
934
+ name: string;
935
+ id: number;
936
+ fluffynessScale: string;
937
+ favourite: number;
938
+ userId?: any;
939
+ }>, "many">;
772
940
  } & {
773
941
  transforms: {
774
- toClient: (dbValue: any[]) => any[];
775
- toDb: (clientValue: any[]) => any[];
942
+ toClient: (dbValue: {
943
+ name: string;
944
+ id: number;
945
+ fluffynessScale: string;
946
+ favourite: number;
947
+ userId?: any;
948
+ }[]) => {
949
+ name: string;
950
+ id: number;
951
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
952
+ favourite: boolean;
953
+ userId?: any;
954
+ }[];
955
+ toDb: (clientValue: {
956
+ name: string;
957
+ id: number;
958
+ fluffynessScale: ("bald" | "fuzzy" | "fluffy" | "poof")[];
959
+ favourite: boolean;
960
+ userId?: any;
961
+ }[]) => {
962
+ name: string;
963
+ id: number;
964
+ fluffynessScale: string;
965
+ favourite: number;
966
+ userId?: any;
967
+ }[];
776
968
  };
777
969
  };
778
970
  };
@@ -74,4 +74,4 @@ const testPets = {
74
74
  toDb: (str) => new Date(str),
75
75
  }),
76
76
  };
77
- const { sqlSchema, clientSchema: clSchema, defaultValues, validationSchema, } = createSchema(testPets);
77
+ const { sqlSchema, clientSchema: clSchema, defaultValues, validationSchema, } = createSchema(userSchema);
package/dist/schema.d.ts CHANGED
@@ -5,7 +5,7 @@ type CurrentTimestampConfig = {
5
5
  };
6
6
  export declare const isFunction: (fn: unknown) => fn is Function;
7
7
  export declare function currentTimeStamp(): CurrentTimestampConfig;
8
- type SQLType = ({
8
+ export type SQLType = ({
9
9
  type: "int";
10
10
  nullable?: boolean;
11
11
  default?: number;
@@ -132,17 +132,17 @@ interface ShapeAPI {
132
132
  date: (config?: Omit<DateConfig, "type">) => ReturnType<typeof createBuilder>;
133
133
  datetime: (config?: Omit<DateConfig, "type">) => ReturnType<typeof createBuilder>;
134
134
  sql: <T extends SQLType>(sqlConfig: T) => Builder<"sql", T, SQLToZodType<T, false>, SQLToZodType<T, false>, z.infer<SQLToZodType<T, false>>, SQLToZodType<T, false>, SQLToZodType<T, false>>;
135
- hasMany: <T extends Schema<any>>(config: {
135
+ hasMany: <T extends Schema<any>, CreateSchema extends ReturnType<typeof createSchema<T>>>(config: {
136
136
  fromKey: string;
137
- toKey: () => any;
137
+ toKey: () => T[keyof T];
138
138
  schema: () => T;
139
139
  defaultCount?: number;
140
- }) => Builder<"relation", RelationConfig<T>, z.ZodArray<z.ZodAny>, z.ZodArray<z.ZodAny>, any[], z.ZodArray<z.ZodAny>, z.ZodArray<z.ZodAny>>;
141
- hasOne: <T extends Schema<any>>(config: {
140
+ }) => Builder<"relation", RelationConfig<T>, z.ZodArray<CreateSchema["sqlSchema"]>, z.ZodArray<CreateSchema["clientSchema"]>, any[], z.ZodArray<CreateSchema["clientSchema"]>, z.ZodArray<CreateSchema["validationSchema"]>>;
141
+ hasOne: <T extends Schema<any>, CreateSchema extends ReturnType<typeof createSchema<T>>>(config: {
142
142
  fromKey: string;
143
- toKey: () => any;
143
+ toKey: () => T[keyof T];
144
144
  schema: () => T;
145
- }) => Builder<"relation", RelationConfig<T>, z.ZodAny, z.ZodAny, any, z.ZodAny, z.ZodAny>;
145
+ }) => Builder<"relation", RelationConfig<T>, z.ZodArray<CreateSchema["sqlSchema"]>, z.ZodArray<CreateSchema["clientSchema"]>, any[], z.ZodArray<CreateSchema["clientSchema"]>, z.ZodArray<CreateSchema["validationSchema"]>>;
146
146
  manyToMany: <T extends Schema<any>>(config: {
147
147
  fromKey: string;
148
148
  toKey: () => any;
@@ -205,7 +205,7 @@ export declare function manyToMany<T extends Schema<any>>(config: {
205
205
  schema: T;
206
206
  defaultCount: number | undefined;
207
207
  };
208
- type RelationType = "hasMany" | "belongsTo" | "hasOne" | "manyToMany";
208
+ export type RelationType = "hasMany" | "hasOne" | "manyToMany";
209
209
  type BaseSchemaField<T extends SQLType = SQLType> = {
210
210
  type: "field";
211
211
  sql: T;
@@ -272,25 +272,43 @@ type SchemaDefinition = {
272
272
  _tableName: string;
273
273
  [key: string]: any;
274
274
  };
275
- type InferSchemaByKey<T, Key extends "zodSqlSchema" | "zodClientSchema" | "zodValidationSchema"> = {
275
+ type InferSchemaByKey<T, Key extends "zodSqlSchema" | "zodClientSchema" | "zodValidationSchema", Depth extends any[] = []> = Depth["length"] extends 10 ? any : {
276
276
  [K in keyof T as K extends "_tableName" ? never : K]: T[K] extends {
277
277
  config: {
278
- [P in Key]: infer S extends z.ZodTypeAny;
278
+ sql: {
279
+ type: "hasMany" | "manyToMany";
280
+ schema: () => infer S;
281
+ };
279
282
  };
280
- } ? S : T[K] extends {
281
- type: "reference";
282
- to: () => {
283
- config: {
284
- [P in Key]: infer S extends z.ZodTypeAny;
283
+ } ? z.ZodArray<S extends {
284
+ _tableName: string;
285
+ } ? z.ZodObject<InferSchemaByKey<S, Key, [...Depth, 1]>> : z.ZodObject<any>> : T[K] extends {
286
+ config: {
287
+ sql: {
288
+ type: "hasOne" | "belongsTo";
289
+ schema: () => infer S;
285
290
  };
286
291
  };
287
- } ? S : T[K] extends () => {
292
+ } ? S extends {
293
+ _tableName: string;
294
+ } ? z.ZodObject<InferSchemaByKey<S, Key, [...Depth, 1]>> : z.ZodObject<any> : T[K] extends () => {
288
295
  type: "hasMany" | "manyToMany";
289
- schema: infer S extends SchemaDefinition;
290
- } ? z.ZodArray<z.ZodObject<Prettify<InferSchemaByKey<S, Key>>>> : T[K] extends () => {
296
+ schema: infer S extends {
297
+ _tableName: string;
298
+ };
299
+ } ? z.ZodArray<z.ZodObject<InferSchemaByKey<S, Key, [...Depth, 1]>>> : T[K] extends () => {
291
300
  type: "hasOne" | "belongsTo";
292
- schema: infer S extends SchemaDefinition;
293
- } ? z.ZodObject<Prettify<InferSchemaByKey<S, Key>>> : never;
301
+ schema: infer S extends {
302
+ _tableName: string;
303
+ };
304
+ } ? z.ZodObject<InferSchemaByKey<S, Key, [...Depth, 1]>> : T[K] extends {
305
+ type: "reference";
306
+ to: infer ToFn;
307
+ } ? ToFn extends () => any ? z.ZodAny : never : T[K] extends {
308
+ config: {
309
+ [P in Key]: infer ZodSchema extends z.ZodTypeAny;
310
+ };
311
+ } ? ZodSchema : never;
294
312
  };
295
313
  type InferSqlSchema<T> = InferSchemaByKey<T, "zodSqlSchema">;
296
314
  type InferClientSchema<T> = InferSchemaByKey<T, "zodClientSchema">;
@@ -331,49 +349,4 @@ export type InferSchemaTypes<T extends {
331
349
  /** The TypeScript type for the default values object. */
332
350
  defaults: ReturnType<typeof createSchema<T>>["defaultValues"];
333
351
  }>;
334
- type SerializableFieldMetadata = {
335
- type: "field";
336
- sql: SQLType;
337
- };
338
- type SerializableRelationMetadata = {
339
- type: "relation";
340
- relationType: RelationType;
341
- fromKey: string;
342
- toKey: string;
343
- schema: SerializableSchemaMetadata;
344
- };
345
- type SerializableSchemaMetadata = {
346
- _tableName: string;
347
- primaryKey: string | null;
348
- fields: Record<string, SerializableFieldMetadata>;
349
- relations: Record<string, SerializableRelationMetadata>;
350
- };
351
- export type ProcessedSyncSchemaEntry<T extends {
352
- _tableName: string;
353
- }> = {
354
- rawSchema: T;
355
- schemas: ReturnType<typeof createSchema<T>>;
356
- validate: (data: unknown) => z.SafeParseReturnType<any, any>;
357
- validateClient: (data: unknown) => z.SafeParseReturnType<any, any>;
358
- serializable: {
359
- key: string;
360
- validationJsonSchema: object;
361
- clientJsonSchema: object;
362
- metadata: SerializableSchemaMetadata;
363
- };
364
- };
365
- export type ProcessedSyncSchemaMap<T extends Record<string, {
366
- _tableName: string;
367
- }>> = {
368
- [K in keyof T]: ProcessedSyncSchemaEntry<T[K]>;
369
- };
370
- export declare function createSyncSchema<T extends Record<string, {
371
- _tableName: string;
372
- }>>(config: {
373
- [K in keyof T]: {
374
- schema: T[K];
375
- validation?: (schema: ReturnType<typeof createSchema<T[K]>>["validationSchema"]) => z.ZodSchema;
376
- client?: (schema: ReturnType<typeof createSchema<T[K]>>["clientSchema"]) => z.ZodSchema;
377
- };
378
- }): ProcessedSyncSchemaMap<T>;
379
352
  export {};
package/dist/schema.js CHANGED
@@ -85,16 +85,16 @@ export const shape = {
85
85
  type: "hasMany",
86
86
  ...config,
87
87
  };
88
- // Just pass the config object like reference does
88
+ const placeholderSchema = z.array(z.any());
89
89
  return createBuilder({
90
90
  stage: "relation",
91
- sqlConfig: relationConfig, // Pass the whole config object
92
- sqlZod: z.array(z.any()), // Remove .optional()
93
- newZod: z.array(z.any()),
91
+ sqlConfig: relationConfig,
92
+ sqlZod: placeholderSchema,
93
+ newZod: placeholderSchema,
94
94
  initialValue: Array.from({ length: config.defaultCount || 0 }, () => ({})),
95
- clientZod: z.array(z.any()),
96
- validationZod: z.array(z.any()),
97
- });
95
+ clientZod: placeholderSchema,
96
+ validationZod: placeholderSchema,
97
+ }); // Just cast to any here to satisfy the interface
98
98
  },
99
99
  hasOne: (config) => {
100
100
  const relationConfig = {
@@ -420,15 +420,24 @@ export function createSchema(schema) {
420
420
  deferredFields.push({ key, definition });
421
421
  }
422
422
  else if (definition && typeof definition.config === "object") {
423
- // It's a standard field builder. Process it now.
424
- sqlFields[key] = definition.config.zodSqlSchema;
425
- clientFields[key] = definition.config.zodClientSchema;
426
- validationFields[key] = definition.config.zodValidationSchema;
427
- defaultValues[key] = definition.config.initialValue;
423
+ // Check if it's a relation builder
424
+ const sqlConfig = definition.config.sql;
425
+ if (sqlConfig &&
426
+ typeof sqlConfig === "object" &&
427
+ ["hasMany", "manyToMany", "hasOne", "belongsTo"].includes(sqlConfig.type)) {
428
+ // It's a relation builder - defer it
429
+ deferredFields.push({ key, definition: sqlConfig });
430
+ }
431
+ else {
432
+ // It's a standard field builder. Process it now.
433
+ sqlFields[key] = definition.config.zodSqlSchema;
434
+ clientFields[key] = definition.config.zodClientSchema;
435
+ validationFields[key] = definition.config.zodValidationSchema;
436
+ defaultValues[key] = definition.config.initialValue;
437
+ }
428
438
  }
429
439
  }
430
440
  // --- PASS 2: Process all deferred references and relations ---
431
- // Now we can safely call the functions because the schemas they refer to exist.
432
441
  for (const { key, definition } of deferredFields) {
433
442
  let resolvedDefinition = definition;
434
443
  // If it's a relation like hasMany, call the outer function to get the config object
@@ -473,130 +482,3 @@ export function createSchema(schema) {
473
482
  defaultValues: defaultValues,
474
483
  };
475
484
  }
476
- // --- 2. The Smart Introspection Logic (Also good, keep it) ---
477
- /**
478
- * (This is the smart function from the last answer that resolves `toKey` functions)
479
- */
480
- // In your cogsbox-shape file, replace the entire `serializeSchemaMetadata` function.
481
- function serializeSchemaMetadata(schema) {
482
- const fields = {};
483
- const relations = {};
484
- let primaryKey = null;
485
- for (const key in schema) {
486
- if (key === "_tableName" || key.startsWith("__"))
487
- continue;
488
- const definition = schema[key];
489
- // Case 1: Is it a relation object? Check the `type` property.
490
- if (definition &&
491
- (definition.type === "hasMany" ||
492
- definition.type === "hasOne" ||
493
- definition.type === "belongsTo" ||
494
- definition.type === "manyToMany")) {
495
- const relation = definition;
496
- let toKeyName = null;
497
- try {
498
- let targetFieldDefinition = relation.toKey;
499
- if (targetFieldDefinition &&
500
- targetFieldDefinition.type === "reference") {
501
- targetFieldDefinition = targetFieldDefinition.to;
502
- }
503
- const targetSchemaObject = relation.schema;
504
- for (const targetKey in targetSchemaObject) {
505
- if (targetSchemaObject[targetKey] === targetFieldDefinition) {
506
- toKeyName = targetKey;
507
- break;
508
- }
509
- }
510
- if (!toKeyName)
511
- throw new Error(`Could not find field name for relation target.`);
512
- relations[key] = {
513
- type: "relation",
514
- relationType: relation.type,
515
- fromKey: relation.fromKey,
516
- toKey: toKeyName,
517
- schema: serializeSchemaMetadata(targetSchemaObject),
518
- };
519
- }
520
- catch (e) {
521
- console.error(`[cogsbox-shape] Error resolving 'toKey' for relation '${key}' in schema '${schema._tableName}'.`);
522
- throw e;
523
- }
524
- // This is the critical part: ADD the processed relation to the relations object.
525
- relations[key] = {
526
- type: "relation",
527
- relationType: relation.type,
528
- fromKey: relation.fromKey,
529
- toKey: toKeyName,
530
- schema: serializeSchemaMetadata(relation.schema),
531
- };
532
- }
533
- else if (definition && definition.config && definition.config.sql) {
534
- fields[key] = { type: "field", sql: definition.config.sql };
535
- if (definition.config.sql.pk === true) {
536
- if (primaryKey)
537
- console.warn(`[cogsbox-shape] Multiple primary keys found. Using last one: '${key}'.`);
538
- primaryKey = key;
539
- }
540
- }
541
- else if (definition && definition.type === "reference") {
542
- const targetFieldBuilder = definition.to();
543
- if (targetFieldBuilder &&
544
- targetFieldBuilder.config &&
545
- targetFieldBuilder.config.sql) {
546
- fields[key] = { type: "field", sql: targetFieldBuilder.config.sql };
547
- if (targetFieldBuilder.config.sql.pk) {
548
- if (primaryKey)
549
- console.warn(`[cogsbox-shape] Multiple primary keys found. Using last one: '${key}'.`);
550
- primaryKey = key;
551
- }
552
- }
553
- }
554
- }
555
- return { _tableName: schema._tableName, primaryKey, fields, relations };
556
- }
557
- // --- 4. The Final, Corrected `createSyncSchema` Function ---
558
- export function createSyncSchema(config) {
559
- const processedOutput = {};
560
- for (const key in config) {
561
- const entry = config[key];
562
- // Part 1: Generate Zod Schemas and Live Validators (same as before)
563
- const { sqlSchema, clientSchema, validationSchema, defaultValues } = createSchema(entry.schema);
564
- const finalValidationSchema = entry.validation
565
- ? entry.validation(validationSchema)
566
- : validationSchema;
567
- const finalClientSchema = entry.client
568
- ? entry.client(clientSchema)
569
- : clientSchema;
570
- // Part 2: Generate the Serializable Payload (NEW, integrated logic)
571
- const validationJsonSchema = zodToJsonSchema(finalValidationSchema, {
572
- target: "jsonSchema7",
573
- $refStrategy: "none",
574
- });
575
- const clientJsonSchema = zodToJsonSchema(finalClientSchema, {
576
- target: "jsonSchema7",
577
- $refStrategy: "none",
578
- });
579
- const metadata = serializeSchemaMetadata(entry.schema);
580
- // Part 3: Combine EVERYTHING into the final output object for this key
581
- processedOutput[key] = {
582
- // For runtime server use
583
- rawSchema: entry.schema,
584
- schemas: {
585
- sql: sqlSchema,
586
- client: clientSchema,
587
- validation: validationSchema,
588
- defaults: defaultValues,
589
- },
590
- validate: (data) => finalValidationSchema.safeParse(data),
591
- validateClient: (data) => finalClientSchema.safeParse(data),
592
- // For deployment to DO
593
- serializable: {
594
- key,
595
- validationJsonSchema,
596
- clientJsonSchema,
597
- metadata,
598
- },
599
- };
600
- }
601
- return processedOutput;
602
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.58",
3
+ "version": "0.5.60",
4
4
  "description": "A TypeScript library for creating type-safe database schemas with Zod validation, SQL type definitions, and automatic client/server transformations. Unifies client, server, and database types through a single schema definition, with built-in support for relationships and serialization.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",