Skip to content

Commit

Permalink
refactor(ref-imp): make MongoDbOperationStore inherit MongoDbStore
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai committed Apr 9, 2021
1 parent 9fb410c commit 4cc7511
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
6 changes: 3 additions & 3 deletions lib/common/MongoDbStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class MongoDbStore {
/** MondoDB instance. */
protected db: Db | undefined;
/** MongoDB collection */
protected collection: Collection<any> | undefined;
protected collection!: Collection<any>;

/**
* Constructs a `MongoDbStore`;
Expand All @@ -31,7 +31,7 @@ export default class MongoDbStore {
* 2. Some cloud MongoDB services such as CosmosDB will lead to `MongoError: ns not found` connectivity error.
*/
public async clearCollection () {
await this.collection!.deleteMany({ }); // Empty filter removes all entries in collection.
await this.collection.deleteMany({ }); // Empty filter removes all entries in collection.
}

/**
Expand Down Expand Up @@ -59,7 +59,7 @@ export default class MongoDbStore {

/**
* Create the indices required by the collection passed.
* To be overridden by inherited classes if needed.
* To be overridden by inherited classes if a collection index is needed.
*/
protected async createIndex (_collection: Collection): Promise<void> {
}
Expand Down
35 changes: 9 additions & 26 deletions lib/core/MongoDbOperationStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Binary, Collection, Long, MongoClient } from 'mongodb';
import { Binary, Collection, Long } from 'mongodb';
import AnchoredOperationModel from './models/AnchoredOperationModel';
import IOperationStore from './interfaces/IOperationStore';
import MongoDbStore from '../common/MongoDbStore';
import OperationType from './enums/OperationType';

/**
Expand All @@ -22,35 +23,17 @@ interface IMongoOperation {
* Implementation of OperationStore that stores the operation data in
* a MongoDB database.
*/
export default class MongoDbOperationStore implements IOperationStore {
export default class MongoDbOperationStore extends MongoDbStore implements IOperationStore {
/** MongoDB collection name under the database where the operations are stored. */
public static readonly collectionName: string = 'operations';

private collection: Collection<any> | undefined;

constructor (
private serverUrl: string,
private databaseName: string
) { }

/**
* Initialize the MongoDB operation store.
*/
public async initialize (): Promise<void> {
const client = await MongoClient.connect(this.serverUrl, { useNewUrlParser: true }); // `useNewUrlParser` addresses nodejs's URL parser deprecation warning.
const db = client.db(this.databaseName);
const collections = await db.collections();
const collectionNames = collections.map(collection => collection.collectionName);
constructor (serverUrl: string, databaseName: string) {
super(serverUrl, MongoDbOperationStore.collectionName, databaseName);
}

// If the operation collection exists, use it; else create it then use it.
if (collectionNames.includes(MongoDbOperationStore.collectionName)) {
this.collection = db.collection(MongoDbOperationStore.collectionName);
} else {
this.collection = await db.createCollection(MongoDbOperationStore.collectionName);
// create an index on didSuffix, txnNumber, opIndex, and type to make DB operations more efficient.
// This is an unique index, so duplicate inserts are rejected/ignored.
await this.collection.createIndex({ didSuffix: 1, txnNumber: 1, opIndex: 1, type: 1 }, { unique: true });
}
protected async createIndex (collection: Collection) {
// This is an unique index, so duplicate inserts are rejected/ignored.
await collection.createIndex({ didSuffix: 1, txnNumber: 1, opIndex: 1, type: 1 }, { unique: true });
}

public async insertOrReplace (operations: AnchoredOperationModel[]): Promise<void> {
Expand Down

0 comments on commit 4cc7511

Please sign in to comment.