kysely-gen 0.7.0 → 0.9.0
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/cli.js +110 -94
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -110411,8 +110411,8 @@ function toCamelCase(str) {
|
|
|
110411
110411
|
}
|
|
110412
110412
|
|
|
110413
110413
|
// src/transform/table.ts
|
|
110414
|
-
function transformTable(table, enums, enumResolver, mapType, options, unknownTypes) {
|
|
110415
|
-
const properties = table.columns.map((column) => transformColumn(column, enums, enumResolver, mapType, options, unknownTypes));
|
|
110414
|
+
function transformTable(table, enums, enumResolver, mapType, options, unknownTypes, usedHelpers) {
|
|
110415
|
+
const properties = table.columns.map((column) => transformColumn(column, enums, enumResolver, mapType, options, unknownTypes, usedHelpers));
|
|
110416
110416
|
return {
|
|
110417
110417
|
kind: "interface",
|
|
110418
110418
|
name: toPascalCase(singularize(table.name)),
|
|
@@ -110420,7 +110420,7 @@ function transformTable(table, enums, enumResolver, mapType, options, unknownTyp
|
|
|
110420
110420
|
exported: true
|
|
110421
110421
|
};
|
|
110422
110422
|
}
|
|
110423
|
-
function transformColumn(column, enums, enumResolver, mapType, options, unknownTypes) {
|
|
110423
|
+
function transformColumn(column, enums, enumResolver, mapType, options, unknownTypes, usedHelpers) {
|
|
110424
110424
|
const matchingEnum = enums.find((e) => e.name === column.dataType && e.schema === (column.dataTypeSchema ?? "public"));
|
|
110425
110425
|
let type;
|
|
110426
110426
|
if (matchingEnum) {
|
|
@@ -110436,10 +110436,11 @@ function transformColumn(column, enums, enumResolver, mapType, options, unknownT
|
|
|
110436
110436
|
type = mapType(column.dataType, {
|
|
110437
110437
|
isNullable: column.isNullable,
|
|
110438
110438
|
isArray: column.isArray,
|
|
110439
|
-
unknownTypes
|
|
110439
|
+
unknownTypes,
|
|
110440
|
+
usedHelpers
|
|
110440
110441
|
});
|
|
110441
110442
|
}
|
|
110442
|
-
if (column.isAutoIncrement) {
|
|
110443
|
+
if (column.isAutoIncrement || column.hasDefaultValue) {
|
|
110443
110444
|
type = {
|
|
110444
110445
|
kind: "generic",
|
|
110445
110446
|
name: "Generated",
|
|
@@ -110773,25 +110774,15 @@ function parsePostgresArray(value) {
|
|
|
110773
110774
|
}
|
|
110774
110775
|
|
|
110775
110776
|
// src/dialects/postgres/type-mapper.ts
|
|
110776
|
-
function
|
|
110777
|
-
|
|
110778
|
-
|
|
110779
|
-
typeArguments.push(insertType);
|
|
110780
|
-
if (updateType) {
|
|
110781
|
-
typeArguments.push(updateType);
|
|
110782
|
-
}
|
|
110783
|
-
}
|
|
110784
|
-
return {
|
|
110785
|
-
kind: "generic",
|
|
110786
|
-
name: "ColumnType",
|
|
110787
|
-
typeArguments
|
|
110788
|
-
};
|
|
110777
|
+
function helper(name, options) {
|
|
110778
|
+
options.usedHelpers?.add(name);
|
|
110779
|
+
return { kind: "reference", name };
|
|
110789
110780
|
}
|
|
110790
110781
|
function mapPostgresType(dataType, options) {
|
|
110791
110782
|
const { isNullable, isArray, unknownTypes } = options;
|
|
110792
110783
|
if (isArray || dataType.endsWith("[]")) {
|
|
110793
110784
|
const baseTypeName = dataType.endsWith("[]") ? dataType.slice(0, -2) : dataType;
|
|
110794
|
-
const elementType = mapPostgresType(baseTypeName, { isNullable: false, isArray: false, unknownTypes });
|
|
110785
|
+
const elementType = mapPostgresType(baseTypeName, { isNullable: false, isArray: false, unknownTypes, usedHelpers: options.usedHelpers });
|
|
110795
110786
|
const isSimple = elementType.kind === "primitive" && ["boolean", "number", "string"].includes(elementType.value);
|
|
110796
110787
|
let arrayType;
|
|
110797
110788
|
if (isSimple) {
|
|
@@ -110817,21 +110808,7 @@ function mapPostgresType(dataType, options) {
|
|
|
110817
110808
|
break;
|
|
110818
110809
|
case "int8":
|
|
110819
110810
|
case "bigint":
|
|
110820
|
-
baseType =
|
|
110821
|
-
kind: "union",
|
|
110822
|
-
types: [
|
|
110823
|
-
{ kind: "primitive", value: "string" },
|
|
110824
|
-
{ kind: "primitive", value: "number" },
|
|
110825
|
-
{ kind: "primitive", value: "bigint" }
|
|
110826
|
-
]
|
|
110827
|
-
}, {
|
|
110828
|
-
kind: "union",
|
|
110829
|
-
types: [
|
|
110830
|
-
{ kind: "primitive", value: "string" },
|
|
110831
|
-
{ kind: "primitive", value: "number" },
|
|
110832
|
-
{ kind: "primitive", value: "bigint" }
|
|
110833
|
-
]
|
|
110834
|
-
});
|
|
110811
|
+
baseType = helper("Int8", options);
|
|
110835
110812
|
break;
|
|
110836
110813
|
case "float4":
|
|
110837
110814
|
case "float8":
|
|
@@ -110841,19 +110818,7 @@ function mapPostgresType(dataType, options) {
|
|
|
110841
110818
|
break;
|
|
110842
110819
|
case "numeric":
|
|
110843
110820
|
case "decimal":
|
|
110844
|
-
baseType =
|
|
110845
|
-
kind: "union",
|
|
110846
|
-
types: [
|
|
110847
|
-
{ kind: "primitive", value: "number" },
|
|
110848
|
-
{ kind: "primitive", value: "string" }
|
|
110849
|
-
]
|
|
110850
|
-
}, {
|
|
110851
|
-
kind: "union",
|
|
110852
|
-
types: [
|
|
110853
|
-
{ kind: "primitive", value: "number" },
|
|
110854
|
-
{ kind: "primitive", value: "string" }
|
|
110855
|
-
]
|
|
110856
|
-
});
|
|
110821
|
+
baseType = helper("Numeric", options);
|
|
110857
110822
|
break;
|
|
110858
110823
|
case "varchar":
|
|
110859
110824
|
case "char":
|
|
@@ -110869,38 +110834,14 @@ function mapPostgresType(dataType, options) {
|
|
|
110869
110834
|
case "timestamp":
|
|
110870
110835
|
case "timestamptz":
|
|
110871
110836
|
case "date":
|
|
110872
|
-
baseType =
|
|
110873
|
-
kind: "union",
|
|
110874
|
-
types: [
|
|
110875
|
-
{ kind: "primitive", value: "Date" },
|
|
110876
|
-
{ kind: "primitive", value: "string" }
|
|
110877
|
-
]
|
|
110878
|
-
}, {
|
|
110879
|
-
kind: "union",
|
|
110880
|
-
types: [
|
|
110881
|
-
{ kind: "primitive", value: "Date" },
|
|
110882
|
-
{ kind: "primitive", value: "string" }
|
|
110883
|
-
]
|
|
110884
|
-
});
|
|
110837
|
+
baseType = helper("Timestamp", options);
|
|
110885
110838
|
break;
|
|
110886
110839
|
case "time":
|
|
110887
110840
|
case "timetz":
|
|
110888
110841
|
baseType = { kind: "primitive", value: "string" };
|
|
110889
110842
|
break;
|
|
110890
110843
|
case "interval":
|
|
110891
|
-
baseType =
|
|
110892
|
-
kind: "union",
|
|
110893
|
-
types: [
|
|
110894
|
-
{ kind: "reference", name: "IPostgresInterval" },
|
|
110895
|
-
{ kind: "primitive", value: "string" }
|
|
110896
|
-
]
|
|
110897
|
-
}, {
|
|
110898
|
-
kind: "union",
|
|
110899
|
-
types: [
|
|
110900
|
-
{ kind: "reference", name: "IPostgresInterval" },
|
|
110901
|
-
{ kind: "primitive", value: "string" }
|
|
110902
|
-
]
|
|
110903
|
-
});
|
|
110844
|
+
baseType = helper("Interval", options);
|
|
110904
110845
|
break;
|
|
110905
110846
|
case "money":
|
|
110906
110847
|
baseType = { kind: "primitive", value: "string" };
|
|
@@ -110912,6 +110853,12 @@ function mapPostgresType(dataType, options) {
|
|
|
110912
110853
|
case "bytea":
|
|
110913
110854
|
baseType = { kind: "primitive", value: "Buffer" };
|
|
110914
110855
|
break;
|
|
110856
|
+
case "point":
|
|
110857
|
+
baseType = helper("Point", options);
|
|
110858
|
+
break;
|
|
110859
|
+
case "circle":
|
|
110860
|
+
baseType = helper("Circle", options);
|
|
110861
|
+
break;
|
|
110915
110862
|
case "int4range":
|
|
110916
110863
|
case "int8range":
|
|
110917
110864
|
case "numrange":
|
|
@@ -111119,7 +111066,7 @@ function extractEnums(tables) {
|
|
|
111119
111066
|
}
|
|
111120
111067
|
|
|
111121
111068
|
// src/dialects/mysql/type-mapper.ts
|
|
111122
|
-
function
|
|
111069
|
+
function createColumnType(selectType, insertType, updateType) {
|
|
111123
111070
|
const typeArguments = [selectType];
|
|
111124
111071
|
if (insertType) {
|
|
111125
111072
|
typeArguments.push(insertType);
|
|
@@ -111147,7 +111094,7 @@ function mapMysqlType(dataType, options) {
|
|
|
111147
111094
|
baseType = { kind: "primitive", value: "number" };
|
|
111148
111095
|
break;
|
|
111149
111096
|
case "bigint":
|
|
111150
|
-
baseType =
|
|
111097
|
+
baseType = createColumnType({ kind: "primitive", value: "string" }, {
|
|
111151
111098
|
kind: "union",
|
|
111152
111099
|
types: [
|
|
111153
111100
|
{ kind: "primitive", value: "string" },
|
|
@@ -111170,7 +111117,7 @@ function mapMysqlType(dataType, options) {
|
|
|
111170
111117
|
break;
|
|
111171
111118
|
case "decimal":
|
|
111172
111119
|
case "numeric":
|
|
111173
|
-
baseType =
|
|
111120
|
+
baseType = createColumnType({ kind: "primitive", value: "string" }, {
|
|
111174
111121
|
kind: "union",
|
|
111175
111122
|
types: [
|
|
111176
111123
|
{ kind: "primitive", value: "number" },
|
|
@@ -111206,7 +111153,7 @@ function mapMysqlType(dataType, options) {
|
|
|
111206
111153
|
case "date":
|
|
111207
111154
|
case "datetime":
|
|
111208
111155
|
case "timestamp":
|
|
111209
|
-
baseType =
|
|
111156
|
+
baseType = createColumnType({ kind: "primitive", value: "Date" }, {
|
|
111210
111157
|
kind: "union",
|
|
111211
111158
|
types: [
|
|
111212
111159
|
{ kind: "primitive", value: "Date" },
|
|
@@ -111668,10 +111615,71 @@ function getDialect(name) {
|
|
|
111668
111615
|
}
|
|
111669
111616
|
}
|
|
111670
111617
|
|
|
111618
|
+
// src/dialects/postgres/helpers.ts
|
|
111619
|
+
function createColumnType2(selectType, insertType, updateType) {
|
|
111620
|
+
return {
|
|
111621
|
+
kind: "generic",
|
|
111622
|
+
name: "ColumnType",
|
|
111623
|
+
typeArguments: [selectType, insertType, updateType]
|
|
111624
|
+
};
|
|
111625
|
+
}
|
|
111626
|
+
function createUnion(...types2) {
|
|
111627
|
+
return { kind: "union", types: types2 };
|
|
111628
|
+
}
|
|
111629
|
+
function primitive(value) {
|
|
111630
|
+
if (value === "Date") {
|
|
111631
|
+
return { kind: "reference", name: "Date" };
|
|
111632
|
+
}
|
|
111633
|
+
return { kind: "primitive", value };
|
|
111634
|
+
}
|
|
111635
|
+
function ref(name) {
|
|
111636
|
+
return { kind: "reference", name };
|
|
111637
|
+
}
|
|
111638
|
+
var HELPER_DEFINITIONS = {
|
|
111639
|
+
Timestamp: {
|
|
111640
|
+
kind: "typeAlias",
|
|
111641
|
+
name: "Timestamp",
|
|
111642
|
+
exported: true,
|
|
111643
|
+
type: createColumnType2(primitive("Date"), createUnion(primitive("Date"), primitive("string")), createUnion(primitive("Date"), primitive("string")))
|
|
111644
|
+
},
|
|
111645
|
+
Int8: {
|
|
111646
|
+
kind: "typeAlias",
|
|
111647
|
+
name: "Int8",
|
|
111648
|
+
exported: true,
|
|
111649
|
+
type: createColumnType2(primitive("string"), createUnion(primitive("string"), primitive("number"), primitive("bigint")), createUnion(primitive("string"), primitive("number"), primitive("bigint")))
|
|
111650
|
+
},
|
|
111651
|
+
Numeric: {
|
|
111652
|
+
kind: "typeAlias",
|
|
111653
|
+
name: "Numeric",
|
|
111654
|
+
exported: true,
|
|
111655
|
+
type: createColumnType2(primitive("string"), createUnion(primitive("number"), primitive("string")), createUnion(primitive("number"), primitive("string")))
|
|
111656
|
+
},
|
|
111657
|
+
Interval: {
|
|
111658
|
+
kind: "typeAlias",
|
|
111659
|
+
name: "Interval",
|
|
111660
|
+
exported: true,
|
|
111661
|
+
type: createColumnType2(ref("IPostgresInterval"), createUnion(ref("IPostgresInterval"), primitive("string")), createUnion(ref("IPostgresInterval"), primitive("string")))
|
|
111662
|
+
},
|
|
111663
|
+
Point: {
|
|
111664
|
+
kind: "typeAlias",
|
|
111665
|
+
name: "Point",
|
|
111666
|
+
exported: true,
|
|
111667
|
+
type: { kind: "raw", value: "{ x: number; y: number }" }
|
|
111668
|
+
},
|
|
111669
|
+
Circle: {
|
|
111670
|
+
kind: "typeAlias",
|
|
111671
|
+
name: "Circle",
|
|
111672
|
+
exported: true,
|
|
111673
|
+
type: { kind: "raw", value: "{ x: number; y: number; radius: number }" }
|
|
111674
|
+
}
|
|
111675
|
+
};
|
|
111676
|
+
var HELPER_NAMES = Object.keys(HELPER_DEFINITIONS);
|
|
111677
|
+
|
|
111671
111678
|
// src/transform/index.ts
|
|
111672
111679
|
function transformDatabase(metadata, options) {
|
|
111673
111680
|
const declarations = [];
|
|
111674
111681
|
const unknownTypes = new Set;
|
|
111682
|
+
const usedHelpers = new Set;
|
|
111675
111683
|
const dialectName = options?.dialectName ?? "postgres";
|
|
111676
111684
|
const dialect = getDialect(dialectName);
|
|
111677
111685
|
const mapType = options?.mapType ?? ((dataType, opts) => dialect.mapType(dataType, opts));
|
|
@@ -111759,22 +111767,6 @@ function transformDatabase(metadata, options) {
|
|
|
111759
111767
|
},
|
|
111760
111768
|
exported: true
|
|
111761
111769
|
});
|
|
111762
|
-
if (dialectName === "postgres") {
|
|
111763
|
-
declarations.push({
|
|
111764
|
-
kind: "interface",
|
|
111765
|
-
name: "IPostgresInterval",
|
|
111766
|
-
properties: [
|
|
111767
|
-
{ name: "years", type: { kind: "primitive", value: "number" }, optional: true },
|
|
111768
|
-
{ name: "months", type: { kind: "primitive", value: "number" }, optional: true },
|
|
111769
|
-
{ name: "days", type: { kind: "primitive", value: "number" }, optional: true },
|
|
111770
|
-
{ name: "hours", type: { kind: "primitive", value: "number" }, optional: true },
|
|
111771
|
-
{ name: "minutes", type: { kind: "primitive", value: "number" }, optional: true },
|
|
111772
|
-
{ name: "seconds", type: { kind: "primitive", value: "number" }, optional: true },
|
|
111773
|
-
{ name: "milliseconds", type: { kind: "primitive", value: "number" }, optional: true }
|
|
111774
|
-
],
|
|
111775
|
-
exported: true
|
|
111776
|
-
});
|
|
111777
|
-
}
|
|
111778
111770
|
if (dialectName === "mysql") {
|
|
111779
111771
|
declarations.push({
|
|
111780
111772
|
kind: "interface",
|
|
@@ -111818,7 +111810,31 @@ function transformDatabase(metadata, options) {
|
|
|
111818
111810
|
const filteredTables = filterTables(metadata.tables, options);
|
|
111819
111811
|
const tableInterfaces = [];
|
|
111820
111812
|
for (const table of filteredTables) {
|
|
111821
|
-
tableInterfaces.push(transformTable(table, metadata.enums, enumResolver, mapType, options, unknownTypes));
|
|
111813
|
+
tableInterfaces.push(transformTable(table, metadata.enums, enumResolver, mapType, options, unknownTypes, usedHelpers));
|
|
111814
|
+
}
|
|
111815
|
+
if (dialectName === "postgres") {
|
|
111816
|
+
if (usedHelpers.has("Interval")) {
|
|
111817
|
+
declarations.push({
|
|
111818
|
+
kind: "interface",
|
|
111819
|
+
name: "IPostgresInterval",
|
|
111820
|
+
properties: [
|
|
111821
|
+
{ name: "years", type: { kind: "primitive", value: "number" }, optional: true },
|
|
111822
|
+
{ name: "months", type: { kind: "primitive", value: "number" }, optional: true },
|
|
111823
|
+
{ name: "days", type: { kind: "primitive", value: "number" }, optional: true },
|
|
111824
|
+
{ name: "hours", type: { kind: "primitive", value: "number" }, optional: true },
|
|
111825
|
+
{ name: "minutes", type: { kind: "primitive", value: "number" }, optional: true },
|
|
111826
|
+
{ name: "seconds", type: { kind: "primitive", value: "number" }, optional: true },
|
|
111827
|
+
{ name: "milliseconds", type: { kind: "primitive", value: "number" }, optional: true }
|
|
111828
|
+
],
|
|
111829
|
+
exported: true
|
|
111830
|
+
});
|
|
111831
|
+
}
|
|
111832
|
+
const helperOrder = ["Timestamp", "Int8", "Numeric", "Interval", "Point", "Circle"];
|
|
111833
|
+
for (const name of helperOrder) {
|
|
111834
|
+
if (usedHelpers.has(name)) {
|
|
111835
|
+
declarations.push(HELPER_DEFINITIONS[name]);
|
|
111836
|
+
}
|
|
111837
|
+
}
|
|
111822
111838
|
}
|
|
111823
111839
|
declarations.push(...tableInterfaces);
|
|
111824
111840
|
declarations.push(createDBInterface(filteredTables, options));
|