Last active
December 4, 2020 02:13
-
-
Save calderaro/647cfd709ec133b56fa5338da31fc7ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
MongoClientOptions, | |
MongoClient, | |
Db, | |
FilterQuery, | |
FindOneOptions, | |
OptionalId, | |
CollectionInsertOneOptions, | |
FindOneAndUpdateOption, | |
UpdateQuery, | |
CommonOptions | |
} from "mongodb"; | |
let connection: MongoClient | null = null; | |
let db: Db | null = null; | |
export async function createConnectionInstance() { | |
const uri = process.env.uri | |
const connection = await MongoClient.connect(uri, { | |
useNewUrlParser: true, | |
poolSize: 10, | |
useUnifiedTopology: true | |
}); | |
return connection; | |
} | |
export async function getDatabase() { | |
if (db) { | |
return db; | |
} | |
connection = await createConnectionInstance(); | |
const name = getConfig<string>("mongo.name"); | |
db = connection.db(name); | |
return db; | |
} | |
export async function getCollection<T>(collection: string) { | |
const db = await getDatabase(); | |
return db.collection<T>(collection); | |
} | |
export async function find<T>( | |
collectionName: string, | |
query?: FilterQuery<T>, | |
options?: FindOneOptions<T extends T ? T : T> | |
) { | |
const collection = await getCollection<T>(collectionName); | |
return collection.find(query || {}, options); | |
} | |
export async function findOne<T>( | |
collectionName: string, | |
query?: FilterQuery<T>, | |
options?: FindOneOptions<T extends T ? T : T> | |
) { | |
const collection = await getCollection<T>(collectionName); | |
return collection.findOne(query || {}, options); | |
} | |
export async function count<T>(collectionName: string) { | |
const collection = await getCollection<T>(collectionName); | |
return collection.count(); | |
} | |
export async function insertOne<T>( | |
collectionName: string, | |
payload: OptionalId<T>, | |
options?: CollectionInsertOneOptions | |
) { | |
const collection = await getCollection<T>(collectionName); | |
return collection.insertOne(payload, options); | |
} | |
export async function findOneAndUpdate<T>( | |
collectionName: string, | |
filter: FilterQuery<T>, | |
payload: UpdateQuery<T> | T, | |
options?: FindOneAndUpdateOption<T> | |
) { | |
const collection = await getCollection<T>(collectionName); | |
return collection.findOneAndUpdate(filter, payload, options); | |
} | |
export async function deleteOne<T>( | |
collectionName: string, | |
filter: FilterQuery<T>, | |
options?: CommonOptions & { bypassDocumentValidation?: boolean } | |
) { | |
const collection = await getCollection<T>(collectionName); | |
return collection.deleteOne(filter, options); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment