rado 0.1.17 → 0.1.19

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.
@@ -1,4 +1,4 @@
1
- import { Expr, ExprData } from './Expr';
1
+ import { EV, Expr, ExprData } from './Expr';
2
2
  export declare enum ColumnType {
3
3
  String = "String",
4
4
  Integer = "Integer",
@@ -10,7 +10,7 @@ interface PartialColumnData {
10
10
  type: ColumnType;
11
11
  name?: string;
12
12
  nullable?: boolean;
13
- defaultValue?: any;
13
+ defaultValue?: ExprData;
14
14
  autoIncrement?: boolean;
15
15
  primaryKey?: boolean;
16
16
  unique?: boolean;
@@ -29,7 +29,7 @@ export declare class Column<T> {
29
29
  primaryKey<K extends string>(): Column<Column.IsPrimary<T, K>>;
30
30
  references(column: Expr<T>): Column<T>;
31
31
  unique(): Column<T>;
32
- defaultValue(value: T): Column<Column.IsOptional<T>>;
32
+ defaultValue(value: EV<T>): Column<Column.IsOptional<T>>;
33
33
  }
34
34
  export type PrimaryKey<T, K> = string extends K ? T : T & {
35
35
  [Column.isPrimary]: K;
@@ -31,7 +31,7 @@ var Column = class {
31
31
  return new Column({ ...this.data, unique: true });
32
32
  }
33
33
  defaultValue(value) {
34
- return new Column({ ...this.data, defaultValue: value });
34
+ return new Column({ ...this.data, defaultValue: ExprData.create(value) });
35
35
  }
36
36
  };
37
37
  ((Column2) => {
@@ -167,8 +167,13 @@ var Formatter = class {
167
167
  }
168
168
  formatColumn(column) {
169
169
  return identifier(column.name).add(this.formatType(column.type)).addIf(column.unique, "UNIQUE").addIf(column.autoIncrement, "AUTOINCREMENT").addIf(column.primaryKey, "PRIMARY KEY").addIf(!column.nullable, "NOT NULL").addIf(
170
- column.defaultValue !== void 0,
171
- raw("DEFAULT").add(this.formatInlineValue(column.defaultValue))
170
+ column.defaultValue,
171
+ () => raw("DEFAULT").addParenthesis(
172
+ this.formatExpr(column.defaultValue, {
173
+ formatAsJson: false,
174
+ forceInline: true
175
+ })
176
+ )
172
177
  ).addIf(column.references, () => {
173
178
  return this.formatContraintReference(column.references);
174
179
  });
@@ -205,8 +210,10 @@ var Formatter = class {
205
210
  }
206
211
  formatColumnValue(column, columnValue) {
207
212
  const isNull = columnValue === void 0 || columnValue === null;
208
- const isOptional = column.nullable || column.autoIncrement || column.primaryKey || column.defaultValue !== void 0;
213
+ const isOptional = column.nullable || column.autoIncrement || column.primaryKey;
209
214
  if (isNull) {
215
+ if (column.defaultValue !== void 0)
216
+ return this.formatExpr(column.defaultValue, {});
210
217
  if (!isOptional)
211
218
  throw new TypeError(`Expected value for column ${column.name}`);
212
219
  return raw("NULL");
@@ -324,14 +331,15 @@ var Formatter = class {
324
331
  const column = expr.target.table.columns[field];
325
332
  switch (column?.type) {
326
333
  case ColumnType.Json:
327
- return call("json", selection);
334
+ if (ctx.formatAsJson)
335
+ return call("json", selection);
328
336
  default:
329
337
  return selection;
330
338
  }
331
339
  }
332
340
  default:
333
341
  return this.formatAccess(
334
- this.formatExpr(expr, { ...ctx, formatAsJson: true }),
342
+ this.formatExpr(expr, ctx),
335
343
  field,
336
344
  ctx.formatAsJson
337
345
  );
@@ -10,6 +10,9 @@ var Schema;
10
10
  return Query.Batch({ queries });
11
11
  }
12
12
  Schema2.create = create;
13
+ function removeLeadingWhitespace(str) {
14
+ return str.replace(/\n\s+/g, "\n");
15
+ }
13
16
  function upgrade(formatter, local, schema) {
14
17
  const columnNames = /* @__PURE__ */ new Set([
15
18
  ...Object.keys(local.columns),
@@ -25,7 +28,7 @@ var Schema;
25
28
  res.push(Query.AlterTable({ table: schema, dropColumn: columnName }));
26
29
  } else {
27
30
  const [instruction] = formatter.formatColumn({ ...schemaCol, references: void 0 }).compile(formatter);
28
- if (localInstruction !== instruction) {
31
+ if (removeLeadingWhitespace(localInstruction) !== removeLeadingWhitespace(instruction)) {
29
32
  res.push(Query.AlterTable({ table: schema, alterColumn: schemaCol }));
30
33
  }
31
34
  }
@@ -45,7 +48,7 @@ var Schema;
45
48
  const [instruction] = formatter.formatCreateIndex(
46
49
  Query.CreateIndex({ table: schema, index: schemaIndex })
47
50
  ).compile(formatter);
48
- if (localInstruction !== instruction) {
51
+ if (removeLeadingWhitespace(localInstruction) !== removeLeadingWhitespace(instruction)) {
49
52
  res.push(Query.DropIndex({ table: schema, name: indexName }));
50
53
  res.push(Query.CreateIndex({ table: schema, index: schemaIndex }));
51
54
  }
@@ -16,6 +16,10 @@ declare class Token {
16
16
  static Identifier(data: string): Token;
17
17
  static Value(data: any): Token;
18
18
  }
19
+ export interface CompileOptions {
20
+ formatInline?: boolean;
21
+ spaces?: number;
22
+ }
19
23
  export declare class Statement {
20
24
  tokens: Array<Token>;
21
25
  constructor(tokens: Array<Token>);
@@ -30,7 +34,7 @@ export declare class Statement {
30
34
  addIf(condition: any, addition: string | Statement | (() => string | Statement)): Statement;
31
35
  indent(): Statement;
32
36
  dedent(): Statement;
33
- newline(): Statement;
37
+ newline(ignore?: boolean): Statement;
34
38
  identifier(name: string): Statement;
35
39
  addIdentifier(name: string): Statement;
36
40
  value(value: any): Statement;
@@ -76,7 +76,9 @@ var Statement = class {
76
76
  dedent() {
77
77
  return this.concat(Token.Dedent());
78
78
  }
79
- newline() {
79
+ newline(ignore = false) {
80
+ if (ignore)
81
+ return this;
80
82
  return this.raw(NEWLINE);
81
83
  }
82
84
  identifier(name) {
@@ -28,7 +28,7 @@ var SqliteSchema;
28
28
  column.name,
29
29
  identifier(column.name).add(column.type).addIf(column.pk === 1, "PRIMARY KEY").addIf(column.notnull === 1, "NOT NULL").addIf(
30
30
  column.dflt_value !== null,
31
- raw("DEFAULT").add(column.dflt_value)
31
+ raw("DEFAULT").addParenthesis(column.dflt_value)
32
32
  ).compile(formatter)[0]
33
33
  ];
34
34
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rado",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {