|
921 | 921 | <div class="content-wrap">
|
922 | 922 | <div class="tsd-panel tsd-typography">
|
923 | 923 | <h1 id="mongoose-transact-utils">mongoose-transact-utils</h1>
|
924 |
| - <p>Helper methods for Mongoose and MongoDB transactions</p> |
| 924 | + <p>Helper methods for Mongoose and MongoDB transactions. The library comes with @types for Typescript users.</p> |
| 925 | + <h2 id="installation">Installation</h2> |
| 926 | + <pre><code class="language-shell">npm i mongoose-transact-utils</code></pre> |
| 927 | + <p>OR</p> |
| 928 | + <pre><code class="language-shell">yarn add mongoose-transact-utils</code></pre> |
| 929 | + <h2 id="api-reference-and-examples">API Reference and Examples</h2> |
| 930 | + <h3 id="a-simple-use-case-for-transaction">A simple use case for transaction</h3> |
| 931 | + <pre><code class="language-js"><span class="hljs-keyword">const</span> { runInTransaction } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'mongoose-transact-utils'</span>); |
| 932 | + |
| 933 | +<span class="hljs-keyword">const</span> { User } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./models'</span>); |
| 934 | + |
| 935 | +<span class="hljs-comment">// any queries or write you want to do using transaction</span> |
| 936 | +(<span class="hljs-keyword">async</span> () => { |
| 937 | + <span class="hljs-comment">// runInTransction catches any error in the callback to abort the transaction session</span> |
| 938 | + <span class="hljs-comment">// and then rethrows the error for you to handle the reporting</span> |
| 939 | + <span class="hljs-keyword">await</span> runInTransaction(<span class="hljs-keyword">async</span> session => { |
| 940 | + <span class="hljs-comment">// run any queries here</span> |
| 941 | + <span class="hljs-keyword">await</span> addFriend(<span class="hljs-string">'John'</span>, <span class="hljs-string">'Jane'</span>, session); |
| 942 | + }); |
| 943 | + |
| 944 | + <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'John and Jane are friend now!'</span>); |
| 945 | +})(); |
| 946 | + |
| 947 | +<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addFriend</span>(<span class="hljs-params">nameA, nameB, session</span>) </span>{ |
| 948 | + <span class="hljs-keyword">const</span> userA = <span class="hljs-keyword">await</span> User.find({ <span class="hljs-attr">name</span>: nameA }).session(session); |
| 949 | + <span class="hljs-keyword">const</span> userB = <span class="hljs-keyword">await</span> User.find({ <span class="hljs-attr">name</span>: nameB }).session(session); |
| 950 | + |
| 951 | + userA.friends.push(userB._id); |
| 952 | + userB.friends.push(userA._id); |
| 953 | + |
| 954 | + <span class="hljs-keyword">await</span> userA.save(); |
| 955 | + <span class="hljs-keyword">await</span> userB.save(); |
| 956 | +}</code></pre> |
| 957 | + <h3 id="usage-with-several-different-mongoose-apis">Usage with several different mongoose APIs</h3> |
| 958 | + <pre><code class="language-js"><span class="hljs-keyword">const</span> { runInTransaction } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'mongoose-transact-utils'</span>); |
| 959 | +<span class="hljs-keyword">const</span> { User } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./models'</span>); |
| 960 | + |
| 961 | +<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">example</span>(<span class="hljs-params"></span>) </span>{ |
| 962 | + <span class="hljs-keyword">await</span> runInTransaction(<span class="hljs-keyword">async</span> session => { |
| 963 | + <span class="hljs-comment">// all the query methods listed here - https://mongoosejs.com/docs/queries.html</span> |
| 964 | + <span class="hljs-comment">// session works with query methods as follows -</span> |
| 965 | + <span class="hljs-keyword">const</span> user = <span class="hljs-keyword">await</span> User.findOne({}).session(session); |
| 966 | + |
| 967 | + <span class="hljs-comment">// as mentioned earlier, if you use save</span> |
| 968 | + <span class="hljs-comment">// it will use the associated session ($session)</span> |
| 969 | + <span class="hljs-keyword">await</span> users.save(); |
| 970 | + |
| 971 | + <span class="hljs-comment">// apart from using $session to set another session</span> |
| 972 | + <span class="hljs-comment">// you can also pass it as an option</span> |
| 973 | + <span class="hljs-keyword">await</span> users.save({ session }); |
| 974 | + |
| 975 | + <span class="hljs-comment">// you can also use the options object for passing session</span> |
| 976 | + <span class="hljs-keyword">await</span> User.find({}, <span class="hljs-literal">null</span>, { session }); |
| 977 | + |
| 978 | + <span class="hljs-comment">// works with where as well</span> |
| 979 | + <span class="hljs-keyword">await</span> User.where({}).session(session); |
| 980 | + |
| 981 | + <span class="hljs-comment">// anywhere where queryOptions object can be passed, it accepts the session</span> |
| 982 | + <span class="hljs-comment">// for example</span> |
| 983 | + <span class="hljs-keyword">await</span> User.create( |
| 984 | + [ |
| 985 | + <span class="hljs-comment">/* some date */</span> |
| 986 | + ], |
| 987 | + { session } |
| 988 | + ); |
| 989 | + <span class="hljs-keyword">await</span> User.bulkWrite( |
| 990 | + [ |
| 991 | + <span class="hljs-comment">/* some update commands */</span> |
| 992 | + ], |
| 993 | + { session } |
| 994 | + ); |
| 995 | + |
| 996 | + <span class="hljs-comment">// session can be used with aggregation as well</span> |
| 997 | + <span class="hljs-keyword">await</span> User.aggregate([ |
| 998 | + <span class="hljs-comment">/* pipeline */</span> |
| 999 | + ]).session(session); |
| 1000 | + |
| 1001 | + <span class="hljs-comment">// here is an example with populate, skip, limit, etc.</span> |
| 1002 | + <span class="hljs-comment">// you can chain session like all other similiar methods</span> |
| 1003 | + <span class="hljs-keyword">await</span> User.find({}) |
| 1004 | + .skip(<span class="hljs-number">10</span>) |
| 1005 | + .limit(<span class="hljs-number">10</span>) |
| 1006 | + .populate(<span class="hljs-string">'address'</span>) |
| 1007 | + .session(session); |
| 1008 | + }); |
| 1009 | +}</code></pre> |
925 | 1010 | </div>
|
926 | 1011 | <div style="position:relative;"><a name="typedoc-main-index" class="tsd-anchor"></a></div>
|
927 | 1012 | <section class="tsd-panel-group tsd-index-group">
|
@@ -951,7 +1036,7 @@ <h3>Mutation<wbr>Callback</h3>
|
951 | 1036 | <div class="tsd-signature tsd-kind-icon">Mutation<wbr>Callback<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">function</span></div>
|
952 | 1037 | <aside class="tsd-sources">
|
953 | 1038 | <ul>
|
954 |
| - <li>Defined in <a href="https://github.com/cashpositive/mongoose-transact-utils/blob/9f45a6f/src/index.ts#L3">index.ts:3</a></li> |
| 1039 | + <li>Defined in <a href="https://github.com/cashpositive/mongoose-transact-utils/blob/954a2da/src/index.ts#L3">index.ts:3</a></li> |
955 | 1040 | </ul>
|
956 | 1041 | </aside>
|
957 | 1042 | <div class="tsd-type-declaration">
|
@@ -989,7 +1074,7 @@ <h3>run<wbr>InTransaction</h3>
|
989 | 1074 | <li class="tsd-description">
|
990 | 1075 | <aside class="tsd-sources">
|
991 | 1076 | <ul>
|
992 |
| - <li>Defined in <a href="https://github.com/cashpositive/mongoose-transact-utils/blob/9f45a6f/src/index.ts#L11">index.ts:11</a></li> |
| 1077 | + <li>Defined in <a href="https://github.com/cashpositive/mongoose-transact-utils/blob/954a2da/src/index.ts#L11">index.ts:11</a></li> |
993 | 1078 | </ul>
|
994 | 1079 | </aside>
|
995 | 1080 | <div class="tsd-comment tsd-typography">
|
|
0 commit comments