Skip to content

Commit

Permalink
add rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Jun 4, 2024
1 parent f6cd627 commit a0ee147
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 22 deletions.
19 changes: 17 additions & 2 deletions lib/DboBuilder/runSQL.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ParameterizedQuery as PQ, ParameterizedQuery } from 'pg-promise';
import pgPromise, { ParameterizedQuery as PQ, ParameterizedQuery } from 'pg-promise';
import pg from "pg-promise/typescript/pg-subset";
import { AnyObject, SQLOptions, SQLResult, SQLResultInfo } from "prostgles-types";
import { DB, Prostgles } from "../Prostgles";
Expand Down Expand Up @@ -58,7 +58,22 @@ export async function runSQL(this: DboBuilder, queryWithoutRLS: string, args: un
});
}

const queryResult = await db.result<AnyObject>(finalQuery, hasParams ? args : undefined)
const params = hasParams ? args : undefined;
let queryResult: pgPromise.IResultExt<AnyObject> | undefined;

if(returnType === "default-with-rollback"){
const ROLLBACK = "Rollback";
await db.tx(async t => {
queryResult = await t.result<AnyObject>(finalQuery, params);
/** Rollback */
return Promise.reject(new Error(ROLLBACK));
}).catch(e => {
if(!(e instanceof Error && e.message === ROLLBACK)) throw e;
});
} else {
queryResult = await db.result<AnyObject>(finalQuery, params);
}
if(!queryResult) throw "No query result";
const { fields, rows } = queryResult;

const listenHandlers = await onSQLResult.bind(this)(queryWithoutRLS, queryResult, allowListen, localParams);
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"pg-cursor": "^2.10.5",
"pg-promise": "^11.6.0",
"prostgles-client": "^4.0.53",
"prostgles-types": "^4.0.86"
"prostgles-types": "^4.0.87"
},
"devDependencies": {
"@types/bluebird": "^3.5.36",
Expand Down
16 changes: 8 additions & 8 deletions tests/client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"license": "ISC",
"dependencies": {
"@types/node": "^20.9.2",
"prostgles-client": "^4.0.131",
"prostgles-client": "^4.0.132",
"prostgles-types": "^4.0.51",
"socket.io-client": "^4.7.5"
},
Expand Down
10 changes: 9 additions & 1 deletion tests/isomorphicQueries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,15 @@ export const isomorphicQueries = async (db: DBOFullyTyped | DBHandlerClient, log
resV,
["a", "b"]
);
})
});

await test("sql returntype default-with-rollback", async () => {
const item = { name: "a" };
const res = await db.sql!("delete from items2 returning name; ", {}, { returnType: "default-with-rollback" });
assert.deepEqual(res.rows, [item]);
const count = await db.items2.count!();
assert.equal(count, 1);
});

/**
* returnType "value"
Expand Down
4 changes: 2 additions & 2 deletions tests/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a0ee147

Please sign in to comment.