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

Update with-mongodb to be TypeScript-friendly #19383

Merged
merged 6 commits into from
Dec 28, 2020
Merged

Conversation

Zertz
Copy link
Contributor

@Zertz Zertz commented Nov 21, 2020

I slightly modified the approach so TypeScript can correctly infer types without actually having to type anything but the global:

index.d.ts

import { Db, MongoClient } from "mongodb";

declare global {
  type MongoConnection = {
    client: MongoClient;
    db: Db;
  };

  var mongo: {
    conn: MongoConnection | null;
    promise: Promise<MongoConnection> | null;
  };
}

I slightly modified the approach so TypeScript can correctly infer types without actually having to type anything but the global:

**index.d.ts**
```ts
import { Db, MongoClient } from "mongodb";

declare global {
  namespace NodeJS {
    interface Global {
      mongoCache: {
        conn: {
          client: MongoClient | null;
          db: Db | null;
        }
        promise: Promise<MongoClient> | null;
      };
    }
  }
}
```
@ijjk ijjk added the examples Issue/PR related to examples label Nov 21, 2020
@vercel vercel bot temporarily deployed to Preview November 21, 2020 04:05 Inactive
@ijjk
Copy link
Member

ijjk commented Nov 21, 2020

Stats from current PR

Default Server Mode (Increase detected ⚠️)
General
vercel/next.js canary Zertz/next.js patch-1 Change
buildDuration 9.8s 9.7s -101ms
nodeModulesSize 84.9 MB 84.9 MB
Page Load Tests Overall increase ✓
vercel/next.js canary Zertz/next.js patch-1 Change
/ failed reqs 0 0
/ total time (seconds) 2.237 2.22 -0.02
/ avg req/sec 1117.7 1125.9 +8.2
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.526 1.453 -0.07
/error-in-render avg req/sec 1638.27 1721 +82.73
Client Bundles (main, webpack, commons)
vercel/next.js canary Zertz/next.js patch-1 Change
677f882d2ed8..8b81.js gzip 12.8 kB 12.8 kB
framework.HASH.js gzip 39 kB 39 kB
main-9cc22a8..b04f.js gzip 6.53 kB 6.53 kB
webpack-e067..f178.js gzip 751 B 751 B
Overall change 59 kB 59 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary Zertz/next.js patch-1 Change
polyfills-4b..e242.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary Zertz/next.js patch-1 Change
_app-3b0cf13..85f8.js gzip 1.28 kB 1.28 kB
_error-6f635..c393.js gzip 3.44 kB 3.44 kB
hooks-d4ffc3..9e0f.js gzip 887 B 887 B
index-17468f..5d83.js gzip 227 B 227 B
link-b618194..5477.js gzip 1.61 kB 1.61 kB
routerDirect..924c.js gzip 284 B 284 B
withRouter-7..c13d.js gzip 284 B 284 B
Overall change 8.01 kB 8.01 kB
Client Build Manifests
vercel/next.js canary Zertz/next.js patch-1 Change
_buildManifest.js gzip 321 B 321 B
Overall change 321 B 321 B
Rendered Page Sizes
vercel/next.js canary Zertz/next.js patch-1 Change
index.html gzip 615 B 615 B
link.html gzip 622 B 622 B
withRouter.html gzip 609 B 609 B
Overall change 1.85 kB 1.85 kB

Serverless Mode
General
vercel/next.js canary Zertz/next.js patch-1 Change
buildDuration 11.6s 11.3s -292ms
nodeModulesSize 84.9 MB 84.9 MB
Client Bundles (main, webpack, commons)
vercel/next.js canary Zertz/next.js patch-1 Change
677f882d2ed8..8b81.js gzip 12.8 kB 12.8 kB
framework.HASH.js gzip 39 kB 39 kB
main-9cc22a8..b04f.js gzip 6.53 kB 6.53 kB
webpack-e067..f178.js gzip 751 B 751 B
Overall change 59 kB 59 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary Zertz/next.js patch-1 Change
polyfills-4b..e242.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary Zertz/next.js patch-1 Change
_app-3b0cf13..85f8.js gzip 1.28 kB 1.28 kB
_error-6f635..c393.js gzip 3.44 kB 3.44 kB
hooks-d4ffc3..9e0f.js gzip 887 B 887 B
index-17468f..5d83.js gzip 227 B 227 B
link-b618194..5477.js gzip 1.61 kB 1.61 kB
routerDirect..924c.js gzip 284 B 284 B
withRouter-7..c13d.js gzip 284 B 284 B
Overall change 8.01 kB 8.01 kB
Client Build Manifests
vercel/next.js canary Zertz/next.js patch-1 Change
_buildManifest.js gzip 321 B 321 B
Overall change 321 B 321 B
Serverless bundles
vercel/next.js canary Zertz/next.js patch-1 Change
_error.js 915 kB 915 kB
404.html 2.67 kB 2.67 kB
hooks.html 1.92 kB 1.92 kB
index.js 915 kB 915 kB
link.js 973 kB 973 kB
routerDirect.js 966 kB 966 kB
withRouter.js 966 kB 966 kB
Overall change 4.74 MB 4.74 MB
Commit: c936642

@vercel vercel bot temporarily deployed to Preview November 21, 2020 18:33 Inactive
@vercel vercel bot temporarily deployed to Preview November 21, 2020 18:40 Inactive
@ijjk
Copy link
Member

ijjk commented Nov 21, 2020

Stats from current PR

Default Server Mode (Decrease detected ✓)
General
vercel/next.js canary Zertz/next.js patch-1 Change
buildDuration 9.5s 9.7s ⚠️ +151ms
nodeModulesSize 84.9 MB 84.9 MB
Page Load Tests Overall decrease ⚠️
vercel/next.js canary Zertz/next.js patch-1 Change
/ failed reqs 0 0
/ total time (seconds) 2.161 2.217 ⚠️ +0.06
/ avg req/sec 1156.72 1127.5 ⚠️ -29.22
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.248 1.252 0
/error-in-render avg req/sec 2002.74 1997.02 ⚠️ -5.72
Client Bundles (main, webpack, commons)
vercel/next.js canary Zertz/next.js patch-1 Change
677f882d2ed8..8b81.js gzip 12.8 kB 12.8 kB
framework.HASH.js gzip 39 kB 39 kB
main-9cc22a8..b04f.js gzip 6.53 kB 6.53 kB
webpack-e067..f178.js gzip 751 B 751 B
Overall change 59 kB 59 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary Zertz/next.js patch-1 Change
polyfills-4b..e242.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary Zertz/next.js patch-1 Change
_app-3b0cf13..85f8.js gzip 1.28 kB 1.28 kB
_error-6f635..c393.js gzip 3.44 kB 3.44 kB
hooks-d4ffc3..9e0f.js gzip 887 B 887 B
index-17468f..5d83.js gzip 227 B 227 B
link-b618194..5477.js gzip 1.61 kB 1.61 kB
routerDirect..924c.js gzip 284 B 284 B
withRouter-7..c13d.js gzip 284 B 284 B
Overall change 8.01 kB 8.01 kB
Client Build Manifests
vercel/next.js canary Zertz/next.js patch-1 Change
_buildManifest.js gzip 321 B 321 B
Overall change 321 B 321 B
Rendered Page Sizes
vercel/next.js canary Zertz/next.js patch-1 Change
index.html gzip 615 B 615 B
link.html gzip 622 B 622 B
withRouter.html gzip 609 B 609 B
Overall change 1.85 kB 1.85 kB

Serverless Mode
General
vercel/next.js canary Zertz/next.js patch-1 Change
buildDuration 11.2s 11.6s ⚠️ +370ms
nodeModulesSize 84.9 MB 84.9 MB
Client Bundles (main, webpack, commons)
vercel/next.js canary Zertz/next.js patch-1 Change
677f882d2ed8..8b81.js gzip 12.8 kB 12.8 kB
framework.HASH.js gzip 39 kB 39 kB
main-9cc22a8..b04f.js gzip 6.53 kB 6.53 kB
webpack-e067..f178.js gzip 751 B 751 B
Overall change 59 kB 59 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary Zertz/next.js patch-1 Change
polyfills-4b..e242.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary Zertz/next.js patch-1 Change
_app-3b0cf13..85f8.js gzip 1.28 kB 1.28 kB
_error-6f635..c393.js gzip 3.44 kB 3.44 kB
hooks-d4ffc3..9e0f.js gzip 887 B 887 B
index-17468f..5d83.js gzip 227 B 227 B
link-b618194..5477.js gzip 1.61 kB 1.61 kB
routerDirect..924c.js gzip 284 B 284 B
withRouter-7..c13d.js gzip 284 B 284 B
Overall change 8.01 kB 8.01 kB
Client Build Manifests
vercel/next.js canary Zertz/next.js patch-1 Change
_buildManifest.js gzip 321 B 321 B
Overall change 321 B 321 B
Serverless bundles
vercel/next.js canary Zertz/next.js patch-1 Change
_error.js 915 kB 915 kB
404.html 2.67 kB 2.67 kB
hooks.html 1.92 kB 1.92 kB
index.js 915 kB 915 kB
link.js 973 kB 973 kB
routerDirect.js 966 kB 966 kB
withRouter.js 966 kB 966 kB
Overall change 4.74 MB 4.74 MB
Commit: 35d47b5

@ijjk
Copy link
Member

ijjk commented Nov 21, 2020

Stats from current PR

Default Server Mode (Increase detected ⚠️)
General
vercel/next.js canary Zertz/next.js patch-1 Change
buildDuration 8.6s 8.6s -47ms
nodeModulesSize 84.9 MB 84.9 MB
Page Load Tests Overall increase ✓
vercel/next.js canary Zertz/next.js patch-1 Change
/ failed reqs 0 0
/ total time (seconds) 1.919 1.887 -0.03
/ avg req/sec 1302.87 1324.83 +21.96
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.082 1.079 0
/error-in-render avg req/sec 2309.58 2317.27 +7.69
Client Bundles (main, webpack, commons)
vercel/next.js canary Zertz/next.js patch-1 Change
677f882d2ed8..8b81.js gzip 12.8 kB 12.8 kB
framework.HASH.js gzip 39 kB 39 kB
main-9cc22a8..b04f.js gzip 6.53 kB 6.53 kB
webpack-e067..f178.js gzip 751 B 751 B
Overall change 59 kB 59 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary Zertz/next.js patch-1 Change
polyfills-4b..e242.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary Zertz/next.js patch-1 Change
_app-3b0cf13..85f8.js gzip 1.28 kB 1.28 kB
_error-6f635..c393.js gzip 3.44 kB 3.44 kB
hooks-d4ffc3..9e0f.js gzip 887 B 887 B
index-17468f..5d83.js gzip 227 B 227 B
link-b618194..5477.js gzip 1.61 kB 1.61 kB
routerDirect..924c.js gzip 284 B 284 B
withRouter-7..c13d.js gzip 284 B 284 B
Overall change 8.01 kB 8.01 kB
Client Build Manifests
vercel/next.js canary Zertz/next.js patch-1 Change
_buildManifest.js gzip 321 B 321 B
Overall change 321 B 321 B
Rendered Page Sizes
vercel/next.js canary Zertz/next.js patch-1 Change
index.html gzip 615 B 615 B
link.html gzip 622 B 622 B
withRouter.html gzip 609 B 609 B
Overall change 1.85 kB 1.85 kB

Serverless Mode
General
vercel/next.js canary Zertz/next.js patch-1 Change
buildDuration 9.7s 9.9s ⚠️ +140ms
nodeModulesSize 84.9 MB 84.9 MB
Client Bundles (main, webpack, commons)
vercel/next.js canary Zertz/next.js patch-1 Change
677f882d2ed8..8b81.js gzip 12.8 kB 12.8 kB
framework.HASH.js gzip 39 kB 39 kB
main-9cc22a8..b04f.js gzip 6.53 kB 6.53 kB
webpack-e067..f178.js gzip 751 B 751 B
Overall change 59 kB 59 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary Zertz/next.js patch-1 Change
polyfills-4b..e242.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary Zertz/next.js patch-1 Change
_app-3b0cf13..85f8.js gzip 1.28 kB 1.28 kB
_error-6f635..c393.js gzip 3.44 kB 3.44 kB
hooks-d4ffc3..9e0f.js gzip 887 B 887 B
index-17468f..5d83.js gzip 227 B 227 B
link-b618194..5477.js gzip 1.61 kB 1.61 kB
routerDirect..924c.js gzip 284 B 284 B
withRouter-7..c13d.js gzip 284 B 284 B
Overall change 8.01 kB 8.01 kB
Client Build Manifests
vercel/next.js canary Zertz/next.js patch-1 Change
_buildManifest.js gzip 321 B 321 B
Overall change 321 B 321 B
Serverless bundles
vercel/next.js canary Zertz/next.js patch-1 Change
_error.js 915 kB 915 kB
404.html 2.67 kB 2.67 kB
hooks.html 1.92 kB 1.92 kB
index.js 915 kB 915 kB
link.js 973 kB 973 kB
routerDirect.js 966 kB 966 kB
withRouter.js 966 kB 966 kB
Overall change 4.74 MB 4.74 MB
Commit: 0fcbba5

@Zertz
Copy link
Contributor Author

Zertz commented Nov 21, 2020

Remaining failures don't seem to be related to my changes 👀

@vercel vercel bot temporarily deployed to Preview November 22, 2020 20:43 Inactive
@ijjk
Copy link
Member

ijjk commented Nov 22, 2020

Stats from current PR

Default Server Mode (Increase detected ⚠️)
General
vercel/next.js canary Zertz/next.js patch-1 Change
buildDuration 9.5s 9.6s ⚠️ +16ms
nodeModulesSize 84.9 MB 84.9 MB
Page Load Tests Overall increase ✓
vercel/next.js canary Zertz/next.js patch-1 Change
/ failed reqs 0 0
/ total time (seconds) 2.189 2.285 ⚠️ +0.1
/ avg req/sec 1141.91 1094.21 ⚠️ -47.7
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.468 1.409 -0.06
/error-in-render avg req/sec 1703.25 1773.76 +70.51
Client Bundles (main, webpack, commons)
vercel/next.js canary Zertz/next.js patch-1 Change
677f882d2ed8..8b81.js gzip 12.8 kB 12.8 kB
framework.HASH.js gzip 39 kB 39 kB
main-9cc22a8..b04f.js gzip 6.53 kB 6.53 kB
webpack-e067..f178.js gzip 751 B 751 B
Overall change 59 kB 59 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary Zertz/next.js patch-1 Change
polyfills-4b..e242.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary Zertz/next.js patch-1 Change
_app-3b0cf13..85f8.js gzip 1.28 kB 1.28 kB
_error-6f635..c393.js gzip 3.44 kB 3.44 kB
hooks-d4ffc3..9e0f.js gzip 887 B 887 B
index-17468f..5d83.js gzip 227 B 227 B
link-b618194..5477.js gzip 1.61 kB 1.61 kB
routerDirect..924c.js gzip 284 B 284 B
withRouter-7..c13d.js gzip 284 B 284 B
Overall change 8.01 kB 8.01 kB
Client Build Manifests
vercel/next.js canary Zertz/next.js patch-1 Change
_buildManifest.js gzip 321 B 321 B
Overall change 321 B 321 B
Rendered Page Sizes
vercel/next.js canary Zertz/next.js patch-1 Change
index.html gzip 615 B 615 B
link.html gzip 622 B 622 B
withRouter.html gzip 609 B 609 B
Overall change 1.85 kB 1.85 kB

Serverless Mode
General
vercel/next.js canary Zertz/next.js patch-1 Change
buildDuration 10.8s 10.8s ⚠️ +38ms
nodeModulesSize 84.9 MB 84.9 MB
Client Bundles (main, webpack, commons)
vercel/next.js canary Zertz/next.js patch-1 Change
677f882d2ed8..8b81.js gzip 12.8 kB 12.8 kB
framework.HASH.js gzip 39 kB 39 kB
main-9cc22a8..b04f.js gzip 6.53 kB 6.53 kB
webpack-e067..f178.js gzip 751 B 751 B
Overall change 59 kB 59 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary Zertz/next.js patch-1 Change
polyfills-4b..e242.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary Zertz/next.js patch-1 Change
_app-3b0cf13..85f8.js gzip 1.28 kB 1.28 kB
_error-6f635..c393.js gzip 3.44 kB 3.44 kB
hooks-d4ffc3..9e0f.js gzip 887 B 887 B
index-17468f..5d83.js gzip 227 B 227 B
link-b618194..5477.js gzip 1.61 kB 1.61 kB
routerDirect..924c.js gzip 284 B 284 B
withRouter-7..c13d.js gzip 284 B 284 B
Overall change 8.01 kB 8.01 kB
Client Build Manifests
vercel/next.js canary Zertz/next.js patch-1 Change
_buildManifest.js gzip 321 B 321 B
Overall change 321 B 321 B
Serverless bundles
vercel/next.js canary Zertz/next.js patch-1 Change
_error.js 915 kB 915 kB
404.html 2.67 kB 2.67 kB
hooks.html 1.92 kB 1.92 kB
index.js 915 kB 915 kB
link.js 973 kB 973 kB
routerDirect.js 966 kB 966 kB
withRouter.js 966 kB 966 kB
Overall change 4.74 MB 4.74 MB
Commit: c64201a

@vercel vercel bot temporarily deployed to Preview November 23, 2020 19:17 Inactive
lfades
lfades previously approved these changes Nov 24, 2020
Copy link
Member

@lfades lfades left a comment

Choose a reason for hiding this comment

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

@Zertz LGTM. Thank you!

@vercel vercel bot temporarily deployed to Preview December 28, 2020 19:32 Inactive
@Timer Timer merged commit e6c3518 into vercel:canary Dec 28, 2020
@Zertz Zertz deleted the patch-1 branch December 29, 2020 00:03
@ghost
Copy link

ghost commented Aug 4, 2021

let cached = global.mongo
I'm getting the following on a fresh install: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.ts(7017)

edit: "noImplicitAny": false is a dirty hack

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
examples Issue/PR related to examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants