Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add formatColumnName option #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ export declare type Options = {
* exclude: ["migration", "migration_lock"]
*/
exclude?: string[] | string;
/**
* If you defined, f.x. the [`postProcessResponse`](@link http://knexjs.org/#Installation-post-process-response) on your knex config,
* you might also want to format the generated interfaces to fit the same logic.
*
* @example
* formatColumnName(value: string) => camelCase(value),
*/
formatColumnName?: (value: string) => string;
};
/**
* Generates TypeScript definitions (types) from a PostgreSQL database schema.
Expand Down
17 changes: 14 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.updateTypes = updateTypes;
exports.getType = getType;
exports.updateTypes = updateTypes;

var _camelCase2 = _interopRequireDefault(require("lodash/camelCase"));

Expand Down Expand Up @@ -82,9 +82,20 @@ async function updateTypes(db, options) {
const key = (_overrides$value = overrides[value]) !== null && _overrides$value !== void 0 ? _overrides$value : (0, _upperFirst2.default)((0, _camelCase2.default)(value));
output.write(` ${key} = "${value}",\n`);
});
output.write("}\n\n"); // Construct TypeScript db record types
output.write("}\n\n"); // The list of tables as type

output.write("export type Tables = {\n");
Array.from(tableSet).forEach(key => {
var _overrides$key;

const value = (_overrides$key = overrides[key]) !== null && _overrides$key !== void 0 ? _overrides$key : (0, _upperFirst2.default)((0, _camelCase2.default)(key));
output.write(` "${key}": ${value},\n`);
});
output.write("};\n\n"); // Construct TypeScript db record types

columns.forEach((x, i) => {
var _options$formatColumn, _options$formatColumn2;

if (!(columns[i - 1] && columns[i - 1].table === x.table)) {
var _overrides$x$table;

Expand All @@ -99,7 +110,7 @@ async function updateTypes(db, options) {
type += " | null";
}

output.write(` ${sanitize(x.column)}: ${type};\n`);
output.write(` ${sanitize((_options$formatColumn = options === null || options === void 0 ? void 0 : (_options$formatColumn2 = options.formatColumnName) === null || _options$formatColumn2 === void 0 ? void 0 : _options$formatColumn2.call(options, x.column)) !== null && _options$formatColumn !== void 0 ? _options$formatColumn : x.column)}: ${type};\n`);

if (!(columns[i + 1] && columns[i + 1].table === x.table)) {
output.write("};\n\n");
Expand Down
15 changes: 14 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ export type Options = {
* exclude: ["migration", "migration_lock"]
*/
exclude?: string[] | string;

/**
* If you defined, f.x. the [`postProcessResponse`](@link http://knexjs.org/#Installation-post-process-response) on your knex config,
* you might also want to format the generated interfaces to fit the same logic.
*
* @example
* formatColumnName(value: string) => camelCase(value),
*/
formatColumnName?: (value: string) => string;
};

/**
Expand Down Expand Up @@ -185,7 +194,11 @@ export async function updateTypes(db: Knex, options: Options): Promise<void> {
type += " | null";
}

output.write(` ${sanitize(x.column)}: ${type};\n`);
output.write(
` ${sanitize(
options?.formatColumnName?.(x.column) ?? x.column
)}: ${type};\n`
);

if (!(columns[i + 1] && columns[i + 1].table === x.table)) {
output.write("};\n\n");
Expand Down