w-orm-mongodb 1.1.43 → 1.1.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -207,8 +207,9 @@ test()
207
207
  select(find) → [ {...}, {...} ] 無符合為 []
208
208
  selectByPk(pk) → {...} | null
209
209
 
210
- insert(data) → { n, nInserted, ok }
211
- insertBulk(data) → { n, nInserted, ok } 衝突即整批reject且不寫入任何一筆
210
+ insert(data, option) → { n, nInserted, ok }
211
+ insert(data, {returnList: true}) [ { n, nInserted, ok }, ... ] 與輸入等長保序
212
+ insertBulk(data) → { n, nInserted, ok } 衝突即整批reject且不寫入任何一筆
212
213
  save(data, option) → [ { n, nInserted, nModified, ok }, ... ]
213
214
  del(data) → [ { n, nDeleted, ok }, ... ]
214
215
  delAll(find) → { n, nDeleted, ok }
@@ -234,6 +235,7 @@ delAll(find) → { n, nDeleted, ok }
234
235
  |---|---|
235
236
  | 這批有沒有撞到既有主鍵 | `insertBulk`是否`reject` |
236
237
  | 這批有幾筆是新資料 | `insert`之`nInserted` |
238
+ | 這批**是哪幾筆**是新資料 | `insert(data, {returnList: true})`之各元素`nInserted === 1` |
237
239
  | 這筆是不是新資料 | `save`之`nInserted === 1` |
238
240
  | 這筆內容有沒有實際寫入 | `save`之`nModified === 1` |
239
241
  | 這筆有沒有真的被刪 | `nDeleted` |
@@ -244,6 +246,35 @@ delAll(find) → { n, nDeleted, ok }
244
246
 
245
247
  `del`對未帶有效`id`者不送查詢條件,直接回`ok: 0`並附`err`,以免`undefined`經序列化為`null`而誤刪`id`為`null`之數據。
246
248
 
249
+ ## insert 的 returnList
250
+
251
+ 聚合的`nInserted`回答「有幾筆是新的」,回答不了「**是哪幾筆**」——而後者正是去重的產出物(下游只對新資料做昂貴動作)。`option.returnList`把函數內部本就算出的逐筆判定交出來,免得呼叫端為了知道哪幾筆而把批次退化成單筆呼叫。
252
+
253
+ | `option.returnList` | 回傳 |
254
+ |---|---|
255
+ | `false`(預設) | `{ n, nInserted, ok }` |
256
+ | `true` | 與輸入**等長且保序**之陣列,元素為`{ n, nInserted, ok }` |
257
+
258
+ 逐筆元素:已插入者`nInserted`為`1`,主鍵已存在而跳過者(含同批重複之非首筆)為`0`;`n`與`ok`恆為`1`——`insert`的任何錯誤都是整批性錯誤而`reject`,故逐筆元素不出現`ok: 0`與`err`。
259
+
260
+ ```alias
261
+ let rs = await wo.insert([
262
+ { id: 'a', name: 'x' },
263
+ { id: 'b', name: 'y' }, //假設b已存在
264
+ { id: 'c', name: 'z' },
265
+ ], { returnList: true })
266
+ // => [ {n:1,nInserted:1,ok:1}, {n:1,nInserted:0,ok:1}, {n:1,nInserted:1,ok:1} ]
267
+
268
+ //取出本次真正新增的那幾筆
269
+ let rsNew = data.filter((v, k) => rs[k].nInserted === 1)
270
+ ```
271
+
272
+ 不變式:陣列長度等於輸入筆數,且`rs.filter(v => v.nInserted === 1).length`等於預設模式的`nInserted`。輸入無效時回`[]`。
273
+
274
+ 回傳形式的切換是**靜態**的——呼叫點寫死取值即知回傳形狀。兩種取值是兩份契約,共用的結果處理程式碼不要跨不同取值的呼叫點混用。
275
+
276
+ `save`與`del`不提供此選項,因為它們本來就回等長保序的逐筆陣列,資訊已經在裡面(`rs[i].nInserted === 1`、`rs[i].nModified === 1`);`insertBulk`也不提供,因為成功時逐筆恆為已插入(零資訊量)、失敗時整批`reject`而無部分結果。
277
+
247
278
  ## insert vs insertBulk
248
279
 
249
280
  兩者**衝突政策不同,不是同一個操作的加速版**:
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * w-orm-mongodb v1.1.43
2
+ * w-orm-mongodb v1.1.45
3
3
  * (c) 2018-2021 yuda-lyu(semisphere)
4
4
  * Released under the MIT License.
5
5
  */
6
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("mongodb"),require("stream")):"function"==typeof define&&define.amd?define(["mongodb","stream"],e):(t="undefined"!=typeof globalThis?globalThis:t||self)["w-orm-mongodb"]=e(t.mongodb,t.stream)}(this,(function(t,e){"use strict";function n(t,e){return t===e||t!=t&&e!=e}function r(t,e){for(var r=t.length;r--;)if(n(t[r][0],e))return r;return-1}var o=Array.prototype.splice;function u(t){var e=-1,n=null==t?0:t.length;for(this.clear();++e<n;){var r=t[e];this.set(r[0],r[1])}}u.prototype.clear=function(){this.__data__=[],this.size=0},u.prototype.delete=function(t){var e=this.__data__,n=r(e,t);return!(n<0)&&(n==e.length-1?e.pop():o.call(e,n,1),--this.size,!0)},u.prototype.get=function(t){var e=this.__data__,n=r(e,t);return n<0?void 0:e[n][1]},u.prototype.has=function(t){return r(this.__data__,t)>-1},u.prototype.set=function(t,e){var n=this.__data__,o=r(n,t);return o<0?(++this.size,n.push([t,e])):n[o][1]=e,this};var i="object"==typeof global&&global&&global.Object===Object&&global,c="object"==typeof self&&self&&self.Object===Object&&self,a=i||c||Function("return this")(),l=a.Symbol,f=Object.prototype,s=f.hasOwnProperty,v=f.toString,d=l?l.toStringTag:void 0;var p=Object.prototype.toString;var y="[object Null]",b="[object Undefined]",h=l?l.toStringTag:void 0;function j(t){return null==t?void 0===t?b:y:h&&h in Object(t)?function(t){var e=s.call(t,d),n=t[d];try{t[d]=void 0;var r=!0}catch(t){}var o=v.call(t);return r&&(e?t[d]=n:delete t[d]),o}(t):function(t){return p.call(t)}(t)}function g(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}var _="[object AsyncFunction]",w="[object Function]",m="[object GeneratorFunction]",O="[object Proxy]";function A(t){if(!g(t))return!1;var e=j(t);return e==w||e==m||e==_||e==O}var k,x=a["__core-js_shared__"],S=(k=/[^.]+$/.exec(x&&x.keys&&x.keys.IE_PROTO||""))?"Symbol(src)_1."+k:"";var P=Function.prototype.toString;function B(t){if(null!=t){try{return P.call(t)}catch(t){}try{return t+""}catch(t){}}return""}var z=/^\[object .+?Constructor\]$/,I=Function.prototype,E=Object.prototype,M=I.toString,$=E.hasOwnProperty,C=RegExp("^"+M.call($).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");function D(t){return!(!g(t)||(e=t,S&&S in e))&&(A(t)?C:z).test(B(t));var e}function U(t,e){var n=function(t,e){return null==t?void 0:t[e]}(t,e);return D(n)?n:void 0}var F=U(a,"Map"),G=U(Object,"create");var N=Object.prototype.hasOwnProperty;var T=Object.prototype.hasOwnProperty;function L(t){var e=-1,n=null==t?0:t.length;for(this.clear();++e<n;){var r=t[e];this.set(r[0],r[1])}}function q(t,e){var n,r,o=t.__data__;return("string"==(r=typeof(n=e))||"number"==r||"symbol"==r||"boolean"==r?"__proto__"!==n:null===n)?o["string"==typeof e?"string":"hash"]:o.map}function R(t){var e=-1,n=null==t?0:t.length;for(this.clear();++e<n;){var r=t[e];this.set(r[0],r[1])}}L.prototype.clear=function(){this.__data__=G?G(null):{},this.size=0},L.prototype.delete=function(t){var e=this.has(t)&&delete this.__data__[t];return this.size-=e?1:0,e},L.prototype.get=function(t){var e=this.__data__;if(G){var n=e[t];return"__lodash_hash_undefined__"===n?void 0:n}return N.call(e,t)?e[t]:void 0},L.prototype.has=function(t){var e=this.__data__;return G?void 0!==e[t]:T.call(e,t)},L.prototype.set=function(t,e){var n=this.__data__;return this.size+=this.has(t)?0:1,n[t]=G&&void 0===e?"__lodash_hash_undefined__":e,this},R.prototype.clear=function(){this.size=0,this.__data__={hash:new L,map:new(F||u),string:new L}},R.prototype.delete=function(t){var e=q(this,t).delete(t);return this.size-=e?1:0,e},R.prototype.get=function(t){return q(this,t).get(t)},R.prototype.has=function(t){return q(this,t).has(t)},R.prototype.set=function(t,e){var n=q(this,t),r=n.size;return n.set(t,e),this.size+=n.size==r?0:1,this};function V(t){var e=this.__data__=new u(t);this.size=e.size}function W(t,e){for(var n=-1,r=null==t?0:t.length;++n<r&&!1!==e(t[n],n,t););return t}V.prototype.clear=function(){this.__data__=new u,this.size=0},V.prototype.delete=function(t){var e=this.__data__,n=e.delete(t);return this.size=e.size,n},V.prototype.get=function(t){return this.__data__.get(t)},V.prototype.has=function(t){return this.__data__.has(t)},V.prototype.set=function(t,e){var n=this.__data__;if(n instanceof u){var r=n.__data__;if(!F||r.length<199)return r.push([t,e]),this.size=++n.size,this;n=this.__data__=new R(r)}return n.set(t,e),this.size=n.size,this};var H=function(){try{var t=U(Object,"defineProperty");return t({},"",{}),t}catch(t){}}(),J=H;function K(t,e,n){"__proto__"==e&&J?J(t,e,{configurable:!0,enumerable:!0,value:n,writable:!0}):t[e]=n}var Q=Object.prototype.hasOwnProperty;function X(t,e,r){var o=t[e];Q.call(t,e)&&n(o,r)&&(void 0!==r||e in t)||K(t,e,r)}function Y(t,e,n,r){var o=!n;n||(n={});for(var u=-1,i=e.length;++u<i;){var c=e[u],a=r?r(n[c],t[c],c,n,t):void 0;void 0===a&&(a=t[c]),o?K(n,c,a):X(n,c,a)}return n}function Z(t){return null!=t&&"object"==typeof t}function tt(t){return Z(t)&&"[object Arguments]"==j(t)}var et=Object.prototype,nt=et.hasOwnProperty,rt=et.propertyIsEnumerable,ot=tt(function(){return arguments}())?tt:function(t){return Z(t)&&nt.call(t,"callee")&&!rt.call(t,"callee")},ut=ot,it=Array.isArray;var ct="object"==typeof exports&&exports&&!exports.nodeType&&exports,at=ct&&"object"==typeof module&&module&&!module.nodeType&&module,lt=at&&at.exports===ct?a.Buffer:void 0,ft=(lt?lt.isBuffer:void 0)||function(){return!1},st=9007199254740991,vt=/^(?:0|[1-9]\d*)$/;function dt(t,e){var n=typeof t;return!!(e=null==e?st:e)&&("number"==n||"symbol"!=n&&vt.test(t))&&t>-1&&t%1==0&&t<e}var pt=9007199254740991;function yt(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=pt}var bt={};function ht(t){return function(e){return t(e)}}bt["[object Float32Array]"]=bt["[object Float64Array]"]=bt["[object Int8Array]"]=bt["[object Int16Array]"]=bt["[object Int32Array]"]=bt["[object Uint8Array]"]=bt["[object Uint8ClampedArray]"]=bt["[object Uint16Array]"]=bt["[object Uint32Array]"]=!0,bt["[object Arguments]"]=bt["[object Array]"]=bt["[object ArrayBuffer]"]=bt["[object Boolean]"]=bt["[object DataView]"]=bt["[object Date]"]=bt["[object Error]"]=bt["[object Function]"]=bt["[object Map]"]=bt["[object Number]"]=bt["[object Object]"]=bt["[object RegExp]"]=bt["[object Set]"]=bt["[object String]"]=bt["[object WeakMap]"]=!1;var jt="object"==typeof exports&&exports&&!exports.nodeType&&exports,gt=jt&&"object"==typeof module&&module&&!module.nodeType&&module,_t=gt&&gt.exports===jt&&i.process,wt=function(){try{var t=gt&&gt.require&&gt.require("util").types;return t||_t&&_t.binding&&_t.binding("util")}catch(t){}}(),mt=wt&&wt.isTypedArray,Ot=mt?ht(mt):function(t){return Z(t)&&yt(t.length)&&!!bt[j(t)]},At=Object.prototype.hasOwnProperty;function kt(t,e){var n=it(t),r=!n&&ut(t),o=!n&&!r&&ft(t),u=!n&&!r&&!o&&Ot(t),i=n||r||o||u,c=i?function(t,e){for(var n=-1,r=Array(t);++n<t;)r[n]=e(n);return r}(t.length,String):[],a=c.length;for(var l in t)!e&&!At.call(t,l)||i&&("length"==l||o&&("offset"==l||"parent"==l)||u&&("buffer"==l||"byteLength"==l||"byteOffset"==l)||dt(l,a))||c.push(l);return c}var xt=Object.prototype;function St(t){var e=t&&t.constructor;return t===("function"==typeof e&&e.prototype||xt)}function Pt(t,e){return function(n){return t(e(n))}}var Bt=Pt(Object.keys,Object),zt=Object.prototype.hasOwnProperty;function It(t){if(!St(t))return Bt(t);var e=[];for(var n in Object(t))zt.call(t,n)&&"constructor"!=n&&e.push(n);return e}function Et(t){return null!=t&&yt(t.length)&&!A(t)}function Mt(t){return Et(t)?kt(t):It(t)}var $t=Object.prototype.hasOwnProperty;function Ct(t){if(!g(t))return function(t){var e=[];if(null!=t)for(var n in Object(t))e.push(n);return e}(t);var e=St(t),n=[];for(var r in t)("constructor"!=r||!e&&$t.call(t,r))&&n.push(r);return n}function Dt(t){return Et(t)?kt(t,!0):Ct(t)}var Ut="object"==typeof exports&&exports&&!exports.nodeType&&exports,Ft=Ut&&"object"==typeof module&&module&&!module.nodeType&&module,Gt=Ft&&Ft.exports===Ut?a.Buffer:void 0,Nt=Gt?Gt.allocUnsafe:void 0;function Tt(t,e){for(var n=-1,r=null==t?0:t.length,o=0,u=[];++n<r;){var i=t[n];e(i,n,t)&&(u[o++]=i)}return u}function Lt(){return[]}var qt=Object.prototype.propertyIsEnumerable,Rt=Object.getOwnPropertySymbols,Vt=Rt?function(t){return null==t?[]:(t=Object(t),Tt(Rt(t),(function(e){return qt.call(t,e)})))}:Lt;function Wt(t,e){for(var n=-1,r=e.length,o=t.length;++n<r;)t[o+n]=e[n];return t}var Ht=Pt(Object.getPrototypeOf,Object),Jt=Object.getOwnPropertySymbols?function(t){for(var e=[];t;)Wt(e,Vt(t)),t=Ht(t);return e}:Lt,Kt=Jt;function Qt(t,e,n){var r=e(t);return it(t)?r:Wt(r,n(t))}function Xt(t){return Qt(t,Mt,Vt)}function Yt(t){return Qt(t,Dt,Kt)}var Zt=U(a,"DataView"),te=U(a,"Promise"),ee=U(a,"Set"),ne=U(a,"WeakMap"),re="[object Map]",oe="[object Promise]",ue="[object Set]",ie="[object WeakMap]",ce="[object DataView]",ae=B(Zt),le=B(F),fe=B(te),se=B(ee),ve=B(ne),de=j;(Zt&&de(new Zt(new ArrayBuffer(1)))!=ce||F&&de(new F)!=re||te&&de(te.resolve())!=oe||ee&&de(new ee)!=ue||ne&&de(new ne)!=ie)&&(de=function(t){var e=j(t),n="[object Object]"==e?t.constructor:void 0,r=n?B(n):"";if(r)switch(r){case ae:return ce;case le:return re;case fe:return oe;case se:return ue;case ve:return ie}return e});var pe=de,ye=Object.prototype.hasOwnProperty;var be=a.Uint8Array;function he(t){var e=new t.constructor(t.byteLength);return new be(e).set(new be(t)),e}var je=/\w*$/;var ge=l?l.prototype:void 0,_e=ge?ge.valueOf:void 0;var we="[object Boolean]",me="[object Date]",Oe="[object Map]",Ae="[object Number]",ke="[object RegExp]",xe="[object Set]",Se="[object String]",Pe="[object Symbol]",Be="[object ArrayBuffer]",ze="[object DataView]",Ie="[object Float32Array]",Ee="[object Float64Array]",Me="[object Int8Array]",$e="[object Int16Array]",Ce="[object Int32Array]",De="[object Uint8Array]",Ue="[object Uint8ClampedArray]",Fe="[object Uint16Array]",Ge="[object Uint32Array]";function Ne(t,e,n){var r,o,u,i=t.constructor;switch(e){case Be:return he(t);case we:case me:return new i(+t);case ze:return function(t,e){var n=e?he(t.buffer):t.buffer;return new t.constructor(n,t.byteOffset,t.byteLength)}(t,n);case Ie:case Ee:case Me:case $e:case Ce:case De:case Ue:case Fe:case Ge:return function(t,e){var n=e?he(t.buffer):t.buffer;return new t.constructor(n,t.byteOffset,t.length)}(t,n);case Oe:return new i;case Ae:case Se:return new i(t);case ke:return(u=new(o=t).constructor(o.source,je.exec(o))).lastIndex=o.lastIndex,u;case xe:return new i;case Pe:return r=t,_e?Object(_e.call(r)):{}}}var Te=Object.create,Le=function(){function t(){}return function(e){if(!g(e))return{};if(Te)return Te(e);t.prototype=e;var n=new t;return t.prototype=void 0,n}}(),qe=Le;var Re=wt&&wt.isMap,Ve=Re?ht(Re):function(t){return Z(t)&&"[object Map]"==pe(t)};var We=wt&&wt.isSet,He=We?ht(We):function(t){return Z(t)&&"[object Set]"==pe(t)},Je=1,Ke=2,Qe=4,Xe="[object Arguments]",Ye="[object Function]",Ze="[object GeneratorFunction]",tn="[object Object]",en={};function nn(t,e,n,r,o,u){var i,c=e&Je,a=e&Ke,l=e&Qe;if(n&&(i=o?n(t,r,o,u):n(t)),void 0!==i)return i;if(!g(t))return t;var f=it(t);if(f){if(i=function(t){var e=t.length,n=new t.constructor(e);return e&&"string"==typeof t[0]&&ye.call(t,"index")&&(n.index=t.index,n.input=t.input),n}(t),!c)return function(t,e){var n=-1,r=t.length;for(e||(e=Array(r));++n<r;)e[n]=t[n];return e}(t,i)}else{var s=pe(t),v=s==Ye||s==Ze;if(ft(t))return function(t,e){if(e)return t.slice();var n=t.length,r=Nt?Nt(n):new t.constructor(n);return t.copy(r),r}(t,c);if(s==tn||s==Xe||v&&!o){if(i=a||v?{}:function(t){return"function"!=typeof t.constructor||St(t)?{}:qe(Ht(t))}(t),!c)return a?function(t,e){return Y(t,Kt(t),e)}(t,function(t,e){return t&&Y(e,Dt(e),t)}(i,t)):function(t,e){return Y(t,Vt(t),e)}(t,function(t,e){return t&&Y(e,Mt(e),t)}(i,t))}else{if(!en[s])return o?t:{};i=Ne(t,s,c)}}u||(u=new V);var d=u.get(t);if(d)return d;u.set(t,i),He(t)?t.forEach((function(r){i.add(nn(r,e,n,r,t,u))})):Ve(t)&&t.forEach((function(r,o){i.set(o,nn(r,e,n,o,t,u))}));var p=f?void 0:(l?a?Yt:Xt:a?Dt:Mt)(t);return W(p||t,(function(r,o){p&&(r=t[o=r]),X(i,o,nn(r,e,n,o,t,u))})),i}en[Xe]=en["[object Array]"]=en["[object ArrayBuffer]"]=en["[object DataView]"]=en["[object Boolean]"]=en["[object Date]"]=en["[object Float32Array]"]=en["[object Float64Array]"]=en["[object Int8Array]"]=en["[object Int16Array]"]=en["[object Int32Array]"]=en["[object Map]"]=en["[object Number]"]=en[tn]=en["[object RegExp]"]=en["[object Set]"]=en["[object String]"]=en["[object Symbol]"]=en["[object Uint8Array]"]=en["[object Uint8ClampedArray]"]=en["[object Uint16Array]"]=en["[object Uint32Array]"]=!0,en["[object Error]"]=en[Ye]=en["[object WeakMap]"]=!1;var rn=1,on=4;function un(t){return nn(t,rn|on)}function cn(t,e){for(var n=-1,r=null==t?0:t.length;++n<r;)if(!e(t[n],n,t))return!1;return!0}var an,ln=function(t,e,n){for(var r=-1,o=Object(t),u=n(t),i=u.length;i--;){var c=u[an?i:++r];if(!1===e(o[c],c,o))break}return t};var fn=function(t,e){return function(n,r){if(null==n)return n;if(!Et(n))return t(n,r);for(var o=n.length,u=e?o:-1,i=Object(n);(e?u--:++u<o)&&!1!==r(i[u],u,i););return n}}((function(t,e){return t&&ln(t,e,Mt)})),sn=fn;function vn(t,e){var n=!0;return sn(t,(function(t,r,o){return n=!!e(t,r,o)})),n}function dn(t){var e=-1,n=null==t?0:t.length;for(this.__data__=new R;++e<n;)this.add(t[e])}function pn(t,e){for(var n=-1,r=null==t?0:t.length;++n<r;)if(e(t[n],n,t))return!0;return!1}dn.prototype.add=dn.prototype.push=function(t){return this.__data__.set(t,"__lodash_hash_undefined__"),this},dn.prototype.has=function(t){return this.__data__.has(t)};var yn=1,bn=2;function hn(t,e,n,r,o,u){var i=n&yn,c=t.length,a=e.length;if(c!=a&&!(i&&a>c))return!1;var l=u.get(t),f=u.get(e);if(l&&f)return l==e&&f==t;var s=-1,v=!0,d=n&bn?new dn:void 0;for(u.set(t,e),u.set(e,t);++s<c;){var p=t[s],y=e[s];if(r)var b=i?r(y,p,s,e,t,u):r(p,y,s,t,e,u);if(void 0!==b){if(b)continue;v=!1;break}if(d){if(!pn(e,(function(t,e){if(i=e,!d.has(i)&&(p===t||o(p,t,n,r,u)))return d.push(e);var i}))){v=!1;break}}else if(p!==y&&!o(p,y,n,r,u)){v=!1;break}}return u.delete(t),u.delete(e),v}function jn(t){var e=-1,n=Array(t.size);return t.forEach((function(t,r){n[++e]=[r,t]})),n}function gn(t){var e=-1,n=Array(t.size);return t.forEach((function(t){n[++e]=t})),n}var _n=1,wn=2,mn="[object Boolean]",On="[object Date]",An="[object Error]",kn="[object Map]",xn="[object Number]",Sn="[object RegExp]",Pn="[object Set]",Bn="[object String]",zn="[object Symbol]",In="[object ArrayBuffer]",En="[object DataView]",Mn=l?l.prototype:void 0,$n=Mn?Mn.valueOf:void 0;var Cn=1,Dn=Object.prototype.hasOwnProperty;var Un=1,Fn="[object Arguments]",Gn="[object Array]",Nn="[object Object]",Tn=Object.prototype.hasOwnProperty;function Ln(t,e,r,o,u,i){var c=it(t),a=it(e),l=c?Gn:pe(t),f=a?Gn:pe(e),s=(l=l==Fn?Nn:l)==Nn,v=(f=f==Fn?Nn:f)==Nn,d=l==f;if(d&&ft(t)){if(!ft(e))return!1;c=!0,s=!1}if(d&&!s)return i||(i=new V),c||Ot(t)?hn(t,e,r,o,u,i):function(t,e,r,o,u,i,c){switch(r){case En:if(t.byteLength!=e.byteLength||t.byteOffset!=e.byteOffset)return!1;t=t.buffer,e=e.buffer;case In:return!(t.byteLength!=e.byteLength||!i(new be(t),new be(e)));case mn:case On:case xn:return n(+t,+e);case An:return t.name==e.name&&t.message==e.message;case Sn:case Bn:return t==e+"";case kn:var a=jn;case Pn:var l=o&_n;if(a||(a=gn),t.size!=e.size&&!l)return!1;var f=c.get(t);if(f)return f==e;o|=wn,c.set(t,e);var s=hn(a(t),a(e),o,u,i,c);return c.delete(t),s;case zn:if($n)return $n.call(t)==$n.call(e)}return!1}(t,e,l,r,o,u,i);if(!(r&Un)){var p=s&&Tn.call(t,"__wrapped__"),y=v&&Tn.call(e,"__wrapped__");if(p||y){var b=p?t.value():t,h=y?e.value():e;return i||(i=new V),u(b,h,r,o,i)}}return!!d&&(i||(i=new V),function(t,e,n,r,o,u){var i=n&Cn,c=Xt(t),a=c.length;if(a!=Xt(e).length&&!i)return!1;for(var l=a;l--;){var f=c[l];if(!(i?f in e:Dn.call(e,f)))return!1}var s=u.get(t),v=u.get(e);if(s&&v)return s==e&&v==t;var d=!0;u.set(t,e),u.set(e,t);for(var p=i;++l<a;){var y=t[f=c[l]],b=e[f];if(r)var h=i?r(b,y,f,e,t,u):r(y,b,f,t,e,u);if(!(void 0===h?y===b||o(y,b,n,r,u):h)){d=!1;break}p||(p="constructor"==f)}if(d&&!p){var j=t.constructor,g=e.constructor;j==g||!("constructor"in t)||!("constructor"in e)||"function"==typeof j&&j instanceof j&&"function"==typeof g&&g instanceof g||(d=!1)}return u.delete(t),u.delete(e),d}(t,e,r,o,u,i))}function qn(t,e,n,r,o){return t===e||(null==t||null==e||!Z(t)&&!Z(e)?t!=t&&e!=e:Ln(t,e,n,r,qn,o))}var Rn=1,Vn=2;function Wn(t){return t==t&&!g(t)}function Hn(t,e){return function(n){return null!=n&&(n[t]===e&&(void 0!==e||t in Object(n)))}}function Jn(t){var e=function(t){for(var e=Mt(t),n=e.length;n--;){var r=e[n],o=t[r];e[n]=[r,o,Wn(o)]}return e}(t);return 1==e.length&&e[0][2]?Hn(e[0][0],e[0][1]):function(n){return n===t||function(t,e,n,r){var o=n.length,u=o,i=!r;if(null==t)return!u;for(t=Object(t);o--;){var c=n[o];if(i&&c[2]?c[1]!==t[c[0]]:!(c[0]in t))return!1}for(;++o<u;){var a=(c=n[o])[0],l=t[a],f=c[1];if(i&&c[2]){if(void 0===l&&!(a in t))return!1}else{var s=new V;if(r)var v=r(l,f,a,t,e,s);if(!(void 0===v?qn(f,l,Rn|Vn,r,s):v))return!1}}return!0}(n,t,e)}}var Kn="[object Symbol]";function Qn(t){return"symbol"==typeof t||Z(t)&&j(t)==Kn}var Xn=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,Yn=/^\w*$/;function Zn(t,e){if(it(t))return!1;var n=typeof t;return!("number"!=n&&"symbol"!=n&&"boolean"!=n&&null!=t&&!Qn(t))||(Yn.test(t)||!Xn.test(t)||null!=e&&t in Object(e))}var tr="Expected a function";function er(t,e){if("function"!=typeof t||null!=e&&"function"!=typeof e)throw new TypeError(tr);var n=function(){var r=arguments,o=e?e.apply(this,r):r[0],u=n.cache;if(u.has(o))return u.get(o);var i=t.apply(this,r);return n.cache=u.set(o,i)||u,i};return n.cache=new(er.Cache||R),n}er.Cache=R;var nr,rr,or,ur=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,ir=/\\(\\)?/g,cr=(nr=function(t){var e=[];return 46===t.charCodeAt(0)&&e.push(""),t.replace(ur,(function(t,n,r,o){e.push(r?o.replace(ir,"$1"):n||t)})),e},rr=er(nr,(function(t){return 500===or.size&&or.clear(),t})),or=rr.cache,rr),ar=cr;function lr(t,e){for(var n=-1,r=null==t?0:t.length,o=Array(r);++n<r;)o[n]=e(t[n],n,t);return o}var fr=1/0,sr=l?l.prototype:void 0,vr=sr?sr.toString:void 0;function dr(t){if("string"==typeof t)return t;if(it(t))return lr(t,dr)+"";if(Qn(t))return vr?vr.call(t):"";var e=t+"";return"0"==e&&1/t==-fr?"-0":e}function pr(t,e){return it(t)?t:Zn(t,e)?[t]:ar(function(t){return null==t?"":dr(t)}(t))}var yr=1/0;function br(t){if("string"==typeof t||Qn(t))return t;var e=t+"";return"0"==e&&1/t==-yr?"-0":e}function hr(t,e){for(var n=0,r=(e=pr(e,t)).length;null!=t&&n<r;)t=t[br(e[n++])];return n&&n==r?t:void 0}function jr(t,e,n){var r=null==t?void 0:hr(t,e);return void 0===r?n:r}function gr(t,e){return null!=t&&e in Object(t)}function _r(t,e){return null!=t&&function(t,e,n){for(var r=-1,o=(e=pr(e,t)).length,u=!1;++r<o;){var i=br(e[r]);if(!(u=null!=t&&n(t,i)))break;t=t[i]}return u||++r!=o?u:!!(o=null==t?0:t.length)&&yt(o)&&dt(i,o)&&(it(t)||ut(t))}(t,e,gr)}var wr=1,mr=2;function Or(t){return t}function Ar(t){return function(e){return null==e?void 0:e[t]}}function kr(t){return Zn(t)?Ar(br(t)):function(t){return function(e){return hr(e,t)}}(t)}function xr(t){return"function"==typeof t?t:null==t?Or:"object"==typeof t?it(t)?(e=t[0],n=t[1],Zn(e)&&Wn(n)?Hn(br(e),n):function(t){var r=jr(t,e);return void 0===r&&r===n?_r(t,e):qn(n,r,wr|mr)}):Jn(t):kr(t);var e,n}function Sr(t,e,r){var o=it(t)?cn:vn;return r&&function(t,e,r){if(!g(r))return!1;var o=typeof e;return!!("number"==o?Et(r)&&dt(e,r.length):"string"==o&&e in r)&&n(r[e],t)}(t,e,r)&&(e=void 0),o(t,xr(e))}function Pr(t,e){var n=[];return sn(t,(function(t,r,o){e(t,r,o)&&n.push(t)})),n}function Br(t,e){var n=-1,r=Et(t)?Array(t.length):[];return sn(t,(function(t,o,u){r[++n]=e(t,o,u)})),r}function zr(t,e){return(it(t)?lr:Br)(t,xr(e))}function Ir(t,e,n){var r=-1,o=t.length;e<0&&(e=-e>o?0:o+e),(n=n>o?o:n)<0&&(n+=o),o=e>n?0:n-e>>>0,e>>>=0;for(var u=Array(o);++r<o;)u[r]=t[r+e];return u}var Er=Object.prototype.hasOwnProperty;function Mr(t,e){var n=-1,r=(e=pr(e,t)).length;if(!r)return!0;for(;++n<r;){var o=br(e[n]);if("__proto__"===o&&!Er.call(t,"__proto__"))return!1;if(("constructor"===o||"prototype"===o)&&n<r-1)return!1}var u=function(t,e){return e.length<2?t:hr(t,Ir(e,0,-1))}(t,e);return null==u||delete u[br(function(t){var e=null==t?0:t.length;return e?t[e-1]:void 0}(e))]}var $r="[object Object]",Cr=Function.prototype,Dr=Object.prototype,Ur=Cr.toString,Fr=Dr.hasOwnProperty,Gr=Ur.call(Object);function Nr(t){return function(t){if(!Z(t)||j(t)!=$r)return!1;var e=Ht(t);if(null===e)return!0;var n=Fr.call(e,"constructor")&&e.constructor;return"function"==typeof n&&n instanceof n&&Ur.call(n)==Gr}(t)?void 0:t}var Tr=l?l.isConcatSpreadable:void 0;function Lr(t){return it(t)||ut(t)||!!(Tr&&t&&t[Tr])}function qr(t,e,n,r,o){var u=-1,i=t.length;for(n||(n=Lr),o||(o=[]);++u<i;){var c=t[u];e>0&&n(c)?e>1?qr(c,e-1,n,r,o):Wt(o,c):r||(o[o.length]=c)}return o}function Rr(t){return(null==t?0:t.length)?qr(t,1):[]}var Vr=Math.max;var Wr=J?function(t,e){return J(t,"toString",{configurable:!0,enumerable:!1,value:(n=e,function(){return n}),writable:!0});var n}:Or,Hr=Wr,Jr=Date.now;var Kr=function(t){var e=0,n=0;return function(){var r=Jr(),o=16-(r-n);if(n=r,o>0){if(++e>=800)return arguments[0]}else e=0;return t.apply(void 0,arguments)}}(Hr),Qr=Kr;var Xr=function(t){return Qr(function(t,e,n){return e=Vr(void 0===e?t.length-1:e,0),function(){for(var r=arguments,o=-1,u=Vr(r.length-e,0),i=Array(u);++o<u;)i[o]=r[e+o];o=-1;for(var c=Array(e+1);++o<e;)c[o]=r[o];return c[e]=n(i),function(t,e,n){switch(n.length){case 0:return t.call(e);case 1:return t.call(e,n[0]);case 2:return t.call(e,n[0],n[1]);case 3:return t.call(e,n[0],n[1],n[2])}return t.apply(e,n)}(t,this,c)}}(t,void 0,Rr),t+"")}((function(t,e){var n={};if(null==t)return n;var r=!1;e=lr(e,(function(e){return e=pr(e,t),r||(r=e.length>1),e})),Y(t,Yt(t),n),r&&(n=nn(n,7,Nr));for(var o=e.length;o--;)Mr(n,e[o]);return n})),Yr=Xr,Zr="[object String]";var to=Ar("length"),eo=RegExp("[\\u200d\\ud800-\\udfff\\u0300-\\u036f\\ufe20-\\ufe2f\\u20d0-\\u20ff\\ufe0e\\ufe0f]");var no="\\ud800-\\udfff",ro="["+no+"]",oo="[\\u0300-\\u036f\\ufe20-\\ufe2f\\u20d0-\\u20ff]",uo="\\ud83c[\\udffb-\\udfff]",io="[^"+no+"]",co="(?:\\ud83c[\\udde6-\\uddff]){2}",ao="[\\ud800-\\udbff][\\udc00-\\udfff]",lo="(?:"+oo+"|"+uo+")"+"?",fo="[\\ufe0e\\ufe0f]?",so=fo+lo+("(?:\\u200d(?:"+[io,co,ao].join("|")+")"+fo+lo+")*"),vo="(?:"+[io+oo+"?",oo,co,ao,ro].join("|")+")",po=RegExp(uo+"(?="+uo+")|"+vo+so,"g");function yo(t){return function(t){return eo.test(t)}(t)?function(t){for(var e=po.lastIndex=0;po.test(t);)++e;return e}(t):to(t)}var bo="[object Map]",ho="[object Set]";function jo(t){if(null==t)return 0;if(Et(t))return"string"==typeof(e=t)||!it(e)&&Z(e)&&j(e)==Zr?yo(t):t.length;var e,n=pe(t);return n==bo||n==ho?t.size:It(t).length}var go={};!function(t){var e=Object.prototype.hasOwnProperty,n="~";function r(){}function o(t,e,n){this.fn=t,this.context=e,this.once=n||!1}function u(t,e,r,u,i){if("function"!=typeof r)throw new TypeError("The listener must be a function");var c=new o(r,u||t,i),a=n?n+e:e;return t._events[a]?t._events[a].fn?t._events[a]=[t._events[a],c]:t._events[a].push(c):(t._events[a]=c,t._eventsCount++),t}function i(t,e){0==--t._eventsCount?t._events=new r:delete t._events[e]}function c(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),c.prototype.eventNames=function(){var t,r,o=[];if(0===this._eventsCount)return o;for(r in t=this._events)e.call(t,r)&&o.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?o.concat(Object.getOwnPropertySymbols(t)):o},c.prototype.listeners=function(t){var e=n?n+t:t,r=this._events[e];if(!r)return[];if(r.fn)return[r.fn];for(var o=0,u=r.length,i=new Array(u);o<u;o++)i[o]=r[o].fn;return i},c.prototype.listenerCount=function(t){var e=n?n+t:t,r=this._events[e];return r?r.fn?1:r.length:0},c.prototype.emit=function(t,e,r,o,u,i){var c=n?n+t:t;if(!this._events[c])return!1;var a,l,f=this._events[c],s=arguments.length;if(f.fn){switch(f.once&&this.removeListener(t,f.fn,void 0,!0),s){case 1:return f.fn.call(f.context),!0;case 2:return f.fn.call(f.context,e),!0;case 3:return f.fn.call(f.context,e,r),!0;case 4:return f.fn.call(f.context,e,r,o),!0;case 5:return f.fn.call(f.context,e,r,o,u),!0;case 6:return f.fn.call(f.context,e,r,o,u,i),!0}for(l=1,a=new Array(s-1);l<s;l++)a[l-1]=arguments[l];f.fn.apply(f.context,a)}else{var v,d=f.length;for(l=0;l<d;l++)switch(f[l].once&&this.removeListener(t,f[l].fn,void 0,!0),s){case 1:f[l].fn.call(f[l].context);break;case 2:f[l].fn.call(f[l].context,e);break;case 3:f[l].fn.call(f[l].context,e,r);break;case 4:f[l].fn.call(f[l].context,e,r,o);break;default:if(!a)for(v=1,a=new Array(s-1);v<s;v++)a[v-1]=arguments[v];f[l].fn.apply(f[l].context,a)}}return!0},c.prototype.on=function(t,e,n){return u(this,t,e,n,!1)},c.prototype.once=function(t,e,n){return u(this,t,e,n,!0)},c.prototype.removeListener=function(t,e,r,o){var u=n?n+t:t;if(!this._events[u])return this;if(!e)return i(this,u),this;var c=this._events[u];if(c.fn)c.fn!==e||o&&!c.once||r&&c.context!==r||i(this,u);else{for(var a=0,l=[],f=c.length;a<f;a++)(c[a].fn!==e||o&&!c[a].once||r&&c[a].context!==r)&&l.push(c[a]);l.length?this._events[u]=1===l.length?l[0]:l:i(this,u)}return this},c.prototype.removeAllListeners=function(t){var e;return t?(e=n?n+t:t,this._events[e]&&i(this,e)):(this._events=new r,this._eventsCount=0),this},c.prototype.off=c.prototype.removeListener,c.prototype.addListener=c.prototype.on,c.prefixed=n,c.EventEmitter=c,t.exports=c}({get exports(){return go},set exports(t){go=t}});var _o=go;function wo(){let t,e,n=new Promise((function(){t=arguments[0],e=arguments[1]}));return n.resolve=t,n.reject=e,n}const mo=[];for(let t=0;t<256;++t)mo.push((t+256).toString(16).slice(1));const Oo=new Uint8Array(16);function Ao(){return crypto.getRandomValues(Oo)}const ko={};function xo(t,e,n){let r;if(t)r=So(t.random??t.rng?.()??Ao(),t.msecs,t.seq,e,n);else{const t=Date.now(),o=Ao();!function(t,e,n){t.msecs??=-1/0,t.seq??=0,e>t.msecs?(t.seq=n[6]<<23|n[7]<<16|n[8]<<8|n[9],t.msecs=e):(t.seq=t.seq+1|0,0===t.seq&&t.msecs++)}(ko,t,o),r=So(o,ko.msecs,ko.seq,e,n)}return e??function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return(mo[t[e+0]]+mo[t[e+1]]+mo[t[e+2]]+mo[t[e+3]]+"-"+mo[t[e+4]]+mo[t[e+5]]+"-"+mo[t[e+6]]+mo[t[e+7]]+"-"+mo[t[e+8]]+mo[t[e+9]]+"-"+mo[t[e+10]]+mo[t[e+11]]+mo[t[e+12]]+mo[t[e+13]]+mo[t[e+14]]+mo[t[e+15]]).toLowerCase()}(r)}function So(t,e,n,r){let o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:0;if(t.length<16)throw new Error("Random bytes length must be >= 16");if(r){if(o<0||o+16>r.length)throw new RangeError(`UUID byte range ${o}:${o+15} is out of buffer bounds`)}else r=new Uint8Array(16),o=0;return e??=Date.now(),n??=127*t[6]<<24|t[7]<<16|t[8]<<8|t[9],r[o++]=e/1099511627776&255,r[o++]=e/4294967296&255,r[o++]=e/16777216&255,r[o++]=e/65536&255,r[o++]=e/256&255,r[o++]=255&e,r[o++]=112|n>>>28&15,r[o++]=n>>>20&255,r[o++]=128|n>>>14&63,r[o++]=n>>>6&255,r[o++]=n<<2&255|3&t[10],r[o++]=t[11],r[o++]=t[12],r[o++]=t[13],r[o++]=t[14],r[o++]=t[15],r}var Po="[object Boolean]";function Bo(t){return!0===(e=t)||!1===e||Z(e)&&j(e)==Po;var e}function zo(t){return"[object String]"===Object.prototype.toString.call(t)}function Io(t){return!(!zo(t)||""===t)}function Eo(t){return"[object Array]"===Object.prototype.toString.call(t)}function Mo(t){return"[object Object]"===Object.prototype.toString.call(t)}function $o(t){return!!function(t){return"[object Undefined]"===Object.prototype.toString.call(t)}(t)||(!!function(t){return"[object Null]"===Object.prototype.toString.call(t)}(t)||(!!function(t){if(Mo(t)){for(let e in t)return!1;return!0}return!1}(t)||(!!function(t){return!(!zo(t)||""!==t)}(t)||(!!function(t){return!!Eo(t)&&0===t.length}(t)||!!function(t){return t!=t}(t)))))}function Co(t){return!!Eo(t)&&(0!==t.length&&(1!==t.length||!$o(t[0])))}function Do(t){if(Mo(t)){for(let e in t)return!0;return!1}return!1}var Uo=/\s/;var Fo=/^\s+/;function Go(t){return t?t.slice(0,function(t){for(var e=t.length;e--&&Uo.test(t.charAt(e)););return e}(t)+1).replace(Fo,""):t}var No=NaN,To=/^[-+]0x[0-9a-f]+$/i,Lo=/^0b[01]+$/i,qo=/^0o[0-7]+$/i,Ro=parseInt;var Vo=1/0,Wo=17976931348623157e292;function Ho(t){return t?(t=function(t){if("number"==typeof t)return t;if(Qn(t))return No;if(g(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=g(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=Go(t);var n=Lo.test(t);return n||qo.test(t)?Ro(t.slice(2),n?2:8):To.test(t)?No:+t}(t))===Vo||t===-Vo?(t<0?-1:1)*Wo:t==t?t:0:0===t?t:0}function Jo(t,e,n){var r=null==t?0:t.length;return r?(e=n||void 0===e?1:function(t){var e=Ho(t),n=e%1;return e==e?n?e-n:e:0}(e),Ir(t,e<0?0:e,r)):[]}function Ko(t,e){var n;return(it(t)?W:sn)(t,"function"==typeof(n=e)?n:Or)}function Qo(t){let e=Object.prototype.toString.call(t);return"[object Function]"===e||"[object AsyncFunction]"===e}function Xo(t,e){let n=wo();if(!Eo(t)&&!Mo(t))return n.reject("rs is not an array or object"),n;let r=!1;if(Mo(t)){r=!0;let e=[];Ko(t,((t,n)=>{e.push({k:n,v:t})})),t=e}Qo(e)||(e=function(t){return t});let o=-1,u=[];return t.reduce((function(t,n){return t.then((function(t){u.push(t),o+=1;let i=o,c=n;return r&&(i=n.k,c=n.v),Qo(e)?e(c,i):c}))}),Promise.resolve()).then((function(t){u.push(t),u=Jo(u),n.resolve(u)})).catch((function(t){n.reject(t)})),n}return function(){let n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};n.url||(n.url="mongodb://127.0.0.1:27017"),n.db||(n.db="worm"),n.cl||(n.cl="test");let r=jr(n,"autoGenPk");Bo(r)||(r=!0);let o=!1,u=!1,i=null,c=new _o,a=t.MongoClient;function l(t){let e=jr(t,"message");return Io(e)?e:String(t)}function f(t,e,n){try{c.emit("change",t,e,n)}catch(t){console.log(t)}}function s(t,e,n){try{c.emit("error",t,e,l(n))}catch(t){console.log(t)}}function v(t,e){if(!Io(t.id)){if(!r)throw new Error(`invalid data[${e}].id, autoGenPk is false`);t.id=xo()}return t}function d(t){let e=jr(t,"writeErrors");return Co(e)?Sr(e,(function(t){return 11e3===jr(t,"code")||11e3===jr(t,"err.code")})):11e3===jr(t,"code")}async function p(t){o||(await t.createIndex({id:1},{unique:!0}),o=!0)}async function y(t,e){let n=null;try{await t.insertMany(e,{ordered:!1})}catch(t){n=t}if(null===n)return;let r=zr(e,(function(t){return t._id}));throw r=function(t,e){return(it(t)?Tt:Pr)(t,xr(e))}(r,(function(t){return null!=t})),jo(r)>0&&await t.deleteMany({_id:{$in:r}}),n}async function b(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=arguments.length>1?arguments[1]:void 0,n=!1,r=null;try{let n=e.find(t);r=await n.toArray()}catch(t){n=!0,r=t}return n?Promise.reject(r):r}return c.select=async function(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=!1,r=new a(n.url),o=null;try{let e=r.db(n.db).collection(n.cl).find(t);o=await e.toArray(),o=zr(o,(function(t){return t=Yr(t,"_id")}))}catch(t){e=!0,o=t}finally{await r.close(),r=null}return e?(s("select",null,o),Promise.reject(o)):o},c.selectByPk=async function(t){let e=!1;if(!Io(t))return null;let r=new a(n.url),o=null;try{let e=r.db(n.db).collection(n.cl),u=await e.findOne({id:t},{projection:{_id:0}});o=Do(u)?u:null}catch(t){e=!0,o=t}finally{await r.close(),r=null}return e?(s("selectByPk",null,o),Promise.reject(o)):o},c.insert=async function(t){let e=!1;if(!Do(t)&&!Co(t))return{n:0,nInserted:0,ok:1};t=un(t);let r=new a(n.url),o=null;try{await r.connect();let e=r.db(n.db).collection(n.cl);Eo(t)||(t=[t]),t=zr(t,v),await p(e);let u=jo(t),i=0;try{i=(await e.insertMany(t,{ordered:!1})).insertedCount}catch(t){if(!d(t))throw t;i=jr(t,"result.insertedCount",0)}o={n:u,nInserted:i,ok:1}}catch(t){e=!0,o=t}finally{await r.close(),r=null}return e||f("insert",t,o),e?(s("insert",t,o),Promise.reject(o)):o},c.insertBulk=async function(t){let e=!1;if(!Do(t)&&!Co(t))return{n:0,nInserted:0,ok:1};t=un(t);let r=new a(n.url),o=null;try{await r.connect();let e=r.db(n.db).collection(n.cl);Eo(t)||(t=[t]),t=zr(t,v),await p(e);let u=jo(t),c=await async function(t){if(Bo(i))return i;let e=await t.db(n.db).admin().command({hello:1});return i=Io(jr(e,"setName"))||"isdbgrid"===jr(e,"msg"),i}(r);c?await async function(t,e,n){let r=t.startSession();try{await r.withTransaction((async()=>{await e.insertMany(n,{session:r,ordered:!0})}))}finally{await r.endSession()}}(r,e,t):await y(e,t),o={n:u,nInserted:u,ok:1}}catch(t){e=!0,o=t}finally{await r.close(),r=null}return e||f("insertBulk",t,o),e?(s("insertBulk",t,o),Promise.reject(o)):o},c.save=async function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=!1;if(!Do(t)&&!Co(t))return[];t=un(t);let o=jr(e,"autoInsert",!0),u=new a(n.url),i=null;try{await u.connect();let e=u.db(n.db).collection(n.cl);Eo(t)||(t=[t]),t=zr(t,v),await p(e),i=await Xo(t,(async t=>async function(t,e,n){let r=null,o=!1;for(let u=0;u<3;u++)try{let u=await t.updateOne({id:e.id},{$set:e},{upsert:n}),i=jr(u,"matchedCount",0),c=jr(u,"modifiedCount",0);jr(u,"upsertedCount",0)>0?(r={n:1,nInserted:1,nModified:0,ok:1},o=!0):r=i>0?{n:1,nInserted:0,nModified:c>0?1:0,ok:1}:{n:0,nInserted:0,nModified:0,ok:1};break}catch(t){if(d(t)&&u<2)continue;r={n:1,nInserted:0,nModified:0,ok:0,err:l(t)};break}return o&&f("insert",[e],r),0===r.ok&&s("save",[e],r.err),r}(e,t,o)))}catch(t){r=!0,i=t}finally{await u.close(),u=null}return r||f("save",t,i),r?(s("save",t,i),Promise.reject(i)):i},c.del=async function(t){let e=!1;if(!Do(t)&&!Co(t))return[];t=un(t);let r=new a(n.url),o=null;try{await r.connect();let e=r.db(n.db).collection(n.cl);Eo(t)||(t=[t]),o=await Xo(t,(async t=>{let n=jr(t,"id"),r=null;if(Io(n))try{let t=jr(await e.deleteOne({id:n}),"deletedCount",0);r={n:t,nDeleted:t,ok:1}}catch(t){r={n:1,nDeleted:0,ok:0,err:l(t)}}else r={n:0,nDeleted:0,ok:0,err:`invalid id[${n}]`};return 0===r.ok&&s("del",[t],r.err),r}))}catch(t){e=!0,o=t}finally{await r.close(),r=null}return e||f("del",t,o),e?(s("del",t,o),Promise.reject(o)):o},c.delAll=async function(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=!1,r=new a(n.url),o=null;try{let e=r.db(n.db).collection(n.cl),u=jr(await e.deleteMany(t),"deletedCount",0);o={n:u,nDeleted:u,ok:1}}catch(t){e=!0,o=t}finally{await r.close(),r=null}return e||f("delAll",null,o),e?(s("delAll",null,o),Promise.reject(o)):o},c.selectByPkGfs=async function(e){let r=!1;if(!Io(e))return null;let o=new a(n.url),u=null;try{u={id:e,u8a:await(async e=>{let r=wo(),u=o.db(n.db),i=new t.GridFSBucket(u,{chunkSizeBytes:10485760,bucketName:n.cl}),c=Buffer.from(""),a=i.openDownloadStreamByName(e);return a.on("data",(function(t){c=Buffer.concat([c,t])})),a.on("error",(function(t){r.reject(t)})),a.on("end",(function(){let t=new Uint8Array(c);c=null,r.resolve(t)})),r})(e)}}catch(t){"ENOENT"===jr(t,"code")?u=null:(r=!0,u=t)}finally{await o.close(),o=null}return r?(s("selectByPkGfs",null,u),Promise.reject(u)):u},c.insertGfs=async function(r){let o=!1;if(!Do(r)&&!Co(r))return{n:0,nInserted:0,ok:1};let i=new a(n.url),c=null;try{await i.connect();let o=i.db(n.db),a=new t.GridFSBucket(o,{chunkSizeBytes:10485760,bucketName:n.cl}),l=o.collection(`${n.cl}.chunks`);Eo(r)||(r=[r]),r=zr(r,(function(t,e){if(!function(t){return"[object Uint8Array]"===Object.prototype.toString.call(t)}((t=v(t={...t},e)).u8a))throw new Error(`invalid data[${e}].u8a`);return t})),await async function(t){u||(await t.collection(`${n.cl}.files`).createIndex({filename:1},{unique:!0}),u=!0)}(o);let f=jo(r),s=0;await Xo(r,(async t=>{let n=await function(t,n,r,o){let u=wo(),i=t.openUploadStream(r),c=i.id,a=new e.Readable;return a._read=()=>{},a.push(Buffer.from(o)),a.push(null),a.pipe(i).on("error",(function(t){d(t)?n.deleteMany({files_id:c}).then((function(){u.resolve(!1)})).catch((function(t){u.reject(t)})):u.reject(t)})).on("finish",(function(){u.resolve(!0)})),u}(a,l,t.id,t.u8a);n&&s++})),c={n:f,nInserted:s,ok:1}}catch(t){o=!0,c=t}finally{await i.close(),i=null}return o||f("insertGfs",r,c),o?(s("insertGfs",r,c),Promise.reject(c)):c},c.delGfs=async function(e){let r=!1;if(!Do(e)&&!Co(e))return[];let o=new a(n.url),u=null;try{await o.connect();let r=o.db(n.db),i=new t.GridFSBucket(r,{chunkSizeBytes:10485760,bucketName:n.cl});Eo(e)||(e=[e]),u=await Xo(e,(async t=>{let e=jr(t,"id"),n=null;if(Io(e))try{let t=await b({filename:e},i),r=0;for(let e of t)await i.delete(e._id),r++;n={n:r>0?1:0,nDeleted:r,ok:1}}catch(t){n={n:1,nDeleted:0,ok:0,err:l(t)}}else n={n:0,nDeleted:0,ok:0,err:`invalid id[${e}]`};return 0===n.ok&&s("delGfs",[t],n.err),n}))}catch(t){r=!0,u=t}finally{await o.close(),o=null}return r||f("delGfs",e,u),r?(s("delGfs",e,u),Promise.reject(u)):u},c.delAllGfs=async function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=!1,o=new a(n.url),u=null;try{let r=o.db(n.db),i=new t.GridFSBucket(r,{chunkSizeBytes:10485760,bucketName:n.cl}),c=await b(e,i),a=zr(c,(function(t){return async function(t,e){await e.delete(t)}(t._id,i)}));await Promise.all(a);let l=jo(c);u={n:l,nDeleted:l,ok:1}}catch(t){r=!0,u=t}finally{await o.close(),o=null}return r||f("delAllGfs",null,u),r?(s("delAllGfs",null,u),Promise.reject(u)):u},c}}));
6
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("mongodb"),require("stream")):"function"==typeof define&&define.amd?define(["mongodb","stream"],e):(t="undefined"!=typeof globalThis?globalThis:t||self)["w-orm-mongodb"]=e(t.mongodb,t.stream)}(this,(function(t,e){"use strict";function n(t,e){return t===e||t!=t&&e!=e}function r(t,e){for(var r=t.length;r--;)if(n(t[r][0],e))return r;return-1}var o=Array.prototype.splice;function u(t){var e=-1,n=null==t?0:t.length;for(this.clear();++e<n;){var r=t[e];this.set(r[0],r[1])}}u.prototype.clear=function(){this.__data__=[],this.size=0},u.prototype.delete=function(t){var e=this.__data__,n=r(e,t);return!(n<0)&&(n==e.length-1?e.pop():o.call(e,n,1),--this.size,!0)},u.prototype.get=function(t){var e=this.__data__,n=r(e,t);return n<0?void 0:e[n][1]},u.prototype.has=function(t){return r(this.__data__,t)>-1},u.prototype.set=function(t,e){var n=this.__data__,o=r(n,t);return o<0?(++this.size,n.push([t,e])):n[o][1]=e,this};var i="object"==typeof global&&global&&global.Object===Object&&global,c="object"==typeof self&&self&&self.Object===Object&&self,a=i||c||Function("return this")(),l=a.Symbol,f=Object.prototype,s=f.hasOwnProperty,d=f.toString,v=l?l.toStringTag:void 0;var p=Object.prototype.toString;var y="[object Null]",b="[object Undefined]",h=l?l.toStringTag:void 0;function j(t){return null==t?void 0===t?b:y:h&&h in Object(t)?function(t){var e=s.call(t,v),n=t[v];try{t[v]=void 0;var r=!0}catch(t){}var o=d.call(t);return r&&(e?t[v]=n:delete t[v]),o}(t):function(t){return p.call(t)}(t)}function g(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}var _="[object AsyncFunction]",w="[object Function]",m="[object GeneratorFunction]",O="[object Proxy]";function A(t){if(!g(t))return!1;var e=j(t);return e==w||e==m||e==_||e==O}var k,x=a["__core-js_shared__"],S=(k=/[^.]+$/.exec(x&&x.keys&&x.keys.IE_PROTO||""))?"Symbol(src)_1."+k:"";var P=Function.prototype.toString;function B(t){if(null!=t){try{return P.call(t)}catch(t){}try{return t+""}catch(t){}}return""}var I=/^\[object .+?Constructor\]$/,z=Function.prototype,E=Object.prototype,M=z.toString,$=E.hasOwnProperty,C=RegExp("^"+M.call($).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");function D(t){return!(!g(t)||(e=t,S&&S in e))&&(A(t)?C:I).test(B(t));var e}function U(t,e){var n=function(t,e){return null==t?void 0:t[e]}(t,e);return D(n)?n:void 0}var F=U(a,"Map"),N=U(Object,"create");var G=Object.prototype.hasOwnProperty;var T=Object.prototype.hasOwnProperty;function L(t){var e=-1,n=null==t?0:t.length;for(this.clear();++e<n;){var r=t[e];this.set(r[0],r[1])}}function q(t,e){var n,r,o=t.__data__;return("string"==(r=typeof(n=e))||"number"==r||"symbol"==r||"boolean"==r?"__proto__"!==n:null===n)?o["string"==typeof e?"string":"hash"]:o.map}function R(t){var e=-1,n=null==t?0:t.length;for(this.clear();++e<n;){var r=t[e];this.set(r[0],r[1])}}L.prototype.clear=function(){this.__data__=N?N(null):{},this.size=0},L.prototype.delete=function(t){var e=this.has(t)&&delete this.__data__[t];return this.size-=e?1:0,e},L.prototype.get=function(t){var e=this.__data__;if(N){var n=e[t];return"__lodash_hash_undefined__"===n?void 0:n}return G.call(e,t)?e[t]:void 0},L.prototype.has=function(t){var e=this.__data__;return N?void 0!==e[t]:T.call(e,t)},L.prototype.set=function(t,e){var n=this.__data__;return this.size+=this.has(t)?0:1,n[t]=N&&void 0===e?"__lodash_hash_undefined__":e,this},R.prototype.clear=function(){this.size=0,this.__data__={hash:new L,map:new(F||u),string:new L}},R.prototype.delete=function(t){var e=q(this,t).delete(t);return this.size-=e?1:0,e},R.prototype.get=function(t){return q(this,t).get(t)},R.prototype.has=function(t){return q(this,t).has(t)},R.prototype.set=function(t,e){var n=q(this,t),r=n.size;return n.set(t,e),this.size+=n.size==r?0:1,this};function V(t){var e=this.__data__=new u(t);this.size=e.size}function W(t,e){for(var n=-1,r=null==t?0:t.length;++n<r&&!1!==e(t[n],n,t););return t}V.prototype.clear=function(){this.__data__=new u,this.size=0},V.prototype.delete=function(t){var e=this.__data__,n=e.delete(t);return this.size=e.size,n},V.prototype.get=function(t){return this.__data__.get(t)},V.prototype.has=function(t){return this.__data__.has(t)},V.prototype.set=function(t,e){var n=this.__data__;if(n instanceof u){var r=n.__data__;if(!F||r.length<199)return r.push([t,e]),this.size=++n.size,this;n=this.__data__=new R(r)}return n.set(t,e),this.size=n.size,this};var H=function(){try{var t=U(Object,"defineProperty");return t({},"",{}),t}catch(t){}}(),J=H;function K(t,e,n){"__proto__"==e&&J?J(t,e,{configurable:!0,enumerable:!0,value:n,writable:!0}):t[e]=n}var Q=Object.prototype.hasOwnProperty;function X(t,e,r){var o=t[e];Q.call(t,e)&&n(o,r)&&(void 0!==r||e in t)||K(t,e,r)}function Y(t,e,n,r){var o=!n;n||(n={});for(var u=-1,i=e.length;++u<i;){var c=e[u],a=r?r(n[c],t[c],c,n,t):void 0;void 0===a&&(a=t[c]),o?K(n,c,a):X(n,c,a)}return n}function Z(t){return null!=t&&"object"==typeof t}function tt(t){return Z(t)&&"[object Arguments]"==j(t)}var et=Object.prototype,nt=et.hasOwnProperty,rt=et.propertyIsEnumerable,ot=tt(function(){return arguments}())?tt:function(t){return Z(t)&&nt.call(t,"callee")&&!rt.call(t,"callee")},ut=ot,it=Array.isArray;var ct="object"==typeof exports&&exports&&!exports.nodeType&&exports,at=ct&&"object"==typeof module&&module&&!module.nodeType&&module,lt=at&&at.exports===ct?a.Buffer:void 0,ft=(lt?lt.isBuffer:void 0)||function(){return!1},st=9007199254740991,dt=/^(?:0|[1-9]\d*)$/;function vt(t,e){var n=typeof t;return!!(e=null==e?st:e)&&("number"==n||"symbol"!=n&&dt.test(t))&&t>-1&&t%1==0&&t<e}var pt=9007199254740991;function yt(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=pt}var bt={};function ht(t){return function(e){return t(e)}}bt["[object Float32Array]"]=bt["[object Float64Array]"]=bt["[object Int8Array]"]=bt["[object Int16Array]"]=bt["[object Int32Array]"]=bt["[object Uint8Array]"]=bt["[object Uint8ClampedArray]"]=bt["[object Uint16Array]"]=bt["[object Uint32Array]"]=!0,bt["[object Arguments]"]=bt["[object Array]"]=bt["[object ArrayBuffer]"]=bt["[object Boolean]"]=bt["[object DataView]"]=bt["[object Date]"]=bt["[object Error]"]=bt["[object Function]"]=bt["[object Map]"]=bt["[object Number]"]=bt["[object Object]"]=bt["[object RegExp]"]=bt["[object Set]"]=bt["[object String]"]=bt["[object WeakMap]"]=!1;var jt="object"==typeof exports&&exports&&!exports.nodeType&&exports,gt=jt&&"object"==typeof module&&module&&!module.nodeType&&module,_t=gt&&gt.exports===jt&&i.process,wt=function(){try{var t=gt&&gt.require&&gt.require("util").types;return t||_t&&_t.binding&&_t.binding("util")}catch(t){}}(),mt=wt&&wt.isTypedArray,Ot=mt?ht(mt):function(t){return Z(t)&&yt(t.length)&&!!bt[j(t)]},At=Object.prototype.hasOwnProperty;function kt(t,e){var n=it(t),r=!n&&ut(t),o=!n&&!r&&ft(t),u=!n&&!r&&!o&&Ot(t),i=n||r||o||u,c=i?function(t,e){for(var n=-1,r=Array(t);++n<t;)r[n]=e(n);return r}(t.length,String):[],a=c.length;for(var l in t)!e&&!At.call(t,l)||i&&("length"==l||o&&("offset"==l||"parent"==l)||u&&("buffer"==l||"byteLength"==l||"byteOffset"==l)||vt(l,a))||c.push(l);return c}var xt=Object.prototype;function St(t){var e=t&&t.constructor;return t===("function"==typeof e&&e.prototype||xt)}function Pt(t,e){return function(n){return t(e(n))}}var Bt=Pt(Object.keys,Object),It=Object.prototype.hasOwnProperty;function zt(t){if(!St(t))return Bt(t);var e=[];for(var n in Object(t))It.call(t,n)&&"constructor"!=n&&e.push(n);return e}function Et(t){return null!=t&&yt(t.length)&&!A(t)}function Mt(t){return Et(t)?kt(t):zt(t)}var $t=Object.prototype.hasOwnProperty;function Ct(t){if(!g(t))return function(t){var e=[];if(null!=t)for(var n in Object(t))e.push(n);return e}(t);var e=St(t),n=[];for(var r in t)("constructor"!=r||!e&&$t.call(t,r))&&n.push(r);return n}function Dt(t){return Et(t)?kt(t,!0):Ct(t)}var Ut="object"==typeof exports&&exports&&!exports.nodeType&&exports,Ft=Ut&&"object"==typeof module&&module&&!module.nodeType&&module,Nt=Ft&&Ft.exports===Ut?a.Buffer:void 0,Gt=Nt?Nt.allocUnsafe:void 0;function Tt(t,e){for(var n=-1,r=null==t?0:t.length,o=0,u=[];++n<r;){var i=t[n];e(i,n,t)&&(u[o++]=i)}return u}function Lt(){return[]}var qt=Object.prototype.propertyIsEnumerable,Rt=Object.getOwnPropertySymbols,Vt=Rt?function(t){return null==t?[]:(t=Object(t),Tt(Rt(t),(function(e){return qt.call(t,e)})))}:Lt;function Wt(t,e){for(var n=-1,r=e.length,o=t.length;++n<r;)t[o+n]=e[n];return t}var Ht=Pt(Object.getPrototypeOf,Object),Jt=Object.getOwnPropertySymbols?function(t){for(var e=[];t;)Wt(e,Vt(t)),t=Ht(t);return e}:Lt,Kt=Jt;function Qt(t,e,n){var r=e(t);return it(t)?r:Wt(r,n(t))}function Xt(t){return Qt(t,Mt,Vt)}function Yt(t){return Qt(t,Dt,Kt)}var Zt=U(a,"DataView"),te=U(a,"Promise"),ee=U(a,"Set"),ne=U(a,"WeakMap"),re="[object Map]",oe="[object Promise]",ue="[object Set]",ie="[object WeakMap]",ce="[object DataView]",ae=B(Zt),le=B(F),fe=B(te),se=B(ee),de=B(ne),ve=j;(Zt&&ve(new Zt(new ArrayBuffer(1)))!=ce||F&&ve(new F)!=re||te&&ve(te.resolve())!=oe||ee&&ve(new ee)!=ue||ne&&ve(new ne)!=ie)&&(ve=function(t){var e=j(t),n="[object Object]"==e?t.constructor:void 0,r=n?B(n):"";if(r)switch(r){case ae:return ce;case le:return re;case fe:return oe;case se:return ue;case de:return ie}return e});var pe=ve,ye=Object.prototype.hasOwnProperty;var be=a.Uint8Array;function he(t){var e=new t.constructor(t.byteLength);return new be(e).set(new be(t)),e}var je=/\w*$/;var ge=l?l.prototype:void 0,_e=ge?ge.valueOf:void 0;var we="[object Boolean]",me="[object Date]",Oe="[object Map]",Ae="[object Number]",ke="[object RegExp]",xe="[object Set]",Se="[object String]",Pe="[object Symbol]",Be="[object ArrayBuffer]",Ie="[object DataView]",ze="[object Float32Array]",Ee="[object Float64Array]",Me="[object Int8Array]",$e="[object Int16Array]",Ce="[object Int32Array]",De="[object Uint8Array]",Ue="[object Uint8ClampedArray]",Fe="[object Uint16Array]",Ne="[object Uint32Array]";function Ge(t,e,n){var r,o,u,i=t.constructor;switch(e){case Be:return he(t);case we:case me:return new i(+t);case Ie:return function(t,e){var n=e?he(t.buffer):t.buffer;return new t.constructor(n,t.byteOffset,t.byteLength)}(t,n);case ze:case Ee:case Me:case $e:case Ce:case De:case Ue:case Fe:case Ne:return function(t,e){var n=e?he(t.buffer):t.buffer;return new t.constructor(n,t.byteOffset,t.length)}(t,n);case Oe:return new i;case Ae:case Se:return new i(t);case ke:return(u=new(o=t).constructor(o.source,je.exec(o))).lastIndex=o.lastIndex,u;case xe:return new i;case Pe:return r=t,_e?Object(_e.call(r)):{}}}var Te=Object.create,Le=function(){function t(){}return function(e){if(!g(e))return{};if(Te)return Te(e);t.prototype=e;var n=new t;return t.prototype=void 0,n}}(),qe=Le;var Re=wt&&wt.isMap,Ve=Re?ht(Re):function(t){return Z(t)&&"[object Map]"==pe(t)};var We=wt&&wt.isSet,He=We?ht(We):function(t){return Z(t)&&"[object Set]"==pe(t)},Je=1,Ke=2,Qe=4,Xe="[object Arguments]",Ye="[object Function]",Ze="[object GeneratorFunction]",tn="[object Object]",en={};function nn(t,e,n,r,o,u){var i,c=e&Je,a=e&Ke,l=e&Qe;if(n&&(i=o?n(t,r,o,u):n(t)),void 0!==i)return i;if(!g(t))return t;var f=it(t);if(f){if(i=function(t){var e=t.length,n=new t.constructor(e);return e&&"string"==typeof t[0]&&ye.call(t,"index")&&(n.index=t.index,n.input=t.input),n}(t),!c)return function(t,e){var n=-1,r=t.length;for(e||(e=Array(r));++n<r;)e[n]=t[n];return e}(t,i)}else{var s=pe(t),d=s==Ye||s==Ze;if(ft(t))return function(t,e){if(e)return t.slice();var n=t.length,r=Gt?Gt(n):new t.constructor(n);return t.copy(r),r}(t,c);if(s==tn||s==Xe||d&&!o){if(i=a||d?{}:function(t){return"function"!=typeof t.constructor||St(t)?{}:qe(Ht(t))}(t),!c)return a?function(t,e){return Y(t,Kt(t),e)}(t,function(t,e){return t&&Y(e,Dt(e),t)}(i,t)):function(t,e){return Y(t,Vt(t),e)}(t,function(t,e){return t&&Y(e,Mt(e),t)}(i,t))}else{if(!en[s])return o?t:{};i=Ge(t,s,c)}}u||(u=new V);var v=u.get(t);if(v)return v;u.set(t,i),He(t)?t.forEach((function(r){i.add(nn(r,e,n,r,t,u))})):Ve(t)&&t.forEach((function(r,o){i.set(o,nn(r,e,n,o,t,u))}));var p=f?void 0:(l?a?Yt:Xt:a?Dt:Mt)(t);return W(p||t,(function(r,o){p&&(r=t[o=r]),X(i,o,nn(r,e,n,o,t,u))})),i}en[Xe]=en["[object Array]"]=en["[object ArrayBuffer]"]=en["[object DataView]"]=en["[object Boolean]"]=en["[object Date]"]=en["[object Float32Array]"]=en["[object Float64Array]"]=en["[object Int8Array]"]=en["[object Int16Array]"]=en["[object Int32Array]"]=en["[object Map]"]=en["[object Number]"]=en[tn]=en["[object RegExp]"]=en["[object Set]"]=en["[object String]"]=en["[object Symbol]"]=en["[object Uint8Array]"]=en["[object Uint8ClampedArray]"]=en["[object Uint16Array]"]=en["[object Uint32Array]"]=!0,en["[object Error]"]=en[Ye]=en["[object WeakMap]"]=!1;var rn=1,on=4;function un(t){return nn(t,rn|on)}var cn,an=function(t,e,n){for(var r=-1,o=Object(t),u=n(t),i=u.length;i--;){var c=u[cn?i:++r];if(!1===e(o[c],c,o))break}return t};var ln=function(t,e){return function(n,r){if(null==n)return n;if(!Et(n))return t(n,r);for(var o=n.length,u=e?o:-1,i=Object(n);(e?u--:++u<o)&&!1!==r(i[u],u,i););return n}}((function(t,e){return t&&an(t,e,Mt)})),fn=ln;function sn(t){return t}function dn(t,e){var n;return(it(t)?W:fn)(t,"function"==typeof(n=e)?n:sn)}function vn(t,e){for(var n=-1,r=null==t?0:t.length;++n<r;)if(!e(t[n],n,t))return!1;return!0}function pn(t,e){var n=!0;return fn(t,(function(t,r,o){return n=!!e(t,r,o)})),n}function yn(t){var e=-1,n=null==t?0:t.length;for(this.__data__=new R;++e<n;)this.add(t[e])}function bn(t,e){for(var n=-1,r=null==t?0:t.length;++n<r;)if(e(t[n],n,t))return!0;return!1}yn.prototype.add=yn.prototype.push=function(t){return this.__data__.set(t,"__lodash_hash_undefined__"),this},yn.prototype.has=function(t){return this.__data__.has(t)};var hn=1,jn=2;function gn(t,e,n,r,o,u){var i=n&hn,c=t.length,a=e.length;if(c!=a&&!(i&&a>c))return!1;var l=u.get(t),f=u.get(e);if(l&&f)return l==e&&f==t;var s=-1,d=!0,v=n&jn?new yn:void 0;for(u.set(t,e),u.set(e,t);++s<c;){var p=t[s],y=e[s];if(r)var b=i?r(y,p,s,e,t,u):r(p,y,s,t,e,u);if(void 0!==b){if(b)continue;d=!1;break}if(v){if(!bn(e,(function(t,e){if(i=e,!v.has(i)&&(p===t||o(p,t,n,r,u)))return v.push(e);var i}))){d=!1;break}}else if(p!==y&&!o(p,y,n,r,u)){d=!1;break}}return u.delete(t),u.delete(e),d}function _n(t){var e=-1,n=Array(t.size);return t.forEach((function(t,r){n[++e]=[r,t]})),n}function wn(t){var e=-1,n=Array(t.size);return t.forEach((function(t){n[++e]=t})),n}var mn=1,On=2,An="[object Boolean]",kn="[object Date]",xn="[object Error]",Sn="[object Map]",Pn="[object Number]",Bn="[object RegExp]",In="[object Set]",zn="[object String]",En="[object Symbol]",Mn="[object ArrayBuffer]",$n="[object DataView]",Cn=l?l.prototype:void 0,Dn=Cn?Cn.valueOf:void 0;var Un=1,Fn=Object.prototype.hasOwnProperty;var Nn=1,Gn="[object Arguments]",Tn="[object Array]",Ln="[object Object]",qn=Object.prototype.hasOwnProperty;function Rn(t,e,r,o,u,i){var c=it(t),a=it(e),l=c?Tn:pe(t),f=a?Tn:pe(e),s=(l=l==Gn?Ln:l)==Ln,d=(f=f==Gn?Ln:f)==Ln,v=l==f;if(v&&ft(t)){if(!ft(e))return!1;c=!0,s=!1}if(v&&!s)return i||(i=new V),c||Ot(t)?gn(t,e,r,o,u,i):function(t,e,r,o,u,i,c){switch(r){case $n:if(t.byteLength!=e.byteLength||t.byteOffset!=e.byteOffset)return!1;t=t.buffer,e=e.buffer;case Mn:return!(t.byteLength!=e.byteLength||!i(new be(t),new be(e)));case An:case kn:case Pn:return n(+t,+e);case xn:return t.name==e.name&&t.message==e.message;case Bn:case zn:return t==e+"";case Sn:var a=_n;case In:var l=o&mn;if(a||(a=wn),t.size!=e.size&&!l)return!1;var f=c.get(t);if(f)return f==e;o|=On,c.set(t,e);var s=gn(a(t),a(e),o,u,i,c);return c.delete(t),s;case En:if(Dn)return Dn.call(t)==Dn.call(e)}return!1}(t,e,l,r,o,u,i);if(!(r&Nn)){var p=s&&qn.call(t,"__wrapped__"),y=d&&qn.call(e,"__wrapped__");if(p||y){var b=p?t.value():t,h=y?e.value():e;return i||(i=new V),u(b,h,r,o,i)}}return!!v&&(i||(i=new V),function(t,e,n,r,o,u){var i=n&Un,c=Xt(t),a=c.length;if(a!=Xt(e).length&&!i)return!1;for(var l=a;l--;){var f=c[l];if(!(i?f in e:Fn.call(e,f)))return!1}var s=u.get(t),d=u.get(e);if(s&&d)return s==e&&d==t;var v=!0;u.set(t,e),u.set(e,t);for(var p=i;++l<a;){var y=t[f=c[l]],b=e[f];if(r)var h=i?r(b,y,f,e,t,u):r(y,b,f,t,e,u);if(!(void 0===h?y===b||o(y,b,n,r,u):h)){v=!1;break}p||(p="constructor"==f)}if(v&&!p){var j=t.constructor,g=e.constructor;j==g||!("constructor"in t)||!("constructor"in e)||"function"==typeof j&&j instanceof j&&"function"==typeof g&&g instanceof g||(v=!1)}return u.delete(t),u.delete(e),v}(t,e,r,o,u,i))}function Vn(t,e,n,r,o){return t===e||(null==t||null==e||!Z(t)&&!Z(e)?t!=t&&e!=e:Rn(t,e,n,r,Vn,o))}var Wn=1,Hn=2;function Jn(t){return t==t&&!g(t)}function Kn(t,e){return function(n){return null!=n&&(n[t]===e&&(void 0!==e||t in Object(n)))}}function Qn(t){var e=function(t){for(var e=Mt(t),n=e.length;n--;){var r=e[n],o=t[r];e[n]=[r,o,Jn(o)]}return e}(t);return 1==e.length&&e[0][2]?Kn(e[0][0],e[0][1]):function(n){return n===t||function(t,e,n,r){var o=n.length,u=o,i=!r;if(null==t)return!u;for(t=Object(t);o--;){var c=n[o];if(i&&c[2]?c[1]!==t[c[0]]:!(c[0]in t))return!1}for(;++o<u;){var a=(c=n[o])[0],l=t[a],f=c[1];if(i&&c[2]){if(void 0===l&&!(a in t))return!1}else{var s=new V;if(r)var d=r(l,f,a,t,e,s);if(!(void 0===d?Vn(f,l,Wn|Hn,r,s):d))return!1}}return!0}(n,t,e)}}var Xn="[object Symbol]";function Yn(t){return"symbol"==typeof t||Z(t)&&j(t)==Xn}var Zn=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,tr=/^\w*$/;function er(t,e){if(it(t))return!1;var n=typeof t;return!("number"!=n&&"symbol"!=n&&"boolean"!=n&&null!=t&&!Yn(t))||(tr.test(t)||!Zn.test(t)||null!=e&&t in Object(e))}var nr="Expected a function";function rr(t,e){if("function"!=typeof t||null!=e&&"function"!=typeof e)throw new TypeError(nr);var n=function(){var r=arguments,o=e?e.apply(this,r):r[0],u=n.cache;if(u.has(o))return u.get(o);var i=t.apply(this,r);return n.cache=u.set(o,i)||u,i};return n.cache=new(rr.Cache||R),n}rr.Cache=R;var or,ur,ir,cr=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,ar=/\\(\\)?/g,lr=(or=function(t){var e=[];return 46===t.charCodeAt(0)&&e.push(""),t.replace(cr,(function(t,n,r,o){e.push(r?o.replace(ar,"$1"):n||t)})),e},ur=rr(or,(function(t){return 500===ir.size&&ir.clear(),t})),ir=ur.cache,ur),fr=lr;function sr(t,e){for(var n=-1,r=null==t?0:t.length,o=Array(r);++n<r;)o[n]=e(t[n],n,t);return o}var dr=1/0,vr=l?l.prototype:void 0,pr=vr?vr.toString:void 0;function yr(t){if("string"==typeof t)return t;if(it(t))return sr(t,yr)+"";if(Yn(t))return pr?pr.call(t):"";var e=t+"";return"0"==e&&1/t==-dr?"-0":e}function br(t,e){return it(t)?t:er(t,e)?[t]:fr(function(t){return null==t?"":yr(t)}(t))}var hr=1/0;function jr(t){if("string"==typeof t||Yn(t))return t;var e=t+"";return"0"==e&&1/t==-hr?"-0":e}function gr(t,e){for(var n=0,r=(e=br(e,t)).length;null!=t&&n<r;)t=t[jr(e[n++])];return n&&n==r?t:void 0}function _r(t,e,n){var r=null==t?void 0:gr(t,e);return void 0===r?n:r}function wr(t,e){return null!=t&&e in Object(t)}function mr(t,e){return null!=t&&function(t,e,n){for(var r=-1,o=(e=br(e,t)).length,u=!1;++r<o;){var i=jr(e[r]);if(!(u=null!=t&&n(t,i)))break;t=t[i]}return u||++r!=o?u:!!(o=null==t?0:t.length)&&yt(o)&&vt(i,o)&&(it(t)||ut(t))}(t,e,wr)}var Or=1,Ar=2;function kr(t){return function(e){return null==e?void 0:e[t]}}function xr(t){return er(t)?kr(jr(t)):function(t){return function(e){return gr(e,t)}}(t)}function Sr(t){return"function"==typeof t?t:null==t?sn:"object"==typeof t?it(t)?(e=t[0],n=t[1],er(e)&&Jn(n)?Kn(jr(e),n):function(t){var r=_r(t,e);return void 0===r&&r===n?mr(t,e):Vn(n,r,Or|Ar)}):Qn(t):xr(t);var e,n}function Pr(t,e,r){var o=it(t)?vn:pn;return r&&function(t,e,r){if(!g(r))return!1;var o=typeof e;return!!("number"==o?Et(r)&&vt(e,r.length):"string"==o&&e in r)&&n(r[e],t)}(t,e,r)&&(e=void 0),o(t,Sr(e))}function Br(t,e){var n=[];return fn(t,(function(t,r,o){e(t,r,o)&&n.push(t)})),n}function Ir(t,e){var n=-1,r=Et(t)?Array(t.length):[];return fn(t,(function(t,o,u){r[++n]=e(t,o,u)})),r}function zr(t,e){return(it(t)?sr:Ir)(t,Sr(e))}function Er(t,e,n){var r=-1,o=t.length;e<0&&(e=-e>o?0:o+e),(n=n>o?o:n)<0&&(n+=o),o=e>n?0:n-e>>>0,e>>>=0;for(var u=Array(o);++r<o;)u[r]=t[r+e];return u}var Mr=Object.prototype.hasOwnProperty;function $r(t,e){var n=-1,r=(e=br(e,t)).length;if(!r)return!0;for(;++n<r;){var o=jr(e[n]);if("__proto__"===o&&!Mr.call(t,"__proto__"))return!1;if(("constructor"===o||"prototype"===o)&&n<r-1)return!1}var u=function(t,e){return e.length<2?t:gr(t,Er(e,0,-1))}(t,e);return null==u||delete u[jr(function(t){var e=null==t?0:t.length;return e?t[e-1]:void 0}(e))]}var Cr="[object Object]",Dr=Function.prototype,Ur=Object.prototype,Fr=Dr.toString,Nr=Ur.hasOwnProperty,Gr=Fr.call(Object);function Tr(t){return function(t){if(!Z(t)||j(t)!=Cr)return!1;var e=Ht(t);if(null===e)return!0;var n=Nr.call(e,"constructor")&&e.constructor;return"function"==typeof n&&n instanceof n&&Fr.call(n)==Gr}(t)?void 0:t}var Lr=l?l.isConcatSpreadable:void 0;function qr(t){return it(t)||ut(t)||!!(Lr&&t&&t[Lr])}function Rr(t,e,n,r,o){var u=-1,i=t.length;for(n||(n=qr),o||(o=[]);++u<i;){var c=t[u];e>0&&n(c)?e>1?Rr(c,e-1,n,r,o):Wt(o,c):r||(o[o.length]=c)}return o}function Vr(t){return(null==t?0:t.length)?Rr(t,1):[]}var Wr=Math.max;var Hr=J?function(t,e){return J(t,"toString",{configurable:!0,enumerable:!1,value:(n=e,function(){return n}),writable:!0});var n}:sn,Jr=Hr,Kr=Date.now;var Qr=function(t){var e=0,n=0;return function(){var r=Kr(),o=16-(r-n);if(n=r,o>0){if(++e>=800)return arguments[0]}else e=0;return t.apply(void 0,arguments)}}(Jr),Xr=Qr;var Yr=function(t){return Xr(function(t,e,n){return e=Wr(void 0===e?t.length-1:e,0),function(){for(var r=arguments,o=-1,u=Wr(r.length-e,0),i=Array(u);++o<u;)i[o]=r[e+o];o=-1;for(var c=Array(e+1);++o<e;)c[o]=r[o];return c[e]=n(i),function(t,e,n){switch(n.length){case 0:return t.call(e);case 1:return t.call(e,n[0]);case 2:return t.call(e,n[0],n[1]);case 3:return t.call(e,n[0],n[1],n[2])}return t.apply(e,n)}(t,this,c)}}(t,void 0,Vr),t+"")}((function(t,e){var n={};if(null==t)return n;var r=!1;e=sr(e,(function(e){return e=br(e,t),r||(r=e.length>1),e})),Y(t,Yt(t),n),r&&(n=nn(n,7,Tr));for(var o=e.length;o--;)$r(n,e[o]);return n})),Zr=Yr,to="[object String]";var eo=kr("length"),no=RegExp("[\\u200d\\ud800-\\udfff\\u0300-\\u036f\\ufe20-\\ufe2f\\u20d0-\\u20ff\\ufe0e\\ufe0f]");var ro="\\ud800-\\udfff",oo="["+ro+"]",uo="[\\u0300-\\u036f\\ufe20-\\ufe2f\\u20d0-\\u20ff]",io="\\ud83c[\\udffb-\\udfff]",co="[^"+ro+"]",ao="(?:\\ud83c[\\udde6-\\uddff]){2}",lo="[\\ud800-\\udbff][\\udc00-\\udfff]",fo="(?:"+uo+"|"+io+")"+"?",so="[\\ufe0e\\ufe0f]?",vo=so+fo+("(?:\\u200d(?:"+[co,ao,lo].join("|")+")"+so+fo+")*"),po="(?:"+[co+uo+"?",uo,ao,lo,oo].join("|")+")",yo=RegExp(io+"(?="+io+")|"+po+vo,"g");function bo(t){return function(t){return no.test(t)}(t)?function(t){for(var e=yo.lastIndex=0;yo.test(t);)++e;return e}(t):eo(t)}var ho="[object Map]",jo="[object Set]";function go(t){if(null==t)return 0;if(Et(t))return"string"==typeof(e=t)||!it(e)&&Z(e)&&j(e)==to?bo(t):t.length;var e,n=pe(t);return n==ho||n==jo?t.size:zt(t).length}var _o={};!function(t){var e=Object.prototype.hasOwnProperty,n="~";function r(){}function o(t,e,n){this.fn=t,this.context=e,this.once=n||!1}function u(t,e,r,u,i){if("function"!=typeof r)throw new TypeError("The listener must be a function");var c=new o(r,u||t,i),a=n?n+e:e;return t._events[a]?t._events[a].fn?t._events[a]=[t._events[a],c]:t._events[a].push(c):(t._events[a]=c,t._eventsCount++),t}function i(t,e){0==--t._eventsCount?t._events=new r:delete t._events[e]}function c(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),c.prototype.eventNames=function(){var t,r,o=[];if(0===this._eventsCount)return o;for(r in t=this._events)e.call(t,r)&&o.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?o.concat(Object.getOwnPropertySymbols(t)):o},c.prototype.listeners=function(t){var e=n?n+t:t,r=this._events[e];if(!r)return[];if(r.fn)return[r.fn];for(var o=0,u=r.length,i=new Array(u);o<u;o++)i[o]=r[o].fn;return i},c.prototype.listenerCount=function(t){var e=n?n+t:t,r=this._events[e];return r?r.fn?1:r.length:0},c.prototype.emit=function(t,e,r,o,u,i){var c=n?n+t:t;if(!this._events[c])return!1;var a,l,f=this._events[c],s=arguments.length;if(f.fn){switch(f.once&&this.removeListener(t,f.fn,void 0,!0),s){case 1:return f.fn.call(f.context),!0;case 2:return f.fn.call(f.context,e),!0;case 3:return f.fn.call(f.context,e,r),!0;case 4:return f.fn.call(f.context,e,r,o),!0;case 5:return f.fn.call(f.context,e,r,o,u),!0;case 6:return f.fn.call(f.context,e,r,o,u,i),!0}for(l=1,a=new Array(s-1);l<s;l++)a[l-1]=arguments[l];f.fn.apply(f.context,a)}else{var d,v=f.length;for(l=0;l<v;l++)switch(f[l].once&&this.removeListener(t,f[l].fn,void 0,!0),s){case 1:f[l].fn.call(f[l].context);break;case 2:f[l].fn.call(f[l].context,e);break;case 3:f[l].fn.call(f[l].context,e,r);break;case 4:f[l].fn.call(f[l].context,e,r,o);break;default:if(!a)for(d=1,a=new Array(s-1);d<s;d++)a[d-1]=arguments[d];f[l].fn.apply(f[l].context,a)}}return!0},c.prototype.on=function(t,e,n){return u(this,t,e,n,!1)},c.prototype.once=function(t,e,n){return u(this,t,e,n,!0)},c.prototype.removeListener=function(t,e,r,o){var u=n?n+t:t;if(!this._events[u])return this;if(!e)return i(this,u),this;var c=this._events[u];if(c.fn)c.fn!==e||o&&!c.once||r&&c.context!==r||i(this,u);else{for(var a=0,l=[],f=c.length;a<f;a++)(c[a].fn!==e||o&&!c[a].once||r&&c[a].context!==r)&&l.push(c[a]);l.length?this._events[u]=1===l.length?l[0]:l:i(this,u)}return this},c.prototype.removeAllListeners=function(t){var e;return t?(e=n?n+t:t,this._events[e]&&i(this,e)):(this._events=new r,this._eventsCount=0),this},c.prototype.off=c.prototype.removeListener,c.prototype.addListener=c.prototype.on,c.prefixed=n,c.EventEmitter=c,t.exports=c}({get exports(){return _o},set exports(t){_o=t}});var wo=_o;function mo(){let t,e,n=new Promise((function(){t=arguments[0],e=arguments[1]}));return n.resolve=t,n.reject=e,n}const Oo=[];for(let t=0;t<256;++t)Oo.push((t+256).toString(16).slice(1));const Ao=new Uint8Array(16);function ko(){return crypto.getRandomValues(Ao)}const xo={};function So(t,e,n){let r;if(t)r=Po(t.random??t.rng?.()??ko(),t.msecs,t.seq,e,n);else{const t=Date.now(),o=ko();!function(t,e,n){t.msecs??=-1/0,t.seq??=0,e>t.msecs?(t.seq=n[6]<<23|n[7]<<16|n[8]<<8|n[9],t.msecs=e):(t.seq=t.seq+1|0,0===t.seq&&t.msecs++)}(xo,t,o),r=Po(o,xo.msecs,xo.seq,e,n)}return e??function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return(Oo[t[e+0]]+Oo[t[e+1]]+Oo[t[e+2]]+Oo[t[e+3]]+"-"+Oo[t[e+4]]+Oo[t[e+5]]+"-"+Oo[t[e+6]]+Oo[t[e+7]]+"-"+Oo[t[e+8]]+Oo[t[e+9]]+"-"+Oo[t[e+10]]+Oo[t[e+11]]+Oo[t[e+12]]+Oo[t[e+13]]+Oo[t[e+14]]+Oo[t[e+15]]).toLowerCase()}(r)}function Po(t,e,n,r){let o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:0;if(t.length<16)throw new Error("Random bytes length must be >= 16");if(r){if(o<0||o+16>r.length)throw new RangeError(`UUID byte range ${o}:${o+15} is out of buffer bounds`)}else r=new Uint8Array(16),o=0;return e??=Date.now(),n??=127*t[6]<<24|t[7]<<16|t[8]<<8|t[9],r[o++]=e/1099511627776&255,r[o++]=e/4294967296&255,r[o++]=e/16777216&255,r[o++]=e/65536&255,r[o++]=e/256&255,r[o++]=255&e,r[o++]=112|n>>>28&15,r[o++]=n>>>20&255,r[o++]=128|n>>>14&63,r[o++]=n>>>6&255,r[o++]=n<<2&255|3&t[10],r[o++]=t[11],r[o++]=t[12],r[o++]=t[13],r[o++]=t[14],r[o++]=t[15],r}var Bo="[object Boolean]";function Io(t){return!0===(e=t)||!1===e||Z(e)&&j(e)==Bo;var e}function zo(t){return"[object String]"===Object.prototype.toString.call(t)}function Eo(t){return!(!zo(t)||""===t)}function Mo(t){return"[object Array]"===Object.prototype.toString.call(t)}function $o(t){return"[object Object]"===Object.prototype.toString.call(t)}function Co(t){return t!=t}function Do(t){return!!function(t){return"[object Undefined]"===Object.prototype.toString.call(t)}(t)||(!!function(t){return"[object Null]"===Object.prototype.toString.call(t)}(t)||(!!function(t){if($o(t)){for(let e in t)return!1;return!0}return!1}(t)||(!!function(t){return!(!zo(t)||""!==t)}(t)||(!!function(t){return!!Mo(t)&&0===t.length}(t)||!!Co(t)))))}function Uo(t){return!!Mo(t)&&(0!==t.length&&(1!==t.length||!Do(t[0])))}function Fo(t){if($o(t)){for(let e in t)return!0;return!1}return!1}function No(t){let e=!1;if(Eo(t))e=!isNaN(Number(t));else if(function(t){return"[object Number]"===Object.prototype.toString.call(t)}(t)){if(Co(t))return!1;e=!0}return e}var Go=/\s/;var To=/^\s+/;function Lo(t){return t?t.slice(0,function(t){for(var e=t.length;e--&&Go.test(t.charAt(e)););return e}(t)+1).replace(To,""):t}var qo=NaN,Ro=/^[-+]0x[0-9a-f]+$/i,Vo=/^0b[01]+$/i,Wo=/^0o[0-7]+$/i,Ho=parseInt;var Jo=1/0,Ko=17976931348623157e292;function Qo(t){return t?(t=function(t){if("number"==typeof t)return t;if(Yn(t))return qo;if(g(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=g(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=Lo(t);var n=Vo.test(t);return n||Wo.test(t)?Ho(t.slice(2),n?2:8):Ro.test(t)?qo:+t}(t))===Jo||t===-Jo?(t<0?-1:1)*Ko:t==t?t:0:0===t?t:0}function Xo(t,e,n){var r=null==t?0:t.length;return r?(e=n||void 0===e?1:function(t){var e=Qo(t),n=e%1;return e==e?n?e-n:e:0}(e),Er(t,e<0?0:e,r)):[]}function Yo(t){let e=Object.prototype.toString.call(t);return"[object Function]"===e||"[object AsyncFunction]"===e}function Zo(t,e){let n=mo();if(!Mo(t)&&!$o(t))return n.reject("rs is not an array or object"),n;let r=!1;if($o(t)){r=!0;let e=[];dn(t,((t,n)=>{e.push({k:n,v:t})})),t=e}Yo(e)||(e=function(t){return t});let o=-1,u=[];return t.reduce((function(t,n){return t.then((function(t){u.push(t),o+=1;let i=o,c=n;return r&&(i=n.k,c=n.v),Yo(e)?e(c,i):c}))}),Promise.resolve()).then((function(t){u.push(t),u=Xo(u),n.resolve(u)})).catch((function(t){n.reject(t)})),n}return function(){let n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};n.url||(n.url="mongodb://127.0.0.1:27017"),n.db||(n.db="worm"),n.cl||(n.cl="test");let r=_r(n,"autoGenPk");Io(r)||(r=!0);let o=!1,u=!1,i=null,c=new wo,a=t.MongoClient;function l(t){let e=_r(t,"message");return Eo(e)?e:String(t)}function f(t,e,n){try{c.emit("change",t,e,n)}catch(t){console.log(t)}}function s(t,e,n){try{c.emit("error",t,e,l(n))}catch(t){console.log(t)}}function d(t,e){if(!Eo(t.id)){if(!r)throw new Error(`invalid data[${e}].id, autoGenPk is false`);t.id=So()}return t}function v(t){let e=_r(t,"writeErrors");return Uo(e)?Pr(e,(function(t){return 11e3===_r(t,"code")||11e3===_r(t,"err.code")})):11e3===_r(t,"code")}async function p(t){o||(await t.createIndex({id:1},{unique:!0}),o=!0)}async function y(t,e){let n=null;try{await t.insertMany(e,{ordered:!1})}catch(t){n=t}if(null===n)return;let r=zr(e,(function(t){return t._id}));throw r=function(t,e){return(it(t)?Tt:Br)(t,Sr(e))}(r,(function(t){return null!=t})),go(r)>0&&await t.deleteMany({_id:{$in:r}}),n}async function b(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=arguments.length>1?arguments[1]:void 0,n=!1,r=null;try{let n=e.find(t);r=await n.toArray()}catch(t){n=!0,r=t}return n?Promise.reject(r):r}return c.select=async function(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=!1,r=new a(n.url),o=null;try{let e=r.db(n.db).collection(n.cl).find(t);o=await e.toArray(),o=zr(o,(function(t){return t=Zr(t,"_id")}))}catch(t){e=!0,o=t}finally{await r.close(),r=null}return e?(s("select",null,o),Promise.reject(o)):o},c.selectByPk=async function(t){let e=!1;if(!Eo(t))return null;let r=new a(n.url),o=null;try{let e=r.db(n.db).collection(n.cl),u=await e.findOne({id:t},{projection:{_id:0}});o=Fo(u)?u:null}catch(t){e=!0,o=t}finally{await r.close(),r=null}return e?(s("selectByPk",null,o),Promise.reject(o)):o},c.insert=async function(t){let e=!1,r=_r(arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},"returnList");if(Io(r)||(r=!1),!Fo(t)&&!Uo(t))return r?[]:{n:0,nInserted:0,ok:1};t=un(t);let o=new a(n.url),u=null;try{await o.connect();let e=o.db(n.db).collection(n.cl);Mo(t)||(t=[t]),t=zr(t,d),await p(e);let i=go(t),c=0,a={};try{c=(await e.insertMany(t,{ordered:!1})).insertedCount}catch(t){if(!v(t))throw t;c=_r(t,"result.insertedCount",0),dn(_r(t,"writeErrors",[]),(function(t){let e=_r(t,"index",_r(t,"err.index"));No(e)&&(a[e]=!0)}))}u=r?zr(t,(function(t,e){return{n:1,nInserted:!0===a[e]?0:1,ok:1}})):{n:i,nInserted:c,ok:1}}catch(t){e=!0,u=t}finally{await o.close(),o=null}return e||f("insert",t,u),e?(s("insert",t,u),Promise.reject(u)):u},c.insertBulk=async function(t){let e=!1;if(!Fo(t)&&!Uo(t))return{n:0,nInserted:0,ok:1};t=un(t);let r=new a(n.url),o=null;try{await r.connect();let e=r.db(n.db).collection(n.cl);Mo(t)||(t=[t]),t=zr(t,d),await p(e);let u=go(t),c=await async function(t){if(Io(i))return i;let e=await t.db(n.db).admin().command({hello:1});return i=Eo(_r(e,"setName"))||"isdbgrid"===_r(e,"msg"),i}(r);c?await async function(t,e,n){let r=t.startSession();try{await r.withTransaction((async()=>{await e.insertMany(n,{session:r,ordered:!0})}))}finally{await r.endSession()}}(r,e,t):await y(e,t),o={n:u,nInserted:u,ok:1}}catch(t){e=!0,o=t}finally{await r.close(),r=null}return e||f("insertBulk",t,o),e?(s("insertBulk",t,o),Promise.reject(o)):o},c.save=async function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=!1;if(!Fo(t)&&!Uo(t))return[];t=un(t);let o=_r(e,"autoInsert",!0),u=new a(n.url),i=null;try{await u.connect();let e=u.db(n.db).collection(n.cl);Mo(t)||(t=[t]),t=zr(t,d),await p(e),i=await Zo(t,(async t=>async function(t,e,n){let r=null,o=!1;for(let u=0;u<3;u++)try{let u=await t.updateOne({id:e.id},{$set:e},{upsert:n}),i=_r(u,"matchedCount",0),c=_r(u,"modifiedCount",0);_r(u,"upsertedCount",0)>0?(r={n:1,nInserted:1,nModified:0,ok:1},o=!0):r=i>0?{n:1,nInserted:0,nModified:c>0?1:0,ok:1}:{n:0,nInserted:0,nModified:0,ok:1};break}catch(t){if(v(t)&&u<2)continue;r={n:1,nInserted:0,nModified:0,ok:0,err:l(t)};break}return o&&f("insert",[e],r),0===r.ok&&s("save",[e],r.err),r}(e,t,o)))}catch(t){r=!0,i=t}finally{await u.close(),u=null}return r||f("save",t,i),r?(s("save",t,i),Promise.reject(i)):i},c.del=async function(t){let e=!1;if(!Fo(t)&&!Uo(t))return[];t=un(t);let r=new a(n.url),o=null;try{await r.connect();let e=r.db(n.db).collection(n.cl);Mo(t)||(t=[t]),o=await Zo(t,(async t=>{let n=_r(t,"id"),r=null;if(Eo(n))try{let t=_r(await e.deleteOne({id:n}),"deletedCount",0);r={n:t,nDeleted:t,ok:1}}catch(t){r={n:1,nDeleted:0,ok:0,err:l(t)}}else r={n:0,nDeleted:0,ok:0,err:`invalid id[${n}]`};return 0===r.ok&&s("del",[t],r.err),r}))}catch(t){e=!0,o=t}finally{await r.close(),r=null}return e||f("del",t,o),e?(s("del",t,o),Promise.reject(o)):o},c.delAll=async function(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=!1,r=new a(n.url),o=null;try{let e=r.db(n.db).collection(n.cl),u=_r(await e.deleteMany(t),"deletedCount",0);o={n:u,nDeleted:u,ok:1}}catch(t){e=!0,o=t}finally{await r.close(),r=null}return e||f("delAll",null,o),e?(s("delAll",null,o),Promise.reject(o)):o},c.selectByPkGfs=async function(e){let r=!1;if(!Eo(e))return null;let o=new a(n.url),u=null;try{u={id:e,u8a:await(async e=>{let r=mo(),u=o.db(n.db),i=new t.GridFSBucket(u,{chunkSizeBytes:10485760,bucketName:n.cl}),c=Buffer.from(""),a=i.openDownloadStreamByName(e);return a.on("data",(function(t){c=Buffer.concat([c,t])})),a.on("error",(function(t){r.reject(t)})),a.on("end",(function(){let t=new Uint8Array(c);c=null,r.resolve(t)})),r})(e)}}catch(t){"ENOENT"===_r(t,"code")?u=null:(r=!0,u=t)}finally{await o.close(),o=null}return r?(s("selectByPkGfs",null,u),Promise.reject(u)):u},c.insertGfs=async function(r){let o=!1;if(!Fo(r)&&!Uo(r))return{n:0,nInserted:0,ok:1};let i=new a(n.url),c=null;try{await i.connect();let o=i.db(n.db),a=new t.GridFSBucket(o,{chunkSizeBytes:10485760,bucketName:n.cl}),l=o.collection(`${n.cl}.chunks`);Mo(r)||(r=[r]),r=zr(r,(function(t,e){if(!function(t){return"[object Uint8Array]"===Object.prototype.toString.call(t)}((t=d(t={...t},e)).u8a))throw new Error(`invalid data[${e}].u8a`);return t})),await async function(t){u||(await t.collection(`${n.cl}.files`).createIndex({filename:1},{unique:!0}),u=!0)}(o);let f=go(r),s=0;await Zo(r,(async t=>{let n=await function(t,n,r,o){let u=mo(),i=t.openUploadStream(r),c=i.id,a=new e.Readable;return a._read=()=>{},a.push(Buffer.from(o)),a.push(null),a.pipe(i).on("error",(function(t){v(t)?n.deleteMany({files_id:c}).then((function(){u.resolve(!1)})).catch((function(t){u.reject(t)})):u.reject(t)})).on("finish",(function(){u.resolve(!0)})),u}(a,l,t.id,t.u8a);n&&s++})),c={n:f,nInserted:s,ok:1}}catch(t){o=!0,c=t}finally{await i.close(),i=null}return o||f("insertGfs",r,c),o?(s("insertGfs",r,c),Promise.reject(c)):c},c.delGfs=async function(e){let r=!1;if(!Fo(e)&&!Uo(e))return[];let o=new a(n.url),u=null;try{await o.connect();let r=o.db(n.db),i=new t.GridFSBucket(r,{chunkSizeBytes:10485760,bucketName:n.cl});Mo(e)||(e=[e]),u=await Zo(e,(async t=>{let e=_r(t,"id"),n=null;if(Eo(e))try{let t=await b({filename:e},i),r=0;for(let e of t)await i.delete(e._id),r++;n={n:r>0?1:0,nDeleted:r,ok:1}}catch(t){n={n:1,nDeleted:0,ok:0,err:l(t)}}else n={n:0,nDeleted:0,ok:0,err:`invalid id[${e}]`};return 0===n.ok&&s("delGfs",[t],n.err),n}))}catch(t){r=!0,u=t}finally{await o.close(),o=null}return r||f("delGfs",e,u),r?(s("delGfs",e,u),Promise.reject(u)):u},c.delAllGfs=async function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=!1,o=new a(n.url),u=null;try{let r=o.db(n.db),i=new t.GridFSBucket(r,{chunkSizeBytes:10485760,bucketName:n.cl}),c=await b(e,i),a=zr(c,(function(t){return async function(t,e){await e.delete(t)}(t._id,i)}));await Promise.all(a);let l=go(c);u={n:l,nDeleted:l,ok:1}}catch(t){r=!0,u=t}finally{await o.close(),o=null}return r||f("delAllGfs",null,u),r?(s("delAllGfs",null,u),Promise.reject(u)):u},c}}));
7
7
  //# sourceMappingURL=w-orm-mongodb.umd.js.map