-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit & build tools setup (#1)
- Loading branch information
1 parent
05197ae
commit 9f45a6f
Showing
16 changed files
with
5,546 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pkg | ||
node_modules |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,66 @@ | ||
{ | ||
"name": "mongoose-transact-utils", | ||
"version": "0.1.0", | ||
"description": "Helper methods for Mongoose and MongoDB transactions", | ||
"main": "src/index.ts", | ||
"scripts": { | ||
"build": "pack build && typedoc --out docs --target es6 --theme minimal --mode file src", | ||
"publish": "pack publish && ts-node tools/gh-pages-publish" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/cashpositive/mongoose-transact-utils.git" | ||
}, | ||
"keywords": [ | ||
"mongooose", | ||
"mongodb", | ||
"transactions" | ||
], | ||
"prettier": { | ||
"singleQuote": true, | ||
"printWidth": 100 | ||
}, | ||
"@pika/pack": { | ||
"pipeline": [ | ||
[ | ||
"@pika/plugin-ts-standard-pkg", | ||
{ | ||
"exclude": [ | ||
"tests/**/*", | ||
"examples/**/*" | ||
] | ||
} | ||
], | ||
[ | ||
"@pika/plugin-build-node" | ||
], | ||
[ | ||
"@pika/plugin-build-types" | ||
] | ||
] | ||
}, | ||
"devDependencies": { | ||
"@pika/plugin-build-node": "^0.3.14", | ||
"@pika/plugin-build-types": "^0.3.14", | ||
"@pika/plugin-ts-standard-pkg": "^0.3.14", | ||
"@types/mongoose": "^5.2", | ||
"@types/node": "^11.11.3", | ||
"mongoose": "^5.2", | ||
"prettier": "^1.16.4", | ||
"ts-node": "^8.0.3", | ||
"tslint": "^5.14.0", | ||
"tslint-config-prettier": "^1.18.0", | ||
"tslint-config-standard": "^8.0.1", | ||
"typedoc": "^0.14.2", | ||
"typescript": "^3.3.3333" | ||
}, | ||
"peerDependencies": { | ||
"mongoose": "^5.2" | ||
}, | ||
"author": "CashPositive", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/cashpositive/mongoose-transact-utils/issues" | ||
}, | ||
"homepage": "https://github.com/cashpositive/mongoose-transact-utils#readme" | ||
} |
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,32 @@ | ||
import mongoose, { ClientSession } from 'mongoose'; | ||
|
||
export type MutationCallback<T> = (session: ClientSession) => Promise<T>; | ||
|
||
/** | ||
* Runs the provided `mutations` callback within a transaction and commits the changes to the DB | ||
* only when it has run successfully. | ||
* | ||
* @param mutations A callback which does DB writes and reads using the session. | ||
*/ | ||
export async function runInTransaction<T>(mutations: MutationCallback<T>): Promise<T> { | ||
const session = await mongoose.startSession(); | ||
session.startTransaction(); | ||
try { | ||
const value = await mutations(session); | ||
|
||
// Since the mutations ran without an error, commit the transaction. | ||
await session.commitTransaction(); | ||
|
||
// Return any value returned by `mutations` to make this function as transparent as possible. | ||
return value; | ||
} catch (error) { | ||
// Abort the transaction as an error has occurred in the mutations above. | ||
await session.abortTransaction(); | ||
|
||
// Rethrow the error to be caught by the caller. | ||
throw error; | ||
} finally { | ||
// End the previous session. | ||
session.endSession(); | ||
} | ||
} |
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,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2018", | ||
"module": "esnext", | ||
"strict": true, | ||
"moduleResolution": "node", | ||
"allowSyntheticDefaultImports": true, | ||
"typeRoots": ["node_modules/@types"] | ||
}, | ||
"include": ["src"] | ||
} |
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,3 @@ | ||
{ | ||
"extends": ["tslint-config-standard", "tslint-config-prettier"] | ||
} |
Oops, something went wrong.