@expressive/sql 0.3.0 → 0.3.1

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
@@ -53,8 +53,8 @@ declare class Primary extends Field<number> {
53
53
  readonly unique = true;
54
54
  }
55
55
 
56
- type ANumeric = Query.Value<string | number>;
57
- type Numeric = Query.Value<number>;
56
+ type ANumeric = Query.Match<string | number>;
57
+ type Numeric = Query.Match<number>;
58
58
  interface Bitwise {
59
59
  not(value: Numeric): Numeric;
60
60
  and(left: Numeric, right: Numeric): Numeric;
@@ -75,8 +75,8 @@ interface MathOps {
75
75
  }
76
76
  declare const MathOps: MathOps;
77
77
  declare class Computed<T> extends Value {
78
- readonly left?: Query.Value | Field<T> | Computed<T>;
79
- readonly right: Query.Value | Field<T> | Computed<T>;
78
+ readonly left?: Query.Match | Field<T> | Computed<T>;
79
+ readonly right: Query.Match | Field<T> | Computed<T>;
80
80
  readonly operator: string;
81
81
  readonly rank: number;
82
82
  constructor(operator: string, value: Value | Field<T> | Computed<T>, right?: Value | Field<T> | Computed<T>, rank?: number);
@@ -149,7 +149,7 @@ declare namespace Query {
149
149
  type FromData<T extends {}> = {
150
150
  [K in keyof T]: Field<T[K]>;
151
151
  };
152
- type Value<T = any> = T | Field<T> | Computed<T>;
152
+ type Match<T = any> = T | (T extends Field<infer U> ? Match<U> : Field<T> | Computed<T>);
153
153
  type Insert<T extends Type> = {
154
154
  [K in Type.Required<T>]: Field.Assigns<T[K]> | Field;
155
155
  } & {
@@ -170,7 +170,7 @@ declare namespace Query {
170
170
  *
171
171
  * Will alternate between AND-OR depending on depth, starting with OR.
172
172
  */
173
- (...orWhere: (Cond | Group)[]): Group;
173
+ (...orWhere: Expression[]): Group;
174
174
  /**
175
175
  * Declare inserts to be made into a given table.
176
176
  */
@@ -199,13 +199,19 @@ declare namespace Query {
199
199
  };
200
200
  connection: Connection.Ready;
201
201
  };
202
- type Compare<T extends Field> = ReturnType<T["where"]>;
202
+ type Compare<T> = {
203
+ is(value: Match<T>): Expression;
204
+ not(value: Match<T>): Expression;
205
+ over(value: Match<T>, orEqual?: boolean): Expression;
206
+ under(value: Match<T>, orEqual?: boolean): Expression;
207
+ in(value: Match<T>[]): Expression;
208
+ };
203
209
  type Asserts<T extends Field> = Compare<T> & {
204
210
  asc(): void;
205
211
  desc(): void;
206
212
  };
207
213
  type Updates<T> = Field.Updates<T> | T | Field;
208
- type Function<R> = (this: Builder, where: Where, fn: Query.Functions) => R;
214
+ type Function<R> = (this: Builder, where: Where, fn: Functions) => R;
209
215
  type Factory<R, A extends any[]> = Function<(...args: A) => R>;
210
216
  type Template<A extends any[]> = (...args: A) => Query;
211
217
  type TemplateSelects<T, A extends any[]> = (...args: A) => Selects<T>;
@@ -286,22 +292,29 @@ declare class Builder {
286
292
  commit(returns: unknown): void;
287
293
  inject(args: number): (() => Parameter)[];
288
294
  table(table: Query.From): Query.Verbs<Type>;
295
+ compare<T extends Field>(field: T): {
296
+ is: (right: unknown, orEqual?: boolean) => Expression;
297
+ in: (right: unknown, orEqual?: boolean) => Expression;
298
+ not: (right: unknown, orEqual?: boolean) => Expression;
299
+ over: (right: unknown, orEqual?: boolean) => Expression;
300
+ under: (right: unknown, orEqual?: boolean) => Expression;
301
+ };
289
302
  field<T extends Field>(field: T): {
290
303
  asc: () => void;
291
304
  desc: () => void;
292
- is(value: unknown): Cond;
293
- not(value: unknown): Cond;
294
- over(value: unknown, orEqual?: boolean): Cond;
295
- under(value: unknown, orEqual?: boolean): Cond;
296
- in(value: unknown[]): Cond;
305
+ is: (right: unknown, orEqual?: boolean) => Expression;
306
+ in: (right: unknown, orEqual?: boolean) => Expression;
307
+ not: (right: unknown, orEqual?: boolean) => Expression;
308
+ over: (right: unknown, orEqual?: boolean) => Expression;
309
+ under: (right: unknown, orEqual?: boolean) => Expression;
297
310
  };
298
- andOr(...args: (Cond | Group)[]): Group;
311
+ andOr(...args: Expression[]): Group;
299
312
  use<T extends Type>(type: Type.EntityType<T>, optional?: false): Query.From<T>;
300
313
  use<T extends Type>(type: Type.EntityType<T>, optional: boolean): Query.Join<T>;
301
314
  use<T extends Type>(type: Type.EntityType<T>, ...inserts: Type.Insert<T>[]): Query.From<T>;
302
315
  insert<T extends Type>(table: Builder.Table<T>, input: Type.Insert<T>): void;
303
316
  digest(value: any, field: Field): any;
304
- where(left: Field, op: string, right: unknown): Cond | Group;
317
+ where(left: Field, op: string, right: unknown): Expression;
305
318
  with<T extends Record<string, any>>(data: Iterable<T>): { [K in keyof T]: Field<T[K]>; };
306
319
  accept(args: unknown[]): unknown[];
307
320
  parse(raw: Record<string, any>): any;
@@ -338,6 +351,7 @@ declare class DataTable<T extends Record<string, unknown> = any> extends Paramet
338
351
  constructor(parent: Builder, input: Iterable<T>);
339
352
  toParam(): any;
340
353
  }
354
+ type Expression = Cond | Group;
341
355
  declare class Cond {
342
356
  readonly left: Field;
343
357
  readonly op: string;
@@ -346,9 +360,9 @@ declare class Cond {
346
360
  constructor(left: Field, op: string, right: unknown, restricted?: boolean);
347
361
  }
348
362
  declare class Group {
349
- children: Set<Cond | Group>;
350
- add(left: Field | Group | Cond, op?: string, right?: unknown): Cond | Group;
351
- delete(child: Cond | Group): void;
363
+ children: Set<Expression>;
364
+ add(left: Field | Expression, op?: string, right?: unknown): Expression;
365
+ delete(child: Expression): void;
352
366
  get size(): number;
353
367
  }
354
368
  declare class QueryTemplate extends Value {
@@ -368,16 +382,16 @@ declare namespace Field {
368
382
  type Opts<T extends Field = Field> = Partial<T>;
369
383
  type Modifier<T, TT> = T extends {
370
384
  nullable: true;
371
- } ? TT & Nullable : T extends {
385
+ } | true ? TT & Nullable : T extends {
372
386
  fallback?: any;
373
387
  increment?: true;
374
388
  } ? TT & Optional : TT;
375
- type Choose<T, TS, D extends Field = any> = T extends {
389
+ type Type<T, A, D extends Field = any> = T extends {
376
390
  type: infer U;
377
- } ? (U extends keyof TS ? TS[U] : D) : TS extends {
391
+ } ? (U extends keyof A ? A[U] : D) : A extends {
378
392
  default: infer U;
379
393
  } ? U : D;
380
- type Infer<T, TT, D extends Field = any> = Modifier<T, Choose<T, TT, D>>;
394
+ type Infer<T, A, D extends Field = any> = Modifier<T, Type<T, A, D>>;
381
395
  type Returns<T> = Field & {
382
396
  get(value: any): T;
383
397
  };
@@ -390,13 +404,6 @@ declare namespace Field {
390
404
  type Updates<T> = T extends Accepts<infer U> ? (T extends Nullable ? U | null : U) | undefined : never;
391
405
  type Assigns<T> = T extends Accepts<infer U> ? T extends Nullable ? U | null | undefined : T extends Optional ? U | undefined : U : never;
392
406
  type Output = Record<string, string | number>;
393
- type Compare<T> = {
394
- is(value: Query.Value<T>): Cond;
395
- not(value: Query.Value<T>): Cond;
396
- over(value: Query.Value<T>, orEqual?: boolean): Cond;
397
- under(value: Query.Value<T>, orEqual?: boolean): Cond;
398
- in(value: Query.Value<T>[]): Cond;
399
- };
400
407
  const does: (callback: Callback) => void;
401
408
  }
402
409
  declare class Field<T = unknown> {
@@ -406,6 +413,7 @@ declare class Field<T = unknown> {
406
413
  optional: boolean;
407
414
  increment: boolean;
408
415
  fallback?: unknown;
416
+ column: string;
409
417
  property: string;
410
418
  parent: Type.EntityType;
411
419
  table?: Query.Table;
@@ -414,9 +422,7 @@ declare class Field<T = unknown> {
414
422
  reference?: Field;
415
423
  /** Real datatype of this field in database. */
416
424
  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;
425
+ static new<T extends Field>(this: new (...args: any[]) => T, options?: Field.Opts<T> | boolean): Field;
420
426
  create(property: string, parent: Type.EntityType): this;
421
427
  /**
422
428
  * Optional method generates value of property this Field is applied to when accessed inside a query.
@@ -438,13 +444,6 @@ declare class Field<T = unknown> {
438
444
  * a TINYINT(1) field to a boolean, or a DATETIME field to a Date object.
439
445
  */
440
446
  get(value: any): T;
441
- raw(value: T): string;
442
- /**
443
- * This method is used to compare this field with another value in a query.
444
- *
445
- * @param acc Set of comparisons to be added to if not part of a group.
446
- */
447
- where(): Field.Compare<T>;
448
447
  }
449
448
  type Callback = (parent: Type.EntityType, property: string) => void;
450
449
 
@@ -484,6 +483,7 @@ declare namespace Bool {
484
483
  type Opts = Partial<Any>;
485
484
  }
486
485
  type Bool = Bool.Any;
486
+ declare function Bool<T extends boolean>(nullable: T): Field.Infer<T, Bool.Types, Bool>;
487
487
  declare function Bool<T extends Bool.Opts>(opts?: T): Field.Infer<T, Bool.Types, Bool>;
488
488
  declare namespace Bool {
489
489
  var Type: typeof BooleanColumn;
@@ -532,18 +532,18 @@ declare namespace Num {
532
532
  type Opts = Partial<Any>;
533
533
  }
534
534
  type Num = Num.Any;
535
+ declare function Num<T extends boolean>(nullable: T): Field.Infer<T, Num.Types, Num>;
535
536
  declare function Num<T extends Num.Opts>(opts?: T): Field.Infer<T, Num.Types, Num>;
536
537
  declare namespace Num {
537
538
  var Type: typeof NumericColumn;
538
539
  }
539
540
 
540
541
  declare class ForeignColumn<T extends Type = Type> extends Field<Type.Values<T>> {
541
- readonly entity: Type.EntityType<T>;
542
542
  readonly type: One.DataType;
543
+ readonly entity: Type.EntityType<T>;
543
544
  readonly nullable: boolean;
544
545
  readonly onDelete?: string;
545
546
  readonly onUpdate?: string;
546
- get column(): string;
547
547
  set(value: Type.Values<T> | number): any;
548
548
  use(query: Builder): any;
549
549
  }
@@ -551,16 +551,15 @@ declare namespace One {
551
551
  interface Types {
552
552
  }
553
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'>;
554
+ type Options = {
555
+ [K in Exclude<keyof ForeignColumn, 'entity'>]?: ForeignColumn[K];
556
+ };
558
557
  }
559
558
  interface One<T extends Type> extends ForeignColumn<T> {
560
559
  }
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>;
560
+ declare function One<T extends Type>(entity: Type.EntityType<T>, nullable?: false): One<T>;
561
+ declare function One<T extends Type>(entity: Type.EntityType<T>, nullable: boolean): One<T> & Nullable;
562
+ declare function One<T extends Type, O extends One.Options>(entity: Type.EntityType<T>, options: O): Field.Modifier<O, One<T>>;
564
563
  declare namespace One {
565
564
  var Type: typeof ForeignColumn;
566
565
  }
@@ -605,6 +604,7 @@ declare namespace Str {
605
604
  type Options = Partial<Any>;
606
605
  }
607
606
  type Str = Str.Any;
607
+ declare function Str<T extends boolean>(nullable: T): Field.Infer<T, Str.Types, Str>;
608
608
  declare function Str<T extends Str.Options>(opts?: T): Field.Infer<T, Str.Types, Str>;
609
609
  declare namespace Str {
610
610
  var Type: typeof StringColumn;
@@ -648,6 +648,7 @@ declare namespace Time {
648
648
  type Options = Partial<Any>;
649
649
  }
650
650
  type Time = Time.Any;
651
+ declare function Time<T extends boolean>(nullable: T): Field.Infer<T, Time.Types, Time>;
651
652
  declare function Time<T extends Time.Options>(opts?: T): Field.Infer<T, Time.Types, Time>;
652
653
  declare namespace Time {
653
654
  var Type: typeof TimeColumn;
package/dist/index.d.ts CHANGED
@@ -53,8 +53,8 @@ declare class Primary extends Field<number> {
53
53
  readonly unique = true;
54
54
  }
55
55
 
56
- type ANumeric = Query.Value<string | number>;
57
- type Numeric = Query.Value<number>;
56
+ type ANumeric = Query.Match<string | number>;
57
+ type Numeric = Query.Match<number>;
58
58
  interface Bitwise {
59
59
  not(value: Numeric): Numeric;
60
60
  and(left: Numeric, right: Numeric): Numeric;
@@ -75,8 +75,8 @@ interface MathOps {
75
75
  }
76
76
  declare const MathOps: MathOps;
77
77
  declare class Computed<T> extends Value {
78
- readonly left?: Query.Value | Field<T> | Computed<T>;
79
- readonly right: Query.Value | Field<T> | Computed<T>;
78
+ readonly left?: Query.Match | Field<T> | Computed<T>;
79
+ readonly right: Query.Match | Field<T> | Computed<T>;
80
80
  readonly operator: string;
81
81
  readonly rank: number;
82
82
  constructor(operator: string, value: Value | Field<T> | Computed<T>, right?: Value | Field<T> | Computed<T>, rank?: number);
@@ -149,7 +149,7 @@ declare namespace Query {
149
149
  type FromData<T extends {}> = {
150
150
  [K in keyof T]: Field<T[K]>;
151
151
  };
152
- type Value<T = any> = T | Field<T> | Computed<T>;
152
+ type Match<T = any> = T | (T extends Field<infer U> ? Match<U> : Field<T> | Computed<T>);
153
153
  type Insert<T extends Type> = {
154
154
  [K in Type.Required<T>]: Field.Assigns<T[K]> | Field;
155
155
  } & {
@@ -170,7 +170,7 @@ declare namespace Query {
170
170
  *
171
171
  * Will alternate between AND-OR depending on depth, starting with OR.
172
172
  */
173
- (...orWhere: (Cond | Group)[]): Group;
173
+ (...orWhere: Expression[]): Group;
174
174
  /**
175
175
  * Declare inserts to be made into a given table.
176
176
  */
@@ -199,13 +199,19 @@ declare namespace Query {
199
199
  };
200
200
  connection: Connection.Ready;
201
201
  };
202
- type Compare<T extends Field> = ReturnType<T["where"]>;
202
+ type Compare<T> = {
203
+ is(value: Match<T>): Expression;
204
+ not(value: Match<T>): Expression;
205
+ over(value: Match<T>, orEqual?: boolean): Expression;
206
+ under(value: Match<T>, orEqual?: boolean): Expression;
207
+ in(value: Match<T>[]): Expression;
208
+ };
203
209
  type Asserts<T extends Field> = Compare<T> & {
204
210
  asc(): void;
205
211
  desc(): void;
206
212
  };
207
213
  type Updates<T> = Field.Updates<T> | T | Field;
208
- type Function<R> = (this: Builder, where: Where, fn: Query.Functions) => R;
214
+ type Function<R> = (this: Builder, where: Where, fn: Functions) => R;
209
215
  type Factory<R, A extends any[]> = Function<(...args: A) => R>;
210
216
  type Template<A extends any[]> = (...args: A) => Query;
211
217
  type TemplateSelects<T, A extends any[]> = (...args: A) => Selects<T>;
@@ -286,22 +292,29 @@ declare class Builder {
286
292
  commit(returns: unknown): void;
287
293
  inject(args: number): (() => Parameter)[];
288
294
  table(table: Query.From): Query.Verbs<Type>;
295
+ compare<T extends Field>(field: T): {
296
+ is: (right: unknown, orEqual?: boolean) => Expression;
297
+ in: (right: unknown, orEqual?: boolean) => Expression;
298
+ not: (right: unknown, orEqual?: boolean) => Expression;
299
+ over: (right: unknown, orEqual?: boolean) => Expression;
300
+ under: (right: unknown, orEqual?: boolean) => Expression;
301
+ };
289
302
  field<T extends Field>(field: T): {
290
303
  asc: () => void;
291
304
  desc: () => void;
292
- is(value: unknown): Cond;
293
- not(value: unknown): Cond;
294
- over(value: unknown, orEqual?: boolean): Cond;
295
- under(value: unknown, orEqual?: boolean): Cond;
296
- in(value: unknown[]): Cond;
305
+ is: (right: unknown, orEqual?: boolean) => Expression;
306
+ in: (right: unknown, orEqual?: boolean) => Expression;
307
+ not: (right: unknown, orEqual?: boolean) => Expression;
308
+ over: (right: unknown, orEqual?: boolean) => Expression;
309
+ under: (right: unknown, orEqual?: boolean) => Expression;
297
310
  };
298
- andOr(...args: (Cond | Group)[]): Group;
311
+ andOr(...args: Expression[]): Group;
299
312
  use<T extends Type>(type: Type.EntityType<T>, optional?: false): Query.From<T>;
300
313
  use<T extends Type>(type: Type.EntityType<T>, optional: boolean): Query.Join<T>;
301
314
  use<T extends Type>(type: Type.EntityType<T>, ...inserts: Type.Insert<T>[]): Query.From<T>;
302
315
  insert<T extends Type>(table: Builder.Table<T>, input: Type.Insert<T>): void;
303
316
  digest(value: any, field: Field): any;
304
- where(left: Field, op: string, right: unknown): Cond | Group;
317
+ where(left: Field, op: string, right: unknown): Expression;
305
318
  with<T extends Record<string, any>>(data: Iterable<T>): { [K in keyof T]: Field<T[K]>; };
306
319
  accept(args: unknown[]): unknown[];
307
320
  parse(raw: Record<string, any>): any;
@@ -338,6 +351,7 @@ declare class DataTable<T extends Record<string, unknown> = any> extends Paramet
338
351
  constructor(parent: Builder, input: Iterable<T>);
339
352
  toParam(): any;
340
353
  }
354
+ type Expression = Cond | Group;
341
355
  declare class Cond {
342
356
  readonly left: Field;
343
357
  readonly op: string;
@@ -346,9 +360,9 @@ declare class Cond {
346
360
  constructor(left: Field, op: string, right: unknown, restricted?: boolean);
347
361
  }
348
362
  declare class Group {
349
- children: Set<Cond | Group>;
350
- add(left: Field | Group | Cond, op?: string, right?: unknown): Cond | Group;
351
- delete(child: Cond | Group): void;
363
+ children: Set<Expression>;
364
+ add(left: Field | Expression, op?: string, right?: unknown): Expression;
365
+ delete(child: Expression): void;
352
366
  get size(): number;
353
367
  }
354
368
  declare class QueryTemplate extends Value {
@@ -368,16 +382,16 @@ declare namespace Field {
368
382
  type Opts<T extends Field = Field> = Partial<T>;
369
383
  type Modifier<T, TT> = T extends {
370
384
  nullable: true;
371
- } ? TT & Nullable : T extends {
385
+ } | true ? TT & Nullable : T extends {
372
386
  fallback?: any;
373
387
  increment?: true;
374
388
  } ? TT & Optional : TT;
375
- type Choose<T, TS, D extends Field = any> = T extends {
389
+ type Type<T, A, D extends Field = any> = T extends {
376
390
  type: infer U;
377
- } ? (U extends keyof TS ? TS[U] : D) : TS extends {
391
+ } ? (U extends keyof A ? A[U] : D) : A extends {
378
392
  default: infer U;
379
393
  } ? U : D;
380
- type Infer<T, TT, D extends Field = any> = Modifier<T, Choose<T, TT, D>>;
394
+ type Infer<T, A, D extends Field = any> = Modifier<T, Type<T, A, D>>;
381
395
  type Returns<T> = Field & {
382
396
  get(value: any): T;
383
397
  };
@@ -390,13 +404,6 @@ declare namespace Field {
390
404
  type Updates<T> = T extends Accepts<infer U> ? (T extends Nullable ? U | null : U) | undefined : never;
391
405
  type Assigns<T> = T extends Accepts<infer U> ? T extends Nullable ? U | null | undefined : T extends Optional ? U | undefined : U : never;
392
406
  type Output = Record<string, string | number>;
393
- type Compare<T> = {
394
- is(value: Query.Value<T>): Cond;
395
- not(value: Query.Value<T>): Cond;
396
- over(value: Query.Value<T>, orEqual?: boolean): Cond;
397
- under(value: Query.Value<T>, orEqual?: boolean): Cond;
398
- in(value: Query.Value<T>[]): Cond;
399
- };
400
407
  const does: (callback: Callback) => void;
401
408
  }
402
409
  declare class Field<T = unknown> {
@@ -406,6 +413,7 @@ declare class Field<T = unknown> {
406
413
  optional: boolean;
407
414
  increment: boolean;
408
415
  fallback?: unknown;
416
+ column: string;
409
417
  property: string;
410
418
  parent: Type.EntityType;
411
419
  table?: Query.Table;
@@ -414,9 +422,7 @@ declare class Field<T = unknown> {
414
422
  reference?: Field;
415
423
  /** Real datatype of this field in database. */
416
424
  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;
425
+ static new<T extends Field>(this: new (...args: any[]) => T, options?: Field.Opts<T> | boolean): Field;
420
426
  create(property: string, parent: Type.EntityType): this;
421
427
  /**
422
428
  * Optional method generates value of property this Field is applied to when accessed inside a query.
@@ -438,13 +444,6 @@ declare class Field<T = unknown> {
438
444
  * a TINYINT(1) field to a boolean, or a DATETIME field to a Date object.
439
445
  */
440
446
  get(value: any): T;
441
- raw(value: T): string;
442
- /**
443
- * This method is used to compare this field with another value in a query.
444
- *
445
- * @param acc Set of comparisons to be added to if not part of a group.
446
- */
447
- where(): Field.Compare<T>;
448
447
  }
449
448
  type Callback = (parent: Type.EntityType, property: string) => void;
450
449
 
@@ -484,6 +483,7 @@ declare namespace Bool {
484
483
  type Opts = Partial<Any>;
485
484
  }
486
485
  type Bool = Bool.Any;
486
+ declare function Bool<T extends boolean>(nullable: T): Field.Infer<T, Bool.Types, Bool>;
487
487
  declare function Bool<T extends Bool.Opts>(opts?: T): Field.Infer<T, Bool.Types, Bool>;
488
488
  declare namespace Bool {
489
489
  var Type: typeof BooleanColumn;
@@ -532,18 +532,18 @@ declare namespace Num {
532
532
  type Opts = Partial<Any>;
533
533
  }
534
534
  type Num = Num.Any;
535
+ declare function Num<T extends boolean>(nullable: T): Field.Infer<T, Num.Types, Num>;
535
536
  declare function Num<T extends Num.Opts>(opts?: T): Field.Infer<T, Num.Types, Num>;
536
537
  declare namespace Num {
537
538
  var Type: typeof NumericColumn;
538
539
  }
539
540
 
540
541
  declare class ForeignColumn<T extends Type = Type> extends Field<Type.Values<T>> {
541
- readonly entity: Type.EntityType<T>;
542
542
  readonly type: One.DataType;
543
+ readonly entity: Type.EntityType<T>;
543
544
  readonly nullable: boolean;
544
545
  readonly onDelete?: string;
545
546
  readonly onUpdate?: string;
546
- get column(): string;
547
547
  set(value: Type.Values<T> | number): any;
548
548
  use(query: Builder): any;
549
549
  }
@@ -551,16 +551,15 @@ declare namespace One {
551
551
  interface Types {
552
552
  }
553
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'>;
554
+ type Options = {
555
+ [K in Exclude<keyof ForeignColumn, 'entity'>]?: ForeignColumn[K];
556
+ };
558
557
  }
559
558
  interface One<T extends Type> extends ForeignColumn<T> {
560
559
  }
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>;
560
+ declare function One<T extends Type>(entity: Type.EntityType<T>, nullable?: false): One<T>;
561
+ declare function One<T extends Type>(entity: Type.EntityType<T>, nullable: boolean): One<T> & Nullable;
562
+ declare function One<T extends Type, O extends One.Options>(entity: Type.EntityType<T>, options: O): Field.Modifier<O, One<T>>;
564
563
  declare namespace One {
565
564
  var Type: typeof ForeignColumn;
566
565
  }
@@ -605,6 +604,7 @@ declare namespace Str {
605
604
  type Options = Partial<Any>;
606
605
  }
607
606
  type Str = Str.Any;
607
+ declare function Str<T extends boolean>(nullable: T): Field.Infer<T, Str.Types, Str>;
608
608
  declare function Str<T extends Str.Options>(opts?: T): Field.Infer<T, Str.Types, Str>;
609
609
  declare namespace Str {
610
610
  var Type: typeof StringColumn;
@@ -648,6 +648,7 @@ declare namespace Time {
648
648
  type Options = Partial<Any>;
649
649
  }
650
650
  type Time = Time.Any;
651
+ declare function Time<T extends boolean>(nullable: T): Field.Infer<T, Time.Types, Time>;
651
652
  declare function Time<T extends Time.Options>(opts?: T): Field.Infer<T, Time.Types, Time>;
652
653
  declare namespace Time {
653
654
  var Type: typeof TimeColumn;
package/dist/index.js CHANGED
@@ -78,19 +78,17 @@ var Field = class {
78
78
  get datatype() {
79
79
  return this.type;
80
80
  }
81
- get column() {
82
- return underscore(this.property);
83
- }
84
- set column(value) {
85
- Object.defineProperty(this, "column", { value });
86
- }
87
81
  static new(options) {
82
+ if (typeof options === "boolean")
83
+ options = { nullable: options };
88
84
  return Object.assign(new this(), options);
89
85
  }
90
86
  create(property, parent) {
91
87
  const field = create(this);
92
88
  field.parent = parent;
93
89
  field.property = property;
90
+ if (!field.column)
91
+ field.column = underscore(property);
94
92
  return field;
95
93
  }
96
94
  /**
@@ -110,24 +108,6 @@ var Field = class {
110
108
  get(value) {
111
109
  return value;
112
110
  }
113
- raw(value) {
114
- return escape(this.set(value));
115
- }
116
- /**
117
- * This method is used to compare this field with another value in a query.
118
- *
119
- * @param acc Set of comparisons to be added to if not part of a group.
120
- */
121
- where() {
122
- const use = (op2) => (right, orEqual) => this.query.where(this, op2 + (orEqual ? "=" : ""), right);
123
- return {
124
- is: use("="),
125
- in: use("IN"),
126
- not: use("<>"),
127
- over: use(">"),
128
- under: use("<")
129
- };
130
- }
131
111
  };
132
112
  defineProperty(Field, "does", {
133
113
  value: (callback) => callback
@@ -180,7 +160,7 @@ var NumericColumn = class extends Field {
180
160
  return value;
181
161
  }
182
162
  };
183
- function Num(opts) {
163
+ function Num(opts = {}) {
184
164
  return Num.Type.new(opts);
185
165
  }
186
166
  Num.Type = NumericColumn;
@@ -191,9 +171,6 @@ var ForeignColumn = class extends Field {
191
171
  super(...arguments);
192
172
  this.nullable = false;
193
173
  }
194
- get column() {
195
- return underscore(this.property) + "_id";
196
- }
197
174
  set(value) {
198
175
  if (this.query?.tables.has(value))
199
176
  return value[this.reference.column];
@@ -209,8 +186,9 @@ function One(entity, arg2) {
209
186
  if (typeof arg2 === "boolean")
210
187
  arg2 = { nullable: arg2 };
211
188
  const opts = Object.assign({}, arg2);
189
+ const column = opts.column || underscore(entity.name) + "_id";
212
190
  const reference = entity.fields.get("id");
213
- return One.Type.new({ entity, reference, ...opts });
191
+ return One.Type.new({ entity, reference, column, ...opts });
214
192
  }
215
193
  One.Type = ForeignColumn;
216
194
 
@@ -331,9 +309,19 @@ var Builder = class {
331
309
  }
332
310
  };
333
311
  }
312
+ compare(field) {
313
+ const use = (op2) => (right, orEqual) => this.where(field, orEqual ? op2 + "=" : op2, right);
314
+ return {
315
+ is: use("="),
316
+ in: use("IN"),
317
+ not: use("<>"),
318
+ over: use(">"),
319
+ under: use("<")
320
+ };
321
+ }
334
322
  field(field) {
335
323
  return {
336
- ...field.where(),
324
+ ...this.compare(field),
337
325
  asc: () => {
338
326
  this.order.set(field, "asc");
339
327
  },
@@ -736,7 +724,7 @@ var Generator = class {
736
724
  else
737
725
  throw new Error(`Column ${field} is not nullable.`);
738
726
  } else
739
- value = field.raw(value);
727
+ value = escape(field.set(value));
740
728
  sets.push(`${this.escape(field.column)} = ${value}`);
741
729
  }
742
730
  if (!sets.length)
@@ -806,6 +794,7 @@ var Generator = class {
806
794
  return "?";
807
795
  }
808
796
  filter(left, op2, right) {
797
+ const raw = (value) => escape(left.set(value));
809
798
  if (right === null) {
810
799
  right === "NULL";
811
800
  if (op2 == "=")
@@ -815,11 +804,11 @@ var Generator = class {
815
804
  else
816
805
  throw new Error("Cannot compare NULL with " + op2);
817
806
  } else if (right instanceof Array)
818
- right = `(${right.map(left.raw, left)})`;
807
+ right = `(${right.map(raw)})`;
819
808
  else if (right instanceof Field || right instanceof Value)
820
809
  right = this.reference(right);
821
810
  else
822
- right = left.raw(right);
811
+ right = raw(right);
823
812
  return `${this.reference(left)} ${op2} ${right}`;
824
813
  }
825
814
  where() {
package/dist/index.mjs CHANGED
@@ -39,19 +39,17 @@ var Field = class {
39
39
  get datatype() {
40
40
  return this.type;
41
41
  }
42
- get column() {
43
- return underscore(this.property);
44
- }
45
- set column(value) {
46
- Object.defineProperty(this, "column", { value });
47
- }
48
42
  static new(options) {
43
+ if (typeof options === "boolean")
44
+ options = { nullable: options };
49
45
  return Object.assign(new this(), options);
50
46
  }
51
47
  create(property, parent) {
52
48
  const field = create(this);
53
49
  field.parent = parent;
54
50
  field.property = property;
51
+ if (!field.column)
52
+ field.column = underscore(property);
55
53
  return field;
56
54
  }
57
55
  /**
@@ -71,24 +69,6 @@ var Field = class {
71
69
  get(value) {
72
70
  return value;
73
71
  }
74
- raw(value) {
75
- return escape(this.set(value));
76
- }
77
- /**
78
- * This method is used to compare this field with another value in a query.
79
- *
80
- * @param acc Set of comparisons to be added to if not part of a group.
81
- */
82
- where() {
83
- const use = (op2) => (right, orEqual) => this.query.where(this, op2 + (orEqual ? "=" : ""), right);
84
- return {
85
- is: use("="),
86
- in: use("IN"),
87
- not: use("<>"),
88
- over: use(">"),
89
- under: use("<")
90
- };
91
- }
92
72
  };
93
73
  defineProperty(Field, "does", {
94
74
  value: (callback) => callback
@@ -141,7 +121,7 @@ var NumericColumn = class extends Field {
141
121
  return value;
142
122
  }
143
123
  };
144
- function Num(opts) {
124
+ function Num(opts = {}) {
145
125
  return Num.Type.new(opts);
146
126
  }
147
127
  Num.Type = NumericColumn;
@@ -152,9 +132,6 @@ var ForeignColumn = class extends Field {
152
132
  super(...arguments);
153
133
  this.nullable = false;
154
134
  }
155
- get column() {
156
- return underscore(this.property) + "_id";
157
- }
158
135
  set(value) {
159
136
  if (this.query?.tables.has(value))
160
137
  return value[this.reference.column];
@@ -170,8 +147,9 @@ function One(entity, arg2) {
170
147
  if (typeof arg2 === "boolean")
171
148
  arg2 = { nullable: arg2 };
172
149
  const opts = Object.assign({}, arg2);
150
+ const column = opts.column || underscore(entity.name) + "_id";
173
151
  const reference = entity.fields.get("id");
174
- return One.Type.new({ entity, reference, ...opts });
152
+ return One.Type.new({ entity, reference, column, ...opts });
175
153
  }
176
154
  One.Type = ForeignColumn;
177
155
 
@@ -292,9 +270,19 @@ var Builder = class {
292
270
  }
293
271
  };
294
272
  }
273
+ compare(field) {
274
+ const use = (op2) => (right, orEqual) => this.where(field, orEqual ? op2 + "=" : op2, right);
275
+ return {
276
+ is: use("="),
277
+ in: use("IN"),
278
+ not: use("<>"),
279
+ over: use(">"),
280
+ under: use("<")
281
+ };
282
+ }
295
283
  field(field) {
296
284
  return {
297
- ...field.where(),
285
+ ...this.compare(field),
298
286
  asc: () => {
299
287
  this.order.set(field, "asc");
300
288
  },
@@ -697,7 +685,7 @@ var Generator = class {
697
685
  else
698
686
  throw new Error(`Column ${field} is not nullable.`);
699
687
  } else
700
- value = field.raw(value);
688
+ value = escape(field.set(value));
701
689
  sets.push(`${this.escape(field.column)} = ${value}`);
702
690
  }
703
691
  if (!sets.length)
@@ -767,6 +755,7 @@ var Generator = class {
767
755
  return "?";
768
756
  }
769
757
  filter(left, op2, right) {
758
+ const raw = (value) => escape(left.set(value));
770
759
  if (right === null) {
771
760
  right === "NULL";
772
761
  if (op2 == "=")
@@ -776,11 +765,11 @@ var Generator = class {
776
765
  else
777
766
  throw new Error("Cannot compare NULL with " + op2);
778
767
  } else if (right instanceof Array)
779
- right = `(${right.map(left.raw, left)})`;
768
+ right = `(${right.map(raw)})`;
780
769
  else if (right instanceof Field || right instanceof Value)
781
770
  right = this.reference(right);
782
771
  else
783
- right = left.raw(right);
772
+ right = raw(right);
784
773
  return `${this.reference(left)} ${op2} ${right}`;
785
774
  }
786
775
  where() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expressive/sql",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Query builder and ORM",
5
5
  "license": "ISC",
6
6
  "publishConfig": {
@@ -24,6 +24,7 @@
24
24
  },
25
25
  "scripts": {
26
26
  "build": "tsup src/index.ts --dts --format esm,cjs",
27
+ "lint": "tsc --noEmit",
27
28
  "test": "vitest run"
28
29
  }
29
30
  }