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

Replace lodash with native js #753

Merged
merged 17 commits into from
Apr 27, 2021
Merged

Replace lodash with native js #753

merged 17 commits into from
Apr 27, 2021

Conversation

Shinigami92
Copy link
Collaborator

fixes #738

(options: ColumnDefinition, columnName) =>
typeof options.comment !== 'undefined' &&
makeComment('COLUMN', `${mOptions.literal(tableName)}.${mOptions.literal(columnName)}`, options.comment),
const comments = Object.keys(columnsWithOptions).filter((columnName) => {
Copy link
Collaborator Author

@Shinigami92 Shinigami92 Mar 4, 2021

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

@Shinigami92
Copy link
Collaborator Author

Mh 🤔, seems that 360cb7c broke it 🙁
I need to look deeper into this chain function

@Shinigami92
Copy link
Collaborator Author

@goce-cz Okay I'm really confused why this is not working 🙁
Could you have a look into the e2e tests and hopefully provide me with a hint what the problem could be?

@Shinigami92
Copy link
Collaborator Author

Okay I check it by reverting my last 3 commits and yes, it is definitely the _.chain call that I currently misunderstand 🤔
I will ask for help on a TypeScript Community Discord Server 🙂

@Shinigami92
Copy link
Collaborator Author

Aha! The primaryColumns are correct but the comments are not

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' ]

@Shinigami92 Shinigami92 marked this pull request as ready for review March 5, 2021 07:54
@Shinigami92
Copy link
Collaborator Author

We may extract the following to its own interface

{
  [x: string]: ColumnDefinition & FunctionParamType
}

Or we could replace it with Record<string, ColumnDefinition & FunctionParamType>

Or both solutions together:

export type NewType = Record<string, ColumnDefinition & FunctionParamType>
// ...
Object.keys(/*...*/).reduce<NewType>(// ...

@Shinigami92
Copy link
Collaborator Author

@goce-cz If merging this PR, you should prefer a squash-merge 🙂

Copy link
Contributor

@goce-cz goce-cz left a 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.

Comment on lines +73 to +75
let columnsWithOptions = Object.keys(columns).reduce<{
[x: string]: ColumnDefinition & FunctionParamType
}>((previous, column) => ({ ...previous, [column]: applyType(columns[column], extendingTypeShorthands) }), {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this would be:

Suggested change
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:

Suggested change
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) }), {})

Copy link
Contributor

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.

Suggested change
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) }),
{},
)

Copy link
Collaborator Author

@Shinigami92 Shinigami92 Mar 18, 2021

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 🤔

Copy link
Collaborator Author

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

Copy link
Contributor

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.

src/operations/tables.ts Outdated Show resolved Hide resolved
src/operations/tables.ts Outdated Show resolved Hide resolved
src/operations/tables.ts Outdated Show resolved Hide resolved
src/operations/tables.ts Outdated Show resolved Hide resolved
src/operations/tables.ts Show resolved Hide resolved
src/operations/types.ts Outdated Show resolved Hide resolved
@Shinigami92
Copy link
Collaborator Author

Thanks for the really great review 🚀
I will look into this this evening and apply changes

I also thought about extracting an intersection-function to a utility file

@Shinigami92 Shinigami92 self-assigned this Mar 18, 2021
@Shinigami92 Shinigami92 requested a review from goce-cz March 18, 2021 16:51
Copy link
Contributor

@goce-cz goce-cz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see below:

Comment on lines +73 to +75
let columnsWithOptions = Object.keys(columns).reduce<{
[x: string]: ColumnDefinition & FunctionParamType
}>((previous, column) => ({ ...previous, [column]: applyType(columns[column], extendingTypeShorthands) }), {})
Copy link
Contributor

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.

@Shinigami92 Shinigami92 requested a review from goce-cz April 23, 2021 14:14
@Shinigami92
Copy link
Collaborator Author

@goce-cz All tests are green ✔️

Copy link
Contributor

@goce-cz goce-cz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot 👍

@goce-cz goce-cz merged commit 4e3e892 into salsita:master Apr 27, 2021
@Shinigami92 Shinigami92 deleted the issue-738 branch April 27, 2021 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prototype pollution issue with lodash dependency
2 participants