Skip to content

Commit

Permalink
remove appCheck WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Jun 21, 2024
1 parent c0f5549 commit 92196d2
Show file tree
Hide file tree
Showing 24 changed files with 791 additions and 433 deletions.
5 changes: 2 additions & 3 deletions lib/DboBuilder/DboBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
} from "prostgles-types";
import { getDBSchema } from "../DBSchemaBuilder";
import {
DB,
Join, Prostgles
DB, Prostgles
} from "../Prostgles";
import { PubSubManager } from "../PubSubManager/PubSubManager";
import {
Expand All @@ -30,7 +29,7 @@ import { PGConstraint, getCanExecute, getConstraints, getSerializedClientErrorFr
import { getTablesForSchemaPostgresSQL } from "./getTablesForSchemaPostgresSQL";
import { prepareShortestJoinPaths } from "./prepareShortestJoinPaths";
import { runSQL } from "./runSQL";
import { PostgresNotifListenManager } from "../PostgresNotifListenManager";
import { Join } from "../ProstglesTypes";

export * from "./DboBuilderTypes";
export * from "./dboBuilderUtils";
Expand Down
3 changes: 2 additions & 1 deletion lib/DboBuilder/ViewHandler/ViewHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
isObject
} from "prostgles-types";
import { TableEvent } from "../../Logging";
import { DB, Join } from "../../Prostgles";
import { DB } from "../../Prostgles";
import { TableRule } from "../../PublishParser/PublishParser";
import { Graph } from "../../shortestPath";
import {
Expand All @@ -31,6 +31,7 @@ import { prepareWhere } from "./prepareWhere";
import { size } from "./size";
import { LocalFuncs, subscribe } from "./subscribe";
import { validateViewRules } from "./validateViewRules";
import { Join } from "../../ProstglesTypes";

export type JoinPaths = {
t1: string;
Expand Down
2 changes: 1 addition & 1 deletion lib/DboBuilder/dboBuilderUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
} from "prostgles-types";
import {
DB,
ProstglesInitOptions
} from "../Prostgles";
import { pickKeys } from "../PubSubManager/PubSubManager";
import { LocalParams, SortItem, pgp } from "./DboBuilderTypes";
Expand All @@ -23,6 +22,7 @@ import { ViewHandler } from "./ViewHandler/ViewHandler";
import { getSchemaFilter } from "./getTablesForSchemaPostgresSQL";

import { sqlErrCodeToMsg } from "./sqlErrCodeToMsg";
import { ProstglesInitOptions } from "../ProstglesTypes";
export function escapeTSNames(str: string, capitalize = false): string {
let res = str;
res = (capitalize ? str[0]?.toUpperCase() : str[0]) + str.slice(1);
Expand Down
6 changes: 4 additions & 2 deletions lib/DboBuilder/getTablesForSchemaPostgresSQL.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { SQLResult, asName } from "prostgles-types";
import { omitKeys, tryCatch } from "prostgles-types/dist/util";
import { DboBuilder } from "../DboBuilder/DboBuilder";
import { DBorTx, ProstglesInitOptions } from "../Prostgles";
import { DBorTx } from "../Prostgles";
import { clone } from "../utils";
import { TableSchema, TableSchemaColumn } from "./DboBuilderTypes";
import { ProstglesInitOptions } from "../ProstglesTypes";

const getMaterialViews = (db: DBorTx, schema: ProstglesInitOptions["schema"]) => {
const { sql, schemaNames } = getSchemaFilter(schema);
Expand Down Expand Up @@ -109,7 +110,8 @@ export const getSchemaFilter = (schema: ProstglesInitOptions["schema"] = { publi
}
}

// TODO: Add a onSocketConnect timeout for this query. Reason: this query gets blocked by prostgles.app_triggers from PubSubManager.addTrigger in some cases (pg_dump locks that table)
// TODO: Add a onSocketConnect timeout for this query.
// Reason: this query gets blocked by prostgles.app_triggers from PubSubManager.addTrigger in some cases (pg_dump locks that table)
export async function getTablesForSchemaPostgresSQL(
{ db, runSQL }: DboBuilder,
schema: ProstglesInitOptions["schema"]
Expand Down
2 changes: 1 addition & 1 deletion lib/DboBuilder/prepareShortestJoinPaths.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DboBuilder } from "../DboBuilder/DboBuilder";
import { JOIN_TYPES, Join } from "../Prostgles";
import { JOIN_TYPES, Join } from "../ProstglesTypes";
import { Graph, findShortestPath } from "../shortestPath";
import { TableSchema } from "./DboBuilderTypes";
import { JoinPaths } from "./ViewHandler/ViewHandler";
Expand Down
210 changes: 13 additions & 197 deletions lib/Prostgles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,227 +4,40 @@
*--------------------------------------------------------------------------------------------*/

import * as pgPromise from 'pg-promise';
import { Auth, AuthHandler, AuthRequestParams, SessionUser } from "./AuthHandler";
import { EventTriggerTagFilter } from "./Event_Trigger_Tags";
import { CloudClient, FileManager, ImageOptions, LocalConfig } from "./FileManager/FileManager";
import { AuthHandler } from "./AuthHandler";
import { FileManager } from "./FileManager/FileManager";
import { SchemaWatch } from "./SchemaWatch/SchemaWatch";
import { DbConnection, DbConnectionOpts, OnInitReason, OnReadyCallback, initProstgles } from "./initProstgles";
import { OnInitReason, initProstgles } from "./initProstgles";
import { clientCanRunSqlRequest, runClientMethod, runClientRequest, runClientSqlRequest } from "./runClientRequest";
import pg = require('pg-promise/typescript/pg-subset');
const { version } = require('../package.json');

import { ExpressApp, RestApi, RestApiConfig } from "./RestApi";
import TableConfigurator, { TableConfig } from "./TableConfig/TableConfig";
import type { ProstglesInitOptions } from "./ProstglesTypes";
import { RestApi } from "./RestApi";
import TableConfigurator from "./TableConfig/TableConfig";

import { DBHandlerServer, DboBuilder, LocalParams, PRGLIOSocket, getErrorAsObject } from "./DboBuilder/DboBuilder";
export { DBHandlerServer };
export type PGP = pgPromise.IMain<{}, pg.IClient>;

import {
AnyObject,
CHANNELS,
ClientSchema,
DBSchemaTable, FileColumnConfig,
DBSchemaTable,
SQLRequest, TableSchemaForClient,
getKeys,
isObject, omitKeys, tryCatch
} from "prostgles-types";
import type { Server } from "socket.io";
import { DBEventsManager } from "./DBEventsManager";
import { Publish, PublishMethods, PublishParams, PublishParser } from "./PublishParser/PublishParser";
import { PublishParser } from "./PublishParser/PublishParser";

export type DB = pgPromise.IDatabase<{}, pg.IClient>;
export type DBorTx = DB | pgPromise.ITask<{}>


export const TABLE_METHODS = ["update", "find", "findOne", "insert", "delete", "upsert"] as const;

export const JOIN_TYPES = ["one-many", "many-one", "one-one", "many-many"] as const;
export type Join = {
tables: [string, string];
on: { [key: string]: string }[]; // Allow multi references to table
type: typeof JOIN_TYPES[number];
};
export type Joins = Join[] | "inferred";


type Keywords = {
$and: string;
$or: string;
$not: string;
};

export type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};
// export type I18N_CONFIG<LANG_IDS = { en: 1, fr: 1 }> = {
// fallbackLang: keyof LANG_IDS;
// column_labels?: DeepPartial<{
// [table_name: string]: {
// [column_name: string]: {
// [lang_id in keyof LANG_IDS]: string
// }
// }
// }>;
// }

/**
* Allows uploading and downloading files.
* Currently supports only S3.
*
* @description
* Will create a media table that contains file metadata and urls
* Inserting a file into this table through prostgles will upload it to S3 and insert the relevant metadata into the media table
* Requesting a file from HTTP GET {fileUrlPath}/{fileId} will:
* 1. check auth (if provided)
* 2. check the permissions in publish (if provided)
* 3. redirect the request to the signed url (if allowed)
*
* Specifying referencedTables will:
* 1. create a column in that table called media
* 2. create a lookup table lookup_media_{referencedTable} that joins referencedTable to the media table
*/
export type FileTableConfig = {
tableName?: string; /* defaults to 'media' */

/**
* GET path used in serving media. defaults to /${tableName}
*/
fileServeRoute?: string;

cloudClient?: CloudClient;
localConfig?: LocalConfig;

/**
* If defined the the files will not be deleted immediately
* Instead, the "deleted" field will be updated to the current timestamp and after the day interval provided in "deleteAfterNDays" the files will be deleted
* "checkIntervalMinutes" is the frequency in hours at which the files ready for deletion are deleted
*/
delayedDelete?: {
/**
* Minimum amount of time measured in days for which the files will not be deleted after requesting delete
*/
deleteAfterNDays: number;
/**
* How freuquently the files will be checked for deletion delay
*/
checkIntervalHours?: number;
}
expressApp: ExpressApp;
referencedTables?: {
[tableName: string]:

/**
* If defined then will try to create (if necessary) these columns which will reference files_table(id)
* Prostgles UI will use these hints (obtained through tableHandler.getInfo())
* */
| { type: "column", referenceColumns: Record<string, FileColumnConfig> }
},
imageOptions?: ImageOptions
};

export type OnSchemaChangeCallback = ((event: { command: string; query: string }) => void);

export type ProstglesInitOptions<S = void, SUser extends SessionUser = SessionUser> = {
dbConnection: DbConnection;
dbOptions?: DbConnectionOpts;
tsGeneratedTypesDir?: string;
disableRealtime?: boolean;
io?: Server;
publish?: Publish<S, SUser>;
/**
* If true then will test all table methods on each socket connect
*/
testRulesOnConnect?: boolean;
publishMethods?: PublishMethods<S, SUser>;
publishRawSQL?(params: PublishParams<S, SUser>): ((boolean | "*") | Promise<(boolean | "*")>);
joins?: Joins;
schema?: Record<string, 1> | Record<string, 0>;
sqlFilePath?: string;
onReady: OnReadyCallback<S>;
transactions?: string | boolean;
wsChannelNamePrefix?: string;
/**
* Use for connection verification. Will disconnect socket on any errors
*/
onSocketConnect?: (args: AuthRequestParams<S, SUser> & { socket: PRGLIOSocket }) => void | Promise<void>;
onSocketDisconnect?: (args: AuthRequestParams<S, SUser> & { socket: PRGLIOSocket }) => void | Promise<void>;
auth?: Auth<S, SUser>;
DEBUG_MODE?: boolean;
watchSchemaType?:

/**
* Will set database event trigger for schema changes. Requires superuser
* Default
*/
| "DDL_trigger"

/**
* Will check client queries for schema changes
* fallback if DDL not possible
*/
| "prostgles_queries"

/**
* If truthy then DBoGenerated.d.ts will be updated and "onReady" will be called with new schema on both client and server
*/
watchSchema?:
/**
* Will listen only to few events (create table/view)
*/
| boolean

/**
* Will listen to specified events (or all if "*" is specified)
*/
| EventTriggerTagFilter

/**
* Will only rewrite the DBoGenerated.d.ts found in tsGeneratedTypesDir
* This is meant to be used in development when server restarts on file change
*/
| "hotReloadMode"

/**
* Function called when schema changes. Nothing else triggered
*/
| OnSchemaChangeCallback;

keywords?: Keywords;
onNotice?: (notice: AnyObject, message?: string) => void;
fileTable?: FileTableConfig;
restApi?: RestApiConfig;
/**
* Creates tables and provides UI labels, autocomplete and hints for a given json structure
*/
tableConfig?: TableConfig;
tableConfigMigrations?: {
/**
* If false then prostgles won't start on any tableConfig error
* true by default
*/
silentFail?: boolean;

version: number;
/** Table that will contain the schema version number and the tableConfig
* Defaults to schema_version
*/
versionTableName?: string;
/**
* Script run before tableConfig is loaded IF an older schema_version is present
*/
onMigrate: (args: {
db: DB;
oldVersion: number | undefined;
getConstraints: (
table: string,
column?: string,
types?: ColConstraint["type"][]
) => Promise<ColConstraint[]>
}) => void;
};
onLog?: (evt: EventInfo) => Promise<void>;
}

/*
1. Connect to db
Expand All @@ -246,11 +59,14 @@ const DEFAULT_KEYWORDS = {
$not: "$not"
};

import { randomUUID } from "crypto";
import * as fs from 'fs';
import { EventInfo } from "./Logging";
import { ColConstraint } from "./TableConfig/getConstraintDefinitionQueries";

export class Prostgles {
/**
* Used facilitate concurrent prostgles connections to the same database
*/
readonly appId = randomUUID();
opts: ProstglesInitOptions = {
DEBUG_MODE: false,
dbConnection: {
Expand Down
Loading

0 comments on commit 92196d2

Please sign in to comment.