@dreamtree-org/korm-js 1.0.47 → 1.0.48

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/Logger.js ADDED
@@ -0,0 +1 @@
1
+ const LOG_LEVELS={NONE:0,ERROR:1,WARN:2,INFO:3,LOG:4,DEBUG:5};class Logger{constructor(e={}){this.prefix=e.prefix||"[KORM]",this.enabled=!1!==e.enabled,this.level=this._parseLevel(e.level||process.env.KORM_LOG_LEVEL||"warn"),this.timestamps=e.timestamps||!1}_parseLevel(e){if("number"==typeof e)return e;const s=String(e).toUpperCase();return void 0!==LOG_LEVELS[s]?LOG_LEVELS[s]:LOG_LEVELS.WARN}_getTimestamp(){return(new Date).toISOString()}_formatMessage(e,s){const t=[this.prefix];return this.timestamps&&t.push(`[${this._getTimestamp()}]`),t.push(`[${e}]`),t}debug(...e){this.enabled&&this.level>=LOG_LEVELS.DEBUG&&console.debug(...this._formatMessage("DEBUG",e),...e)}log(...e){this.enabled&&this.level>=LOG_LEVELS.LOG&&console.log(...this._formatMessage("LOG",e),...e)}info(...e){this.enabled&&this.level>=LOG_LEVELS.INFO&&console.info(...this._formatMessage("INFO",e),...e)}warn(...e){this.enabled&&this.level>=LOG_LEVELS.WARN&&console.warn(...this._formatMessage("WARN",e),...e)}error(...e){this.enabled&&this.level>=LOG_LEVELS.ERROR&&console.error(...this._formatMessage("ERROR",e),...e)}setLevel(e){this.level=this._parseLevel(e)}enable(){this.enabled=!0}disable(){this.enabled=!1}child(e){return new Logger({prefix:`${this.prefix}${e}`,enabled:this.enabled,level:this.level,timestamps:this.timestamps})}}const logger=new Logger;module.exports=logger,module.exports.Logger=Logger,module.exports.LOG_LEVELS=LOG_LEVELS;
@@ -1 +1 @@
1
- const path=require("path");class HookService{constructor(e,t,o=null){this.db=e,this.utils=t,this.controllerWrapper=o,this.controllerWrapper.hookService=this,this.appRoot=o&&o.resolverPath?o.resolverPath:process.cwd()}loadModelClass(e){try{const t=path.join(this.appRoot,"models",`${e}.model.js`);delete require.cache[require.resolve(t)];return require(t)}catch(e){return void console.log("loadModelClass error",{err:e})}}getModelInstance(e){let t="string"==typeof e?e:e.modelName;const o=this.loadModelClass(t);if(o)return"function"==typeof o?new o:o}resolveModelHook(e,t,o){const r=e.modelName,s=this.loadModelClass(r);if(!s)return;const l=this.getModelInstance(e);let i;if("validate"===t)i="validate";else if("on"===t)i=`on${o.charAt(0).toUpperCase()+o.slice(1)}`;else if("before"===t)i=`before${o.charAt(0).toUpperCase()+o.slice(1)}`;else if("after"===t)i=`after${o.charAt(0).toUpperCase()+o.slice(1)}`;else{if("custom"!==t)return;i=`on${o.charAt(0).toUpperCase()+o.slice(1)}Action`}return"function"==typeof l[i]?l[i].bind(l):"function"==typeof s[i]?s[i].bind(s):void 0}async executeValidatorHook({model:e,action:t,request:o,ctx:r,controller:s}){const l=this.resolveModelHook(e,"validate",t);if(l)return await l({model:e,action:t,request:o,context:r,db:this.db,utils:this.utils,controller:s})}async executeBeforeHook({model:e,action:t,request:o,ctx:r,controller:s}){const l=this.resolveModelHook(e,"before",t);if(l)return await l({model:e,action:t,request:o,context:r,db:this.db,utils:this.utils,controller:s})}async executeAfterHook({model:e,action:t,data:o,request:r,ctx:s,controller:l}){const i=this.resolveModelHook(e,"after",t);return i?await i({model:e,action:t,data:o,request:r,context:s,db:this.db,utils:this.utils,controller:l}):o}async executeCustomAction({model:e,action:t,request:o,ctx:r,controller:s}){const l=this.resolveModelHook(e,"custom",t);if(l)return await l({model:e,action:t,request:o,context:r,db:this.db,utils:this.utils,controller:s});throw new Error(`No custom action hook found for ${e.modelName}.${t}`)}async executeHasSoftDeleteHook(e){const t=this.getModelInstance(e);if(!t)return!1;return t.hasOwnProperty("hasSoftDelete")&&!0===t.hasSoftDelete}}module.exports=HookService;
1
+ const path=require("path"),logger=require("../../Logger");class HookService{constructor(e,t,o=null){this.db=e,this.utils=t,this.controllerWrapper=o,this.controllerWrapper.hookService=this,this.appRoot=o&&o.resolverPath?o.resolverPath:process.cwd()}loadModelClass(e){try{const t=path.join(this.appRoot,"models",`${e}.model.js`);delete require.cache[require.resolve(t)];return require(t)}catch(e){return void logger.debug("loadModelClass error",{err:e})}}getModelInstance(e){let t="string"==typeof e?e:e.modelName;const o=this.loadModelClass(t);if(o)return"function"==typeof o?new o:o}resolveModelHook(e,t,o){const r=e.modelName,s=this.loadModelClass(r);if(!s)return;const l=this.getModelInstance(e);let i;if("validate"===t)i="validate";else if("on"===t)i=`on${o.charAt(0).toUpperCase()+o.slice(1)}`;else if("before"===t)i=`before${o.charAt(0).toUpperCase()+o.slice(1)}`;else if("after"===t)i=`after${o.charAt(0).toUpperCase()+o.slice(1)}`;else{if("custom"!==t)return;i=`on${o.charAt(0).toUpperCase()+o.slice(1)}Action`}return"function"==typeof l[i]?l[i].bind(l):"function"==typeof s[i]?s[i].bind(s):void 0}async executeValidatorHook({model:e,action:t,request:o,ctx:r,controller:s}){const l=this.resolveModelHook(e,"validate",t);if(l)return await l({model:e,action:t,request:o,context:r,db:this.db,utils:this.utils,controller:s})}async executeBeforeHook({model:e,action:t,request:o,ctx:r,controller:s}){const l=this.resolveModelHook(e,"before",t);if(l)return await l({model:e,action:t,request:o,context:r,db:this.db,utils:this.utils,controller:s})}async executeAfterHook({model:e,action:t,data:o,request:r,ctx:s,controller:l}){const i=this.resolveModelHook(e,"after",t);return i?await i({model:e,action:t,data:o,request:r,context:s,db:this.db,utils:this.utils,controller:l}):o}async executeCustomAction({model:e,action:t,request:o,ctx:r,controller:s}){const l=this.resolveModelHook(e,"custom",t);if(l)return await l({model:e,action:t,request:o,context:r,db:this.db,utils:this.utils,controller:s});throw new Error(`No custom action hook found for ${e.modelName}.${t}`)}async executeHasSoftDeleteHook(e){const t=this.getModelInstance(e);if(!t)return!1;return t.hasOwnProperty("hasSoftDelete")&&!0===t.hasSoftDelete}}module.exports=HookService;
@@ -1 +1 @@
1
- const HelperUtility=require("./HelperUtility");class QueryBuilder{constructor(e,t,r=null){this.db=e,this.utils=t,this.controllerWrapper=r,this.helperUtility=new HelperUtility}getHookService(){return this.controllerWrapper.hookService}getQueryBuilder(e,t=null){let r=this.db(e.table);return t&&(r=t),r._getMyModel=()=>e,r}parseValue(e){return this.helperUtility.parseValue(e)}parseWhereValue(e){return this.helperUtility.parseWhereValue(e)}parseWhereColumn(e){return this.helperUtility.parseWhereColumn(e)}_applyOrWhereCondition(e,t,r,i){if(null!==i)if(Array.isArray(i))e.orWhereIn(t,i);else switch(r){case"between":e.orWhereBetween(t,i);break;case"notBetween":e.orWhereNotBetween(t,i);break;case"in":e.orWhereIn(t,i);break;case"notIn":e.orWhereNotIn(t,i);break;case"like":e.orWhereRaw(`\`${t}\` LIKE ?`,[i]);break;default:e.orWhere(t,r,i)}else e.orWhereNull(t)}_applyAndWhereCondition(e,t,r,i){if(null!==i)if(Array.isArray(i))e.whereIn(t,i);else switch(r){case"between":e.whereBetween(t,i);break;case"notBetween":e.whereNotBetween(t,i);break;case"in":e.whereIn(t,i);break;case"notIn":e.whereNotIn(t,i);break;case"like":e.whereRaw(`\`${t}\` LIKE ?`,[i]);break;default:e.where(t,r,i)}else e.whereNull(t)}buildWithTree(e,t=null){return this.helperUtility.dotWalkTree(e,{resolver:({current:e,part:r,source:i})=>t?t({current:e,part:r,source:i}):{}})}async fetchRelatedRows(e,t){if(e.through){let r=await this.db(e.through).whereIn(e.throughLocalKey,t);return await this.db(e.table).whereIn(e.foreignKey,r.map(t=>t[e.throughForeignKey]))}return this.db(e.table).whereIn(e.foreignKey,t)}async fetchAndAttachRelated(e){const{parentRows:t,relName:r,model:i,withTree:o,relation:l}=e;if(!l){const e=this.getHookService().getModelInstance(i),l=`get${r.charAt(0).toUpperCase()+r.slice(1)}Relation`;if("function"==typeof e[l]){const a={rows:t,relName:r,model:i,withTree:o,controller:this.controllerWrapper,relation:i.hasRelations[r],qb:this,db:this.db};await e[l](a)}return t}let a=t.map(e=>e[l.localKey]),s=[];const n="one"===l?.type;s=await this.fetchRelatedRows(l,a);let h=new Map;for(const e of s){let t=e[l.foreignKey];h.has(t)||h.set(t,[]),h.get(t).push(e)}for(const e of t){let t=e[l.localKey];n&&1==h.get(t)?.length?e[r]=h.get(t)[0]:e[r]=h.get(t)||[]}let c=Object.keys(o);for(const e of c){let i=o[e],l=t.filter(e=>n?e[r]:e[r].length>0).map(e=>e[r]).reduce((e,t)=>e.concat(t),[]),a=this.utils.getModel(this.controllerWrapper,r);await this.fetchAndAttachRelated({parentRows:l,relName:e,model:a,withTree:i,relation:a.hasRelations[e]})}return t}filterWhere(e,t=""){return t?Object.keys(e).filter(e=>e.includes(t)).reduce((r,i)=>(r[i.replace(t,"")]=e[i],r),{}):Object.keys(e).filter(e=>!e.includes(".")).reduce((t,r)=>(t[r]=e[r],t),{})}async getQuery(e,t){try{const{where:r={},with:i,withWhere:o,select:l,orderBy:a={column:"id",direction:"asc"},limit:s=10,offset:n=0,page:h,groupBy:c,having:p,distinct:y,join:u,leftJoin:f,rightJoin:d,innerJoin:g,count:w=!1}=t;let b=this.getQueryBuilder(e);l&&(Array.isArray(l)||"string"==typeof l)?b.select(l):b.select("*"),y&&(Array.isArray(y)||"string"==typeof y?b.distinct(y):b.distinct()),u&&this._applyJoins(b,u,"join"),f&&this._applyJoins(b,f,"leftJoin"),d&&this._applyJoins(b,d,"rightJoin"),g&&this._applyJoins(b,g,"innerJoin"),this._applyWhereClause(b,r,i),c&&(Array.isArray(c),b.groupBy(c)),p&&this._applyHavingClause(b,p),a&&this._applyOrderBy(b,a);let W=!1,A=s,m=n,_=1,k=0;h&&s>0&&(_=Math.max(1,parseInt(h)),m=(_-1)*s),s>0&&(b.limit(A),m>0&&b.offset(m));const j=await b;if(i&&i.length>0){const t=this.buildWithTree(i);for(const r of Object.keys(t))await this.fetchAndAttachRelated({parentRows:j,relName:r,model:e,withTree:t[r],relation:e.hasRelations[r]})}let B=null;if(s>0)try{let t=this.getQueryBuilder(e);r&&Object.keys(r).length>0&&this._applyWhereClause(t,r),u&&this._applyJoins(t,u,"join"),f&&this._applyJoins(t,f,"leftJoin"),d&&this._applyJoins(t,d,"rightJoin"),g&&this._applyJoins(t,g,"innerJoin");B=(await t.count("* as cnt").first()).cnt}catch(e){console.warn("Failed to get total count:",e.message),B=j.length}s>0&&null!==B&&(k=Math.ceil(B/s),W=_<k);return{data:j,totalCount:B,...s>0?{pagination:{page:_,limit:A,offset:m,totalPages:k,hasNext:W,hasPrev:_>1,nextPage:W?_+1:null,prevPage:_>1?_-1:null}}:{}}}catch(e){throw console.error("QueryService.getQuery error:",e),new Error(`Failed to execute query: ${e.message}`)}}_applyWhereWithArray(e,t,r){for(const i of t)this._applyWhereClause(e,i,r)}_applyWhereClause(e,t,r=[]){if(Array.isArray(t))this._applyWhereWithArray(e,t,r);else{if(t&&Object.keys(t).length>0){let r=this.helperUtility.getDotWalkQuery(t);console.log({filteredWhere:r}),r=this.helperUtility.objectFilter(r,(e,t)=>"object"!=typeof t||null===t);for(const[t,i]of Object.entries(r)){const{joinType:r="AND",column:o}=this.parseWhereColumn(t),{operator:l,value:a}=this.parseWhereValue(i);"AND"===r?this._applyAndWhereCondition(e,o,l,a):this._applyOrWhereCondition(e,o,l,a)}}r&&r.length>0&&this._applyNestedWhere(e,t,r)}}_applyNestedWhere(e,t,r){let i=this,o=e._getMyModel();if(r&&r.length>0)for(const l of r){let a=this.helperUtility.getDotWalkQuery(t,l);if(a&&Object.keys(a).length>0){let t=this.utils.getModel(this.controllerWrapper,l),s=o.hasRelations[l],n=r.map(e=>this.helperUtility.pluckDotWalkKey(e,1));e.whereExists(function(){let e=i.getQueryBuilder(t,this.select("*").from(t.table));e.whereRaw(`${s.foreignKey} = ${o.table}.${s.localKey}`),i._applyWhereClause(e,a,n)})}}}_applyJoins(e,t,r){const i=Array.isArray(t)?t:[t];for(const t of i)"string"==typeof t?e[r](t):"object"==typeof t&&(t.table&&t.on?e[r](t.table,t.on):t.table&&t.first&&t.operator&&t.second&&e[r](t.table,t.first,t.operator,t.second))}_applyWithWhere(e,t){try{if(Array.isArray(t))for(const r of t)"string"==typeof r?e.withWhere(r):"object"==typeof r&&e.withWhere(r.column,r.operator,r.value);else if("object"==typeof t)for(const[r,i]of Object.entries(t))e.withWhere(r,i)}catch(e){console.warn("Failed to apply withWhere:",e.message)}}_applyHavingClause(e,t){for(const[r,i]of Object.entries(t))"object"==typeof i&&i.operator?e.having(r,i.operator,i.value):e.having(r,i)}_applyOrderBy(e,t){if(Array.isArray(t))for(const r of t)"string"==typeof r?e.orderBy(r):"object"==typeof r&&e.orderBy(r.column,r.direction||"asc");else"string"==typeof t?e.orderBy(t):"object"==typeof t&&e.orderBy(t.column,t.direction||"asc")}}module.exports=QueryBuilder;
1
+ const HelperUtility=require("./HelperUtility"),logger=require("../../Logger");class QueryBuilder{constructor(e,t,r=null){this.db=e,this.utils=t,this.controllerWrapper=r,this.helperUtility=new HelperUtility}getHookService(){return this.controllerWrapper.hookService}getQueryBuilder(e,t=null){let r=this.db(e.table);return t&&(r=t),r._getMyModel=()=>e,r}parseValue(e){return this.helperUtility.parseValue(e)}parseWhereValue(e){return this.helperUtility.parseWhereValue(e)}parseWhereColumn(e){return this.helperUtility.parseWhereColumn(e)}_applyOrWhereCondition(e,t,r,i){if(null!==i)if(Array.isArray(i))e.orWhereIn(t,i);else switch(r){case"between":e.orWhereBetween(t,i);break;case"notBetween":e.orWhereNotBetween(t,i);break;case"in":e.orWhereIn(t,i);break;case"notIn":e.orWhereNotIn(t,i);break;case"like":e.orWhereRaw(`\`${t}\` LIKE ?`,[i]);break;default:e.orWhere(t,r,i)}else e.orWhereNull(t)}_applyAndWhereCondition(e,t,r,i){if(null!==i)if(Array.isArray(i))e.whereIn(t,i);else switch(r){case"between":e.whereBetween(t,i);break;case"notBetween":e.whereNotBetween(t,i);break;case"in":e.whereIn(t,i);break;case"notIn":e.whereNotIn(t,i);break;case"like":e.whereRaw(`\`${t}\` LIKE ?`,[i]);break;default:e.where(t,r,i)}else e.whereNull(t)}buildWithTree(e,t=null){return this.helperUtility.dotWalkTree(e,{resolver:({current:e,part:r,source:i})=>t?t({current:e,part:r,source:i}):{}})}_getWithWhereForRelation(e,t){if(!e||"object"!=typeof e)return{};const r=`${t}.`,i={};for(const[t,o]of Object.entries(e))if(t.startsWith(r)){i[t.slice(r.length)]=o}return i}_applyWithWhereConditions(e,t){for(const[r,i]of Object.entries(t)){const{joinType:t="AND",column:o}=this.parseWhereColumn(r),{operator:l,value:a}=this.parseWhereValue(i);"AND"===t?this._applyAndWhereCondition(e,o,l,a):this._applyOrWhereCondition(e,o,l,a)}}async fetchRelatedRows(e,t,r={}){if(e.through){let i=await this.db(e.through).whereIn(e.throughLocalKey,t),o=this.db(e.table).whereIn(e.foreignKey,i.map(t=>t[e.throughForeignKey]));return Object.keys(r).length>0&&this._applyWithWhereConditions(o,r),o}{let i=this.db(e.table).whereIn(e.foreignKey,t);return Object.keys(r).length>0&&this._applyWithWhereConditions(i,r),i}}async fetchAndAttachRelated(e){const{parentRows:t,relName:r,model:i,withTree:o,relation:l,withWhere:a={}}=e;if(!l){const e=this.getHookService().getModelInstance(i),l=`get${r.charAt(0).toUpperCase()+r.slice(1)}Relation`;if("function"==typeof e[l]){const s={rows:t,relName:r,model:i,withTree:o,controller:this.controllerWrapper,relation:i.hasRelations[r],qb:this,db:this.db,withWhere:a};await e[l](s)}return t}let s=t.map(e=>e[l.localKey]),n=[];const h="one"===l?.type,p=this._getWithWhereForRelation(a,r);n=await this.fetchRelatedRows(l,s,p);let c=new Map;for(const e of n){let t=e[l.foreignKey];c.has(t)||c.set(t,[]),c.get(t).push(e)}for(const e of t){let t=e[l.localKey];h&&1==c.get(t)?.length?e[r]=c.get(t)[0]:e[r]=c.get(t)||[]}let y=Object.keys(o);for(const e of y){let i=o[e],l=t.filter(e=>h?e[r]:e[r].length>0).map(e=>e[r]).reduce((e,t)=>e.concat(t),[]),s=this.utils.getModel(this.controllerWrapper,r);await this.fetchAndAttachRelated({parentRows:l,relName:e,model:s,withTree:i,relation:s.hasRelations[e],withWhere:a})}return t}filterWhere(e,t=""){return t?Object.keys(e).filter(e=>e.includes(t)).reduce((r,i)=>(r[i.replace(t,"")]=e[i],r),{}):Object.keys(e).filter(e=>!e.includes(".")).reduce((t,r)=>(t[r]=e[r],t),{})}async getQuery(e,t){try{const{where:r={},with:i,withWhere:o,select:l,orderBy:a={column:"id",direction:"asc"},limit:s=10,offset:n=0,page:h,groupBy:p,having:c,distinct:y,join:u,leftJoin:f,rightJoin:g,innerJoin:d,count:W=!1}=t;let w=this.getQueryBuilder(e);l&&(Array.isArray(l)||"string"==typeof l)?w.select(l):w.select("*"),y&&(Array.isArray(y)||"string"==typeof y?w.distinct(y):w.distinct()),u&&this._applyJoins(w,u,"join"),f&&this._applyJoins(w,f,"leftJoin"),g&&this._applyJoins(w,g,"rightJoin"),d&&this._applyJoins(w,d,"innerJoin"),this._applyWhereClause(w,r,i),p&&(Array.isArray(p),w.groupBy(p)),c&&this._applyHavingClause(w,c),a&&this._applyOrderBy(w,a);let b=!1,_=s,A=n,m=1,k=0;h&&s>0&&(m=Math.max(1,parseInt(h)),A=(m-1)*s),s>0&&(w.limit(_),A>0&&w.offset(A));const j=await w;if(i&&i.length>0){const t=this.buildWithTree(i);for(const r of Object.keys(t))await this.fetchAndAttachRelated({parentRows:j,relName:r,model:e,withTree:t[r],relation:e.hasRelations[r],withWhere:o||{}})}let C=null;if(s>0)try{let t=this.getQueryBuilder(e);r&&Object.keys(r).length>0&&this._applyWhereClause(t,r),u&&this._applyJoins(t,u,"join"),f&&this._applyJoins(t,f,"leftJoin"),g&&this._applyJoins(t,g,"rightJoin"),d&&this._applyJoins(t,d,"innerJoin");C=(await t.count("* as cnt").first()).cnt}catch(e){logger.warn("Failed to get total count:",e.message),C=j.length}s>0&&null!==C&&(k=Math.ceil(C/s),b=m<k);return{data:j,totalCount:C,...s>0?{pagination:{page:m,limit:_,offset:A,totalPages:k,hasNext:b,hasPrev:m>1,nextPage:b?m+1:null,prevPage:m>1?m-1:null}}:{}}}catch(e){throw logger.error("QueryService.getQuery error:",e),new Error(`Failed to execute query: ${e.message}`)}}_applyWhereWithArray(e,t,r){for(const i of t)this._applyWhereClause(e,i,r)}_applyWhereClause(e,t,r=[]){if(Array.isArray(t))this._applyWhereWithArray(e,t,r);else{if(t&&Object.keys(t).length>0){let r=this.helperUtility.getDotWalkQuery(t);logger.debug({filteredWhere:r}),r=this.helperUtility.objectFilter(r,(e,t)=>"object"!=typeof t||null===t);for(const[t,i]of Object.entries(r)){const{joinType:r="AND",column:o}=this.parseWhereColumn(t),{operator:l,value:a}=this.parseWhereValue(i);"AND"===r?this._applyAndWhereCondition(e,o,l,a):this._applyOrWhereCondition(e,o,l,a)}}r&&r.length>0&&this._applyNestedWhere(e,t,r)}}_applyNestedWhere(e,t,r){let i=this,o=e._getMyModel();if(r&&r.length>0)for(const l of r){let a=this.helperUtility.getDotWalkQuery(t,l);if(a&&Object.keys(a).length>0){let t=this.utils.getModel(this.controllerWrapper,l),s=o.hasRelations[l],n=r.map(e=>this.helperUtility.pluckDotWalkKey(e,1));e.whereExists(function(){let e=i.getQueryBuilder(t,this.select("*").from(t.table));e.whereRaw(`${s.foreignKey} = ${o.table}.${s.localKey}`),i._applyWhereClause(e,a,n)})}}}_applyJoins(e,t,r){const i=Array.isArray(t)?t:[t];for(const t of i)"string"==typeof t?e[r](t):"object"==typeof t&&(t.table&&t.on?e[r](t.table,t.on):t.table&&t.first&&t.operator&&t.second&&e[r](t.table,t.first,t.operator,t.second))}_applyWithWhere(e,t){try{if(Array.isArray(t))for(const r of t)"string"==typeof r?e.withWhere(r):"object"==typeof r&&e.withWhere(r.column,r.operator,r.value);else if("object"==typeof t)for(const[r,i]of Object.entries(t))e.withWhere(r,i)}catch(e){logger.warn("Failed to apply withWhere:",e.message)}}_applyHavingClause(e,t){for(const[r,i]of Object.entries(t))"object"==typeof i&&i.operator?e.having(r,i.operator,i.value):e.having(r,i)}_applyOrderBy(e,t){if(Array.isArray(t))for(const r of t)"string"==typeof r?e.orderBy(r):"object"==typeof r&&e.orderBy(r.column,r.direction||"asc");else"string"==typeof t?e.orderBy(t):"object"==typeof t&&e.orderBy(t.column,t.direction||"asc")}}module.exports=QueryBuilder;
@@ -1 +1 @@
1
- const CurdTable=require("./CurdTable"),HelperUtility=require("./HelperUtility");class SyncTable{constructor(e,t,n=null){this.db=e,this.utils=t,this.controllerWrapper=n,this.curd=new CurdTable(e,t,n),this.helperUtility=new HelperUtility}_getClientName(){return"mysql"}async executeSql(e,t=[]){return this.db.raw(e,t)}async existsTable(e){return this.db.schema.hasTable(e)}async getCurrentColumns(e){const t=this.db.client.database(),[n]=await this.executeSql("\n SELECT \n c.COLUMN_NAME,\n c.DATA_TYPE,\n c.COLUMN_TYPE,\n c.IS_NULLABLE,\n c.COLUMN_KEY,\n c.EXTRA,\n c.CHARACTER_MAXIMUM_LENGTH,\n c.COLUMN_DEFAULT,\n c.COLUMN_COMMENT AS COMMENT,\n kcu.REFERENCED_TABLE_NAME,\n kcu.REFERENCED_COLUMN_NAME\n FROM information_schema.COLUMNS c\n LEFT JOIN information_schema.KEY_COLUMN_USAGE kcu\n ON c.TABLE_SCHEMA = kcu.TABLE_SCHEMA\n AND c.TABLE_NAME = kcu.TABLE_NAME\n AND c.COLUMN_NAME = kcu.COLUMN_NAME\n WHERE c.TABLE_SCHEMA = ?\n AND c.TABLE_NAME = ?\n ",[t,e]),a={};for(const e of n)a[e.COLUMN_NAME]=this.utils.formatColumnDef(e.COLUMN_NAME,e);return a}hasColumnChanged(e,t){const n={isNullableChanged:e.nullable!==t.nullable,isTypeChanged:e.type!==t.type,isSizeChanged:e.size!==t.size,isUnsignedChanged:e.isUnsigned!==t.isUnsigned,isPrimaryChanged:e.primary!==t.primary,isUniqueChanged:e.unique!==t.unique,isAutoIncrementChanged:e.autoIncrement!==t.autoIncrement,isDefaultChanged:e.default!==t.default,isOnUpdateChanged:e.onUpdate!==t.onUpdate,isCommentChanged:e.comment!==t.comment,isForeignKeyChanged:e.hasForeignKey!==t.hasForeignKey},a=Object.values(n).some(Boolean);return a&&console.log({changes:n,oldComment:e.comment,newComment:t.comment,name:e.name}),a}async getAlterations(e){const t={add:[],drop:[],modify:[]},n=await this.getCurrentColumns(e.table);for(const[a,s]of Object.entries(e.columns)){const e=this.utils.formatColumnSchema(a,s),o=n[a];o?(e.oldColDef=o,this.hasColumnChanged(o,e)&&t.modify.push(e)):t.add.push(e)}for(const a of Object.keys(n))e.columns[a]||t.drop.push({name:a});return t}getColumnStr(e,t,n={actionType:"CREATE",tableName:""}){const{actionType:a,tableName:s}=n,o="string"==typeof t?this.utils.formatColumnSchema(e,t):t,i=o.type,l=o.size??this.utils.getDefaultTypeSize(i);let r=`\`${e}\` ${i}${l?`(${l})`:""}`;if(o.isUnsigned&&(r+=" UNSIGNED"),o.primary&&["CREATE","ADD_COLUMN"].includes(a)&&(r+=" PRIMARY KEY"),o.autoIncrement&&(r+=" AUTO_INCREMENT"),o.nullable||(r+=" NOT NULL"),o.unique&&(r+=" UNIQUE"),o.default&&(r+=` DEFAULT ${o.default}`),o.onUpdate&&(r+=` ON UPDATE ${o.onUpdate}`),o.comment&&(r+=` COMMENT '${this.utils.escapeComment(o.comment)}'`),o.hasForeignKey&&1===o.foreignMapTables?.length&&"CREATE"===a){const{table:t,column:n}=o.foreignMapTables[0],a=`idx_${s}__${e}__fk_${t}_${n}`;r+=`, KEY \`${a}\` (\`${e}\`), CONSTRAINT \`cn_${a}\`\n FOREIGN KEY (\`${e}\`) REFERENCES \`${t}\` (\`${n}\`)\n ON DELETE RESTRICT ON UPDATE RESTRICT`}return r}async createTable(e){const t=e.table,n=e.columns,a=[];for(const[e,s]of Object.entries(n))a.push(this.getColumnStr(e,s,{actionType:"CREATE",tableName:t}));const s=`CREATE TABLE IF NOT EXISTS \`${t}\` (${a.join(", ")})`;await this.executeSql(s)}async alterTable(e,t){if(!t||"object"!=typeof t)throw new Error("alterations must be an object");const n=[];if(t.add?.length)for(const a of t.add)n.push(`ADD COLUMN ${this.getColumnStr(a.name,a,{actionType:"ADD_COLUMN",tableName:e})}`);if(t.drop?.length)for(const e of t.drop)n.push(`DROP COLUMN \`${e.name}\``);if(t.modify?.length)for(const a of t.modify)n.push(`MODIFY COLUMN ${this.getColumnStr(a.name,a,{actionType:"MODIFY_COLUMN",tableName:e})}`);if(!n.length)return void console.log("No alterations to apply for",e);const a=`ALTER TABLE \`${e}\` ${n.join(", ")}`;await this.executeSql(a)}async alterColumn(e,t,n){const a=`ALTER TABLE \`${e}\` MODIFY COLUMN ${this.getColumnStr(t,n,{actionType:"MODIFY_COLUMN",tableName:e})}`;await this.executeSql(a)}async alterIndex(e,t,n){const a=`ALTER TABLE \`${e}\` MODIFY INDEX ${`${t} ${n.type} ${n.unique?"UNIQUE":""}`}`;await this.executeSql(a)}async dropIndex(e,t){const n=`DROP INDEX \`${t}\` ON \`${e}\``;await this.executeSql(n)}async dropTable(e){const t=`DROP TABLE IF EXISTS \`${e}\``;await this.executeSql(t)}async updateTable(e){console.log(`Update table not implemented for ${e.table}`)}async syncTable(e){if(await this.existsTable(e.table)){const t=await this.getAlterations(e);return console.log(...Object.values(t)),void await this.alterTable(e.table,t)}await this.createTable(e)}async syncSeedData(e,t){let n=this.utils.getModel(this.controllerWrapper,t),a=await this.curd.processRequest({action:"count"},n.name,{isCallFromServer:!0});if(console.log("count",a),a>0)console.log("Seed data already synced for",t);else if(e.seed&&Array.isArray(e.seed)){for(const t of e.seed)await this.curd.processRequest({action:"create",data:t},n.name,{isCallFromServer:!0});console.log("Seed data synced for",t)}}async syncDatabase(){if(!this.controllerWrapper?.schema)throw new Error("controllerWrapper.schema not set.");const e=this.controllerWrapper.schema;for(const t of Object.keys(e))await this.syncTable(e[t]),await this.syncSeedData(e[t],t);console.log("Database synced by SyncTable...")}async getTablesOfDatabase(){const[e]=await this.db.raw("SHOW TABLES");return e.map(e=>e[`Tables_in_${this.db.client.database()}`])}getColumnString(e){return Object.keys(e).reduce((t,n)=>{const a=e[n];let s=a.type,o=a.size,i=a.isUnsigned,l=a.primary,r=a.autoIncrement,c=a.nullable,u=a.unique,m=a.default,d=a.onUpdate,E=a.comment,h=a.hasForeignKey,C=a.foreignMapTables?.[0]?.table,T=a.foreignMapTables?.[0]?.column;return t[n]=`${s}`,o&&(t[n]+=`|size:${o}`),i&&(t[n]+="|unsigned"),l&&(t[n]+="|primaryKey"),r&&(t[n]+="|autoIncrement"),c&&(t[n]+="|nullable"),u&&(t[n]+="|unique"),m&&(t[n]+=`|default:${m}`),d&&(t[n]+=`|onUpdate:${d}`),E&&(t[n]+=`|comment:${E}`),h&&(t[n]+=`|foreignKey:${C}:${T}`),t},{})}async getRelations(e){await this.db.raw(`SHOW CREATE TABLE ${e}`);return{}}async generateSchema(){const e=await this.getTablesOfDatabase(),t={};for(const n of e){let e=this.helperUtility.modelName(n),a=await this.getRelations(n);t[e]={table:n,alias:e,columns:this.getColumnString(await this.getCurrentColumns(n)),modelName:e,seed:[],hasRelations:a,indexes:[]}}return t}}module.exports=SyncTable;
1
+ const CurdTable=require("./CurdTable"),HelperUtility=require("./HelperUtility"),logger=require("../../Logger");class SyncTable{constructor(e,t,a=null){this.db=e,this.utils=t,this.controllerWrapper=a,this.curd=new CurdTable(e,t,a),this.helperUtility=new HelperUtility}_getClientName(){return"mysql"}async executeSql(e,t=[]){return this.db.raw(e,t)}async existsTable(e){return this.db.schema.hasTable(e)}async getCurrentColumns(e){const t=this.db.client.database(),[a]=await this.executeSql("\n SELECT \n c.COLUMN_NAME,\n c.DATA_TYPE,\n c.COLUMN_TYPE,\n c.IS_NULLABLE,\n c.COLUMN_KEY,\n c.EXTRA,\n c.CHARACTER_MAXIMUM_LENGTH,\n c.COLUMN_DEFAULT,\n c.COLUMN_COMMENT AS COMMENT,\n kcu.REFERENCED_TABLE_NAME,\n kcu.REFERENCED_COLUMN_NAME\n FROM information_schema.COLUMNS c\n LEFT JOIN information_schema.KEY_COLUMN_USAGE kcu\n ON c.TABLE_SCHEMA = kcu.TABLE_SCHEMA\n AND c.TABLE_NAME = kcu.TABLE_NAME\n AND c.COLUMN_NAME = kcu.COLUMN_NAME\n WHERE c.TABLE_SCHEMA = ?\n AND c.TABLE_NAME = ?\n ",[t,e]),n={};for(const e of a)n[e.COLUMN_NAME]=this.utils.formatColumnDef(e.COLUMN_NAME,e);return n}hasColumnChanged(e,t){const a={isNullableChanged:e.nullable!==t.nullable,isTypeChanged:e.type!==t.type,isSizeChanged:e.size!==t.size,isUnsignedChanged:e.isUnsigned!==t.isUnsigned,isPrimaryChanged:e.primary!==t.primary,isUniqueChanged:e.unique!==t.unique,isAutoIncrementChanged:e.autoIncrement!==t.autoIncrement,isDefaultChanged:e.default!==t.default,isOnUpdateChanged:e.onUpdate!==t.onUpdate,isCommentChanged:e.comment!==t.comment,isForeignKeyChanged:e.hasForeignKey!==t.hasForeignKey},n=Object.values(a).some(Boolean);return n&&logger.debug({changes:a,oldComment:e.comment,newComment:t.comment,name:e.name}),n}async getAlterations(e){const t={add:[],drop:[],modify:[]},a=await this.getCurrentColumns(e.table);for(const[n,s]of Object.entries(e.columns)){const e=this.utils.formatColumnSchema(n,s),o=a[n];o?(e.oldColDef=o,this.hasColumnChanged(o,e)&&t.modify.push(e)):t.add.push(e)}for(const n of Object.keys(a))e.columns[n]||t.drop.push({name:n});return t}getColumnStr(e,t,a={actionType:"CREATE",tableName:""}){const{actionType:n,tableName:s}=a,o="string"==typeof t?this.utils.formatColumnSchema(e,t):t,i=o.type,r=o.size??this.utils.getDefaultTypeSize(i);let l=`\`${e}\` ${i}${r?`(${r})`:""}`;if(o.isUnsigned&&(l+=" UNSIGNED"),o.primary&&["CREATE","ADD_COLUMN"].includes(n)&&(l+=" PRIMARY KEY"),o.autoIncrement&&(l+=" AUTO_INCREMENT"),o.nullable||(l+=" NOT NULL"),o.unique&&(l+=" UNIQUE"),o.default&&(l+=` DEFAULT ${o.default}`),o.onUpdate&&(l+=` ON UPDATE ${o.onUpdate}`),o.comment&&(l+=` COMMENT '${this.utils.escapeComment(o.comment)}'`),o.hasForeignKey&&1===o.foreignMapTables?.length&&"CREATE"===n){const{table:t,column:a}=o.foreignMapTables[0],n=`idx_${s}__${e}__fk_${t}_${a}`;l+=`, KEY \`${n}\` (\`${e}\`), CONSTRAINT \`cn_${n}\`\n FOREIGN KEY (\`${e}\`) REFERENCES \`${t}\` (\`${a}\`)\n ON DELETE RESTRICT ON UPDATE RESTRICT`}return l}async createTable(e){const t=e.table,a=e.columns,n=[];for(const[e,s]of Object.entries(a))n.push(this.getColumnStr(e,s,{actionType:"CREATE",tableName:t}));const s=`CREATE TABLE IF NOT EXISTS \`${t}\` (${n.join(", ")})`;await this.executeSql(s)}async alterTable(e,t){if(!t||"object"!=typeof t)throw new Error("alterations must be an object");const a=[];if(t.add?.length)for(const n of t.add)a.push(`ADD COLUMN ${this.getColumnStr(n.name,n,{actionType:"ADD_COLUMN",tableName:e})}`);if(t.drop?.length)for(const e of t.drop)a.push(`DROP COLUMN \`${e.name}\``);if(t.modify?.length)for(const n of t.modify)a.push(`MODIFY COLUMN ${this.getColumnStr(n.name,n,{actionType:"MODIFY_COLUMN",tableName:e})}`);if(!a.length)return void logger.info("No alterations to apply for",e);const n=`ALTER TABLE \`${e}\` ${a.join(", ")}`;await this.executeSql(n)}async alterColumn(e,t,a){const n=`ALTER TABLE \`${e}\` MODIFY COLUMN ${this.getColumnStr(t,a,{actionType:"MODIFY_COLUMN",tableName:e})}`;await this.executeSql(n)}async alterIndex(e,t,a){const n=`ALTER TABLE \`${e}\` MODIFY INDEX ${`${t} ${a.type} ${a.unique?"UNIQUE":""}`}`;await this.executeSql(n)}async dropIndex(e,t){const a=`DROP INDEX \`${t}\` ON \`${e}\``;await this.executeSql(a)}async dropTable(e){const t=`DROP TABLE IF EXISTS \`${e}\``;await this.executeSql(t)}async updateTable(e){logger.warn(`Update table not implemented for ${e.table}`)}async syncTable(e){if(await this.existsTable(e.table)){const t=await this.getAlterations(e);return logger.debug(...Object.values(t)),void await this.alterTable(e.table,t)}await this.createTable(e)}async syncSeedData(e,t){let a=this.utils.getModel(this.controllerWrapper,t),n=await this.curd.processRequest({action:"count"},a.name,{isCallFromServer:!0});if(logger.debug("count",n),n>0)logger.info("Seed data already synced for",t);else if(e.seed&&Array.isArray(e.seed)){for(const t of e.seed)await this.curd.processRequest({action:"create",data:t},a.name,{isCallFromServer:!0});logger.info("Seed data synced for",t)}}async syncDatabase(){if(!this.controllerWrapper?.schema)throw new Error("controllerWrapper.schema not set.");const e=this.controllerWrapper.schema;for(const t of Object.keys(e))await this.syncTable(e[t]),await this.syncSeedData(e[t],t);logger.info("Database synced by SyncTable...")}async getTablesOfDatabase(){const[e]=await this.db.raw("SHOW TABLES");return e.map(e=>e[`Tables_in_${this.db.client.database()}`])}getColumnString(e){return Object.keys(e).reduce((t,a)=>{const n=e[a];let s=n.type,o=n.size,i=n.isUnsigned,r=n.primary,l=n.autoIncrement,c=n.nullable,u=n.unique,m=n.default,d=n.onUpdate,E=n.comment,h=n.hasForeignKey,C=n.foreignMapTables?.[0]?.table,T=n.foreignMapTables?.[0]?.column;return t[a]=`${s}`,o&&(t[a]+=`|size:${o}`),i&&(t[a]+="|unsigned"),r&&(t[a]+="|primaryKey"),l&&(t[a]+="|autoIncrement"),c&&(t[a]+="|nullable"),u&&(t[a]+="|unique"),m&&(t[a]+=`|default:${m}`),d&&(t[a]+=`|onUpdate:${d}`),E&&(t[a]+=`|comment:${E}`),h&&(t[a]+=`|foreignKey:${C}:${T}`),t},{})}async getRelations(e){await this.db.raw(`SHOW CREATE TABLE ${e}`);return{}}async generateSchema(){const e=await this.getTablesOfDatabase(),t={};for(const a of e){let e=this.helperUtility.modelName(a),n=await this.getRelations(a);t[e]={table:a,alias:e,columns:this.getColumnString(await this.getCurrentColumns(a)),modelName:e,seed:[],hasRelations:n,indexes:[]}}return t}}module.exports=SyncTable;
@@ -1 +1 @@
1
- const path=require("path");class HookService{constructor(e,t,o=null){this.db=e,this.utils=t,this.controllerWrapper=o,this.controllerWrapper.hookService=this,this.appRoot=o&&o.resolverPath?o.resolverPath:process.cwd()}loadModelClass(e){try{const t=path.join(this.appRoot,"models",`${e}.model.js`);delete require.cache[require.resolve(t)];return require(t)}catch(e){return void console.log("loadModelClass error",{err:e})}}getModelInstance(e){let t="string"==typeof e?e:e.modelName;const o=this.loadModelClass(t);if(o)return"function"==typeof o?new o:o}resolveModelHook(e,t,o){const r=e.modelName,s=this.loadModelClass(r);if(!s)return;const l=this.getModelInstance(e);let i;if("validate"===t)i="validate";else if("on"===t)i=`on${o.charAt(0).toUpperCase()+o.slice(1)}`;else if("before"===t)i=`before${o.charAt(0).toUpperCase()+o.slice(1)}`;else if("after"===t)i=`after${o.charAt(0).toUpperCase()+o.slice(1)}`;else{if("custom"!==t)return;i=`on${o.charAt(0).toUpperCase()+o.slice(1)}Action`}return"function"==typeof l[i]?l[i].bind(l):"function"==typeof s[i]?s[i].bind(s):void 0}async executeValidatorHook({model:e,action:t,request:o,ctx:r,controller:s}){const l=this.resolveModelHook(e,"validate",t);if(l)return await l({model:e,action:t,request:o,context:r,db:this.db,utils:this.utils,controller:s})}async executeBeforeHook({model:e,action:t,request:o,ctx:r,controller:s}){const l=this.resolveModelHook(e,"before",t);if(l)return await l({model:e,action:t,request:o,context:r,db:this.db,utils:this.utils,controller:s})}async executeAfterHook({model:e,action:t,data:o,request:r,ctx:s,controller:l}){const i=this.resolveModelHook(e,"after",t);return i?await i({model:e,action:t,data:o,request:r,context:s,db:this.db,utils:this.utils,controller:l}):o}async executeCustomAction({model:e,action:t,request:o,ctx:r,controller:s}){const l=this.resolveModelHook(e,"custom",t);if(l)return await l({model:e,action:t,request:o,context:r,db:this.db,utils:this.utils,controller:s});throw new Error(`No custom action hook found for ${e.modelName}.${t}`)}async executeHasSoftDeleteHook(e){const t=this.getModelInstance(e);if(!t)return!1;return t.hasOwnProperty("hasSoftDelete")&&!0===t.hasSoftDelete}}module.exports=HookService;
1
+ const path=require("path"),logger=require("../../Logger");class HookService{constructor(e,t,o=null){this.db=e,this.utils=t,this.controllerWrapper=o,this.controllerWrapper.hookService=this,this.appRoot=o&&o.resolverPath?o.resolverPath:process.cwd()}loadModelClass(e){try{const t=path.join(this.appRoot,"models",`${e}.model.js`);delete require.cache[require.resolve(t)];return require(t)}catch(e){return void logger.debug("loadModelClass error",{err:e})}}getModelInstance(e){let t="string"==typeof e?e:e.modelName;const o=this.loadModelClass(t);if(o)return"function"==typeof o?new o:o}resolveModelHook(e,t,o){const r=e.modelName,s=this.loadModelClass(r);if(!s)return;const l=this.getModelInstance(e);let i;if("validate"===t)i="validate";else if("on"===t)i=`on${o.charAt(0).toUpperCase()+o.slice(1)}`;else if("before"===t)i=`before${o.charAt(0).toUpperCase()+o.slice(1)}`;else if("after"===t)i=`after${o.charAt(0).toUpperCase()+o.slice(1)}`;else{if("custom"!==t)return;i=`on${o.charAt(0).toUpperCase()+o.slice(1)}Action`}return"function"==typeof l[i]?l[i].bind(l):"function"==typeof s[i]?s[i].bind(s):void 0}async executeValidatorHook({model:e,action:t,request:o,ctx:r,controller:s}){const l=this.resolveModelHook(e,"validate",t);if(l)return await l({model:e,action:t,request:o,context:r,db:this.db,utils:this.utils,controller:s})}async executeBeforeHook({model:e,action:t,request:o,ctx:r,controller:s}){const l=this.resolveModelHook(e,"before",t);if(l)return await l({model:e,action:t,request:o,context:r,db:this.db,utils:this.utils,controller:s})}async executeAfterHook({model:e,action:t,data:o,request:r,ctx:s,controller:l}){const i=this.resolveModelHook(e,"after",t);return i?await i({model:e,action:t,data:o,request:r,context:s,db:this.db,utils:this.utils,controller:l}):o}async executeCustomAction({model:e,action:t,request:o,ctx:r,controller:s}){const l=this.resolveModelHook(e,"custom",t);if(l)return await l({model:e,action:t,request:o,context:r,db:this.db,utils:this.utils,controller:s});throw new Error(`No custom action hook found for ${e.modelName}.${t}`)}async executeHasSoftDeleteHook(e){const t=this.getModelInstance(e);if(!t)return!1;return t.hasOwnProperty("hasSoftDelete")&&!0===t.hasSoftDelete}}module.exports=HookService;
@@ -1 +1 @@
1
- const HelperUtility=require("./HelperUtility");class QueryBuilder{constructor(e,t,r=null){this.db=e,this.utils=t,this.controllerWrapper=r,this.helperUtility=new HelperUtility}getHookService(){return this.controllerWrapper.hookService}getQueryBuilder(e,t=null){let r=this.db(e.table);return t&&(r=t),r._getMyModel=()=>e,r}parseValue(e){return this.helperUtility.parseValue(e)}parseWhereValue(e){return this.helperUtility.parseWhereValue(e)}parseWhereColumn(e){return this.helperUtility.parseWhereColumn(e)}_applyOrWhereCondition(e,t,r,i){if(null!==i)if(Array.isArray(i))e.orWhereIn(t,i);else switch(r){case"between":e.orWhereBetween(t,i);break;case"notBetween":e.orWhereNotBetween(t,i);break;case"in":e.orWhereIn(t,i);break;case"notIn":e.orWhereNotIn(t,i);break;case"like":e.orWhere(t,"like",i);break;default:e.orWhere(t,r,i)}else e.orWhereNull(t)}_applyAndWhereCondition(e,t,r,i){if(null!==i)if(Array.isArray(i))e.whereIn(t,i);else switch(r){case"between":e.whereBetween(t,i);break;case"notBetween":e.whereNotBetween(t,i);break;case"in":e.whereIn(t,i);break;case"notIn":e.whereNotIn(t,i);break;case"like":e.where(t,"like",i);break;default:e.where(t,r,i)}else e.whereNull(t)}buildWithTree(e,t=null){return this.helperUtility.dotWalkTree(e,{resolver:({current:e,part:r,source:i})=>t?t({current:e,part:r,source:i}):{}})}async fetchRelatedRows(e,t){if(e.through){let r=await this.db(e.through).whereIn(e.throughLocalKey,t);return await this.db(e.table).whereIn(e.foreignKey,r.map(t=>t[e.throughForeignKey]))}return this.db(e.table).whereIn(e.foreignKey,t)}async fetchAndAttachRelated(e){const{parentRows:t,relName:r,model:i,withTree:o,relation:l}=e;if(!l){const e=this.getHookService().getModelInstance(i),l=`get${r.charAt(0).toUpperCase()+r.slice(1)}Relation`;if("function"==typeof e[l]){const a={rows:t,relName:r,model:i,withTree:o,controller:this.controllerWrapper,relation:i.hasRelations[r],qb:this,db:this.db};await e[l](a)}return t}let a=t.map(e=>e[l.localKey]);const s="one"===l?.type;let n=[];n=await this.fetchRelatedRows(l,a);let h=new Map;for(const e of n){let t=e[l.foreignKey];h.has(t)||h.set(t,[]),h.get(t).push(e)}for(const e of t){let t=e[l.localKey];s&&1==h.get(t)?.length?e[r]=h.get(t)[0]:e[r]=h.get(t)||[]}let c=Object.keys(o);for(const e of c){let i=o[e],l=t.filter(e=>s?e[r]:e[r].length>0).map(e=>e[r]).reduce((e,t)=>e.concat(t),[]),a=this.utils.getModel(this.controllerWrapper,r);await this.fetchAndAttachRelated({parentRows:l,relName:e,model:a,withTree:i,relation:a.hasRelations[e]})}return t}filterWhere(e,t=""){return t?Object.keys(e).filter(e=>e.includes(t)).reduce((r,i)=>(r[i.replace(t,"")]=e[i],r),{}):Object.keys(e).filter(e=>!e.includes(".")).reduce((t,r)=>(t[r]=e[r],t),{})}async getQuery(e,t){try{const{where:r={},with:i,withWhere:o,select:l,orderBy:a={column:"id",direction:"asc"},limit:s=10,offset:n=0,page:h,groupBy:c,having:p,distinct:y,join:u,leftJoin:f,rightJoin:d,innerJoin:g,count:w=!1}=t;let b=this.getQueryBuilder(e);l&&(Array.isArray(l)||"string"==typeof l)?b.select(l):b.select("*"),y&&(Array.isArray(y)||"string"==typeof y?b.distinct(y):b.distinct()),u&&this._applyJoins(b,u,"join"),f&&this._applyJoins(b,f,"leftJoin"),d&&this._applyJoins(b,d,"rightJoin"),g&&this._applyJoins(b,g,"innerJoin"),this._applyWhereClause(b,r,i),c&&(Array.isArray(c),b.groupBy(c)),p&&this._applyHavingClause(b,p),a&&this._applyOrderBy(b,a);let W=!1,A=s,k=n,m=1,_=0;h&&s>0&&(m=Math.max(1,parseInt(h)),k=(m-1)*s),s>0&&(b.limit(A),k>0&&b.offset(k));const j=await b;if(i&&i.length>0){const t=this.buildWithTree(i);for(const r of Object.keys(t))await this.fetchAndAttachRelated({parentRows:j,relName:r,model:e,withTree:t[r],relation:e.hasRelations[r]})}let B=null;if(s>0)try{let t=this.getQueryBuilder(e);r&&Object.keys(r).length>0&&this._applyWhereClause(t,r),u&&this._applyJoins(t,u,"join"),f&&this._applyJoins(t,f,"leftJoin"),d&&this._applyJoins(t,d,"rightJoin"),g&&this._applyJoins(t,g,"innerJoin");B=(await t.count("* as cnt").first()).cnt}catch(e){console.warn("Failed to get total count:",e.message),B=j.length}s>0&&null!==B&&(_=Math.ceil(B/s),W=m<_);return{data:j,totalCount:B,...s>0?{pagination:{page:m,limit:A,offset:k,totalPages:_,hasNext:W,hasPrev:m>1,nextPage:W?m+1:null,prevPage:m>1?m-1:null}}:{}}}catch(e){throw console.error("QueryService.getQuery error:",e),new Error(`Failed to execute query: ${e.message}`)}}_applyWhereWithArray(e,t,r){for(const i of t)this._applyWhereClause(e,i,r)}_applyWhereClause(e,t,r=[]){if(Array.isArray(t))this._applyWhereWithArray(e,t,r);else{if(t&&Object.keys(t).length>0){let r=this.helperUtility.getDotWalkQuery(t);console.log({filteredWhere:r}),r=this.helperUtility.objectFilter(r,(e,t)=>"object"!=typeof t||null===t);for(const[t,i]of Object.entries(r)){const{joinType:r="AND",column:o}=this.parseWhereColumn(t),{operator:l,value:a}=this.parseWhereValue(i);"AND"===r?this._applyAndWhereCondition(e,o,l,a):this._applyOrWhereCondition(e,o,l,a)}}r&&r.length>0&&this._applyNestedWhere(e,t,r)}}_applyNestedWhere(e,t,r){let i=this,o=e._getMyModel();if(r&&r.length>0)for(const l of r){let a=this.helperUtility.getDotWalkQuery(t,l);if(a&&Object.keys(a).length>0){let t=this.utils.getModel(this.controllerWrapper,l),s=o.hasRelations[l],n=r.map(e=>this.helperUtility.pluckDotWalkKey(e,1));e.whereExists(function(){let e=i.getQueryBuilder(t,this.select("*").from(t.table));e.whereRaw(`${s.foreignKey} = ${o.table}.${s.localKey}`),i._applyWhereClause(e,a,n)})}}}_applyJoins(e,t,r){const i=Array.isArray(t)?t:[t];for(const t of i)"string"==typeof t?e[r](t):"object"==typeof t&&(t.table&&t.on?e[r](t.table,t.on):t.table&&t.first&&t.operator&&t.second&&e[r](t.table,t.first,t.operator,t.second))}_applyWithWhere(e,t){try{if(Array.isArray(t))for(const r of t)"string"==typeof r?e.withWhere(r):"object"==typeof r&&e.withWhere(r.column,r.operator,r.value);else if("object"==typeof t)for(const[r,i]of Object.entries(t))e.withWhere(r,i)}catch(e){console.warn("Failed to apply withWhere:",e.message)}}_applyHavingClause(e,t){for(const[r,i]of Object.entries(t))"object"==typeof i&&i.operator?e.having(r,i.operator,i.value):e.having(r,i)}_applyOrderBy(e,t){if(Array.isArray(t))for(const r of t)"string"==typeof r?e.orderBy(r):"object"==typeof r&&e.orderBy(r.column,r.direction||"asc");else"string"==typeof t?e.orderBy(t):"object"==typeof t&&e.orderBy(t.column,t.direction||"asc")}}module.exports=QueryBuilder;
1
+ const HelperUtility=require("./HelperUtility"),logger=require("../../Logger");class QueryBuilder{constructor(e,t,r=null){this.db=e,this.utils=t,this.controllerWrapper=r,this.helperUtility=new HelperUtility}getHookService(){return this.controllerWrapper.hookService}getQueryBuilder(e,t=null){let r=this.db(e.table);return t&&(r=t),r._getMyModel=()=>e,r}parseValue(e){return this.helperUtility.parseValue(e)}parseWhereValue(e){return this.helperUtility.parseWhereValue(e)}parseWhereColumn(e){return this.helperUtility.parseWhereColumn(e)}_applyOrWhereCondition(e,t,r,i){if(null!==i)if(Array.isArray(i))e.orWhereIn(t,i);else switch(r){case"between":e.orWhereBetween(t,i);break;case"notBetween":e.orWhereNotBetween(t,i);break;case"in":e.orWhereIn(t,i);break;case"notIn":e.orWhereNotIn(t,i);break;case"like":e.orWhere(t,"like",i);break;default:e.orWhere(t,r,i)}else e.orWhereNull(t)}_applyAndWhereCondition(e,t,r,i){if(null!==i)if(Array.isArray(i))e.whereIn(t,i);else switch(r){case"between":e.whereBetween(t,i);break;case"notBetween":e.whereNotBetween(t,i);break;case"in":e.whereIn(t,i);break;case"notIn":e.whereNotIn(t,i);break;case"like":e.where(t,"like",i);break;default:e.where(t,r,i)}else e.whereNull(t)}buildWithTree(e,t=null){return this.helperUtility.dotWalkTree(e,{resolver:({current:e,part:r,source:i})=>t?t({current:e,part:r,source:i}):{}})}_getWithWhereForRelation(e,t){if(!e||"object"!=typeof e)return{};const r=`${t}.`,i={};for(const[t,o]of Object.entries(e))if(t.startsWith(r)){i[t.slice(r.length)]=o}return i}_applyWithWhereConditions(e,t){for(const[r,i]of Object.entries(t)){const{joinType:t="AND",column:o}=this.parseWhereColumn(r),{operator:l,value:s}=this.parseWhereValue(i);"AND"===t?this._applyAndWhereCondition(e,o,l,s):this._applyOrWhereCondition(e,o,l,s)}}async fetchRelatedRows(e,t,r={}){if(e.through){let i=await this.db(e.through).whereIn(e.throughLocalKey,t),o=this.db(e.table).whereIn(e.foreignKey,i.map(t=>t[e.throughForeignKey]));return Object.keys(r).length>0&&this._applyWithWhereConditions(o,r),o}{let i=this.db(e.table).whereIn(e.foreignKey,t);return Object.keys(r).length>0&&this._applyWithWhereConditions(i,r),i}}async fetchAndAttachRelated(e){const{parentRows:t,relName:r,model:i,withTree:o,relation:l,withWhere:s={}}=e;if(!l){const e=this.getHookService().getModelInstance(i),l=`get${r.charAt(0).toUpperCase()+r.slice(1)}Relation`;if("function"==typeof e[l]){const n={rows:t,relName:r,model:i,withTree:o,controller:this.controllerWrapper,relation:i.hasRelations[r],qb:this,db:this.db,withWhere:s};await e[l](n)}return t}let n=t.map(e=>e[l.localKey]);const a="one"===l?.type;let h=[];const p=this._getWithWhereForRelation(s,r);h=await this.fetchRelatedRows(l,n,p);let c=new Map;for(const e of h){let t=e[l.foreignKey];c.has(t)||c.set(t,[]),c.get(t).push(e)}for(const e of t){let t=e[l.localKey];a&&1==c.get(t)?.length?e[r]=c.get(t)[0]:e[r]=c.get(t)||[]}let y=Object.keys(o);for(const e of y){let i=o[e],l=t.filter(e=>a?e[r]:e[r].length>0).map(e=>e[r]).reduce((e,t)=>e.concat(t),[]),n=this.utils.getModel(this.controllerWrapper,r);await this.fetchAndAttachRelated({parentRows:l,relName:e,model:n,withTree:i,relation:n.hasRelations[e],withWhere:s})}return t}filterWhere(e,t=""){return t?Object.keys(e).filter(e=>e.includes(t)).reduce((r,i)=>(r[i.replace(t,"")]=e[i],r),{}):Object.keys(e).filter(e=>!e.includes(".")).reduce((t,r)=>(t[r]=e[r],t),{})}async getQuery(e,t){try{const{where:r={},with:i,withWhere:o,select:l,orderBy:s={column:"id",direction:"asc"},limit:n=10,offset:a=0,page:h,groupBy:p,having:c,distinct:y,join:u,leftJoin:f,rightJoin:g,innerJoin:d,count:W=!1}=t;let w=this.getQueryBuilder(e);l&&(Array.isArray(l)||"string"==typeof l)?w.select(l):w.select("*"),y&&(Array.isArray(y)||"string"==typeof y?w.distinct(y):w.distinct()),u&&this._applyJoins(w,u,"join"),f&&this._applyJoins(w,f,"leftJoin"),g&&this._applyJoins(w,g,"rightJoin"),d&&this._applyJoins(w,d,"innerJoin"),this._applyWhereClause(w,r,i),p&&(Array.isArray(p),w.groupBy(p)),c&&this._applyHavingClause(w,c),s&&this._applyOrderBy(w,s);let b=!1,_=n,A=a,k=1,m=0;h&&n>0&&(k=Math.max(1,parseInt(h)),A=(k-1)*n),n>0&&(w.limit(_),A>0&&w.offset(A));const j=await w;if(i&&i.length>0){const t=this.buildWithTree(i);for(const r of Object.keys(t))await this.fetchAndAttachRelated({parentRows:j,relName:r,model:e,withTree:t[r],relation:e.hasRelations[r],withWhere:o||{}})}let C=null;if(n>0)try{let t=this.getQueryBuilder(e);r&&Object.keys(r).length>0&&this._applyWhereClause(t,r),u&&this._applyJoins(t,u,"join"),f&&this._applyJoins(t,f,"leftJoin"),g&&this._applyJoins(t,g,"rightJoin"),d&&this._applyJoins(t,d,"innerJoin");C=(await t.count("* as cnt").first()).cnt}catch(e){logger.warn("Failed to get total count:",e.message),C=j.length}n>0&&null!==C&&(m=Math.ceil(C/n),b=k<m);return{data:j,totalCount:C,...n>0?{pagination:{page:k,limit:_,offset:A,totalPages:m,hasNext:b,hasPrev:k>1,nextPage:b?k+1:null,prevPage:k>1?k-1:null}}:{}}}catch(e){throw logger.error("QueryService.getQuery error:",e),new Error(`Failed to execute query: ${e.message}`)}}_applyWhereWithArray(e,t,r){for(const i of t)this._applyWhereClause(e,i,r)}_applyWhereClause(e,t,r=[]){if(Array.isArray(t))this._applyWhereWithArray(e,t,r);else{if(t&&Object.keys(t).length>0){let r=this.helperUtility.getDotWalkQuery(t);logger.debug({filteredWhere:r}),r=this.helperUtility.objectFilter(r,(e,t)=>"object"!=typeof t||null===t);for(const[t,i]of Object.entries(r)){const{joinType:r="AND",column:o}=this.parseWhereColumn(t),{operator:l,value:s}=this.parseWhereValue(i);"AND"===r?this._applyAndWhereCondition(e,o,l,s):this._applyOrWhereCondition(e,o,l,s)}}r&&r.length>0&&this._applyNestedWhere(e,t,r)}}_applyNestedWhere(e,t,r){let i=this,o=e._getMyModel();if(r&&r.length>0)for(const l of r){let s=this.helperUtility.getDotWalkQuery(t,l);if(s&&Object.keys(s).length>0){let t=this.utils.getModel(this.controllerWrapper,l),n=o.hasRelations[l],a=r.map(e=>this.helperUtility.pluckDotWalkKey(e,1));e.whereExists(function(){let e=i.getQueryBuilder(t,this.select("*").from(t.table));e.whereRaw(`${n.foreignKey} = ${o.table}.${n.localKey}`),i._applyWhereClause(e,s,a)})}}}_applyJoins(e,t,r){const i=Array.isArray(t)?t:[t];for(const t of i)"string"==typeof t?e[r](t):"object"==typeof t&&(t.table&&t.on?e[r](t.table,t.on):t.table&&t.first&&t.operator&&t.second&&e[r](t.table,t.first,t.operator,t.second))}_applyWithWhere(e,t){try{if(Array.isArray(t))for(const r of t)"string"==typeof r?e.withWhere(r):"object"==typeof r&&e.withWhere(r.column,r.operator,r.value);else if("object"==typeof t)for(const[r,i]of Object.entries(t))e.withWhere(r,i)}catch(e){logger.warn("Failed to apply withWhere:",e.message)}}_applyHavingClause(e,t){for(const[r,i]of Object.entries(t))"object"==typeof i&&i.operator?e.having(r,i.operator,i.value):e.having(r,i)}_applyOrderBy(e,t){if(Array.isArray(t))for(const r of t)"string"==typeof r?e.orderBy(r):"object"==typeof r&&e.orderBy(r.column,r.direction||"asc");else"string"==typeof t?e.orderBy(t):"object"==typeof t&&e.orderBy(t.column,t.direction||"asc")}}module.exports=QueryBuilder;
@@ -1 +1 @@
1
- const CurdTable=require("./CurdTable"),HelperUtility=require("./HelperUtility"),{getSchemaType:getSchemaType}=require("./DataTypeMap");class SyncTable{constructor(e,t,n=null){this.db=e,this.utils=t,this.controllerWrapper=n,this.curd=new CurdTable(e,t,n),this.helperUtility=new HelperUtility}_getClientName(){return"pg"}async executeSql(e,t=[]){return this.db.raw(e,t)}async existsTable(e){return this.db.schema.hasTable(e)}async getTablesReferencedByTable(e,t="public"){return(await this.executeSql("SELECT\n tc.table_name AS \"TABLE_NAME\", -- The table that contains the FK (e.g., 'Posts')\n kcu.column_name AS \"REFERENCING_COLUMN_NAME\", -- The FK column (e.g., 'user_id')\n kcu2.column_name AS \"REFERENCED_COLUMN_NAME\" -- The PK/Unique column (e.g., 'id')\n FROM\n information_schema.referential_constraints AS rc\n -- Join to get the table and column being referenced (the PK/Unique key on the target table)\n JOIN\n information_schema.key_column_usage AS kcu2\n ON rc.unique_constraint_name = kcu2.constraint_name\n AND rc.unique_constraint_schema = kcu2.constraint_schema\n -- Join to get the table constraint information for the foreign key\n JOIN\n information_schema.table_constraints AS tc\n ON rc.constraint_name = tc.constraint_name\n AND rc.constraint_schema = tc.table_schema\n -- Join to get the column in the referencing table (the FK column)\n JOIN\n information_schema.key_column_usage AS kcu\n ON rc.constraint_name = kcu.constraint_name\n AND rc.constraint_schema = kcu.table_schema\n -- Link the FK column to its corresponding PK/Unique column\n AND kcu.position_in_unique_constraint = kcu2.ordinal_position\n WHERE\n kcu2.table_name = ?\n AND kcu2.table_schema = ?\n AND tc.constraint_type = 'FOREIGN KEY'\n ORDER BY\n tc.table_name, kcu.column_name",[e,t])).rows||[]}async getTablesWithColumn(e){return(await this.executeSql("\n SELECT table_name\n FROM information_schema.columns\n WHERE column_name = ?\n ",[e])).rows||[]}async getCurrentColumns(e){const t=(await this.executeSql('\n SELECT \n c.column_name AS "COLUMN_NAME",\n c.data_type AS "DATA_TYPE",\n c.udt_name AS "UDT_NAME",\n c.is_nullable AS "IS_NULLABLE",\n c.column_default AS "COLUMN_DEFAULT",\n c.character_maximum_length AS "CHARACTER_MAXIMUM_LENGTH",\n pgd.description AS "COMMENT",\n tc.constraint_type AS "CONSTRAINT_TYPE",\n kcu2.table_name AS "REFERENCED_TABLE_NAME",\n kcu2.column_name AS "REFERENCED_COLUMN_NAME"\n FROM information_schema.columns c\n LEFT JOIN pg_catalog.pg_statio_all_tables as st\n ON c.table_schema = st.schemaname AND c.table_name = st.relname\n LEFT JOIN pg_catalog.pg_description pgd\n ON pgd.objoid = st.relid AND pgd.objsubid = c.ordinal_position\n LEFT JOIN information_schema.key_column_usage kcu\n ON c.table_name = kcu.table_name\n AND c.column_name = kcu.column_name\n AND c.table_schema = kcu.table_schema\n LEFT JOIN information_schema.table_constraints tc\n ON kcu.constraint_name = tc.constraint_name\n AND kcu.table_schema = tc.table_schema\n LEFT JOIN information_schema.referential_constraints rc\n ON tc.constraint_name = rc.constraint_name\n AND tc.table_schema = rc.constraint_schema\n LEFT JOIN information_schema.key_column_usage kcu2\n ON rc.unique_constraint_name = kcu2.constraint_name\n AND rc.unique_constraint_schema = kcu2.constraint_schema\n AND kcu.ordinal_position = kcu2.ordinal_position\n WHERE c.table_schema = ?\n AND c.table_name = ?\n ORDER BY c.ordinal_position\n ',["public",e])).rows||[],n={};for(const e of t)n[e.COLUMN_NAME]=this.utils.formatColumnDef(e.COLUMN_NAME,e);return n}logColumnChanges(e,t,n){console.log(`${t.name} changed`),e.isTypeChanged&&console.log(`Type changed from ${t.type} to ${n.type}`),e.isSizeChanged&&console.log(`Size changed from ${t.size} to ${n.size}`),e.isNullableChanged&&console.log(`Nullable changed from ${t.nullable} to ${n.nullable}`),e.isPrimaryChanged&&console.log(`Primary changed from ${t.primary} to ${n.primary}`),e.isUniqueChanged&&console.log(`Unique changed from ${t.unique} to ${n.unique}`),e.isAutoIncrementChanged&&console.log(`Auto increment changed from ${t.autoIncrement} to ${n.autoIncrement}`),e.isDefaultChanged&&console.log(`Default changed from ${t.default} to ${n.default}`),e.isOnUpdateChanged&&console.log(`On update changed from ${t.onUpdate} to ${n.onUpdate}`),e.isCommentChanged&&console.log(`Comment changed from ${t.comment} to ${n.comment}`),e.isForeignKeyChanged&&console.log(`ForeignKey changed from ${t.hasForeignKey} to ${n.hasForeignKey}`)}hasColumnChanged(e,t){const n={isNullableChanged:e.nullable!==t.nullable,isTypeChanged:e.type!==t.type,isSizeChanged:e.size!==t.size,isUnsignedChanged:e.isUnsigned!==t.isUnsigned,isPrimaryChanged:e.primary!==t.primary,isUniqueChanged:e.unique!==t.unique,isAutoIncrementChanged:e.autoIncrement!==t.autoIncrement,isDefaultChanged:e.default!==t.default,isOnUpdateChanged:e.onUpdate!==t.onUpdate,isCommentChanged:e.comment!==t.comment,isForeignKeyChanged:e.hasForeignKey!==t.hasForeignKey},a=Object.values(n).some(Boolean);return a&&this.logColumnChanges(n,e,t),a}async getAlterations(e){const t={add:[],drop:[],modify:[]},n=await this.getCurrentColumns(e.table);for(const[a,o]of Object.entries(e.columns)){const e=this.utils.formatColumnSchema(a,o),l=n[a];l?(e.oldColDef=l,this.hasColumnChanged(l,e)&&t.modify.push(e)):t.add.push(e)}for(const a of Object.keys(n))e.columns[a]||t.drop.push({name:a});return t}getColumnStr(e,t,n={actionType:"CREATE",tableName:""}){const{actionType:a,tableName:o}=n,l="string"==typeof t?this.utils.formatColumnSchema(e,t):t;let s=`"${e}"`,i=l.type,c=l.size??this.utils.getDefaultTypeSize(i);if(c&&["character varying","varchar","char","character"].includes(i)?s+=` ${i}(${c})`:s+="numeric"===i&&c?` ${i}(${c})`:` ${i}`,l.autoIncrement&&("integer"===i||"int4"===i?s=`"${e}" SERIAL`:"bigint"!==i&&"int8"!==i||(s=`"${e}" BIGSERIAL`)),l.primary&&["CREATE","ADD_COLUMN"].includes(a)&&(s+=" PRIMARY KEY"),!1===l.nullable||void 0===l.nullable?s+=" NOT NULL":!0===l.nullable&&(s+=" NULL"),l.unique&&(s+=" UNIQUE"),void 0!==l.default&&null!==l.default&&!l.primary&&!l.autoIncrement){s+=this.utils.isInternalDefault(l.default)?` DEFAULT ${l.default}`:` DEFAULT '${l.default}'`}if(l.hasForeignKey&&1===l.foreignMapTables?.length&&"CREATE"===a){const{table:e,column:t}=l.foreignMapTables[0];s+=` REFERENCES "${e}"("${t}")`,s+=" ON DELETE RESTRICT ON UPDATE RESTRICT"}return s}async createTable(e){const t=e.table,n=e.columns,a=[],o=[];for(const[e,l]of Object.entries(n)){const n="string"==typeof l?this.utils.formatColumnSchema(e,l):l;if(a.push(this.getColumnStr(e,n,{actionType:"CREATE",tableName:t})),n.hasForeignKey&&1===n.foreignMapTables?.length){const{table:a,column:l}=n.foreignMapTables[0],s=`fk_${t}_${e}_${a}_${l}`;o.push(`CONSTRAINT "${s}" FOREIGN KEY ("${e}") REFERENCES "${a}"("${l}") ON DELETE RESTRICT ON UPDATE RESTRICT`)}}const l=`CREATE TABLE IF NOT EXISTS "${t}" (${a.concat(o).join(", ")})`;await this.executeSql(l);for(const[e,a]of Object.entries(n)){const n="string"==typeof a?this.utils.formatColumnSchema(e,a):a;if(n.comment){const a=`COMMENT ON COLUMN "${t}"."${e}" IS '${this.utils.escapeComment(n.comment)}'`;await this.executeSql(a)}}for(const n of e.indexes)if(n?.columns?.length){const e=`CREATE ${n.unique?"UNIQUE":""} INDEX IF NOT EXISTS "${n.name}" ON "${t}" (${n.columns.map(e=>`"${e}"`).join(", ")})`;await this.executeSql(e)}}async alterTable(e,t){if(!t||"object"!=typeof t)throw new Error("alterations must be an object");const n=[];if(t.add?.length)for(const a of t.add)n.push(`ADD COLUMN ${this.getColumnStr(a.name,a,{actionType:"ADD_COLUMN",tableName:e})}`);if(t.drop?.length)for(const e of t.drop)n.push(`DROP COLUMN "${e.name}"`);if(t.modify?.length)for(const e of t.modify){if(e.type){let t=e.type;e.size&&["character varying","varchar","char","character"].includes(e.type)&&(t+=`(${e.size})`),n.push(`ALTER COLUMN "${e.name}" TYPE ${t}`)}if(!1===e.nullable||void 0===e.nullable?n.push(`ALTER COLUMN "${e.name}" SET NOT NULL`):e.nullable,void 0!==e.default&&null!==e.default){let t=this.utils.isInternalDefault(e.default);"string"!=typeof e.default||t?n.push(`ALTER COLUMN "${e.name}" SET DEFAULT ${e.default}`):n.push(`ALTER COLUMN "${e.name}" SET DEFAULT '${e.default}'`)}else n.push(`ALTER COLUMN "${e.name}" DROP DEFAULT`);e.comment}if(!n.length)return void console.log("No alterations to apply for",e);const a=`ALTER TABLE "${e}" ${n.join(", ")}`;if(await this.executeSql(a),t.modify?.length)for(const n of t.modify)if(n.comment){const t=`COMMENT ON COLUMN "${e}"."${n.name}" IS '${this.utils.escapeComment(n.comment)}'`;await this.executeSql(t)}}async alterColumn(e,t,n){const a="string"==typeof n?this.utils.formatColumnSchema(t,n):n;if(a.type){let n=a.type;a.size&&["character varying","varchar","char","character"].includes(a.type)&&(n+=`(${a.size})`);const o=`ALTER TABLE "${e}" ALTER COLUMN "${t}" TYPE ${n}`;await this.executeSql(o)}if(!1===a.nullable||void 0===a.nullable?await this.executeSql(`ALTER TABLE "${e}" ALTER COLUMN "${t}" SET NOT NULL`):!0===a.nullable&&await this.executeSql(`ALTER TABLE "${e}" ALTER COLUMN "${t}" DROP NOT NULL`),void 0!==a.default&&null!==a.default){this.utils.isInternalDefault(a.default)?await this.executeSql(`ALTER TABLE "${e}" ALTER COLUMN "${t}" SET DEFAULT ${a.default}`):await this.executeSql(`ALTER TABLE "${e}" ALTER COLUMN "${t}" SET DEFAULT '${a.default}'`)}else await this.executeSql(`ALTER TABLE "${e}" ALTER COLUMN "${t}" DROP DEFAULT`);if(a.comment){const n=`COMMENT ON COLUMN "${e}"."${t}" IS '${this.utils.escapeComment(a.comment)}'`;await this.executeSql(n)}}async alterIndex(e,t,n){const a=Array.isArray(n.columns)?n.columns:[n.columns],o=`CREATE ${n.unique?"UNIQUE ":""}INDEX "${t}" ON "${e}" (${a.map(e=>`"${e}"`).join(", ")})`;await this.executeSql(o)}async dropIndex(e,t){const n=`DROP INDEX IF EXISTS "${t}"`;await this.executeSql(n)}async dropTable(e){const t=`DROP TABLE IF EXISTS "${e}" CASCADE`;await this.executeSql(t)}async updateTable(e){console.log(`Update table not implemented for ${e.table}`)}async syncTable(e){if(await this.existsTable(e.table)){const t=await this.getAlterations(e);return void await this.alterTable(e.table,t)}await this.createTable(e)}async syncSeedData(e,t){let n=this.utils.getModel(this.controllerWrapper,t),a=await this.curd.processRequest({action:"count"},n.name,{isCallFromServer:!0});if(console.log("count",a),a>0)console.log("Seed data already synced for",t);else if(e.seed&&Array.isArray(e.seed)){for(const t of e.seed)await this.curd.processRequest({action:"create",data:t},n.name,{isCallFromServer:!0});console.log("Seed data synced for",t)}}async syncDatabase(){if(!this.controllerWrapper?.schema)throw new Error("controllerWrapper.schema not set.");const e=this.controllerWrapper.schema;for(const t of Object.keys(e))await this.syncTable(e[t]),await this.syncSeedData(e[t],t);console.log("Database synced by SyncTable...")}async getTablesOfDatabase(){return((await this.db.raw("\n SELECT table_name\n FROM information_schema.tables\n WHERE table_schema = 'public'\n AND table_type = 'BASE TABLE'\n ")).rows||[]).map(e=>e.table_name)}getColumnString(e){return Object.keys(e).reduce((t,n)=>{const a=e[n];let o=a.type,l=a.size,s=a.isUnsigned,i=a.primary,c=a.autoIncrement,r=a.nullable,u=a.unique,m=a.default,h=a.onUpdate,E=a.comment,d=a.hasForeignKey,g=a.foreignMapTables?.[0]?.table,f=a.foreignMapTables?.[0]?.column,T=getSchemaType(o);return t[n]=`${T}`,l&&(t[n]+=`|size:${l}`),s&&(t[n]+="|unsigned"),i&&(t[n]+="|primaryKey"),c&&(t[n]+="|autoIncrement"),r&&(t[n]+="|nullable"),u&&(t[n]+="|unique"),m&&(t[n]+=`|default:${m}`),h&&(t[n]+=`|onUpdate:${h}`),E&&(t[n]+=`|comment:${E}`),d&&(t[n]+=`|foreignKey:${g}:${f}`),t},{})}async getRelations(e){let t=await this.getCurrentColumns(e);const n=Object.keys(t).filter(e=>t[e].hasForeignKey).reduce((e,n)=>{let a=t[n];if(!a)return e;if(0===a.foreignMapTables?.length)return e;let o=a.foreignMapTables;for(const t of o){let a=t.table,o=t.column;e[this.helperUtility.modelName(a)]={type:"one",table:a,localKey:n,foreignKey:o,through:null,throughLocalKey:null,throughForeignKey:null}}return e},{}),a=(this.helperUtility.modelName(e).toLowerCase(),await this.getTablesReferencedByTable(e)),o=a.map(e=>e.TABLE_NAME);for(const e of o){const t=this.helperUtility.modelName(e),o=a.find(t=>t.TABLE_NAME===e)?.REFERENCED_COLUMN_NAME,l=a.find(t=>t.TABLE_NAME===e)?.COLUMN_NAME;n[t]={type:"many",table:e,localKey:o,foreignKey:l,through:null,throughLocalKey:null,throughForeignKey:null}}return n}async generateSchema(){const e=await this.getTablesOfDatabase(),t={};for(const n of e){let e=this.helperUtility.modelName(n),a=await this.getRelations(n);t[e]={table:n,alias:e,columns:this.getColumnString(await this.getCurrentColumns(n)),modelName:e,seed:[],hasRelations:a,indexes:[]}}return t}}module.exports=SyncTable;
1
+ const CurdTable=require("./CurdTable"),HelperUtility=require("./HelperUtility"),{getSchemaType:getSchemaType}=require("./DataTypeMap"),logger=require("../../Logger");class SyncTable{constructor(e,t,n=null){this.db=e,this.utils=t,this.controllerWrapper=n,this.curd=new CurdTable(e,t,n),this.helperUtility=new HelperUtility}_getClientName(){return"pg"}async executeSql(e,t=[]){return this.db.raw(e,t)}async existsTable(e){return this.db.schema.hasTable(e)}async getTablesReferencedByTable(e,t="public"){return(await this.executeSql("SELECT\n tc.table_name AS \"TABLE_NAME\", -- The table that contains the FK (e.g., 'Posts')\n kcu.column_name AS \"REFERENCING_COLUMN_NAME\", -- The FK column (e.g., 'user_id')\n kcu2.column_name AS \"REFERENCED_COLUMN_NAME\" -- The PK/Unique column (e.g., 'id')\n FROM\n information_schema.referential_constraints AS rc\n -- Join to get the table and column being referenced (the PK/Unique key on the target table)\n JOIN\n information_schema.key_column_usage AS kcu2\n ON rc.unique_constraint_name = kcu2.constraint_name\n AND rc.unique_constraint_schema = kcu2.constraint_schema\n -- Join to get the table constraint information for the foreign key\n JOIN\n information_schema.table_constraints AS tc\n ON rc.constraint_name = tc.constraint_name\n AND rc.constraint_schema = tc.table_schema\n -- Join to get the column in the referencing table (the FK column)\n JOIN\n information_schema.key_column_usage AS kcu\n ON rc.constraint_name = kcu.constraint_name\n AND rc.constraint_schema = kcu.table_schema\n -- Link the FK column to its corresponding PK/Unique column\n AND kcu.position_in_unique_constraint = kcu2.ordinal_position\n WHERE\n kcu2.table_name = ?\n AND kcu2.table_schema = ?\n AND tc.constraint_type = 'FOREIGN KEY'\n ORDER BY\n tc.table_name, kcu.column_name",[e,t])).rows||[]}async getTablesWithColumn(e){return(await this.executeSql("\n SELECT table_name\n FROM information_schema.columns\n WHERE column_name = ?\n ",[e])).rows||[]}async getCurrentColumns(e){const t=(await this.executeSql('\n SELECT \n c.column_name AS "COLUMN_NAME",\n c.data_type AS "DATA_TYPE",\n c.udt_name AS "UDT_NAME",\n c.is_nullable AS "IS_NULLABLE",\n c.column_default AS "COLUMN_DEFAULT",\n c.character_maximum_length AS "CHARACTER_MAXIMUM_LENGTH",\n pgd.description AS "COMMENT",\n tc.constraint_type AS "CONSTRAINT_TYPE",\n kcu2.table_name AS "REFERENCED_TABLE_NAME",\n kcu2.column_name AS "REFERENCED_COLUMN_NAME"\n FROM information_schema.columns c\n LEFT JOIN pg_catalog.pg_statio_all_tables as st\n ON c.table_schema = st.schemaname AND c.table_name = st.relname\n LEFT JOIN pg_catalog.pg_description pgd\n ON pgd.objoid = st.relid AND pgd.objsubid = c.ordinal_position\n LEFT JOIN information_schema.key_column_usage kcu\n ON c.table_name = kcu.table_name\n AND c.column_name = kcu.column_name\n AND c.table_schema = kcu.table_schema\n LEFT JOIN information_schema.table_constraints tc\n ON kcu.constraint_name = tc.constraint_name\n AND kcu.table_schema = tc.table_schema\n LEFT JOIN information_schema.referential_constraints rc\n ON tc.constraint_name = rc.constraint_name\n AND tc.table_schema = rc.constraint_schema\n LEFT JOIN information_schema.key_column_usage kcu2\n ON rc.unique_constraint_name = kcu2.constraint_name\n AND rc.unique_constraint_schema = kcu2.constraint_schema\n AND kcu.ordinal_position = kcu2.ordinal_position\n WHERE c.table_schema = ?\n AND c.table_name = ?\n ORDER BY c.ordinal_position\n ',["public",e])).rows||[],n={};for(const e of t)n[e.COLUMN_NAME]=this.utils.formatColumnDef(e.COLUMN_NAME,e);return n}logColumnChanges(e,t,n){logger.debug(`${t.name} changed`),e.isTypeChanged&&logger.debug(`Type changed from ${t.type} to ${n.type}`),e.isSizeChanged&&logger.debug(`Size changed from ${t.size} to ${n.size}`),e.isNullableChanged&&logger.debug(`Nullable changed from ${t.nullable} to ${n.nullable}`),e.isPrimaryChanged&&logger.debug(`Primary changed from ${t.primary} to ${n.primary}`),e.isUniqueChanged&&logger.debug(`Unique changed from ${t.unique} to ${n.unique}`),e.isAutoIncrementChanged&&logger.debug(`Auto increment changed from ${t.autoIncrement} to ${n.autoIncrement}`),e.isDefaultChanged&&logger.debug(`Default changed from ${t.default} to ${n.default}`),e.isOnUpdateChanged&&logger.debug(`On update changed from ${t.onUpdate} to ${n.onUpdate}`),e.isCommentChanged&&logger.debug(`Comment changed from ${t.comment} to ${n.comment}`),e.isForeignKeyChanged&&logger.debug(`ForeignKey changed from ${t.hasForeignKey} to ${n.hasForeignKey}`)}hasColumnChanged(e,t){const n={isNullableChanged:e.nullable!==t.nullable,isTypeChanged:e.type!==t.type,isSizeChanged:e.size!==t.size,isUnsignedChanged:e.isUnsigned!==t.isUnsigned,isPrimaryChanged:e.primary!==t.primary,isUniqueChanged:e.unique!==t.unique,isAutoIncrementChanged:e.autoIncrement!==t.autoIncrement,isDefaultChanged:e.default!==t.default,isOnUpdateChanged:e.onUpdate!==t.onUpdate,isCommentChanged:e.comment!==t.comment,isForeignKeyChanged:e.hasForeignKey!==t.hasForeignKey},a=Object.values(n).some(Boolean);return a&&this.logColumnChanges(n,e,t),a}async getAlterations(e){const t={add:[],drop:[],modify:[]},n=await this.getCurrentColumns(e.table);for(const[a,o]of Object.entries(e.columns)){const e=this.utils.formatColumnSchema(a,o),i=n[a];i?(e.oldColDef=i,this.hasColumnChanged(i,e)&&t.modify.push(e)):t.add.push(e)}for(const a of Object.keys(n))e.columns[a]||t.drop.push({name:a});return t}getColumnStr(e,t,n={actionType:"CREATE",tableName:""}){const{actionType:a,tableName:o}=n,i="string"==typeof t?this.utils.formatColumnSchema(e,t):t;let l=`"${e}"`,s=i.type,c=i.size??this.utils.getDefaultTypeSize(s);if(c&&["character varying","varchar","char","character"].includes(s)?l+=` ${s}(${c})`:l+="numeric"===s&&c?` ${s}(${c})`:` ${s}`,i.autoIncrement&&("integer"===s||"int4"===s?l=`"${e}" SERIAL`:"bigint"!==s&&"int8"!==s||(l=`"${e}" BIGSERIAL`)),i.primary&&["CREATE","ADD_COLUMN"].includes(a)&&(l+=" PRIMARY KEY"),!1===i.nullable||void 0===i.nullable?l+=" NOT NULL":!0===i.nullable&&(l+=" NULL"),i.unique&&(l+=" UNIQUE"),void 0!==i.default&&null!==i.default&&!i.primary&&!i.autoIncrement){l+=this.utils.isInternalDefault(i.default)?` DEFAULT ${i.default}`:` DEFAULT '${i.default}'`}if(i.hasForeignKey&&1===i.foreignMapTables?.length&&"CREATE"===a){const{table:e,column:t}=i.foreignMapTables[0];l+=` REFERENCES "${e}"("${t}")`,l+=" ON DELETE RESTRICT ON UPDATE RESTRICT"}return l}async createTable(e){const t=e.table,n=e.columns,a=[],o=[];for(const[e,i]of Object.entries(n)){const n="string"==typeof i?this.utils.formatColumnSchema(e,i):i;if(a.push(this.getColumnStr(e,n,{actionType:"CREATE",tableName:t})),n.hasForeignKey&&1===n.foreignMapTables?.length){const{table:a,column:i}=n.foreignMapTables[0],l=`fk_${t}_${e}_${a}_${i}`;o.push(`CONSTRAINT "${l}" FOREIGN KEY ("${e}") REFERENCES "${a}"("${i}") ON DELETE RESTRICT ON UPDATE RESTRICT`)}}const i=`CREATE TABLE IF NOT EXISTS "${t}" (${a.concat(o).join(", ")})`;await this.executeSql(i);for(const[e,a]of Object.entries(n)){const n="string"==typeof a?this.utils.formatColumnSchema(e,a):a;if(n.comment){const a=`COMMENT ON COLUMN "${t}"."${e}" IS '${this.utils.escapeComment(n.comment)}'`;await this.executeSql(a)}}for(const n of e.indexes)if(n?.columns?.length){const e=`CREATE ${n.unique?"UNIQUE":""} INDEX IF NOT EXISTS "${n.name}" ON "${t}" (${n.columns.map(e=>`"${e}"`).join(", ")})`;await this.executeSql(e)}}async alterTable(e,t){if(!t||"object"!=typeof t)throw new Error("alterations must be an object");const n=[];if(t.add?.length)for(const a of t.add)n.push(`ADD COLUMN ${this.getColumnStr(a.name,a,{actionType:"ADD_COLUMN",tableName:e})}`);if(t.drop?.length)for(const e of t.drop)n.push(`DROP COLUMN "${e.name}"`);if(t.modify?.length)for(const e of t.modify){if(e.type){let t=e.type;e.size&&["character varying","varchar","char","character"].includes(e.type)&&(t+=`(${e.size})`),n.push(`ALTER COLUMN "${e.name}" TYPE ${t}`)}if(!1===e.nullable||void 0===e.nullable?n.push(`ALTER COLUMN "${e.name}" SET NOT NULL`):e.nullable,void 0!==e.default&&null!==e.default){let t=this.utils.isInternalDefault(e.default);"string"!=typeof e.default||t?n.push(`ALTER COLUMN "${e.name}" SET DEFAULT ${e.default}`):n.push(`ALTER COLUMN "${e.name}" SET DEFAULT '${e.default}'`)}else n.push(`ALTER COLUMN "${e.name}" DROP DEFAULT`);e.comment}if(!n.length)return void logger.info("No alterations to apply for",e);const a=`ALTER TABLE "${e}" ${n.join(", ")}`;if(await this.executeSql(a),t.modify?.length)for(const n of t.modify)if(n.comment){const t=`COMMENT ON COLUMN "${e}"."${n.name}" IS '${this.utils.escapeComment(n.comment)}'`;await this.executeSql(t)}}async alterColumn(e,t,n){const a="string"==typeof n?this.utils.formatColumnSchema(t,n):n;if(a.type){let n=a.type;a.size&&["character varying","varchar","char","character"].includes(a.type)&&(n+=`(${a.size})`);const o=`ALTER TABLE "${e}" ALTER COLUMN "${t}" TYPE ${n}`;await this.executeSql(o)}if(!1===a.nullable||void 0===a.nullable?await this.executeSql(`ALTER TABLE "${e}" ALTER COLUMN "${t}" SET NOT NULL`):!0===a.nullable&&await this.executeSql(`ALTER TABLE "${e}" ALTER COLUMN "${t}" DROP NOT NULL`),void 0!==a.default&&null!==a.default){this.utils.isInternalDefault(a.default)?await this.executeSql(`ALTER TABLE "${e}" ALTER COLUMN "${t}" SET DEFAULT ${a.default}`):await this.executeSql(`ALTER TABLE "${e}" ALTER COLUMN "${t}" SET DEFAULT '${a.default}'`)}else await this.executeSql(`ALTER TABLE "${e}" ALTER COLUMN "${t}" DROP DEFAULT`);if(a.comment){const n=`COMMENT ON COLUMN "${e}"."${t}" IS '${this.utils.escapeComment(a.comment)}'`;await this.executeSql(n)}}async alterIndex(e,t,n){const a=Array.isArray(n.columns)?n.columns:[n.columns],o=`CREATE ${n.unique?"UNIQUE ":""}INDEX "${t}" ON "${e}" (${a.map(e=>`"${e}"`).join(", ")})`;await this.executeSql(o)}async dropIndex(e,t){const n=`DROP INDEX IF EXISTS "${t}"`;await this.executeSql(n)}async dropTable(e){const t=`DROP TABLE IF EXISTS "${e}" CASCADE`;await this.executeSql(t)}async updateTable(e){logger.warn(`Update table not implemented for ${e.table}`)}async syncTable(e){if(await this.existsTable(e.table)){const t=await this.getAlterations(e);return void await this.alterTable(e.table,t)}await this.createTable(e)}async syncSeedData(e,t){let n=this.utils.getModel(this.controllerWrapper,t),a=await this.curd.processRequest({action:"count"},n.name,{isCallFromServer:!0});if(logger.debug("count",a),a>0)logger.info("Seed data already synced for",t);else if(e.seed&&Array.isArray(e.seed)){for(const t of e.seed)await this.curd.processRequest({action:"create",data:t},n.name,{isCallFromServer:!0});logger.info("Seed data synced for",t)}}async syncDatabase(){if(!this.controllerWrapper?.schema)throw new Error("controllerWrapper.schema not set.");const e=this.controllerWrapper.schema;for(const t of Object.keys(e))await this.syncTable(e[t]),await this.syncSeedData(e[t],t);logger.info("Database synced by SyncTable...")}async getTablesOfDatabase(){return((await this.db.raw("\n SELECT table_name\n FROM information_schema.tables\n WHERE table_schema = 'public'\n AND table_type = 'BASE TABLE'\n ")).rows||[]).map(e=>e.table_name)}getColumnString(e){return Object.keys(e).reduce((t,n)=>{const a=e[n];let o=a.type,i=a.size,l=a.isUnsigned,s=a.primary,c=a.autoIncrement,r=a.nullable,u=a.unique,m=a.default,h=a.onUpdate,g=a.comment,d=a.hasForeignKey,E=a.foreignMapTables?.[0]?.table,f=a.foreignMapTables?.[0]?.column,T=getSchemaType(o);return t[n]=`${T}`,i&&(t[n]+=`|size:${i}`),l&&(t[n]+="|unsigned"),s&&(t[n]+="|primaryKey"),c&&(t[n]+="|autoIncrement"),r&&(t[n]+="|nullable"),u&&(t[n]+="|unique"),m&&(t[n]+=`|default:${m}`),h&&(t[n]+=`|onUpdate:${h}`),g&&(t[n]+=`|comment:${g}`),d&&(t[n]+=`|foreignKey:${E}:${f}`),t},{})}async getRelations(e){let t=await this.getCurrentColumns(e);const n=Object.keys(t).filter(e=>t[e].hasForeignKey).reduce((e,n)=>{let a=t[n];if(!a)return e;if(0===a.foreignMapTables?.length)return e;let o=a.foreignMapTables;for(const t of o){let a=t.table,o=t.column;e[this.helperUtility.modelName(a)]={type:"one",table:a,localKey:n,foreignKey:o,through:null,throughLocalKey:null,throughForeignKey:null}}return e},{}),a=(this.helperUtility.modelName(e).toLowerCase(),await this.getTablesReferencedByTable(e)),o=a.map(e=>e.TABLE_NAME);for(const e of o){const t=this.helperUtility.modelName(e),o=a.find(t=>t.TABLE_NAME===e)?.REFERENCED_COLUMN_NAME,i=a.find(t=>t.TABLE_NAME===e)?.COLUMN_NAME;n[t]={type:"many",table:e,localKey:o,foreignKey:i,through:null,throughLocalKey:null,throughForeignKey:null}}return n}async generateSchema(){const e=await this.getTablesOfDatabase(),t={};for(const n of e){let e=this.helperUtility.modelName(n),a=await this.getRelations(n);t[e]={table:n,alias:e,columns:this.getColumnString(await this.getCurrentColumns(n)),modelName:e,seed:[],hasRelations:a,indexes:[]}}return t}}module.exports=SyncTable;
@@ -1 +1 @@
1
- const HelperUtility=require("./HelperUtility");class QueryBuilder{constructor(e,t,r=null){this.db=e,this.utils=t,this.controllerWrapper=r,this.helperUtility=new HelperUtility}getHookService(){return this.controllerWrapper.hookService}getQueryBuilder(e,t=null){let r=this.db(e.table);return t&&(r=t),r._getMyModel=()=>e,r}parseValue(e){return this.helperUtility.parseValue(e)}parseWhereValue(e){return this.helperUtility.parseWhereValue(e)}parseWhereColumn(e){return this.helperUtility.parseWhereColumn(e)}_applyOrWhereCondition(e,t,r,i){if(null!==i)if(Array.isArray(i))e.orWhereIn(t,i);else switch(r){case"between":e.orWhereBetween(t,i);break;case"notBetween":e.orWhereNotBetween(t,i);break;case"in":e.orWhereIn(t,i);break;case"notIn":e.orWhereNotIn(t,i);break;case"like":e.orWhere(t,"like",i);break;default:e.orWhere(t,r,i)}else e.orWhereNull(t)}_applyAndWhereCondition(e,t,r,i){if(null!==i)if(Array.isArray(i))e.whereIn(t,i);else switch(r){case"between":e.whereBetween(t,i);break;case"notBetween":e.whereNotBetween(t,i);break;case"in":e.whereIn(t,i);break;case"notIn":e.whereNotIn(t,i);break;case"like":e.where(t,"like",i);break;default:e.where(t,r,i)}else e.whereNull(t)}buildWithTree(e,t=null){return this.helperUtility.dotWalkTree(e,{resolver:({current:e,part:r,source:i})=>t?t({current:e,part:r,source:i}):{}})}async fetchRelatedRows(e,t){if(e.through){let r=await this.db(e.through).whereIn(e.throughLocalKey,t);return await this.db(e.table).whereIn(e.foreignKey,r.map(t=>t[e.throughForeignKey]))}return this.db(e.table).whereIn(e.foreignKey,t)}async fetchAndAttachRelated(e){const{parentRows:t,relName:r,model:i,withTree:o,relation:l}=e;if(!l){const e=this.getHookService().getModelInstance(i),l=`get${r.charAt(0).toUpperCase()+r.slice(1)}Relation`;if("function"==typeof e[l]){const a={rows:t,relName:r,model:i,withTree:o,controller:this.controllerWrapper,relation:i.hasRelations[r],qb:this,db:this.db};await e[l](a)}return t}let a=t.map(e=>e[l.localKey]);const s="one"===l?.type;let n=[];n=await this.fetchRelatedRows(l,a);let h=new Map;for(const e of n){let t=e[l.foreignKey];h.has(t)||h.set(t,[]),h.get(t).push(e)}for(const e of t){let t=e[l.localKey];s&&1==h.get(t)?.length?e[r]=h.get(t)[0]:e[r]=h.get(t)||[]}let c=Object.keys(o);for(const e of c){let i=o[e],l=t.filter(e=>s?e[r]:e[r].length>0).map(e=>e[r]).reduce((e,t)=>e.concat(t),[]),a=this.utils.getModel(this.controllerWrapper,r);await this.fetchAndAttachRelated({parentRows:l,relName:e,model:a,withTree:i,relation:a.hasRelations[e]})}return t}filterWhere(e,t=""){return t?Object.keys(e).filter(e=>e.includes(t)).reduce((r,i)=>(r[i.replace(t,"")]=e[i],r),{}):Object.keys(e).filter(e=>!e.includes(".")).reduce((t,r)=>(t[r]=e[r],t),{})}async getQuery(e,t){try{const{where:r={},with:i,withWhere:o,select:l,orderBy:a={column:"id",direction:"asc"},limit:s=10,offset:n=0,page:h,groupBy:c,having:p,distinct:y,join:u,leftJoin:f,rightJoin:d,innerJoin:g,count:w=!1}=t;let b=this.getQueryBuilder(e);l&&(Array.isArray(l)||"string"==typeof l)?b.select(l):b.select("*"),y&&(Array.isArray(y)||"string"==typeof y?b.distinct(y):b.distinct()),u&&this._applyJoins(b,u,"join"),f&&this._applyJoins(b,f,"leftJoin"),d&&this._applyJoins(b,d,"rightJoin"),g&&this._applyJoins(b,g,"innerJoin"),this._applyWhereClause(b,r,i),c&&(Array.isArray(c),b.groupBy(c)),p&&this._applyHavingClause(b,p),a&&this._applyOrderBy(b,a);let W=!1,A=s,k=n,m=1,_=0;h&&s>0&&(m=Math.max(1,parseInt(h)),k=(m-1)*s),s>0&&(b.limit(A),k>0&&b.offset(k));const j=await b;if(i&&i.length>0){const t=this.buildWithTree(i);for(const r of Object.keys(t))await this.fetchAndAttachRelated({parentRows:j,relName:r,model:e,withTree:t[r],relation:e.hasRelations[r]})}let B=null;if(s>0)try{let t=this.getQueryBuilder(e);r&&Object.keys(r).length>0&&this._applyWhereClause(t,r),u&&this._applyJoins(t,u,"join"),f&&this._applyJoins(t,f,"leftJoin"),d&&this._applyJoins(t,d,"rightJoin"),g&&this._applyJoins(t,g,"innerJoin");B=(await t.count("* as cnt").first()).cnt}catch(e){console.warn("Failed to get total count:",e.message),B=j.length}s>0&&null!==B&&(_=Math.ceil(B/s),W=m<_);return{data:j,totalCount:B,...s>0?{pagination:{page:m,limit:A,offset:k,totalPages:_,hasNext:W,hasPrev:m>1,nextPage:W?m+1:null,prevPage:m>1?m-1:null}}:{}}}catch(e){throw console.error("QueryService.getQuery error:",e),new Error(`Failed to execute query: ${e.message}`)}}_applyWhereWithArray(e,t,r){for(const i of t)this._applyWhereClause(e,i,r)}_applyWhereClause(e,t,r=[]){if(Array.isArray(t))this._applyWhereWithArray(e,t,r);else{if(t&&Object.keys(t).length>0){let r=this.helperUtility.getDotWalkQuery(t);r=this.helperUtility.objectFilter(r,(e,t)=>"object"!=typeof t||null===t);for(const[t,i]of Object.entries(r)){const{joinType:r="AND",column:o}=this.parseWhereColumn(t),{operator:l,value:a}=this.parseWhereValue(i);"AND"===r?this._applyAndWhereCondition(e,o,l,a):this._applyOrWhereCondition(e,o,l,a)}}r&&r.length>0&&this._applyNestedWhere(e,t,r)}}_applyNestedWhere(e,t,r){let i=this,o=e._getMyModel();if(r&&r.length>0)for(const l of r){let a=this.helperUtility.getDotWalkQuery(t,l);if(a&&Object.keys(a).length>0){let t=this.utils.getModel(this.controllerWrapper,l),s=o.hasRelations[l],n=r.map(e=>this.helperUtility.pluckDotWalkKey(e,1));e.whereExists(function(){let e=i.getQueryBuilder(t,this.select("*").from(t.table));e.whereRaw(`${s.foreignKey} = ${o.table}.${s.localKey}`),i._applyWhereClause(e,a,n)})}}}_applyJoins(e,t,r){const i=Array.isArray(t)?t:[t];for(const t of i)"string"==typeof t?e[r](t):"object"==typeof t&&(t.table&&t.on?e[r](t.table,t.on):t.table&&t.first&&t.operator&&t.second&&e[r](t.table,t.first,t.operator,t.second))}_applyWithWhere(e,t){try{if(Array.isArray(t))for(const r of t)"string"==typeof r?e.withWhere(r):"object"==typeof r&&e.withWhere(r.column,r.operator,r.value);else if("object"==typeof t)for(const[r,i]of Object.entries(t))e.withWhere(r,i)}catch(e){console.warn("Failed to apply withWhere:",e.message)}}_applyHavingClause(e,t){for(const[r,i]of Object.entries(t))"object"==typeof i&&i.operator?e.having(r,i.operator,i.value):e.having(r,i)}_applyOrderBy(e,t){if(Array.isArray(t))for(const r of t)"string"==typeof r?e.orderBy(r):"object"==typeof r&&e.orderBy(r.column,r.direction||"asc");else"string"==typeof t?e.orderBy(t):"object"==typeof t&&e.orderBy(t.column,t.direction||"asc")}}module.exports=QueryBuilder;
1
+ const HelperUtility=require("./HelperUtility"),logger=require("../../Logger");class QueryBuilder{constructor(e,t,r=null){this.db=e,this.utils=t,this.controllerWrapper=r,this.helperUtility=new HelperUtility}getHookService(){return this.controllerWrapper.hookService}getQueryBuilder(e,t=null){let r=this.db(e.table);return t&&(r=t),r._getMyModel=()=>e,r}parseValue(e){return this.helperUtility.parseValue(e)}parseWhereValue(e){return this.helperUtility.parseWhereValue(e)}parseWhereColumn(e){return this.helperUtility.parseWhereColumn(e)}_applyOrWhereCondition(e,t,r,i){if(null!==i)if(Array.isArray(i))e.orWhereIn(t,i);else switch(r){case"between":e.orWhereBetween(t,i);break;case"notBetween":e.orWhereNotBetween(t,i);break;case"in":e.orWhereIn(t,i);break;case"notIn":e.orWhereNotIn(t,i);break;case"like":e.orWhere(t,"like",i);break;default:e.orWhere(t,r,i)}else e.orWhereNull(t)}_applyAndWhereCondition(e,t,r,i){if(null!==i)if(Array.isArray(i))e.whereIn(t,i);else switch(r){case"between":e.whereBetween(t,i);break;case"notBetween":e.whereNotBetween(t,i);break;case"in":e.whereIn(t,i);break;case"notIn":e.whereNotIn(t,i);break;case"like":e.where(t,"like",i);break;default:e.where(t,r,i)}else e.whereNull(t)}buildWithTree(e,t=null){return this.helperUtility.dotWalkTree(e,{resolver:({current:e,part:r,source:i})=>t?t({current:e,part:r,source:i}):{}})}_getWithWhereForRelation(e,t){if(!e||"object"!=typeof e)return{};const r=`${t}.`,i={};for(const[t,o]of Object.entries(e))if(t.startsWith(r)){i[t.slice(r.length)]=o}return i}_applyWithWhereConditions(e,t){for(const[r,i]of Object.entries(t)){const{joinType:t="AND",column:o}=this.parseWhereColumn(r),{operator:l,value:s}=this.parseWhereValue(i);"AND"===t?this._applyAndWhereCondition(e,o,l,s):this._applyOrWhereCondition(e,o,l,s)}}async fetchRelatedRows(e,t,r={}){if(e.through){let i=await this.db(e.through).whereIn(e.throughLocalKey,t),o=this.db(e.table).whereIn(e.foreignKey,i.map(t=>t[e.throughForeignKey]));return Object.keys(r).length>0&&this._applyWithWhereConditions(o,r),o}{let i=this.db(e.table).whereIn(e.foreignKey,t);return Object.keys(r).length>0&&this._applyWithWhereConditions(i,r),i}}async fetchAndAttachRelated(e){const{parentRows:t,relName:r,model:i,withTree:o,relation:l,withWhere:s={}}=e;if(!l){const e=this.getHookService().getModelInstance(i),l=`get${r.charAt(0).toUpperCase()+r.slice(1)}Relation`;if("function"==typeof e[l]){const n={rows:t,relName:r,model:i,withTree:o,controller:this.controllerWrapper,relation:i.hasRelations[r],qb:this,db:this.db,withWhere:s};await e[l](n)}return t}let n=t.map(e=>e[l.localKey]);const a="one"===l?.type;let h=[];const p=this._getWithWhereForRelation(s,r);h=await this.fetchRelatedRows(l,n,p);let c=new Map;for(const e of h){let t=e[l.foreignKey];c.has(t)||c.set(t,[]),c.get(t).push(e)}for(const e of t){let t=e[l.localKey];a&&1==c.get(t)?.length?e[r]=c.get(t)[0]:e[r]=c.get(t)||[]}let y=Object.keys(o);for(const e of y){let i=o[e],l=t.filter(e=>a?e[r]:e[r].length>0).map(e=>e[r]).reduce((e,t)=>e.concat(t),[]),n=this.utils.getModel(this.controllerWrapper,r);await this.fetchAndAttachRelated({parentRows:l,relName:e,model:n,withTree:i,relation:n.hasRelations[e],withWhere:s})}return t}filterWhere(e,t=""){return t?Object.keys(e).filter(e=>e.includes(t)).reduce((r,i)=>(r[i.replace(t,"")]=e[i],r),{}):Object.keys(e).filter(e=>!e.includes(".")).reduce((t,r)=>(t[r]=e[r],t),{})}async getQuery(e,t){try{const{where:r={},with:i,withWhere:o,select:l,orderBy:s={column:"id",direction:"asc"},limit:n=10,offset:a=0,page:h,groupBy:p,having:c,distinct:y,join:u,leftJoin:f,rightJoin:g,innerJoin:d,count:W=!1}=t;let w=this.getQueryBuilder(e);l&&(Array.isArray(l)||"string"==typeof l)?w.select(l):w.select("*"),y&&(Array.isArray(y)||"string"==typeof y?w.distinct(y):w.distinct()),u&&this._applyJoins(w,u,"join"),f&&this._applyJoins(w,f,"leftJoin"),g&&this._applyJoins(w,g,"rightJoin"),d&&this._applyJoins(w,d,"innerJoin"),this._applyWhereClause(w,r,i),p&&(Array.isArray(p),w.groupBy(p)),c&&this._applyHavingClause(w,c),s&&this._applyOrderBy(w,s);let b=!1,_=n,A=a,k=1,m=0;h&&n>0&&(k=Math.max(1,parseInt(h)),A=(k-1)*n),n>0&&(w.limit(_),A>0&&w.offset(A));const j=await w;if(i&&i.length>0){const t=this.buildWithTree(i);for(const r of Object.keys(t))await this.fetchAndAttachRelated({parentRows:j,relName:r,model:e,withTree:t[r],relation:e.hasRelations[r],withWhere:o||{}})}let C=null;if(n>0)try{let t=this.getQueryBuilder(e);r&&Object.keys(r).length>0&&this._applyWhereClause(t,r),u&&this._applyJoins(t,u,"join"),f&&this._applyJoins(t,f,"leftJoin"),g&&this._applyJoins(t,g,"rightJoin"),d&&this._applyJoins(t,d,"innerJoin");C=(await t.count("* as cnt").first()).cnt}catch(e){logger.warn("Failed to get total count:",e.message),C=j.length}n>0&&null!==C&&(m=Math.ceil(C/n),b=k<m);return{data:j,totalCount:C,...n>0?{pagination:{page:k,limit:_,offset:A,totalPages:m,hasNext:b,hasPrev:k>1,nextPage:b?k+1:null,prevPage:k>1?k-1:null}}:{}}}catch(e){throw logger.error("QueryService.getQuery error:",e),new Error(`Failed to execute query: ${e.message}`)}}_applyWhereWithArray(e,t,r){for(const i of t)this._applyWhereClause(e,i,r)}_applyWhereClause(e,t,r=[]){if(Array.isArray(t))this._applyWhereWithArray(e,t,r);else{if(t&&Object.keys(t).length>0){let r=this.helperUtility.getDotWalkQuery(t);r=this.helperUtility.objectFilter(r,(e,t)=>"object"!=typeof t||null===t);for(const[t,i]of Object.entries(r)){const{joinType:r="AND",column:o}=this.parseWhereColumn(t),{operator:l,value:s}=this.parseWhereValue(i);"AND"===r?this._applyAndWhereCondition(e,o,l,s):this._applyOrWhereCondition(e,o,l,s)}}r&&r.length>0&&this._applyNestedWhere(e,t,r)}}_applyNestedWhere(e,t,r){let i=this,o=e._getMyModel();if(r&&r.length>0)for(const l of r){let s=this.helperUtility.getDotWalkQuery(t,l);if(s&&Object.keys(s).length>0){let t=this.utils.getModel(this.controllerWrapper,l),n=o.hasRelations[l],a=r.map(e=>this.helperUtility.pluckDotWalkKey(e,1));e.whereExists(function(){let e=i.getQueryBuilder(t,this.select("*").from(t.table));e.whereRaw(`${n.foreignKey} = ${o.table}.${n.localKey}`),i._applyWhereClause(e,s,a)})}}}_applyJoins(e,t,r){const i=Array.isArray(t)?t:[t];for(const t of i)"string"==typeof t?e[r](t):"object"==typeof t&&(t.table&&t.on?e[r](t.table,t.on):t.table&&t.first&&t.operator&&t.second&&e[r](t.table,t.first,t.operator,t.second))}_applyWithWhere(e,t){try{if(Array.isArray(t))for(const r of t)"string"==typeof r?e.withWhere(r):"object"==typeof r&&e.withWhere(r.column,r.operator,r.value);else if("object"==typeof t)for(const[r,i]of Object.entries(t))e.withWhere(r,i)}catch(e){logger.warn("Failed to apply withWhere:",e.message)}}_applyHavingClause(e,t){for(const[r,i]of Object.entries(t))"object"==typeof i&&i.operator?e.having(r,i.operator,i.value):e.having(r,i)}_applyOrderBy(e,t){if(Array.isArray(t))for(const r of t)"string"==typeof r?e.orderBy(r):"object"==typeof r&&e.orderBy(r.column,r.direction||"asc");else"string"==typeof t?e.orderBy(t):"object"==typeof t&&e.orderBy(t.column,t.direction||"asc")}}module.exports=QueryBuilder;
@@ -1 +1 @@
1
- const CurdTable=require("./CurdTable"),HelperUtility=require("./HelperUtility");class SyncTable{constructor(e,t,n=null){this.db=e,this.utils=t,this.controllerWrapper=n,this.curd=new CurdTable(e,t,n),this.helperUtility=new HelperUtility}_getClientName(){return"mysql"}async executeSql(e,t=[]){return this.db.raw(e,t)}async existsTable(e){return this.db.schema.hasTable(e)}async getCurrentColumns(e){const t=this.db.client.database(),[n]=await this.executeSql("\n SELECT \n c.COLUMN_NAME,\n c.DATA_TYPE,\n c.COLUMN_TYPE,\n c.IS_NULLABLE,\n c.COLUMN_KEY,\n c.EXTRA,\n c.CHARACTER_MAXIMUM_LENGTH,\n c.COLUMN_DEFAULT,\n c.COLUMN_COMMENT AS COMMENT,\n kcu.REFERENCED_TABLE_NAME,\n kcu.REFERENCED_COLUMN_NAME\n FROM information_schema.COLUMNS c\n LEFT JOIN information_schema.KEY_COLUMN_USAGE kcu\n ON c.TABLE_SCHEMA = kcu.TABLE_SCHEMA\n AND c.TABLE_NAME = kcu.TABLE_NAME\n AND c.COLUMN_NAME = kcu.COLUMN_NAME\n WHERE c.TABLE_SCHEMA = ?\n AND c.TABLE_NAME = ?\n ",[t,e]),a={};for(const e of n)a[e.COLUMN_NAME]=this.utils.formatColumnDef(e.COLUMN_NAME,e);return a}hasColumnChanged(e,t){const n={isNullableChanged:e.nullable!==t.nullable,isTypeChanged:e.type!==t.type,isSizeChanged:e.size!==t.size,isUnsignedChanged:e.isUnsigned!==t.isUnsigned,isPrimaryChanged:e.primary!==t.primary,isUniqueChanged:e.unique!==t.unique,isAutoIncrementChanged:e.autoIncrement!==t.autoIncrement,isDefaultChanged:e.default!==t.default,isOnUpdateChanged:e.onUpdate!==t.onUpdate,isCommentChanged:e.comment!==t.comment,isForeignKeyChanged:e.hasForeignKey!==t.hasForeignKey},a=Object.values(n).some(Boolean);return a&&console.log({changes:n,oldComment:e.comment,newComment:t.comment,name:e.name}),a}async getAlterations(e){const t={add:[],drop:[],modify:[]},n=await this.getCurrentColumns(e.table);for(const[a,s]of Object.entries(e.columns)){const e=this.utils.formatColumnSchema(a,s),o=n[a];o?(e.oldColDef=o,this.hasColumnChanged(o,e)&&t.modify.push(e)):t.add.push(e)}for(const a of Object.keys(n))e.columns[a]||t.drop.push({name:a});return t}getColumnStr(e,t,n={actionType:"CREATE",tableName:""}){const{actionType:a,tableName:s}=n,o="string"==typeof t?this.utils.formatColumnSchema(e,t):t,i=o.type,l=o.size??this.utils.getDefaultTypeSize(i);let r=`\`${e}\` ${i}${l?`(${l})`:""}`;if(o.isUnsigned&&(r+=" UNSIGNED"),o.primary&&["CREATE","ADD_COLUMN"].includes(a)&&(r+=" PRIMARY KEY"),o.autoIncrement&&(r+=" AUTO_INCREMENT"),o.nullable||(r+=" NOT NULL"),o.unique&&(r+=" UNIQUE"),o.default&&(r+=` DEFAULT ${o.default}`),o.onUpdate&&(r+=` ON UPDATE ${o.onUpdate}`),o.comment&&(r+=` COMMENT '${this.utils.escapeComment(o.comment)}'`),o.hasForeignKey&&1===o.foreignMapTables?.length&&"CREATE"===a){const{table:t,column:n}=o.foreignMapTables[0],a=`idx_${s}__${e}__fk_${t}_${n}`;r+=`, KEY \`${a}\` (\`${e}\`), CONSTRAINT \`cn_${a}\`\n FOREIGN KEY (\`${e}\`) REFERENCES \`${t}\` (\`${n}\`)\n ON DELETE RESTRICT ON UPDATE RESTRICT`}return r}async createTable(e){const t=e.table,n=e.columns,a=[];for(const[e,s]of Object.entries(n))a.push(this.getColumnStr(e,s,{actionType:"CREATE",tableName:t}));const s=`CREATE TABLE IF NOT EXISTS \`${t}\` (${a.join(", ")})`;await this.executeSql(s)}async alterTable(e,t){if(!t||"object"!=typeof t)throw new Error("alterations must be an object");const n=[];if(t.add?.length)for(const a of t.add)n.push(`ADD COLUMN ${this.getColumnStr(a.name,a,{actionType:"ADD_COLUMN",tableName:e})}`);if(t.drop?.length)for(const e of t.drop)n.push(`DROP COLUMN \`${e.name}\``);if(t.modify?.length)for(const a of t.modify)n.push(`MODIFY COLUMN ${this.getColumnStr(a.name,a,{actionType:"MODIFY_COLUMN",tableName:e})}`);if(!n.length)return void console.log("No alterations to apply for",e);const a=`ALTER TABLE \`${e}\` ${n.join(", ")}`;await this.executeSql(a)}async alterColumn(e,t,n){const a=`ALTER TABLE \`${e}\` MODIFY COLUMN ${this.getColumnStr(t,n,{actionType:"MODIFY_COLUMN",tableName:e})}`;await this.executeSql(a)}async alterIndex(e,t,n){const a=`ALTER TABLE \`${e}\` MODIFY INDEX ${`${t} ${n.type} ${n.unique?"UNIQUE":""}`}`;await this.executeSql(a)}async dropIndex(e,t){const n=`DROP INDEX \`${t}\` ON \`${e}\``;await this.executeSql(n)}async dropTable(e){const t=`DROP TABLE IF EXISTS \`${e}\``;await this.executeSql(t)}async updateTable(e){console.log(`Update table not implemented for ${e.table}`)}async syncTable(e){if(await this.existsTable(e.table)){const t=await this.getAlterations(e);return console.log(...Object.values(t)),void await this.alterTable(e.table,t)}await this.createTable(e)}async syncSeedData(e,t){let n=this.utils.getModel(this.controllerWrapper,t),a=await this.curd.processRequest({action:"count"},n.name,{isCallFromServer:!0});if(console.log("count",a),a>0)console.log("Seed data already synced for",t);else if(e.seed&&Array.isArray(e.seed)){for(const t of e.seed)await this.curd.processRequest({action:"create",data:t},n.name,{isCallFromServer:!0});console.log("Seed data synced for",t)}}async syncDatabase(){if(!this.controllerWrapper?.schema)throw new Error("controllerWrapper.schema not set.");const e=this.controllerWrapper.schema;for(const t of Object.keys(e))await this.syncTable(e[t]),await this.syncSeedData(e[t],t);console.log("Database synced by SyncTable...")}async getTablesOfDatabase(){const[e]=await this.db.raw("SHOW TABLES");return e.map(e=>e[`Tables_in_${this.db.client.database()}`])}getColumnString(e){return Object.keys(e).reduce((t,n)=>{const a=e[n];let s=a.type,o=a.size,i=a.isUnsigned,l=a.primary,r=a.autoIncrement,c=a.nullable,u=a.unique,m=a.default,d=a.onUpdate,E=a.comment,h=a.hasForeignKey,C=a.foreignMapTables?.[0]?.table,T=a.foreignMapTables?.[0]?.column;return t[n]=`${s}`,o&&(t[n]+=`|size:${o}`),i&&(t[n]+="|unsigned"),l&&(t[n]+="|primaryKey"),r&&(t[n]+="|autoIncrement"),c&&(t[n]+="|nullable"),u&&(t[n]+="|unique"),m&&(t[n]+=`|default:${m}`),d&&(t[n]+=`|onUpdate:${d}`),E&&(t[n]+=`|comment:${E}`),h&&(t[n]+=`|foreignKey:${C}:${T}`),t},{})}async generateSchema(){const e=await this.getTablesOfDatabase(),t={};for(const n of e){let e=this.helperUtility.modelName(n);t[e]={table:n,alias:e,columns:this.getColumnString(await this.getCurrentColumns(n)),modelName:e,seed:[],hasRelations:{},indexes:[]}}return t}}module.exports=SyncTable;
1
+ const CurdTable=require("./CurdTable"),HelperUtility=require("./HelperUtility"),logger=require("../../Logger");class SyncTable{constructor(e,t,n=null){this.db=e,this.utils=t,this.controllerWrapper=n,this.curd=new CurdTable(e,t,n),this.helperUtility=new HelperUtility}_getClientName(){return"mysql"}async executeSql(e,t=[]){return this.db.raw(e,t)}async existsTable(e){return this.db.schema.hasTable(e)}async getCurrentColumns(e){const t=this.db.client.database(),[n]=await this.executeSql("\n SELECT \n c.COLUMN_NAME,\n c.DATA_TYPE,\n c.COLUMN_TYPE,\n c.IS_NULLABLE,\n c.COLUMN_KEY,\n c.EXTRA,\n c.CHARACTER_MAXIMUM_LENGTH,\n c.COLUMN_DEFAULT,\n c.COLUMN_COMMENT AS COMMENT,\n kcu.REFERENCED_TABLE_NAME,\n kcu.REFERENCED_COLUMN_NAME\n FROM information_schema.COLUMNS c\n LEFT JOIN information_schema.KEY_COLUMN_USAGE kcu\n ON c.TABLE_SCHEMA = kcu.TABLE_SCHEMA\n AND c.TABLE_NAME = kcu.TABLE_NAME\n AND c.COLUMN_NAME = kcu.COLUMN_NAME\n WHERE c.TABLE_SCHEMA = ?\n AND c.TABLE_NAME = ?\n ",[t,e]),a={};for(const e of n)a[e.COLUMN_NAME]=this.utils.formatColumnDef(e.COLUMN_NAME,e);return a}hasColumnChanged(e,t){const n={isNullableChanged:e.nullable!==t.nullable,isTypeChanged:e.type!==t.type,isSizeChanged:e.size!==t.size,isUnsignedChanged:e.isUnsigned!==t.isUnsigned,isPrimaryChanged:e.primary!==t.primary,isUniqueChanged:e.unique!==t.unique,isAutoIncrementChanged:e.autoIncrement!==t.autoIncrement,isDefaultChanged:e.default!==t.default,isOnUpdateChanged:e.onUpdate!==t.onUpdate,isCommentChanged:e.comment!==t.comment,isForeignKeyChanged:e.hasForeignKey!==t.hasForeignKey},a=Object.values(n).some(Boolean);return a&&logger.debug({changes:n,oldComment:e.comment,newComment:t.comment,name:e.name}),a}async getAlterations(e){const t={add:[],drop:[],modify:[]},n=await this.getCurrentColumns(e.table);for(const[a,s]of Object.entries(e.columns)){const e=this.utils.formatColumnSchema(a,s),o=n[a];o?(e.oldColDef=o,this.hasColumnChanged(o,e)&&t.modify.push(e)):t.add.push(e)}for(const a of Object.keys(n))e.columns[a]||t.drop.push({name:a});return t}getColumnStr(e,t,n={actionType:"CREATE",tableName:""}){const{actionType:a,tableName:s}=n,o="string"==typeof t?this.utils.formatColumnSchema(e,t):t,i=o.type,r=o.size??this.utils.getDefaultTypeSize(i);let l=`\`${e}\` ${i}${r?`(${r})`:""}`;if(o.isUnsigned&&(l+=" UNSIGNED"),o.primary&&["CREATE","ADD_COLUMN"].includes(a)&&(l+=" PRIMARY KEY"),o.autoIncrement&&(l+=" AUTO_INCREMENT"),o.nullable||(l+=" NOT NULL"),o.unique&&(l+=" UNIQUE"),o.default&&(l+=` DEFAULT ${o.default}`),o.onUpdate&&(l+=` ON UPDATE ${o.onUpdate}`),o.comment&&(l+=` COMMENT '${this.utils.escapeComment(o.comment)}'`),o.hasForeignKey&&1===o.foreignMapTables?.length&&"CREATE"===a){const{table:t,column:n}=o.foreignMapTables[0],a=`idx_${s}__${e}__fk_${t}_${n}`;l+=`, KEY \`${a}\` (\`${e}\`), CONSTRAINT \`cn_${a}\`\n FOREIGN KEY (\`${e}\`) REFERENCES \`${t}\` (\`${n}\`)\n ON DELETE RESTRICT ON UPDATE RESTRICT`}return l}async createTable(e){const t=e.table,n=e.columns,a=[];for(const[e,s]of Object.entries(n))a.push(this.getColumnStr(e,s,{actionType:"CREATE",tableName:t}));const s=`CREATE TABLE IF NOT EXISTS \`${t}\` (${a.join(", ")})`;await this.executeSql(s)}async alterTable(e,t){if(!t||"object"!=typeof t)throw new Error("alterations must be an object");const n=[];if(t.add?.length)for(const a of t.add)n.push(`ADD COLUMN ${this.getColumnStr(a.name,a,{actionType:"ADD_COLUMN",tableName:e})}`);if(t.drop?.length)for(const e of t.drop)n.push(`DROP COLUMN \`${e.name}\``);if(t.modify?.length)for(const a of t.modify)n.push(`MODIFY COLUMN ${this.getColumnStr(a.name,a,{actionType:"MODIFY_COLUMN",tableName:e})}`);if(!n.length)return void logger.info("No alterations to apply for",e);const a=`ALTER TABLE \`${e}\` ${n.join(", ")}`;await this.executeSql(a)}async alterColumn(e,t,n){const a=`ALTER TABLE \`${e}\` MODIFY COLUMN ${this.getColumnStr(t,n,{actionType:"MODIFY_COLUMN",tableName:e})}`;await this.executeSql(a)}async alterIndex(e,t,n){const a=`ALTER TABLE \`${e}\` MODIFY INDEX ${`${t} ${n.type} ${n.unique?"UNIQUE":""}`}`;await this.executeSql(a)}async dropIndex(e,t){const n=`DROP INDEX \`${t}\` ON \`${e}\``;await this.executeSql(n)}async dropTable(e){const t=`DROP TABLE IF EXISTS \`${e}\``;await this.executeSql(t)}async updateTable(e){logger.warn(`Update table not implemented for ${e.table}`)}async syncTable(e){if(await this.existsTable(e.table)){const t=await this.getAlterations(e);return logger.debug(...Object.values(t)),void await this.alterTable(e.table,t)}await this.createTable(e)}async syncSeedData(e,t){let n=this.utils.getModel(this.controllerWrapper,t),a=await this.curd.processRequest({action:"count"},n.name,{isCallFromServer:!0});if(logger.debug("count",a),a>0)logger.info("Seed data already synced for",t);else if(e.seed&&Array.isArray(e.seed)){for(const t of e.seed)await this.curd.processRequest({action:"create",data:t},n.name,{isCallFromServer:!0});logger.info("Seed data synced for",t)}}async syncDatabase(){if(!this.controllerWrapper?.schema)throw new Error("controllerWrapper.schema not set.");const e=this.controllerWrapper.schema;for(const t of Object.keys(e))await this.syncTable(e[t]),await this.syncSeedData(e[t],t);logger.info("Database synced by SyncTable...")}async getTablesOfDatabase(){const[e]=await this.db.raw("SHOW TABLES");return e.map(e=>e[`Tables_in_${this.db.client.database()}`])}getColumnString(e){return Object.keys(e).reduce((t,n)=>{const a=e[n];let s=a.type,o=a.size,i=a.isUnsigned,r=a.primary,l=a.autoIncrement,c=a.nullable,u=a.unique,m=a.default,d=a.onUpdate,E=a.comment,h=a.hasForeignKey,C=a.foreignMapTables?.[0]?.table,T=a.foreignMapTables?.[0]?.column;return t[n]=`${s}`,o&&(t[n]+=`|size:${o}`),i&&(t[n]+="|unsigned"),r&&(t[n]+="|primaryKey"),l&&(t[n]+="|autoIncrement"),c&&(t[n]+="|nullable"),u&&(t[n]+="|unique"),m&&(t[n]+=`|default:${m}`),d&&(t[n]+=`|onUpdate:${d}`),E&&(t[n]+=`|comment:${E}`),h&&(t[n]+=`|foreignKey:${C}:${T}`),t},{})}async generateSchema(){const e=await this.getTablesOfDatabase(),t={};for(const n of e){let e=this.helperUtility.modelName(n);t[e]={table:n,alias:e,columns:this.getColumnString(await this.getCurrentColumns(n)),modelName:e,seed:[],hasRelations:{},indexes:[]}}return t}}module.exports=SyncTable;
package/helpers/files.js CHANGED
@@ -1 +1 @@
1
- const fs=require("fs"),path=require("path"),{S3Client:S3Client,PutObjectCommand:PutObjectCommand}=require("@aws-sdk/client-s3");class Files{s3Client=null;config=null;validateConfig(e){let t=[];if(e.region||t.push("Region as config.region"),e.endpoint||e.host||t.push("Endpoint as config.endpoint or Host as config.host"),e.accessKeyId||t.push("Access key ID as config.accessKeyId"),e.secretAccessKey||t.push("Secret access key as config.secretAccessKey"),e.bucketName||t.push("Bucket name as config.bucketName"),t.length>0)throw new Error(`Missing config: ${t.join(", ")}. Please set the config in the config file.`)}setConfig(e){this.validateConfig(e),this.config=e;let t={region:e.region,endpoint:e.endpoint||`https://${e.region}.${e.host}`,credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}};this.s3Client=new S3Client(t)}async upload2Spaces(e){const{fileBuffer:t,originalName:i,mimeType:s}=e;console.log("Uploading file to Spaces",{fileBuffer:t,originalName:i,mimeType:s},e);const n=i.split(".").pop(),r=this.config.bucketName,a=`${this.config.folderName||"uploads"}/${`${Date.now()}-${Math.random().toString(36).substring(2,9)}`}-${i}.${n}`,c={Bucket:r,Key:a,Body:t,ContentType:s,ACL:"public-read",CacheControl:"max-age=31536000"};try{const e=new PutObjectCommand(c);await this.s3Client.send(e);const t=`https://${r}.${this.config.region}.cdn.digitaloceanspaces.com/${a}`;return console.log(`File uploaded to Spaces key: ${a}`),{key:a,url:t}}catch(e){throw console.error("DigitalOcean Spaces Upload Error:",e),new Error(`Spaces upload failed: ${e.message}`)}}needSync(e,t){let i=this.readJSON(e),s=this.checkFileUpdatedAt(e),n=this.readFile(t);return n&&(n=new Date(+n)),{needSync:n&&n<s||!n,syncSchema:i,lastSyncedAt:s.getTime().toString()}}writeFromTemplate(e,t,i){const s=__dirname,n=fs.readFileSync(`${s}/${t}`,"utf8").replace(/{{(.*?)}}/g,(e,t)=>i[t]||e);this.createDirectory(path.dirname(e)),fs.writeFileSync(e,n)}writeModel(e,t){this.exists(e)?console.log("File already exists",e):(console.log("Writing model to",e),this.writeFromTemplate(e,"../templates/model.template",t))}checkFileUpdatedAt(e){return this.exists(e)?fs.statSync(e).mtime:null}isLatestFile(e,t=1e3){let i=Date.now(),s=this.checkFileUpdatedAt(e);return!!s&&i-new Date(s).getTime()<t}createDirectory(e){this.exists(e)||fs.mkdirSync(e,{recursive:!0})}deleteDirectory(e){this.exists(e)&&fs.rmSync(e,{recursive:!0,force:!0})}exists(e){return fs.existsSync(e)}rename(e,t){this.exists(e)&&fs.renameSync(e,t)}deleteFile(e){this.exists(e)&&fs.unlinkSync(e)}readFile(e,t="utf8"){return this.exists(e)?fs.readFileSync(e,t):null}writeFile(e,t,i="utf8"){fs.writeFileSync(e,t,i)}appendFile(e,t,i="utf8"){fs.appendFileSync(e,t,i)}readJSON(e){return JSON.parse(this.readFile(e))}writeJSON(e,t){this.writeFile(e,JSON.stringify(t,null,2))}readJSONL(e){return this.readFile(e).trim().split("\n").map(e=>JSON.parse(e))}writeJSONL(e,t){const i=t.map(e=>JSON.stringify(e)).join("\n");this.writeFile(e,i)}appendJSONL(e,t){const i=t.map(e=>JSON.stringify(e)).join("\n");this.appendFile(e,"\n"+i)}escapeCSV(e){if(null==e)return"";const t=String(e);return t.includes('"')||t.includes(",")||t.includes("\n")?`"${t.replace(/"/g,'""')}"`:t}writeCSV(e,t){if(!Array.isArray(t)||0===t.length)throw new Error("Data must be a non-empty array");const i=Object.keys(t[0]),s=[i.join(","),...t.map(e=>i.map(t=>this.escapeCSV(e[t])).join(","))];this.writeFile(e,s.join("\n"))}readCSV(e){return this.readFile(e).trim().split("\n").map(e=>e.split(","))}appendCSV(e,t){if(!Array.isArray(t)||0===t.length)return;const[i]=this.readFile(e).split("\n"),s=i.split(","),n=t.map(e=>s.map(t=>this.escapeCSV(e[t])).join(","));this.appendFile(e,"\n"+n.join("\n"))}}module.exports=Files;
1
+ const fs=require("fs"),path=require("path"),{S3Client:S3Client,PutObjectCommand:PutObjectCommand}=require("@aws-sdk/client-s3"),logger=require("../Logger");class Files{s3Client=null;config=null;validateConfig(e){let t=[];if(e.region||t.push("Region as config.region"),e.endpoint||e.host||t.push("Endpoint as config.endpoint or Host as config.host"),e.accessKeyId||t.push("Access key ID as config.accessKeyId"),e.secretAccessKey||t.push("Secret access key as config.secretAccessKey"),e.bucketName||t.push("Bucket name as config.bucketName"),t.length>0)throw new Error(`Missing config: ${t.join(", ")}. Please set the config in the config file.`)}setConfig(e){this.validateConfig(e),this.config=e;let t={region:e.region,endpoint:e.endpoint||`https://${e.region}.${e.host}`,credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}};this.s3Client=new S3Client(t)}async upload2Spaces(e){const{fileBuffer:t,originalName:i,mimeType:s}=e;logger.debug("Uploading file to Spaces",{fileBuffer:t,originalName:i,mimeType:s},e);const n=i.split(".").pop(),r=this.config.bucketName,a=`${this.config.folderName||"uploads"}/${`${Date.now()}-${Math.random().toString(36).substring(2,9)}`}-${i}.${n}`,c={Bucket:r,Key:a,Body:t,ContentType:s,ACL:"public-read",CacheControl:"max-age=31536000"};try{const e=new PutObjectCommand(c);await this.s3Client.send(e);const t=`https://${r}.${this.config.region}.cdn.digitaloceanspaces.com/${a}`;return logger.info(`File uploaded to Spaces key: ${a}`),{key:a,url:t}}catch(e){throw logger.error("DigitalOcean Spaces Upload Error:",e),new Error(`Spaces upload failed: ${e.message}`)}}needSync(e,t){let i=this.readJSON(e),s=this.checkFileUpdatedAt(e),n=this.readFile(t);return n&&(n=new Date(+n)),{needSync:n&&n<s||!n,syncSchema:i,lastSyncedAt:s.getTime().toString()}}writeFromTemplate(e,t,i){const s=__dirname,n=fs.readFileSync(`${s}/${t}`,"utf8").replace(/{{(.*?)}}/g,(e,t)=>i[t]||e);this.createDirectory(path.dirname(e)),fs.writeFileSync(e,n)}writeModel(e,t){this.exists(e)?logger.debug("File already exists",e):(logger.debug("Writing model to",e),this.writeFromTemplate(e,"../templates/model.template",t))}checkFileUpdatedAt(e){return this.exists(e)?fs.statSync(e).mtime:null}isLatestFile(e,t=1e3){let i=Date.now(),s=this.checkFileUpdatedAt(e);return!!s&&i-new Date(s).getTime()<t}createDirectory(e){this.exists(e)||fs.mkdirSync(e,{recursive:!0})}deleteDirectory(e){this.exists(e)&&fs.rmSync(e,{recursive:!0,force:!0})}exists(e){return fs.existsSync(e)}rename(e,t){this.exists(e)&&fs.renameSync(e,t)}deleteFile(e){this.exists(e)&&fs.unlinkSync(e)}readFile(e,t="utf8"){return this.exists(e)?fs.readFileSync(e,t):null}writeFile(e,t,i="utf8"){fs.writeFileSync(e,t,i)}appendFile(e,t,i="utf8"){fs.appendFileSync(e,t,i)}readJSON(e){return JSON.parse(this.readFile(e))}writeJSON(e,t){this.writeFile(e,JSON.stringify(t,null,2))}readJSONL(e){return this.readFile(e).trim().split("\n").map(e=>JSON.parse(e))}writeJSONL(e,t){const i=t.map(e=>JSON.stringify(e)).join("\n");this.writeFile(e,i)}appendJSONL(e,t){const i=t.map(e=>JSON.stringify(e)).join("\n");this.appendFile(e,"\n"+i)}escapeCSV(e){if(null==e)return"";const t=String(e);return t.includes('"')||t.includes(",")||t.includes("\n")?`"${t.replace(/"/g,'""')}"`:t}writeCSV(e,t){if(!Array.isArray(t)||0===t.length)throw new Error("Data must be a non-empty array");const i=Object.keys(t[0]),s=[i.join(","),...t.map(e=>i.map(t=>this.escapeCSV(e[t])).join(","))];this.writeFile(e,s.join("\n"))}readCSV(e){return this.readFile(e).trim().split("\n").map(e=>e.split(","))}appendCSV(e,t){if(!Array.isArray(t)||0===t.length)return;const[i]=this.readFile(e).split("\n"),s=i.split(","),n=t.map(e=>s.map(t=>this.escapeCSV(e[t])).join(","));this.appendFile(e,"\n"+n.join("\n"))}}module.exports=Files;
package/index.js CHANGED
@@ -1 +1 @@
1
- const ControllerWrapper=require("./ControllerWrapper"),BaseHelperUtility=require("./BaseHelperUtility"),RequestValidator=require("./RequestValidator"),Emitter=require("./Emitter");module.exports={LibClasses:{Emitter:Emitter},initializeKORM(){return ControllerWrapper.initializeKORM(...arguments)},helperUtility:new BaseHelperUtility,emitter:new Emitter,validate:RequestValidator.validate,lib:{createValidationMiddleware:RequestValidator.createValidationMiddleware,validateEmail:RequestValidator.validateEmail,validatePassword:RequestValidator.validatePassword,validatePhone:RequestValidator.validatePhone,validatePAN:RequestValidator.validatePAN,validateAadhaar:RequestValidator.validateAadhaar}};
1
+ const ControllerWrapper=require("./ControllerWrapper"),BaseHelperUtility=require("./BaseHelperUtility"),RequestValidator=require("./RequestValidator"),Emitter=require("./Emitter"),logger=require("./Logger");module.exports={LibClasses:{Emitter:Emitter},initializeKORM(){return ControllerWrapper.initializeKORM(...arguments)},helperUtility:new BaseHelperUtility,emitter:new Emitter,logger:logger,validate:RequestValidator.validate,lib:{createValidationMiddleware:RequestValidator.createValidationMiddleware,validateEmail:RequestValidator.validateEmail,validatePassword:RequestValidator.validatePassword,validatePhone:RequestValidator.validatePhone,validatePAN:RequestValidator.validatePAN,validateAadhaar:RequestValidator.validateAadhaar}};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dreamtree-org/korm-js",
3
- "version": "1.0.47",
3
+ "version": "1.0.48",
4
4
  "description": "Knowledge Object-Relational Mapping - A powerful, modular ORM system for Node.js with dynamic database operations, complex queries, relationships, and nested requests",
5
5
  "author": {
6
6
  "name": "Partha Preetham Krishna",