@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 +175 -126
- package/dist/index.d.ts +175 -126
- package/dist/index.js +60 -100
- package/dist/index.mjs +60 -100
- package/package.json +1 -1
package/dist/index.d.ts
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 = "
|
|
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
|
|
295
|
-
under(value: unknown, orEqual?: boolean
|
|
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
|
|
369
|
-
type Modifier<T, TT
|
|
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
|
|
375
|
+
type Choose<T, TS, D extends Field = any> = T extends {
|
|
376
376
|
type: infer U;
|
|
377
|
-
} ?
|
|
378
|
-
|
|
379
|
-
}
|
|
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
|
-
|
|
413
|
-
|
|
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
|
-
|
|
418
|
-
|
|
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:
|
|
450
|
-
readonly
|
|
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
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
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
|
|
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
|
-
|
|
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:
|
|
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
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
531
|
-
readonly foreignTable: string;
|
|
542
|
+
readonly type: One.DataType;
|
|
532
543
|
readonly nullable: boolean;
|
|
533
|
-
|
|
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>(
|
|
540
|
-
declare function One<T extends Type>(
|
|
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:
|
|
547
|
-
readonly length
|
|
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
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
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
|
-
|
|
571
|
-
type
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
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:
|
|
614
|
+
readonly type: Time.DataType;
|
|
596
615
|
readonly fallback?: "NOW" | Date;
|
|
597
|
-
|
|
598
|
-
|
|
616
|
+
readonly precision?: number;
|
|
617
|
+
get(value: string | Date): Date;
|
|
618
|
+
set(value: string | Date): string | number;
|
|
599
619
|
}
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
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
|
-
|
|
658
|
+
table?: string;
|
|
610
659
|
schema?: string;
|
|
611
660
|
}
|
|
612
661
|
}
|
package/dist/index.js
CHANGED
|
@@ -67,29 +67,30 @@ var {
|
|
|
67
67
|
// src/type/Field.ts
|
|
68
68
|
var REGISTER = /* @__PURE__ */ new Map();
|
|
69
69
|
var Field = class {
|
|
70
|
-
constructor(
|
|
71
|
-
this.options = options;
|
|
70
|
+
constructor() {
|
|
72
71
|
this.type = "";
|
|
73
72
|
this.unique = false;
|
|
74
73
|
this.nullable = false;
|
|
75
74
|
this.optional = false;
|
|
76
75
|
this.increment = false;
|
|
77
76
|
}
|
|
78
|
-
|
|
79
|
-
|
|
77
|
+
/** Real datatype of this field in database. */
|
|
78
|
+
get datatype() {
|
|
79
|
+
return this.type;
|
|
80
|
+
}
|
|
81
|
+
get column() {
|
|
82
|
+
return underscore(this.property);
|
|
83
|
+
}
|
|
84
|
+
set column(value) {
|
|
85
|
+
Object.defineProperty(this, "column", { value });
|
|
86
|
+
}
|
|
87
|
+
static new(options) {
|
|
88
|
+
return Object.assign(new this(), options);
|
|
89
|
+
}
|
|
90
|
+
create(property, parent) {
|
|
80
91
|
const field = create(this);
|
|
81
92
|
field.parent = parent;
|
|
82
93
|
field.property = property;
|
|
83
|
-
field.column = underscore(property);
|
|
84
|
-
if (typeof options === "function")
|
|
85
|
-
options = options(field) || {};
|
|
86
|
-
for (const key in options) {
|
|
87
|
-
const value = options[key];
|
|
88
|
-
if (value !== void 0)
|
|
89
|
-
field[key] = value;
|
|
90
|
-
}
|
|
91
|
-
if (!field.datatype)
|
|
92
|
-
field.datatype = field.type;
|
|
93
94
|
return field;
|
|
94
95
|
}
|
|
95
96
|
/**
|
|
@@ -128,6 +129,9 @@ var Field = class {
|
|
|
128
129
|
};
|
|
129
130
|
}
|
|
130
131
|
};
|
|
132
|
+
defineProperty(Field, "does", {
|
|
133
|
+
value: (callback) => callback
|
|
134
|
+
});
|
|
131
135
|
function fields(from) {
|
|
132
136
|
let fields2 = REGISTER.get(from);
|
|
133
137
|
if (!fields2) {
|
|
@@ -136,13 +140,15 @@ function fields(from) {
|
|
|
136
140
|
const reference = new from();
|
|
137
141
|
for (const key in reference) {
|
|
138
142
|
const { value } = getOwnPropertyDescriptor(reference, key);
|
|
139
|
-
if (
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
143
|
+
if (value instanceof Field) {
|
|
144
|
+
const instance = value.create(key, from);
|
|
145
|
+
from.fields.set(key, instance);
|
|
146
|
+
freeze(instance);
|
|
147
|
+
} else if (typeof value === "function") {
|
|
148
|
+
value(from, key);
|
|
149
|
+
} else throw new Error(
|
|
150
|
+
`Entities do not support normal values, only fields. Did you forget to import \`${capitalize(typeof value)}\`?`
|
|
151
|
+
);
|
|
146
152
|
}
|
|
147
153
|
}
|
|
148
154
|
return fields2;
|
|
@@ -150,76 +156,47 @@ function fields(from) {
|
|
|
150
156
|
|
|
151
157
|
// src/columns/Bool.ts
|
|
152
158
|
var BooleanColumn = class extends Field {
|
|
153
|
-
constructor(opts = {}) {
|
|
154
|
-
let type = "tinyint";
|
|
155
|
-
let datatype = "tinyint";
|
|
156
|
-
let TRUE = 1;
|
|
157
|
-
let FALSE = 0;
|
|
158
|
-
if ("either" in opts && opts.either) {
|
|
159
|
-
[TRUE, FALSE] = opts.either;
|
|
160
|
-
type = "varchar";
|
|
161
|
-
datatype = `varchar(${Math.max(TRUE.length, FALSE.length)})`;
|
|
162
|
-
}
|
|
163
|
-
super(opts);
|
|
164
|
-
this.type = "tinyint";
|
|
165
|
-
this.TRUE = 1;
|
|
166
|
-
this.FALSE = 0;
|
|
167
|
-
this.type = type;
|
|
168
|
-
this.datatype = datatype;
|
|
169
|
-
this.TRUE = TRUE;
|
|
170
|
-
this.FALSE = FALSE;
|
|
171
|
-
}
|
|
172
159
|
get(value) {
|
|
173
|
-
|
|
160
|
+
const [TRUE] = this.either || [1];
|
|
161
|
+
return value === true || value === TRUE;
|
|
174
162
|
}
|
|
175
163
|
set(value) {
|
|
164
|
+
const [TRUE, FALSE] = this.either || [1, 0];
|
|
176
165
|
if (typeof value !== "boolean")
|
|
177
166
|
throw "Value must be a boolean.";
|
|
178
|
-
return value ?
|
|
167
|
+
return value ? TRUE : FALSE;
|
|
179
168
|
}
|
|
180
169
|
};
|
|
181
170
|
function Bool(opts) {
|
|
182
|
-
return new
|
|
171
|
+
return Bool.Type.new(opts);
|
|
183
172
|
}
|
|
184
173
|
Bool.Type = BooleanColumn;
|
|
185
174
|
|
|
186
175
|
// src/columns/Num.ts
|
|
187
176
|
var NumericColumn = class extends Field {
|
|
188
|
-
constructor() {
|
|
189
|
-
super(...arguments);
|
|
190
|
-
this.type = "int";
|
|
191
|
-
}
|
|
192
177
|
set(value) {
|
|
193
178
|
if (typeof value !== "number" || isNaN(value))
|
|
194
179
|
throw `Got '${value}' but value must be a number.`;
|
|
195
|
-
if (this.type === "int" && value !== Math.floor(value))
|
|
196
|
-
throw `Got '${value}' but datatype is integer.`;
|
|
197
180
|
return value;
|
|
198
181
|
}
|
|
199
182
|
};
|
|
200
183
|
function Num(opts) {
|
|
201
|
-
return new
|
|
184
|
+
return Num.Type.new(opts);
|
|
202
185
|
}
|
|
203
186
|
Num.Type = NumericColumn;
|
|
204
187
|
|
|
205
188
|
// src/columns/One.ts
|
|
206
189
|
var ForeignColumn = class extends Field {
|
|
207
|
-
constructor(
|
|
208
|
-
super(
|
|
209
|
-
column: underscore(property) + "_id"
|
|
210
|
-
}));
|
|
211
|
-
this.foreignKey = "id";
|
|
190
|
+
constructor() {
|
|
191
|
+
super(...arguments);
|
|
212
192
|
this.nullable = false;
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
this.
|
|
216
|
-
this.foreignTable = type.table;
|
|
217
|
-
this.foreignTable = type.table;
|
|
218
|
-
this.nullable = nullable || false;
|
|
193
|
+
}
|
|
194
|
+
get column() {
|
|
195
|
+
return underscore(this.property) + "_id";
|
|
219
196
|
}
|
|
220
197
|
set(value) {
|
|
221
198
|
if (this.query?.tables.has(value))
|
|
222
|
-
return value[this.
|
|
199
|
+
return value[this.reference.column];
|
|
223
200
|
return value;
|
|
224
201
|
}
|
|
225
202
|
use(query) {
|
|
@@ -228,30 +205,17 @@ var ForeignColumn = class extends Field {
|
|
|
228
205
|
return table;
|
|
229
206
|
}
|
|
230
207
|
};
|
|
231
|
-
function One(
|
|
232
|
-
|
|
208
|
+
function One(entity, arg2) {
|
|
209
|
+
if (typeof arg2 === "boolean")
|
|
210
|
+
arg2 = { nullable: arg2 };
|
|
211
|
+
const opts = Object.assign({}, arg2);
|
|
212
|
+
const reference = entity.fields.get("id");
|
|
213
|
+
return One.Type.new({ entity, reference, ...opts });
|
|
233
214
|
}
|
|
234
215
|
One.Type = ForeignColumn;
|
|
235
216
|
|
|
236
217
|
// src/columns/Str.ts
|
|
237
|
-
var VARIABLE_TYPE = /* @__PURE__ */ new Set(["char", "varchar", "text"]);
|
|
238
218
|
var StringColumn = class extends Field {
|
|
239
|
-
constructor(opts) {
|
|
240
|
-
super(({ parent, property }) => {
|
|
241
|
-
let { type = "varchar", length = 255, datatype } = opts || {};
|
|
242
|
-
if (VARIABLE_TYPE.has(type))
|
|
243
|
-
if (length)
|
|
244
|
-
datatype = `${datatype || type}(${length})`;
|
|
245
|
-
else
|
|
246
|
-
throw `Can't determine datatype! Length is not specified for ${parent.name}.${property}.`;
|
|
247
|
-
return {
|
|
248
|
-
datatype,
|
|
249
|
-
...opts
|
|
250
|
-
};
|
|
251
|
-
});
|
|
252
|
-
this.type = "varchar";
|
|
253
|
-
this.length = 255;
|
|
254
|
-
}
|
|
255
219
|
set(value) {
|
|
256
220
|
if (typeof value !== "string")
|
|
257
221
|
throw "Value must be a string.";
|
|
@@ -261,31 +225,30 @@ var StringColumn = class extends Field {
|
|
|
261
225
|
}
|
|
262
226
|
};
|
|
263
227
|
function Str(opts) {
|
|
264
|
-
return new
|
|
228
|
+
return Str.Type.new(opts);
|
|
265
229
|
}
|
|
266
230
|
Str.Type = StringColumn;
|
|
267
231
|
|
|
268
232
|
// src/columns/Time.ts
|
|
269
233
|
var TimeColumn = class extends Field {
|
|
270
|
-
constructor() {
|
|
271
|
-
super(...arguments);
|
|
272
|
-
this.type = "datetime";
|
|
273
|
-
this.fallback = void 0;
|
|
274
|
-
}
|
|
275
234
|
get(value) {
|
|
276
|
-
|
|
235
|
+
if (value instanceof Date)
|
|
236
|
+
return value;
|
|
237
|
+
return new Date(value.replace(/[-]/g, "/"));
|
|
277
238
|
}
|
|
278
239
|
set(value) {
|
|
279
240
|
if (value === "NOW")
|
|
280
241
|
return "CURRENT_TIMESTAMP";
|
|
281
242
|
else if (value instanceof Date)
|
|
282
243
|
return value.toISOString().slice(0, 19).replace("T", " ");
|
|
244
|
+
else if (typeof value === "string")
|
|
245
|
+
return value;
|
|
283
246
|
else
|
|
284
|
-
throw "Value must be a Date or 'NOW'.";
|
|
247
|
+
throw "Value must be a Date, string, or 'NOW'.";
|
|
285
248
|
}
|
|
286
249
|
};
|
|
287
|
-
function Time(
|
|
288
|
-
return new
|
|
250
|
+
function Time(opts) {
|
|
251
|
+
return Time.Type.new(opts);
|
|
289
252
|
}
|
|
290
253
|
Time.Type = TimeColumn;
|
|
291
254
|
|
|
@@ -477,7 +440,7 @@ var Builder = class {
|
|
|
477
440
|
return where;
|
|
478
441
|
}
|
|
479
442
|
if (right instanceof Parameter) {
|
|
480
|
-
right.digest = left.set.bind(
|
|
443
|
+
right.digest = left.set.bind(left);
|
|
481
444
|
}
|
|
482
445
|
return this.filters.add(left, op2, right);
|
|
483
446
|
}
|
|
@@ -1020,7 +983,7 @@ var Type = _Type;
|
|
|
1020
983
|
var Primary = class extends Field {
|
|
1021
984
|
constructor() {
|
|
1022
985
|
super(...arguments);
|
|
1023
|
-
this.type = "
|
|
986
|
+
this.type = "integer";
|
|
1024
987
|
this.increment = true;
|
|
1025
988
|
this.optional = true;
|
|
1026
989
|
this.nullable = false;
|
|
@@ -1115,15 +1078,12 @@ Query.fn = { ...MathOps, bit: BitWise };
|
|
|
1115
1078
|
|
|
1116
1079
|
// src/type/Table.ts
|
|
1117
1080
|
function Table(arg1, arg2) {
|
|
1118
|
-
return
|
|
1081
|
+
return Field.does((parent) => {
|
|
1119
1082
|
if (typeof arg1 == "string") {
|
|
1120
|
-
const [
|
|
1121
|
-
arg1 = { schema, ...arg2,
|
|
1083
|
+
const [table, schema] = arg1.split(".").reverse();
|
|
1084
|
+
arg1 = { schema, ...arg2, table };
|
|
1122
1085
|
}
|
|
1123
|
-
|
|
1124
|
-
if (name)
|
|
1125
|
-
parent.table = name;
|
|
1126
|
-
assign(parent, rest);
|
|
1086
|
+
assign(parent, arg1);
|
|
1127
1087
|
});
|
|
1128
1088
|
}
|
|
1129
1089
|
// Annotate the CommonJS export names for ESM import in node:
|