-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
70 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { MongoClient } from 'mongodb' | ||
|
||
const uri = ( | ||
'DATABASE_URL' in process.env | ||
? process.env.DATABASE_URL | ||
: 'mongodb://localhost' | ||
) as string | ||
if (typeof uri !== 'string') { | ||
throw Error('Unable to connect to DB due to missing DATABASE_URL') | ||
} | ||
|
||
declare global { | ||
var _mongoClientPromise: Promise<MongoClient> | ||
} | ||
|
||
class Singleton { | ||
private static _instance: Singleton | ||
private client: MongoClient | ||
private clientPromise: Promise<MongoClient> | ||
private constructor() { | ||
this.client = new MongoClient(uri, { | ||
maxPoolSize: 5, | ||
directConnection: true, | ||
}) | ||
this.clientPromise = this.client.connect() | ||
if (process.env.NODE_ENV === 'development') { | ||
// In development mode, use a global variable so that the value | ||
// is preserved across module reloads caused by HMR (Hot Module Replacement). | ||
global._mongoClientPromise = this.clientPromise | ||
} | ||
} | ||
|
||
public static get instance() { | ||
if (!this._instance) { | ||
this._instance = new Singleton() | ||
} | ||
return this._instance.clientPromise | ||
} | ||
} | ||
const clientPromise = Singleton.instance | ||
|
||
// Export a module-scoped MongoClient promise. By doing this in a | ||
// separate module, the client can be shared across functions. | ||
export default clientPromise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters