Skip to content

Commit

Permalink
Initial commit & build tools setup (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
drenther authored and nitish-mehta committed Mar 16, 2019
1 parent 05197ae commit 9f45a6f
Show file tree
Hide file tree
Showing 16 changed files with 5,546 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pkg
node_modules
865 changes: 865 additions & 0 deletions docs/assets/css/main.css

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions docs/assets/css/main.css.map

Large diffs are not rendered by default.

Binary file added docs/assets/images/icons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/images/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/images/widgets.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/images/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions docs/assets/js/main.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions docs/assets/js/search.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,092 changes: 1,092 additions & 0 deletions docs/globals.html

Large diffs are not rendered by default.

1,093 changes: 1,093 additions & 0 deletions docs/index.html

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions package.json
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"
}
32 changes: 32 additions & 0 deletions src/index.ts
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();
}
}
11 changes: 11 additions & 0 deletions tsconfig.json
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"]
}
3 changes: 3 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["tslint-config-standard", "tslint-config-prettier"]
}
Loading

0 comments on commit 9f45a6f

Please sign in to comment.