Open d7ark opened 2 years ago
Hey @d7ark! Do you think we can extend the existing overrides
option to support these scenarios? For example:
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)
Hey. Thank you for the pointers. I think your suggestion looks great, but Im not sure what should be the arguments of the overrides functions:
Could you explain what you had in mind (I think $column
would a good example)? What happens when function returns {name, type}
object? And as I mentioned before what should be provided as the 2nd and 3rd argument?
Thanks!
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 $column
callback would be called with the following args for the story.content
column:
overrides["$column"]("content", "story", "jsonb");
If the function returns a null
, false
, or undefined
the default column name formatter would be used.
updateTypes(db, {
output: "./types.ts",
overrides: {
"$column": (name, table) => table === "story" ? camelCase(name) : undefined,
}
});
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.