randydev commited on
Commit
54c3ff7
·
verified ·
1 Parent(s): 16aa298

Update startup/lifestyle.js

Browse files
Files changed (1) hide show
  1. startup/lifestyle.js +20 -9
startup/lifestyle.js CHANGED
@@ -8,21 +8,32 @@ import {
8
  import mongoose from 'mongoose';
9
  const port = 7860
10
 
 
 
 
11
  const startup = async () => {
12
  try {
13
  await mongoose.connect(`mongodb+srv://${UsernameDB}:${PasswordDB}@cluster0.1asx6h1.mongodb.net/AkenoXJs?retryWrites=true&w=majority`);
14
  console.log("Mongoose connected successfully.");
15
 
16
- const collection = mongoose.connection.collection("ApiKey");
17
- try {
18
- await collection.dropIndex("expiresAt_1");
19
- console.log("Dropped TTL index on expiresAt.");
20
- } catch (err) {
21
- if (err.codeName === "IndexNotFound") {
22
- console.log("No TTL index found, skipping dropIndex.");
23
- } else {
24
- console.error("Error dropping index:", err.message);
 
 
 
 
 
 
25
  }
 
 
26
  }
27
 
28
  const dbClient = new Database("AkenoXJs");
 
8
  import mongoose from 'mongoose';
9
  const port = 7860
10
 
11
+ import mongoose from "mongoose";
12
+ import ApiKey from "../models.js";
13
+
14
  const startup = async () => {
15
  try {
16
  await mongoose.connect(`mongodb+srv://${UsernameDB}:${PasswordDB}@cluster0.1asx6h1.mongodb.net/AkenoXJs?retryWrites=true&w=majority`);
17
  console.log("Mongoose connected successfully.");
18
 
19
+ const db = mongoose.connection.db;
20
+ const collections = await db.listCollections().toArray();
21
+ const collectionNames = collections.map(col => col.name);
22
+
23
+ if (collectionNames.includes("ApiKey")) {
24
+ const collection = db.collection("ApiKey");
25
+ try {
26
+ await collection.dropIndex("expiresAt_1");
27
+ console.log("Dropped TTL index on expiresAt.");
28
+ } catch (err) {
29
+ if (err.codeName === "IndexNotFound") {
30
+ console.log("No TTL index found, skipping dropIndex.");
31
+ } else {
32
+ console.error("Error dropping index:", err.message);
33
+ }
34
  }
35
+ } else {
36
+ console.log("Collection 'ApiKey' does not exist yet. Skipping dropIndex.");
37
  }
38
 
39
  const dbClient = new Database("AkenoXJs");