feat: database server-rpc

This commit is contained in:
arashsheyda
2023-04-21 12:07:50 +03:00
parent 85f1547150
commit c8b8583ae9
3 changed files with 60 additions and 1 deletions

View File

@ -0,0 +1,47 @@
import { logger } from '@nuxt/kit'
import { ObjectId } from 'mongodb'
import { connection as MongooseConnection, connect } from 'mongoose'
import type { NuxtDevtoolsServerContext, ServerFunctions } from '../types'
export function setupDatabaseRPC({ nuxt }: NuxtDevtoolsServerContext): any {
// TODO:
connect('mongodb://127.0.0.1:27017/arcane')
return {
async createCollection(name: string) {
return await MongooseConnection.db.createCollection(name)
},
async listCollections() {
return await MongooseConnection.db.listCollections().toArray()
},
async getCollection(name: string) {
return MongooseConnection.db.collection(name)
},
async dropCollection(name: string) {
return await MongooseConnection.db.collection(name).drop()
},
async createDocument(collection: string, data: any) {
return await MongooseConnection.db.collection(collection).insertOne(data)
},
async listDocuments(collection: string) {
return await MongooseConnection.db.collection(collection).find().toArray()
},
async getDocument(collection: string, document: {}) {
return await MongooseConnection.db.collection(collection).findOne({ document })
},
async updateDocument(collection: string, data: any) {
const { _id, ...rest } = data
try {
return await MongooseConnection.db.collection(collection).findOneAndUpdate({ _id: new ObjectId(_id) }, { $set: rest })
}
catch (error) {
logger.log(error)
return error
}
},
async deleteDocument(collection: string, id: string) {
return await MongooseConnection.db.collection(collection).deleteOne({ _id: new ObjectId(id) })
},
} satisfies Partial<ServerFunctions>
}

View File

@ -7,6 +7,7 @@ import type { ChannelOptions } from 'birpc'
import { parse, stringify } from 'flatted' import { parse, stringify } from 'flatted'
import type { Nuxt } from 'nuxt/schema' import type { Nuxt } from 'nuxt/schema'
import type { ClientFunctions, ModuleOptions, NuxtDevtoolsServerContext, ServerFunctions } from '../types' import type { ClientFunctions, ModuleOptions, NuxtDevtoolsServerContext, ServerFunctions } from '../types'
import { setupDatabaseRPC } from './database'
export function setupRPC(nuxt: Nuxt, options: ModuleOptions): any { export function setupRPC(nuxt: Nuxt, options: ModuleOptions): any {
const serverFunctions = {} as ServerFunctions const serverFunctions = {} as ServerFunctions
@ -61,7 +62,7 @@ export function setupRPC(nuxt: Nuxt, options: ModuleOptions): any {
nuxt.devtools = ctx nuxt.devtools = ctx
Object.assign(serverFunctions, { Object.assign(serverFunctions, {
// TODO: add rpc ...setupDatabaseRPC(ctx),
} satisfies Partial<ServerFunctions>) } satisfies Partial<ServerFunctions>)
const wsClients = new Set<WebSocket>() const wsClients = new Set<WebSocket>()

View File

@ -1,5 +1,16 @@
export interface ServerFunctions { export interface ServerFunctions {
// collections
createCollection(name: string): Promise<any>
listCollections(): Promise<any> listCollections(): Promise<any>
getCollection(name: string): Promise<any>
dropCollection(name: string): Promise<any>
// documents
createDocument(collection: string, data: any): Promise<any>
listDocuments(collection: string): Promise<any>
getDocument(collection: string, id: string): Promise<any>
updateDocument(collection: string, data: any): Promise<any>
deleteDocument(collection: string, id: string): Promise<any>
} }
export interface ClientFunctions { export interface ClientFunctions {