dblang/src/db.ts

75 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-01-23 21:19:18 +01:00
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;
}
2023-01-23 22:10:33 +01:00
newTable(name: string) {
2023-01-23 21:19:18 +01:00
return new Table(name);
}
}
export class Attribute {
2023-01-23 22:10:33 +01:00
name: string;
constructor(name: string) {
2023-01-23 21:19:18 +01:00
this.name = name;
}
2023-01-23 22:10:33 +01:00
serialize() {
2023-01-23 21:19:18 +01:00
return this.toString();
}
2023-01-23 22:10:33 +01:00
toString() {
2023-01-23 21:19:18 +01:00
return this.name;
}
}
2023-01-23 22:10:33 +01:00
export class Table {
dbLangTableName: string;
dbLangTableAttributes: { [key: string]: Attribute; } = {};
[key: string]: Attribute | any
constructor(name: string) {
2023-01-23 21:19:18 +01:00
this.dbLangTableName = name;
}
2023-01-23 22:10:33 +01:00
serialize() {
2023-01-23 21:19:18 +01:00
return this.toString();
}
2023-01-23 22:10:33 +01:00
toString() {
2023-01-23 21:19:18 +01:00
return this.dbLangTableName;
}
2023-01-23 22:10:33 +01:00
addAttribute(name: string, ops: {
2023-01-23 21:19:18 +01:00
unique?: boolean,
A_I?: boolean,
default?: primaryData,
notNull?: boolean
primaryKey?: boolean,
foreginKey?: {
link: Attribute,
onDelete?: onAction,
onUpdate?: onAction
}
2023-01-23 22:10:33 +01:00
}, noErrorOnNameConflict = false) {
2023-01-23 21:19:18 +01:00
let attr = new Attribute(name);
this.dbLangTableAttributes[name] = attr;
2023-01-23 22:10:33 +01:00
if (["serialize", "toString", "addAttribute", "dbLangTableName", "dbLangTableAttributes"].includes(name)) {
if (!noErrorOnNameConflict) throw new Error("You cannot name Attribute like Methode of this Table!");
} else {
2023-01-23 21:19:18 +01:00
this[name] = attr;
}
return attr;
}
}
export * from './funcs';
2023-01-23 22:10:33 +01:00
export { onAction };