-
Notifications
You must be signed in to change notification settings - Fork 180
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
Replace lodash with native js #753
Conversation
src/operations/tables.ts
Outdated
(options: ColumnDefinition, columnName) => | ||
typeof options.comment !== 'undefined' && | ||
makeComment('COLUMN', `${mOptions.literal(tableName)}.${mOptions.literal(columnName)}`, options.comment), | ||
const comments = Object.keys(columnsWithOptions).filter((columnName) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has no unit test coverage
Mh 🤔, seems that 360cb7c broke it 🙁 |
@goce-cz Okay I'm really confused why this is not working 🙁 |
This reverts commit 360cb7c.
Okay I check it by reverting my last 3 commits and yes, it is definitely the |
Aha! The columnsWithOptions before comments: { id:
{ type: 'serial',
primaryKey: true,
comment: 'comment on column id' } }
comments with chain: [ 'COMMENT ON COLUMN "test"."tcc"."id" IS $pga$comment on column id$pga$;' ]
comments with native: [ 'id' ] |
We may extract the following to its own interface {
[x: string]: ColumnDefinition & FunctionParamType
} Or we could replace it with Or both solutions together: export type NewType = Record<string, ColumnDefinition & FunctionParamType>
// ...
Object.keys(/*...*/).reduce<NewType>(// ... |
@goce-cz If merging this PR, you should prefer a squash-merge 🙂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for your contribution. I agree that using lodash
for primitive operations like Array.map()
and Object.keys()
is a relic from the times long gone and having it in a library like this is a signal of malfunctioning maintenance 👍
The non-trivial helpers like intersect
are a different question, but it probably makes little sense to depend on whole lodash
because a few usages of simple helpers.
Please polish the types and variable names as suggested and also consider using Object.entries
instead of Object.keys
where appropriate.
let columnsWithOptions = Object.keys(columns).reduce<{ | ||
[x: string]: ColumnDefinition & FunctionParamType | ||
}>((previous, column) => ({ ...previous, [column]: applyType(columns[column], extendingTypeShorthands) }), {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally this would be:
let columnsWithOptions = Object.keys(columns).reduce<{ | |
[x: string]: ColumnDefinition & FunctionParamType | |
}>((previous, column) => ({ ...previous, [column]: applyType(columns[column], extendingTypeShorthands) }), {}) | |
let columnsWithOptions = Object.fromEntries( | |
Object.entries(columns) | |
.map(([columnName, columnDefinition]) => | |
[columnName, applyType(columnDefinition,extendingTypeShorthands)] | |
) | |
) |
too bad that Object.fromEntries
is not supported prior Node 14...
I'm not a great fan of allocating a new object in each iteration of the reduce operation, but I guess it's a common practice and we probably don't expect thousands of columns here. Anyway I'd still prefer if you used Object.entries()
to avoid dependency on the source object from within the callback:
let columnsWithOptions = Object.keys(columns).reduce<{ | |
[x: string]: ColumnDefinition & FunctionParamType | |
}>((previous, column) => ({ ...previous, [column]: applyType(columns[column], extendingTypeShorthands) }), {}) | |
let columnsWithOptions = Object.entries(columns).reduce<{ | |
[x: string]: ColumnDefinition & FunctionParamType | |
}>((previous, [columnName, columnDefinition]) => ({ ...previous, [columnName]: applyType(columnDefinition, extendingTypeShorthands) }), {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding the type, I would just use Record<>
instead to make the code more readable and keep the explicit type extension.
let columnsWithOptions = Object.keys(columns).reduce<{ | |
[x: string]: ColumnDefinition & FunctionParamType | |
}>((previous, column) => ({ ...previous, [column]: applyType(columns[column], extendingTypeShorthands) }), {}) | |
let columnsWithOptions: Record<string, ColumnDefinition & FunctionParamType> = Object.keys(columns).reduce( | |
(previous, column) => ({ ...previous, [column]: applyType(columns[column], extendingTypeShorthands) }), | |
{}, | |
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I can use Object.fromEntries
because the build step targets es2017
https://node.green/#ES2019-features--Object-fromEntries
But looking into the produced dist folder, the Object.fromEntries
is still there 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we drop node 10 support?
node 12 supports it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the reduce or eventually some custom helper is IMO not a big enough annoyance to justify dropping support of Node 10. 🤷
We can deprecate the old Node with the next major/minor release (issue a warning) and then drop it later.
TL;DR; Please remove the Object.fromEntries
for now - the tests are failing.
Thanks for the really great review 🚀 I also thought about extracting an |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see below:
let columnsWithOptions = Object.keys(columns).reduce<{ | ||
[x: string]: ColumnDefinition & FunctionParamType | ||
}>((previous, column) => ({ ...previous, [column]: applyType(columns[column], extendingTypeShorthands) }), {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the reduce or eventually some custom helper is IMO not a big enough annoyance to justify dropping support of Node 10. 🤷
We can deprecate the old Node with the next major/minor release (issue a warning) and then drop it later.
TL;DR; Please remove the Object.fromEntries
for now - the tests are failing.
@goce-cz All tests are green ✔️ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot 👍
fixes #738