Skip to content

Commit

Permalink
wip changing clients to a singleton class based structure
Browse files Browse the repository at this point in the history
  • Loading branch information
mzrimsek committed Jun 22, 2021
1 parent e1a52cf commit 77535d4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 53 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 17 additions & 6 deletions src/clients/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@ import * as discord from 'discord.js';

import { discordConfig, logger } from '../config';

export const discordClient = new discord.Client();
discordClient.login(discordConfig.token);
import { Client } from '../models';

discordClient.on('ready', () => {
logger.info('Connected to Discord');
logger.info(`Logged in as: ${discordClient.user?.tag} - (${discordClient.user?.id})`);
});
export class DiscordClient implements Client<discord.Client> {
private client: discord.Client | null = null;

async getClient(): Promise<discord.Client> {
if (!this.client) {
this.client = new discord.Client();
this.client.login(discordConfig.token);

this.client.on('ready', () => {
logger.info('Connected to Discord');
logger.info(`Logged in as: ${this.client?.user?.tag} - (${this.client?.user?.id})`);
});
}
return this.client;
}
}
98 changes: 56 additions & 42 deletions src/clients/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,66 @@
import * as admin from 'firebase-admin';

import { COMMANDS_COLLECTION, WORD_TRACKING_COLLECTION } from '../constants';
import { Command, FirestoreCollection, TrackedWord } from '../models';
import { Client, Command, FirestoreCollection, FirestoreData, TrackedWord } from '../models';
import { firebaseConfig, logger } from '../config';

const firestoreSettings = {
timestampsInSnapshots: true
};

admin.initializeApp({
credential: admin.credential.cert({
projectId: firebaseConfig.service_account.project_id,
clientEmail: firebaseConfig.service_account.client_email,
privateKey: firebaseConfig.service_account.private_key
}),
databaseURL: firebaseConfig.database_url
});

export const firestore: FirebaseFirestore.Firestore = admin.firestore();
firestore.settings(firestoreSettings);
logger.info('Connection to Firebase established');

const commandConverter: FirebaseFirestore.FirestoreDataConverter<Command> = {
toFirestore(command: Command): FirebaseFirestore.DocumentData {
return { command: command.command, message: command.message };
},
fromFirestore(snapshot: FirebaseFirestore.QueryDocumentSnapshot): Command {
const data = snapshot.data();
return { command: data.command, message: data.message };
export default class FirestoreClient implements Client<FirestoreData> {
private client: FirestoreData | null = null;

private getFirestore(): FirebaseFirestore.Firestore {
const firestoreSettings = {
timestampsInSnapshots: true
};

admin.initializeApp({
credential: admin.credential.cert({
projectId: firebaseConfig.service_account.project_id,
clientEmail: firebaseConfig.service_account.client_email,
privateKey: firebaseConfig.service_account.private_key
}),
databaseURL: firebaseConfig.database_url
});

const firestore: FirebaseFirestore.Firestore = admin.firestore();
firestore.settings(firestoreSettings);
return firestore;
}
};

const trackingWordConverter: FirebaseFirestore.FirestoreDataConverter<TrackedWord> = {
toFirestore(trackedWord: TrackedWord): FirebaseFirestore.DocumentData {
return { count: trackedWord.count };
},
fromFirestore(snapshot: FirebaseFirestore.QueryDocumentSnapshot): TrackedWord {
const data = snapshot.data();
return { count: data.count };

private getCollection<DataType>(
firestore: FirebaseFirestore.Firestore,
collection: string
): FirestoreCollection<DataType> {
const converter: FirebaseFirestore.FirestoreDataConverter<DataType> = {
toFirestore(concreteType: DataType): FirebaseFirestore.DocumentData {
return concreteType;
},
fromFirestore(snapshot: FirebaseFirestore.QueryDocumentSnapshot): DataType {
const data = snapshot.data();
return data as DataType;
}
};

return firestore.collection(collection).withConverter(converter);
}
};

const commandsCollection: FirestoreCollection<Command> = firestore
.collection(COMMANDS_COLLECTION)
.withConverter(commandConverter);
async getClient(): Promise<FirestoreData> {
if (!this.client) {
const firestore = this.getFirestore();
logger.info('Connected to Firestore');

const commandsCollection = this.getCollection<Command>(firestore, COMMANDS_COLLECTION);
const trackingWordsCollection = this.getCollection<TrackedWord>(
firestore,
WORD_TRACKING_COLLECTION
);

const trackingWordsCollection: FirestoreCollection<TrackedWord> = firestore
.collection(WORD_TRACKING_COLLECTION)
.withConverter(trackingWordConverter);
const collections = { commandsCollection, trackingWordsCollection };

export const collections = { commandsCollection, trackingWordsCollection };
return {
firestore,
collections
};
}
return this.client;
}
}
19 changes: 15 additions & 4 deletions src/clients/mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@ import * as mqtt from 'mqtt';

import { logger, mqttConfig } from '../config';

export const mqttClient = mqtt.connect(`tcp://${mqttConfig.address}`);
mqttClient.on('connect', () => {
logger.info('Connected to MQTT Broker');
});
import { Client } from '../models';

export default class MqttClient implements Client<mqtt.MqttClient> {
private client: mqtt.MqttClient | null = null;

async getClient(): Promise<mqtt.MqttClient> {
if (!this.client) {
this.client = mqtt.connect(`tcp://${mqttConfig.address}`);
this.client.on('connect', () => {
logger.info('Connected to MQTT Broker');
});
}
return this.client;
}
}

0 comments on commit 77535d4

Please sign in to comment.