Skip to content

Commit 3f27d6a

Browse files
committed
fix(rpc): update types
1 parent dde3431 commit 3f27d6a

File tree

2 files changed

+140
-41
lines changed

2 files changed

+140
-41
lines changed

src/rpc/database.ts

Lines changed: 86 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,159 @@
1+
import type { DevtoolsServerContext, RPCError, ServerFunctions } from '../types'
12
import mongoose from 'mongoose'
2-
import type { DevtoolsServerContext, ServerFunctions } from '../types'
3+
import type { Document } from 'mongodb'
34

45
// eslint-disable-next-line no-empty-pattern
56
export function setupDatabaseRPC({}: DevtoolsServerContext) {
67
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+
}
926
},
1027
async createCollection(name: string) {
1128
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)
1334
}
1435
catch (error) {
15-
return ErrorIT(error)
36+
return createError(error)
1637
}
1738
},
1839
async listCollections() {
1940
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()
2146
}
2247
catch (error) {
23-
return ErrorIT(error)
48+
return createError(error)
2449
}
2550
},
2651
async getCollection(name: string) {
2752
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()
2958
}
3059
catch (error) {
31-
return ErrorIT(error)
60+
return createError(error)
3261
}
3362
},
3463
async dropCollection(name: string) {
3564
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)
3770
}
3871
catch (error) {
39-
return ErrorIT(error)
72+
return createError(error)
4073
}
4174
},
4275

4376
async createDocument(collection: string, data: any) {
4477
const { _id, ...rest } = data
4578
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)
4784
}
4885
catch (error: any) {
49-
return ErrorIT(error)
86+
return createError(error)
5087
}
5188
},
5289
async countDocuments(collection: string) {
5390
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()
5596
}
5697
catch (error) {
57-
return ErrorIT(error)
98+
return createError(error)
5899
}
59100
},
60101
async listDocuments(collection: string, options: { page: number, limit: number } = { page: 1, limit: 10 }) {
61102
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)
63108
if (options.limit !== 0)
64-
cursor?.limit(options.limit)
65-
return await cursor?.toArray()
109+
cursor.limit(options.limit)
110+
return await cursor.toArray()
66111
},
67-
async getDocument(collection: string, document: any) {
112+
async getDocument(collection: string, id: string) {
68113
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) })
70119
}
71120
catch (error) {
72-
return ErrorIT(error)
121+
return createError(error)
73122
}
74123
},
75-
async updateDocument(collection: string, data: any) {
124+
async updateDocument(collection: string, data: { _id: string } & Document) {
76125
const { _id, ...rest } = data
77126
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 })
79132
}
80133
catch (error) {
81-
return ErrorIT(error)
134+
return createError(error)
82135
}
83136
},
84137
async deleteDocument(collection: string, id: string) {
85138
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) })
87144
}
88145
catch (error) {
89-
return ErrorIT(error)
146+
return createError(error)
90147
}
91148
},
92149
} satisfies Partial<ServerFunctions>
93150
}
94151

95-
function ErrorIT(error: any) {
152+
function createError(error: any): RPCError {
96153
return {
97154
error: {
98-
message: error?.message,
99-
code: error?.code,
155+
message: error?.message || 'An unknown error occurred',
156+
code: error?.code || 'UNKNOWN_ERROR',
100157
},
101158
}
102159
}

src/types/index.ts

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
import type { Nuxt } from 'nuxt/schema'
22
import type { WebSocketServer } from 'vite'
33
import type { ModuleOptions } from '../module'
4+
import type { ConnectionStates } from 'mongoose'
5+
import type {
6+
Collection as MCollection,
7+
Document,
8+
WithId,
9+
InsertOneResult,
10+
DeleteResult,
11+
UpdateResult,
12+
} from 'mongodb'
413

514
export interface ServerFunctions {
615
getOptions(): ModuleOptions
716

17+
connectionInfo(): Promise<DatabaseConnectionInfo>
18+
819
// Database - collections
9-
readyState(): Promise<any>
10-
createCollection(name: string): Promise<any>
11-
listCollections(): Promise<any>
12-
getCollection(name: string): Promise<any>
13-
dropCollection(name: string): Promise<any>
20+
createCollection(name: string): Promise<RPCResult<MCollection<Document>>>
21+
listCollections(): Promise<RPCResult<Array<Document>>>
22+
getCollection(name: string): Promise<RPCResult<WithId<Document> | null>>
23+
dropCollection(name: string): Promise<RPCResult<boolean>>
1424

1525
// Database - documents
16-
createDocument(collection: string, data: any): Promise<any>
17-
countDocuments(collection: string): Promise<any>
18-
listDocuments(collection: string, options: any): Promise<any>
19-
getDocument(collection: string, id: string): Promise<any>
20-
updateDocument(collection: string, data: any): Promise<any>
21-
deleteDocument(collection: string, id: string): Promise<any>
26+
createDocument(collection: string, data: Document): Promise<RPCResult<InsertOneResult<Document>>>
27+
countDocuments(collection: string): Promise<RPCResult<number>>
28+
listDocuments(collection: string, options: PaginationOptions): Promise<RPCResult<Array<WithId<Document>>>>
29+
getDocument(collection: string, id: string): Promise<RPCResult<WithId<Document> | null>>
30+
updateDocument(collection: string, data: { _id: string } & Document): Promise<RPCResult<UpdateResult>>
31+
deleteDocument(collection: string, id: string): Promise<RPCResult<DeleteResult>>
2232

2333
// Resource - api-routes & models
24-
generateResource(collection: Collection, resources: Resource[]): Promise<any>
34+
generateResource(collection: MCollection, resources: Resource[]): Promise<any>
2535
resourceSchema(collection: string): Promise<any>
2636

2737
reset(): void
@@ -46,3 +56,35 @@ export interface Resource {
4656
type: 'index' | 'create' | 'show' | 'put' | 'delete'
4757
by?: string
4858
}
59+
60+
export interface DatabaseConnectionInfo {
61+
connectionState: ConnectionStates
62+
63+
name?: string
64+
host?: string
65+
hosts?: string[]
66+
67+
authenticated: boolean
68+
69+
mongooseVersion: string
70+
driverVersion?: string
71+
72+
timestamps: {
73+
now: number
74+
connectedAt?: number
75+
}
76+
}
77+
78+
export interface RPCError {
79+
error: {
80+
message?: string
81+
code?: number | string
82+
}
83+
}
84+
85+
export interface PaginationOptions {
86+
page: number
87+
limit: number
88+
}
89+
90+
export type RPCResult<T> = T | RPCError

0 commit comments

Comments
 (0)