aboutsummaryrefslogtreecommitdiff
path: root/src/server/MemoryDatabase.ts
diff options
context:
space:
mode:
authorMelissa Zhang <mzhang19096@gmail.com>2020-05-15 19:10:07 -0700
committerMelissa Zhang <mzhang19096@gmail.com>2020-05-15 19:10:07 -0700
commit253d5efbbafe4a44c350fef7dda6d553a70a94c9 (patch)
treeb43a55e1efddc86e25df4bb7d90a99f3c7b52a17 /src/server/MemoryDatabase.ts
parent36c01920e3874e1484222a7012745b581405f67d (diff)
parent08b6bf8b51ab631c8cfe9c3e12bfb0ae2dd7b4c7 (diff)
merge with master
Diffstat (limited to 'src/server/MemoryDatabase.ts')
-rw-r--r--src/server/MemoryDatabase.ts23
1 files changed, 17 insertions, 6 deletions
diff --git a/src/server/MemoryDatabase.ts b/src/server/MemoryDatabase.ts
index 543f96e7f..1f1d702d9 100644
--- a/src/server/MemoryDatabase.ts
+++ b/src/server/MemoryDatabase.ts
@@ -1,4 +1,4 @@
-import { IDatabase, DocumentsCollection, NewDocumentsCollection } from './IDatabase';
+import { IDatabase, DocumentsCollection } from './IDatabase';
import { Transferable } from './Message';
import * as mongodb from 'mongodb';
@@ -15,6 +15,10 @@ export class MemoryDatabase implements IDatabase {
}
}
+ public getCollectionNames() {
+ return Promise.resolve(Object.keys(this.db));
+ }
+
public update(id: string, value: any, callback: (err: mongodb.MongoError, res: mongodb.UpdateWriteOpResult) => void, _upsert?: boolean, collectionName = DocumentsCollection): Promise<void> {
const collection = this.getCollection(collectionName);
const set = "$set";
@@ -41,7 +45,7 @@ export class MemoryDatabase implements IDatabase {
return Promise.resolve(undefined);
}
- public updateMany(query: any, update: any, collectionName = NewDocumentsCollection): Promise<mongodb.WriteOpResult> {
+ public updateMany(query: any, update: any, collectionName = DocumentsCollection): Promise<mongodb.WriteOpResult> {
throw new Error("Can't updateMany a MemoryDatabase");
}
@@ -58,8 +62,15 @@ export class MemoryDatabase implements IDatabase {
return Promise.resolve({} as any);
}
- public deleteAll(collectionName = DocumentsCollection, _persist = true): Promise<any> {
- delete this.db[collectionName];
+ public async dropSchema(...schemaNames: string[]): Promise<any> {
+ const existing = await this.getCollectionNames();
+ let valid: string[];
+ if (schemaNames.length) {
+ valid = schemaNames.filter(collection => existing.includes(collection));
+ } else {
+ valid = existing;
+ }
+ valid.forEach(schemaName => delete this.db[schemaName]);
return Promise.resolve();
}
@@ -69,14 +80,14 @@ export class MemoryDatabase implements IDatabase {
return Promise.resolve();
}
- public getDocument(id: string, fn: (result?: Transferable) => void, collectionName = NewDocumentsCollection): void {
+ public getDocument(id: string, fn: (result?: Transferable) => void, collectionName = DocumentsCollection): void {
fn(this.getCollection(collectionName)[id]);
}
public getDocuments(ids: string[], fn: (result: Transferable[]) => void, collectionName = DocumentsCollection): void {
fn(ids.map(id => this.getCollection(collectionName)[id]));
}
- public async visit(ids: string[], fn: (result: any) => string[] | Promise<string[]>, collectionName = NewDocumentsCollection): Promise<void> {
+ public async visit(ids: string[], fn: (result: any) => string[] | Promise<string[]>, collectionName = DocumentsCollection): Promise<void> {
const visited = new Set<string>();
while (ids.length) {
const count = Math.min(ids.length, 1000);