mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 11:03:44 +00:00
Merge pull request #7 from jcreek/4-add-a-database-connection
chore(#4): Add a database connection to mongodb
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
MONGO_URL=""
|
||||||
Generated
+735
-79
File diff suppressed because it is too large
Load Diff
+9
-2
@@ -14,7 +14,7 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@sveltejs/adapter-auto": "^3.0.0",
|
"@sveltejs/adapter-auto": "^3.0.0",
|
||||||
"@sveltejs/adapter-netlify": "^4.1.0",
|
"@sveltejs/adapter-netlify": "^4.1.0",
|
||||||
"@sveltejs/kit": "^2.0.0",
|
"@sveltejs/kit": "^2.0.6",
|
||||||
"@sveltejs/vite-plugin-svelte": "^3.0.0",
|
"@sveltejs/vite-plugin-svelte": "^3.0.0",
|
||||||
"@types/eslint": "^8.56.0",
|
"@types/eslint": "^8.56.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
||||||
@@ -30,5 +30,12 @@
|
|||||||
"typescript": "^5.0.0",
|
"typescript": "^5.0.0",
|
||||||
"vite": "^5.0.3"
|
"vite": "^5.0.3"
|
||||||
},
|
},
|
||||||
"type": "module"
|
"type": "module",
|
||||||
|
"dependencies": {
|
||||||
|
"mongoose": "^8.0.3",
|
||||||
|
"nanoid": "^5.0.4"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18.13.0"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+7
-7
@@ -1,13 +1,13 @@
|
|||||||
// See https://kit.svelte.dev/docs/types#app
|
// See https://kit.svelte.dev/docs/types#app
|
||||||
// for information about these interfaces
|
// for information about these interfaces
|
||||||
declare global {
|
declare global {
|
||||||
namespace App {
|
namespace App {
|
||||||
// interface Error {}
|
// interface Error {}
|
||||||
// interface Locals {}
|
// interface Locals {}
|
||||||
// interface PageData {}
|
// interface PageData {}
|
||||||
// interface PageState {}
|
// interface PageState {}
|
||||||
// interface Platform {}
|
// interface Platform {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export {};
|
export {};
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import type { Handle } from '@sveltejs/kit';
|
||||||
|
import { dbConnect } from '$lib/utils/db';
|
||||||
|
|
||||||
|
await dbConnect();
|
||||||
|
|
||||||
|
export const handle: Handle = async ({ event, resolve }) => {
|
||||||
|
const response = await resolve(event);
|
||||||
|
return response;
|
||||||
|
};
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import mongoose from 'mongoose';
|
||||||
|
import { getEnv } from '$lib/utils/env';
|
||||||
|
const env = getEnv();
|
||||||
|
|
||||||
|
/*
|
||||||
|
0 - disconnected
|
||||||
|
1 - connected
|
||||||
|
2 - connecting
|
||||||
|
3 - disconnecting
|
||||||
|
4 - uninitialized
|
||||||
|
*/
|
||||||
|
let mongoConnectionState = 0;
|
||||||
|
|
||||||
|
export const dbConnect = async () => {
|
||||||
|
if (mongoConnectionState === 1) {
|
||||||
|
console.log('connection established');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mongoose.connections.length > 0) {
|
||||||
|
mongoConnectionState = mongoose.connections[0].readyState;
|
||||||
|
if (mongoConnectionState === 1) {
|
||||||
|
console.log('using existing connection');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await mongoose.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await mongoose.connect(env.MONGO_URL, {
|
||||||
|
serverApi: { version: '1', strict: true, deprecationErrors: true }
|
||||||
|
});
|
||||||
|
await mongoose.connection.db.admin().command({ ping: 1 });
|
||||||
|
console.log('Pinged your deployment. You successfully connected to MongoDB!');
|
||||||
|
mongoConnectionState = 1;
|
||||||
|
} finally {
|
||||||
|
// Ensures that the client will close when you finish/error
|
||||||
|
await mongoose.disconnect();
|
||||||
|
}
|
||||||
|
mongoConnectionState = 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const dbDisconnect = async () => {
|
||||||
|
if (process.env.NODE_ENV === 'development') return;
|
||||||
|
if (mongoConnectionState === 0) return;
|
||||||
|
|
||||||
|
await mongoose.disconnect();
|
||||||
|
mongoConnectionState = 0;
|
||||||
|
console.log('disconnected from mongodb');
|
||||||
|
};
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
export function getEnv() {
|
||||||
|
return process.env.hasOwnProperty('MONGO_URL')
|
||||||
|
? process.env
|
||||||
|
: {
|
||||||
|
MONGO_URL: import.meta.env.VITE_MONGO_URL
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user