Skip to content

Commit

Permalink
add return condition
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Oct 22, 2023
1 parent ae7cb43 commit e97260f
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/DboBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export type LocalParams = {

// localTX?: pgPromise.ITask<{}>;

returnQuery?: boolean | "noRLS";
returnQuery?: boolean | "noRLS" | "where-condition";
returnNewQuery?: boolean;
/** Used for count/size queries */
bypassLimit?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion lib/DboBuilder/TableHandler/runInsertUpdateQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const runInsertUpdateQuery = async ({ tableHandler, queryWithoutUserRLS,
}

if(checkFilter && result.failed_check?.length){
throw { message: `New data failed the check condition`, failed: result.failed_check };
throw { message: `New data failed the check condition` };
}

const finalDBtx = tableHandler.getFinalDBtx(localParams);
Expand Down
5 changes: 3 additions & 2 deletions lib/DboBuilder/ViewHandler/prepareWhere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type PrepareWhereParams = {
tableRule: TableRule | undefined
};

export async function prepareWhere(this: ViewHandler, params: PrepareWhereParams): Promise<{ where: string; filter: AnyObject; exists: ExistsFilterConfig[]; }> {
export async function prepareWhere(this: ViewHandler, params: PrepareWhereParams): Promise<{ condition: string; where: string; filter: AnyObject; exists: ExistsFilterConfig[]; }> {
const { filter, select, forcedFilter, filterFields: ff, addWhere: addKeywords = true, tableAlias, localParams, tableRule } = params;
const { $and: $and_key, $or: $or_key } = this.dboBuilder.prostgles.keywords;

Expand Down Expand Up @@ -82,8 +82,9 @@ export async function prepareWhere(this: ViewHandler, params: PrepareWhereParams
[$and_key]: [forcedFilter, filter].filter(isDefined)
} : { ...filter };

const condition = cond;
if (cond && addKeywords) {
cond = `WHERE ${cond}`;
}
return { where: cond || "", filter: finalFilter, exists };
return { condition, where: cond || "", filter: finalFilter, exists };
}
12 changes: 7 additions & 5 deletions lib/DboBuilder/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const find = async function(this: ViewHandler, filter?: Filter, selectPar
throw `returnType (${returnType}) can only be ${allowedReturnTypes.join(" OR ")}`
}

const { testRule = false, returnQuery = false, returnNewQuery, bypassLimit = false } = localParams || {};
const { testRule = false } = localParams || {};

if (testRule) return [];
if (selectParams) {
Expand Down Expand Up @@ -58,7 +58,6 @@ export const find = async function(this: ViewHandler, filter?: Filter, selectPar
);

const queryWithRLS = withUserRLS(localParams, queryWithoutRLS);
// console.log(_query, JSON.stringify(q, null, 2))
if (testRule) {
try {
await this.db.any(withUserRLS(localParams, "EXPLAIN " + queryWithRLS));
Expand All @@ -70,9 +69,12 @@ export const find = async function(this: ViewHandler, filter?: Filter, selectPar
}

/** Used for subscribe */
if(returnNewQuery) return (q as unknown as any);
if (returnQuery) {
return ((returnQuery === "noRLS"? queryWithoutRLS : queryWithRLS) as unknown as any[]);
if(localParams?.returnNewQuery) return (q as unknown as any);
if (localParams?.returnQuery) {
if(localParams?.returnQuery === "where-condition"){
return q.whereOpts.condition as any;
}
return ((localParams?.returnQuery === "noRLS"? queryWithoutRLS : queryWithRLS) as unknown as any[]);
}

return runQueryReturnType(queryWithRLS, returnType, this, localParams);
Expand Down
2 changes: 1 addition & 1 deletion tests/client/PID.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124144
122840
24 changes: 24 additions & 0 deletions tests/server/DBoGenerated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ export type DBSchemaGenerated = {
public?: null | string;
};
};
files: {
is_view: false;
select: true;
insert: true;
update: true;
delete: true;
columns: {
added?: string;
cloud_url?: null | string;
content_length?: number;
content_type: string;
deleted?: null | number;
deleted_from_storage?: null | number;
description?: null | string;
etag?: null | string;
extension: string;
id?: string;
name: string;
original_name: string;
signed_url?: null | string;
signed_url_expires?: null | number;
url: string;
};
};
geography_columns: {
is_view: true;
select: true;
Expand Down
4 changes: 2 additions & 2 deletions tests/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,10 +493,10 @@ function dd(){

} else if(process.env.TEST_TYPE === "server"){

await server_only_queries(db as any);
log("Server-only query tests successful");
await isomorphic(db as any);
log("Server isomorphic tests successful");
await server_only_queries(db);
log("Server-only query tests successful");

stopTest()
} else {
Expand Down
21 changes: 14 additions & 7 deletions tests/server_only_queries.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
export default async function f(db: any){
import { DBHandlerServer } from "../dist/DboBuilder";
import { strict as assert } from 'assert';

export default async function f(db: DBHandlerServer){

/** Self reference recursion bug */
await db.rec.findOne({ id: 1 }, { select: { "*": 1, rec_ref: "*" } })
await db.rec.findOne!({ id: 1 }, { select: { "*": 1, rec_ref: "*" } })

const whereStatement = await db.rec.find!({ id: 1 }, undefined, undefined, undefined, { returnQuery: "where-condition" });

assert.equal(whereStatement, `"id" = 1`);

/* Transaction example */
await db.tx(async t => {
await t.items.insert({ name: "tx_" });
const expect1 = await t.items.count({ name: "tx_" });
const expect0 = await db.items.count({ name: "tx_" });
await db.tx!(async t => {
await t.items.insert!({ name: "tx_" });
const expect1 = await t.items.count!({ name: "tx_" });
const expect0 = await db.items.count!({ name: "tx_" });
if(expect0 !== 0 || expect1 !== 1) throw "db.tx failed";

//throw "err"; // Any errors will revert all data-changing commands using the transaction object ( t )
});
const expect1 = await db.items.count({ name: "tx_" });
const expect1 = await db.items.count!({ name: "tx_" });
if(expect1 !== 1) throw "db.tx failed";

}

0 comments on commit e97260f

Please sign in to comment.