-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Conversation
We recently disabled This cause bit of conflicts in your PR - would you be able to fix those conflicts? |
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 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" |
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.
If possible I prefer to be explicit in the imports.
import * as db from "../db" | |
import {saveState, startAutosave} from "../db" |
packages/gatsby/src/db/nodes.ts
Outdated
import _ from "lodash" | ||
import { Node } from "gatsby" | ||
import { store } from "../redux" | ||
import * as reduxNodes from "../redux/nodes" | ||
|
||
if (process.env.GATSBY_DB_NODES === `loki`) { |
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 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" |
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.
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 |
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.
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>( |
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.
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 { |
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.
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) |
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 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?
Hi @arthurjdam , can you please confirm whether or not you'd like to continue working on this PR? Thank you 💜 |
Your pull request can be previewed in Gatsby Cloud: https://build-5048d338-6c45-42c0-8085-e6ff35954c8f.staging-previews.gtsb.io |
Looks like this is having some typechecking and linting issues after recent changes?. Mind making the updates and I'll prioritize getting this in? |
I'll close this PR as stale. Thanks for the PR though! |
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