prostgles-server 4.2.598 → 4.2.600

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +26 -0
  2. package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.external.fixture.d.ts +2 -2
  3. package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.fixture.d.ts +8 -8
  4. package/dist/DboBuilder/TableHandler/TableHandler.d.ts +8 -5
  5. package/dist/DboBuilder/TableHandler/TableHandler.d.ts.map +1 -1
  6. package/dist/DboBuilder/TableHandler/TableHandler.js +1 -0
  7. package/dist/DboBuilder/TableHandler/TableHandler.js.map +1 -1
  8. package/dist/DboBuilder/TableHandler/delete.d.ts.map +1 -1
  9. package/dist/DboBuilder/TableHandler/delete.js +1 -0
  10. package/dist/DboBuilder/TableHandler/delete.js.map +1 -1
  11. package/dist/DboBuilder/TableHandler/executeAfterHooksCheckAndPostValidation.d.ts.map +1 -1
  12. package/dist/DboBuilder/TableHandler/executeAfterHooksCheckAndPostValidation.js +2 -0
  13. package/dist/DboBuilder/TableHandler/executeAfterHooksCheckAndPostValidation.js.map +1 -1
  14. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts +2 -2
  15. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts.map +1 -1
  16. package/dist/DboBuilder/ViewHandler/ViewHandler.js.map +1 -1
  17. package/dist/Prostgles.d.ts +13 -8
  18. package/dist/Prostgles.d.ts.map +1 -1
  19. package/dist/Prostgles.js +57 -4
  20. package/dist/Prostgles.js.map +1 -1
  21. package/dist/ProstglesContext.spec.d.ts +2 -0
  22. package/dist/ProstglesContext.spec.d.ts.map +1 -0
  23. package/dist/ProstglesContext.spec.js +73 -0
  24. package/dist/ProstglesContext.spec.js.map +1 -0
  25. package/dist/ProstglesTypes.d.ts +11 -5
  26. package/dist/ProstglesTypes.d.ts.map +1 -1
  27. package/dist/PublishParser/PublishParser.d.ts +2 -2
  28. package/dist/PublishParser/PublishParser.d.ts.map +1 -1
  29. package/dist/PublishParser/PublishParser.js +2 -0
  30. package/dist/PublishParser/PublishParser.js.map +1 -1
  31. package/dist/PublishParser/defineServerFunction.d.ts +21 -16
  32. package/dist/PublishParser/defineServerFunction.d.ts.map +1 -1
  33. package/dist/PublishParser/defineServerFunction.js +7 -1
  34. package/dist/PublishParser/defineServerFunction.js.map +1 -1
  35. package/dist/PublishParser/defineServerFunction.spec.js +38 -0
  36. package/dist/PublishParser/defineServerFunction.spec.js.map +1 -1
  37. package/dist/PublishParser/publishTypesAndUtils.d.ts +6 -3
  38. package/dist/PublishParser/publishTypesAndUtils.d.ts.map +1 -1
  39. package/dist/PublishParser/publishTypesAndUtils.js.map +1 -1
  40. package/dist/StorageClient/getFileTableConfig.d.ts +1 -1
  41. package/dist/StorageClient/getFileTableConfig.d.ts.map +1 -1
  42. package/dist/StorageClient/getFileTableConfig.js.map +1 -1
  43. package/dist/TableConfig/TableConfigTypes.d.ts.map +1 -1
  44. package/dist/TableConfig/initTableConfig.js +3 -3
  45. package/dist/TableHooks/TableHooks.d.ts +26 -20
  46. package/dist/TableHooks/TableHooks.d.ts.map +1 -1
  47. package/dist/index.d.ts +5 -2
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +6 -0
  50. package/dist/index.js.map +1 -1
  51. package/dist/initProstgles.d.ts +22 -13
  52. package/dist/initProstgles.d.ts.map +1 -1
  53. package/dist/initProstgles.js +11 -2
  54. package/dist/initProstgles.js.map +1 -1
  55. package/dist/runClientRequest.d.ts.map +1 -1
  56. package/dist/runClientRequest.js.map +1 -1
  57. package/dist/updateConfiguration.d.ts +1 -1
  58. package/dist/updateConfiguration.d.ts.map +1 -1
  59. package/dist/updateConfiguration.js.map +1 -1
  60. package/lib/DboBuilder/TableHandler/TableHandler.ts +2 -1
  61. package/lib/DboBuilder/TableHandler/delete.ts +1 -0
  62. package/lib/DboBuilder/TableHandler/executeAfterHooksCheckAndPostValidation.ts +2 -0
  63. package/lib/DboBuilder/ViewHandler/ViewHandler.ts +2 -2
  64. package/lib/Prostgles.ts +63 -8
  65. package/lib/ProstglesContext.spec.ts +76 -0
  66. package/lib/ProstglesTypes.ts +16 -5
  67. package/lib/PublishParser/PublishParser.ts +6 -4
  68. package/lib/PublishParser/defineServerFunction.spec.ts +48 -0
  69. package/lib/PublishParser/defineServerFunction.ts +61 -21
  70. package/lib/PublishParser/publishTypesAndUtils.ts +6 -3
  71. package/lib/StorageClient/getFileTableConfig.ts +2 -2
  72. package/lib/TableConfig/TableConfigTypes.ts +2 -13
  73. package/lib/TableConfig/initTableConfig.ts +3 -3
  74. package/lib/TableHooks/TableHooks.ts +33 -20
  75. package/lib/index.ts +25 -5
  76. package/lib/initProstgles.ts +57 -19
  77. package/lib/runClientRequest.ts +6 -2
  78. package/lib/updateConfiguration.ts +1 -1
  79. package/package.json +2 -2
  80. package/tsconfig.json +2 -1
@@ -1,6 +1,6 @@
1
1
  import type pgPromise from "pg-promise";
2
2
  import type pg from "pg-promise/typescript/pg-subset";
3
- import type { SQLHandler, TableSchema } from "prostgles-types";
3
+ import type { MaybePromise, SQLHandler, TableSchema } from "prostgles-types";
4
4
  import type { AuthClientRequest, SessionUser } from "./Auth/AuthTypes";
5
5
  import { removeExpressRoutesTest } from "./Auth/utils/removeExpressRoute";
6
6
  import { DBEventsManager } from "./DBEventsManager";
@@ -31,8 +31,12 @@ export type DbConnectionOpts = pg.IDefaults;
31
31
  export type PGP = pgPromise.IMain<{}, pg.IClient>;
32
32
  export type DB = pgPromise.IDatabase<{}, pg.IClient>;
33
33
 
34
- export type UpdatableOptions<S = void, SUser extends SessionUser = SessionUser> = Pick<
35
- ProstglesInitOptions<S, SUser>,
34
+ export type UpdatableOptions<
35
+ S = void,
36
+ SUser extends SessionUser = SessionUser,
37
+ Context = undefined,
38
+ > = Pick<
39
+ ProstglesInitOptions<S, SUser, Context>,
36
40
  | "io"
37
41
  | "fileTable"
38
42
  | "restApi"
@@ -46,6 +50,7 @@ export type UpdatableOptions<S = void, SUser extends SessionUser = SessionUser>
46
50
  | "tsGeneratedTypesDir"
47
51
  | "tsGeneratedTypesFunctionsPath"
48
52
  | "modifyClientSchema"
53
+ | "createContext"
49
54
  >;
50
55
  export type OnInitReason =
51
56
  | {
@@ -55,10 +60,13 @@ export type OnInitReason =
55
60
  }
56
61
  | {
57
62
  type: "prgl.update";
58
- newOpts: Omit<UpdatableOptions, (typeof clientOnlyUpdateKeys)[number]>;
63
+ newOpts: Omit<
64
+ UpdatableOptions<void, SessionUser, any>,
65
+ (typeof clientOnlyUpdateKeys)[number]
66
+ >;
59
67
  }
60
68
  | {
61
- type: "init" | "prgl.restart" | "TableConfig";
69
+ type: "init" | "prgl.restart" | "TableConfig" | "dbo.refresh";
62
70
  };
63
71
 
64
72
  type OnReadyParamsCommon = {
@@ -69,26 +77,46 @@ type OnReadyParamsCommon = {
69
77
  };
70
78
  export type OnReadyParamsBasic = OnReadyParamsCommon & {
71
79
  dbo: DBHandlerServer;
80
+ context: any;
72
81
  };
73
- export type OnReadyParams<S> = OnReadyParamsCommon & {
82
+ export type OnReadyParams<S, Context = undefined> = OnReadyParamsCommon & {
74
83
  dbo: DBOFullyTyped<S>;
84
+ context: Context;
75
85
  };
76
86
 
77
- export type OnReadyCallback<S, SUser extends SessionUser> = (
78
- params: OnReadyParams<S>,
79
- update: (newOpts: UpdatableOptions<S, SUser>, force?: true) => Promise<void>,
87
+ export type ContextCleanup = () => MaybePromise<void>;
88
+ export type CreateContextParams<S> = OnReadyParamsCommon & {
89
+ dbo: DBOFullyTyped<S>;
90
+ onCleanup: (cleanup: ContextCleanup) => void;
91
+ };
92
+ export type CreateContext<S, Context> = (
93
+ params: CreateContextParams<S>,
94
+ ) => MaybePromise<Context>;
95
+
96
+ export type OnReadyCallback<
97
+ S,
98
+ SUser extends SessionUser,
99
+ Context = undefined,
100
+ > = (
101
+ params: OnReadyParams<S, Context>,
102
+ update: (newOpts: UpdatableOptions<S, SUser, Context>, force?: true) => Promise<void>,
80
103
  ) => void | Promise<void>;
81
104
  export type OnReadyCallbackBasic = (
82
105
  params: OnReadyParamsBasic,
83
- update: (newOpts: UpdatableOptions<void, SessionUser>, force?: true) => Promise<void>,
106
+ update: (newOpts: UpdatableOptions<void, SessionUser, any>, force?: true) => Promise<void>,
84
107
  ) => void | Promise<void>;
85
108
 
86
- export type InitResult<S = void, SUser extends SessionUser = SessionUser> = {
109
+ export type InitResult<
110
+ S = void,
111
+ SUser extends SessionUser = SessionUser,
112
+ Context = undefined,
113
+ > = {
87
114
  db: DBOFullyTyped<S>;
115
+ context: Context;
88
116
  sql: SQLHandler;
89
117
  _db: DB;
90
118
  pgp: PGP;
91
- io: ProstglesInitOptions<S, SUser>["io"];
119
+ io: ProstglesInitOptions<S, SUser, Context>["io"];
92
120
  destroy: () => Promise<boolean>;
93
121
  /**
94
122
  * Generated database public schema TS types for all tables and views
@@ -96,9 +124,9 @@ export type InitResult<S = void, SUser extends SessionUser = SessionUser> = {
96
124
  getTSSchema: typeof DboBuilder.prototype.getTsDefinitions;
97
125
  getSchema: typeof DboBuilder.prototype.getSchema;
98
126
  reWriteDBSchema: () => void;
99
- update: (newOpts: UpdatableOptions<S, SUser>, force?: true) => Promise<void>;
100
- restart: () => Promise<InitResult<S, SUser>>;
101
- options: ProstglesInitOptions<S, SUser>;
127
+ update: (newOpts: UpdatableOptions<S, SUser, Context>, force?: true) => Promise<void>;
128
+ restart: () => Promise<InitResult<S, SUser, Context>>;
129
+ options: ProstglesInitOptions<S, SUser, Context>;
102
130
  getClientDBHandlers: (
103
131
  clientReq: AuthClientRequest,
104
132
  scope: PermissionScope | undefined,
@@ -111,7 +139,7 @@ export const initProstgles = async function (
111
139
  this: Prostgles,
112
140
  onReady: OnReadyCallbackBasic,
113
141
  reason: OnInitReason,
114
- ): Promise<InitResult> {
142
+ ): Promise<InitResult<void, SessionUser, any>> {
115
143
  this.loaded = false;
116
144
  const expressApp =
117
145
  this.opts.fileTable?.expressApp ??
@@ -191,11 +219,14 @@ export const initProstgles = async function (
191
219
  const pgp = this.pgp!;
192
220
 
193
221
  try {
222
+ await this.cleanupContext();
223
+
194
224
  /* 2. Execute any SQL file if provided */
195
225
  await runSQLFile(this);
196
- await this.refreshDBO();
226
+ await this.rebuildDBO();
197
227
  await this.initTableConfig(reason);
198
- await this.refreshDBO();
228
+ await this.rebuildDBO();
229
+ await this.createContext(reason);
199
230
  this.initRestApi();
200
231
 
201
232
  this.schemaWatch = await SchemaWatch.create(this.dboBuilder);
@@ -233,6 +264,7 @@ export const initProstgles = async function (
233
264
  db: this.db,
234
265
  tables: this.dboBuilder.tables,
235
266
  reason,
267
+ context: this.context,
236
268
  },
237
269
  async (...args) => {
238
270
  await updateConfiguration(this, onReady, ...args);
@@ -243,8 +275,12 @@ export const initProstgles = async function (
243
275
  }
244
276
 
245
277
  this.loaded = true;
246
- const initResult: InitResult = {
278
+ const getContext = () => this.context;
279
+ const initResult: InitResult<void, SessionUser, any> = {
247
280
  db: this.dbo as DBOFullyTyped,
281
+ get context() {
282
+ return getContext();
283
+ },
248
284
  sql: this.dboBuilder.sql,
249
285
  _db: db,
250
286
  pgp,
@@ -276,6 +312,7 @@ export const initProstgles = async function (
276
312
  this.opts.io.engine.close();
277
313
  }
278
314
  }
315
+ await this.cleanupContext();
279
316
  await this.dboBuilder.destroy();
280
317
  this.authHandler.destroy();
281
318
  await this.tableConfigurator?.destroy();
@@ -293,6 +330,7 @@ export const initProstgles = async function (
293
330
 
294
331
  return initResult;
295
332
  } catch (e: any) {
333
+ await this.cleanupContext();
296
334
  console.trace(e);
297
335
  throw "init issues: " + (e as Error).toString();
298
336
  }
@@ -2,7 +2,6 @@ import type { SQLRequest, TableHandler, UserLike } from "prostgles-types";
2
2
  import {
3
3
  ABORTABLE_METHODS,
4
4
  getJSONBObjectSchemaValidationError,
5
- getJSONBSchemaValidationError,
6
5
  getJSONBSchemaValidationErrorAsync,
7
6
  getKeys,
8
7
  pickKeys,
@@ -242,6 +241,7 @@ type ArgsMethod = {
242
241
  name: unknown;
243
242
  input?: unknown;
244
243
  };
244
+
245
245
  export const runClientMethod = async function (
246
246
  this: Prostgles,
247
247
  unvalidatedArgs: ArgsMethod,
@@ -274,7 +274,11 @@ export const runClientMethod = async function (
274
274
 
275
275
  const expectedArgsError =
276
276
  !inputSchema ? undefined : (
277
- await getJSONBSchemaValidationErrorAsync({ type: inputSchema }, input, this.dboBuilder.dboMap)
277
+ await getJSONBSchemaValidationErrorAsync(
278
+ { type: inputSchema },
279
+ input,
280
+ this.dboBuilder.dboMap as Map<string, TableHandler>,
281
+ )
278
282
  );
279
283
 
280
284
  if (expectedArgsError?.error !== undefined) {
@@ -5,7 +5,7 @@ import type { Prostgles } from "./Prostgles";
5
5
  export const updateConfiguration = async (
6
6
  prgl: Prostgles,
7
7
  onReady: OnReadyCallbackBasic,
8
- newOpts: UpdatableOptions,
8
+ newOpts: UpdatableOptions<void, any, any>,
9
9
  force?: true,
10
10
  ) => {
11
11
  const optionsThatChanged = getKeys(newOpts)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "4.2.598",
3
+ "version": "4.2.600",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -57,7 +57,7 @@
57
57
  "pg": "^8.23.0",
58
58
  "pg-cursor": "^2.22.0",
59
59
  "pg-promise": "^12.7.0",
60
- "prostgles-types": "^4.0.279"
60
+ "prostgles-types": "^4.0.280"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@eslint/js": "^9.22.0",
package/tsconfig.json CHANGED
@@ -18,8 +18,9 @@
18
18
  "declaration": true,
19
19
  "skipLibCheck": true,
20
20
  "declarationMap": true,
21
+ "removeComments": false,
21
22
  "noImplicitAny": true,
22
- "esModuleInterop": true
23
+ "esModuleInterop": true
23
24
  },
24
25
  "include": [
25
26
  "lib/**/*.spec.ts"