75 lines
1.9 KiB
TypeScript
75 lines
1.9 KiB
TypeScript
|
import mariadb from 'mariadb';
|
||
|
import { Handler } from './defaultHandler';
|
||
|
import { Query } from './query';
|
||
|
import { onAction, primaryData } from './types';
|
||
|
|
||
|
|
||
|
export class DB {
|
||
|
//pool:mariadb.Pool;
|
||
|
constructor(/*{ host, user, password, database, connectionLimit = 5 }*/) {
|
||
|
//this.pool = mariadb.createPool({ host, user, password, database, connectionLimit, multipleStatements: true });
|
||
|
}
|
||
|
async query(query: Query) {
|
||
|
//return this.pool.query(query);
|
||
|
}
|
||
|
getHandler() {
|
||
|
return Handler;
|
||
|
}
|
||
|
|
||
|
newTable(name:string){
|
||
|
return new Table(name);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export class Attribute {
|
||
|
name : string;
|
||
|
constructor(name: string){
|
||
|
this.name = name;
|
||
|
}
|
||
|
serialize(){
|
||
|
return this.toString();
|
||
|
}
|
||
|
toString(){
|
||
|
return this.name;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
export class Table{
|
||
|
dbLangTableName : string;
|
||
|
dbLangTableAttributes:{ [key: string]: Attribute; } = {};
|
||
|
[key:string]: Attribute | any
|
||
|
constructor(name: string){
|
||
|
this.dbLangTableName = name;
|
||
|
}
|
||
|
serialize(){
|
||
|
return this.toString();
|
||
|
}
|
||
|
toString(){
|
||
|
return this.dbLangTableName;
|
||
|
}
|
||
|
addAttribute(name:string,ops:{
|
||
|
unique?: boolean,
|
||
|
A_I?: boolean,
|
||
|
default?: primaryData,
|
||
|
notNull?: boolean
|
||
|
primaryKey?: boolean,
|
||
|
foreginKey?: {
|
||
|
link: Attribute,
|
||
|
onDelete?: onAction,
|
||
|
onUpdate?: onAction
|
||
|
}
|
||
|
},noErrorOnNameConflict = false){
|
||
|
let attr = new Attribute(name);
|
||
|
this.dbLangTableAttributes[name] = attr;
|
||
|
if(["serialize", "toString","addAttribute","dbLangTableName","dbLangTableAttributes"].includes(name)){
|
||
|
if(!noErrorOnNameConflict) throw new Error("You cannot name Attribute like Methode of this Table!");
|
||
|
}else{
|
||
|
this[name] = attr;
|
||
|
}
|
||
|
return attr;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export * from './funcs';
|
||
|
export {onAction};
|