From f63c0cf6d0e24be1125c04aa05d4bb0472d92f7d Mon Sep 17 00:00:00 2001 From: jusax23 Date: Wed, 22 Feb 2023 11:13:43 +0100 Subject: [PATCH] responses --- src/db.ts | 4 +++- src/defaultHandler.ts | 8 +++++++- src/query.ts | 12 ++++++++---- src/responses.ts | 22 ++++++++++++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 src/responses.ts diff --git a/src/db.ts b/src/db.ts index 5685448..0d2ab04 100644 --- a/src/db.ts +++ b/src/db.ts @@ -4,6 +4,7 @@ import { Handler } from './defaultHandler'; import { Query } from './query'; import { attributeSettings, extendedAttributeSettings, onAction, dbType } from './types'; import { TableAlias } from "./alias" +import { insertResponse, readResponse, writeResponse } from "./responses" //import { postgresHandler } from "./postgresHandler" @@ -162,4 +163,5 @@ export class Table { export * from './funcs'; export { onAction }; -export { dbType as databaseType } \ No newline at end of file +export { dbType as databaseType } +export { readResponse, writeResponse, insertResponse } \ No newline at end of file diff --git a/src/defaultHandler.ts b/src/defaultHandler.ts index 45394ff..2c36030 100644 --- a/src/defaultHandler.ts +++ b/src/defaultHandler.ts @@ -1,5 +1,5 @@ import { AttributeAlias, TableAlias } from "./alias"; -import { Attribute, DB, Table } from "./db" +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" @@ -570,6 +570,12 @@ export class Handler { }, } + responses = { + readResponse: (resp: any) => new readResponse(resp), + writeResponse: (resp: any) => new writeResponse(resp.affectedRows), + insertResponse: (resp: any) => new insertResponse(resp.affectedRows, resp.insertId), + } + aggregations = { count: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "count(" + a.toString(handler) + ")" }]), sum: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "sum(" + a.toString(handler) + ")" }]), diff --git a/src/query.ts b/src/query.ts index 29b713e..a69d5ef 100644 --- a/src/query.ts +++ b/src/query.ts @@ -100,7 +100,8 @@ export class selectQuery { const handler = db.getHandler(); const builder = this.serialize(handler); const s = handler.builders.query(builder); - return await db.query(s); + let readResp = await db.query(s); + return db.getHandler().responses.readResponse(readResp); } } @@ -137,7 +138,8 @@ export class insertQuery { const handler = db.getHandler(); const builder = this.serialize(handler); const s = handler.builders.query(builder); - return await db.query(s); + let readResp = await db.query(s); + return db.getHandler().responses.insertResponse(readResp); } } @@ -165,7 +167,8 @@ export class updateQuery { const handler = db.getHandler(); const builder = this.serialize(handler); const s = handler.builders.query(builder); - return await db.query(s); + let readResp = await db.query(s); + return db.getHandler().responses.writeResponse(readResp); } } @@ -186,6 +189,7 @@ export class removeQuery { const handler = db.getHandler(); const builder = this.serialize(handler); const s = handler.builders.query(builder); - return await db.query(s); + let readResp = await db.query(s); + return db.getHandler().responses.writeResponse(readResp); } } \ No newline at end of file diff --git a/src/responses.ts b/src/responses.ts new file mode 100644 index 0000000..d8f8ba1 --- /dev/null +++ b/src/responses.ts @@ -0,0 +1,22 @@ + +export class readResponse extends Array{ + constructor(rows:({[key: string]:any})[]){ + super(); + this.push(...rows); + } +} + +export class writeResponse{ + affectedRows: number; + constructor(affectedRows: number){ + this.affectedRows = affectedRows; + } +} + +export class insertResponse extends writeResponse{ + insertId: number; + constructor(affectedRows: number, insertId: number){ + super(affectedRows); + this.insertId = insertId; + } +}