kysely-gen 0.4.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 +80 -31
- 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));
|
|
@@ -45874,7 +45912,7 @@ function detectDialect(connectionString) {
|
|
|
45874
45912
|
|
|
45875
45913
|
// src/cli.ts
|
|
45876
45914
|
var program2 = new Command;
|
|
45877
|
-
program2.name("kysely-gen").description("Generate Kysely types from your database").version("0.1.0").option("-o, --out <path>", "Output file path", "./db.d.ts").option("-s, --schema <name>", "Schema to introspect (can be specified multiple times)", collect, []).option("--url <connection-string>", "Database connection string (overrides DATABASE_URL env)").option("-d, --dialect <name>", "Database dialect (postgres, mysql, sqlite). Auto-detected from URL if not specified").option("--camel-case", "Convert column and table names to camelCase (use with Kysely CamelCasePlugin)").option("--include-pattern <pattern>", "Only include tables matching glob pattern (schema.table format)", collect, []).option("--exclude-pattern <pattern>", "Exclude tables matching glob pattern (schema.table format)", collect, []).action(async (options) => {
|
|
45915
|
+
program2.name("kysely-gen").description("Generate Kysely types from your database").version("0.1.0").option("-o, --out <path>", "Output file path", "./db.d.ts").option("-s, --schema <name>", "Schema to introspect (can be specified multiple times)", collect, []).option("--url <connection-string>", "Database connection string (overrides DATABASE_URL env)").option("-d, --dialect <name>", "Database dialect (postgres, mysql, sqlite). Auto-detected from URL if not specified").option("--camel-case", "Convert column and table names to camelCase (use with Kysely CamelCasePlugin)").option("--include-pattern <pattern>", "Only include tables matching glob pattern (schema.table format)", collect, []).option("--exclude-pattern <pattern>", "Exclude tables matching glob pattern (schema.table format)", collect, []).option("--print", "Output to stdout instead of writing to file").action(async (options) => {
|
|
45878
45916
|
try {
|
|
45879
45917
|
await generate(options);
|
|
45880
45918
|
} catch (error2) {
|
|
@@ -45891,6 +45929,8 @@ function collect(value, previous) {
|
|
|
45891
45929
|
return previous.concat([value]);
|
|
45892
45930
|
}
|
|
45893
45931
|
async function generate(options) {
|
|
45932
|
+
const printMode = options.print === true;
|
|
45933
|
+
const log = printMode ? (...args) => console.error(...args) : (...args) => console.log(...args);
|
|
45894
45934
|
const databaseUrl = options.url || process.env.DATABASE_URL;
|
|
45895
45935
|
if (!databaseUrl) {
|
|
45896
45936
|
console.error(source_default.red("Error: DATABASE_URL environment variable is required"));
|
|
@@ -45921,15 +45961,17 @@ async function generate(options) {
|
|
|
45921
45961
|
const outputPath = options.out;
|
|
45922
45962
|
const defaultSchema = dialectName === "sqlite" ? "main" : "public";
|
|
45923
45963
|
const schemas = options.schema.length > 0 ? options.schema : [defaultSchema];
|
|
45924
|
-
|
|
45925
|
-
|
|
45926
|
-
|
|
45927
|
-
|
|
45928
|
-
|
|
45929
|
-
|
|
45930
|
-
|
|
45931
|
-
|
|
45932
|
-
|
|
45964
|
+
log("");
|
|
45965
|
+
log(source_default.bold("kysely-gen") + source_default.dim(" v0.1.0"));
|
|
45966
|
+
log("");
|
|
45967
|
+
log(source_default.dim("Dialect:"), dialectName);
|
|
45968
|
+
log(source_default.dim("Connection:"), maskPassword(databaseUrl));
|
|
45969
|
+
log(source_default.dim("Schemas:"), schemas.join(", "));
|
|
45970
|
+
if (!printMode) {
|
|
45971
|
+
log(source_default.dim("Output:"), resolve(outputPath));
|
|
45972
|
+
}
|
|
45973
|
+
log("");
|
|
45974
|
+
const spinner = ora({ text: "Connecting to database...", stream: printMode ? process.stderr : process.stdout }).start();
|
|
45933
45975
|
let db;
|
|
45934
45976
|
try {
|
|
45935
45977
|
const kyselyDialect = await dialect.createKyselyDialect(databaseUrl);
|
|
@@ -45945,9 +45987,9 @@ async function generate(options) {
|
|
|
45945
45987
|
const enumCount = metadata.enums.length;
|
|
45946
45988
|
if (tableCount === 0 && enumCount === 0) {
|
|
45947
45989
|
spinner.warn("No tables or enums found");
|
|
45948
|
-
|
|
45949
|
-
|
|
45950
|
-
|
|
45990
|
+
log("");
|
|
45991
|
+
log(source_default.yellow("Warning: No tables or enums found in the specified schemas."));
|
|
45992
|
+
log(source_default.dim(" Make sure the schema names are correct and contain tables."));
|
|
45951
45993
|
await db.destroy();
|
|
45952
45994
|
return;
|
|
45953
45995
|
}
|
|
@@ -45960,20 +46002,27 @@ async function generate(options) {
|
|
|
45960
46002
|
excludePattern: options.excludePattern.length > 0 ? options.excludePattern : undefined
|
|
45961
46003
|
});
|
|
45962
46004
|
const code = serialize(astProgram);
|
|
45963
|
-
|
|
45964
|
-
|
|
45965
|
-
|
|
46005
|
+
if (printMode) {
|
|
46006
|
+
spinner.succeed("Types generated");
|
|
46007
|
+
process.stdout.write(code);
|
|
46008
|
+
} else {
|
|
46009
|
+
const absolutePath = resolve(outputPath);
|
|
46010
|
+
await writeFile(absolutePath, code, "utf-8");
|
|
46011
|
+
spinner.succeed(`Types written to ${source_default.cyan(absolutePath)}`);
|
|
46012
|
+
}
|
|
45966
46013
|
if (warnings.length > 0) {
|
|
45967
|
-
|
|
45968
|
-
|
|
46014
|
+
log("");
|
|
46015
|
+
log(source_default.yellow("Warnings:"));
|
|
45969
46016
|
for (const w of warnings) {
|
|
45970
|
-
|
|
46017
|
+
log(source_default.dim(` Unknown type '${w.pgType}' mapped to 'unknown'`));
|
|
45971
46018
|
}
|
|
45972
46019
|
}
|
|
45973
46020
|
await db.destroy();
|
|
45974
|
-
|
|
45975
|
-
|
|
45976
|
-
|
|
46021
|
+
if (!printMode) {
|
|
46022
|
+
log("");
|
|
46023
|
+
log(source_default.green("Done!"));
|
|
46024
|
+
log("");
|
|
46025
|
+
}
|
|
45977
46026
|
}
|
|
45978
46027
|
function maskPassword(connectionString) {
|
|
45979
46028
|
try {
|