Created
June 10, 2022 09:46
-
-
Save pujie/15bf581217dfc72eab4b117b2af31bb2 to your computer and use it in GitHub Desktop.
my simple orm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
showError = obj => { | |
switch(obj.errCode){ | |
case '1': | |
return 'No table specified' | |
break | |
case '2': | |
return 'Main table has no name' | |
break | |
} | |
} | |
checkProperty = obj => { | |
if(obj.hasOwnProperty('tables')){ | |
if(1==2) | |
console.log('>>it has tables') | |
if(obj.tables.hasOwnProperty('name')){ | |
if(1==2) | |
console.log('>>it has name') | |
return true | |
}else{ | |
showError({errCode:'2'}) | |
return false | |
} | |
}else{ | |
showError({errCode:'1'}) | |
return false; | |
} | |
} | |
join = obj => { | |
if(checkProperty(obj)){ | |
maincols = obj.tables.cols.map(cols=>{ | |
return obj.tables.cols.map(col=>{obj.tables.name+'.'+col}) | |
}) | |
childcols = obj.tables.leftjoin.map(tbl=>{ | |
return tbl.glue.map(glue=>{ | |
return tbl.name+''+glue.child | |
}) | |
}) | |
console.log(maincols.concat(childcols).join(',')) | |
console.log("\n") | |
maintbl = 'select ' + obj.tables.cols.map(col=>{return obj.tables.name+'.'+col}).join(',') | |
+ obj.tables.leftjoin.map(tbl=>{ | |
return tbl.cols.map(col=>{return tbl.name+'.'+col}).join(',') | |
}).join(',') | |
+ ' from ' + obj.tables.name + ' left outer join ' + obj.tables.leftjoin.map(tbl=>{ | |
return tbl.name + ' on ' + tbl.glue.map(glue=>{ | |
return tbl.name+'.'+glue.child+'='+obj.tables.name+'.'+glue.parent | |
}).join(',') | |
}).join(' left outer join ') | |
return maintbl | |
} | |
} | |
module.exports = { | |
join:join | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
app = express(),port = 2000, | |
i = require('./js/orm') | |
console.log(i.join({ | |
tables:{ | |
cols:['a','b'], | |
'name':'tableA', | |
'leftjoin':[ | |
{ | |
'cols':[], | |
'name':'tableB', | |
'glue':[{'parent':'id','child':'id'}] | |
} | |
] | |
} | |
})) | |
console.log(i.join({ | |
tables:{ | |
'cols':['c'], | |
'name':'tableA', | |
'leftjoin':[ | |
{ | |
'cols':['a','b'], | |
'name':'tableB', | |
'glue':[{'parent':'id','child':'id'}] | |
}, | |
{ | |
'cols':['a','b'], | |
'name':'tableC', | |
'glue':[{'parent':'id','child':'id'}] | |
} | |
] | |
} | |
})) | |
console.log(i.join({ | |
tables:{ | |
'cols':['c'], | |
'name':'tableA', | |
'leftjoin':[ | |
{ | |
'cols':['a','b'], | |
'name':'tableB', | |
'glue':[{'parent':'id','child':'id'}] | |
}, | |
{ | |
'cols':['a','b'], | |
'name':'tableC', | |
'glue':[{'parent':'id','child':'id'}] | |
}, | |
{ | |
'cols':['a','f'], | |
'name':'tableD', | |
'glue':[{'parent':'id','child':'id'}] | |
} | |
] | |
} | |
})) | |
app.listen(port,_=>{ | |
console.log('Work Order Server start at port ',port) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment