Skip to content

Commit

Permalink
fix join count
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Oct 6, 2023
1 parent eefb8a6 commit 60f93f8
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 40 deletions.
27 changes: 18 additions & 9 deletions lib/DboBuilder/ViewHandler/ViewHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,25 @@ export class ViewHandler {
return this.subscribe(filter, { ...params, limit: 2 }, func, table_rules, localParams);
}

async count(filter?: Filter, param2_unused?: undefined, param3_unused?: undefined, table_rules?: TableRule, localParams?: LocalParams): Promise<number> {
async count(filter?: Filter, selectParams?: SelectParams, param3_unused?: undefined, table_rules?: TableRule, localParams?: LocalParams): Promise<number> {
filter = filter || {};
try {
await this._log({ command: "count", localParams, data: { filter } });
return await this.find(filter, { select: "", limit: 0 }, undefined, table_rules, localParams)
.then(async _allowed => {
const { filterFields, forcedFilter } = table_rules?.select || {};
const where = (await this.prepareWhere({ filter, forcedFilter, filterFields, addKeywords: true, localParams, tableRule: table_rules })).where;
const query = "SELECT COUNT(*) FROM " + this.escapedName + " " + where;
return (this.t || this.db).one(query, { _psqlWS_tableName: this.name }).then(({ count }) => +count);
.then(async _allowed => {
const q: string = await this.find(
filter, { ...selectParams, limit: selectParams?.limit ?? Number.MAX_SAFE_INTEGER },
undefined,
table_rules,
{ ...localParams, returnQuery: "noRLS" }
) as any;
const query = [
withUserRLS(localParams, ""),
"SELECT COUNT(*) FROM (",
q,
") t"
].join("\n");
return (this.t || this.db).one(query).then(({ count }) => +count);
});
} catch (e) {
if (localParams && localParams.testRule) throw e;
Expand All @@ -347,15 +356,15 @@ export class ViewHandler {
) as any;
const query = withUserRLS(
localParams,
`
`${withUserRLS(localParams, "")}
SELECT sum(pg_column_size((prgl_size_query.*))) as size
FROM (
${q}
${q}
) prgl_size_query
`
);

return (this.t || this.db).one(query, { _psqlWS_tableName: this.name }).then(({ size }) => size || '0');
return (this.t || this.db).one(query).then(({ size }) => size || '0');
});
} catch (e) {
if (localParams && localParams.testRule) throw e;
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prostgles-server",
"version": "4.1.71",
"version": "4.1.72",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -41,7 +41,7 @@
"check-disk-space": "^3.3.1",
"file-type": "^17.1.4",
"pg-promise": "^11.3.0",
"prostgles-types": "^4.0.36"
"prostgles-types": "^4.0.40"
},
"devDependencies": {
"@types/bluebird": "^3.5.36",
Expand Down
2 changes: 1 addition & 1 deletion tests/client/PID.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119454
172314
30 changes: 15 additions & 15 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": "^14.14.16",
"prostgles-client": "^4.0.19",
"prostgles-client": "^4.0.23",
"socket.io-client": "^4.7.1"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 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 60f93f8

Please sign in to comment.