-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
Support for formatters (that could fix few problems at once) #12
Comments
Hey @d7ark! Do you think we can extend the existing CREATE TABLE story (
id uuid PRIMARY KEY,
content jsonb NOT NULL DEFAULT '[]'::jsonb,
created timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
); type Overrides = Record<string, { name: string } | { type: string } | string> & {
'$table'?: (name: string, schema: string) => string;
'$column'?: (name: string, table: string, type: string) => { name?: string, type?: string } | string;
'$enum'?: (name: string, schema: string) => string;
'$enumValue'?: (name: string, enumName: string) => { name?: string, type?: string } | string;
}; Also check How to create a Pull Request (PR) |
Given the following schema: CREATE TABLE story (
id uuid PRIMARY KEY,
content jsonb NOT NULL DEFAULT '[]'::jsonb,
created timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
); And the callback (formatter) function for column names: updateTypes(db, {
output: "./types.ts",
overrides: {
"$column": (name) => camelCase(name),
}
}); The overrides["$column"]("content", "story", "jsonb"); If the function returns a updateTypes(db, {
output: "./types.ts",
overrides: {
"$column": (name, table) => table === "story" ? camelCase(name) : undefined,
}
}); |
I used a Proxy to singularize the table names
|
Hey,
I'm just migrating from using schemats (https://www.npmjs.com/package/schemats) and would like to work with knex-types, but there's few features missing. I thought rather than forking and just adding it for myself I'll make this project better(?) for everyone who uses it. I hope it's fine.
First thing is an idea that sprouted from this comment. I've looked into suggested approach (asterisk argument for overrides being function taking type and name) and found there are two problems with this:
But working on that I came up with (i think) better solution: another option - formatters - it's a set of optional functions that each formats different type (right now I have column, enum, enumEl(ement) and table, but im not 100% sold on these).
It would look like this:
I think it could solve multiple problems: columns with space
formatters = { column: name =>
"${name}"}
, obviously camelCasing columns, but also escaping column namesformatters={ column: name => quoteColumnName(name)
. I think It would add a lot of freedom for users.I rarely contribute to open source*, so I just wrote code thinking I'll go straight to pushing Pull requests... So I have few things already written. And It seems I cant push to this repo 😅
What should I do next? 🙇
*
I'd love to write more for open source projects, but Im not sure where to start and to be honest it's a bit scary.The text was updated successfully, but these errors were encountered: