@dreamtree-org/korm-js 1.0.31 → 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,s=r("default"),u=r("onUpdate"),o=r("comment"),g=r("foreignKey"),m=r("size"),p=this.parseForeignKey(g),c=!!g,E=getDbType(n,m?a(m):null),f=m?a(m):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(s)),onUpdate:this.map2DbDefault(a(u)),comment:a(o)||"",hasForeignKey:c,...p}}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,"\\'")}}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;
@@ -1 +1 @@
1
- const{getDefaultTypeSize:getDefaultTypeSize,getDbType:getDbType}=require("./DataTypeMap");class BaseUtility{isInternalDefault(e){if("string"!=typeof e)return!1;const t=/^CURRENT_TIMESTAMP$/i.test(e),n=/^[a-zA-Z_][a-zA-Z0-9_]*\s*\(.*\)$/.test(e),r=/::[a-zA-Z0-9_]+/.test(e);return t||n||r}getModel(e,t){let n=e?.schema,r=null,l=Object.keys(n);if(l.forEach(e=>{e===t&&(r=n[e])}),r||l.forEach(e=>{let l=n[e];l.table===t&&(r=l)}),r)return{...r,name:t,columns:Object.keys(r.columns).map(e=>{let t=r.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:r="{}"}={}){if(null==e)return null;const l=String(e),i=l.indexOf(t),a=e=>{if(!r)return e;if("string"==typeof r&&2===r.length){const[t,n]=r;return e.startsWith(t)&&e.endsWith(n)?e.slice(1,-1):e}return"string"==typeof r&&r.length&&e.startsWith(r)&&e.endsWith(r)?e.slice(r.length,-r.length):e};if(i>=0){const e=l.slice(0,i).trim(),r=l.slice(i+t.length).trim();return"both"===n?{left:a(e),right:a(r)}:a("left"===n?e:r)}return a(l)}parseForeignKey(e){if(!e)return{foreignMapTables:[]};const t=this.getCollenedValue(e),[n,r="id"]=String(t||"").split(":"),l=(this.getCollenedValue(n)||"").split(",").filter(Boolean),i=(this.getCollenedValue(r)||"").split(",").filter(Boolean);return{foreignMapTables:l.map((e,t)=>({table:e,column:i[t]||i[0]||"id"}))}}parseColumnString(e,t){const[n,...r]=String(t).split("|"),l=e=>r.find(t=>t.startsWith(e+":")),i=e=>r.join("|").includes(e),a=e=>e&&e.includes(":")?e.slice(e.indexOf(":")+1):null,s=l("default"),u=l("onUpdate"),o=l("comment"),g=l("foreignKey"),p=l("size"),E=this.parseForeignKey(g),m=!!g,T=getDbType(n,p?a(p):null),c=p?a(p):getDefaultTypeSize(T);return{name:e,type:T,size:+c,isUnsigned:i("unsigned")||i("primaryKey")||m,columnType:`${n}${c?`(${c})`:""}`,nullable:!i("notNull"),primary:i("primaryKey"),autoIncrement:i("autoIncrement"),unique:i("unique"),default:this.map2DbDefault(a(s)),onUpdate:this.map2DbDefault(a(u)),comment:a(o)||"",hasForeignKey:m,...E}}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:"PRIMARY KEY"===t.CONSTRAINT_TYPE,unique:"UNIQUE"===t.CONSTRAINT_TYPE,autoIncrement:String(t.EXTRA||"").toLowerCase().includes("auto_increment"),hasForeignKey:"FOREIGN KEY"===t.CONSTRAINT_TYPE,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:"FOREIGN KEY"===t.CONSTRAINT_TYPE&&t.REFERENCED_TABLE_NAME?[{table:t.REFERENCED_TABLE_NAME,column:t.REFERENCED_COLUMN_NAME}]:[]}}escapeComment(e){return null==e?"":String(e).replace(/'/g,"\\'")}}module.exports=BaseUtility;
1
+ const{getDefaultTypeSize:getDefaultTypeSize,getDbType:getDbType}=require("./DataTypeMap");class BaseUtility{isInternalDefault(e){if("string"!=typeof e)return!1;const t=/^CURRENT_TIMESTAMP$/i.test(e),n=/^[a-zA-Z_][a-zA-Z0-9_]*\s*\(.*\)$/.test(e),r=/::[a-zA-Z0-9_]+/.test(e);return t||n||r}getModel(e,t){let n=e?.schema,r=null,l=Object.keys(n);if(l.forEach(e=>{e===t&&(r=n[e])}),r||l.forEach(e=>{let l=n[e];l.table===t&&(r=l)}),r)return{...r,name:t,columns:Object.keys(r.columns).map(e=>{let t=r.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:r="{}"}={}){if(null==e)return null;const l=String(e),i=l.indexOf(t),a=e=>{if(!r)return e;if("string"==typeof r&&2===r.length){const[t,n]=r;return e.startsWith(t)&&e.endsWith(n)?e.slice(1,-1):e}return"string"==typeof r&&r.length&&e.startsWith(r)&&e.endsWith(r)?e.slice(r.length,-r.length):e};if(i>=0){const e=l.slice(0,i).trim(),r=l.slice(i+t.length).trim();return"both"===n?{left:a(e),right:a(r)}:a("left"===n?e:r)}return a(l)}parseForeignKey(e){if(!e)return{foreignMapTables:[]};const t=this.getCollenedValue(e),[n,r="id"]=String(t||"").split(":"),l=(this.getCollenedValue(n)||"").split(",").filter(Boolean),i=(this.getCollenedValue(r)||"").split(",").filter(Boolean);return{foreignMapTables:l.map((e,t)=>({table:e,column:i[t]||i[0]||"id"}))}}parseColumnString(e,t){const[n,...r]=String(t).split("|"),l=e=>r.find(t=>t.startsWith(e+":")),i=e=>r.join("|").includes(e),a=e=>e&&e.includes(":")?e.slice(e.indexOf(":")+1):null,s=l("default"),u=l("onUpdate"),o=l("comment"),g=l("foreignKey"),p=l("size"),T=this.parseForeignKey(g),E=!!g,m=getDbType(n,p?a(p):null),c=p?a(p):getDefaultTypeSize(m);return{name:e,type:m,size:+c,isUnsigned:i("unsigned")||i("primaryKey")||E,columnType:`${n}${c?`(${c})`:""}`,nullable:!i("notNull"),primary:i("primaryKey"),autoIncrement:i("autoIncrement"),unique:i("unique"),default:this.map2DbDefault(a(s)),onUpdate:this.map2DbDefault(a(u)),comment:a(o)||"",hasForeignKey:E,...T}}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:"PRIMARY KEY"===t.CONSTRAINT_TYPE,unique:"UNIQUE"===t.CONSTRAINT_TYPE,autoIncrement:String(t.EXTRA||"").toLowerCase().includes("auto_increment"),hasForeignKey:"FOREIGN KEY"===t.CONSTRAINT_TYPE,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:"FOREIGN KEY"===t.CONSTRAINT_TYPE&&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
- class BaseUtility{getModel(e,t){let n=e?.schema,r=null,i=Object.keys(n);if(i.forEach(e=>{e===t&&(r=n[e])}),r||i.forEach(e=>{let i=n[e];i.table===t&&(r=i)}),r)return{...r,name:t,columns:Object.keys(r.columns).map(e=>{let t=r.columns[e];return this.parseColumnString(e,t)})};throw new Error(`Model ${t} not found`)}getDefaultTypeSize(e,t="toUpperCase"){let n=e;switch(n?.[t]&&"function"==typeof n[t]&&(n=n[t]()),n){case"VARCHAR":return 255;case"TINYINT":return 1;case"INT":return 11;default:return 0}}map2DbType(e,t="toLowerCase"){let n;switch(e){case"number":n="INT";break;case"string":default:n="VARCHAR";break;case"boolean":n="TINYINT";break;case"date":n="DATETIME";break;case"json":n="JSON"}return n?.[t]&&"function"==typeof n[t]&&(n=n[t]()),n}map2DbDefault(e){return"now"===e?"CURRENT_TIMESTAMP":e}getCollenedValue(e,{splitChar:t=":",give:n="right",trimWrap:r="{}"}={}){if(null==e)return null;const i=String(e),l=i.indexOf(t),a=e=>{if(!r)return e;if("string"==typeof r&&2===r.length){const[t,n]=r;return e.startsWith(t)&&e.endsWith(n)?e.slice(1,-1):e}return"string"==typeof r&&r.length&&e.startsWith(r)&&e.endsWith(r)?e.slice(r.length,-r.length):e};if(l>=0){const e=i.slice(0,l).trim(),r=i.slice(l+t.length).trim();return"both"===n?{left:a(e),right:a(r)}:a("left"===n?e:r)}return a(i)}parseForeignKey(e){if(!e)return{foreignMapTables:[]};const t=this.getCollenedValue(e),[n,r="id"]=String(t||"").split(":"),i=(this.getCollenedValue(n)||"").split(",").filter(Boolean),l=(this.getCollenedValue(r)||"").split(",").filter(Boolean);return{foreignMapTables:i.map((e,t)=>({table:e,column:l[t]||l[0]||"id"}))}}parseColumnString(e,t){const[n,...r]=String(t).split("|"),i=e=>r.find(t=>t.startsWith(e+":")),l=e=>r.join("|").includes(e),a=e=>e&&e.includes(":")?e.slice(e.indexOf(":")+1):null,s=i("default"),u=i("onUpdate"),o=i("comment"),c=i("foreignKey"),m=i("size"),p=this.parseForeignKey(c),g=!!c,f=this.map2DbType(n),E=m?a(m):this.getDefaultTypeSize(f);return{name:e,type:f,size:E,isUnsigned:l("unsigned")||l("primaryKey")||g,columnType:`${n}${E?`(${E})`:""}`,nullable:!l("notNull"),primary:l("primaryKey"),autoIncrement:l("autoIncrement"),unique:l("unique"),default:this.map2DbDefault(a(s)),onUpdate:this.map2DbDefault(a(u)),comment:a(o)||"",hasForeignKey:g,...p}}formatColumnSchema(e,t){return this.parseColumnString(e,t)}formatColumnDef(e,t){return{name:e,type:t.DATA_TYPE,size:t.CHARACTER_MAXIMUM_LENGTH||this.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,"\\'")}}module.exports=BaseUtility;
1
+ class BaseUtility{getModel(e,t){let n=e?.schema,r=null,i=Object.keys(n);if(i.forEach(e=>{e===t&&(r=n[e])}),r||i.forEach(e=>{let i=n[e];i.table===t&&(r=i)}),r)return{...r,name:t,columns:Object.keys(r.columns).map(e=>{let t=r.columns[e];return this.parseColumnString(e,t)})};throw new Error(`Model ${t} not found`)}getDefaultTypeSize(e,t="toUpperCase"){let n=e;switch(n?.[t]&&"function"==typeof n[t]&&(n=n[t]()),n){case"VARCHAR":return 255;case"TINYINT":return 1;case"INT":return 11;default:return 0}}map2DbType(e,t="toLowerCase"){let n;switch(e){case"number":n="INT";break;case"string":default:n="VARCHAR";break;case"boolean":n="TINYINT";break;case"date":n="DATETIME";break;case"json":n="JSON"}return n?.[t]&&"function"==typeof n[t]&&(n=n[t]()),n}map2DbDefault(e){return"now"===e?"CURRENT_TIMESTAMP":e}getCollenedValue(e,{splitChar:t=":",give:n="right",trimWrap:r="{}"}={}){if(null==e)return null;const i=String(e),l=i.indexOf(t),a=e=>{if(!r)return e;if("string"==typeof r&&2===r.length){const[t,n]=r;return e.startsWith(t)&&e.endsWith(n)?e.slice(1,-1):e}return"string"==typeof r&&r.length&&e.startsWith(r)&&e.endsWith(r)?e.slice(r.length,-r.length):e};if(l>=0){const e=i.slice(0,l).trim(),r=i.slice(l+t.length).trim();return"both"===n?{left:a(e),right:a(r)}:a("left"===n?e:r)}return a(i)}parseForeignKey(e){if(!e)return{foreignMapTables:[]};const t=this.getCollenedValue(e),[n,r="id"]=String(t||"").split(":"),i=(this.getCollenedValue(n)||"").split(",").filter(Boolean),l=(this.getCollenedValue(r)||"").split(",").filter(Boolean);return{foreignMapTables:i.map((e,t)=>({table:e,column:l[t]||l[0]||"id"}))}}parseColumnString(e,t){const[n,...r]=String(t).split("|"),i=e=>r.find(t=>t.startsWith(e+":")),l=e=>r.join("|").includes(e),a=e=>e&&e.includes(":")?e.slice(e.indexOf(":")+1):null,s=i("default"),u=i("onUpdate"),o=i("comment"),c=i("foreignKey"),p=i("size"),m=this.parseForeignKey(c),g=!!c,f=this.map2DbType(n),E=p?a(p):this.getDefaultTypeSize(f);return{name:e,type:f,size:E,isUnsigned:l("unsigned")||l("primaryKey")||g,columnType:`${n}${E?`(${E})`:""}`,nullable:!l("notNull"),primary:l("primaryKey"),autoIncrement:l("autoIncrement"),unique:l("unique"),default:this.map2DbDefault(a(s)),onUpdate:this.map2DbDefault(a(u)),comment:a(o)||"",hasForeignKey:g,...m}}formatColumnSchema(e,t){return this.parseColumnString(e,t)}formatColumnDef(e,t){return{name:e,type:t.DATA_TYPE,size:t.CHARACTER_MAXIMUM_LENGTH||this.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 this.getDefaultTypeSize(e)}}module.exports=BaseUtility;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dreamtree-org/korm-js",
3
- "version": "1.0.31",
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",