Aliases #3

Merged
jusax23 merged 10 commits from dev into main 2023-02-18 15:47:44 +01:00
6 changed files with 225 additions and 48 deletions
Showing only changes of commit af4208ed3a - Show all commits

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ dist
node_modules node_modules
ltests ltests
sqlsave sqlsave
postgresHandler.ts

153
package-lock.json generated
View file

@ -9,10 +9,12 @@
"version": "1.0.0", "version": "1.0.0",
"license": "UNLICENSED", "license": "UNLICENSED",
"dependencies": { "dependencies": {
"mariadb": "^3.0.2" "mariadb": "^3.0.2",
"pg": "^8.9.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^18.11.18", "@types/node": "^18.11.18",
"@types/pg": "^8.6.6",
"esbuild": "^0.17.4", "esbuild": "^0.17.4",
"typescript": "^4.9.4" "typescript": "^4.9.4"
}, },
@ -383,6 +385,25 @@
"integrity": "sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==", "integrity": "sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==",
"dev": true "dev": true
}, },
"node_modules/@types/pg": {
"version": "8.6.6",
"resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.6.6.tgz",
"integrity": "sha512-O2xNmXebtwVekJDD+02udOncjVcMZQuTEQEMpKJ0ZRf5E7/9JJX3izhKUcUifBkyKpljyUM6BTgy2trmviKlpw==",
"dev": true,
"dependencies": {
"@types/node": "*",
"pg-protocol": "*",
"pg-types": "^2.2.0"
}
},
"node_modules/buffer-writer": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz",
"integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==",
"engines": {
"node": ">=4"
}
},
"node_modules/denque": { "node_modules/denque": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz", "resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz",
@ -487,11 +508,133 @@
"node": "*" "node": "*"
} }
}, },
"node_modules/packet-reader": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz",
"integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ=="
},
"node_modules/pg": {
"version": "8.9.0",
"resolved": "https://registry.npmjs.org/pg/-/pg-8.9.0.tgz",
"integrity": "sha512-ZJM+qkEbtOHRuXjmvBtOgNOXOtLSbxiMiUVMgE4rV6Zwocy03RicCVvDXgx8l4Biwo8/qORUnEqn2fdQzV7KCg==",
"dependencies": {
"buffer-writer": "2.0.0",
"packet-reader": "1.0.0",
"pg-connection-string": "^2.5.0",
"pg-pool": "^3.5.2",
"pg-protocol": "^1.6.0",
"pg-types": "^2.1.0",
"pgpass": "1.x"
},
"engines": {
"node": ">= 8.0.0"
},
"peerDependencies": {
"pg-native": ">=3.0.1"
},
"peerDependenciesMeta": {
"pg-native": {
"optional": true
}
}
},
"node_modules/pg-connection-string": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.5.0.tgz",
"integrity": "sha512-r5o/V/ORTA6TmUnyWZR9nCj1klXCO2CEKNRlVuJptZe85QuhFayC7WeMic7ndayT5IRIR0S0xFxFi2ousartlQ=="
},
"node_modules/pg-int8": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz",
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==",
"engines": {
"node": ">=4.0.0"
}
},
"node_modules/pg-pool": {
"version": "3.5.2",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.5.2.tgz",
"integrity": "sha512-His3Fh17Z4eg7oANLob6ZvH8xIVen3phEZh2QuyrIl4dQSDVEabNducv6ysROKpDNPSD+12tONZVWfSgMvDD9w==",
"peerDependencies": {
"pg": ">=8.0"
}
},
"node_modules/pg-protocol": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.6.0.tgz",
"integrity": "sha512-M+PDm637OY5WM307051+bsDia5Xej6d9IR4GwJse1qA1DIhiKlksvrneZOYQq42OM+spubpcNYEo2FcKQrDk+Q=="
},
"node_modules/pg-types": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz",
"integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==",
"dependencies": {
"pg-int8": "1.0.1",
"postgres-array": "~2.0.0",
"postgres-bytea": "~1.0.0",
"postgres-date": "~1.0.4",
"postgres-interval": "^1.1.0"
},
"engines": {
"node": ">=4"
}
},
"node_modules/pgpass": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.5.tgz",
"integrity": "sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==",
"dependencies": {
"split2": "^4.1.0"
}
},
"node_modules/postgres-array": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz",
"integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==",
"engines": {
"node": ">=4"
}
},
"node_modules/postgres-bytea": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz",
"integrity": "sha512-xy3pmLuQqRBZBXDULy7KbaitYqLcmxigw14Q5sj8QBVLqEwXfeybIKVWiqAXTlcvdvb0+xkOtDbfQMOf4lST1w==",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/postgres-date": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz",
"integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/postgres-interval": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz",
"integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==",
"dependencies": {
"xtend": "^4.0.0"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/safer-buffer": { "node_modules/safer-buffer": {
"version": "2.1.2", "version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
}, },
"node_modules/split2": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/split2/-/split2-4.1.0.tgz",
"integrity": "sha512-VBiJxFkxiXRlUIeyMQi8s4hgvKCSjtknJv/LVYbrgALPwf5zSKmEwV9Lst25AkvMDnvxODugjdl6KZgwKM1WYQ==",
"engines": {
"node": ">= 10.x"
}
},
"node_modules/typescript": { "node_modules/typescript": {
"version": "4.9.4", "version": "4.9.4",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz",
@ -504,6 +647,14 @@
"engines": { "engines": {
"node": ">=4.2.0" "node": ">=4.2.0"
} }
},
"node_modules/xtend": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
"integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==",
"engines": {
"node": ">=0.4"
}
} }
} }
} }

View file

@ -18,13 +18,15 @@
"author": "jusax23", "author": "jusax23",
"license": "UNLICENSED", "license": "UNLICENSED",
"dependencies": { "dependencies": {
"mariadb": "^3.0.2" "mariadb": "^3.0.2",
"pg": "^8.9.0"
}, },
"engines": { "engines": {
"node": ">= 14" "node": ">= 14"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^18.11.18", "@types/node": "^18.11.18",
"@types/pg": "^8.6.6",
"esbuild": "^0.17.4", "esbuild": "^0.17.4",
"typescript": "^4.9.4" "typescript": "^4.9.4"
} }

View file

@ -2,22 +2,40 @@ import mariadb from 'mariadb';
import { checkConstraint, Constraint, Datatype, foreignConstraint, uniqueConstraint } from './dbStructure'; import { checkConstraint, Constraint, Datatype, foreignConstraint, uniqueConstraint } from './dbStructure';
import { Handler } from './defaultHandler'; import { Handler } from './defaultHandler';
import { Query } from './query'; import { Query } from './query';
import { attributeSettings, extendedAttributeSettings, onAction } from './types'; import { attributeSettings, extendedAttributeSettings, onAction, dbType } from './types';
//import { postgresHandler } from "./postgresHandler"
export class DB { export class DB {
tables: Table[] = []; tables: Table[] = [];
handler: Handler; handler: Handler;
name: string; name: string;
pool: mariadb.Pool; type: dbType;
constructor({ host, user, password, database, connectionLimit = 5 }: { host: string, user: string, password: string, database: string, connectionLimit: number }) { mariaPool!: mariadb.Pool;
this.pool = mariadb.createPool({ host, user, password, database, connectionLimit, multipleStatements: true }); //pgPool!: pg.Pool;
constructor({ host, user, password, database, connectionLimit = 5, databaseType = dbType.mariadb }: { host: string, user: string, password: string, database: string, connectionLimit: number, databaseType: dbType }) {
this.type = databaseType;
if (databaseType == dbType.mariadb) {
this.mariaPool = mariadb.createPool({ host, user, password, database, connectionLimit, multipleStatements: true });
this.handler = new Handler(); this.handler = new Handler();
}
/*else if (databaseType == dbType.postgres) {
this.pgPool = new pg.Pool({ host, user, password, max: connectionLimit, database, idleTimeoutMillis: 30000, connectionTimeoutMillis: 2000 });
this.handler = new postgresHandler();
}*/
else throw new Error("Unsuported Database type!");
this.name = database; this.name = database;
} }
async query(query: Query) { async query(query: Query) {
//console.log(query); console.log(query);
return await this.pool.query(query); if (this.type == dbType.mariadb) return await this.mariaPool.query(query);
/*else if (this.type == dbType.postgres) {
let res = await this.pgPool.query(query.sql, query.values);
console.log([...res.rows]);
return res.rows;
}*/
else throw new Error("Invalid Database Type!");
} }
getHandler() { getHandler() {
return this.handler; return this.handler;
@ -26,9 +44,6 @@ export class DB {
async sync(force = false) { async sync(force = false) {
let handler = this.getHandler(); let handler = this.getHandler();
await handler.syncDB(this, handler, force); await handler.syncDB(this, handler, force);
//this.tables.forEach(t=>{
// console.log(handler.builders.query(handler.querys.create(handler,t)));
//})
} }
newTable(name: string) { newTable(name: string) {
@ -42,7 +57,8 @@ export class DB {
return null; return null;
} }
async close() { async close() {
if (this.pool) await this.pool.end(); if (this.type == dbType.mariadb && this.mariaPool) await this.mariaPool.end();
//if (this.type == dbType.postgres && this.pgPool) await this.pgPool.end();
} }
} }
@ -140,3 +156,4 @@ export class Table {
export * from './funcs'; export * from './funcs';
export { onAction }; export { onAction };
export { dbType as databaseType }

View file

@ -10,8 +10,7 @@ export class Handler {
let key = await db.query(handler.builders.query(handler.querys.listConstraintDetails(handler, db))); let key = await db.query(handler.builders.query(handler.querys.listConstraintDetails(handler, db)));
//Table List //Table List
let allTables = (await db.query(handler.builders.query(handler.querys.listTables(handler, db)))).map((d: any) => d.TABLE_NAME); let allTables = (await db.query(handler.builders.query(handler.querys.listTables(handler, db)))).map((d: any) => d.TABLE_NAME ?? d.table_name);
// gether Table Schematics // gether Table Schematics
let tableData: { [key: string]: any; } = {}; let tableData: { [key: string]: any; } = {};
for (let i = 0; i < db.tables.length; i++) { for (let i = 0; i < db.tables.length; i++) {
@ -40,10 +39,10 @@ export class Handler {
let found = 0; let found = 0;
for (let j = 0; j < key.length; j++) { for (let j = 0; j < key.length; j++) {
if ( if (
key[j].CONSTRAINT_NAME == n (key[j].CONSTRAINT_NAME ?? key[j].constraint_name) == n
&& key[j].TABLE_NAME == table && (key[j].TABLE_NAME ?? key[j].table_name) == table
) { ) {
if (attrs.includes(key[j].COLUMN_NAME.toLowerCase())) found++; if (attrs.includes((key[j].COLUMN_NAME ?? key[j].column_name).toLowerCase())) found++;
else return false; else return false;
} }
} }
@ -65,12 +64,12 @@ export class Handler {
let found = 0; let found = 0;
for (let j = 0; j < key.length; j++) { for (let j = 0; j < key.length; j++) {
if ( if (
key[j].CONSTRAINT_NAME == n (key[j].CONSTRAINT_NAME ?? key[j].constraint_name) == n
&& key[j].TABLE_NAME == table && (key[j].TABLE_NAME ?? key[j].table_name) == table
&& key[j].REFERENCED_TABLE_NAME == refTable && (key[j].REFERENCED_TABLE_NAME ?? key[j].referenced_table_name) == refTable
) { ) {
let inF = fromAttrs.indexOf(key[j].COLUMN_NAME.toLowerCase()); let inF = fromAttrs.indexOf((key[j].COLUMN_NAME ?? key[j].column_name).toLowerCase());
let inT = toAttrs.indexOf(key[j].REFERENCED_COLUMN_NAME.toLowerCase()); let inT = toAttrs.indexOf((key[j].REFERENCED_COLUMN_NAME ?? key[j].referenced_column_name).toLowerCase());
if (inF != -1 && inT == inF) found++; if (inF != -1 && inT == inF) found++;
else return false; else return false;
} }
@ -86,10 +85,10 @@ export class Handler {
let found = 0; let found = 0;
for (let j = 0; j < key.length; j++) { for (let j = 0; j < key.length; j++) {
if ( if (
key[j].CONSTRAINT_NAME.toLowerCase() == c.name (key[j].CONSTRAINT_NAME ?? key[j].constraint_name).toLowerCase() == c.name
&& key[j].TABLE_NAME == tabel.dbLangTableName && (key[j].TABLE_NAME ?? key[j].table_name) == tabel.dbLangTableName
) { ) {
if (attrs.includes(key[j].COLUMN_NAME.toLowerCase())) found++; if (attrs.includes((key[j].COLUMN_NAME ?? key[j].column_name).toLowerCase())) found++;
else return false; else return false;
} }
} }
@ -102,12 +101,12 @@ export class Handler {
let found = 0; let found = 0;
for (let j = 0; j < key.length; j++) { for (let j = 0; j < key.length; j++) {
if ( if (
key[j].CONSTRAINT_NAME == c.name (key[j].CONSTRAINT_NAME ?? key[j].constraint_name) == c.name
&& key[j].TABLE_NAME == tabel.dbLangTableName && (key[j].TABLE_NAME ?? key[j].table_name) == tabel.dbLangTableName
&& key[j].REFERENCED_TABLE_NAME == refTable && (key[j].REFERENCED_TABLE_NAME ?? key[j].referenced_table_name) == refTable
) { ) {
let inF = fromAttrs.indexOf(key[j].COLUMN_NAME.toLowerCase()); let inF = fromAttrs.indexOf((key[j].COLUMN_NAME ?? key[j].column_name).toLowerCase());
let inT = toAttrs.indexOf(key[j].REFERENCED_COLUMN_NAME.toLowerCase()); let inT = toAttrs.indexOf((key[j].REFERENCED_COLUMN_NAME ?? key[j].referenced_column_name).toLowerCase());
if (inF != -1 && inT == inF) found++; if (inF != -1 && inT == inF) found++;
else return false; else return false;
} }
@ -118,33 +117,33 @@ export class Handler {
//delete check and unused/false(unique, foreign) constraints //delete check and unused/false(unique, foreign) constraints
for (let i = 0; i < constraints.length; i++) { for (let i = 0; i < constraints.length; i++) {
const c = constraints[i]; const c = constraints[i];
if (c.CONSTRAINT_TYPE == "CHECK") { if ((c.CONSTRAINT_TYPE ?? c.constraint_type) == "CHECK") {
del.appendEnding(handler.querys.removeCheck(handler, c.TABLE_NAME, c.CONSTRAINT_NAME)); del.appendEnding(handler.querys.removeCheck(handler, (c.TABLE_NAME ?? c.table_name), (c.CONSTRAINT_NAME ?? c.constraint_name)));
} else if (c.CONSTRAINT_TYPE == "UNIQUE") { } else if ((c.CONSTRAINT_TYPE ?? c.constraint_type) == "UNIQUE") {
if ( if (
(typeof tableData[c.TABLE_NAME.toLowerCase()] == "undefined" && deleteInDB) (typeof tableData[(c.TABLE_NAME ?? c.table_name).toLowerCase()] == "undefined" && deleteInDB)
|| !checkUniqueIsVaild(c.CONSTRAINT_NAME, c.TABLE_NAME) || !checkUniqueIsVaild((c.CONSTRAINT_NAME ?? c.constraint_name), (c.TABLE_NAME ?? c.table_name))
) del.appendEnding(handler.querys.removeUnique(handler, c.TABLE_NAME, c.CONSTRAINT_NAME)); ) del.appendEnding(handler.querys.removeUnique(handler, (c.TABLE_NAME ?? c.table_name), (c.CONSTRAINT_NAME ?? c.constraint_name)));
} else if (c.CONSTRAINT_TYPE == "FOREIGN KEY") { } else if ((c.CONSTRAINT_TYPE ?? c.constraint_type) == "FOREIGN KEY") {
if ( if (
(typeof tableData[c.TABLE_NAME.toLowerCase()] == "undefined" && deleteInDB) (typeof tableData[(c.TABLE_NAME ?? c.table_name).toLowerCase()] == "undefined" && deleteInDB)
|| !checkForeignIsVaild(c.CONSTRAINT_NAME, c.TABLE_NAME) || !checkForeignIsVaild((c.CONSTRAINT_NAME ?? c.constraint_name), (c.TABLE_NAME ?? c.table_name))
) del.appendEnding(handler.querys.removeForeignKey(handler, c.TABLE_NAME, c.CONSTRAINT_NAME)); ) del.appendEnding(handler.querys.removeForeignKey(handler, (c.TABLE_NAME ?? c.table_name), (c.CONSTRAINT_NAME ?? c.constraint_name)));
} }
} }
//delete unused Tables //delete unused Tables
if (deleteInDB) for (let i = 0; i < allTables.length; i++) { if (deleteInDB) for (let i = 0; i < allTables.length; i++) {
if (typeof tableData[allTables[i].toLowerCase()] == "undefined") del.appendEnding(handler.querys.deleteTable(handler, allTables[i])); if (typeof tableData[allTables[i]] == "undefined") del.appendEnding(handler.querys.deleteTable(handler, allTables[i]));
} }
function freeForUpdate(attr: string, table: string) { function freeForUpdate(attr: string, table: string) {
for (let i = 0; i < key.length; i++) { for (let i = 0; i < key.length; i++) {
const k = key[i]; const k = key[i];
if ( if (
k.REFERENCED_TABLE_NAME == table (k.REFERENCED_TABLE_NAME ?? k.referenced_table_name) == table
&& k.REFERENCED_COLUMN_NAME.toLowerCase() == attr.toLowerCase() && (k.REFERENCED_COLUMN_NAME ?? k.referenced_column_name).toLowerCase() == attr.toLowerCase()
) { ) {
del.appendEnding(handler.querys.removeForeignKey(handler, k.TABLE_NAME, k.CONSTRAINT_NAME)); del.appendEnding(handler.querys.removeForeignKey(handler, (k.TABLE_NAME ?? k.table_name), (k.CONSTRAINT_NAME ?? k.constraint_name)));
} }
} }
} }
@ -323,7 +322,8 @@ export class Handler {
const qb = new QueryBuilder(); const qb = new QueryBuilder();
qb.addCode(`SELECT * FROM information_schema.tables where TABLE_SCHEMA = `); qb.addCode(`SELECT * FROM information_schema.tables where TABLE_SCHEMA = `);
qb.addInjection(db.name); qb.addInjection(db.name);
qb.addCode(` and TABLE_TYPE = "BASE TABLE"`); qb.addCode(` and TABLE_TYPE = `);
qb.addInjection("BASE TABLE");
return qb; return qb;
}, },
listConstraints: (handler: Handler, db: DB) => { listConstraints: (handler: Handler, db: DB) => {

View file

@ -57,3 +57,9 @@ export enum joinType {
full, full,
cross cross
} }
export enum dbType {
mariadb,
//mysql,
postgres,
}