From 53cb448e36b8a2aded4d8e2cf24532e5d7d31980 Mon Sep 17 00:00:00 2001 From: jusax23 Date: Mon, 27 Feb 2023 16:33:27 +0100 Subject: [PATCH 1/2] upgrade to es6 and catch conn errors --- package-lock.json | 6 +++--- package.json | 6 +++--- src/alias.ts | 4 ++-- src/db.ts | 21 ++++++++++++++------- src/dbStructure.ts | 8 ++++---- src/defaultHandler.ts | 10 +++++----- src/funcs.ts | 8 ++++---- src/query.ts | 10 +++++----- src/responses.ts | 6 +++--- src/types.ts | 8 ++++---- tsconfig.json | 3 ++- 11 files changed, 49 insertions(+), 41 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5ee3cc6..b87a04b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "dblang", - "version": "1.0.0", + "version": "0.7.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "dblang", - "version": "1.0.0", + "version": "0.7.0", "license": "UNLICENSED", "dependencies": { "mariadb": "^3.0.2", @@ -19,7 +19,7 @@ "typescript": "^4.9.4" }, "engines": { - "node": ">= 14" + "node": ">= 16" } }, "node_modules/@esbuild/android-arm": { diff --git a/package.json b/package.json index b7a92e3..2d93d67 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "dblang", - "version": "0.6.2", + "version": "0.7.0", "description": "", "main": "dist/db.js", "types": "dist/db.d.ts", - "type": "commonjs", + "type": "module", "directories": { "dist": "dist" }, @@ -22,7 +22,7 @@ "pg": "^8.9.0" }, "engines": { - "node": ">= 14" + "node": ">= 16" }, "devDependencies": { "@types/node": "^18.11.18", diff --git a/src/alias.ts b/src/alias.ts index 9ea7ad5..38a8a58 100644 --- a/src/alias.ts +++ b/src/alias.ts @@ -1,5 +1,5 @@ -import { Attribute, DB, Table } from "./db" -import { Handler } from "./defaultHandler"; +import { Attribute, DB, Table } from "./db.js" +import { Handler } from "./defaultHandler.js"; export class AttributeAlias { name: string; diff --git a/src/db.ts b/src/db.ts index 43f4693..8057ce0 100644 --- a/src/db.ts +++ b/src/db.ts @@ -1,12 +1,17 @@ import mariadb from 'mariadb'; -import { checkConstraint, Constraint, Datatype, foreignConstraint, uniqueConstraint } from './dbStructure'; -import { Handler } from './defaultHandler'; -import { Query } from './query'; -import { attributeSettings, extendedAttributeSettings, onAction, dbType } from './types'; -import { TableAlias } from "./alias" -import { insertResponse, readResponse, writeResponse, singleResponse } from "./responses" +import { checkConstraint, Constraint, Datatype, foreignConstraint, uniqueConstraint } from './dbStructure.js'; +import { Handler } from './defaultHandler.js'; +import { Query } from './query.js'; +import { attributeSettings, extendedAttributeSettings, onAction, dbType } from './types.js'; +import { TableAlias } from "./alias.js" +import { insertResponse, readResponse, writeResponse, singleResponse } from "./responses.js" //import { postgresHandler } from "./postgresHandler" +declare module "mariadb" { + interface Pool{ + on(ev: 'error', callback: () => void): Pool; + } +} export class DB { tables: Table[] = []; @@ -21,6 +26,8 @@ export class DB { this.type = databaseType; if (databaseType == dbType.mariadb) { this.mariaPool = mariadb.createPool({ host, user, password, port, database, connectionLimit, multipleStatements: true }); + // catch errors + this.mariaPool.on("error",()=>{}); this.handler = new Handler(this); } /*else if (databaseType == dbType.postgres) { @@ -170,7 +177,7 @@ export class Table { } } -export * from './funcs'; +export * from './funcs.js'; export { onAction }; export { dbType as databaseType } export { readResponse, writeResponse, insertResponse, singleResponse } \ No newline at end of file diff --git a/src/dbStructure.ts b/src/dbStructure.ts index 26de25e..f2a693b 100644 --- a/src/dbStructure.ts +++ b/src/dbStructure.ts @@ -1,7 +1,7 @@ -import { Attribute, Table } from "./db"; -import { Handler } from "./defaultHandler"; -import { QueryBuilder } from "./query"; -import { allModifierInput, joinElements, joinType, onAction, primaryData, serializeReturn } from "./types"; +import { Attribute, Table } from "./db.js"; +import { Handler } from "./defaultHandler.js"; +import { QueryBuilder } from "./query.js"; +import { allModifierInput, joinElements, joinType, onAction, primaryData, serializeReturn } from "./types.js"; export class Datatype { type: string; diff --git a/src/defaultHandler.ts b/src/defaultHandler.ts index 883c337..d37ded8 100644 --- a/src/defaultHandler.ts +++ b/src/defaultHandler.ts @@ -1,8 +1,8 @@ -import { AttributeAlias, TableAlias } from "./alias"; -import { Attribute, DB, insertResponse, readResponse, Table, writeResponse } from "./db" -import { Aggregation, checkConstraint, joinCross, Datatype, foreignConstraint, Modifier, joinNatural, onJoin, uniqueConstraint, usingJoin } from "./dbStructure" -import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery } from "./query" -import { allModifierInput, joinType, onAction, primaryData } from "./types" +import { AttributeAlias, TableAlias } from "./alias.js"; +import { Attribute, DB, insertResponse, readResponse, Table, writeResponse } from "./db.js" +import { Aggregation, checkConstraint, joinCross, Datatype, foreignConstraint, Modifier, joinNatural, onJoin, uniqueConstraint, usingJoin } from "./dbStructure.js" +import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery } from "./query.js" +import { allModifierInput, joinType, onAction, primaryData } from "./types.js" export class Handler { db: DB; diff --git a/src/funcs.ts b/src/funcs.ts index e440898..a11b831 100644 --- a/src/funcs.ts +++ b/src/funcs.ts @@ -1,7 +1,7 @@ -import { Attribute, Table } from "./db"; -import { Aggregation, BooleanModifier, checkConstraint, Datatype, foreignConstraint, joinCross, joinNatural, usingJoin, onJoin, NumberModifier, StringModifier, uniqueConstraint } from "./dbStructure"; -import { insertQuery, removeQuery, selectQuery, updateQuery } from "./query"; -import { allModifierInput, joinType, selectElements, selectFromElements } from "./types"; +import { Attribute, Table } from "./db.js"; +import { Aggregation, BooleanModifier, checkConstraint, Datatype, foreignConstraint, joinCross, joinNatural, usingJoin, onJoin, NumberModifier, StringModifier, uniqueConstraint } from "./dbStructure.js"; +import { insertQuery, removeQuery, selectQuery, updateQuery } from "./query.js"; +import { allModifierInput, joinType, selectElements, selectFromElements } from "./types.js"; //modifiers export const and = (...args: BooleanModifier[]) => new BooleanModifier("and", args); diff --git a/src/query.ts b/src/query.ts index f5202c7..00e581e 100644 --- a/src/query.ts +++ b/src/query.ts @@ -1,8 +1,8 @@ -import { AttributeAlias } from "./alias"; -import { Attribute, DB, Table } from "./db"; -import { BooleanModifier, Modifier } from "./dbStructure"; -import { Handler } from "./defaultHandler"; -import { allModifierInput, primaryData, selectElements, selectFromElements, serializeReturn } from "./types"; +import { AttributeAlias } from "./alias.js"; +import { Attribute, DB, Table } from "./db.js"; +import { BooleanModifier, Modifier } from "./dbStructure.js"; +import { Handler } from "./defaultHandler.js"; +import { allModifierInput, primaryData, selectElements, selectFromElements, serializeReturn } from "./types.js"; export class Query { diff --git a/src/responses.ts b/src/responses.ts index cede0f9..0f2ac4d 100644 --- a/src/responses.ts +++ b/src/responses.ts @@ -1,6 +1,6 @@ -import { AttributeAlias } from "./alias"; -import { Attribute } from "./db"; -import { primaryData } from "./types"; +import { AttributeAlias } from "./alias.js"; +import { Attribute } from "./db.js"; +import { primaryData } from "./types.js"; export class singleResponse { [key: string]: primaryData | any; diff --git a/src/types.ts b/src/types.ts index f256d8d..4783874 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,7 +1,7 @@ -import { AttributeAlias, TableAlias } from "./alias"; -import { Attribute, Table } from "./db"; -import { Aggregation, BooleanModifier, Datatype, Joins, Modifier } from "./dbStructure"; -import { QueryBuilder, selectQuery } from "./query"; +import { AttributeAlias, TableAlias } from "./alias.js"; +import { Attribute, Table } from "./db.js"; +import { Aggregation, BooleanModifier, Datatype, Joins, Modifier } from "./dbStructure.js"; +import { QueryBuilder, selectQuery } from "./query.js"; export type primaryData = string | number | boolean | null; export type allModifierInput = primaryData | Modifier | selectQuery | Attribute | AttributeAlias | Aggregation; diff --git a/tsconfig.json b/tsconfig.json index 408ec89..cca6c8f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { - "module": "CommonJS", + "target": "ES6", + "module": "ES6", "lib": ["es6"], "types": ["node"], "declaration": true, From cbac881a4954841238c8b593b4f4b16f4d0a7e14 Mon Sep 17 00:00:00 2001 From: jusax23 Date: Mon, 27 Feb 2023 17:59:16 +0100 Subject: [PATCH 2/2] bug fix --- src/query.ts | 2 +- src/responses.ts | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/query.ts b/src/query.ts index 00e581e..9571692 100644 --- a/src/query.ts +++ b/src/query.ts @@ -101,7 +101,7 @@ export class selectQuery { const builder = this.serialize(handler); const s = handler.builders.query(builder); let readResp = await db.query(s, printQuery); - return db.getHandler().responses.readResponse(readResp); + return handler.responses.readResponse(readResp); } } diff --git a/src/responses.ts b/src/responses.ts index 0f2ac4d..0261f44 100644 --- a/src/responses.ts +++ b/src/responses.ts @@ -15,7 +15,9 @@ export class singleResponse { export class readResponse extends Array{ constructor(rows: ({ [key: string]: primaryData })[]) { super(); - this.push(...rows.map(r => new singleResponse(r))); + for(let i = 0; i < rows.length; i++){ + this[i] = new singleResponse(rows[i]) + } } }