rado 0.1.56 → 0.1.58

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.
@@ -19,7 +19,7 @@ export interface FormatContext {
19
19
  /** Inline all parameters */
20
20
  forceInline?: boolean;
21
21
  formatAsJson?: boolean;
22
- formatAsDefault?: boolean;
22
+ formatAsInsert?: boolean;
23
23
  topLevel?: boolean;
24
24
  }
25
25
  type FormatSubject = (stmt: Statement, mkSubject: () => void) => void;
@@ -37,7 +37,7 @@ export declare abstract class Formatter implements Sanitizer {
37
37
  tableAsExpr?: boolean | undefined;
38
38
  forceInline?: boolean | undefined;
39
39
  formatAsJson?: boolean | undefined;
40
- formatAsDefault?: boolean | undefined;
40
+ formatAsInsert?: boolean | undefined;
41
41
  topLevel?: boolean | undefined;
42
42
  skipNewlines?: boolean | undefined;
43
43
  };
@@ -117,12 +117,15 @@ var Formatter = class {
117
117
  const data = query.set || {};
118
118
  stmt.add("UPDATE").addIdentifier(query.table.name).add("SET").space();
119
119
  for (const key of stmt.separate(Object.keys(data))) {
120
+ const column = query.table.columns[key];
121
+ if (!column)
122
+ continue;
120
123
  stmt.identifier(key).add("=").space();
121
124
  const value = data[key];
122
125
  if (value instanceof Expr)
123
126
  this.formatExprJson(ctx, ExprData.create(data[key]));
124
127
  else
125
- this.formatValue({ ...ctx, formatAsJson: true }, value);
128
+ this.formatValue({ ...ctx, formatAsInsert: true }, value);
126
129
  }
127
130
  this.formatWhere(ctx, query.where);
128
131
  this.formatLimit(ctx, query);
@@ -241,7 +244,7 @@ var Formatter = class {
241
244
  this.formatExpr(
242
245
  {
243
246
  ...ctx,
244
- formatAsDefault: true,
247
+ formatAsInsert: true,
245
248
  forceInline: true
246
249
  },
247
250
  column.defaultValue
@@ -293,11 +296,11 @@ var Formatter = class {
293
296
  if (column.defaultValue !== void 0) {
294
297
  if (typeof column.defaultValue === "function")
295
298
  return this.formatExprJson(
296
- { ...ctx, formatAsDefault: true },
299
+ { ...ctx, formatAsInsert: true },
297
300
  column.defaultValue()
298
301
  );
299
302
  return this.formatExprJson(
300
- { ...ctx, formatAsDefault: true },
303
+ { ...ctx, formatAsInsert: true },
301
304
  column.defaultValue
302
305
  );
303
306
  }
@@ -454,15 +457,20 @@ var Formatter = class {
454
457
  switch (expr.target.type) {
455
458
  case TargetType.Table:
456
459
  const column = expr.target.table.columns[field];
460
+ const asBoolean = column?.type === ColumnType.Boolean && ctx.formatAsJson;
457
461
  const asJson = column?.type === ColumnType.Json && ctx.formatAsJson;
458
462
  if (asJson) {
459
463
  stmt.add("json");
460
464
  stmt.openParenthesis();
461
465
  }
466
+ if (asBoolean)
467
+ stmt.add(`json(iif(`);
462
468
  if (!ctx.skipTableName) {
463
469
  stmt.identifier(expr.target.table.alias || expr.target.table.name).raw(".");
464
470
  }
465
471
  stmt.identifier(field);
472
+ if (asBoolean)
473
+ stmt.add(`, 'true', 'false'))`);
466
474
  if (asJson)
467
475
  stmt.closeParenthesis();
468
476
  return stmt;
@@ -489,14 +497,14 @@ var Formatter = class {
489
497
  }
490
498
  }
491
499
  formatValue(ctx, rawValue) {
492
- const { stmt, formatAsJson, formatAsDefault, forceInline } = ctx;
500
+ const { stmt, formatAsJson, formatAsInsert, forceInline } = ctx;
501
+ const asJson = formatAsJson || formatAsInsert;
493
502
  switch (true) {
494
503
  case (rawValue === null || rawValue === void 0):
495
504
  return stmt.raw("NULL");
496
- case ((formatAsDefault || !formatAsJson) && typeof rawValue === "boolean"):
505
+ case ((formatAsInsert || !formatAsJson) && typeof rawValue === "boolean"):
497
506
  return rawValue ? stmt.raw("1") : stmt.raw("0");
498
- case Array.isArray(rawValue):
499
- const asJson = formatAsJson || formatAsDefault;
507
+ case (!formatAsInsert && Array.isArray(rawValue)):
500
508
  if (asJson)
501
509
  stmt.raw("json_array");
502
510
  stmt.openParenthesis();
@@ -513,7 +521,10 @@ var Formatter = class {
513
521
  stmt.raw("json");
514
522
  stmt.openParenthesis();
515
523
  }
516
- this.formatString(ctx, JSON.stringify(rawValue));
524
+ if (forceInline)
525
+ this.formatString(ctx, JSON.stringify(rawValue));
526
+ else
527
+ stmt.value(JSON.stringify(rawValue));
517
528
  if (formatAsJson)
518
529
  stmt.closeParenthesis();
519
530
  return stmt;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rado",
3
- "version": "0.1.56",
3
+ "version": "0.1.58",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",