ormdb v1.0.22
ormdb is an easy-to-use and promise-based Node.js ORM tool for MySQL, SQLite, and Resp Api.
Getting Started
Anotation
@DS(type: DATASOURCE,{ url: string; port: string; user?: string; password?: string; database?: string; datasource?: string; })
This Anotation is define the Datasource like Mysql, SQLite etc
Argument:
type: define type of datasource like Mysql,SQLite etc
url: hostname of datasource
datasource: it will define optional think manlly use in SQLite
@Entity
It is define Table of Datasource
@PrimaryAutoIncrement(start: number, max: number, force: boolean)
Example
@PrimaryAutoIncrement(1,10000,true)
@Column: (type: DATATYPE, maxLength: number)
@Validation: (cb: Function)
In this callback function return false then Validation is ok.
@Parse(cb:Function)
In this callback function parsing the value
@Primary(force:boolean)
Identify primary key of entity
@Default(value:string|number|Date)
default value of column
@Unique(group:string,force:boolean)
@Query(query:string)
@Table()
Entity Query
Entity Query is provied in EnitityDefination class
class SQLQuery<T> {
select(option?: {
sort?: {
type: "ASC" | "DESC";
col: string;
};
offset?: number;
limit?: number;
}): any;
selectOne(option?: {
sort?: {
type: string;
col: string;
};
offset?: number;
limit?: number;
}): any;
selectAll(): any;
update(data?: object): any;
delete(): any;
insertMany(): void;
insert(): any;
setTable(tableName: any): void;
}
** Need to extend this class in Entity class **
Entity Query Example
import {Table,DS, Entity, Column, PrimaryAutoIncrement, Query} from "ormdb/ORM/Anotation"
import {DATASOURCE,DATATYPE} from "ormdb/ORM/const"
import {SQLQuery,EnitityDefination } from "ormdb/ORM/JSONQuery";
@Entity
class Stu extends SQLQuery<Stu>{
@PrimaryAutoIncrement(1,10000,true)
@Column(DATATYPE.INT,20)
id:number;
@Column(DATATYPE.VARCHAR,20)
name:string
@Column(DATATYPE.INT,10)
roll:number
@Column(DATATYPE.VARCHAR,20)
address:string
@Column(DATATYPE.VARCHAR,20)
address7:string
@Column(DATATYPE.INT,20)
depId:number;
set(name,roll,address,depId){
this.name=name;
this.roll=roll;
this.address=address;
this.address7=address+"ram";
this.depId=depId;
}
}
@Entity
class SDepartment extends SQLQuery<Stu>{
@PrimaryAutoIncrement(1,10000,true)
@Column(DATATYPE.INT,20)
id:number;
@Column(DATATYPE.VARCHAR,20)
name:string
set(name){
this.name=name;
}
}
//@DS(DATASOURCE.RESTAPI,{url:"http://localhost",port:"4003",password:"password",database:"database",user:"user"})
//@DS(DATASOURCE.SQLLITE,{url:"D:/database",port:"3306",password:"ramsahu",database:"test",user:"ramsahu"})
@DS(DATASOURCE.MYSQL,{url:"localhost",port:"3306",password:"pass",database:"database",user:"user"})
class StudentDetails extends EnitityDefination{
@Table()
stu=new Stu();
@Table()
dep=new SDepartment();
@Query("select * from Stu")
getStudent():any{}
}
let sd=new StudentDetails();
sd.stu.set("pooja1",2,"ranchi7",2);
sd.stu.select()
.then((res)=>{
console.log(res)
})
.catch((err)=>{
console.log(err)
})
If datasource is RespApi then singature or hook of rest api is
let express=require('express');
let app=express();
let bodyParser=require("body-parser");
import {RestApiExecution} from "./ORM/RestApi/RestApiExecution";
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.post("/query",(req:any,res)=>{
let api=new RestApiExecution();
api.__database="database";
api.__datasource="datasource"; //it is used in SQLite for sub folder
api.__url="D:/database";
api.__queryObj=req.body;
api.SqLiteExecution()
.then((result)=>{
res.send(result)
})
.catch((err)=>{
res.send(err)
})
})
app.listen(4003)
Use of OQuery
OQuery with Entity Query which is return promise<Array>
let oQuery=new OQuery();
oQuery.from<Stu>(sd.stu.selectAll()) // sd is define in above class let sd=new StudentDetail();
.groupBy<{deptId:number,tottalStudent:number}>((acc,d)=>{
return {deptId:d.depId,tottalStudent:(acc.tottalStudent || 0)+1};
},"depId")
.join<SDepartment>(sd.dep.selectAll()).on((l,r)=>l.deptId==r.id).select<{deptId:number,deptName:string,totalStudent:number}>((l,r)=>{
return {deptId:r.id,deptName:r.name,totalStudent:l.tottalStudent}
})
oQuery.execute()
.then((result)=>{
console.log("query result==>",result)
})
.catch((err)=>{
console.log(err)
})
OQuery with Local data
class Student{
name:string
roll:number
depId:number;
constructor(n,r,d){
this.name=n;
this.roll=r;
this.depId=d;
}
}
class Department{
id:number;
name:string;
constructor(i,n){
this.id=i;
this.name=n;
}
}
let stuList=new Array<Student>();
let depList=new Array<Department>();
stuList.push(new Student("ram",1,1))
stuList.push(new Student("pooja",1,1))
stuList.push(new Student("guddu",1,2))
stuList.push(new Student("neha",1,2))
stuList.push(new Student("rakhi",1,1))
let stuList1=new Array<Student>();
stuList1.push(new Student("xyz",1,1))
stuList1.push(new Student("pqr",1,2))
depList.push(new Department(1,"CSE"));
depList.push(new Department(2,"ECE"));
let oQuery=new OQuery();
oQuery.from<Student>(stuList)
.groupBy<{deptId:number,tottalStudent:number}>((acc,d)=>{
return {deptId:d.depId,tottalStudent:(acc.tottalStudent || 0)+1};
},"depId")
.join<SDepartment>(depList).on((l,r)=>l.deptId==r.id).select<{deptId:number,deptName:string,totalStudent:number}>((l,r)=>{
return {deptId:r.id,deptName:r.name,totalStudent:l.tottalStudent}
})
oQuery.execute()
.then((result)=>{
console.log("query result==>",result)
})
.catch((err)=>{
console.log(err)
})
OQuery function
From
It is starting point of OQuery
example
let oQuery=new OQuery();
oQuery.from<T>(localData:Array<any>)
or
oQuery.from<T>(localData:Promise<Array<any>>)
Join
It is joining of two two diffrent list
oQuery.join<L>(localData:Array<any>)
or
oQuery.join<T>(localData:Promise<Array<any>>)
eg
oQuery.from<L>(list).join<R>(list2).on((entity1:L,entity2:R)=>entity1.key==entity2.key).select((entity1:L,entity2:R)=>{
return {key1:entity1.key,key2:entity2.key}
})
groupBy
It is used for grouping of list
eg
oQuery.groupBy((acc,entity)=>{
return object;
},key1,key2)
acc: this is final accumulator manuculate data of group by
entity is each value of list
key1,key2 is field of entity which is grouped
select
oQuery.form<{name:string,address:string}>(list1).select<{name1:string}>((entity)=>{
return {name1:entity.name}
})
where
oQuery.form<{name:string,address:string}>(list1).where((entity)=>entity.name=="ram").select<{name1:string}>((entity)=>{
return {name1:entity.name}
})
union
oQuery.form<{name:string,address:string}>(list1).union(list2)
**Note** list2 key field need to same as list1
7 months ago
11 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
12 months ago
12 months ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago