@dreamtree-org/korm-js 1.0.32 → 1.0.33

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.
@@ -1 +1 @@
1
- const{getDefaultTypeSize:getDefaultTypeSize,getDbType:getDbType}=require("./DataTypeMap");class BaseUtility{getModel(e,t){let n=e?.schema,l=null,r=Object.keys(n);if(r.forEach(e=>{e===t&&(l=n[e])}),l||r.forEach(e=>{let r=n[e];r.table===t&&(l=r)}),l)return{...l,name:t,columns:Object.keys(l.columns).map(e=>{let t=l.columns[e];return this.parseColumnString(e,t)})};throw new Error(`Model ${t} not found`)}map2DbDefault(e){return"now"===e?"CURRENT_TIMESTAMP":e}getCollenedValue(e,{splitChar:t=":",give:n="right",trimWrap:l="{}"}={}){if(null==e)return null;const r=String(e),i=r.indexOf(t),a=e=>{if(!l)return e;if("string"==typeof l&&2===l.length){const[t,n]=l;return e.startsWith(t)&&e.endsWith(n)?e.slice(1,-1):e}return"string"==typeof l&&l.length&&e.startsWith(l)&&e.endsWith(l)?e.slice(l.length,-l.length):e};if(i>=0){const e=r.slice(0,i).trim(),l=r.slice(i+t.length).trim();return"both"===n?{left:a(e),right:a(l)}:a("left"===n?e:l)}return a(r)}parseForeignKey(e){if(!e)return{foreignMapTables:[]};const t=this.getCollenedValue(e),[n,l="id"]=String(t||"").split(":"),r=(this.getCollenedValue(n)||"").split(",").filter(Boolean),i=(this.getCollenedValue(l)||"").split(",").filter(Boolean);return{foreignMapTables:r.map((e,t)=>({table:e,column:i[t]||i[0]||"id"}))}}parseColumnString(e,t){const[n,...l]=String(t).split("|"),r=e=>l.find(t=>t.startsWith(e+":")),i=e=>l.join("|").includes(e),a=e=>e&&e.includes(":")?e.slice(e.indexOf(":")+1):null,u=r("default"),s=r("onUpdate"),o=r("comment"),g=r("foreignKey"),p=r("size"),m=this.parseForeignKey(g),c=!!g,E=getDbType(n,p?a(p):null),f=p?a(p):getDefaultTypeSize(E);return{name:e,type:E,size:f,isUnsigned:i("unsigned")||i("primaryKey")||c,columnType:`${n}${f?`(${f})`:""}`,nullable:!i("notNull"),primary:i("primaryKey"),autoIncrement:i("autoIncrement"),unique:i("unique"),default:this.map2DbDefault(a(u)),onUpdate:this.map2DbDefault(a(s)),comment:a(o)||"",hasForeignKey:c,...m}}formatColumnSchema(e,t){return this.parseColumnString(e,t)}formatColumnDef(e,t){return{name:e,type:t.DATA_TYPE,size:t.CHARACTER_MAXIMUM_LENGTH||getDefaultTypeSize(t.DATA_TYPE),isUnsigned:String(t.COLUMN_TYPE||"").toLowerCase().includes("unsigned"),columnType:t.COLUMN_TYPE,nullable:"YES"===t.IS_NULLABLE,primary:"PRI"===t.COLUMN_KEY,unique:"UNI"===t.COLUMN_KEY,autoIncrement:String(t.EXTRA||"").toLowerCase().includes("auto_increment"),hasForeignKey:"MUL"===t.COLUMN_KEY,comment:t.COMMENT,default:t.COLUMN_DEFAULT,onUpdate:((e="")=>{const t=String(e).match(/on update\s+([a-zA-Z_]+)/i);return t?t[1]:null})(t.EXTRA),foreignMapTables:"MUL"===t.COLUMN_KEY&&t.REFERENCED_TABLE_NAME?[{table:t.REFERENCED_TABLE_NAME,column:t.REFERENCED_COLUMN_NAME}]:[]}}escapeComment(e){return null==e?"":String(e).replace(/'/g,"\\'")}getDefaultTypeSize(e){return getDefaultTypeSize(e)}}module.exports=BaseUtility;
1
+ const{getDefaultTypeSize:getDefaultTypeSize,getDbType:getDbType}=require("./DataTypeMap");class BaseUtility{getModel(e,t){let n=e?.schema,l=null,r=Object.keys(n);if(r.forEach(e=>{e===t&&(l=n[e])}),l||r.forEach(e=>{let r=n[e];r.table===t&&(l=r)}),l)return{...l,name:t,columns:Object.keys(l.columns).map(e=>{let t=l.columns[e];return this.parseColumnString(e,t)})};throw new Error(`Model ${t} not found`)}map2DbDefault(e){switch(e){case"now":case"now()":return"CURRENT_TIMESTAMP";default:return e}}getCollenedValue(e,{splitChar:t=":",give:n="right",trimWrap:l="{}"}={}){if(null==e)return null;const r=String(e),i=r.indexOf(t),a=e=>{if(!l)return e;if("string"==typeof l&&2===l.length){const[t,n]=l;return e.startsWith(t)&&e.endsWith(n)?e.slice(1,-1):e}return"string"==typeof l&&l.length&&e.startsWith(l)&&e.endsWith(l)?e.slice(l.length,-l.length):e};if(i>=0){const e=r.slice(0,i).trim(),l=r.slice(i+t.length).trim();return"both"===n?{left:a(e),right:a(l)}:a("left"===n?e:l)}return a(r)}parseForeignKey(e){if(!e)return{foreignMapTables:[]};const t=this.getCollenedValue(e),[n,l="id"]=String(t||"").split(":"),r=(this.getCollenedValue(n)||"").split(",").filter(Boolean),i=(this.getCollenedValue(l)||"").split(",").filter(Boolean);return{foreignMapTables:r.map((e,t)=>({table:e,column:i[t]||i[0]||"id"}))}}parseColumnString(e,t){const[n,...l]=String(t).split("|"),r=e=>l.find(t=>t.startsWith(e+":")),i=e=>l.join("|").includes(e),a=e=>e&&e.includes(":")?e.slice(e.indexOf(":")+1):null,u=r("default"),s=r("onUpdate"),o=r("comment"),g=r("foreignKey"),p=r("size"),m=this.parseForeignKey(g),c=!!g,f=getDbType(n,p?a(p):null),E=p?a(p):getDefaultTypeSize(f);return{name:e,type:f,size:E,isUnsigned:i("unsigned")||i("primaryKey")||c,columnType:`${n}${E?`(${E})`:""}`,nullable:!i("notNull"),primary:i("primaryKey"),autoIncrement:i("autoIncrement"),unique:i("unique"),default:this.map2DbDefault(a(u)),onUpdate:this.map2DbDefault(a(s)),comment:a(o)||"",hasForeignKey:c,...m}}formatColumnSchema(e,t){return this.parseColumnString(e,t)}formatColumnDef(e,t){return{name:e,type:t.DATA_TYPE,size:t.CHARACTER_MAXIMUM_LENGTH||getDefaultTypeSize(t.DATA_TYPE),isUnsigned:String(t.COLUMN_TYPE||"").toLowerCase().includes("unsigned"),columnType:t.COLUMN_TYPE,nullable:"YES"===t.IS_NULLABLE,primary:"PRI"===t.COLUMN_KEY,unique:"UNI"===t.COLUMN_KEY,autoIncrement:String(t.EXTRA||"").toLowerCase().includes("auto_increment"),hasForeignKey:"MUL"===t.COLUMN_KEY,comment:t.COMMENT,default:t.COLUMN_DEFAULT,onUpdate:((e="")=>{const t=String(e).match(/on update\s+([a-zA-Z_]+)/i);return t?t[1]:null})(t.EXTRA),foreignMapTables:"MUL"===t.COLUMN_KEY&&t.REFERENCED_TABLE_NAME?[{table:t.REFERENCED_TABLE_NAME,column:t.REFERENCED_COLUMN_NAME}]:[]}}escapeComment(e){return null==e?"":String(e).replace(/'/g,"\\'")}getDefaultTypeSize(e){return getDefaultTypeSize(e)}}module.exports=BaseUtility;
@@ -1 +1 @@
1
- const DataTypeMap=[{type:"number",maxSize:1,dbType:"tinyint"},{type:"number",maxSize:2,dbType:"smallint"},{type:"number",maxSize:3,dbType:"mediumint"},{type:"number",maxSize:4,dbType:"int"},{type:"number",maxSize:8,dbType:"bigint"},{type:"number",maxSize:null,dbType:"decimal"},{type:"number",maxSize:null,dbType:"numeric"},{type:"number",maxSize:null,dbType:"float"},{type:"number",maxSize:null,dbType:"double"},{type:"number",maxSize:null,dbType:"real"},{type:"number",maxSize:null,dbType:"bit"},{type:"string",maxSize:255,dbType:"varchar"},{type:"string",maxSize:1,dbType:"char"},{type:"string",maxSize:65535,dbType:"text"},{type:"string",maxSize:255,dbType:"tinytext"},{type:"string",maxSize:65535,dbType:"text"},{type:"string",maxSize:16777215,dbType:"mediumtext"},{type:"string",maxSize:4294967295,dbType:"longtext"},{type:"string",maxSize:255,dbType:"enum"},{type:"string",maxSize:255,dbType:"set"},{type:"string",maxSize:null,dbType:"json"},{type:"string",maxSize:36,dbType:"uuid"},{type:"buffer",maxSize:255,dbType:"binary"},{type:"buffer",maxSize:255,dbType:"varbinary"},{type:"buffer",maxSize:255,dbType:"tinyblob"},{type:"buffer",maxSize:65535,dbType:"blob"},{type:"buffer",maxSize:16777215,dbType:"mediumblob"},{type:"buffer",maxSize:4294967295,dbType:"longblob"},{type:"boolean",maxSize:null,dbType:"tinyint(1)"},{type:"boolean",maxSize:null,dbType:"boolean"},{type:"date",maxSize:null,dbType:"date"},{type:"date",maxSize:null,dbType:"datetime"},{type:"date",maxSize:null,dbType:"timestamp"},{type:"date",maxSize:null,dbType:"time"},{type:"date",maxSize:null,dbType:"year"},{type:"string",maxSize:null,dbType:"geometry"},{type:"string",maxSize:null,dbType:"point"},{type:"string",maxSize:null,dbType:"linestring"},{type:"string",maxSize:null,dbType:"polygon"},{type:"string",maxSize:null,dbType:"multipoint"},{type:"string",maxSize:null,dbType:"multilinestring"},{type:"string",maxSize:null,dbType:"multipolygon"},{type:"string",maxSize:null,dbType:"geometrycollection"},{type:"string",maxSize:null,dbType:"USER-DEFINED"}];function getDbType(e,t,p){if(p){const y=DataTypeMap.find(y=>y.type===e&&y.maxSize===t&&y.dbType.toLowerCase()===p.toLowerCase());if(y)return y.dbType}let y=DataTypeMap.find(p=>p.type===e&&p.maxSize===t);if(!y){let t=DataTypeMap.filter(t=>t.type===e&&null===t.maxSize);y=t.length>1&&p?t.find(e=>e.dbType.toLowerCase()===p.toLowerCase()):t[0]}return y?y.dbType:null}function getSchemaType(e){return DataTypeMap.find(t=>t.dbType===e)?.type}function getDefaultTypeSize(e){return DataTypeMap.find(t=>t.dbType===e)?.maxSize}module.exports={DataTypeMap:DataTypeMap,getDbType:getDbType,getSchemaType:getSchemaType,getDefaultTypeSize:getDefaultTypeSize};
1
+ const DataTypeMap=[{type:"number",maxSize:1,dbType:"tinyint"},{type:"number",maxSize:2,dbType:"smallint"},{type:"number",maxSize:3,dbType:"mediumint"},{type:"number",maxSize:4,dbType:"int"},{type:"number",maxSize:8,dbType:"bigint"},{type:"number",maxSize:null,dbType:"decimal"},{type:"number",maxSize:null,dbType:"numeric"},{type:"number",maxSize:null,dbType:"float"},{type:"number",maxSize:null,dbType:"double"},{type:"number",maxSize:null,dbType:"real"},{type:"number",maxSize:null,dbType:"bit"},{type:"string",maxSize:255,dbType:"varchar"},{type:"string",maxSize:1,dbType:"char"},{type:"string",maxSize:65535,dbType:"text"},{type:"string",maxSize:255,dbType:"tinytext"},{type:"string",maxSize:65535,dbType:"text"},{type:"string",maxSize:16777215,dbType:"mediumtext"},{type:"string",maxSize:4294967295,dbType:"longtext"},{type:"string",maxSize:255,dbType:"enum"},{type:"string",maxSize:255,dbType:"set"},{type:"string",maxSize:null,dbType:"json"},{type:"string",maxSize:36,dbType:"uuid"},{type:"buffer",maxSize:255,dbType:"binary"},{type:"buffer",maxSize:255,dbType:"varbinary"},{type:"buffer",maxSize:255,dbType:"tinyblob"},{type:"buffer",maxSize:65535,dbType:"blob"},{type:"buffer",maxSize:16777215,dbType:"mediumblob"},{type:"buffer",maxSize:4294967295,dbType:"longblob"},{type:"boolean",maxSize:null,dbType:"tinyint(1)"},{type:"boolean",maxSize:null,dbType:"boolean"},{type:"date",maxSize:null,dbType:"date"},{type:"date",maxSize:null,dbType:"datetime"},{type:"date",maxSize:null,dbType:"timestamp"},{type:"date",maxSize:null,dbType:"time"},{type:"date",maxSize:null,dbType:"year"},{type:"string",maxSize:null,dbType:"geometry"},{type:"string",maxSize:null,dbType:"point"},{type:"string",maxSize:null,dbType:"linestring"},{type:"string",maxSize:null,dbType:"polygon"},{type:"string",maxSize:null,dbType:"multipoint"},{type:"string",maxSize:null,dbType:"multilinestring"},{type:"string",maxSize:null,dbType:"multipolygon"},{type:"string",maxSize:null,dbType:"geometrycollection"},{type:"string",maxSize:null,dbType:"USER-DEFINED"}];function getDbType(e,t,p){if(p){const y=DataTypeMap.find(y=>y.type===e&&y.maxSize===t&&y.dbType.toLowerCase()===p.toLowerCase());if(y)return y.dbType}let y=DataTypeMap.find(p=>p.type===e&&p.maxSize===t);if(!y){let t=DataTypeMap.filter(t=>t.type===e&&null===t.maxSize);y=t.length>1&&p?t.find(e=>e.dbType.toLowerCase()===p.toLowerCase()):t[0]}return y?y.dbType:e}function getSchemaType(e){return DataTypeMap.find(t=>t.dbType===e)?.type}function getDefaultTypeSize(e){return DataTypeMap.find(t=>t.dbType===e)?.maxSize}module.exports={DataTypeMap:DataTypeMap,getDbType:getDbType,getSchemaType:getSchemaType,getDefaultTypeSize:getDefaultTypeSize};
@@ -1 +1 @@
1
- const path=require("path");class HookService{constructor(e,o,t=null){this.db=e,this.utils=o,this.controllerWrapper=t,this.appRoot=t&&t.resolverPath?t.resolverPath:process.cwd()}loadModelClass(e){try{const o=path.join(this.appRoot,"models",`${e}.model.js`);delete require.cache[require.resolve(o)];return require(o)}catch(e){return}}resolveModelHook(e,o,t){const r=e.name,s=this.loadModelClass(r);if(!s)throw new Error(`Model class not found for ${r}`);const i="function"==typeof s?new s:s;let l;if("validate"===o)l="validate";else if("on"===o)l=`on${t.charAt(0).toUpperCase()+t.slice(1)}`;else if("before"===o)l=`before${t.charAt(0).toUpperCase()+t.slice(1)}`;else if("after"===o)l=`after${t.charAt(0).toUpperCase()+t.slice(1)}`;else{if("custom"!==o)throw new Error(`Invalid hook type: ${o}`);l=`on${t.charAt(0).toUpperCase()+t.slice(1)}Action`}let n=!1;if("function"==typeof i[l])return n=!0,i[l].bind(i);if("function"==typeof s[l])return n=!0,s[l].bind(s);if(!n)throw new Error(`No ${o} hook found for ${r}.${t}`)}async executeValidatorHook(e,o,t,r){const s=this.resolveModelHook(e,"validate",o);if(s)return await s({model:e,action:o,request:t,context:r,db:this.db,utils:this.utils})}async executeBeforeHook(e,o,t,r){const s=this.resolveModelHook(e,"before",o);if(s)return await s({model:e,action:o,request:t,context:r,db:this.db,utils:this.utils})}async executeAfterHook(e,o,t,r,s){const i=this.resolveModelHook(e,"after",o);return i?await i({model:e,action:o,data:t,request:r,context:s,db:this.db,utils:this.utils}):t}async executeCustomAction(e,o,t,r){const s=this.resolveModelHook(e,"custom",o);if(s)return await s({model:e,action:o,request:t,context:r,db:this.db,utils:this.utils});throw new Error(`No custom action hook found for ${e}.${o}`)}}module.exports=HookService;
1
+ const path=require("path");class HookService{constructor(e,o,t=null){this.db=e,this.utils=o,this.controllerWrapper=t,this.appRoot=t&&t.resolverPath?t.resolverPath:process.cwd()}loadModelClass(e){try{const o=path.join(this.appRoot,"models",`${e}.model.js`);delete require.cache[require.resolve(o)];return require(o)}catch(e){return void console.log("loadModelClass error",{err:e})}}resolveModelHook(e,o,t){const r=e.modelName,s=this.loadModelClass(r);if(!s)return;const l="function"==typeof s?new s:s;let i;if("validate"===o)i="validate";else if("on"===o)i=`on${t.charAt(0).toUpperCase()+t.slice(1)}`;else if("before"===o)i=`before${t.charAt(0).toUpperCase()+t.slice(1)}`;else if("after"===o)i=`after${t.charAt(0).toUpperCase()+t.slice(1)}`;else{if("custom"!==o)return;i=`on${t.charAt(0).toUpperCase()+t.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:o,request:t,ctx:r,controller:s}){const l=this.resolveModelHook(e,"validate",o);if(l)return await l({model:e,action:o,request:t,context:r,db:this.db,utils:this.utils,controller:s})}async executeBeforeHook({model:e,action:o,request:t,ctx:r,controller:s}){const l=this.resolveModelHook(e,"before",o);if(l)return await l({model:e,action:o,request:t,context:r,db:this.db,utils:this.utils,controller:s})}async executeAfterHook({model:e,action:o,data:t,request:r,ctx:s,controller:l}){const i=this.resolveModelHook(e,"after",o);return i?await i({model:e,action:o,data:t,request:r,context:s,db:this.db,utils:this.utils,controller:l}):t}async executeCustomAction({model:e,action:o,request:t,ctx:r,controller:s}){const l=this.resolveModelHook(e,"custom",o);if(l)return await l({model:e,action:o,request:t,context:r,db:this.db,utils:this.utils,controller:s});throw new Error(`No custom action hook found for ${e.modelName}.${o}`)}}module.exports=HookService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dreamtree-org/korm-js",
3
- "version": "1.0.32",
3
+ "version": "1.0.33",
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",