rado 0.1.42 → 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.
- package/dist/lib/Column.d.ts +3 -2
- package/dist/lib/Column.js +5 -2
- package/dist/lib/Formatter.js +18 -13
- package/package.json +1 -1
package/dist/lib/Column.d.ts
CHANGED
|
@@ -10,11 +10,11 @@ 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;
|
|
17
|
-
references?: ExprData;
|
|
17
|
+
references?: () => ExprData;
|
|
18
18
|
}
|
|
19
19
|
export interface ColumnData extends PartialColumnData {
|
|
20
20
|
type: ColumnType;
|
|
@@ -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 & {
|
package/dist/lib/Column.js
CHANGED
|
@@ -27,7 +27,7 @@ var Column = class {
|
|
|
27
27
|
references(column2) {
|
|
28
28
|
return new Column({
|
|
29
29
|
...this.data,
|
|
30
|
-
|
|
30
|
+
references() {
|
|
31
31
|
return ExprData.create(typeof column2 === "function" ? column2() : column2);
|
|
32
32
|
}
|
|
33
33
|
});
|
|
@@ -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({
|
|
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) => {
|
package/dist/lib/Formatter.js
CHANGED
|
@@ -229,20 +229,22 @@ var Formatter = class {
|
|
|
229
229
|
if (!column.nullable)
|
|
230
230
|
stmt.add("NOT NULL");
|
|
231
231
|
if (column.defaultValue !== void 0) {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
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
|
-
this.formatContraintReference(ctx, column.references);
|
|
247
|
+
this.formatContraintReference(ctx, column.references());
|
|
246
248
|
return stmt;
|
|
247
249
|
}
|
|
248
250
|
formatContraintReference(ctx, reference) {
|
|
@@ -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");
|