-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
96 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { MongoError } from "mongodb"; | ||
|
||
export async function runUpdate(db, table, $match, updates, options) { | ||
if (updates.$set || updates.$inc || updates.$push || updates.$pull || updates.$addToSet) { | ||
try { | ||
await db.collection(table).update($match, updates, options); | ||
} catch (err) { | ||
if (err instanceof MongoError) { | ||
throw `The following error was thrown by Mongo when attempting to perform this update: ${err.toString()}`; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
} | ||
} | ||
|
||
export async function runDelete(db, table, $match) { | ||
try { | ||
await db.collection(table).remove($match); | ||
} catch (err) { | ||
if (err instanceof MongoError) { | ||
throw `The following error was thrown by Mongo when attempting to perform this deletion: ${err.toString()}`; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
} | ||
|
||
export async function runInsert(db, table, newObject) { | ||
try { | ||
await db.collection(table).insert(newObject); | ||
} catch (err) { | ||
if (err instanceof MongoError) { | ||
throw `The following error was thrown by Mongo when attempting to perform this insertion: ${err.toString()}`; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
} | ||
|
||
export async function runQuery(db, table, aggregateItems) { | ||
try { | ||
return await db | ||
.collection(table) | ||
.aggregate(aggregateItems) | ||
.toArray(); | ||
} catch (err) { | ||
if (err instanceof MongoError) { | ||
throw `The following error was thrown by Mongo when attempting to perform this query: ${err.toString()}`; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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