rado 0.1.43 → 0.1.44

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.
@@ -10,7 +10,7 @@ interface PartialColumnData {
10
10
  type: ColumnType;
11
11
  name?: string;
12
12
  nullable?: boolean;
13
- defaultValue?: ExprData;
13
+ defaultValue?: ExprData | (() => ExprData);
14
14
  autoIncrement?: boolean;
15
15
  primaryKey?: boolean;
16
16
  unique?: boolean;
@@ -29,6 +29,7 @@ export declare class Column<T> {
29
29
  primaryKey<K extends string>(): Column<Column.IsPrimary<T, K>>;
30
30
  references<X extends T>(column: Expr<X> | (() => Expr<X>)): Column<X>;
31
31
  unique(): Column<T>;
32
+ defaultValue(create: () => EV<T>): Column<Column.IsOptional<T>>;
32
33
  defaultValue(value: EV<T>): Column<Column.IsOptional<T>>;
33
34
  }
34
35
  export type PrimaryKey<T, K> = string extends K ? T : T & {
@@ -36,7 +36,10 @@ var Column = class {
36
36
  return new Column({ ...this.data, unique: true });
37
37
  }
38
38
  defaultValue(value) {
39
- return new Column({ ...this.data, defaultValue: ExprData.create(value) });
39
+ return new Column({
40
+ ...this.data,
41
+ defaultValue: typeof value === "function" ? () => ExprData.create(value()) : ExprData.create(value)
42
+ });
40
43
  }
41
44
  };
42
45
  ((Column2) => {
@@ -229,17 +229,19 @@ var Formatter = class {
229
229
  if (!column.nullable)
230
230
  stmt.add("NOT NULL");
231
231
  if (column.defaultValue !== void 0) {
232
- stmt.add("DEFAULT").space();
233
- stmt.openParenthesis();
234
- this.formatExpr(
235
- {
236
- ...ctx,
237
- formatAsJson: true,
238
- forceInline: true
239
- },
240
- column.defaultValue
241
- );
242
- stmt.closeParenthesis();
232
+ if (typeof column.defaultValue !== "function") {
233
+ stmt.add("DEFAULT").space();
234
+ stmt.openParenthesis();
235
+ this.formatExpr(
236
+ {
237
+ ...ctx,
238
+ formatAsJson: true,
239
+ forceInline: true
240
+ },
241
+ column.defaultValue
242
+ );
243
+ stmt.closeParenthesis();
244
+ }
243
245
  }
244
246
  if (column.references)
245
247
  this.formatContraintReference(ctx, column.references());
@@ -282,8 +284,11 @@ var Formatter = class {
282
284
  const isNull = columnValue === void 0 || columnValue === null;
283
285
  const isOptional = column.nullable || column.autoIncrement || column.primaryKey;
284
286
  if (isNull) {
285
- if (column.defaultValue !== void 0)
287
+ if (column.defaultValue !== void 0) {
288
+ if (typeof column.defaultValue === "function")
289
+ return this.formatExprJson(ctx, column.defaultValue());
286
290
  return this.formatExprJson(ctx, column.defaultValue);
291
+ }
287
292
  if (!isOptional)
288
293
  throw new TypeError(`Expected value for column ${column.name}`);
289
294
  return stmt.raw("NULL");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rado",
3
- "version": "0.1.43",
3
+ "version": "0.1.44",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {