|
| 1 | +import type { DevtoolsServerContext, RPCError, ServerFunctions } from '../types' |
1 | 2 | import mongoose from 'mongoose' |
2 | | -import type { DevtoolsServerContext, ServerFunctions } from '../types' |
| 3 | +import type { Document } from 'mongodb' |
3 | 4 |
|
4 | 5 | // eslint-disable-next-line no-empty-pattern |
5 | 6 | export function setupDatabaseRPC({}: DevtoolsServerContext) { |
6 | 7 | return { |
7 | | - async readyState() { |
8 | | - return mongoose.connection.readyState |
| 8 | + async connectionInfo() { |
| 9 | + const conn = mongoose.connection |
| 10 | + const client = conn.getClient?.() |
| 11 | + |
| 12 | + return { |
| 13 | + connectionState: conn.readyState, |
| 14 | + authenticated: !!conn.user, |
| 15 | + |
| 16 | + name: mongoose.connection.name, |
| 17 | + host: mongoose.connection.host, |
| 18 | + |
| 19 | + mongooseVersion: mongoose.version, |
| 20 | + driverVersion: client?.options?.driverInfo?.version, |
| 21 | + |
| 22 | + timestamps: { |
| 23 | + now: Date.now(), |
| 24 | + }, |
| 25 | + } |
9 | 26 | }, |
10 | 27 | async createCollection(name: string) { |
11 | 28 | try { |
12 | | - return await mongoose.connection.db?.createCollection(name) |
| 29 | + const db = mongoose.connection.db |
| 30 | + if (!db) { |
| 31 | + return createError(new Error('Database not connected')) |
| 32 | + } |
| 33 | + return await db.createCollection(name) |
13 | 34 | } |
14 | 35 | catch (error) { |
15 | | - return ErrorIT(error) |
| 36 | + return createError(error) |
16 | 37 | } |
17 | 38 | }, |
18 | 39 | async listCollections() { |
19 | 40 | try { |
20 | | - return await mongoose.connection.db?.listCollections().toArray() |
| 41 | + const db = mongoose.connection.db |
| 42 | + if (!db) { |
| 43 | + return createError(new Error('Database not connected')) |
| 44 | + } |
| 45 | + return await db.listCollections().toArray() |
21 | 46 | } |
22 | 47 | catch (error) { |
23 | | - return ErrorIT(error) |
| 48 | + return createError(error) |
24 | 49 | } |
25 | 50 | }, |
26 | 51 | async getCollection(name: string) { |
27 | 52 | try { |
28 | | - return await mongoose.connection.db?.collection(name).findOne() |
| 53 | + const db = mongoose.connection.db |
| 54 | + if (!db) { |
| 55 | + return createError(new Error('Database not connected')) |
| 56 | + } |
| 57 | + return await db.collection(name).findOne() |
29 | 58 | } |
30 | 59 | catch (error) { |
31 | | - return ErrorIT(error) |
| 60 | + return createError(error) |
32 | 61 | } |
33 | 62 | }, |
34 | 63 | async dropCollection(name: string) { |
35 | 64 | try { |
36 | | - return await mongoose.connection.db?.dropCollection(name) |
| 65 | + const db = mongoose.connection.db |
| 66 | + if (!db) { |
| 67 | + return createError(new Error('Database not connected')) |
| 68 | + } |
| 69 | + return await db.dropCollection(name) |
37 | 70 | } |
38 | 71 | catch (error) { |
39 | | - return ErrorIT(error) |
| 72 | + return createError(error) |
40 | 73 | } |
41 | 74 | }, |
42 | 75 |
|
43 | 76 | async createDocument(collection: string, data: any) { |
44 | 77 | const { _id, ...rest } = data |
45 | 78 | try { |
46 | | - return await mongoose.connection.db?.collection(collection).insertOne(rest) |
| 79 | + const db = mongoose.connection.db |
| 80 | + if (!db) { |
| 81 | + return createError(new Error('Database not connected')) |
| 82 | + } |
| 83 | + return await db.collection(collection).insertOne(rest) |
47 | 84 | } |
48 | 85 | catch (error: any) { |
49 | | - return ErrorIT(error) |
| 86 | + return createError(error) |
50 | 87 | } |
51 | 88 | }, |
52 | 89 | async countDocuments(collection: string) { |
53 | 90 | try { |
54 | | - return await mongoose.connection.db?.collection(collection).countDocuments() |
| 91 | + const db = mongoose.connection.db |
| 92 | + if (!db) { |
| 93 | + return createError(new Error('Database not connected')) |
| 94 | + } |
| 95 | + return await db.collection(collection).countDocuments() |
55 | 96 | } |
56 | 97 | catch (error) { |
57 | | - return ErrorIT(error) |
| 98 | + return createError(error) |
58 | 99 | } |
59 | 100 | }, |
60 | 101 | async listDocuments(collection: string, options: { page: number, limit: number } = { page: 1, limit: 10 }) { |
61 | 102 | const skip = (options.page - 1) * options.limit |
62 | | - const cursor = mongoose.connection.db?.collection(collection).find().skip(skip) |
| 103 | + const db = mongoose.connection.db |
| 104 | + if (!db) { |
| 105 | + return createError(new Error('Database not connected')) |
| 106 | + } |
| 107 | + const cursor = db.collection(collection).find().skip(skip) |
63 | 108 | if (options.limit !== 0) |
64 | | - cursor?.limit(options.limit) |
65 | | - return await cursor?.toArray() |
| 109 | + cursor.limit(options.limit) |
| 110 | + return await cursor.toArray() |
66 | 111 | }, |
67 | | - async getDocument(collection: string, document: any) { |
| 112 | + async getDocument(collection: string, id: string) { |
68 | 113 | try { |
69 | | - return await mongoose.connection.db?.collection(collection).findOne({ document }) |
| 114 | + const db = mongoose.connection.db |
| 115 | + if (!db) { |
| 116 | + return createError(new Error('Database not connected')) |
| 117 | + } |
| 118 | + return await db.collection(collection).findOne({ _id: new mongoose.Types.ObjectId(id) }) |
70 | 119 | } |
71 | 120 | catch (error) { |
72 | | - return ErrorIT(error) |
| 121 | + return createError(error) |
73 | 122 | } |
74 | 123 | }, |
75 | | - async updateDocument(collection: string, data: any) { |
| 124 | + async updateDocument(collection: string, data: { _id: string } & Document) { |
76 | 125 | const { _id, ...rest } = data |
77 | 126 | try { |
78 | | - return await mongoose.connection.db?.collection(collection).findOneAndUpdate({ _id: new mongoose.Types.ObjectId(_id) }, { $set: rest }) |
| 127 | + const db = mongoose.connection.db |
| 128 | + if (!db) { |
| 129 | + return createError(new Error('Database not connected')) |
| 130 | + } |
| 131 | + return await db.collection(collection).updateOne({ _id: new mongoose.Types.ObjectId(_id) }, { $set: rest }) |
79 | 132 | } |
80 | 133 | catch (error) { |
81 | | - return ErrorIT(error) |
| 134 | + return createError(error) |
82 | 135 | } |
83 | 136 | }, |
84 | 137 | async deleteDocument(collection: string, id: string) { |
85 | 138 | try { |
86 | | - return await mongoose.connection.db?.collection(collection).deleteOne({ _id: new mongoose.Types.ObjectId(id) }) |
| 139 | + const db = mongoose.connection.db |
| 140 | + if (!db) { |
| 141 | + return createError(new Error('Database not connected')) |
| 142 | + } |
| 143 | + return await db.collection(collection).deleteOne({ _id: new mongoose.Types.ObjectId(id) }) |
87 | 144 | } |
88 | 145 | catch (error) { |
89 | | - return ErrorIT(error) |
| 146 | + return createError(error) |
90 | 147 | } |
91 | 148 | }, |
92 | 149 | } satisfies Partial<ServerFunctions> |
93 | 150 | } |
94 | 151 |
|
95 | | -function ErrorIT(error: any) { |
| 152 | +function createError(error: any): RPCError { |
96 | 153 | return { |
97 | 154 | error: { |
98 | | - message: error?.message, |
99 | | - code: error?.code, |
| 155 | + message: error?.message || 'An unknown error occurred', |
| 156 | + code: error?.code || 'UNKNOWN_ERROR', |
100 | 157 | }, |
101 | 158 | } |
102 | 159 | } |
0 commit comments