@@ -16,22 +16,49 @@ export class MongodbAdapter {
1616 this . collectionName = this . configService . get < string > ( 'TLDRAW_DB_COLLECTION_NAME' ) ?? 'drawings' ;
1717 }
1818
19+ /**
20+ * Apply a $query and get one document from MongoDB.
21+ * @param {object } query
22+ * @returns {Promise<TldrawDrawing | null> }
23+ */
1924 get ( query : object ) : Promise < TldrawDrawing | null > {
2025 const collection = this . getCollection ( ) ;
2126 return collection . findOne < TldrawDrawing > ( query , { allowDiskUse : true } ) ;
2227 }
2328
24- async put ( query : object , values : object ) {
29+ /**
30+ * Store one document in MongoDB.
31+ * @param {object } query
32+ * @param {object } values
33+ * @returns {Promise<object> } Stored document
34+ */
35+ async put ( query : object , values : object ) : Promise < TldrawDrawing | null > {
2536 const collection = this . getCollection ( ) ;
2637 await collection . updateOne ( query , { $set : values } , { upsert : true } ) ;
38+ return this . get ( query ) ;
2739 }
2840
29- async del ( query : object ) {
41+ /**
42+ * Removes all documents that fit the $query
43+ * @param {object } query
44+ * @returns {Promise<object> } Contains status of the operation
45+ */
46+ del ( query : object ) : Promise < object > {
3047 const collection = this . getCollection ( ) ;
31- await collection . deleteMany ( query ) ;
48+ const bulk = collection . initializeOrderedBulkOp ( ) ;
49+ bulk . find ( query ) . delete ( ) ;
50+ return bulk . execute ( ) ;
3251 }
3352
34- readAsCursor ( query : object , opts : object = { } ) {
53+ /**
54+ * Get all or at least $opts.limit documents that fit the $query.
55+ * @param {object } query
56+ * @param {object } [opts]
57+ * @param {number } [opts.limit]
58+ * @param {boolean } [opts.reverse]
59+ * @returns {Promise<Array<object>> }
60+ */
61+ readAsCursor ( query : object , opts : object = { } ) : Promise < TldrawDrawing [ ] > {
3562 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
3663 // @ts -ignore
3764 const { limit = 0 , reverse = false } = opts ;
@@ -44,6 +71,21 @@ export class MongodbAdapter {
4471 return curs . toArray ( ) ;
4572 }
4673
74+ /**
75+ * Close connection to MongoDB instance.
76+ */
77+ async close ( ) {
78+ await this . orm . close ( ) ;
79+ }
80+
81+ /**
82+ * Delete database
83+ */
84+ async flush ( ) {
85+ await this . getCollection ( ) . drop ( ) ;
86+ await this . close ( ) ;
87+ }
88+
4789 getCollection ( ) {
4890 return this . em . getCollection ( this . collectionName ) ;
4991 }
0 commit comments