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

sync JavaScript enums with PostgreSQL #562

Closed
lindavin opened this issue Feb 22, 2021 · 10 comments
Closed

sync JavaScript enums with PostgreSQL #562

lindavin opened this issue Feb 22, 2021 · 10 comments
Assignees
Labels
i-good-first-issue This issue is probably a good starting point for people new to coding or the OED project. p-low-priority t-enhancement This issues tracks a potential improvement to the software
Milestone

Comments

@lindavin
Copy link
Contributor

In our PostgreSQL database, we use enums for meter types. We also supply logical names for these types as an enum in the Meter class. These two enums need to be kept in sync, i.e. if we make changes to either one these enums in the future, we need to remember to change the other.

@huss huss added i-good-first-issue This issue is probably a good starting point for people new to coding or the OED project. p-low-priority t-enhancement This issues tracks a potential improvement to the software labels Feb 22, 2021
@huss huss added this to the 1.1 release milestone Dec 26, 2021
@RuxueJ
Copy link
Contributor

RuxueJ commented Feb 24, 2024

Hello! Can I create a folder named "shared" which contains meterTypes.js and meterTypes.ts, which can export the type nums. Then I import one in src/client/app/types/redux/meters.ts , and import another in src/server/models/Meter.js? Since the two meterTypes file are in one folder, poeple will probably remember to keep them in sync.

@huss
Copy link
Member

huss commented Feb 24, 2024

@RuxueJ Thanks for this idea. It is probably an improvement but I would like to see if there is a way to automatically keep the JS and TS in sync. If not, we could do what you suggest.

Also, this does not seme to address the direct issue of making the Postgres enums match the TS/JS ones. Any thoughts on that?

@huss huss modified the milestones: 1.1 release, 1.x Mar 22, 2024
@DPJProgramming
Copy link
Contributor

My team and I would Like to work on this issue.

@huss
Copy link
Member

huss commented Nov 8, 2024

@DPJProgramming Thanks for wanting to work on this. I'm not sure how to make this work and I hope you can come up with ideas. I'm also happy to help as needed.

@EBoyd3443
Copy link
Contributor

EBoyd3443 commented Nov 18, 2024

Hello! We've (@DPJProgramming, @dalejk88, @EBoyd3443, @Gballreich) set up an initial test case for checking JS objects to SQL enums:
https://github.com/EBoyd3443/OED/blob/development/src/server/test/db/enumTests.js
Any comments about the test case before we generalize for each other enum.

@huss
Copy link
Member

huss commented Nov 19, 2024

Thanks to @DPJProgramming, @dalejk88, @EBoyd3443, @Gballreich for working on this. I have not run the code to check it but it seems good. Here are a few thoughts:

  • The rest of the OED code base does:
const database = require('../../models/database');
const sqlFile = database.sqlFile;

so it might be best to do that.

  • It is okay to add this to the sql/ directory. However, since it is only used in testing and is simple, I wonder about putting the SQL into the test file. Take a look at src/server/test/db/unitReadingsTests.js (line 53 is an example) where this is done in a test.
  • The indenting around line 20/21 seems off as the loop over Meter.type is not part of the database loop.
  • OED normally checks the length before the values. The reason is that the values will clearly fail and it is valuable to know the number involved. I'm not sure that strictly applies here as the other message would be okay but maybe do that for consistence unless there is a reason not to.
  • Finally, comments in code is always helpful.
    I think it looks fine so it is okay to move forward on more tests if you are comfortable with these comments. I'm also happy to look again at any changes if that help you.

@EBoyd3443
Copy link
Contributor

EBoyd3443 commented Nov 29, 2024

Hi Huss,
While we've finished our five tests for the DB enums which have JS object equivalents, and on the last one (unitType):

CREATE TYPE unit_type AS ENUM('unit', 'meter', 'suffix');
(line 8)
Unit.unitType = Object.freeze({
(line 232)
These two do not equate. We've written the test for them, but wanted to double check that they should equate and if we should make any changes outside our tests when submitting the pull request.

@huss
Copy link
Member

huss commented Nov 30, 2024

Well, this is an example of how testing can help find issues. Good catch and thanks for asking. I searched the code and Unit.AREA does not seem to be used. It has been a while but I think that is a remnant of the idea that area would be calculated on the server and use the unit concept. Instead, OED does the calculation on the client. So, Unit.AREA should be removed from the Object. Let me know if this causes any issues.

@Gballreich
Copy link
Contributor

Currently we have an open pull request to create tests for keeping DB Enums in sync with the JS objects link here

What we tried to address the TS Enum tests:

  • Creating a TS test file with it's own config.

  • Pulling from the bundle.js TS output file.

After speaking with @huss 12.04.24 our team has come to the conclusion of:

  • There is a possible solution by creating an individual route for testing the TS Enums. (not approved by @huss)
  • There could be an underlying issue with the internal webpack.
  • The best long term solution for this issue would be to convert the codebase to TS.
  • @huss recommendation is to close this issue.

@huss
Copy link
Member

huss commented Dec 5, 2024

As stated, I concur to close this issue. If anyone has ideas on how to check the TS enums then that would be welcome. When OED fully converts to TS then the tests for the JS objects that are logically enums should be converted to check the TS enums. The current client only enums could be compared to the DB ones. It is also assumed that a single enum will exist for the client and server.

@huss huss closed this as completed Dec 5, 2024
huss added a commit that referenced this issue Dec 5, 2024
Tests JS objects and DB Enums in sync for Issue #562
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
i-good-first-issue This issue is probably a good starting point for people new to coding or the OED project. p-low-priority t-enhancement This issues tracks a potential improvement to the software
Projects
None yet
Development

No branches or pull requests

6 participants