Skip to content

Commit

Permalink
add checkFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Oct 20, 2023
1 parent 2d0588d commit f73e232
Show file tree
Hide file tree
Showing 33 changed files with 828 additions and 872 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
env:
POSTGRES_USER: api
POSTGRES_PASSWORD: api
POSTGRES_DB: postgres
POSTGRES_DB: prostgles_server_tests
ports:
# will assign a random free host port
- 5432/tcp
Expand Down
65 changes: 31 additions & 34 deletions lib/DboBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,26 @@
import * as Bluebird from "bluebird";

import * as pgPromise from 'pg-promise';
import { runSQL } from "./DboBuilder/runSQL";
import pg = require('pg-promise/typescript/pg-subset');
import { getSchemaFilter, getTablesForSchemaPostgresSQL } from "./DboBuilder/getTablesForSchemaPostgresSQL";
import {
ColumnInfo, SQLOptions,
AnyObject,
ColumnInfo,
DbJoinMaker,
EXISTS_KEY,
PG_COLUMN_UDT_DATA_TYPE,
TS_PG_Types,
TableInfo as TInfo,
ProstglesError,
RawJoinPath,
SQLHandler,
AnyObject,
JoinMaker,
isObject,
getKeys,
ProstglesError,
_PG_geometric,
EXISTS_KEY,
RawJoinPath,
SQLOptions,
TableInfo as TInfo,
TS_PG_Types,
getJoinHandlers,
asName
getKeys,
isObject
} from "prostgles-types";
import { sqlErrCodeToMsg } from "./DboBuilder/sqlErrCodeToMsg"
import { getSchemaFilter, getTablesForSchemaPostgresSQL } from "./DboBuilder/getTablesForSchemaPostgresSQL";
import { runSQL } from "./DboBuilder/runSQL";
import { sqlErrCodeToMsg } from "./DboBuilder/sqlErrCodeToMsg";
import pg = require('pg-promise/typescript/pg-subset');

export type SortItem = {
asc: boolean;
Expand Down Expand Up @@ -92,17 +90,19 @@ export type DBHandlerServer<TH = TableHandlers> =
}


import { clone } from "./utils";
import { FieldSpec, } from "./DboBuilder/QueryBuilder/Functions";
import { JoinPaths, ViewHandler } from "./DboBuilder/ViewHandler/ViewHandler";
import {
Join, Prostgles, DB, ProstglesInitOptions
DB,
Join, Prostgles,
ProstglesInitOptions
} from "./Prostgles";
import { BasicCallback, PubSubManager, pickKeys } from "./PubSubManager/PubSubManager";
import {
PublishParser, PublishAllOrNothing,
PublishAllOrNothing,
PublishParser,
} from "./PublishParser";
import { PubSubManager, asValue, BasicCallback, pickKeys, omitKeys } from "./PubSubManager/PubSubManager";
import { _delete } from "./DboBuilder/delete";
import { JoinPaths, ViewHandler } from "./DboBuilder/ViewHandler/ViewHandler";
import { clone } from "./utils";


type PGP = pgPromise.IMain<{}, pg.IClient>;
Expand Down Expand Up @@ -392,12 +392,12 @@ export type ExistsFilterConfig = {
targetTable: string;
});

import { prepareShortestJoinPaths } from "./DboBuilder/prepareShortestJoinPaths";
import { BasicSession, UserLike } from "./AuthHandler";
import { getDBSchema } from "./DBSchemaBuilder";
import { TableHandler } from "./DboBuilder/TableHandler";
import { ParsedJoinPath, parseJoinPath } from "./DboBuilder/ViewHandler/parseJoinPath";
import { asNameAlias } from "./DboBuilder/QueryBuilder/QueryBuilder";
import { TableHandler } from "./DboBuilder/TableHandler/TableHandler";
import { ParsedJoinPath, parseJoinPath } from "./DboBuilder/ViewHandler/parseJoinPath";
import { prepareShortestJoinPaths } from "./DboBuilder/prepareShortestJoinPaths";

export class DboBuilder {
tablesOrViews?: TableSchema[]; //TableSchema TableOrViewInfo
Expand Down Expand Up @@ -561,7 +561,7 @@ export class DboBuilder {
Alternatively you can rename the table column\n`;
}

this.dbo[tov.escaped_identifier] = new (tov.is_view ? ViewHandler : TableHandler)(this.db, tov, this, undefined, undefined, this.shortestJoinPaths);
this.dbo[tov.escaped_identifier] = new (tov.is_view ? ViewHandler : TableHandler)(this.db, tov, this, undefined, this.shortestJoinPaths);

if (this.shortestJoinPaths && this.shortestJoinPaths.find(jp => [jp.t1, jp.t2].includes(tov.name))) {

Expand Down Expand Up @@ -626,17 +626,14 @@ export class DboBuilder {
}

getTX = (cb: TxCB) => {
return this.db.tx((t) => {
return this.db.tx(t => {
const dbTX: DbTxTableHandlers & Pick<DBHandlerServer, "sql"> = {};
this.tablesOrViews?.map(tov => {
dbTX[tov.name] = new (tov.is_view ? ViewHandler : TableHandler)(this.db, tov, this, t, dbTX, this.shortestJoinPaths);
});
if (!dbTX.sql) {
dbTX.sql = this.runSQL;
}
getKeys(dbTX).map(k => {
dbTX[k]!.dbTX = dbTX;
dbTX[tov.name] = new (tov.is_view ? ViewHandler : TableHandler)(this.db, tov, this, { t, dbTX }, this.shortestJoinPaths);
});
// ???? getKeys(dbTX).map(k => {
// dbTX[k]!.dbTX = dbTX;
// });
dbTX.sql = (q, args, opts, localP) => this.runSQL(q, args, opts, { tx: { dbTX, t }, ...(localP ?? {}) })

return cb(dbTX, t);
Expand Down
2 changes: 1 addition & 1 deletion lib/DboBuilder/QueryBuilder/getNewQuery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { get } from "../../utils";
import { TableHandler } from "../TableHandler";
import { TableHandler } from "../TableHandler/TableHandler";
import { TableRule } from "../../PublishParser";
import { Filter, LocalParams } from "../../DboBuilder";
import { SelectParams, ColumnInfo, getKeys, DetailedJoinSelect, SimpleJoinSelect, JoinPath, JoinSelect, RawJoinPath } from "prostgles-types";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import pgPromise from "pg-promise";
import { AnyObject, asName, DeleteParams, FieldFilter, getKeys, InsertParams, isObject, Select, SelectParams, UpdateParams } from "prostgles-types";
import { DboBuilder, Filter, LocalParams, makeErrorFromPGError, parseError, TableHandlers, TableSchema, withUserRLS } from "../DboBuilder";
import { DB } from "../Prostgles";
import { SyncRule, TableRule } from "../PublishParser";
import { _delete } from "./delete";
import { AnyObject, asName, DeleteParams, FieldFilter, getKeys, InsertParams, isObject, Select, UpdateParams } from "prostgles-types";
import { DboBuilder, Filter, LocalParams, parseError, TableHandlers, TableSchema } from "../../DboBuilder";
import { DB } from "../../Prostgles";
import { SyncRule, TableRule } from "../../PublishParser";
import { _delete } from "../delete";
import { parseUpdateRules } from "../parseUpdateRules";
import { COMPUTED_FIELDS, FUNCTIONS } from "../QueryBuilder/Functions";
import { SelectItem, SelectItemBuilder } from "../QueryBuilder/QueryBuilder";
import { JoinPaths, ViewHandler } from "../ViewHandler/ViewHandler";
import { insert } from "./insert";
import { insertDataParse } from "./insertDataParse";
import { SelectItem, SelectItemBuilder } from "./QueryBuilder/QueryBuilder";
import { update } from "./update";
import { JoinPaths, ViewHandler } from "./ViewHandler/ViewHandler";
import { parseUpdateRules } from "./parseUpdateRules";
import { COMPUTED_FIELDS, FUNCTIONS } from "./QueryBuilder/Functions";
import { updateBatch } from "./updateBatch";


type ValidatedParams = {
Expand All @@ -23,52 +23,29 @@ type ValidatedParams = {

export class TableHandler extends ViewHandler {

constructor(db: DB, tableOrViewInfo: TableSchema, dboBuilder: DboBuilder, t?: pgPromise.ITask<{}>, dbTX?: TableHandlers, joinPaths?: JoinPaths) {
super(db, tableOrViewInfo, dboBuilder, t, dbTX, joinPaths);
constructor(db: DB, tableOrViewInfo: TableSchema, dboBuilder: DboBuilder, tx?: {t: pgPromise.ITask<{}>, dbTX: TableHandlers}, joinPaths?: JoinPaths) {
super(db, tableOrViewInfo, dboBuilder, tx, joinPaths);

this.remove = this.delete;

this.is_view = false;
this.is_media = dboBuilder.prostgles.isMedia(this.name)
}

async updateBatch(data: [Filter, AnyObject][], params?: UpdateParams, tableRules?: TableRule, localParams?: LocalParams): Promise<any> {
try {
await this._log({ command: "updateBatch", localParams, data: { data, params } });
const updateQueries: string[] = await Promise.all(
data.map(async ([filter, data]) =>
(await this.update(
filter,
data,
{ ...(params || {}), returning: undefined },
tableRules,
{ ...(localParams || {}), returnQuery: "noRLS" }
)) as unknown as string
)
);
const queries = [
withUserRLS(localParams, ""),
...updateQueries
];
if(this.t){
const _queries = queries.map(q => this.t!.none(q as unknown as string))
return this.t.batch(_queries)
}
return this.db.tx(t => {
const _queries = queries.map(q => t.none(q as unknown as string))
return t.batch(_queries)
}).catch(err => makeErrorFromPGError(err, localParams, this, []));
} catch (e) {
if (localParams && localParams.testRule) throw e;
throw parseError(e, `dbo.${this.name}.update()`);
}
getFinalDBtx = (localParams: LocalParams | undefined) => {
return localParams?.tx?.dbTX ?? this.tx?.dbTX;
}
getFinalDbo = (localParams: LocalParams | undefined) => {
return this.getFinalDBtx(localParams) ?? this.dboBuilder.dbo;
}


parseUpdateRules = parseUpdateRules.bind(this);

update = update.bind(this);
updateBatch = updateBatch.bind(this);

validateNewData({ row, forcedData, allowedFields, tableRules, fixIssues = false }: ValidatedParams): { data: any; allowedCols: string[] } {
validateNewData({ row, forcedData, allowedFields, tableRules, fixIssues = false }: ValidatedParams) {
const synced_field = (tableRules ?? {})?.sync?.synced_field;

/* Update synced_field if sync is on and missing */
Expand All @@ -94,8 +71,7 @@ export class TableHandler extends ViewHandler {

return { data, allowedCols: this.columns.filter(c => dataKeys.includes(c.name)).map(c => c.name) }
}

insertDataParse = insertDataParse;

async insert(
rowOrRows: (AnyObject | AnyObject[]), param2?: InsertParams,
param3_unused?: undefined, tableRules?: TableRule, _localParams?: LocalParams
Expand Down Expand Up @@ -124,7 +100,7 @@ export class TableHandler extends ViewHandler {
}

makeReturnQuery(items?: SelectItem[]) {
if (items?.length) return " RETURNING " + items.map(s => s.getQuery() + " AS " + asName(s.alias)).join(", ");
if (items?.length) return " RETURNING " + getSelectItemQuery(items);
return "";
}

Expand All @@ -151,7 +127,7 @@ export class TableHandler extends ViewHandler {
}

/* Do it within a transaction to ensure consisency */
if (!this.t) {
if (!this.tx) {
return this.dboBuilder.getTX(dbTX => _upsert(dbTX[this.name] as TableHandler))
} else {
return _upsert(this);
Expand All @@ -176,7 +152,7 @@ export class TableHandler extends ViewHandler {

if (!table_rules || !table_rules.sync || !table_rules.select) throw "sync or select table rules missing";

if (this.t) throw "Sync not allowed within transactions";
if (this.tx) throw "Sync not allowed within transactions";

const ALLOWED_PARAMS = ["select"];
const invalidParams = Object.keys(params || {}).filter(k => !ALLOWED_PARAMS.includes(k));
Expand Down Expand Up @@ -263,4 +239,6 @@ export class TableHandler extends ViewHandler {
*/
}

}
}

export const getSelectItemQuery = (items: SelectItem[]) => items.map(s => s.getQuery() + " AS " + asName(s.alias)).join(", ")
Loading

0 comments on commit f73e232

Please sign in to comment.