Skip to content

Commit

Permalink
fix(multiple-db-instances): fix the use of multiple nedb instances (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmoura authored Dec 10, 2020
1 parent f01c445 commit 22cd6b5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nfl-telegram-bot",
"version": "2.1.1",
"version": "2.1.2",
"description": "A Telegram bot that provides the latest news about the NFL",
"repository": "github:jpmoura/nfl-news-for-telegram",
"main": "index.ts",
Expand Down
13 changes: 13 additions & 0 deletions src/infra/repository/DatastoreFabric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Datastore from 'nedb';

export default class DatastoreFabric {
private static internalChatsDatastore: Datastore = null;

static get chatsDatastore() {
if (!this.internalChatsDatastore) {
this.internalChatsDatastore = new Datastore({ filename: 'chats.db', autoload: true });
}

return this.internalChatsDatastore;
}
}
3 changes: 2 additions & 1 deletion src/infra/repository/chat/ChatRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Datastore from 'nedb';
import { promisify } from 'util';
import DatastoreFabric from '../DatastoreFabric';

export default class ChatRepository {
private readonly db: Datastore;
Expand All @@ -9,7 +10,7 @@ export default class ChatRepository {
private readonly promisifyRemove: any;

constructor() {
this.db = new Datastore({ filename: 'bot.db', autoload: true });
this.db = DatastoreFabric.chatsDatastore;
this.promisifyUpdate = promisify(this.db.update.bind(this.db));
this.promisifyRemove = promisify(this.db.remove).bind(this.db);
}
Expand Down

0 comments on commit 22cd6b5

Please sign in to comment.