Browse Source

initialize MongodbPersistence with connection object

Yuki Takei 1 year ago
parent
commit
c008c07126
1 changed files with 14 additions and 6 deletions
  1. 14 6
      apps/app/src/server/service/yjs-connection-manager.ts

+ 14 - 6
apps/app/src/server/service/yjs-connection-manager.ts

@@ -1,10 +1,9 @@
+import mongoose from 'mongoose';
 import type { Server } from 'socket.io';
 import type { Server } from 'socket.io';
 import { MongodbPersistence } from 'y-mongodb-provider';
 import { MongodbPersistence } from 'y-mongodb-provider';
 import { YSocketIO, type Document as Ydoc } from 'y-socket.io/dist/server';
 import { YSocketIO, type Document as Ydoc } from 'y-socket.io/dist/server';
 import * as Y from 'yjs';
 import * as Y from 'yjs';
 
 
-import { getMongoUri } from '../util/mongoose-utils';
-
 const MONGODB_PERSISTENCE_COLLECTION_NAME = 'yjs-writings';
 const MONGODB_PERSISTENCE_COLLECTION_NAME = 'yjs-writings';
 const MONGODB_PERSISTENCE_FLUSH_SIZE = 100;
 const MONGODB_PERSISTENCE_FLUSH_SIZE = 100;
 
 
@@ -29,10 +28,19 @@ class YjsConnectionManager {
     this.ysocketio = new YSocketIO(io);
     this.ysocketio = new YSocketIO(io);
     this.ysocketio.initialize();
     this.ysocketio.initialize();
 
 
-    this.mdb = new MongodbPersistence(getMongoUri(), {
-      collectionName: MONGODB_PERSISTENCE_COLLECTION_NAME,
-      flushSize: MONGODB_PERSISTENCE_FLUSH_SIZE,
-    });
+    this.mdb = new MongodbPersistence(
+      {
+        // TODO: Required upgrading mongoose and unifying the versions of mongodb to omit 'as any'
+        // eslint-disable-next-line @typescript-eslint/no-explicit-any
+        client: mongoose.connection.getClient() as any,
+        // eslint-disable-next-line @typescript-eslint/no-explicit-any
+        db: mongoose.connection.db as any,
+      },
+      {
+        collectionName: MONGODB_PERSISTENCE_COLLECTION_NAME,
+        flushSize: MONGODB_PERSISTENCE_FLUSH_SIZE,
+      },
+    );
   }
   }
 
 
   public static getInstance(io?: Server) {
   public static getInstance(io?: Server) {