Skip to content

Commit

Permalink
add tables in onReady
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Jan 6, 2024
1 parent 259a4f3 commit 427b8e0
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/DBSchemaBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export type PublishFullyTyped<Schema = void> = Schema extends DBSchema? (

return "*" as const
},
onReady: (dbo) => {
onReady: ({ dbo }) => {
dbo.tdwa?.find!()
}
});
Expand Down
9 changes: 9 additions & 0 deletions lib/DboBuilder/DboBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from "../Prostgles";
import { PubSubManager } from "../PubSubManager/PubSubManager";
import {
DbTableInfo,
PublishParser
} from "../PublishParser/PublishParser";
import { Graph } from "../shortestPath";
Expand Down Expand Up @@ -74,6 +75,14 @@ export class DboBuilder {

queryStreamer: QueryStreamer;

get tables(): DbTableInfo[] {
return (this.tablesOrViews ?? []).map(({ name, columns }) => ({
name,
columns,
info: this.dbo[name]!.tableOrViewInfo!
}));
}

getPubSubManager = async (): Promise<PubSubManager> => {
if (!this._pubSubManager) {
let onSchemaChange;
Expand Down
2 changes: 1 addition & 1 deletion lib/Prostgles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ export class Prostgles {
return { fileName, fullPath }
}

private getFileText(fullPath: string, format = "utf8"): Promise<string> {
private getFileText(fullPath: string, _format = "utf8"): Promise<string> {
return new Promise((resolve, reject) => {
fs.readFile(fullPath, 'utf8', function (err, data) {
if (err) reject(err);
Expand Down
6 changes: 1 addition & 5 deletions lib/PublishParser/PublishParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ export class PublishParser {
dbo: this.dbo,
db: this.db,
socket: localParams.socket!,
tables: (this.prostgles.dboBuilder.tablesOrViews ?? []).map(({ name, columns }) => ({
name,
columns,
info: this.dbo[name]!.tableOrViewInfo!
}))
tables: this.prostgles.dboBuilder.tables,
}
}

Expand Down
12 changes: 6 additions & 6 deletions lib/PublishParser/publishTypesAndUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,18 +373,18 @@ export type ParsedPublishTable = {
subscribe?: SubscribeRule;
subscribeOne?: SubscribeRule;
}

export type DbTableInfo = {
name: string;
info: TableOrViewInfo;
columns: TableSchemaColumn[];
}
export type PublishParams<S = void, SUser extends SessionUser = SessionUser> = {
sid?: string;
dbo: DBOFullyTyped<S>;
db: DB;
user?: SUser["user"];
socket: PRGLIOSocket;
tables: {
name: string;
info: TableOrViewInfo;
columns: TableSchemaColumn[];
}[];
tables: DbTableInfo[];
}
export type RequestParams = { dbo?: DBHandlerServer, socket?: any };
export type PublishAllOrNothing = true | "*" | false | null;
Expand Down
40 changes: 29 additions & 11 deletions lib/initProstgles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import AuthHandler from "./AuthHandler";
import { DBEventsManager } from "./DBEventsManager";
import { DBOFullyTyped } from "./DBSchemaBuilder";
import { DBHandlerServer, Prostgles, ProstglesInitOptions, isSuperUser } from "./Prostgles";
import { PublishParser } from "./PublishParser/PublishParser";
import { DbTableInfo, PublishParser } from "./PublishParser/PublishParser";
import { sleep } from "./utils";

export type DbConnection = string | pg.IConnectionParameters<pg.IClient>;
Expand All @@ -16,12 +16,29 @@ export type PGP = pgPromise.IMain<{}, pg.IClient>;
export type DB = pgPromise.IDatabase<{}, pg.IClient>;

export type OnInitReason =
| { type: "schema change"; query: string; command: string; }
| {
type: "schema change";
query: string;
command: string;
}
| {
type: "init" | "prgl.restart" | "prgl.update" | "TableConfig"
}
export type OnReadyCallback<S = void> = (dbo: DBOFullyTyped<S>, db: DB, reason: OnInitReason) => any;
export type OnReadyCallbackBasic = (dbo: DBHandlerServer, db: DB, reason: OnInitReason) => any;
};

type OnReadyParamsCommon = {
db: DB;
tables: DbTableInfo[];
reason: OnInitReason;
}
type OnReadyParamsBasic = OnReadyParamsCommon & {
dbo: DBHandlerServer;
}
type OnReadyParams<S> = OnReadyParamsCommon & {
dbo: DBOFullyTyped<S>;
}

export type OnReadyCallback<S = void> = (params: OnReadyParams<S>) => any;
export type OnReadyCallbackBasic = (params: OnReadyParamsBasic) => any;

export type InitResult = {
db: DBOFullyTyped;
Expand Down Expand Up @@ -100,7 +117,12 @@ export const initProstgles = async function(this: Prostgles, onReady: OnReadyCal
if (this.destroyed) {
console.trace(1)
}
onReady(this.dbo as any, this.db, { type: "init" });
onReady({
dbo: this.dbo as any,
db: this.db,
tables: this.dboBuilder.tables,
reason: { type: "init" }
});
} catch (err) {
console.error("Prostgles: Error within onReady: \n", err)
}
Expand Down Expand Up @@ -147,11 +169,7 @@ export const initProstgles = async function(this: Prostgles, onReady: OnReadyCal
}
if(this.opts.io.engine.constructor.name === 'Server'){
this.opts.io.engine.close();
}
// if (typeof this.opts.io.close === "function") {
// this.opts.io.close();
// console.log("this.io.close")
// }
}
}
this.fileManager?.destroy();
this.dboBuilder?.destroy();
Expand Down
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.154",
"version": "4.2.1",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
8 changes: 4 additions & 4 deletions tests/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ prostgles<DBSchemaGenerated>({
type: "many-many"
}
],
onReady: async (db, _db) => {
onReady: async ({ dbo, db }) => {
log("prostgles onReady");

try {
Expand All @@ -232,17 +232,17 @@ prostgles<DBSchemaGenerated>({

} else if(process.env.TEST_TYPE === "server"){

await server_only_queries(db as any);
await server_only_queries(dbo as any);
log("Server-only query tests successful");
await isomorphic(db as any, log);
await isomorphic(dbo as any, log);
log("Server isomorphic tests successful");

stopTest()
}
} catch(err) {
console.trace(err)
if(process.env.TEST_TYPE){
stopTest(err)
stopTest(err ?? "Error")
}
}

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.

3 changes: 2 additions & 1 deletion tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ fi

npm run build

npm run test-server 2>&1 ./server.log
#npm run test-server 2>&1 ./server.log
npm run test-server && \
TEST_NAME="main" npm run test-client && \
TEST_NAME="files" npm run test-client && \
TEST_NAME="rest_api" npm run test-client
Expand Down

0 comments on commit 427b8e0

Please sign in to comment.