move over to database service cause I forgot I made that
This commit is contained in:
@@ -9,38 +9,11 @@ const address = `0.0.0.0:${port}`
|
|||||||
|
|
||||||
const app = new Server()
|
const app = new Server()
|
||||||
|
|
||||||
const client = new MongoClient('mongodb://rushvault:rushvault@mongo:27017/')
|
const databaseService = new DatabaseService('mongodb://rushvault:rushvault@mongo:27017/', 'rush-vault')
|
||||||
|
const characterService = new CharacterService(databaseService)
|
||||||
// initCharacterService(app)
|
|
||||||
|
|
||||||
const characterService = new CharacterService(client, "rush-vault")
|
|
||||||
|
|
||||||
characterService.addToServer(app)
|
characterService.addToServer(app)
|
||||||
|
|
||||||
// async function testDb() {
|
|
||||||
// try {
|
|
||||||
// // await client.connect()
|
|
||||||
// const database = client.db('test')
|
|
||||||
// const potato = database.collection('potato')
|
|
||||||
|
|
||||||
// await potato.insertOne({ name: 'potato', potato: 'potato' })
|
|
||||||
// } finally {
|
|
||||||
// await client.close()
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
app.bindAsync(address, ServerCredentials.createInsecure(), () => {
|
app.bindAsync(address, ServerCredentials.createInsecure(), () => {
|
||||||
// await testDb().catch(console.dir)
|
|
||||||
|
|
||||||
// let db = new DatabaseService(
|
|
||||||
// 'mongodb://rushvault:rushvault@mongo:27017/',
|
|
||||||
// 'rush-vault',
|
|
||||||
// )
|
|
||||||
// console.log(await db.insertCharacter({
|
|
||||||
// playerName: 'badger',
|
|
||||||
// characterName: 'potato',
|
|
||||||
// characterAlias: ['spud', 'tuber'],
|
|
||||||
// }))
|
|
||||||
|
|
||||||
console.log(`Starting server at ${address}`)
|
console.log(`Starting server at ${address}`)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { Server, ServerUnaryCall, sendUnaryData } from '@grpc/grpc-js'
|
|||||||
import { ReflectionService } from '@grpc/reflection'
|
import { ReflectionService } from '@grpc/reflection'
|
||||||
import { loadSync } from '@grpc/proto-loader'
|
import { loadSync } from '@grpc/proto-loader'
|
||||||
import { MongoClient, Collection, Db } from 'mongodb'
|
import { MongoClient, Collection, Db } from 'mongodb'
|
||||||
|
import { DatabaseService } from './database'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Character,
|
Character,
|
||||||
@@ -17,12 +18,13 @@ import {
|
|||||||
export class CharacterService {
|
export class CharacterService {
|
||||||
reflectionService: ReflectionService
|
reflectionService: ReflectionService
|
||||||
characterManager: ICharacterManager
|
characterManager: ICharacterManager
|
||||||
|
databaseService: DatabaseService
|
||||||
|
|
||||||
mongoClient: MongoClient
|
// mongoClient: MongoClient
|
||||||
db: Db
|
// db: Db
|
||||||
characterCollection: Collection
|
// characterCollection: Collection
|
||||||
|
|
||||||
constructor(mongoClient: MongoClient, dbName: string) {
|
constructor(databaseService: DatabaseService) {
|
||||||
this.reflectionService = new ReflectionService(
|
this.reflectionService = new ReflectionService(
|
||||||
loadSync('./src/proto/character.proto'),
|
loadSync('./src/proto/character.proto'),
|
||||||
)
|
)
|
||||||
@@ -32,9 +34,11 @@ export class CharacterService {
|
|||||||
getCharacter: this.getCharacterCall.bind(this),
|
getCharacter: this.getCharacterCall.bind(this),
|
||||||
}
|
}
|
||||||
|
|
||||||
this.mongoClient = mongoClient
|
this.databaseService = databaseService
|
||||||
this.db = this.mongoClient.db(dbName)
|
|
||||||
this.characterCollection = this.db.collection('Characters')
|
// this.mongoClient = mongoClient
|
||||||
|
// this.db = this.mongoClient.db(dbName)
|
||||||
|
// this.characterCollection = this.db.collection('Characters')
|
||||||
}
|
}
|
||||||
|
|
||||||
addToServer(server: Server) {
|
addToServer(server: Server) {
|
||||||
@@ -48,14 +52,16 @@ export class CharacterService {
|
|||||||
callback: sendUnaryData<Character>,
|
callback: sendUnaryData<Character>,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
let newCharacter = await this.characterCollection
|
let newCharacter = await this.databaseService.insertCharacter(call.request.characterData)
|
||||||
.insertOne(call.request.characterData)
|
|
||||||
.then((insertResult) => {
|
// this.characterCollection
|
||||||
console.log(insertResult)
|
// .insertOne(call.request.characterData)
|
||||||
return this.characterCollection.findOne({
|
// .then((insertResult) => {
|
||||||
_id: insertResult.insertedId,
|
// console.log(insertResult)
|
||||||
})
|
// return this.characterCollection.findOne({
|
||||||
})
|
// _id: insertResult.insertedId,
|
||||||
|
// })
|
||||||
|
// })
|
||||||
|
|
||||||
console.log(newCharacter)
|
console.log(newCharacter)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user