rado 0.2.40 → 0.2.42

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.
@@ -170,7 +170,7 @@ export declare namespace Expr {
170
170
  const IsExpr: unique symbol;
171
171
  const ToExpr: unique symbol;
172
172
  const NULL: Expr<null>;
173
- function value<T>(value: T): Expr<T>;
173
+ function value<T>(value: T, inline?: boolean): Expr<T>;
174
174
  function get<T = any>(expr: ObjectExpr, field: string): Expr<T>;
175
175
  function create<T>(input: EV<T>): Expr<T>;
176
176
  function and(...conditions: Array<EV<boolean>>): Expr<boolean>;
@@ -392,8 +392,8 @@ class Expr {
392
392
  Expr2.IsExpr = Symbol("Expr.IsExpr");
393
393
  Expr2.ToExpr = Symbol("Expr.ToExpr");
394
394
  Expr2.NULL = create(null);
395
- function value(value2) {
396
- return new Expr2(new ExprData.Param(new ParamData.Value(value2)));
395
+ function value(value2, inline = false) {
396
+ return new Expr2(new ExprData.Param(new ParamData.Value(value2, inline)));
397
397
  }
398
398
  Expr2.value = value;
399
399
  function get(expr, field) {
@@ -5,8 +5,9 @@ export declare enum ParamType {
5
5
  export declare namespace ParamData {
6
6
  class Value {
7
7
  value: any;
8
+ inline: boolean;
8
9
  type: ParamType.Value;
9
- constructor(value: any);
10
+ constructor(value: any, inline?: boolean);
10
11
  }
11
12
  class Named {
12
13
  name: string;
@@ -6,8 +6,9 @@ var ParamType = /* @__PURE__ */ ((ParamType2) => {
6
6
  var ParamData;
7
7
  ((ParamData2) => {
8
8
  class Value {
9
- constructor(value) {
9
+ constructor(value, inline = false) {
10
10
  this.value = value;
11
+ this.inline = inline;
11
12
  }
12
13
  type = "ParamData.Value" /* Value */;
13
14
  }
@@ -1,6 +1,6 @@
1
1
  import { Query } from './Query.js';
2
2
  export declare function sql<T>(strings: TemplateStringsArray, ...params: Array<unknown>): Query<T>;
3
3
  export declare namespace sql {
4
- function all<T>(strings: TemplateStringsArray, ...params: Array<unknown>): Query<T>;
5
- function get<T>(strings: TemplateStringsArray, ...params: Array<unknown>): Query<T>;
4
+ function all<T = unknown>(strings: TemplateStringsArray, ...params: Array<unknown>): Query<Array<T>>;
5
+ function get<T = unknown>(strings: TemplateStringsArray, ...params: Array<unknown>): Query<T>;
6
6
  }
@@ -20,7 +20,6 @@ export interface FormatContext {
20
20
  forceInline?: boolean;
21
21
  formatAsJson?: boolean;
22
22
  formatAsInsert?: boolean;
23
- formatAsIn?: boolean;
24
23
  topLevel?: boolean;
25
24
  selectAsColumns?: boolean;
26
25
  formattingCte?: string;
@@ -43,7 +42,6 @@ export declare abstract class Formatter implements Sanitizer {
43
42
  forceInline?: boolean | undefined;
44
43
  formatAsJson?: boolean | undefined;
45
44
  formatAsInsert?: boolean | undefined;
46
- formatAsIn?: boolean | undefined;
47
45
  topLevel?: boolean | undefined;
48
46
  selectAsColumns?: boolean | undefined;
49
47
  formattingCte?: string | undefined;
@@ -77,7 +75,7 @@ export declare abstract class Formatter implements Sanitizer {
77
75
  formatSelection(ctx: FormatContext, selection: ExprData, formatSubject?: FormatSubject): Statement;
78
76
  formatString(ctx: FormatContext, input: string): Statement;
79
77
  formatInlineValue(ctx: FormatContext, rawValue: any): Statement;
80
- formatValue(ctx: FormatContext, rawValue: any): Statement;
78
+ formatValue(ctx: FormatContext, rawValue: any, inline?: boolean): Statement;
81
79
  formatExprJson(ctx: FormatContext, expr: ExprData): Statement;
82
80
  formatExprValue(ctx: FormatContext, expr: ExprData): Statement;
83
81
  formatExpr(ctx: FormatContext, expr: ExprData): Statement;
@@ -85,14 +83,24 @@ export declare abstract class Formatter implements Sanitizer {
85
83
  formatBinOp(ctx: FormatContext, expr: ExprData.BinOp): Statement;
86
84
  formatParam(ctx: FormatContext, expr: ExprData.Param): Statement;
87
85
  retrieveField(expr: ExprData, field: string): ExprData | undefined;
86
+ /**
87
+ * Format a in "{a}->>{b}"
88
+ */
89
+ formatExprAccess(ctx: FormatContext, expr: ExprData): Statement;
90
+ /**
91
+ * Format b in "{a} in {b}"
92
+ */
93
+ formatIn(ctx: FormatContext, expr: ExprData): Statement;
88
94
  formatFieldOf(ctx: FormatContext, from: ExprData, field: string): Statement;
89
- formatField({ formatAsIn, ...ctx }: FormatContext, expr: ExprData.Field): Statement;
95
+ formatField(ctx: FormatContext, expr: ExprData.Field): Statement;
90
96
  formatCall(ctx: FormatContext, expr: ExprData.Call): Statement;
91
97
  formatQuery(ctx: FormatContext, expr: ExprData.Query): Statement;
92
98
  formatRow(ctx: FormatContext, expr: ExprData.Row): Statement;
93
99
  formatMerge(ctx: FormatContext, expr: ExprData.Merge): Statement;
94
100
  formatRecord(ctx: FormatContext, expr: ExprData.Record): Statement;
95
- formatFilter({ formatAsIn, ...ctx }: FormatContext, expr: ExprData.Filter): Statement;
96
- formatMap({ formatAsIn, ...ctx }: FormatContext, expr: ExprData.Map): Statement;
101
+ formatFilterIn(ctx: FormatContext, expr: ExprData.Filter): Statement;
102
+ formatFilter(ctx: FormatContext, expr: ExprData.Filter): Statement;
103
+ formatMapIn(ctx: FormatContext, expr: ExprData.Map): Statement;
104
+ formatMap(ctx: FormatContext, expr: ExprData.Map): Statement;
97
105
  }
98
106
  export {};
@@ -502,8 +502,9 @@ class Formatter {
502
502
  return this.formatString(ctx, JSON.stringify(rawValue));
503
503
  }
504
504
  }
505
- formatValue(ctx, rawValue) {
505
+ formatValue(ctx, rawValue, inline = false) {
506
506
  const { stmt, formatAsJson, formatAsInsert, forceInline } = ctx;
507
+ const inlineValue = inline || forceInline;
507
508
  const asJson = formatAsJson || formatAsInsert;
508
509
  switch (true) {
509
510
  case (rawValue === null || rawValue === void 0):
@@ -519,7 +520,7 @@ class Formatter {
519
520
  stmt.closeParenthesis();
520
521
  return stmt;
521
522
  case (typeof rawValue === "string" || typeof rawValue === "number"):
522
- if (forceInline)
523
+ if (inlineValue)
523
524
  return stmt.raw(this.escapeValue(rawValue));
524
525
  return stmt.value(rawValue);
525
526
  default:
@@ -527,7 +528,7 @@ class Formatter {
527
528
  stmt.raw("json");
528
529
  stmt.openParenthesis();
529
530
  }
530
- if (forceInline)
531
+ if (inlineValue)
531
532
  this.formatString(ctx, JSON.stringify(rawValue));
532
533
  else
533
534
  stmt.value(JSON.stringify(rawValue));
@@ -584,14 +585,17 @@ class Formatter {
584
585
  stmt.openParenthesis();
585
586
  this.formatExprValue(ctx, expr.a).add(binOps[expr.op]).space();
586
587
  const asIn = expr.op === BinOpType.In || expr.op === BinOpType.NotIn;
587
- this.formatExprValue({ ...ctx, formatAsIn: asIn }, expr.b);
588
+ if (asIn)
589
+ this.formatIn(ctx, expr.b);
590
+ else
591
+ this.formatExprValue(ctx, expr.b);
588
592
  return stmt.closeParenthesis();
589
593
  }
590
594
  formatParam(ctx, expr) {
591
595
  const { stmt } = ctx;
592
596
  switch (expr.param.type) {
593
597
  case ParamType.Value:
594
- return this.formatValue(ctx, expr.param.value);
598
+ return this.formatValue(ctx, expr.param.value, expr.param.inline);
595
599
  case ParamType.Named:
596
600
  return stmt.param(expr.param);
597
601
  }
@@ -606,6 +610,51 @@ class Formatter {
606
610
  return void 0;
607
611
  }
608
612
  }
613
+ /**
614
+ * Format a in "{a}->>{b}"
615
+ */
616
+ formatExprAccess(ctx, expr) {
617
+ const { stmt } = ctx;
618
+ if (expr.type === ExprType.Field && expr.expr.type === ExprType.Row) {
619
+ const from = expr.expr;
620
+ switch (from.target.type) {
621
+ case TargetType.Table:
622
+ if (!ctx.skipTableName) {
623
+ stmt.identifier(from.target.table.alias || from.target.table.name).raw(".");
624
+ }
625
+ return stmt.identifier(expr.field);
626
+ }
627
+ }
628
+ return this.formatExpr(ctx, expr);
629
+ }
630
+ /**
631
+ * Format b in "{a} in {b}"
632
+ */
633
+ formatIn(ctx, expr) {
634
+ const { stmt } = ctx;
635
+ switch (expr.type) {
636
+ case ExprType.Filter:
637
+ return this.formatFilterIn(ctx, expr);
638
+ case ExprType.Map:
639
+ return this.formatMapIn(ctx, expr);
640
+ case ExprType.Field:
641
+ stmt.openParenthesis();
642
+ stmt.raw("SELECT value FROM json_each");
643
+ stmt.openParenthesis();
644
+ this.formatExprValue(ctx, expr);
645
+ stmt.closeParenthesis();
646
+ return stmt.closeParenthesis();
647
+ case ExprType.Param:
648
+ if (expr.param.type === ParamType.Value && Array.isArray(expr.param.value)) {
649
+ stmt.openParenthesis();
650
+ for (const v of stmt.separate(expr.param.value))
651
+ this.formatValue(ctx, v);
652
+ return stmt.closeParenthesis();
653
+ }
654
+ default:
655
+ return this.formatExpr(ctx, expr);
656
+ }
657
+ }
609
658
  formatFieldOf(ctx, from, field) {
610
659
  const { stmt } = ctx;
611
660
  const fieldExpr = this.retrieveField(from, field);
@@ -640,19 +689,14 @@ class Formatter {
640
689
  return stmt;
641
690
  }
642
691
  default:
643
- return this.formatAccess(ctx, () => this.formatExpr(ctx, from), field);
692
+ return this.formatAccess(
693
+ ctx,
694
+ () => this.formatExprAccess(ctx, from),
695
+ field
696
+ );
644
697
  }
645
698
  }
646
- formatField({ formatAsIn, ...ctx }, expr) {
647
- const { stmt } = ctx;
648
- if (formatAsIn) {
649
- stmt.openParenthesis();
650
- stmt.raw("SELECT value FROM json_each");
651
- stmt.openParenthesis();
652
- this.formatExprJson(ctx, expr);
653
- stmt.closeParenthesis();
654
- return stmt.closeParenthesis();
655
- }
699
+ formatField(ctx, expr) {
656
700
  return this.formatFieldOf(ctx, expr.expr, expr.field);
657
701
  }
658
702
  formatCall(ctx, expr) {
@@ -678,7 +722,7 @@ class Formatter {
678
722
  const { stmt } = ctx;
679
723
  if (!ctx.formatAsJson) {
680
724
  stmt.openParenthesis();
681
- this.format({ ...ctx, formatAsIn: false }, expr.query);
725
+ this.format(ctx, expr.query);
682
726
  return stmt.closeParenthesis();
683
727
  }
684
728
  if (expr.query.singleResult) {
@@ -747,15 +791,12 @@ class Formatter {
747
791
  }
748
792
  return stmt.closeParenthesis();
749
793
  }
750
- formatFilter({ formatAsIn, ...ctx }, expr) {
794
+ formatFilterIn(ctx, expr) {
751
795
  const { stmt } = ctx;
752
796
  const { target, condition } = expr;
753
797
  switch (target.type) {
754
798
  case TargetType.Expr:
755
799
  stmt.openParenthesis();
756
- if (!formatAsIn) {
757
- stmt.raw("SELECT json_group_array(json(result)) FROM ").openParenthesis();
758
- }
759
800
  stmt.raw("SELECT value AS result").add("FROM json_each").openParenthesis();
760
801
  this.formatExpr(ctx, target.expr);
761
802
  stmt.closeParenthesis();
@@ -763,37 +804,52 @@ class Formatter {
763
804
  stmt.add("AS").addIdentifier(target.alias);
764
805
  stmt.add("WHERE").space();
765
806
  this.formatExprValue(ctx, condition);
766
- if (!formatAsIn) {
767
- stmt.closeParenthesis();
768
- }
769
807
  return stmt.closeParenthesis();
770
808
  default:
771
809
  throw new Error("todo");
772
810
  }
773
811
  }
774
- formatMap({ formatAsIn, ...ctx }, expr) {
812
+ formatFilter(ctx, expr) {
813
+ const { stmt } = ctx;
814
+ switch (expr.target.type) {
815
+ case TargetType.Expr:
816
+ stmt.openParenthesis().raw("SELECT json_group_array(json(result)) FROM ");
817
+ this.formatFilterIn(ctx, expr);
818
+ stmt.closeParenthesis();
819
+ return stmt;
820
+ default:
821
+ throw new Error("todo");
822
+ }
823
+ }
824
+ formatMapIn(ctx, expr) {
775
825
  const { stmt } = ctx;
776
826
  const { target, result } = expr;
777
827
  switch (target.type) {
778
828
  case TargetType.Expr:
779
829
  stmt.openParenthesis();
780
- if (!formatAsIn) {
781
- stmt.raw("SELECT json_group_array(json(result)) FROM ").openParenthesis();
782
- }
783
830
  stmt.raw("SELECT").space();
784
831
  this.formatExpr(ctx, result).add("AS result").add("FROM json_each").openParenthesis();
785
832
  this.formatExprJson(ctx, target.expr);
786
833
  stmt.closeParenthesis();
787
834
  if (target.alias)
788
835
  stmt.add("AS").addIdentifier(target.alias);
789
- if (!formatAsIn) {
790
- stmt.closeParenthesis();
791
- }
792
836
  return stmt.closeParenthesis();
793
837
  default:
794
838
  throw new Error("todo");
795
839
  }
796
840
  }
841
+ formatMap(ctx, expr) {
842
+ const { stmt } = ctx;
843
+ switch (expr.target.type) {
844
+ case TargetType.Expr:
845
+ stmt.openParenthesis().raw("SELECT json_group_array(json(result)) FROM ");
846
+ this.formatMapIn(ctx, expr);
847
+ stmt.closeParenthesis();
848
+ return stmt;
849
+ default:
850
+ throw new Error("todo");
851
+ }
852
+ }
797
853
  }
798
854
  export {
799
855
  Formatter
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rado",
3
- "version": "0.2.40",
3
+ "version": "0.2.42",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",