Skip to content

Commit

Permalink
fix repeating existsJoined tables bug
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Oct 14, 2023
1 parent 84561f4 commit 8b327b0
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 15 deletions.
1 change: 0 additions & 1 deletion lib/DboBuilder/ViewHandler/getExistsCondition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export async function getExistsCondition(this: ViewHandler, eConfig: ExistsFilte
if(eConfig.isJoined){
const { query } = getTableJoinQuery({
path: eConfig.parsedPath,
aliasSufix: "jd",
rootTableAlias: thisTable,
type: "EXISTS",
finalWhere,
Expand Down
14 changes: 7 additions & 7 deletions lib/DboBuilder/ViewHandler/getTableJoinQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ import { asName } from "prostgles-types";
import { ParsedJoinPath } from "./parseJoinPath";

type getTableJoinsArgs = {
aliasSufix: string;
rootTableAlias: string;
type: "INNER" | "LEFT" | "EXISTS";
finalWhere?: string;
path: ParsedJoinPath[];
}
export const getTableJoinQuery = ({ path, type, aliasSufix, rootTableAlias, finalWhere }: getTableJoinsArgs): { targetAlias: string; query: string } => {
export const getTableJoinQuery = ({ path, type, rootTableAlias, finalWhere }: getTableJoinsArgs): { targetAlias: string; query: string } => {

const [firstPath] = path;
if(!firstPath){
throw `Cannot create join query for empty path`;
}
const getTableAlias = (table: string) => asName(`${aliasSufix}_${table}`);
const aliasSufix = "jd";
const getTableAlias = (table: string, pathIndex: number) => asName(`${aliasSufix}_${pathIndex}_${table}`);

const query = path.map(({ table, on }, i) => {
if(!on) throw "on missing";
const tableName = table;
const tableAlias = getTableAlias(table);
const prevTableAlias = i === 0? rootTableAlias : getTableAlias(path[i-1]!.table);
const tableAlias = getTableAlias(table, i);
const prevTableAlias = i === 0? rootTableAlias : getTableAlias(path[i-1]!.table, i-1);

const onCondition = getJoinOnCondition({ on, leftAlias: prevTableAlias, rightAlias: tableAlias });

Expand All @@ -38,7 +38,7 @@ export const getTableJoinQuery = ({ path, type, aliasSufix, rootTableAlias, fina
`WHERE (${getJoinOnCondition({
on: firstPath.on,
leftAlias: rootTableAlias,
rightAlias: getTableAlias(firstPath.table)
rightAlias: getTableAlias(firstPath.table, 0)
})})` : "";

const tableSelect = (isExists && isLast)? [
Expand Down Expand Up @@ -66,7 +66,7 @@ export const getTableJoinQuery = ({ path, type, aliasSufix, rootTableAlias, fina

return {
query,
targetAlias: getTableAlias(path.at(-1)!.table)
targetAlias: getTableAlias(path.at(-1)!.table, path.length - 1)
}
}

Expand Down
63 changes: 62 additions & 1 deletion lib/PublishParser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getKeys, RULE_METHODS, AnyObject, get, TableSchemaForClient, DBSchemaTable, MethodKey, TableInfo, FullFilter, isObject, Method, DBSchema } from "prostgles-types";
import { AuthResult, SessionUser } from "./AuthHandler";
import { CommonTableRules, Filter, isPlainObject, LocalParams, PRGLIOSocket, TableOrViewInfo, TableSchemaColumn } from "./DboBuilder";
import { Prostgles, DBHandlerServer, DB, TABLE_METHODS } from "./Prostgles";
import { Prostgles, DBHandlerServer, DB, TABLE_METHODS, ProstglesInitOptions } from "./Prostgles";
import type { DBOFullyTyped, PublishFullyTyped } from "./DBSchemaBuilder";

export type PublishMethods<S = void, SUser extends SessionUser = SessionUser> = (params: PublishParams<S, SUser>) => { [key: string]: Method } | Promise<{ [key: string]: Method } | null>;
Expand Down Expand Up @@ -101,6 +101,7 @@ import { FieldFilter, SelectParams } from "prostgles-types";
import { DEFAULT_SYNC_BATCH_SIZE } from "./PubSubManager/PubSubManager";
import { TableHandler } from "./DboBuilder/TableHandler";
import { ViewHandler } from "./DboBuilder/ViewHandler/ViewHandler";
import { parseFieldFilter } from "./DboBuilder/ViewHandler/parseFieldFilter";

export type InsertRequestData = {
data: object | object[]
Expand Down Expand Up @@ -796,4 +797,64 @@ function applyParamsIfFunc(maybeFunc: any, ...params: any): any {
}

return maybeFunc;
}

export async function getFileTableRules (this: PublishParser, socket: PRGLIOSocket, clientInfo: AuthResult | undefined) {
const opts = this.prostgles.opts;
const forcedDeleteFilters: FullFilter<AnyObject, void>[] = [];
const forcedSelectFilters: FullFilter<AnyObject, void>[] = [];
if(opts.fileTable?.referencedTables){
Object.entries(opts.fileTable.referencedTables).forEach(async ([tableName, refCols]) => {
if(isObject(refCols)){
const table_rules = await this.getTableRules({ localParams: { socket }, tableName }, clientInfo);
if(table_rules){
Object.keys(refCols).map(column => {

if(table_rules.delete){
forcedDeleteFilters.push({
$existsJoined: {
path: [{ table: tableName, on: [{ [column]: "id" }] }],
filter: table_rules.delete.forcedFilter ?? {},
}
})
}
if(table_rules.select){
const parsedFields = parseFieldFilter(table_rules.select.fields, false, [column]);
/** Must be allowed to view this column */
if(parsedFields.includes(column as any)){
forcedSelectFilters.push({
$existsJoined: {
path: [{ table: tableName, on: [{ [column]: "id" }] }],
filter: table_rules.select.forcedFilter ?? {},
}
});
}
}

})
}
}
})
}


const fileTableRule: TableRule = {};
if(forcedSelectFilters.length){
fileTableRule.select = {
fields: "*",
forcedFilter: {
$or: forcedSelectFilters
}
}
}
if(forcedDeleteFilters.length){
fileTableRule.delete = {
filterFields: "*",
forcedFilter: {
$or: forcedSelectFilters
}
}
}

return fileTableRule;
}
4 changes: 2 additions & 2 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
@@ -1,6 +1,6 @@
{
"name": "prostgles-server",
"version": "4.1.87",
"version": "4.1.88",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion tests/client/PID.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1782762
2319940
12 changes: 11 additions & 1 deletion tests/isomorphic_queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,14 +946,24 @@ export default async function isomorphic(db: Required<DBHandlerServer> | Require
}]
}]);

const exists = await db[`"""quoted0"""`].find({
const exists1 = await db[`"""quoted0"""`].find({
$existsJoined: {
path: ['"""quoted1"""', '"""quoted2"""'],
filter: {
'"id2"': 1,
}
}
}, { select: "*" });
/** Duplicated tables */
const exists2 = await db[`"""quoted0"""`].find({
$existsJoined: {
path: ['"""quoted1"""', '"""quoted2"""','"""quoted1"""', '"""quoted2"""'],
filter: {
'"id2"': 1,
}
}
}, { select: "*" });
assert.deepStrictEqual(exists1, exists2)
})

await tryRun("Reverse join with agg", async () => {
Expand Down
2 changes: 1 addition & 1 deletion 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 8b327b0

Please sign in to comment.