lemoncloud-engine-js
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,1 +0,1 @@ | ||
"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},_slicedToArray=function(e,r){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return function(e,r){var t=[],n=!0,i=!1,o=void 0;try{for(var d,u=e[Symbol.iterator]();!(n=(d=u.next()).done)&&(t.push(d.value),!r||t.length!==r);n=!0);}catch(e){i=!0,o=e}finally{try{!n&&u.return&&u.return()}finally{if(i)throw o}}return t}(e,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")};function _defineProperty(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}module.exports=function(e,r,t){var n=r||"LEM",f=e.U,h=e._,i=e.MS,u=e.DS,o=e.RS,d=e.ES,_=e.ES6;if(!f)throw new Error("$U is required!");if(!h)throw new Error("$U is required!");if(!i)throw new Error("$MS is required!");if(!u)throw new Error("$DS is required!");if(!o)throw new Error("$RS is required!");var m=e.log,a=e.inf,l=e.err,p=f.NS(n,"green"),c=t||{},s=function(e){throw new Error("NOT_IMPLEMENTED - "+p+":"+JSON.stringify(e))};c.do_prepare=s,c.do_create=s,c.do_clone=s,c.do_update=s,c.do_increment=s,c.do_read=s,c.do_readX=s,c.do_delete=s,c.do_destroy=s,c.do_search=s,c.do_initialize=s,c.do_terminate=s,c.do_test_self=s,c.on_records=s,c.do_notify=s,c.do_subscribe=s,c.do_next_id=s,r.startsWith("_")||e(r,c);var v=function(e,r){return void 0===c[e]?r:c[e]},E=v("VERSION",1),y=v("REVISION",1),w=v("VERSION_NAME","V"),P=v("REVISION_NAME","R"),g=v("ID_INPUT","id"),S=v("ID_NAME","id"),j=v("ID_TYPE","test"),N=v("ID_NEXT",0),b=v("DYNA_TABLE","TestTable"),q=v("REDIS_PKEY","TPKEY"),O=v("DEFAULTS",null),A=v("CLONEABLE",!1),I=v("CLONED_ID","cloned"),R=v("PARENT_ID","parent"),D=v("PARENT_IMUT",!0),T=v("ES_INDEX","test-v1"),x=v("ES_TYPE","test"),W=v("ES_MASTER",0),L=v("ES_VERSION",5),M=5<L?_:d;if(!M)throw new Error("$ES is required! Ver:"+L);var $=v("XECURE_KEY",null),F=function(){var e=v("FIELDS",null),r=v("ES_FIELDS",["updated_at","name"]),n=v("XEC_FIELDS",null),t=function(e){return e&&"string"==typeof e?e.split(",").reduce(function(e,r){return(r=r.trim())&&e.push(r),r},[]):e};if(e){if(e=t(e),r=t(r||[]),n=t(n||[]),!Array.isArray(e))throw new Error("FIELDS must be array!");if(!Array.isArray(r))throw new Error("ES_FIELDS must be array!");if(!Array.isArray(n))throw new Error("XEC_FIELDS must be array!");e=e.reduce(function(e,r){var t=r.startsWith("*");if(!(r=t?r.substring(1):r))throw new Error("Invalid field name");return t&&n.indexOf(r)<0&&n.push(r),e.push(r),e},[]),r=r.reduce(function(e,r){var t=r.startsWith("*");if(!(r=t?r.substring(1):r))throw new Error("Invalid field name");return t&&n.indexOf(r)<0&&n.push(r),e.push(r),e},[]),n.length&&a(p,"XECURED-FIELDS =",n)}return(!e||!e.length)&&(n=r=e=null),[e,r,n]}(),U=_slicedToArray(F,3),k=U[0],V=U[1],B=U[2],Y=v("NS_NAME",""),C=require("dynamodb-value"),z=function(e){var o=require("crypto"),d="aes-256-ctr";if(!o)throw new Error("crypto module is required!");var r={crypto:o,algorithm:d,passwd:e};return r.encrypt=function(e){e=void 0===e?null:e;var r=JSON.stringify({alg:d,val:e}),t=new Buffer("LM!#"+(r||""),"utf8"),n=this.passwd||"",i=o.createCipher(d,n);return Buffer.concat([i.update(t),i.final()]).toString("base64")},r.decrypt=function(e){var r=new Buffer(e||"","base64"),t=this.passwd||"",n=o.createDecipher(d,t),i=Buffer.concat([n.update(r),n.final()]).toString("utf8");return(JSON.parse(i.substr("LM!#".length))||{}).val},r},Q=require("./notify-service")(e,"!"+Y,{NS_NAME:Y});c.do_notify=Q.do_notify,c.do_subscribe=Q.do_subscribe,A&&k&&(R&&k.indexOf(R)<0&&k.push(R),I&&k.indexOf(I)<0&&k.push(I));var X=[g,S,"created_at","updated_at","deleted_at",I];D&&R&&X.push(R);var H=function(e,r,t,n){t=t||"";var i=r="object"===(void 0===(e=e||0)?"undefined":_typeof(e))?r?f.extend(e,r):e:(r=null==r?{}:"object"!==(void 0===r?"undefined":_typeof(r))||r instanceof Promise?{params:r}:f.copy(r),f.extend(r,{_id:e}));if(void 0!==i.records)return f.promise(i);if(t.startsWith("notify"))return f.promise(i);if(i&&void 0!==i._is_prepared&&i._is_prepared)return i._current_mode=t,i._method_stack.push(t),1e3<i._method_stack.length?Promise.reject("method-stack full. size:"+i._method_stack):f.promise(i);e=j.startsWith("#")?i._id||i[g]||"":f.N(i._id||i[g]||0);var o=i._current_time||f.current_time_ms();return i._id=e,i._current_time=o,i._current_mode=t,i._method_stack=[],i._method_stack.push(t),i._updated_node=null,i._ctx=n,i._node={},i._is_prepared=!0,void 0===i._params_count&&(i._params_count=0,i=h.reduce(i,function(e,r,t){return t.startsWith("_")||t.startsWith("$")||0<=X.indexOf(t)||e._params_count++,e},i)),f.promise(i)},J=function(e){if(!e)return e;var r=e._current_mode||"";if(void 0!==e.records)return f.promise(e);if(r.startsWith("notify"))return f.promise(e);if(e._method_stack&&e._method_stack.pop(),"read"!==r&&"increment"!==r&&"create"!==r)return e;if(void 0!==e._auto_populate&&!e._auto_populate)return e;var t=e._node;return 0===e._params_count||-1===e._fields_count?e=h.reduce(t,function(e,r,t){return"updated_at"===t&&void 0!==e[t]?e[t]=e[t]>r?e[t]:r:e[t]=r,e},e):0<=e._fields_count&&(e=k?k.reduce(function(e,r){return void 0!==e[r]&&void 0!==t[r]&&(e[r]=t[r]),e},e):e),e},K=function(r){var e=r._id;if(m(p,"- my_prepare_id("+j+", "+e+")...."),j.startsWith("#")){if(j&&j.startsWith("#")&&1<j.length&&0<N){var t=j.substring(1);return i.do_get_next_id(t).then(function(e){return m(p,"> created next-id["+t+"]=",e),r._id=e,r})}}else if(j&&!e)return i.do_get_next_id(j).then(function(e){return m(p,"> created next-id=",e),r._id=e,r});return m(p,"> prepared-id =",e),Promise.resolve(r)},G=function(t){if(!t._current_mode)throw new Error(p+"_current_mode is required!");var n=t._current_mode,i=O||{},o=0,e=t._node||{};return k?"prepare"===n||"create"===n||"clone"===n?e=k.reduce(function(e,r){return r===S||r===w||r===P||("prepare"===n?(void 0!==i[r]?e[r]=O[r]:void 0!==t[r]&&(e[r]=t[r]),w&&(e[w]=E),P&&(e[P]=0)):("create"===n&&P&&(e[P]=y-1),void 0!==t[r]&&(e[r]=t[r])),o++),e},e):"update"!==n&&"increment"!==n&&"read"!==n||(e=k.reduce(function(e,r){return r===S||void 0!==t[r]&&o++,e},e)):o=-1,t._fields_count=o,t._node=e,t},Z=function(e,r){return e.created_at=0,e.updated_at=r,e.deleted_at=r,e},ee=function(e,r){return e.created_at=r,e.updated_at=r,e.deleted_at=0,e},re=function(t){if(!t._node)return Promise.reject(new Error(p+"_node is required!"));if(!t._current_time)return Promise.reject(new Error(p+"_current_time is required!"));var n=t._id,i=(t._node,t._current_time);return n?we(t).catch(function(e){var r=e&&e.message||"";if(!(0<=r.indexOf("404 NOT FOUND")))throw e;return l(p,"not found. err=",r),t}).then(function(e){var r=(e=G(e))._node||{};if(e._force_create)l(p,"WARN! _force_create is set!");else if(!r.deleted_at)return l(p,"INVALID STATE FOR PREPARED. ID=",n,", TIMES[CUD]=",[r.created_at,r.updated_at,r.deleted_at]),Promise.reject(new Error("INVALID STATE. deleted_at:"+r.deleted_at));return e[g]=e._id,r[S]=e._id,e._node=Z(e._node,i),e}):(t=G(t),K(t).then(function(e){var r=e._node||{};return e[g]=e._id,r[S]=e._id,e._node=Z(r,i),e}))},te=function(e){if(!e._id)return Promise.reject(new Error(p+"_id is required!"));if(!e._node)return Promise.reject(new Error(p+"_node is required!"));if(!e._current_time)return Promise.reject(new Error(p+"_current_time is required!"));var t=e._id,n=(e._node,e._current_time);return m(p,"> prepared-id =",t,", current-time =",n),we(e).then(function(e){var r=(e=G(e))._node||{};if(e._force_create)l(p,"WARN! _force_create is set!");else if(!r.deleted_at)return l(p,"INVALID STATE FOR CREATED. ID=",t,", TIMES[CUD]=",[r.created_at,r.updated_at,r.deleted_at]),Promise.reject(new Error("INVALID STATE. deleted_at:"+r.deleted_at));return e._node=ee(e._node,n),e})},ne=function(e){if(!e._id)return Promise.reject(new Error(p+"_id is required!"));if(!e._node)return Promise.reject(new Error(p+"_node is required!"));if(!e._current_time)return Promise.reject(new Error(p+"_current_time is required!"));e._id,e._node;var r=e._current_time;return we(e).then(function(e){return(e=G(e))._node=ee(e._node,r),e})},ie=function(e){if(!e._id)return Promise.reject(new Error(p+"_id is required!"));if(!e._node)return Promise.reject(new Error(p+"_node is required!"));if(!e._current_time)return Promise.reject(new Error(p+"_current_time is required!"));e._id,e._node;var n=e._current_time;return 0===(e=G(e))._fields_count?e:we(e).then(function(e){var r,t;return e._node=(r=e._node,t=n,r.updated_at=t,r),e})},oe=function(e){if(!e._id)return Promise.reject(new Error(p+"_id is required!"));if(!e._node)return Promise.reject(new Error(p+"_node is required!"));if(!e._current_time)return Promise.reject(new Error(p+"_current_time is required!"));e._id,e._node;var n=e._current_time;return we(e).then(function(e){var r,t;return(e=G(e))._node=(r=e._node,t=n,r.updated_at=t,r.deleted_at=t,r),e})},de=function(r){var e;if(!r._id)return Promise.reject(new Error(p+"_id is required!"));var t=r._id,n=j.startsWith("#")?"String":"";return u.do_get_item(b,(e={},_defineProperty(e,S,t),_defineProperty(e,"idType",n),e)).then(function(e){return r._node=e,r})},ue=function(r){if(!r._id)return Promise.reject(new Error(p+"_id is required!"));if(!r._node)return Promise.reject(new Error(p+"_node is required!"));var e=r._id,t=r._node,n={},i=0,o=$?z($):null;for(var d in r)r.hasOwnProperty(d)&&(d=""+d)&&(d.startsWith("_")||d.startsWith("$")||0<=X.indexOf(d)||k&&k.indexOf(d)<0||d&&(n[d]=r[d],t[d]=r[d],i++,o&&B&&0<=B.indexOf(d)&&(t[d]=o.encrypt(r[d]),n[d]=t[d])));return n.updated_at=t.updated_at,r._updated_node=null,(r._updated_count=i)?(void 0!==t[P]&&(t[P]=f.N(t[P],0)+1),u.do_update_item(b,_defineProperty({},S,e),n,P?_defineProperty({},P,1):null).then(function(e){return r._updated_node=n,r._node=Object.assign(r._node,n),r})):k?r:Promise.reject(new Error(p+"nothing to update"))},_e=function(r){if(!r._id)return Promise.reject(new Error(p+"_id is required!"));if(!r._node)return Promise.reject(new Error(p+"_node is required!"));var e=r._id,t=r._node,n=r._current_mode||"",i=r._current_time;if(m(p,"> dynamo:node-id =",e,", current-time =",i,", node :=",f.json(t)),"prepare"!==n){var o=$?z($):null;for(var d in r)r.hasOwnProperty(d)&&(d=""+d)&&(d.startsWith("_")||d.startsWith("$")||0<=X.indexOf(d)||k&&k.indexOf(d)<0||(t[d]=r[d],o&&B&&0<=B.indexOf(d)&&(t[d]=o.encrypt(r[d]))))}return void 0!==t[P]&&(t[P]=f.N(t[P],0)+1),u.do_create_item(b,_defineProperty({},S,e),t).then(function(e){return r})},ae=function(r){if(!r._id)return Promise.reject(new Error(p+"_id is required!"));if(!r._node)return Promise.reject(new Error(p+"_node is required!"));var e=r._id,t=r._node,n={},i=0;for(var o in r)r.hasOwnProperty(o)&&((o=""+o).startsWith("_")||o.startsWith("$")||0<=X.indexOf(o)||k&&k.indexOf(o)<0||o&&(n[o]=r[o],t[o]=f.N(t[o],0)+f.N(r[o]),i++));return n.updated_at=t.updated_at,m(p,"> dynamo:increment-node["+e+"] :=",n),r._updated_node=null,(r._updated_count=i)?(void 0!==t[P]&&(t[P]=f.N(t[P],0)+1),u.do_increment_item(b,_defineProperty({},S,e),n,P?_defineProperty({},P,1):null).then(function(e){return r._updated_node=n,r})):k?r:Promise.reject(new Error(p+"nothing to update"))},ce=function(r){var t=r._id;return t?u.do_delete_item(b,_defineProperty({},S,t)).then(function(e){return m(p,"> dynamo:deleted-item-node("+t+") res=",f.json(e)),r._node=e||{},r}):Promise.reject(new Error("._id is required!"))},se={my_read_node:function(r){var t=r._id;return t?q?o.do_get_item(q,t).then(function(e){return e?(r._node=e,r):Promise.reject(r)}).catch(function(e){throw e instanceof Error&&(l(p,"! redis:get-item("+q+", "+t+") err :=",e.message||e),r._error=e),r}):Promise.reject(r):Promise.reject(new Error("._id is required!"))},my_save_node:function(r){var e=r._id;if(!e)return Promise.reject(new Error("._id is required!"));if(!r._node)return Promise.reject(new Error(p+"_node is required!"));if(!q)return Promise.resolve(r);var t=r._node,n=se.my_set_node_footprint(e,t);return n=n.then(function(){return o.do_create_item(q,e,t)}).then(function(e){return r})},my_update_node:function(r){var e=r._id;if(!e)return Promise.reject(new Error("._id is required!"));if(!r._node)return Promise.reject(new Error("._node is required!"));if(!q)return Promise.resolve(r);var t=r._node,n=se.my_set_node_footprint(e,t);return n=n.then(function(){return o.do_update_item(q,e,t)}).then(function(e){return r})},my_delete_node:function(r){var t=r._id;return t?q?o.do_delete_item(q,t).then(function(e){return m(p,"> redis:delete-item-node("+t+") res=",f.json(e)),r}):Promise.resolve(r):Promise.reject(new Error("._id is required!"))},my_set_node_footprint:function(e,r){if(!e)return Promise.reject(new Error(p+"id is required!"));if(!q)return Promise.resolve(0);r=r||{};var t=f.N(r.updated_at,0),n=f.hash(r);return o.do_create_item([q+"/UPDATED",q+"/HASH"],e,[t,n]).then(function(e){return e})},my_get_updated_at:function(e){return e?q?o.do_get_item(q+"/UPDATED",e).then(function(e){return e}):Promise.resolve(-1):Promise.reject(new Error(p+"id is required!"))},my_get_hash_value:function(e){return e?q?o.do_get_item(q+"/HASH",e).then(function(e){return e}):Promise.resolve(-1):Promise.reject(new Error(p+"id is required!"))}},fe=function(r){var e=r._id;if(!e)return Promise.reject(new Error("._id is required!"));if(!r._node)return Promise.reject(new Error("elasticsearch:_node is required!"));if(!T||!x)return r;var t=r._node;if(V){var n=h.reduce(V,function(e,r){return void 0!==t[r]&&(e[r]=t[r]),e},{});return Object.keys(n).length?(void 0!==t.created_at&&(n.created_at=t.created_at),void 0!==t.updated_at&&(n.updated_at=t.updated_at),void 0!==t.deleted_at&&(n.deleted_at=t.deleted_at),W?M.do_create_item(T,x,e,n).then(function(e){return r}):M.do_update_item(T,x,e,n).then(function(e){return r})):(l(p,"! elasticsearch:WARN! nothing to update ("+e+")...."),r)}return M.do_create_item(T,x,e,t).then(function(e){return r})},he=function(r){var t=r._id;return t?T&&x?V&&!W?(m(p,"! elasticsearch:WARN! ignore delete ("+t+")...."),r):M.do_delete_item(T,x,t).then(function(e){return m(p,"! elasticsearch:deleted-item("+t+") res=",f.json(e)),r}):r:Promise.reject(new Error("._id is required!"))},me=function(i){if(!T||!x)return i;if(V&&!W)return i;var o={$page:0,$limit:0};return o.$page=f.N(i.page||0),o.$limit=f.N(0===i.ipp?0:i.ipp||10),o.deleted_at="0",i.$source&&(o.$source=i.$source),i.Q&&(o.$Q=i.Q),i.A&&(o.$A=i.A),i.O&&(o.$O=i.O),o.$O=o.$O||(j.startsWith("#")?"id.keyword":"id"),V&&V.forEach(function(e){void 0!==i[e]&&(o[e]=i[e]),void 0!==i["!"+e]&&(o["!"+e]=i["!"+e]),void 0!==i["#"+e]&&(o["#"+e]=i["#"+e])}),M.do_search_item(T,x,o).then(function(e){var r=e&&e.hits||{},t=r.hits||[],n=r;return i.list=t.map(function(e){return f.extend({_score:e._score},e._source)}),i.total=f.N(n.total||0),i.page=o.$page,i.ipp=o.$limit,i.took=e.took||0,void 0!==e.aggregations&&(i.aggregations=e.aggregations),i}).catch(function(e){throw l(p,"!ERR =",e),e})},le={},pe=function(e){var r=e._id||"";return[q,b,""+r].join(":").toUpperCase()},ve=function(e){var r=pe(e),t=e._node,n=e._current_time||f.current_time_ms();t||delete le[r];var i={cached_at:n,node:t};return le[r]=i,e},Ee=function(e){var r=pe(e);return delete le[r],e},ye=function(n){var i=n._id;return Object.keys(n).forEach(function(t,e){if(!t.startsWith("_")&&!t.startsWith("$")&&n.hasOwnProperty(t)){var r=n[t];if(r&&"object"==(void 0===r?"undefined":_typeof(r))){if(!Array.isArray(r))throw new Error("Invalid data-type (object) key:"+t+"@"+i);r.forEach(function(e,r){if(e&&"object"==(void 0===e?"undefined":_typeof(e)))throw new Error("Invalid data-type (object) key:"+t+"@"+i+":"+r)})}}}),n},we=function(e){var r,t,n,i,o;return e._id?(t=pe(r=e),n=le[t]||{},i=f.N(n.cached_at,0),o=(r._current_time||f.current_time_ms())-i,n.node&&i&&!(2e3<o)&&(r._node=Object.assign({},n.node),r.C=i,1)?Promise.resolve(e):Promise.resolve(e).then(se.my_read_node).catch(function(e){return de(e).then(function(e){return void 0===(e._node||{})[S]?Promise.reject(new Error("404 NOT FOUND. "+b+".id:"+(e._id||""))):e}).then(se.my_save_node)}).then(ve)):Promise.reject(new Error("._id is required!"))},Pe=function(e){if(!e||!e._node)return e;if(!B||!B.length)return e;var n=$?z($):null;if(!n)return e;var r=e._node||{};return B.reduce(function(e,r){if(r.startsWith("_")||r.startsWith("$"))return e;var t=e[r];return void 0===t||(t=t?n.decrypt(t):t,e[r]=t),e},r),e},ge=function(e){return e._id?e._node?Promise.resolve(e).then(ye).then(_e).then(se.my_save_node).then(fe).then(ve):Promise.reject(new Error("_node is required!")):Promise.reject(new Error("._id is required!"))},Se=function(e){var r=e._id;return r?e._node?e._current_time?(m(p,"- my_clone_node ("+r+")...."),Promise.resolve(e).then(function(n){if(!A)return n;var i=void 0!==n[R]?f.N(n[R],0):r,o=r;return K({_id:0}).then(function(e){var r=e._id,t=n._node||{};return t[S]=r,R&&(t[R]=i),I&&(t[I]=o),R&&(n[R]=i),I&&(n[I]=o),n._clone_id=r,n._node=t,n})}).then(function(e){var r=e._id;return e._node?e._clone_id?(e._id=e._clone_id,h.reduce(e,function(e,r,t){return t&&(t.startsWith("_")||t.startsWith("$")||0<=X.indexOf(t)||void 0!==e[t]&&(e[t]=r)),e},e._node),m(p,">> before save-node().. that._node =",f.json(e._node)),ge(e).then(function(){return e._id=r,e})):Promise.reject(new Error(p+"_clone_id is required!")):Promise.reject(new Error(p+"_node is required!"))}).then(function(e){return m(p,">> cloned-node["+e._id+" -> "+e._clone_id+"] res=",f.json(e._node)),e})):Promise.reject(new Error("._current_time is required!")):Promise.reject(new Error("._node is required!")):Promise.reject(new Error("._id is required!"))},je=function(e){return e._id?Promise.resolve(e).then(ce).then(se.my_delete_node).then(he).then(Ee).then(function(e){return m(p,">> deleted-node res=",f.json(e._node)),e}):Promise.reject(new Error("._id is required!"))},Ne=function(e){return Promise.resolve(e).then(me)},be=function(e){if(!e)return e;if(!e._id)return Promise.reject(new Error("_id is required!"));var n=e._current_mode;return Promise.resolve(e).then(function(r){var e=!1;if("create"===n||"prepare"===n||"delete"===n||"destroy"===n?e=!0:"update"!==n&&"increment"!==n||(e=0<r._fields_count),e){var t=Y+":event:"+n;return m(p,">>> notify event-id:",t,", notifiable=",e),Ae(t,r).then(function(e){return r})}return r})},qe=function(r){if(!r.records)return Promise.reject(new Error(p+"records is required!"));var e=r.records.slice(0);if(m(p,"- my_event_records().... records.len=",e.length),!e.length)return r;var t,n,i;return(t=e,n=Oe,i=f.promise(t.shift()),i=t.reduce(function(e,r){return e.then(function(){return n(r)})},i.then(function(e){return n(e)}))).catch(function(e){return l(p,"error ignored! =",e),r}).then(function(){return r})},Oe=function(e){if(!e)return Promise.reject(new Error("record is required"));if(!e.dynamodb&&!e.Sns)return Promise.reject(new Error("record.dynamodb|Sns is required"));var i=e.eventName||"EVENT",r=null;if(e.dynamodb){var o=e.table||e.eventSourceARN&&e.eventSourceARN.split("/")[1]||"";if(m(p,"--- process-record("+o+") ... "),b!==o)return m(p,"! ignore record-table: "+b+" != "+o+" "),e;var t=e.dynamodb.Keys?C.toJavascript(e.dynamodb.Keys):{},d=e.dynamodb.NewImage?C.toJavascript(e.dynamodb.NewImage):null,n=e.dynamodb.OldImage?C.toJavascript(e.dynamodb.OldImage):null,u=t[S],_="MODIFY"===i?f.diff(n,d):null,a=f.N(d&&d.updated_at||0),c=_?h.reduce(_,function(e,r){return e[r]=n[r],e},{}):null,s={_id:u,_node:d,_updated_at:a,_diff:_,_prev:c};switch(r=Promise.resolve(s),_&&m(p,">> "+o+".different["+u+"]=",f.json(_)),i){case"INSERT":case"MODIFY":r=r.then(function(t){var n=t._id;return se.my_get_updated_at(n).then(function(e){if(l(p,">> "+o+".updated_at["+n+"] res=",e," <- ",t._updated_at,t._updated_at!==e?"*":""),!e||t._updated_at>=e){var r=f.hash(d);return se.my_get_hash_value(n).then(function(e){return e&&e===r?(m(p,"! WARN! ignored due to hash-value matching =",e),t):(m(p,"! INFO! node was updated. hash :=",r,"<-",e),se.my_save_node(t).then(fe))})}return m(p,"! ignored due to old-record"),t})});break;case"REMOVE":m(p,">> removed!"),r=(r=r.then(se.my_delete_node)).then(he)}}else if(e.Sns){m(p,">> event-SNS =",e.Sns);r=Promise.resolve(e.Sns)}return r?r.then(function(r){var e={INSERT:"create",MODIFY:"update",REMOVE:"delete",EVENT:"event"}[i],t=!!e;if(r._current_mode=e,t){var n=Y+":record:"+e;return m(p,">>> notify event-id:",n,", notifiable=",t),Ae(n,r).then(function(e){return r})}return r}).then(function(){return e}):Promise.reject(new Error("invalid chain!"))},Ae=function(e,r){return Q.do_notify(e,r)},Ie=function(r){var e=[];if(b){var t=j.startsWith("#")?"S":"N";e.push(Promise.resolve("DynamoDb").then(function(e){return m(p,"# initialize ",e),u.do_create_table(b,S,t).then(function(e){return e.TableDescription.TableName===b})}).catch(function(e){if("ResourceInUseException"===e.code)return!1;throw e}))}else m(p,"DS: WARN! ignored configuration. DYNA_TABLE=",b);if(j&&!j.startsWith("#")?e.push(Promise.resolve("MySQL").then(function(e){return m(p,"# initialize MySQL (Sequence) "),i.do_create_id_seq(j,N).then(function(e){return m(p,"> create-id-seq res=",e),!0})}).catch(function(e){if("ER_TABLE_EXISTS_ERROR"===e.code)return!1;throw e})):j&&j.startsWith("#")&&1<j.length&&0<N?e.push(Promise.resolve("MySQL").then(function(e){m(p,"# initialize MySQL (Sequence) ");var r=j.substring(1);return i.do_create_id_seq(r,N).then(function(e){return m(p,"> create-id-seq["+r+"] res=",e),!0})}).catch(function(e){if("ER_TABLE_EXISTS_ERROR"===e.code)return!1;throw e})):m(p,"MS: WARN! ignored configuration. ID_TYPE=",j),T&&x&&V){var n={mappings:{_default_:{dynamic_templates:[{string_fields:{match:"*_multi",match_mapping_type:"string",mapping:{type:"multi_field",fields:{"{name}":{type:6<=L?"text":"string",index:"analyzed",omit_norms:!0,index_options:"docs"},"{name}.raw":{type:6<=L?"text":"string",index:"not_analyzed",ignore_above:256}}}}}],properties:{"@version":{type:6<=L?"text":"string",index:!(6<=L)&&"not_analyzed"},title:{type:6<=L?"text":"string"},name:{type:6<=L?"text":"string"},created_at:{type:"date",format:"strict_date_optional_time||epoch_millis"},updated_at:{type:"date",format:"strict_date_optional_time||epoch_millis"},deleted_at:{type:"date",format:"strict_date_optional_time||epoch_millis"}}}}};6<=L||(n.mappings._default_.all={enabled:!0}),e.push(Promise.resolve("ElasticSearch").then(function(e){return m(p,"# initialize ElasticSearch (Type) ",x),M.do_create_index_type(T,x,n).then(function(e){return m(p,"> create-es-index res=",e),!0})}).catch(function(e){if(l(p,"> create-es-index error=",e),"index_already_exists_exception"===e.type)return!0;throw e}).then(function(e){return e}))}else m(p,"MS: WARN! ignored configuration. ES_TYPE=",x);return Promise.all(e).then(function(e){return m(p,">> results=",e),r._result=e,r})},Re=function(r){var e=[];if(b&&(m(p,"# terminate DynamoDB"),e.push(u.do_delete_table(b).then(function(e){return e.TableDescription.TableName===b}).catch(function(e){if("ResourceNotFoundException"===e.code)return!0;throw e}))),j&&!j.startsWith("#"))m(p,"# terminate MySQL (Sequence) "),e.push(i.do_delete_id_seq(j).then(function(e){return m(p,"> delete-id-seq res=",e),!0}).catch(function(e){if("ER_BAD_TABLE_ERROR"===e.code)return!0;throw e}));else if(j&&j.startsWith("#")&&1<j.length&&0<N){m(p,"# terminate MySQL (Sequence) ");var t=j.substring(1);e.push(i.do_delete_id_seq(t).then(function(e){return m(p,"> delete-id-seq["+t+"] res=",e),!0}).catch(function(e){if("ER_BAD_TABLE_ERROR"===e.code)return!0;throw e}))}else m(p,"# ignored MySQL (Sequence) ");return T&&x?(m(p,"# terminate ES (Index) "),e.push(M.do_delete_index_type(T,null).then(function(e){return m(p,"> delete-es-index res=",e),!0}).catch(function(e){throw l(p,"> create-es-index error=",e),e}))):m(p,"# ignored ES (Index) "),Promise.all(e).then(function(e){return m(p,">> results=",e),r._result=e,r})},De=function(e){var r={a:1,b:2,x:3},t={x:3,a:1,b:2},n={b:2,x:3,a:1};r.name="hi",t.name="hi",n.name="hi",m(p,"# node1 =",f.json(r)),m(p,"> node2 =",f.json(t)),m(p,"> node3 =",f.json(n)),m(p,"# node1.sorted =",f.json(r,!0)),m(p,"> node2.sorted =",f.json(t,!0)),m(p,"> node3.sorted =",f.json(n,!0)),m(p,"# hash1 =",f.hash(1)),m(p,"> node1 =",f.hash(r)),m(p,"> node2 =",f.hash(t)),m(p,"> node3 =",f.hash(n));var i={updated_at:234,name:"test hash"};return Promise.resolve(i).then(function(e){return o.do_create_item("H#",1,e)}).then(function(){return o.do_get_item("H#",1).then(function(e){return m(p,"> read-back data=",e),e})}).then(function(){return se.my_set_node_footprint(1,i)}).then(function(){return se.my_get_updated_at(1).then(function(e){return m(p,"> updated-at value=",void 0===e?"undefined":_typeof(e),":",e),e})}).then(function(){return se.my_get_hash_value(1).then(function(e){return m(p,"> hash-value value=",void 0===e?"undefined":_typeof(e),":",e),e})})},Te=function(r){return m(p,"- my_test_self()...."),Promise.resolve(r).then(De).then(function(e){return e||r})};return c.do_prepare_chain=function(e,r,t,n){return H(e,r,t,n)},c.do_finish_chain=function(e,r,t,n){return J(e)},c.do_prepare=function(e,r){return H(e,r,"prepare").then(re).then(ge).then(be).then(J)},c.do_create=function(t,n){return H(t,n,"create").then(te).catch(function(e){l(p,"ERR! prepare_node_created =",e instanceof Error,e.message||e);var r=e&&e.message||"";if(e instanceof Error&&0<=r.indexOf("404 NOT FOUND"))return a(p,"WARN! AUTO TRY TO PREPARE NODE. ID="+t),H(t,n,"create").then(function(e){var r=e._current_time;return e=G(e),m(p,">> prepared old-node=",f.json(e._node)),e._node=ee(e._node,r),e});throw e}).then(ge).then(be).then(J)},c.do_clone=function(e,r){return H(e,r,"clone").then(ne).then(Se).then(be).then(J)},c.do_read=function(e,r){return H(e,r,"read").then(G).then(function(e){return 0!==e._params_count&&0===e._fields_count?e:we(e)}).then(be).then(J)},c.do_readX=function(e,r){return H(e,r,"read").then(G).then(function(e){return 0!==e._params_count&&0===e._fields_count?e:we(e)}).then(Pe).then(be).then(J)},c.do_update=function(e,r){return H(e,r,"update").then(ie).then(function(e){return 0===e._fields_count?e:(n=(t=e)._id)?0===t._fields_count?(m(p,"! my_update_node() no need to update... fields_count="+t._fields_count),t):Promise.resolve(t).then(ye).then(ue).then(function(e){return e._updated_node?se.my_save_node(e):e}).then(function(e){return e._updated_node?fe(e):e}).then(function(e){return e._updated_node?ve(e):e}).then(function(e){return m(p,">> updated-node["+n+"] res=",f.json(e._updated_node)),e}).catch(function(e){var r=e&&e.message||"";if(l(p,">> updated-node["+n+"] err=",r),0<r.indexOf("an attribute that does not exist in the item"))return ge(t);throw e}):Promise.reject(new Error("._id is required!"));var t,n}).then(be).then(J)},c.do_increment=function(e,r){return H(e,r,"increment").then(ie).then(function(e){return 0===e._fields_count?e:(t=(r=e)._id)?0===r._fields_count?(m(p,"! my_increment_node() no need to increment... fields_count="+r._fields_count),r):Promise.resolve(r).then(ye).then(ae).then(function(e){return e._updated_node?se.my_save_node(e):e}).then(function(e){return e._updated_node?fe(e):e}).then(function(e){return e._updated_node?ve(e):e}).then(function(e){return m(p,">> increment-node["+t+"] res=",f.json(e._updated_node)),e}):Promise.reject(new Error("._id is required!"));var r,t}).then(be).then(J)},c.do_delete=function(e,r){return H(e,r,"delete").then(oe).then(ge).then(be).then(J)},c.do_destroy=function(e,r){return H(e,r,"destroy").then(je).then(be).then(J)},c.do_search=function(e,r){return H(e,r,"search").then(Ne).then(J)},c.on_records=function(e,r){return H(e,r,"on_records").then(qe).then(J)},c.do_initialize=function(e,r){return H(e,r,"initialize").then(Ie).then(J)},c.do_terminate=function(e,r){return H(e,r,"terminate").then(Re).then(J)},c.do_test_self=function(e,r){return H(e,r,"self-test").then(Te).then(J)},c.do_next_id=function(){return K({_id:0}).then(function(e){return e._id})},c}; | ||
"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},_slicedToArray=function(e,r){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return function(e,r){var t=[],n=!0,i=!1,o=void 0;try{for(var d,u=e[Symbol.iterator]();!(n=(d=u.next()).done)&&(t.push(d.value),!r||t.length!==r);n=!0);}catch(e){i=!0,o=e}finally{try{!n&&u.return&&u.return()}finally{if(i)throw o}}return t}(e,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")};function _defineProperty(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}module.exports=function(e,r,t){var n=r||"LEM",f=e.U,h=e._,i=e.MS,u=e.DS,o=e.RS,d=e.ES,_=e.ES6;if(!f)throw new Error("$U is required!");if(!h)throw new Error("$U is required!");if(!i)throw new Error("$MS is required!");if(!u)throw new Error("$DS is required!");if(!o)throw new Error("$RS is required!");var m=e.log,a=e.inf,l=e.err,p=f.NS(n,"green"),c=t||{},s=function(e){throw new Error("NOT_IMPLEMENTED - "+p+":"+JSON.stringify(e))};c.do_prepare=s,c.do_create=s,c.do_clone=s,c.do_update=s,c.do_increment=s,c.do_read=s,c.do_readX=s,c.do_delete=s,c.do_destroy=s,c.do_search=s,c.do_initialize=s,c.do_terminate=s,c.do_test_self=s,c.on_records=s,c.do_notify=s,c.do_subscribe=s,c.do_next_id=s,r.startsWith("_")||e(r,c);var v=function(e,r){return void 0===c[e]?r:c[e]},E=v("VERSION",1),y=v("REVISION",1),w=v("VERSION_NAME","V"),P=v("REVISION_NAME","R"),g=v("ID_INPUT","id"),S=v("ID_NAME","id"),j=v("ID_TYPE","test"),q=v("ID_NEXT",0),N=v("DYNA_TABLE","TestTable"),b=v("REDIS_PKEY","TPKEY"),O=v("DEFAULTS",null),A=v("CLONEABLE",!1),I=v("CLONED_ID","cloned"),R=v("PARENT_ID","parent"),D=v("PARENT_IMUT",!0),T=v("ES_INDEX","test-v1"),x=v("ES_TYPE","test"),W=v("ES_MASTER",0),L=v("ES_VERSION",5),M=5<L?_:d;if(!M)throw new Error("$ES is required! Ver:"+L);var $=v("XECURE_KEY",null),F=function(){var e=v("FIELDS",null),r=v("ES_FIELDS",["updated_at","name"]),n=v("XEC_FIELDS",null),t=function(e){return e&&"string"==typeof e?e.split(",").reduce(function(e,r){return(r=r.trim())&&e.push(r),r},[]):e};if(e){if(e=t(e),r=t(r||[]),n=t(n||[]),!Array.isArray(e))throw new Error("FIELDS must be array!");if(!Array.isArray(r))throw new Error("ES_FIELDS must be array!");if(!Array.isArray(n))throw new Error("XEC_FIELDS must be array!");e=e.reduce(function(e,r){var t=r.startsWith("*");if(!(r=t?r.substring(1):r))throw new Error("Invalid field name");return t&&n.indexOf(r)<0&&n.push(r),e.push(r),e},[]),r=r.reduce(function(e,r){var t=r.startsWith("*");if(!(r=t?r.substring(1):r))throw new Error("Invalid field name");return t&&n.indexOf(r)<0&&n.push(r),e.push(r),e},[]),n.length&&a(p,"XECURED-FIELDS =",n)}return(!e||!e.length)&&(n=r=e=null),[e,r,n]}(),U=_slicedToArray(F,3),k=U[0],V=U[1],B=U[2],Y=v("NS_NAME",""),C=require("dynamodb-value"),z=function(e){var o=require("crypto"),d="aes-256-ctr";if(!o)throw new Error("crypto module is required!");var r={crypto:o,algorithm:d,passwd:e};return r.encrypt=function(e){e=void 0===e?null:e;var r=JSON.stringify({alg:d,val:e}),t=new Buffer("LM!#"+(r||""),"utf8"),n=this.passwd||"",i=o.createCipher(d,n);return Buffer.concat([i.update(t),i.final()]).toString("base64")},r.decrypt=function(e){var r=new Buffer(e||"","base64"),t=this.passwd||"",n=o.createDecipher(d,t),i=Buffer.concat([n.update(r),n.final()]).toString("utf8");return(JSON.parse(i.substr("LM!#".length))||{}).val},r},Q=require("./notify-service")(e,"!"+Y,{NS_NAME:Y});c.do_notify=Q.do_notify,c.do_subscribe=Q.do_subscribe,A&&k&&(R&&k.indexOf(R)<0&&k.push(R),I&&k.indexOf(I)<0&&k.push(I));var X=[g,S,"created_at","updated_at","deleted_at",I];D&&R&&X.push(R);var H=function(e,r,t,n){t=t||"";var i=r="object"===(void 0===(e=e||0)?"undefined":_typeof(e))?r?f.extend(e,r):e:(r=null==r?{}:"object"!==(void 0===r?"undefined":_typeof(r))||r instanceof Promise?{params:r}:f.copy(r),f.extend(r,{_id:e}));if(void 0!==i.records)return f.promise(i);if(t.startsWith("notify"))return f.promise(i);if(i&&void 0!==i._is_prepared&&i._is_prepared)return i._current_mode=t,i._method_stack.push(t),1e3<i._method_stack.length?Promise.reject("method-stack full. size:"+i._method_stack):f.promise(i);e=j.startsWith("#")?i._id||i[g]||"":f.N(i._id||i[g]||0);var o=i._current_time||f.current_time_ms();return i._id=e,i._current_time=o,i._current_mode=t,i._method_stack=[],i._method_stack.push(t),i._updated_node=null,i._ctx=n,i._node={},i._is_prepared=!0,void 0===i._params_count&&(i._params_count=0,i=h.reduce(i,function(e,r,t){return t.startsWith("_")||t.startsWith("$")||0<=X.indexOf(t)||e._params_count++,e},i)),f.promise(i)},J=function(e){if(!e)return e;var r=e._current_mode||"";if(void 0!==e.records)return f.promise(e);if(r.startsWith("notify"))return f.promise(e);if(e._method_stack&&e._method_stack.pop(),"read"!==r&&"increment"!==r&&"create"!==r)return e;if(void 0!==e._auto_populate&&!e._auto_populate)return e;var t=e._node;return 0===e._params_count||-1===e._fields_count?e=h.reduce(t,function(e,r,t){return"updated_at"===t&&void 0!==e[t]?e[t]=e[t]>r?e[t]:r:e[t]=r,e},e):0<=e._fields_count&&(e=k?k.reduce(function(e,r){return void 0!==e[r]&&void 0!==t[r]&&(e[r]=t[r]),e},e):e),e},K=function(r){var e=r._id;if(m(p,"- my_prepare_id("+j+", "+e+")...."),j.startsWith("#")){if(j&&j.startsWith("#")&&1<j.length&&0<q){var t=j.substring(1);return i.do_get_next_id(t).then(function(e){return m(p,"> created next-id["+t+"]=",e),r._id=e,r})}}else if(j&&!e)return i.do_get_next_id(j).then(function(e){return m(p,"> created next-id=",e),r._id=e,r});return m(p,"> prepared-id =",e),Promise.resolve(r)},G=function(t){if(!t._current_mode)throw new Error(p+"_current_mode is required!");var n=t._current_mode,i=O||{},o=0,e=t._node||{};return k?"prepare"===n||"create"===n||"clone"===n?e=k.reduce(function(e,r){return r===S||r===w||r===P||("prepare"===n?(void 0!==i[r]?e[r]=O[r]:void 0!==t[r]&&(e[r]=t[r]),w&&(e[w]=E),P&&(e[P]=0)):("create"===n&&P&&(e[P]=y-1),void 0!==t[r]&&(e[r]=t[r])),o++),e},e):"update"!==n&&"increment"!==n&&"read"!==n||(e=k.reduce(function(e,r){return r===S||void 0!==t[r]&&o++,e},e)):o=-1,t._fields_count=o,t._node=e,t},Z=function(e,r){return e.created_at=0,e.updated_at=r,e.deleted_at=r,e},ee=function(e,r){return e.created_at=r,e.updated_at=r,e.deleted_at=0,e},re=function(t){if(!t._node)return Promise.reject(new Error(p+"_node is required!"));if(!t._current_time)return Promise.reject(new Error(p+"_current_time is required!"));var n=t._id,i=(t._node,t._current_time);return n?we(t).catch(function(e){var r=e&&e.message||"";if(!(0<=r.indexOf("404 NOT FOUND")))throw e;return l(p,"not found. err=",r),t}).then(function(e){var r=(e=G(e))._node||{};if(e._force_create)l(p,"WARN! _force_create is set!");else if(!r.deleted_at)return l(p,"INVALID STATE FOR PREPARED. ID=",n,", TIMES[CUD]=",[r.created_at,r.updated_at,r.deleted_at]),Promise.reject(new Error("INVALID STATE. deleted_at:"+r.deleted_at));return e[g]=e._id,r[S]=e._id,e._node=Z(e._node,i),e}):(t=G(t),K(t).then(function(e){var r=e._node||{};return e[g]=e._id,r[S]=e._id,e._node=Z(r,i),e}))},te=function(e){if(!e._id)return Promise.reject(new Error(p+"_id is required!"));if(!e._node)return Promise.reject(new Error(p+"_node is required!"));if(!e._current_time)return Promise.reject(new Error(p+"_current_time is required!"));var t=e._id,n=(e._node,e._current_time);return m(p,"> prepared-id =",t,", current-time =",n),we(e).then(function(e){var r=(e=G(e))._node||{};if(e._force_create)l(p,"WARN! _force_create is set!");else if(!r.deleted_at)return l(p,"INVALID STATE FOR CREATED. ID=",t,", TIMES[CUD]=",[r.created_at,r.updated_at,r.deleted_at]),Promise.reject(new Error("INVALID STATE. deleted_at:"+r.deleted_at));return e._node=ee(e._node,n),e})},ne=function(e){if(!e._id)return Promise.reject(new Error(p+"_id is required!"));if(!e._node)return Promise.reject(new Error(p+"_node is required!"));if(!e._current_time)return Promise.reject(new Error(p+"_current_time is required!"));e._id,e._node;var r=e._current_time;return we(e).then(function(e){return(e=G(e))._node=ee(e._node,r),e})},ie=function(e){if(!e._id)return Promise.reject(new Error(p+"_id is required!"));if(!e._node)return Promise.reject(new Error(p+"_node is required!"));if(!e._current_time)return Promise.reject(new Error(p+"_current_time is required!"));e._id,e._node;var n=e._current_time;return 0===(e=G(e))._fields_count?e:we(e).then(function(e){var r,t;return e._node=(r=e._node,t=n,r.updated_at=t,r),e})},oe=function(e){if(!e._id)return Promise.reject(new Error(p+"_id is required!"));if(!e._node)return Promise.reject(new Error(p+"_node is required!"));if(!e._current_time)return Promise.reject(new Error(p+"_current_time is required!"));e._id,e._node;var n=e._current_time;return we(e).then(function(e){var r,t;return(e=G(e))._node=(r=e._node,t=n,r.updated_at=t,r.deleted_at=t,r),e})},de=function(r){var e;if(!r._id)return Promise.reject(new Error(p+"_id is required!"));var t=r._id,n=j.startsWith("#")?"String":"";return u.do_get_item(N,(e={},_defineProperty(e,S,t),_defineProperty(e,"idType",n),e)).then(function(e){return r._node=e,r})},ue=function(r){if(!r._id)return Promise.reject(new Error(p+"_id is required!"));if(!r._node)return Promise.reject(new Error(p+"_node is required!"));var e=r._id,t=r._node,n={},i=0,o=$?z($):null;for(var d in r)r.hasOwnProperty(d)&&(d=""+d)&&(d.startsWith("_")||d.startsWith("$")||0<=X.indexOf(d)||k&&k.indexOf(d)<0||d&&(n[d]=r[d],t[d]=r[d],i++,o&&B&&0<=B.indexOf(d)&&(t[d]=o.encrypt(r[d]),n[d]=t[d])));return n.updated_at=t.updated_at,r._updated_node=null,(r._updated_count=i)?(void 0!==t[P]&&(t[P]=f.N(t[P],0)+1),u.do_update_item(N,_defineProperty({},S,e),n,P?_defineProperty({},P,1):null).then(function(e){return r._updated_node=n,r._node=Object.assign(r._node,n),r})):k?r:Promise.reject(new Error(p+"nothing to update"))},_e=function(r){if(!r._id)return Promise.reject(new Error(p+"_id is required!"));if(!r._node)return Promise.reject(new Error(p+"_node is required!"));var e=r._id,t=r._node,n=r._current_mode||"",i=r._current_time;if(m(p,"> dynamo:node-id =",e,", current-time =",i,", node :=",f.json(t)),"prepare"!==n){var o=$?z($):null;for(var d in r)r.hasOwnProperty(d)&&(d=""+d)&&(d.startsWith("_")||d.startsWith("$")||0<=X.indexOf(d)||k&&k.indexOf(d)<0||(t[d]=r[d],o&&B&&0<=B.indexOf(d)&&(t[d]=o.encrypt(r[d]))))}return void 0!==t[P]&&(t[P]=f.N(t[P],0)+1),u.do_create_item(N,_defineProperty({},S,e),t).then(function(e){return r})},ae=function(r){if(!r._id)return Promise.reject(new Error(p+"_id is required!"));if(!r._node)return Promise.reject(new Error(p+"_node is required!"));var e=r._id,t=r._node,n={},i=0;for(var o in r)r.hasOwnProperty(o)&&((o=""+o).startsWith("_")||o.startsWith("$")||0<=X.indexOf(o)||k&&k.indexOf(o)<0||o&&(n[o]=r[o],t[o]=f.N(t[o],0)+f.N(r[o]),i++));return n.updated_at=t.updated_at,m(p,"> dynamo:increment-node["+e+"] :=",n),r._updated_node=null,(r._updated_count=i)?(void 0!==t[P]&&(t[P]=f.N(t[P],0)+1),u.do_increment_item(N,_defineProperty({},S,e),n,P?_defineProperty({},P,1):null).then(function(e){return r._updated_node=n,r})):k?r:Promise.reject(new Error(p+"nothing to update"))},ce=function(r){var t=r._id;return t?u.do_delete_item(N,_defineProperty({},S,t)).then(function(e){return m(p,"> dynamo:deleted-item-node("+t+") res=",f.json(e)),r._node=e||{},r}):Promise.reject(new Error("._id is required!"))},se={my_read_node:function(r){var t=r._id;return t?b?o.do_get_item(b,t).then(function(e){return e?(r._node=e,r):Promise.reject(r)}).catch(function(e){throw e instanceof Error&&(l(p,"! redis:get-item("+b+", "+t+") err :=",e.message||e),r._error=e),r}):Promise.reject(r):Promise.reject(new Error("._id is required!"))},my_save_node:function(r){var e=r._id;if(!e)return Promise.reject(new Error("._id is required!"));if(!r._node)return Promise.reject(new Error(p+"_node is required!"));if(!b)return Promise.resolve(r);var t=r._node,n=se.my_set_node_footprint(e,t);return n=n.then(function(){return o.do_create_item(b,e,t)}).then(function(e){return r})},my_update_node:function(r){var e=r._id;if(!e)return Promise.reject(new Error("._id is required!"));if(!r._node)return Promise.reject(new Error("._node is required!"));if(!b)return Promise.resolve(r);var t=r._node,n=se.my_set_node_footprint(e,t);return n=n.then(function(){return o.do_update_item(b,e,t)}).then(function(e){return r})},my_delete_node:function(r){var t=r._id;return t?b?o.do_delete_item(b,t).then(function(e){return m(p,"> redis:delete-item-node("+t+") res=",f.json(e)),r}):Promise.resolve(r):Promise.reject(new Error("._id is required!"))},my_set_node_footprint:function(e,r){if(!e)return Promise.reject(new Error(p+"id is required!"));if(!b)return Promise.resolve(0);r=r||{};var t=f.N(r.updated_at,0),n=f.hash(r);return o.do_create_item([b+"/UPDATED",b+"/HASH"],e,[t,n]).then(function(e){return e})},my_get_updated_at:function(e){return e?b?o.do_get_item(b+"/UPDATED",e).then(function(e){return e}):Promise.resolve(-1):Promise.reject(new Error(p+"id is required!"))},my_get_hash_value:function(e){return e?b?o.do_get_item(b+"/HASH",e).then(function(e){return e}):Promise.resolve(-1):Promise.reject(new Error(p+"id is required!"))}},fe=function(r){var e=r._id;if(!e)return Promise.reject(new Error("._id is required!"));if(!r._node)return Promise.reject(new Error("elasticsearch:_node is required!"));if(!T||!x)return r;var t=r._node;if(V){var n=h.reduce(V,function(e,r){return void 0!==t[r]&&(e[r]=t[r]),e},{});return Object.keys(n).length?(void 0!==t.created_at&&(n.created_at=t.created_at),void 0!==t.updated_at&&(n.updated_at=t.updated_at),void 0!==t.deleted_at&&(n.deleted_at=t.deleted_at),W?M.do_create_item(T,x,e,n).then(function(e){return r}):M.do_update_item(T,x,e,n).then(function(e){return r})):(l(p,"! elasticsearch:WARN! nothing to update ("+e+")...."),r)}return M.do_create_item(T,x,e,t).then(function(e){return r})},he=function(r){var t=r._id;return t?T&&x?V&&!W?(m(p,"! elasticsearch:WARN! ignore delete ("+t+")...."),r):M.do_delete_item(T,x,t).then(function(e){return m(p,"! elasticsearch:deleted-item("+t+") res=",f.json(e)),r}):r:Promise.reject(new Error("._id is required!"))},me=function(i){if(!T||!x)return i;if(V&&!W)return i;var o={$page:0,$limit:0};return o.$page=f.N(i.page||0),o.$limit=f.N(0===i.ipp?0:i.ipp||10),o.deleted_at="0",i.$source&&(o.$source=i.$source),i.Q&&(o.$Q=i.Q),i.A&&(o.$A=i.A),i.O&&(o.$O=i.O),o.$O=o.$O||(j.startsWith("#")?"id.keyword":"id"),V&&V.forEach(function(e){void 0!==i[e]&&(o[e]=i[e]),void 0!==i["!"+e]&&(o["!"+e]=i["!"+e]),void 0!==i["#"+e]&&(o["#"+e]=i["#"+e])}),M.do_search_item(T,x,o).then(function(e){var r=e&&e.hits||{},t=r.hits||[],n=r;return i.list=t.map(function(e){return f.extend({_score:e._score},e._source)}),i.total=f.N(n.total||0),i.page=o.$page,i.ipp=o.$limit,i.took=e.took||0,void 0!==e.aggregations&&(i.aggregations=e.aggregations),i}).catch(function(e){throw l(p,"!ERR =",e),e})},le={},pe=function(e){var r=e._id||"";return[b,N,""+r].join(":").toUpperCase()},ve=function(e){var r=pe(e),t=e._node,n=e._current_time||f.current_time_ms();t||delete le[r];var i={cached_at:n,node:t};return le[r]=i,e},Ee=function(e){var r=pe(e);return delete le[r],e},ye=function(n){var i=n._id;return Object.keys(n).forEach(function(t,e){if(!t.startsWith("_")&&!t.startsWith("$")&&n.hasOwnProperty(t)){var r=n[t];if(r&&"object"==(void 0===r?"undefined":_typeof(r))){if(!Array.isArray(r))throw new Error("Invalid data-type (object) key:"+t+"@"+i);r.forEach(function(e,r){if(e&&"object"==(void 0===e?"undefined":_typeof(e)))throw new Error("Invalid data-type (object) key:"+t+"@"+i+":"+r)})}}}),n},we=function(e){var r,t,n,i,o;return e._id?(t=pe(r=e),n=le[t]||{},i=f.N(n.cached_at,0),o=(r._current_time||f.current_time_ms())-i,n.node&&i&&!(2e3<o)&&(r._node=Object.assign({},n.node),r.C=i,1)?Promise.resolve(e):Promise.resolve(e).then(se.my_read_node).catch(function(e){return de(e).then(function(e){return void 0===(e._node||{})[S]?Promise.reject(new Error("404 NOT FOUND. "+N+".id:"+(e._id||""))):e}).then(se.my_save_node)}).then(ve)):Promise.reject(new Error("._id is required!"))},Pe=function(e){if(!e||!e._node)return e;if(!B||!B.length)return e;var n=$?z($):null;if(!n)return e;var r=e._node||{};return B.reduce(function(e,r){if(r.startsWith("_")||r.startsWith("$"))return e;var t=e[r];return void 0===t||(t=t?n.decrypt(t):t,e[r]=t),e},r),e},ge=function(e){return e._id?e._node?Promise.resolve(e).then(ye).then(_e).then(se.my_save_node).then(fe).then(ve):Promise.reject(new Error("_node is required!")):Promise.reject(new Error("._id is required!"))},Se=function(e){var r=e._id;return r?e._node?e._current_time?(m(p,"- my_clone_node ("+r+")...."),Promise.resolve(e).then(function(n){if(!A)return n;var i=void 0!==n[R]?f.N(n[R],0):r,o=r;return K({_id:0}).then(function(e){var r=e._id,t=n._node||{};return t[S]=r,R&&(t[R]=i),I&&(t[I]=o),R&&(n[R]=i),I&&(n[I]=o),n._clone_id=r,n._node=t,n})}).then(function(e){var r=e._id;return e._node?e._clone_id?(e._id=e._clone_id,h.reduce(e,function(e,r,t){return t&&(t.startsWith("_")||t.startsWith("$")||0<=X.indexOf(t)||void 0!==e[t]&&(e[t]=r)),e},e._node),m(p,">> before save-node().. that._node =",f.json(e._node)),ge(e).then(function(){return e._id=r,e})):Promise.reject(new Error(p+"_clone_id is required!")):Promise.reject(new Error(p+"_node is required!"))}).then(function(e){return m(p,">> cloned-node["+e._id+" -> "+e._clone_id+"] res=",f.json(e._node)),e})):Promise.reject(new Error("._current_time is required!")):Promise.reject(new Error("._node is required!")):Promise.reject(new Error("._id is required!"))},je=function(e){return e._id?Promise.resolve(e).then(ce).then(se.my_delete_node).then(he).then(Ee).then(function(e){return m(p,">> deleted-node res=",f.json(e._node)),e}):Promise.reject(new Error("._id is required!"))},qe=function(e){return Promise.resolve(e).then(me)},Ne=function(e){if(!e)return e;if(!e._id)return Promise.reject(new Error("_id is required!"));var n=e._current_mode;return Promise.resolve(e).then(function(r){var e=!1;if("create"===n||"prepare"===n||"delete"===n||"destroy"===n?e=!0:"update"!==n&&"increment"!==n||(e=0<r._fields_count),e){var t=Y+":event:"+n;return m(p,">>> notify event-id:",t,", notifiable=",e),Ae(t,r).then(function(e){return r})}return r})},be=function(r){if(!r.records)return Promise.reject(new Error(p+"records is required!"));var e=r.records.slice(0);if(m(p,"- my_event_records().... records.len=",e.length),!e.length)return r;var t,n,i;return(t=e,n=Oe,i=f.promise(t.shift()),i=t.reduce(function(e,r){return e.then(function(){return n(r)})},i.then(function(e){return n(e)}))).catch(function(e){return l(p,"error ignored! =",e),r}).then(function(){return r})},Oe=function(e){if(!e)return Promise.reject(new Error("record is required"));if(!e.dynamodb&&!e.Sns)return Promise.reject(new Error("record.dynamodb|Sns is required"));var i=e.eventName||"EVENT",r=null;if(e.dynamodb){var o=e.table||e.eventSourceARN&&e.eventSourceARN.split("/")[1]||"";if(m(p,"--- process-record("+o+") ... "),N!==o)return m(p,"! ignore record-table: "+N+" != "+o+" "),e;var t=e.dynamodb.Keys?C.toJavascript(e.dynamodb.Keys):{},d=e.dynamodb.NewImage?C.toJavascript(e.dynamodb.NewImage):null,n=e.dynamodb.OldImage?C.toJavascript(e.dynamodb.OldImage):null,u=t[S],_="MODIFY"===i?f.diff(n,d):null,a=f.N(d&&d.updated_at||0),c=_?h.reduce(_,function(e,r){return e[r]=n[r],e},{}):null,s={_id:u,_node:d,_updated_at:a,_diff:_,_prev:c};switch(r=Promise.resolve(s),_&&m(p,">> "+o+".different["+u+"]=",f.json(_)),i){case"INSERT":case"MODIFY":r=r.then(function(t){var n=t._id;return se.my_get_updated_at(n).then(function(e){if(l(p,">> "+o+".updated_at["+n+"] res=",e," <- ",t._updated_at,t._updated_at!==e?"*":""),!e||t._updated_at>=e){var r=f.hash(d);return se.my_get_hash_value(n).then(function(e){return e&&e===r?(m(p,"! WARN! ignored due to hash-value matching =",e),t):(m(p,"! INFO! node was updated. hash :=",r,"<-",e),se.my_save_node(t).then(fe))})}return m(p,"! ignored due to old-record"),t})});break;case"REMOVE":m(p,">> removed!"),r=(r=r.then(se.my_delete_node)).then(he)}}else if(e.Sns){m(p,">> event-SNS =",e.Sns);r=Promise.resolve(e.Sns)}return r?r.then(function(r){var e={INSERT:"create",MODIFY:"update",REMOVE:"delete",EVENT:"event"}[i],t=!!e;if(r._current_mode=e,t){var n=Y+":record:"+e;return m(p,">>> notify event-id:",n,", notifiable=",t),Ae(n,r).then(function(e){return r})}return r}).then(function(){return e}):Promise.reject(new Error("invalid chain!"))},Ae=function(e,r){return Q.do_notify(e,r)},Ie=function(r){var e=[];if(N){var t=j.startsWith("#")?"S":"N";e.push(Promise.resolve("DynamoDb").then(function(e){return m(p,"# initialize ",e),u.do_create_table(N,S,t).then(function(e){return e.TableDescription.TableName===N})}).catch(function(e){if("ResourceInUseException"===e.code)return!1;throw e}))}else m(p,"DS: WARN! ignored configuration. DYNA_TABLE=",N);if(j&&!j.startsWith("#")?e.push(Promise.resolve("MySQL").then(function(e){return m(p,"# initialize MySQL (Sequence) "),i.do_create_id_seq(j,q).then(function(e){return m(p,"> create-id-seq res=",e),!0})}).catch(function(e){if("ER_TABLE_EXISTS_ERROR"===e.code)return!1;throw e})):j&&j.startsWith("#")&&1<j.length&&0<q?e.push(Promise.resolve("MySQL").then(function(e){m(p,"# initialize MySQL (Sequence) ");var r=j.substring(1);return i.do_create_id_seq(r,q).then(function(e){return m(p,"> create-id-seq["+r+"] res=",e),!0})}).catch(function(e){if("ER_TABLE_EXISTS_ERROR"===e.code)return!1;throw e})):m(p,"MS: WARN! ignored configuration. ID_TYPE=",j),T&&x&&V){var n={mappings:{_default_:{dynamic_templates:[{string_fields:{match:"*_multi",match_mapping_type:"string",mapping:{type:"multi_field",fields:{"{name}":{type:6<=L?"text":"string",index:"analyzed",omit_norms:!0,index_options:"docs"},"{name}.raw":{type:6<=L?"text":"string",index:"not_analyzed",ignore_above:256}}}}}],properties:{"@version":{type:6<=L?"text":"string",index:!(6<=L)&&"not_analyzed"},title:{type:6<=L?"text":"string"},name:{type:6<=L?"text":"string"},created_at:{type:"date",format:"strict_date_optional_time||epoch_millis"},updated_at:{type:"date",format:"strict_date_optional_time||epoch_millis"},deleted_at:{type:"date",format:"strict_date_optional_time||epoch_millis"}}}}};6<=L||(n.mappings._default_.all={enabled:!0}),e.push(Promise.resolve("ElasticSearch").then(function(e){return m(p,"# initialize ElasticSearch (Type) ",x),M.do_create_index_type(T,x,n).then(function(e){return m(p,"> create-es-index res=",e),!0})}).catch(function(e){if(l(p,"> create-es-index error=",e),"index_already_exists_exception"===e.type)return!0;throw e}).then(function(e){return e}))}else m(p,"MS: WARN! ignored configuration. ES_TYPE=",x);return Promise.all(e).then(function(e){return m(p,">> results=",e),r._result=e,r})},Re=function(r){var e=[];if(N&&(m(p,"# terminate DynamoDB"),e.push(u.do_delete_table(N).then(function(e){return e.TableDescription.TableName===N}).catch(function(e){if("ResourceNotFoundException"===e.code)return!0;throw e}))),j&&!j.startsWith("#"))m(p,"# terminate MySQL (Sequence) "),e.push(i.do_delete_id_seq(j).then(function(e){return m(p,"> delete-id-seq res=",e),!0}).catch(function(e){if("ER_BAD_TABLE_ERROR"===e.code)return!0;throw e}));else if(j&&j.startsWith("#")&&1<j.length&&0<q){m(p,"# terminate MySQL (Sequence) ");var t=j.substring(1);e.push(i.do_delete_id_seq(t).then(function(e){return m(p,"> delete-id-seq["+t+"] res=",e),!0}).catch(function(e){if("ER_BAD_TABLE_ERROR"===e.code)return!0;throw e}))}else m(p,"# ignored MySQL (Sequence) ");return T&&x?(m(p,"# terminate ES (Index) "),e.push(M.do_delete_index_type(T,null).then(function(e){return m(p,"> delete-es-index res=",e),!0}).catch(function(e){throw l(p,"> create-es-index error=",e),e}))):m(p,"# ignored ES (Index) "),Promise.all(e).then(function(e){return m(p,">> results=",e),r._result=e,r})},De=function(e){var r={a:1,b:2,x:3},t={x:3,a:1,b:2},n={b:2,x:3,a:1};r.name="hi",t.name="hi",n.name="hi",m(p,"# node1 =",f.json(r)),m(p,"> node2 =",f.json(t)),m(p,"> node3 =",f.json(n)),m(p,"# node1.sorted =",f.json(r,!0)),m(p,"> node2.sorted =",f.json(t,!0)),m(p,"> node3.sorted =",f.json(n,!0)),m(p,"# hash1 =",f.hash(1)),m(p,"> node1 =",f.hash(r)),m(p,"> node2 =",f.hash(t)),m(p,"> node3 =",f.hash(n));var i={updated_at:234,name:"test hash"};return Promise.resolve(i).then(function(e){return o.do_create_item("H#",1,e)}).then(function(){return o.do_get_item("H#",1).then(function(e){return m(p,"> read-back data=",e),e})}).then(function(){return se.my_set_node_footprint(1,i)}).then(function(){return se.my_get_updated_at(1).then(function(e){return m(p,"> updated-at value=",void 0===e?"undefined":_typeof(e),":",e),e})}).then(function(){return se.my_get_hash_value(1).then(function(e){return m(p,"> hash-value value=",void 0===e?"undefined":_typeof(e),":",e),e})})},Te=function(r){return m(p,"- my_test_self()...."),Promise.resolve(r).then(De).then(function(e){return e||r})};return c.do_prepare_chain=function(e,r,t,n){return H(e,r,t,n)},c.do_finish_chain=function(e,r,t,n){return J(e)},c.do_prepare=function(e,r){return H(e,r,"prepare").then(re).then(ge).then(Ne).then(J)},c.do_create=function(t,n){return H(t,n,"create").then(te).catch(function(e){l(p,"ERR! prepare_node_created =",e instanceof Error,e.message||e);var r=e&&e.message||"";if(e instanceof Error&&0<=r.indexOf("404 NOT FOUND"))return a(p,"WARN! AUTO TRY TO PREPARE NODE. ID="+t),H(t,n,"create").then(function(e){var r=e._current_time;return e=G(e),m(p,">> prepared old-node=",f.json(e._node)),e._node=ee(e._node,r),e});throw e}).then(ge).then(Ne).then(J)},c.do_saveES=function(e,r){return e?r?H(e,{},"save").then(ye).then(function(e){return e._node=r,e}).then(fe).then(J):Promise.reject(new Error("node is required!")):Promise.reject(new Error("id is required!"))},c.do_clone=function(e,r){return H(e,r,"clone").then(ne).then(Se).then(Ne).then(J)},c.do_read=function(e,r){return H(e,r,"read").then(G).then(function(e){return 0!==e._params_count&&0===e._fields_count?e:we(e)}).then(Ne).then(J)},c.do_readX=function(e,r){return H(e,r,"read").then(G).then(function(e){return 0!==e._params_count&&0===e._fields_count?e:we(e)}).then(Pe).then(Ne).then(J)},c.do_update=function(e,r){return H(e,r,"update").then(ie).then(function(e){return 0===e._fields_count?e:(n=(t=e)._id)?0===t._fields_count?(m(p,"! my_update_node() no need to update... fields_count="+t._fields_count),t):Promise.resolve(t).then(ye).then(ue).then(function(e){return e._updated_node?se.my_save_node(e):e}).then(function(e){return e._updated_node?fe(e):e}).then(function(e){return e._updated_node?ve(e):e}).then(function(e){return m(p,">> updated-node["+n+"] res=",f.json(e._updated_node)),e}).catch(function(e){var r=e&&e.message||"";if(l(p,">> updated-node["+n+"] err=",r),0<r.indexOf("an attribute that does not exist in the item"))return ge(t);throw e}):Promise.reject(new Error("._id is required!"));var t,n}).then(Ne).then(J)},c.do_increment=function(e,r){return H(e,r,"increment").then(ie).then(function(e){return 0===e._fields_count?e:(t=(r=e)._id)?0===r._fields_count?(m(p,"! my_increment_node() no need to increment... fields_count="+r._fields_count),r):Promise.resolve(r).then(ye).then(ae).then(function(e){return e._updated_node?se.my_save_node(e):e}).then(function(e){return e._updated_node?fe(e):e}).then(function(e){return e._updated_node?ve(e):e}).then(function(e){return m(p,">> increment-node["+t+"] res=",f.json(e._updated_node)),e}):Promise.reject(new Error("._id is required!"));var r,t}).then(Ne).then(J)},c.do_delete=function(e,r){return H(e,r,"delete").then(oe).then(ge).then(Ne).then(J)},c.do_destroy=function(e,r){return H(e,r,"destroy").then(je).then(Ne).then(J)},c.do_search=function(e,r){return H(e,r,"search").then(qe).then(J)},c.on_records=function(e,r){return H(e,r,"on_records").then(be).then(J)},c.do_initialize=function(e,r){return H(e,r,"initialize").then(Ie).then(J)},c.do_terminate=function(e,r){return H(e,r,"terminate").then(Re).then(J)},c.do_test_self=function(e,r){return H(e,r,"self-test").then(Te).then(J)},c.do_next_id=function(){return K({_id:0}).then(function(e){return e._id})},c}; |
{ | ||
"name": "lemoncloud-engine-js", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "lemoncloud.io - Lemon Engine Modle of Node for Robust Micro-service based on DynamoDB + ElastiCache + Elasticsearch.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -89,2 +89,3 @@ # lemoncloud-engine-js | ||
|-- |-- | ||
| 1.0.2 | `do_saveES()` direct save into ES. | ||
| 1.0.1 | updated package dependencies | ||
@@ -91,0 +92,0 @@ | 1.0.0 | support in-memory cache for node transaction. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
109444
124
2