Skip to content

Add GET /canonicalFields endpoint#55

Merged
slifty merged 2 commits into
mainfrom
30-add-get-canonical-fields
Jul 27, 2022
Merged

Add GET /canonicalFields endpoint#55
slifty merged 2 commits into
mainfrom
30-add-get-canonical-fields

Conversation

@slifty

@slifty slifty commented Jul 21, 2022

Copy link
Copy Markdown
Member

This PR adds a real implementation for the GET /canonicalFields endpoint.

In doing so it also introduces the concept of data validation as well as test fixtures for integration tests.

Resolves #30

(This includes the code in #54 so it should be rebased against main once that is merged)

@slifty slifty force-pushed the 30-add-get-canonical-fields branch from 299374d to a24cf2c Compare July 21, 2022 18:36
Comment thread src/database/migrations/0001_create_canonical_fields.sql Outdated
Comment thread src/database/migrations/0001_create_canonical_fields.sql Outdated
Comment thread src/handlers/canonicalFieldsHandlers.ts
Comment thread src/logger.ts
Comment thread src/types/CanonicalField.ts Outdated
Comment thread tsconfig.dev.json Outdated
Comment thread tsconfig.json Outdated
Comment thread src/handlers/canonicalFieldsHandlers.ts Outdated
@slifty slifty force-pushed the 30-add-get-canonical-fields branch from a24cf2c to e9f5be7 Compare July 21, 2022 19:03
@slifty slifty requested a review from bickelj July 21, 2022 19:04
Comment thread src/database/migrations/0001_create_canonical_fields.sql Outdated
Comment thread package.json
Comment thread src/__tests__/__snapshots__/canonicalFields.int.test.ts.snap Outdated
Comment thread src/__tests__/canonicalFields.int.test.ts Outdated
Comment thread src/database/queries/canonicalFields/fetchAll.sql Outdated
Comment thread src/database/queries/canonicalFields/fetchAll.sql Outdated
Comment thread src/database/queries/fixtures/canonicalFields/returnsAllCanonicalFields/seed.sql Outdated
Comment thread src/handlers/canonicalFieldsHandlers.ts
Comment thread src/types/CanonicalField.ts
Comment thread src/types/CanonicalField.ts
Comment thread src/types/CanonicalField.ts
@bickelj

bickelj commented Jul 21, 2022

Copy link
Copy Markdown
Contributor

Overall this is tremendous.

@bickelj

bickelj commented Jul 21, 2022

Copy link
Copy Markdown
Contributor

I am able to run it with npm run start:dev fine (separately the tests are giving me grief) and get a correct 500 error with well-formatted message from GET /canonicalFields (because I have a different db structure at the moment).

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

Merging #55 (36f75a3) into main (52179b0) will decrease coverage by 0.05%.
The diff coverage is 69.04%.

@@            Coverage Diff             @@
##             main      #55      +/-   ##
==========================================
- Coverage   71.60%   71.55%   -0.06%     
==========================================
  Files          11       16       +5     
  Lines          81      116      +35     
  Branches        5        8       +3     
==========================================
+ Hits           58       83      +25     
- Misses         22       32      +10     
  Partials        1        1              
Impacted Files Coverage Δ
src/index.ts 0.00% <0.00%> (ø)
src/scripts/migrate.ts 0.00% <0.00%> (ø)
src/errors/ValidationError.ts 20.00% <20.00%> (ø)
src/handlers/canonicalFieldsHandlers.ts 72.22% <70.58%> (-27.78%) ⬇️
src/ajv.ts 100.00% <100.00%> (ø)
src/app.ts 100.00% <100.00%> (ø)
src/errors/index.ts 100.00% <100.00%> (ø)
src/logger.ts 100.00% <100.00%> (ø)
src/types/CanonicalField.ts 100.00% <100.00%> (ø)
src/types/index.ts 100.00% <100.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 52179b0...36f75a3. Read the comment docs.

Comment thread src/__tests__/canonicalFields.int.test.ts
Comment thread src/app.ts
Comment thread src/database/migrations/0001_create_canonical_fields.sql
Comment thread src/database/migrations/0001_create_canonical_fields.sql
Comment thread src/database/queries/canonicalFields/fetchAll.sql
Comment thread src/errors/ValidationError.ts
Comment thread src/handlers/canonicalFieldsHandlers.ts
Comment thread src/handlers/canonicalFieldsHandlers.ts
Comment thread src/logger.ts Outdated
Comment thread src/test/integrationSuiteSetup.ts
Comment thread src/types/CanonicalField.ts
@bickelj

bickelj commented Jul 27, 2022

Copy link
Copy Markdown
Contributor

I think it can be merged as-is, but it would be nice to rebase onto main. I see you used LOG_LEVEL and I used PINO_LOG_LEVEL in another PR. I think LOG_LEVEL as you have it here makes sense instead of naming the env var after the particular implementation. My only other hesitation is the test code being in the prod code and whether we can add a unique constraint to the short code.

@slifty

slifty commented Jul 27, 2022

Copy link
Copy Markdown
Member Author

Yes -- in fact, I'm going to update our main branch protections to require PRs to be up to date before allowing merge.

@slifty

slifty commented Jul 27, 2022

Copy link
Copy Markdown
Member Author

@bickelj the local tests may be giving you trouble if you're running them against a database that has an existing schema where migrations have been run ( this is due to ThomWright/postgres-migrations#93 not being merged yet )

If you set up .env.test and point to a DB that has nothing inside does it work?

@slifty slifty force-pushed the 30-add-get-canonical-fields branch from e4c36fd to 43033ed Compare July 27, 2022 18:47
@slifty

slifty commented Jul 27, 2022

Copy link
Copy Markdown
Member Author

@bickelj another round! (sorry I know you keep approving and then I keep making all the optional changes -- please feel free to flag more improvements!)

@slifty slifty requested a review from bickelj July 27, 2022 18:48
Comment thread src/__tests__/canonicalFields.int.test.ts

@bickelj bickelj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I assumed the .keep files were for the purpose of keeping directories present in git (no such thing as a directory in git), but I also see "build:assets": "copyfiles -u 1 \"src/**/*.sql\" \"src/**/.keep\" dist",, but I think it is OK because those will not be copied over if they do not exist.

Thanks for iterating on this one with me a few times, I think this is huge. 👌

slifty added 2 commits July 27, 2022 16:28
This adds support for retrieving all canonical fields contained in the
system.  As part of this change we:

1. Introduce `ajv` which is a runtime validator that uses JSONSchema in
   order to ensure that data conforms to a given TypeScript type.
2. Introduce migrations and basic queries for the `canonical_field`
   table.
3. Introduces a new pattern for integration tests that rely on fixture
   data.

We want to be sure the data result conforms to our API spec / expected
type output so we actually validate it before passing it along to the
response middleware.

Note that Express route handlers cannot be asynchronous which is why we
have to use the promise `then` and `catch` syntax.

Issue #30 Implement GET /canonicalFields API endpoint
We want to make it easy for files to create loggers that point back to
the file generating a given log.

By exposing ONLY `getLogger` method we support a cleaner for the logger
syntax for the logger (rather than importing `logger` and having to use
`childLogger` in our code, we can just import `getChildLogger` and
assign the child logger to a local `logger` value).

This also ensures that all loggers are generated with a name (I suppose
someone could choose to name with an empty string).
@slifty slifty force-pushed the 30-add-get-canonical-fields branch from 43033ed to bc0b98c Compare July 27, 2022 20:28
@slifty

slifty commented Jul 27, 2022

Copy link
Copy Markdown
Member Author

I assumed the .keep files were for the purpose of keeping directories present in git (no such thing as a directory in git),

It served both purposes! We needed the migrations and queries directories in prod to prevent the db code from erroring, so copying .keep helped there too. We can take that out in future (I'll merge this as is though)

Thanks for iterating on this one with me a few times

Same to you! thank you for all the great feedback; I like where this has landed

@slifty slifty merged commit ffaaecf into main Jul 27, 2022
@slifty slifty deleted the 30-add-get-canonical-fields branch July 27, 2022 20:31
@bickelj

bickelj commented Jul 27, 2022

Copy link
Copy Markdown
Contributor

@bickelj the local tests may be giving you trouble if you're running them against a database that has an existing schema where migrations have been run ( this is due to ThomWright/postgres-migrations#93 not being merged yet )

If you set up .env.test and point to a DB that has nothing inside does it work?

That works.

@slifty slifty restored the 30-add-get-canonical-fields branch July 27, 2022 22:30
@slifty slifty deleted the 30-add-get-canonical-fields branch July 28, 2022 03:00
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.

Implement GET /canonicalFields API endpoint

3 participants