@expressive/sql 0.2.1 → 0.3.0

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/dist/index.d.mts CHANGED
@@ -45,7 +45,7 @@ declare abstract class Type {
45
45
  static one<T extends Type>(this: Type.EntityType<T>, id: number): Promise<Type.Values<T>>;
46
46
  }
47
47
  declare class Primary extends Field<number> {
48
- readonly type = "int";
48
+ readonly type = "integer";
49
49
  readonly increment = true;
50
50
  readonly optional = true;
51
51
  readonly nullable = false;
@@ -291,8 +291,8 @@ declare class Builder {
291
291
  desc: () => void;
292
292
  is(value: unknown): Cond;
293
293
  not(value: unknown): Cond;
294
- over(value: unknown, orEqual?: boolean | undefined): Cond;
295
- under(value: unknown, orEqual?: boolean | undefined): Cond;
294
+ over(value: unknown, orEqual?: boolean): Cond;
295
+ under(value: unknown, orEqual?: boolean): Cond;
296
296
  in(value: unknown[]): Cond;
297
297
  };
298
298
  andOr(...args: (Cond | Group)[]): Group;
@@ -365,18 +365,19 @@ type Optional = {
365
365
  };
366
366
  declare namespace Field {
367
367
  type Init<T extends Field = Field> = (self: T) => Partial<T> | void;
368
- type Opts<T extends Field = Field> = Partial<T> | Init<T>;
369
- type Modifier<T, TT extends Field> = T extends {
368
+ type Opts<T extends Field = Field> = Partial<T>;
369
+ type Modifier<T, TT> = T extends {
370
370
  nullable: true;
371
371
  } ? TT & Nullable : T extends {
372
372
  fallback?: any;
373
373
  increment?: true;
374
374
  } ? TT & Optional : TT;
375
- type Specify<T, TT extends Field, D extends Field = TT> = Modifier<T, T extends {
375
+ type Choose<T, TS, D extends Field = any> = T extends {
376
376
  type: infer U;
377
- } ? Extract<TT, {
378
- type: U;
379
- }> : D>;
377
+ } ? (U extends keyof TS ? TS[U] : D) : TS extends {
378
+ default: infer U;
379
+ } ? U : D;
380
+ type Infer<T, TT, D extends Field = any> = Modifier<T, Choose<T, TT, D>>;
380
381
  type Returns<T> = Field & {
381
382
  get(value: any): T;
382
383
  };
@@ -396,9 +397,9 @@ declare namespace Field {
396
397
  under(value: Query.Value<T>, orEqual?: boolean): Cond;
397
398
  in(value: Query.Value<T>[]): Cond;
398
399
  };
400
+ const does: (callback: Callback) => void;
399
401
  }
400
402
  declare class Field<T = unknown> {
401
- private options?;
402
403
  type: string;
403
404
  unique: boolean;
404
405
  nullable: boolean;
@@ -409,13 +410,14 @@ declare class Field<T = unknown> {
409
410
  parent: Type.EntityType;
410
411
  table?: Query.Table;
411
412
  query?: Builder;
412
- foreignKey?: string;
413
- foreignTable?: string;
413
+ /** If column has a reference constraint, applicable field is listed here. */
414
+ reference?: Field;
414
415
  /** Real datatype of this field in database. */
415
- datatype: string;
416
- column: string;
417
- constructor(options?: Field.Opts<Field<unknown>> | undefined);
418
- init(property: string, parent: Type.EntityType): this;
416
+ get datatype(): string;
417
+ get column(): string;
418
+ set column(value: string);
419
+ static new<T extends Field>(this: new (...args: any[]) => T, options?: Field.Opts<T>): Field;
420
+ create(property: string, parent: Type.EntityType): this;
419
421
  /**
420
422
  * Optional method generates value of property this Field is applied to when accessed inside a query.
421
423
  * If not defined, the value will be the Field itself.
@@ -444,169 +446,216 @@ declare class Field<T = unknown> {
444
446
  */
445
447
  where(): Field.Compare<T>;
446
448
  }
449
+ type Callback = (parent: Type.EntityType, property: string) => void;
447
450
 
448
451
  declare class BooleanColumn extends Field<boolean> {
449
- readonly type: "tinyint" | "varchar";
450
- readonly datatype: string;
451
- readonly TRUE: string | number;
452
- readonly FALSE: string | number;
453
- constructor(opts?: Bool.Opts);
452
+ readonly type: Bool.DataType;
453
+ readonly either?: readonly [true: string, false: string];
454
454
  get(value: unknown): boolean;
455
455
  set(value: boolean): string | number;
456
456
  }
457
457
  declare namespace Bool {
458
- interface TinyInt extends BooleanColumn {
459
- readonly type: "tinyint";
460
- }
461
- interface Char extends BooleanColumn {
462
- readonly type: "varchar";
463
- either?: [true: string, false: string];
458
+ /**
459
+ * Defines all available types of boolean columns.
460
+ * Each type represents a different way to store boolean values.
461
+ * SQL adapters will implement these types and add to interface using type as key.
462
+ * "default" is what should be returned when no type is specified by `Bool({ ... })`.
463
+ *
464
+ * @example
465
+ * declare module "@expressive/sql" {
466
+ * namespace Bool {
467
+ * interface Boolean extends Bool {
468
+ * readonly type: "boolean";
469
+ * }
470
+ *
471
+ * interface Types {
472
+ * default: Boolean;
473
+ * boolean: Boolean;
474
+ * }
475
+ * }
476
+ * }
477
+ */
478
+ interface Types {
464
479
  }
465
- type Any = TinyInt | Char;
480
+ /** All available type identifiers for Bool */
481
+ type DataType = keyof Types;
482
+ /** All available database types for Bool */
483
+ type Any = Types[keyof Types] | BooleanColumn;
466
484
  type Opts = Partial<Any>;
467
485
  }
468
- interface Bool extends BooleanColumn {
469
- }
470
- declare function Bool<T extends Bool.Opts>(opts?: T): Field.Modifier<T, T extends {
471
- type: infer U;
472
- } ? Extract<Bool.TinyInt, {
473
- type: U;
474
- }> | Extract<Bool.Char, {
475
- type: U;
476
- }> : Bool.Any>;
486
+ type Bool = Bool.Any;
487
+ declare function Bool<T extends Bool.Opts>(opts?: T): Field.Infer<T, Bool.Types, Bool>;
477
488
  declare namespace Bool {
478
489
  var Type: typeof BooleanColumn;
479
490
  }
480
491
 
481
492
  declare class NumericColumn extends Field<number> {
482
- readonly type: "int" | "tinyint" | "smallint" | "bigint" | "float" | "double";
493
+ readonly type: Num.DataType;
494
+ readonly precision?: number;
495
+ readonly scale?: number;
483
496
  set(value: number): number;
484
497
  }
485
498
  declare namespace Num {
486
- interface Int extends Num {
487
- readonly type: "int";
488
- }
489
- interface TinyInt extends Num {
490
- readonly type: "tinyint";
491
- }
492
- interface SmallInt extends Num {
493
- readonly type: "smallint";
494
- }
495
- interface BigInt extends Num {
496
- readonly type: "bigint";
497
- }
498
- interface Float extends Num {
499
- readonly type: "float";
500
- }
501
- interface Double extends Num {
502
- readonly type: "double";
499
+ /**
500
+ * Defines all available types of numeric columns.
501
+ * Each type represents a different kind of numeric data storage.
502
+ * SQL adapters will implement these types and add to interface using type as key.
503
+ * "default" is what should be returned when no type is specified by `Num({ ... })`.
504
+ *
505
+ * @example
506
+ * declare module "@expressive/sql" {
507
+ * namespace Num {
508
+ * interface Integer extends Num {
509
+ * readonly type: "integer";
510
+ * }
511
+ *
512
+ * interface Decimal extends Num {
513
+ * readonly type: "decimal";
514
+ * readonly precision: number;
515
+ * readonly scale: number;
516
+ * }
517
+ *
518
+ * interface Types {
519
+ * default: Integer;
520
+ * integer: Integer;
521
+ * decimal: Decimal;
522
+ * }
523
+ * }
524
+ * }
525
+ */
526
+ interface Types {
503
527
  }
504
- type Any = Int | TinyInt | SmallInt | BigInt | Float | Double;
528
+ /** All available type identifiers for Num */
529
+ type DataType = keyof Types;
530
+ /** All available database types for Num */
531
+ type Any = Types[keyof Types] | NumericColumn;
505
532
  type Opts = Partial<Any>;
506
533
  }
507
- interface Num extends NumericColumn {
508
- }
509
- declare function Num<T extends Num.Opts>(opts?: T): Field.Modifier<T, T extends {
510
- type: infer U;
511
- } ? Extract<Num.Int, {
512
- type: U;
513
- }> | Extract<Num.TinyInt, {
514
- type: U;
515
- }> | Extract<Num.SmallInt, {
516
- type: U;
517
- }> | Extract<Num.BigInt, {
518
- type: U;
519
- }> | Extract<Num.Float, {
520
- type: U;
521
- }> | Extract<Num.Double, {
522
- type: U;
523
- }> : Num.Int>;
534
+ type Num = Num.Any;
535
+ declare function Num<T extends Num.Opts>(opts?: T): Field.Infer<T, Num.Types, Num>;
524
536
  declare namespace Num {
525
537
  var Type: typeof NumericColumn;
526
538
  }
527
539
 
528
- declare class ForeignColumn<T extends Type> extends Field<Type.Values<T>> {
540
+ declare class ForeignColumn<T extends Type = Type> extends Field<Type.Values<T>> {
529
541
  readonly entity: Type.EntityType<T>;
530
- readonly foreignKey: string;
531
- readonly foreignTable: string;
542
+ readonly type: One.DataType;
532
543
  readonly nullable: boolean;
533
- constructor(type: Type.EntityType<T>, nullable?: boolean);
544
+ readonly onDelete?: string;
545
+ readonly onUpdate?: string;
546
+ get column(): string;
534
547
  set(value: Type.Values<T> | number): any;
535
548
  use(query: Builder): any;
536
549
  }
550
+ declare namespace One {
551
+ interface Types {
552
+ }
553
+ type DataType = keyof Types;
554
+ type Any<T extends Type = Type> = {
555
+ [K in keyof Types]: Types[K] extends ForeignColumn<any> ? Types[K] & ForeignColumn<T> : never;
556
+ }[keyof Types] | ForeignColumn<T>;
557
+ type Options = Omit<Partial<ForeignColumn<any>>, 'entity'>;
558
+ }
537
559
  interface One<T extends Type> extends ForeignColumn<T> {
538
560
  }
539
- declare function One<T extends Type>(type: Type.EntityType<T>, nullable?: false): One<T>;
540
- declare function One<T extends Type>(type: Type.EntityType<T>, nullable: boolean): One<T> & Nullable;
561
+ declare function One<T extends Type>(entity: Type.EntityType<T>): One<T>;
562
+ declare function One<T extends Type>(entity: Type.EntityType<T>, nullable: boolean): One<T> & (boolean extends true ? Nullable : {});
563
+ declare function One<T extends Type, O extends One.Options>(entity: Type.EntityType<T>, options: O): Field.Infer<O, One.Types, One<T>> & ForeignColumn<T>;
541
564
  declare namespace One {
542
565
  var Type: typeof ForeignColumn;
543
566
  }
544
567
 
545
568
  declare class StringColumn extends Field<string> {
546
- readonly type: "char" | "varchar" | "text" | "tinytext" | "mediumtext" | "longtext";
547
- readonly length = 255;
548
- constructor(opts?: Str.Opts);
569
+ readonly type: Str.DataType;
570
+ readonly length?: number;
549
571
  set(value: string): string;
550
572
  }
551
573
  declare namespace Str {
552
- interface Char extends Str {
553
- readonly type: "char";
554
- }
555
- interface VarChar extends Str {
556
- readonly type: "varchar";
557
- }
558
- interface Text extends Str {
559
- readonly type: "text";
560
- }
561
- interface TinyText extends Str {
562
- readonly type: "tinytext";
563
- }
564
- interface MediumText extends Str {
565
- readonly type: "mediumtext";
566
- }
567
- interface LongText extends Str {
568
- readonly type: "longtext";
574
+ /**
575
+ * Defines all available types of Str.
576
+ * Each type is a different kind of string column.
577
+ * SQL adapters will implement these types and add to interface using type as key.
578
+ * "default" is what should be returned when no type is specified by `Str({ ... })`.
579
+ *
580
+ * @example
581
+ * declare module "@expressive/sql" {
582
+ * namespace Str {
583
+ * interface Types {
584
+ * default: Str.Text;
585
+ * varchar: Str.VarChar;
586
+ * }
587
+ *
588
+ * interface Text extends Str {
589
+ * readonly type: "text";
590
+ * }
591
+ *
592
+ * interface VarChar extends Str {
593
+ * readonly type: "varchar";
594
+ * readonly length: number;
595
+ * }
596
+ * }
597
+ * }
598
+ */
599
+ interface Types {
569
600
  }
570
- type Any = Char | VarChar | Text | TinyText | MediumText | LongText;
571
- type Opts = Partial<Any>;
572
- }
573
- interface Str extends StringColumn {
574
- }
575
- declare function Str<T extends Str.Opts>(opts?: T): Field.Modifier<T, T extends {
576
- type: infer U;
577
- } ? Extract<Str.Char, {
578
- type: U;
579
- }> | Extract<Str.VarChar, {
580
- type: U;
581
- }> | Extract<Str.Text, {
582
- type: U;
583
- }> | Extract<Str.TinyText, {
584
- type: U;
585
- }> | Extract<Str.MediumText, {
586
- type: U;
587
- }> | Extract<Str.LongText, {
588
- type: U;
589
- }> : Str.VarChar>;
601
+ /** All available types of Str */
602
+ type DataType = keyof Types;
603
+ /** All available database types for Str */
604
+ type Any = Types[keyof Types] | StringColumn;
605
+ type Options = Partial<Any>;
606
+ }
607
+ type Str = Str.Any;
608
+ declare function Str<T extends Str.Options>(opts?: T): Field.Infer<T, Str.Types, Str>;
590
609
  declare namespace Str {
591
610
  var Type: typeof StringColumn;
592
611
  }
593
612
 
594
613
  declare class TimeColumn extends Field<Date> {
595
- readonly type: "datetime";
614
+ readonly type: Time.DataType;
596
615
  readonly fallback?: "NOW" | Date;
597
- get(value: string): Date;
598
- set(value: string | Date): string;
616
+ readonly precision?: number;
617
+ get(value: string | Date): Date;
618
+ set(value: string | Date): string | number;
599
619
  }
600
- interface Time extends TimeColumn {
601
- }
602
- declare function Time(options?: Partial<Time>): Time;
620
+ declare namespace Time {
621
+ /**
622
+ * Defines all available types of Time columns.
623
+ * Each type represents a different way to store date/time values.
624
+ * SQL adapters will implement these types and add to interface using type as key.
625
+ * "default" is what should be returned when no type is specified by `Time({ ... })`.
626
+ *
627
+ * @example
628
+ * declare module "@expressive/sql" {
629
+ * namespace Time {
630
+ * interface Timestamp extends Time {
631
+ * readonly type: "timestamp";
632
+ * readonly precision?: number;
633
+ * }
634
+ *
635
+ * interface Types {
636
+ * default: Timestamp;
637
+ * timestamp: Timestamp;
638
+ * }
639
+ * }
640
+ * }
641
+ */
642
+ interface Types {
643
+ }
644
+ /** All available type identifiers for Time */
645
+ type DataType = keyof Types;
646
+ /** All available database types for Time */
647
+ type Any = Types[keyof Types] | TimeColumn;
648
+ type Options = Partial<Any>;
649
+ }
650
+ type Time = Time.Any;
651
+ declare function Time<T extends Time.Options>(opts?: T): Field.Infer<T, Time.Types, Time>;
603
652
  declare namespace Time {
604
653
  var Type: typeof TimeColumn;
605
654
  }
606
655
 
607
656
  declare namespace Table {
608
657
  interface Options {
609
- name?: string;
658
+ table?: string;
610
659
  schema?: string;
611
660
  }
612
661
  }