kysely-gen 0.5.0 → 0.5.1
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 +47 -9
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -44644,16 +44644,53 @@ function singularize(str) {
|
|
|
44644
44644
|
}
|
|
44645
44645
|
|
|
44646
44646
|
// src/transform/enum.ts
|
|
44647
|
-
function
|
|
44647
|
+
function getEnumKey(enumMeta) {
|
|
44648
|
+
return `${enumMeta.schema}.${enumMeta.name}`;
|
|
44649
|
+
}
|
|
44650
|
+
function getBaseName(enumMeta, defaultSchema = "public") {
|
|
44648
44651
|
if (enumMeta.schema === defaultSchema) {
|
|
44649
44652
|
return toPascalCase(enumMeta.name);
|
|
44650
44653
|
}
|
|
44651
44654
|
return toPascalCase(enumMeta.schema) + toPascalCase(enumMeta.name);
|
|
44652
44655
|
}
|
|
44653
|
-
function
|
|
44656
|
+
function getFullName(enumMeta) {
|
|
44657
|
+
return toPascalCase(enumMeta.schema) + toPascalCase(enumMeta.name);
|
|
44658
|
+
}
|
|
44659
|
+
|
|
44660
|
+
class EnumNameResolver {
|
|
44661
|
+
nameMap;
|
|
44662
|
+
constructor(enums, defaultSchema = "public") {
|
|
44663
|
+
this.nameMap = new Map;
|
|
44664
|
+
const baseNames = new Map;
|
|
44665
|
+
for (const enumMeta of enums) {
|
|
44666
|
+
const baseName = getBaseName(enumMeta, defaultSchema);
|
|
44667
|
+
const existing = baseNames.get(baseName) ?? [];
|
|
44668
|
+
existing.push(enumMeta);
|
|
44669
|
+
baseNames.set(baseName, existing);
|
|
44670
|
+
}
|
|
44671
|
+
for (const [, enumsWithSameName] of baseNames) {
|
|
44672
|
+
if (enumsWithSameName.length === 1) {
|
|
44673
|
+
const enumMeta = enumsWithSameName[0];
|
|
44674
|
+
this.nameMap.set(getEnumKey(enumMeta), getBaseName(enumMeta, defaultSchema));
|
|
44675
|
+
} else {
|
|
44676
|
+
for (const enumMeta of enumsWithSameName) {
|
|
44677
|
+
this.nameMap.set(getEnumKey(enumMeta), getFullName(enumMeta));
|
|
44678
|
+
}
|
|
44679
|
+
}
|
|
44680
|
+
}
|
|
44681
|
+
}
|
|
44682
|
+
getName(enumMeta) {
|
|
44683
|
+
return this.nameMap.get(getEnumKey(enumMeta));
|
|
44684
|
+
}
|
|
44685
|
+
}
|
|
44686
|
+
function getEnumTypeName(enumMeta, defaultSchema = "public") {
|
|
44687
|
+
return getBaseName(enumMeta, defaultSchema);
|
|
44688
|
+
}
|
|
44689
|
+
function transformEnum(enumMetadata, resolver) {
|
|
44690
|
+
const name = resolver ? resolver.getName(enumMetadata) : getEnumTypeName(enumMetadata);
|
|
44654
44691
|
return {
|
|
44655
44692
|
kind: "typeAlias",
|
|
44656
|
-
name
|
|
44693
|
+
name,
|
|
44657
44694
|
type: {
|
|
44658
44695
|
kind: "union",
|
|
44659
44696
|
types: enumMetadata.values.map((value) => ({
|
|
@@ -44677,8 +44714,8 @@ function toCamelCase(str) {
|
|
|
44677
44714
|
}
|
|
44678
44715
|
|
|
44679
44716
|
// src/transform/table.ts
|
|
44680
|
-
function transformTable(table, enums, mapType, options, unknownTypes) {
|
|
44681
|
-
const properties = table.columns.map((column) => transformColumn(column, enums, mapType, options, unknownTypes));
|
|
44717
|
+
function transformTable(table, enums, enumResolver, mapType, options, unknownTypes) {
|
|
44718
|
+
const properties = table.columns.map((column) => transformColumn(column, enums, enumResolver, mapType, options, unknownTypes));
|
|
44682
44719
|
return {
|
|
44683
44720
|
kind: "interface",
|
|
44684
44721
|
name: toPascalCase(singularize(table.name)),
|
|
@@ -44686,11 +44723,11 @@ function transformTable(table, enums, mapType, options, unknownTypes) {
|
|
|
44686
44723
|
exported: true
|
|
44687
44724
|
};
|
|
44688
44725
|
}
|
|
44689
|
-
function transformColumn(column, enums, mapType, options, unknownTypes) {
|
|
44726
|
+
function transformColumn(column, enums, enumResolver, mapType, options, unknownTypes) {
|
|
44690
44727
|
const matchingEnum = enums.find((e) => e.name === column.dataType && e.schema === (column.dataTypeSchema ?? "public"));
|
|
44691
44728
|
let type;
|
|
44692
44729
|
if (matchingEnum) {
|
|
44693
|
-
const enumTypeName =
|
|
44730
|
+
const enumTypeName = enumResolver.getName(matchingEnum);
|
|
44694
44731
|
type = { kind: "reference", name: enumTypeName };
|
|
44695
44732
|
if (column.isNullable) {
|
|
44696
44733
|
type = {
|
|
@@ -45820,13 +45857,14 @@ function transformDatabase(metadata, options) {
|
|
|
45820
45857
|
exported: true
|
|
45821
45858
|
});
|
|
45822
45859
|
}
|
|
45860
|
+
const enumResolver = new EnumNameResolver(metadata.enums);
|
|
45823
45861
|
for (const enumMetadata of metadata.enums) {
|
|
45824
|
-
declarations.push(transformEnum(enumMetadata));
|
|
45862
|
+
declarations.push(transformEnum(enumMetadata, enumResolver));
|
|
45825
45863
|
}
|
|
45826
45864
|
const filteredTables = filterTables(metadata.tables, options);
|
|
45827
45865
|
const tableInterfaces = [];
|
|
45828
45866
|
for (const table of filteredTables) {
|
|
45829
|
-
tableInterfaces.push(transformTable(table, metadata.enums, mapType, options, unknownTypes));
|
|
45867
|
+
tableInterfaces.push(transformTable(table, metadata.enums, enumResolver, mapType, options, unknownTypes));
|
|
45830
45868
|
}
|
|
45831
45869
|
declarations.push(...tableInterfaces);
|
|
45832
45870
|
declarations.push(createDBInterface(filteredTables, options));
|