dblang/src/db.ts

123 lines
4.4 KiB
TypeScript
Raw Normal View History

2023-01-23 21:19:18 +01:00
import mariadb from 'mariadb';
2023-01-29 22:11:24 +01:00
import { checkConstraint, Constraint, Datatype, uniqueConstraint } from './dbStructure';
2023-01-23 21:19:18 +01:00
import { Handler } from './defaultHandler';
2023-02-13 23:44:08 +01:00
import { Query } from './query';
import { attributeSettings, extendedAttributeSettings, onAction } from './types';
2023-01-23 21:19:18 +01:00
export class DB {
2023-02-13 23:44:08 +01:00
tables: Table[] = [];
2023-01-24 20:34:03 +01:00
handler: Handler;
2023-01-29 22:11:24 +01:00
name: string;
2023-02-13 23:44:08 +01:00
pool: mariadb.Pool;
constructor({ host, user, password, database, connectionLimit = 5 }: { host: string, user: string, password: string, database: string, connectionLimit: number }) {
this.pool = mariadb.createPool({ host, user, password, database, connectionLimit, multipleStatements: true });
2023-01-24 20:34:03 +01:00
this.handler = new Handler();
2023-02-13 23:44:08 +01:00
this.name = database;
2023-01-23 21:19:18 +01:00
}
async query(query: Query) {
2023-01-24 20:34:03 +01:00
console.log(query);
2023-02-13 23:44:08 +01:00
return await this.pool.query(query);
2023-01-23 21:19:18 +01:00
}
getHandler() {
2023-01-24 20:34:03 +01:00
return this.handler;
}
2023-02-13 23:44:08 +01:00
async sync() {
2023-01-24 20:34:03 +01:00
let handler = this.getHandler();
2023-02-13 23:44:08 +01:00
await handler.syncDB(this, handler);
//this.tables.forEach(t=>{
// console.log(handler.builders.query(handler.querys.create(handler,t)));
//})
2023-01-23 21:19:18 +01:00
}
2023-01-23 22:10:33 +01:00
newTable(name: string) {
2023-02-13 23:44:08 +01:00
let tabel = new Table(name, this);
2023-01-24 20:34:03 +01:00
this.tables.push(tabel);
return tabel;
2023-01-23 21:19:18 +01:00
}
}
export class Attribute {
2023-01-23 22:10:33 +01:00
name: string;
2023-01-24 20:34:03 +01:00
table: Table;
2023-01-24 10:40:16 +01:00
ops: attributeSettings;
type: Datatype;
2023-02-13 23:44:08 +01:00
constructor(name: string, table: Table, type: Datatype, ops: attributeSettings) {
2023-01-23 21:19:18 +01:00
this.name = name;
2023-01-24 10:40:16 +01:00
this.type = type;
2023-01-24 20:34:03 +01:00
this.table = table;
2023-02-13 23:44:08 +01:00
this.ops = ops;
if (ops.check != null) {
if(typeof ops.check == "function")ops.check = ops.check(this);
2023-01-29 22:11:24 +01:00
table.addConstraint(new checkConstraint(
2023-02-13 23:44:08 +01:00
table.dbLangDatabaseInstance.name + "_" + table.dbLangTableName + "_" + name + "_check_constraint",
2023-01-29 22:11:24 +01:00
ops.check
))
}
2023-02-13 23:44:08 +01:00
if (ops.unique != null) {
2023-01-29 22:11:24 +01:00
table.addConstraint(new uniqueConstraint(
2023-02-13 23:44:08 +01:00
table.dbLangDatabaseInstance.name + "_" + table.dbLangTableName + " " + name + "_unique_constraint",
2023-01-29 22:11:24 +01:00
[this]
))
}
2023-01-24 20:34:03 +01:00
}
2023-02-13 23:44:08 +01:00
serializeDatatype(handler: Handler) {
2023-01-24 20:34:03 +01:00
return this.type.serialize(handler);
}
2023-02-13 23:44:08 +01:00
serializeSettings(handler: Handler) {
return handler.builders.attributeSettings(handler, this);
2023-01-24 10:40:16 +01:00
}
2023-02-13 23:44:08 +01:00
serialize(handler: Handler) {
2023-01-24 20:34:03 +01:00
return handler.builders.escapeID(this.name);
2023-01-23 21:19:18 +01:00
}
2023-02-13 23:44:08 +01:00
toString(handler: Handler = this.table.dbLangDatabaseInstance.getHandler()) {
return this.table.serialize(handler) + "." + this.serialize(handler);
2023-01-23 21:19:18 +01:00
}
2023-02-13 23:44:08 +01:00
toStringFunc(handler: Handler) {
return this.table.serialize(handler) + "(" + this.serialize(handler) + ")";
2023-01-23 21:19:18 +01:00
}
}
2023-01-23 22:10:33 +01:00
export class Table {
dbLangTableName: string;
dbLangTableAttributes: { [key: string]: Attribute; } = {};
2023-01-24 20:34:03 +01:00
dbLangDatabaseInstance: DB;
2023-01-29 22:11:24 +01:00
dbLangConstrains: Constraint[] = [];
2023-02-13 23:44:08 +01:00
[key: string]: Attribute | any;
constructor(name: string, db: DB) {
2023-01-23 21:19:18 +01:00
this.dbLangTableName = name;
2023-01-29 22:11:24 +01:00
this.dbLangDatabaseInstance = db;
2023-01-23 21:19:18 +01:00
}
2023-02-13 23:44:08 +01:00
serialize(handler: Handler) {
2023-01-24 20:34:03 +01:00
return this.toString(handler);
2023-01-23 21:19:18 +01:00
}
2023-02-13 23:44:08 +01:00
toString(handler: Handler = this.dbLangDatabaseInstance.getHandler()) {
2023-01-24 20:34:03 +01:00
return handler.builders.escapeID(this.dbLangTableName);
2023-01-23 21:19:18 +01:00
}
2023-01-24 20:34:03 +01:00
addAttribute(name: string, type: Datatype, ops: attributeSettings = {}, noErrorOnNameConflict = false) {
2023-02-13 23:44:08 +01:00
if (this.dbLangTableAttributes[name] != null) throw new Error("You are tring to create an Attribute twise!");
let attr = new Attribute(name, this, type, ops);
2023-01-23 21:19:18 +01:00
this.dbLangTableAttributes[name] = attr;
2023-02-13 23:44:08 +01:00
if (["serialize", "toString", "addAttribute", "dbLangTableName", "dbLangTableAttributes", "dbLangDatabaseInstance", "addConstraint", "addAttributes"].includes(name)) {
2023-01-23 22:10:33 +01:00
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;
}
2023-02-13 23:44:08 +01:00
addAttributes(list: { [key: string]: (extendedAttributeSettings) }): { [key: string]: Attribute } {
return Object.fromEntries(Object.entries(list).map(([k, a]) => {
return [k, this.addAttribute(k, a.type, a)];
2023-01-29 22:11:24 +01:00
}));
}
2023-02-13 23:44:08 +01:00
addConstraint(c: Constraint) {
2023-01-29 22:11:24 +01:00
c.check(this);
this.dbLangConstrains.push(c);
}
2023-01-23 21:19:18 +01:00
}
export * from './funcs';
2023-01-23 22:10:33 +01:00
export { onAction };