|
1 | | -import { sleep } from "./utils/sleep"; |
2 | | -import { connectToDatabase } from "./database"; |
| 1 | +import * as TheQueries from "./query/queries"; |
3 | 2 | import { withTiming } from "./utils/timer"; |
4 | | -import { Count } from "./query/count"; |
5 | | -import { Find } from "./query/find"; |
6 | | -import { FindOne } from "./query/findOne"; |
7 | | -import { FindMany } from "./query/findMany"; |
8 | | -import { UpdateOne } from "./query/updateOne"; |
9 | | -import { InsertOne } from "./query/insertOne"; |
10 | | -import { BulkWrite } from "./query/bulkWrite"; |
11 | | -import { DeleteOne } from "./query/deleteOne"; |
12 | | -import { InsertMany } from "./query/insertMany"; |
13 | | -import { UpdateMany } from "./query/updateMany"; |
14 | | -import { ReplaceOne } from "./query/replaceOne"; |
15 | | -import { DeleteMany } from "./query/deleteMany"; |
16 | | -import { CreateIndex } from "./query/createIndex"; |
17 | | -import { FindOneAndDelete } from "./query/findOneAndDelete"; |
18 | | -import { FindAll } from "./query/findAll"; |
19 | 3 |
|
20 | 4 | export const credentials: string = GetConvar("mongoCredentials", "null"); |
21 | 5 | export const resourceName: string = GetCurrentResourceName(); |
22 | 6 |
|
23 | | -async function initializeMongo() { |
24 | | - while (true) { |
25 | | - try { |
26 | | - await connectToDatabase(); |
27 | | - console.log("^2MongoDB connection established^7"); |
28 | | - break; // exit loop on successful connection |
29 | | - } catch (error: any) { |
30 | | - console.error(`^1MongoDB connection failed: ${error.message}, retrying in 30 seconds...^7`); |
31 | | - await sleep(30000); |
32 | | - } |
| 7 | +// Wrap each query function with timing |
| 8 | +for (const [name, fn] of Object.entries(TheQueries)) { |
| 9 | + if (typeof fn === "function") { |
| 10 | + exports(name, async (...args: any[]) => { |
| 11 | + return withTiming(() => fn(...args), { label: name }); |
| 12 | + }); |
33 | 13 | } |
34 | 14 | } |
35 | 15 |
|
36 | | -// Run the MongoDB initialization on resource start |
37 | | -initializeMongo().catch((err) => { |
38 | | - console.error("^1Unhandled error during MongoDB initialization:^7", err); |
39 | | -}); |
40 | | - |
41 | | -// Export all query functions for FiveM usage |
42 | | -const queries = { |
43 | | - Count, |
44 | | - Find, |
45 | | - FindOne, |
46 | | - FindMany, |
47 | | - FindAll, |
48 | | - FindOneAndDelete, |
49 | | - InsertOne, |
50 | | - InsertMany, |
51 | | - UpdateOne, |
52 | | - UpdateMany, |
53 | | - ReplaceOne, |
54 | | - DeleteOne, |
55 | | - DeleteMany, |
56 | | - BulkWrite, |
57 | | - CreateIndex, |
58 | | -}; |
59 | | - |
60 | | -// Export each with timing |
61 | | -for (const [name, fn] of Object.entries(queries)) { |
62 | | - exports(name, (...args: any[]) => withTiming(name, () => fn(...args))); |
63 | | -} |
64 | | - |
| 16 | +// Optional: log ready message |
| 17 | +setTimeout(() => { |
| 18 | + console.log("^2MongoDB module loaded and ready^7"); |
| 19 | +}, 500); |
0 commit comments