responses #8

Merged
jusax23 merged 3 commits from dev into main 2023-02-22 11:17:26 +01:00
4 changed files with 40 additions and 6 deletions
Showing only changes of commit f63c0cf6d0 - Show all commits

View file

@ -4,6 +4,7 @@ import { Handler } from './defaultHandler';
import { Query } from './query'; import { Query } from './query';
import { attributeSettings, extendedAttributeSettings, onAction, dbType } from './types'; import { attributeSettings, extendedAttributeSettings, onAction, dbType } from './types';
import { TableAlias } from "./alias" import { TableAlias } from "./alias"
import { insertResponse, readResponse, writeResponse } from "./responses"
//import { postgresHandler } from "./postgresHandler" //import { postgresHandler } from "./postgresHandler"
@ -162,4 +163,5 @@ export class Table {
export * from './funcs'; export * from './funcs';
export { onAction }; export { onAction };
export { dbType as databaseType } export { dbType as databaseType }
export { readResponse, writeResponse, insertResponse }

View file

@ -1,5 +1,5 @@
import { AttributeAlias, TableAlias } from "./alias"; 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 { Aggregation, checkConstraint, joinCross, Datatype, foreignConstraint, Modifier, joinNatural, onJoin, uniqueConstraint, usingJoin } from "./dbStructure"
import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery } from "./query" import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery } from "./query"
import { allModifierInput, joinType, onAction, primaryData } from "./types" 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 = { aggregations = {
count: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "count(" + a.toString(handler) + ")" }]), 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) + ")" }]), sum: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "sum(" + a.toString(handler) + ")" }]),

View file

@ -100,7 +100,8 @@ export class selectQuery {
const handler = db.getHandler(); const handler = db.getHandler();
const builder = this.serialize(handler); const builder = this.serialize(handler);
const s = handler.builders.query(builder); 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 handler = db.getHandler();
const builder = this.serialize(handler); const builder = this.serialize(handler);
const s = handler.builders.query(builder); 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 handler = db.getHandler();
const builder = this.serialize(handler); const builder = this.serialize(handler);
const s = handler.builders.query(builder); 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 handler = db.getHandler();
const builder = this.serialize(handler); const builder = this.serialize(handler);
const s = handler.builders.query(builder); const s = handler.builders.query(builder);
return await db.query(s); let readResp = await db.query(s);
return db.getHandler().responses.writeResponse(readResp);
} }
} }

22
src/responses.ts Normal file
View file

@ -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;
}
}