dblang/src/db.ts

72 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-01-23 21:19:18 +01:00
import mariadb from 'mariadb';
2023-01-24 10:40:16 +01:00
import { Datatype } from './dbStructure';
2023-01-23 21:19:18 +01:00
import { Handler } from './defaultHandler';
import { Query } from './query';
2023-01-24 10:40:16 +01:00
import { attributeSettings, onAction, primaryData } from './types';
2023-01-23 21:19:18 +01:00
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;
2023-01-24 10:40:16 +01:00
ops: attributeSettings;
type: Datatype;
constructor(name: string, type: Datatype, ops: attributeSettings) {
2023-01-23 21:19:18 +01:00
this.name = name;
2023-01-24 10:40:16 +01:00
this.ops = ops;
this.type = type;
}
serializeDatatype(){
return this.type.serialize();
2023-01-23 21:19:18 +01:00
}
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-24 10:40:16 +01:00
addAttribute(name: string, type: Datatype, ops: attributeSettings, noErrorOnNameConflict = false) {
let attr = new Attribute(name,type,ops);
2023-01-23 21:19:18 +01:00
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 };