UNPKG

mongo-bolt

Version:

A lightweight, beginner-friendly MongoDB wrapper with inbuilt caching and simplified joins/indexing.

155 lines (154 loc) 5.38 kB
import MongoDBConnector from './wrapperFunctions/index.js'; import AggregtationBuilder from './AggregateFunctions/index.js'; class MongoDB { constructor(config) { this.config = config; this.connector = new MongoDBConnector(config); } async connect() { if (!this.connector) { throw new Error('Connector not initialized'); } await this.connector.connect(); } async createStore(collectionName, schema) { if (!this.connector) { throw new Error('Connector not initialized'); } await this.connector.createStore(collectionName, schema); } async insertItem(collectionName, documentData) { if (!this.connector) { throw new Error('Database not connected problem'); } await this.connector.insertItem(collectionName, documentData); } async insertItems(collectionName, documentsData) { if (!this.connector) { throw new Error('Database not connected problem'); } await this.connector.insertItems(collectionName, documentsData); } async findItem(collectionName, query = {}) { if (!this.connector) { throw new Error('Connector not initialized'); } return this.connector.findItem(collectionName, query); } async updateItems(collectionName, query, update) { if (!this.connector) { throw new Error('Connector not initialized'); } return this.connector.updateItems(collectionName, query, update); } async deleteStore(collectionName) { if (!this.connector) { throw new Error('Connector not initialized'); } await this.connector.deleteStore(collectionName); } async getMongoose() { if (!this.connector) { throw new Error('Connector not initialized'); } return this.connector.getMongoose(); } async dropItem(storeName, itemInfo) { if (!this.connector) { throw new Error('Connector not initialized'); } await this.connector.dropItem(storeName, itemInfo); console.log('Item successfully dropped'); } async dropItems(storeName, itemInfo) { if (!this.connector) { throw new Error('Connector not initialized'); } await this.connector.dropItems(storeName, itemInfo); console.log('Items successfully dropped'); } async truncateStore(storeName) { if (!this.connector) { throw new Error('Connector not initialized'); } await this.connector.truncateStore(storeName); } async alterStore(storeName, newSchema, changeValues) { if (!this.connector) { throw new Error('Connector not initialized'); } await this.connector.alterStore(storeName, newSchema, changeValues); } async findAndPopulateById(storeName, thingsToBePopulated, query) { if (!this.connector) { throw new Error('Connector not initialized'); } return await this.connector.findAndPopulateById(storeName, thingsToBePopulated, query); } async aggregate(storeName, pipeline) { if (!this.connector) { throw new Error('Connector not initialized'); } return this.connector.aggregate(storeName, pipeline); } pipelineBuilder() { if (!this.connector) { throw new Error('Connector not initialized'); } return new AggregtationBuilder(); } async createIndex(storeName, column, order, options) { if (!this.connector) { throw new Error('Connector not initialized'); } await this.connector.createIndex(storeName, column, order, options); } async viewIndexes(storeName) { if (!this.connector) { throw new Error('Connector not initialized'); } return this.connector.viewIndexes(storeName); } async dropIndex(storeName, indexName) { if (!this.connector) { throw new Error('Connector not initialized'); } await this.connector.dropIndex(storeName, indexName); } async findAndPopulateByKey(fromstoreName, query, fromKey, findinstoreName, inKey) { if (!this.connector) { throw new Error('Connector not initialized'); } return this.connector.findAndPopulateByKey(fromstoreName, query, fromKey, findinstoreName, inKey); } async startTransaction() { if (!this.connector) { throw new Error('Connector not initialized'); } await this.connector.startTransaction(); } async commitTransaction() { if (!this.connector) { throw new Error('Connector not initialized'); } await this.connector.commitTransaction(); } async abortTransaction() { if (!this.connector) { throw new Error('Connector not initialized'); } await this.connector.abortTransaction(); } async getSession() { if (!this.connector) { throw new Error('Connector not initialized'); } return this.connector.getSession(); } async disconnect() { if (this.connector) { await this.connector.disconnect(); } } } export default MongoDB;