Skip to content

Commit

Permalink
0.1.7 - throw error when no db is connected and is being used
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamint08 committed Apr 6, 2024
1 parent bda2413 commit 3fb5539
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/instances/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,67 @@ import chalk from "chalk";

class Mongo {
private client: MongoClient | null = null;
private isConnected: boolean = false;

async connect(url: string): Promise<void> {
this.client = new MongoClient(url);
const start = Date.now();
await this.client.connect().then(() => {
console.log(chalk.bold.whiteBright(`MongoDB connected in `) + chalk.bold.greenBright(`${Date.now() - start}ms`));
this.isConnected = true;
});
}

async getCollection(db: string, col: string): Promise<any> {
if (!this.isConnected) {
throw new Error('Not connected to MongoDB');
}
return this.client!.db(db).collection(col);
}

async getDatabase(db: string): Promise<any> {
if (!this.isConnected) {
throw new Error('Not connected to MongoDB');
}
return this.client!.db(db);
}

async update(db: string, col: string, query: any, update: any): Promise<any> {
if (!this.isConnected) {
throw new Error('Not connected to MongoDB');
}
const collection = await this.getCollection(db, col);
return collection.updateOne(query, update);
}

async insert(db: string, col: string, data: any): Promise<any> {
if (!this.isConnected) {
throw new Error('Not connected to MongoDB');
}
const collection = await this.getCollection(db, col);
return collection.insertOne(data);
}

async find(db: string, col: string, query: any): Promise<any[]> {
if (!this.isConnected) {
throw new Error('Not connected to MongoDB');
}
const collection = await this.getCollection(db, col);
return collection.find(query).toArray();
}

async findOne(db: string, col: string, query: any): Promise<any> {
if (!this.isConnected) {
throw new Error('Not connected to MongoDB');
}
const collection = await this.getCollection(db, col);
return collection.findOne(query);
}

async delete(db: string, col: string, query: any): Promise<any> {
if (!this.isConnected) {
throw new Error('Not connected to MongoDB');
}
const collection = await this.getCollection(db, col);
return collection.deleteOne(query);
}
Expand Down
5 changes: 5 additions & 0 deletions src/instances/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import chalk from "chalk";

class Pg {
private pool: Pool | null = null;
private isConnected: boolean = false;

async connect(config: object): Promise<void> {
this.pool = new Pool({
Expand All @@ -15,6 +16,7 @@ class Pg {
try {
await this.pool.connect();
console.log(chalk.bold.whiteBright(`PostgreSQL connected in `) + chalk.bold.greenBright(`${Date.now() - start}ms`));
this.isConnected = true;
} catch (error) {
console.log("Error while trying to establish postgres connection", error)
}
Expand All @@ -25,6 +27,9 @@ class Pg {
}

async query(text: string, params?: any[]) :Promise<any>{
if (!this.isConnected) {
throw new Error('Not connected to PostgreSQL');
}
return this.pool?.query(text,params) ;
}
}
Expand Down

0 comments on commit 3fb5539

Please sign in to comment.