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

chore(gatsby) Migrate db methods to Typescript #22725

Closed
wants to merge 8 commits into from
Closed

Conversation

arthurjdam
Copy link
Contributor

Description

Migrates remaining classes in src/db to Typescript.
The export at the bottom of src/db/nodes.ts is a little undesirable but the only way I could get it to work without rewriting all the other require()s.
Open to suggestions!

Related Issues

Related to #21995

@pieh
Copy link
Contributor

pieh commented May 4, 2020

We recently disabled loki ( #23646 ) - we plan to remove it completely as we've been addressing lot of issues that loki was trying to solve and it's unnecessary to keep maintaining it.

This cause bit of conflicts in your PR - would you be able to fix those conflicts?

Copy link
Contributor

@pvdz pvdz left a comment

Choose a reason for hiding this comment

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

Please rebase/merge to master. Loki has been removed from the codebase, which should make this PR easier.

Additionally, please look into refering to IGatsbyNode for internal Gatsby nodes.

@@ -47,7 +47,7 @@ import getSslCert from "../utils/get-ssl-cert"
import { slash } from "gatsby-core-utils"
import { initTracer } from "../utils/tracer"
import apiRunnerNode from "../utils/api-runner-node"
import db from "../db"
import * as db from "../db"
Copy link
Contributor

Choose a reason for hiding this comment

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

If possible I prefer to be explicit in the imports.

Suggested change
import * as db from "../db"
import {saveState, startAutosave} from "../db"

import _ from "lodash"
import { Node } from "gatsby"
import { store } from "../redux"
import * as reduxNodes from "../redux/nodes"

if (process.env.GATSBY_DB_NODES === `loki`) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This file should be much simpler to convert now that loki is dropped.

@@ -1,12 +1,43 @@
const _ = require(`lodash`)
import _ from "lodash"
import { Node } from "gatsby"
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this an IGatsbyNode? From gatsby/packages/gatsby/src/redux/types.ts

import { Node } from "gatsby"

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ObjectOrArray = Record<string, any> | Array<any> | Node
Copy link
Contributor

Choose a reason for hiding this comment

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

Would unknown be a better fit here? I'm not sure if we can refine optional to non-optional here (or somehow exclude void from the typing) but that's probably not very useful.

*/
const sanitizeNode = (data, isNode = true, path = new Set()) => {
export function sanitizeNode<T extends Node>(
Copy link
Contributor

Choose a reason for hiding this comment

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

What is T when isNode is false?

The recursive call to this function might receive a node (IGatsbyNode) but most likely will be a plain object or filter value.

data: T,
isNode: boolean = true,
path: Set<T> = new Set()
): T | undefined {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe the initial call to this recursive function should be separated (fine from a perf pov) because that call must return an IGatsbyNode whereas recursive calls can, but can also return a plain object. Not sure how to go about typing those tbh since it's, from TS's pov, untyped arbitrary content.

@@ -20,15 +51,15 @@ const sanitizeNode = (data, isNode = true, path = new Set()) => {
returnData[key] = o
return
}
returnData[key] = sanitizeNode(o, false, path)
returnData[key] = sanitizeNode(o as T, false, path)
Copy link
Contributor

Choose a reason for hiding this comment

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

This may not be an IGatsbyNode, or even whatever Node is, as it's an arbitrary object inside a node so casting it is probably not what we want here?

@pvdz
Copy link
Contributor

pvdz commented Jun 30, 2020

Hi @arthurjdam , can you please confirm whether or not you'd like to continue working on this PR? Thank you 💜

@pvdz pvdz added the status: awaiting author response Additional information has been requested from the author label Jun 30, 2020
@gatsby-cloud-staging
Copy link

Your pull request can be previewed in Gatsby Cloud: https://build-5048d338-6c45-42c0-8085-e6ff35954c8f.staging-previews.gtsb.io

@blainekasten
Copy link
Contributor

Looks like this is having some typechecking and linting issues after recent changes?. Mind making the updates and I'll prioritize getting this in?

@LekoArts
Copy link
Contributor

I'll close this PR as stale. Thanks for the PR though!

@LekoArts LekoArts closed this Nov 10, 2020
@LekoArts LekoArts deleted the issues/21995-3 branch November 10, 2020 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: awaiting author response Additional information has been requested from the author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants