miridoo-js-orm
Version:
miridoo javascript orm for database
192 lines (177 loc) • 5.43 kB
JavaScript
module.exports = function($name, $connection){
var $only = [];
var $autoRelationsSate = {};
this.$proxy = {
$original : [],
$yield : 0,
$set : {}
};
this.$primaryColumn = 'id';
this.$relations = [];
this.$relationsUes = {};
//contain all query who must be execute
//this.queue = [];
this.$where = '';
this.$query = '';
this.$QueryBuilder = function(){
//return the query builder
return null;
};
this.exports = function(newname){
//export as txt., sql, csv etc...
return this;
};
this.delete = function(newname){
//drop the table
return this;
};
this.rename = function(newname){
//rename the table
return this;
};
this.alter = function(schema){
//change column of the table
return this;
};
this.create = function(schema){
$name = name || $name;
//check if tables exists
//if yes try to update table
//the best way is : call the alter function
//create the table with this schema
//return new table if is success remove the : return this;
return this;
};
this.clone = function(newname){
//create a new table with this shcema
return this;
};
this.removeRows = function(){
//remove all row
//exemple in mysql
//delete from $name;
return this;
};
this.reset = function(){
//exemple in mysql
//truncate $name;
return this;
};
this.query = function(sql, params){
//build the query
return this;
};
this.save = function(){
//single insertion into table
return this;
};
this.msave = function(){
//multiple insertion into table
return this;
};
this.schema = function(){
return this;
};
this.find = function(){
//check if the $primaryColumn have the value
return this;
};
this.get = function(qyery, params){
//build query
return; //promise who build the query
};
this.only = function(){
if(arguments.length > 0) {
$only = Array.from(arguments);
return this;
}
return $only.length > 0?$only.join(','):'*';
};
this.findBy = function(clause){
//clause is query objet
return this;
};
this.$cleanRelation = function(){
$autoRelationsSate = [];
this.$where = '';
this.$query = '';
$only = [];
};
this.with = function(){
for(var i in arguments)
$autoRelationsSate[arguments[i]] = true;
return this;
};
this.notWith = function(){
for(var i in arguments)
$autoRelationsSate[arguments[i]] = false;
return this;
};
this.$hrefBuilder = function(data, fn){
var k = this.$relations.slice(0);
var i = -1;
var kdata = {};
k.push({auto : true, call : function(){
for(var j in data){
for(var z in kdata){
for(var e in kdata[z].v){
if(kdata[z].v[e] && (kdata[z].v[e][kdata[z].r.column] == data[j][kdata[z].r.use])){
if(kdata[z].r.relation == '1'){
data[j][z] = kdata[z].v[0];
break;
}else{
data[j][z] = data[j][z] || [];
data[j][z].push(kdata[z]);
}
}
}
if(!data[j][z]){
data[j][z] = null;
}
}
}
next();
}});
k.push({auto : true, call : fn});
var next = function(name, values, relation){
if(name && values){
kdata[name] = {v : values, r : relation};
}
else if(name && !values){
throw new Error(name);
}
i++;
if(k[i] && ((k[i].auto == true && $autoRelationsSate[k[i].name] !== false) || (k[i].auto == false && $autoRelationsSate[k[i].name] === true))){
k[i].call(next);
}
else if(k[i+1]) next();
};
next();
};
this.link = function(name, relation){
//name is the var name in the result object
//this.$relations.push({auto : relation.auto, call : function(next){ next();} });//use name and relation in the function
return this;
};
this.column = function(){
//return the column manager
return this;
};
/*
* simple query maker
*/
this.where = function(query){
this.$where = query;
return this;
};
this.first = function(nb){
//nb = nb || 1
//this.$query += ' order by $primaryColumn desc limit 0, nb'
return; //promise who call get
};
this.last = function(nb){
//nb = nb || 1
//self.$query += ' order by $primaryColumn asc limit 0, nb'
return; //promise who call get
};
};