prostgles-server 4.2.567 → 4.2.569

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 (223) hide show
  1. package/dist/Auth/AuthTypes.d.ts +5 -5
  2. package/dist/Auth/AuthTypes.d.ts.map +1 -1
  3. package/dist/Auth/AuthTypes.js.map +1 -1
  4. package/dist/Auth/endpoints/setCatchAllRequestHandler.d.ts.map +1 -1
  5. package/dist/Auth/endpoints/setCatchAllRequestHandler.js +2 -2
  6. package/dist/Auth/endpoints/setCatchAllRequestHandler.js.map +1 -1
  7. package/dist/DBSchemaBuilder/getColumnTypescriptDefinition.d.ts +1 -1
  8. package/dist/DBSchemaBuilder/getColumnTypescriptDefinition.d.ts.map +1 -1
  9. package/dist/DBSchemaBuilder/getDBGeneratedSchema.d.ts +1 -1
  10. package/dist/DBSchemaBuilder/getDBGeneratedSchema.d.ts.map +1 -1
  11. package/dist/DboBuilder/DboBuilder.js +4 -4
  12. package/dist/DboBuilder/DboBuilder.js.map +1 -1
  13. package/dist/DboBuilder/DboBuilderTypes.d.ts +2 -18
  14. package/dist/DboBuilder/DboBuilderTypes.d.ts.map +1 -1
  15. package/dist/DboBuilder/DboBuilderTypes.js.map +1 -1
  16. package/dist/DboBuilder/TableHandler/TableHandler.d.ts +15 -5
  17. package/dist/DboBuilder/TableHandler/TableHandler.d.ts.map +1 -1
  18. package/dist/DboBuilder/TableHandler/TableHandler.js +4 -3
  19. package/dist/DboBuilder/TableHandler/TableHandler.js.map +1 -1
  20. package/dist/DboBuilder/TableHandler/delete.d.ts.map +1 -1
  21. package/dist/DboBuilder/TableHandler/delete.js +17 -14
  22. package/dist/DboBuilder/TableHandler/delete.js.map +1 -1
  23. package/dist/DboBuilder/TableHandler/insert/insert.d.ts.map +1 -1
  24. package/dist/DboBuilder/TableHandler/insert/insert.js +3 -12
  25. package/dist/DboBuilder/TableHandler/insert/insert.js.map +1 -1
  26. package/dist/DboBuilder/TableHandler/insert/insertNestedRecords.d.ts.map +1 -1
  27. package/dist/DboBuilder/TableHandler/insert/insertNestedRecords.js +10 -15
  28. package/dist/DboBuilder/TableHandler/insert/insertNestedRecords.js.map +1 -1
  29. package/dist/DboBuilder/TableHandler/insert/insertRowWithNestedRecords.js +1 -1
  30. package/dist/DboBuilder/TableHandler/insert/insertRowWithNestedRecords.js.map +1 -1
  31. package/dist/DboBuilder/TableHandler/onDeleteFromFileTable.d.ts +8 -6
  32. package/dist/DboBuilder/TableHandler/onDeleteFromFileTable.d.ts.map +1 -1
  33. package/dist/DboBuilder/TableHandler/onDeleteFromFileTable.js +30 -48
  34. package/dist/DboBuilder/TableHandler/onDeleteFromFileTable.js.map +1 -1
  35. package/dist/DboBuilder/TableHandler/update.d.ts +1 -1
  36. package/dist/DboBuilder/TableHandler/update.d.ts.map +1 -1
  37. package/dist/DboBuilder/TableHandler/update.js +8 -18
  38. package/dist/DboBuilder/TableHandler/update.js.map +1 -1
  39. package/dist/DboBuilder/TableHandler/updateFile.d.ts +2 -3
  40. package/dist/DboBuilder/TableHandler/updateFile.d.ts.map +1 -1
  41. package/dist/DboBuilder/TableHandler/updateFile.js +8 -28
  42. package/dist/DboBuilder/TableHandler/updateFile.js.map +1 -1
  43. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts +2 -2
  44. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts.map +1 -1
  45. package/dist/DboBuilder/ViewHandler/getInfo.d.ts +1 -1
  46. package/dist/DboBuilder/ViewHandler/getInfo.d.ts.map +1 -1
  47. package/dist/DboBuilder/ViewHandler/getInfo.js +12 -1
  48. package/dist/DboBuilder/ViewHandler/getInfo.js.map +1 -1
  49. package/dist/DboBuilder/getColumns.d.ts.map +1 -1
  50. package/dist/DboBuilder/getColumns.js +29 -4
  51. package/dist/DboBuilder/getColumns.js.map +1 -1
  52. package/dist/DboBuilder/schema/getTablesForSchemaPostgresSQL.d.ts +2 -2
  53. package/dist/DboBuilder/uploadFile.d.ts +5 -6
  54. package/dist/DboBuilder/uploadFile.d.ts.map +1 -1
  55. package/dist/DboBuilder/uploadFile.js +28 -42
  56. package/dist/DboBuilder/uploadFile.js.map +1 -1
  57. package/dist/FileManager/FileManager.d.ts +22 -78
  58. package/dist/FileManager/FileManager.d.ts.map +1 -1
  59. package/dist/FileManager/FileManager.js +1 -219
  60. package/dist/FileManager/FileManager.js.map +1 -1
  61. package/dist/FileManager/getFileTableConfig.d.ts +26 -0
  62. package/dist/FileManager/getFileTableConfig.d.ts.map +1 -0
  63. package/dist/FileManager/getFileTableConfig.js +115 -0
  64. package/dist/FileManager/getFileTableConfig.js.map +1 -0
  65. package/dist/FileManager/getLocalStorageClient.d.ts +3 -0
  66. package/dist/FileManager/getLocalStorageClient.d.ts.map +1 -0
  67. package/dist/FileManager/getLocalStorageClient.js +105 -0
  68. package/dist/FileManager/getLocalStorageClient.js.map +1 -0
  69. package/dist/FileManager/getValidatedFileType.d.ts +2 -2
  70. package/dist/FileManager/getValidatedFileType.d.ts.map +1 -1
  71. package/dist/FileManager/getValidatedFileType.js +7 -9
  72. package/dist/FileManager/getValidatedFileType.js.map +1 -1
  73. package/dist/FileManager/setupFileServeHandler.d.ts +9 -0
  74. package/dist/FileManager/setupFileServeHandler.d.ts.map +1 -0
  75. package/dist/FileManager/setupFileServeHandler.js +114 -0
  76. package/dist/FileManager/setupFileServeHandler.js.map +1 -0
  77. package/dist/Filtering/Filtering.d.ts +1 -1
  78. package/dist/Logging.d.ts +1 -1
  79. package/dist/Logging.d.ts.map +1 -1
  80. package/dist/Prostgles.d.ts +52 -6
  81. package/dist/Prostgles.d.ts.map +1 -1
  82. package/dist/Prostgles.js +8 -47
  83. package/dist/Prostgles.js.map +1 -1
  84. package/dist/ProstglesTypes.d.ts +23 -21
  85. package/dist/ProstglesTypes.d.ts.map +1 -1
  86. package/dist/ProstglesTypes.js.map +1 -1
  87. package/dist/PubSubManager/notifListener.d.ts.map +1 -1
  88. package/dist/PubSubManager/notifListener.js +0 -1
  89. package/dist/PubSubManager/notifListener.js.map +1 -1
  90. package/dist/PublishParser/getDBSchemaTable.d.ts +2 -2
  91. package/dist/PublishParser/getDBSchemaTable.d.ts.map +1 -1
  92. package/dist/PublishParser/getDBSchemaTable.js +5 -3
  93. package/dist/PublishParser/getDBSchemaTable.js.map +1 -1
  94. package/dist/PublishParser/getSchemaFromPublish.d.ts.map +1 -1
  95. package/dist/PublishParser/getSchemaFromPublish.js +2 -2
  96. package/dist/PublishParser/getSchemaFromPublish.js.map +1 -1
  97. package/dist/PublishParser/publishTypesAndUtils.d.ts +1 -0
  98. package/dist/PublishParser/publishTypesAndUtils.d.ts.map +1 -1
  99. package/dist/PublishParser/publishTypesAndUtils.js.map +1 -1
  100. package/dist/RestApi.d.ts +1 -5
  101. package/dist/RestApi.d.ts.map +1 -1
  102. package/dist/RestApi.js +10 -5
  103. package/dist/RestApi.js.map +1 -1
  104. package/dist/TableConfig/{TableConfig.d.ts → TableConfigTypes.d.ts} +19 -64
  105. package/dist/TableConfig/TableConfigTypes.d.ts.map +1 -0
  106. package/dist/TableConfig/TableConfigTypes.js +3 -0
  107. package/dist/TableConfig/TableConfigTypes.js.map +1 -0
  108. package/dist/TableConfig/TableConfigurator.d.ts +104 -0
  109. package/dist/TableConfig/TableConfigurator.d.ts.map +1 -0
  110. package/dist/TableConfig/{TableConfig.js → TableConfigurator.js} +31 -44
  111. package/dist/TableConfig/TableConfigurator.js.map +1 -0
  112. package/dist/TableConfig/applyTableConfig.d.ts.map +1 -1
  113. package/dist/TableConfig/applyTableConfig.js +0 -1
  114. package/dist/TableConfig/applyTableConfig.js.map +1 -1
  115. package/dist/TableConfig/getColumnConfig.d.ts +1 -1
  116. package/dist/TableConfig/getColumnConfig.d.ts.map +1 -1
  117. package/dist/TableConfig/getColumnSQLDefinitionQuery.d.ts +1 -1
  118. package/dist/TableConfig/getColumnSQLDefinitionQuery.d.ts.map +1 -1
  119. package/dist/TableConfig/getColumnSQLDefinitionQuery.js +1 -1
  120. package/dist/TableConfig/getColumnSQLDefinitionQuery.js.map +1 -1
  121. package/dist/TableConfig/getConstraintDefinitionQueries.d.ts +1 -1
  122. package/dist/TableConfig/getConstraintDefinitionQueries.d.ts.map +1 -1
  123. package/dist/TableConfig/getCreateSchemaQueries.d.ts +26 -8
  124. package/dist/TableConfig/getCreateSchemaQueries.d.ts.map +1 -1
  125. package/dist/TableConfig/getSchemaDiffQueries.d.ts.map +1 -1
  126. package/dist/TableConfig/getSchemaDiffQueries.js +12 -8
  127. package/dist/TableConfig/getSchemaDiffQueries.js.map +1 -1
  128. package/dist/TableConfig/getTableColumnQueries.d.ts +1 -1
  129. package/dist/TableConfig/getTableColumnQueries.d.ts.map +1 -1
  130. package/dist/TableConfig/getTableColumnQueries.js +19 -19
  131. package/dist/TableConfig/getTableColumnQueries.js.map +1 -1
  132. package/dist/TableConfig/getTableConfigSchemaQueries.d.ts +26 -10
  133. package/dist/TableConfig/getTableConfigSchemaQueries.d.ts.map +1 -1
  134. package/dist/TableConfig/getTableConfigSchemaQueries.js +8 -10
  135. package/dist/TableConfig/getTableConfigSchemaQueries.js.map +1 -1
  136. package/dist/TableConfig/indexes/getIndexesQueries.d.ts +1 -1
  137. package/dist/TableConfig/indexes/getIndexesQueries.d.ts.map +1 -1
  138. package/dist/TableConfig/initTableConfig.d.ts +1 -1
  139. package/dist/TableConfig/initTableConfig.d.ts.map +1 -1
  140. package/dist/TableConfig/initTableConfig.js +6 -3
  141. package/dist/TableConfig/initTableConfig.js.map +1 -1
  142. package/dist/WebsocketAPI/getClientSchema.d.ts.map +1 -1
  143. package/dist/WebsocketAPI/getClientSchema.js +6 -1
  144. package/dist/WebsocketAPI/getClientSchema.js.map +1 -1
  145. package/dist/index.d.ts +3 -2
  146. package/dist/index.d.ts.map +1 -1
  147. package/dist/index.js +1 -0
  148. package/dist/index.js.map +1 -1
  149. package/dist/initProstgles.d.ts.map +1 -1
  150. package/dist/initProstgles.js +2 -5
  151. package/dist/initProstgles.js.map +1 -1
  152. package/dist/runClientRequest.js +1 -1
  153. package/dist/runClientRequest.js.map +1 -1
  154. package/dist/updateConfiguration.d.ts.map +1 -1
  155. package/dist/updateConfiguration.js +0 -3
  156. package/dist/updateConfiguration.js.map +1 -1
  157. package/eslint.config.mjs +0 -3
  158. package/lib/Auth/AuthTypes.ts +6 -7
  159. package/lib/Auth/endpoints/setCatchAllRequestHandler.ts +6 -2
  160. package/lib/DBSchemaBuilder/getColumnTypescriptDefinition.ts +1 -1
  161. package/lib/DBSchemaBuilder/getDBGeneratedSchema.ts +1 -1
  162. package/lib/DboBuilder/DboBuilder.ts +4 -4
  163. package/lib/DboBuilder/DboBuilderTypes.ts +7 -20
  164. package/lib/DboBuilder/TableHandler/TableHandler.ts +7 -4
  165. package/lib/DboBuilder/TableHandler/delete.ts +23 -17
  166. package/lib/DboBuilder/TableHandler/insert/insert.ts +3 -12
  167. package/lib/DboBuilder/TableHandler/insert/insertNestedRecords.ts +13 -17
  168. package/lib/DboBuilder/TableHandler/insert/insertRowWithNestedRecords.ts +1 -1
  169. package/lib/DboBuilder/TableHandler/onDeleteFromFileTable.ts +41 -54
  170. package/lib/DboBuilder/TableHandler/update.ts +8 -19
  171. package/lib/DboBuilder/TableHandler/updateFile.ts +20 -42
  172. package/lib/DboBuilder/ViewHandler/ViewHandler.ts +1 -1
  173. package/lib/DboBuilder/ViewHandler/getInfo.ts +19 -2
  174. package/lib/DboBuilder/getColumns.ts +40 -4
  175. package/lib/DboBuilder/uploadFile.ts +34 -50
  176. package/lib/FileManager/FileManager.ts +23 -249
  177. package/lib/FileManager/getFileTableConfig.ts +142 -0
  178. package/lib/FileManager/getLocalStorageClient.ts +118 -0
  179. package/lib/FileManager/getValidatedFileType.ts +14 -22
  180. package/lib/FileManager/setupFileServeHandler.ts +102 -0
  181. package/lib/Logging.ts +0 -3
  182. package/lib/Prostgles.ts +9 -44
  183. package/lib/ProstglesTypes.ts +34 -23
  184. package/lib/PubSubManager/notifListener.ts +0 -2
  185. package/lib/PublishParser/getDBSchemaTable.ts +6 -3
  186. package/lib/PublishParser/getSchemaFromPublish.ts +2 -1
  187. package/lib/PublishParser/publishTypesAndUtils.ts +1 -0
  188. package/lib/RestApi.ts +12 -12
  189. package/lib/TableConfig/{TableConfig.ts → TableConfigTypes.ts} +18 -217
  190. package/lib/TableConfig/TableConfigurator.ts +191 -0
  191. package/lib/TableConfig/applyTableConfig.ts +2 -3
  192. package/lib/TableConfig/getColumnConfig.ts +2 -2
  193. package/lib/TableConfig/getColumnSQLDefinitionQuery.ts +2 -2
  194. package/lib/TableConfig/getConstraintDefinitionQueries.ts +1 -1
  195. package/lib/TableConfig/getCreateSchemaQueries.ts +1 -1
  196. package/lib/TableConfig/getSchemaDiffQueries.ts +14 -11
  197. package/lib/TableConfig/getTableColumnQueries.ts +22 -20
  198. package/lib/TableConfig/getTableConfigSchemaQueries.ts +20 -22
  199. package/lib/TableConfig/indexes/getIndexesQueries.ts +1 -1
  200. package/lib/TableConfig/initTableConfig.ts +7 -6
  201. package/lib/WebsocketAPI/getClientSchema.ts +7 -1
  202. package/lib/index.ts +3 -2
  203. package/lib/initProstgles.ts +2 -6
  204. package/lib/runClientRequest.ts +1 -1
  205. package/lib/updateConfiguration.ts +0 -3
  206. package/package.json +2 -3
  207. package/dist/FileManager/initFileManager.d.ts +0 -18
  208. package/dist/FileManager/initFileManager.d.ts.map +0 -1
  209. package/dist/FileManager/initFileManager.js +0 -257
  210. package/dist/FileManager/initFileManager.js.map +0 -1
  211. package/dist/FileManager/upload.d.ts +0 -4
  212. package/dist/FileManager/upload.d.ts.map +0 -1
  213. package/dist/FileManager/upload.js +0 -90
  214. package/dist/FileManager/upload.js.map +0 -1
  215. package/dist/FileManager/uploadStream.d.ts +0 -4
  216. package/dist/FileManager/uploadStream.d.ts.map +0 -1
  217. package/dist/FileManager/uploadStream.js +0 -100
  218. package/dist/FileManager/uploadStream.js.map +0 -1
  219. package/dist/TableConfig/TableConfig.d.ts.map +0 -1
  220. package/dist/TableConfig/TableConfig.js.map +0 -1
  221. package/lib/FileManager/initFileManager.ts +0 -254
  222. package/lib/FileManager/upload.ts +0 -59
  223. package/lib/FileManager/uploadStream.ts +0 -76
@@ -10,7 +10,7 @@ import {
10
10
  } from "prostgles-types";
11
11
  import type { TableSchemaColumn } from "../DboBuilder/DboBuilder";
12
12
  import { escapeTSNames } from "../utils/utils";
13
- import type { TableConfig } from "../TableConfig/TableConfig";
13
+ import type { TableConfig } from "../TableConfig/TableConfigTypes";
14
14
  import { getColumnConfig } from "../TableConfig/getColumnConfig";
15
15
 
16
16
  export const getColumnTypescriptDefinition = ({
@@ -1,5 +1,5 @@
1
1
  import { isDefined, isEmpty, type TableSchema } from "prostgles-types";
2
- import type { TableConfig } from "../TableConfig/TableConfig";
2
+ import type { TableConfig } from "../TableConfig/TableConfigTypes";
3
3
  import { escapeTSNames } from "../utils/utils";
4
4
  import { getColumnTypescriptDefinition } from "./getColumnTypescriptDefinition";
5
5
  import { fromEntries } from "../PublishParser/applyScopeToTableRules";
@@ -242,7 +242,7 @@ export class DboBuilder {
242
242
  tableOrViewInfo: tov,
243
243
  dboBuilder: this,
244
244
  tx: undefined,
245
- config: this.prostgles.opts.tableConfig?.[tov.name],
245
+ config: this.prostgles.mergedTableConfig?.[tov.name],
246
246
  joinPaths: this.shortestJoinPaths,
247
247
  });
248
248
  this.dbo[tov.name] = tableHandler;
@@ -270,7 +270,7 @@ export class DboBuilder {
270
270
  }
271
271
 
272
272
  this.tableTsDefinitions = getDBGeneratedSchema({
273
- config: this.prostgles.opts.tableConfig,
273
+ config: this.prostgles.mergedTableConfig,
274
274
  tablesOrViews,
275
275
  });
276
276
  this.functionTsDefinitions = getFunctionsTypescriptSchema(
@@ -306,7 +306,7 @@ export class DboBuilder {
306
306
  : { tsSchema: string; tablesOrViews: TableSchema[] } => {
307
307
  const getSchema = (tablesOrViews: TableSchema[]) => {
308
308
  const tableTsDefinitions = getDBGeneratedSchema({
309
- config: this.prostgles.opts.tableConfig,
309
+ config: this.prostgles.mergedTableConfig,
310
310
  tablesOrViews: [
311
311
  ...tablesOrViews,
312
312
  ...extraTables.filter(
@@ -379,7 +379,7 @@ export class DboBuilder {
379
379
  tableOrViewInfo: tov,
380
380
  dboBuilder: this,
381
381
  tx: { t, dbTX },
382
- config: this.prostgles.opts.tableConfig?.[tov.name],
382
+ config: this.prostgles.mergedTableConfig?.[tov.name],
383
383
  joinPaths: this.shortestJoinPaths,
384
384
  });
385
385
  });
@@ -9,7 +9,12 @@ import type {
9
9
  TableInfo as TInfo,
10
10
  UserLike,
11
11
  } from "prostgles-types";
12
- import type { AuthClientRequest, BasicSession, SessionUser } from "../Auth/AuthTypes";
12
+ import type {
13
+ AuthClientRequest,
14
+ AuthResultWithSID,
15
+ BasicSession,
16
+ SessionUser,
17
+ } from "../Auth/AuthTypes";
13
18
  import type { BasicCallback } from "../PubSubManager/PubSubManager";
14
19
  import type {
15
20
  DeleteRule,
@@ -88,25 +93,6 @@ export type SortItem = {
88
93
  }
89
94
  );
90
95
 
91
- export type Media = {
92
- id?: string;
93
- title?: string;
94
- extension?: string;
95
- content_type?: string;
96
- content_length?: number;
97
- url?: string;
98
- added?: Date;
99
- signed_url?: string;
100
- signed_url_expires?: number;
101
- name?: string;
102
- original_name?: string;
103
- etag?: string;
104
- deleted?: string | null;
105
- deleted_from_storage?: string | null;
106
- };
107
-
108
- export type ParsedMedia = Required<Pick<Media, "extension" | "content_type">>;
109
-
110
96
  export type TxCB<R = any, TH = DbTxTableHandlers> = {
111
97
  (t: TH, _t: pgPromise.ITask<{}>): R;
112
98
  };
@@ -199,6 +185,7 @@ export type LocalParams = {
199
185
  clientReq?: AuthClientRequest | undefined;
200
186
  isRemoteRequest?: {
201
187
  user?: UserLike | undefined;
188
+ clientInfo: AuthResultWithSID | undefined;
202
189
  };
203
190
  scope?: PermissionScope | undefined;
204
191
  func?: () => any;
@@ -17,8 +17,7 @@ import type {
17
17
  UpdateRule,
18
18
  } from "../../PublishParser/PublishParser";
19
19
  import { getSyncBatchOptions } from "../../PubSubManager/SyncReplication/getSyncBatchOptions";
20
- import type TableConfigurator from "../../TableConfig/TableConfig";
21
- import type { TableDefinition } from "../../TableConfig/TableConfig";
20
+ import type { TableDefinition } from "../../TableConfig/TableConfigTypes";
22
21
  import type { DboBuilder, Filter, LocalParams, TableHandlers } from "../DboBuilder";
23
22
  import { getErrorAsObject, getSerializedClientErrorFromPGError } from "../DboBuilder";
24
23
  import type { TableSchema } from "../DboBuilderTypes";
@@ -35,6 +34,7 @@ import { update } from "./update";
35
34
  import { updateBatch } from "./updateBatch";
36
35
  import { upsert } from "./upsert";
37
36
  import { isApplicableHook } from "./isApplicableHook";
37
+ import type { TableConfigurator } from "../../TableConfig/TableConfigurator";
38
38
 
39
39
  export type ValidatedParams = {
40
40
  row: AnyObject;
@@ -85,6 +85,7 @@ export class TableHandler extends ViewHandler {
85
85
  row: AnyObject,
86
86
  localParams: LocalParams | undefined,
87
87
  command: "insert" | "update",
88
+ filter: AnyObject | undefined,
88
89
  ) => {
89
90
  const transaction = this.getTransaction(localParams);
90
91
  const hooks = this.getBeforeHooks(command, [row]);
@@ -94,11 +95,12 @@ export class TableHandler extends ViewHandler {
94
95
  const isApplicable = isApplicableHook(this, [newRow], hook, command);
95
96
  if (!isApplicable) continue;
96
97
  const hookResult = await hook.validate({
97
- command: "insert",
98
+ command,
98
99
  data: newRow,
99
100
  dbx: this.getFinalDbo(localParams),
100
101
  localParams,
101
102
  tx: transaction?.t || this.db,
103
+ filter,
102
104
  });
103
105
  if (hookResult) {
104
106
  newRow = hookResult.row;
@@ -177,7 +179,8 @@ export class TableHandler extends ViewHandler {
177
179
  const hasBeforeHooks =
178
180
  command.name !== "delete" && this.getBeforeHooks(command.name, newRows).length > 0;
179
181
  return {
180
- shouldWrap: !transaction && (hasAfterChecks || hasBeforeHooks),
182
+ shouldWrap:
183
+ !transaction && (hasAfterChecks || hasBeforeHooks || this.config?.hooks?.onInsteadOfDelete),
181
184
  hasBeforeHooks,
182
185
  hasAfterChecks,
183
186
  };
@@ -140,16 +140,31 @@ export async function _delete(
140
140
  return queryToReturn as unknown[];
141
141
  }
142
142
 
143
- /**
144
- * Delete file
145
- */
146
- if (this.is_media) {
147
- const result = await onDeleteFromFileTable.bind(this)({
148
- localParams,
143
+ const transaction = this.getTransaction(localParams);
144
+ const dbHandler = transaction?.t ?? this.db;
145
+
146
+ const isOneOrNone = includes(["row", "value"], params?.returnType);
147
+ const queryPromise = () =>
148
+ isOneOrNone ?
149
+ dbHandler.oneOrNone<AnyObject>(queryWithRLS).then((data) => (data ? [data] : []))
150
+ : dbHandler.any<AnyObject>(queryWithRLS);
151
+
152
+ const onInsteadOfDelete = this.config?.hooks?.onInsteadOfDelete;
153
+ if (onInsteadOfDelete) {
154
+ if (!transaction) {
155
+ throw new Error(
156
+ "onInsteadOfDelete requires a transaction. Please wrap the delete call in a transaction.",
157
+ );
158
+ }
159
+ const result = await onInsteadOfDelete({
149
160
  queryType,
150
- returningQuery: undefined,
161
+ isOneOrNone,
162
+ dbx: transaction.dbTX,
163
+ tx: transaction.t,
164
+ returningQuery,
151
165
  filterOpts,
152
166
  });
167
+
153
168
  await this._log({
154
169
  command: "delete",
155
170
  localParams,
@@ -158,16 +173,7 @@ export async function _delete(
158
173
  });
159
174
  return result;
160
175
  }
161
-
162
- const dbHandler = this.getTransaction(localParams)?.t ?? this.db;
163
-
164
- const isOneOrNone = includes(["row", "value"], params?.returnType);
165
- const queryPromise =
166
- isOneOrNone ?
167
- dbHandler.oneOrNone<AnyObject>(queryWithRLS).then((data) => (data ? [data] : []))
168
- : dbHandler.any<AnyObject>(queryWithRLS);
169
-
170
- const deletedRows = await queryPromise.catch((err) =>
176
+ const deletedRows = await queryPromise().catch((err) =>
171
177
  rejectWithPGClientError(err, {
172
178
  type: "tableMethod",
173
179
  localParams,
@@ -93,23 +93,14 @@ export async function insert(
93
93
  const successCallbacks: (() => void)[] = [];
94
94
  const preValidatedRows: InsertedRowWithInfo[] = await Promise.all(
95
95
  rows.map(async (nonValidated) => {
96
- const { preValidate, validate } = rule ?? {};
96
+ const { preValidate } = rule ?? {};
97
97
  const { tableConfigurator } = this.dboBuilder.prostgles;
98
98
  if (!tableConfigurator) {
99
99
  throw "tableConfigurator missing";
100
100
  }
101
- let row = await tableConfigurator.getPreInsertRow(this, {
102
- dbx: this.getFinalDbo(localParams),
103
- validate,
104
- localParams,
105
- row: nonValidated,
106
- tx,
107
- command: "insert",
108
- data: nonValidated,
109
- });
110
101
 
111
- const beforeResult = await this.beforeEach(row, localParams, "insert");
112
- row = beforeResult.row;
102
+ const beforeResult = await this.beforeEach(nonValidated, localParams, "insert", undefined);
103
+ let row = beforeResult.row;
113
104
  beforeResult.successCallbacks.forEach((cb) => successCallbacks.push(cb));
114
105
 
115
106
  if (preValidate) {
@@ -24,26 +24,22 @@ export async function insertNestedRecords(
24
24
  this: TableHandler,
25
25
  { data, insertParams, tableRules, localParams }: InsertNestedRecordsArgs,
26
26
  ) {
27
- const MEDIA_COL_NAMES = ["data", "name"];
28
-
29
27
  const getExtraKeys = (row: AnyObject) =>
30
28
  getKeys(row).filter((fieldName) => {
31
- /* If media then use file insert columns */
32
- if (this.is_media) {
33
- return !this.column_names.concat(MEDIA_COL_NAMES).includes(fieldName);
34
- } else if (!this.columns.find((c) => c.name === fieldName)) {
35
- if (
36
- !isObject(row[fieldName]) &&
37
- !Array.isArray(row[fieldName]) &&
38
- row[fieldName] !== undefined
39
- ) {
40
- throw new Error("Invalid/Disallowed field in data: " + fieldName);
41
- } else if (!this.dboBuilder.dbo[fieldName]) {
42
- return false;
43
- }
44
- return true;
29
+ const matchingColumn = this.columns.find((c) => c.name === fieldName);
30
+ if (matchingColumn) return false;
31
+ if (
32
+ !isObject(row[fieldName]) &&
33
+ !Array.isArray(row[fieldName]) &&
34
+ row[fieldName] !== undefined
35
+ ) {
36
+ throw new Error(
37
+ `Invalid/Disallowed field in data: ${fieldName}. If inserting nested records, the field must be an object or array.`,
38
+ );
39
+ } else if (!this.dboBuilder.dboMap.get(fieldName)) {
40
+ return false;
45
41
  }
46
- return false;
42
+ return true;
47
43
  });
48
44
 
49
45
  /**
@@ -187,7 +187,7 @@ export async function insertRowWithNestedRecords(
187
187
  if (!colsRefT1.length || !colsRefT3.length)
188
188
  throw "Incorrectly referenced or missing columns for nested insert";
189
189
 
190
- const fileTable = this.dboBuilder.prostgles.fileManager?.tableName;
190
+ const fileTable = this.dboBuilder.prostgles.opts.fileTable?.tableName;
191
191
  if (targetTable !== fileTable) {
192
192
  throw "Only media allowed to have nested inserts more than 2 tables apart";
193
193
  }
@@ -1,70 +1,57 @@
1
+ import type pgPromise from "pg-promise";
1
2
  import type { AnyObject } from "prostgles-types";
2
3
  import { asName, pickKeys } from "prostgles-types";
3
- import type { LocalParams } from "../DboBuilder";
4
- import { type Media } from "../DboBuilder";
5
- import type { TableHandler } from "./TableHandler";
4
+ import type { FileTableConfig } from "../../ProstglesTypes";
5
+ import type { TableHandlers } from "../DboBuilder";
6
+ import type { FileTableRow } from "../../FileManager/getFileTableConfig";
6
7
 
7
8
  type OnDeleteFromFileTableArgs = {
8
- localParams: LocalParams | undefined;
9
- queryType: "one" | "none" | "many" | "any";
10
9
  returningQuery: undefined | string;
11
10
  filterOpts: {
12
11
  where: string;
13
12
  filter: AnyObject;
14
13
  };
14
+ t: pgPromise.ITask<{}>;
15
+ dbTX: TableHandlers;
16
+ isOneOrNone: boolean;
15
17
  };
16
- export async function onDeleteFromFileTable(
17
- this: TableHandler,
18
- { localParams, queryType, returningQuery, filterOpts }: OnDeleteFromFileTableArgs,
19
- ) {
20
- if (!this.dboBuilder.prostgles.fileManager) {
21
- throw new Error("fileManager missing");
18
+ export const onDeleteFromFileTable = async (
19
+ config: FileTableConfig,
20
+ { dbTX, t, returningQuery, filterOpts, isOneOrNone }: OnDeleteFromFileTableArgs,
21
+ ) => {
22
+ const { storageClient, tableName } = config;
23
+ const tableHandler = dbTX[tableName];
24
+ if (!tableHandler) {
25
+ throw new Error(`TableHandler for ${tableName} not found in dbTX`);
22
26
  }
23
- if (this.dboBuilder.prostgles.opts.fileTable?.delayedDelete) {
24
- return this.dbHandler[queryType]<void>(
25
- `UPDATE ${asName(this.name)} SET deleted = now() ${filterOpts.where} ${returningQuery ?? ""};`,
27
+ if (config.delayedDelete) {
28
+ const result = await t.any(
29
+ `UPDATE ${asName(tableName)} SET deleted = now() ${filterOpts.where} ${returningQuery ?? ""};`,
26
30
  );
27
- } else {
28
- const txDelete = async (tbl: TableHandler) => {
29
- if (!tbl.tx) throw new Error("Missing transaction object tx");
30
- let files: { id: string; name: string }[] = [];
31
- const totalFiles = await tbl.count(filterOpts.filter);
32
- do {
33
- const batch = (await tbl.find(filterOpts.filter, {
34
- limit: 100,
35
- offset: files.length,
36
- })) as Required<Media>[];
37
- files = [...files, ...batch];
38
- } while (files.length < totalFiles);
39
-
40
- const fileManager = tbl.dboBuilder.prostgles.fileManager;
41
- if (!fileManager) throw new Error("fileManager missing");
42
-
43
- for (const file of files) {
44
- await tbl.tx.t.any(`DELETE FROM ${asName(this.name)} WHERE id = \${id}`, file);
45
- }
46
- /** If any table delete fails then do not delete files */
47
- for (const file of files) {
48
- await fileManager.deleteFile(file.name);
49
- /** TODO: Keep track of deleted files in case of failure */
50
- // await tbl.t?.any(`UPDATE ${asName(this.name)} SET deleted = NOW(), deleted_from_storage = NOW() WHERE id = ` + "${id}", file);
51
- }
31
+ return (isOneOrNone ? result[0] : result) as undefined | AnyObject[];
32
+ }
52
33
 
53
- if (returningQuery) {
54
- return files.map((f) => pickKeys(f, ["id", "name"]));
55
- }
34
+ let files: { id: string; name: string }[] = [];
35
+ const totalFiles = await tableHandler.count(filterOpts.filter);
36
+ do {
37
+ const batch = (await tableHandler.find(filterOpts.filter, {
38
+ limit: 100,
39
+ offset: files.length,
40
+ })) as FileTableRow[];
41
+ files = [...files, ...batch];
42
+ } while (files.length < totalFiles);
56
43
 
57
- return undefined;
58
- };
44
+ for (const file of files) {
45
+ await t.any(`DELETE FROM ${asName(tableName)} WHERE id = \${id}`, file);
46
+ }
47
+ /** If any table delete fails then do not delete files */
48
+ for (const file of files) {
49
+ await storageClient.delete(file.name);
50
+ }
59
51
 
60
- if (localParams?.tx?.dbTX) {
61
- return txDelete(localParams.tx.dbTX[this.name] as TableHandler);
62
- } else if (this.tx) {
63
- return txDelete(this);
64
- } else {
65
- return this.dboBuilder.getTX((tx) => {
66
- return txDelete(tx[this.name] as TableHandler);
67
- });
68
- }
52
+ if (returningQuery) {
53
+ return files.map((f) => pickKeys(f, ["id", "name"]));
69
54
  }
70
- }
55
+
56
+ return undefined;
57
+ };
@@ -2,18 +2,17 @@ import type { AnyObject, UpdateParams } from "prostgles-types";
2
2
  import type { ParsedTableRule } from "../../PublishParser/PublishParser";
3
3
  import type { Filter, LocalParams } from "../DboBuilder";
4
4
  import { getErrorAsObject, getSerializedClientErrorFromPGError, withUserRLS } from "../DboBuilder";
5
+ import { getReturnTypeQuery } from "../ViewHandler/getReturnTypeQuery";
5
6
  import { prepareNewData } from "./DataValidator";
7
+ import { getInsertTableRules } from "./insert/getInsertTableRules";
6
8
  import { getReferenceColumnInserts } from "./insert/getReferenceColumnInserts";
7
9
  import { runInsertUpdateQuery } from "./runInsertUpdateQuery";
8
10
  import type { TableHandler } from "./TableHandler";
9
- import { updateFile } from "./updateFile";
10
- import { getInsertTableRules } from "./insert/getInsertTableRules";
11
- import { getReturnTypeQuery } from "../ViewHandler/getReturnTypeQuery";
12
11
 
13
12
  export async function update(
14
13
  this: TableHandler,
15
14
  filter: Filter,
16
- _newData: AnyObject,
15
+ newData: AnyObject,
17
16
  params?: UpdateParams,
18
17
  tableRules?: ParsedTableRule,
19
18
  localParams?: LocalParams,
@@ -26,27 +25,17 @@ export async function update(
26
25
  this.dboBuilder.getTX((th) =>
27
26
  (th[this.name] as Partial<typeof this> | undefined)?.[ACTION]?.(
28
27
  filter,
29
- _newData,
28
+ newData,
30
29
  params,
31
30
  tableRules,
32
31
  localParams,
33
32
  ),
34
33
  );
35
34
  const rule = tableRules?.[ACTION];
36
- if (this.shouldWrapInTx({ name: ACTION, rule }, localParams, [_newData]).shouldWrap) {
35
+ if (this.shouldWrapInTx({ name: ACTION, rule }, localParams, [newData]).shouldWrap) {
37
36
  return wrapInTx();
38
37
  }
39
38
 
40
- let newData = _newData;
41
- if (this.is_media) {
42
- ({ newData } = await updateFile.bind(this)({
43
- newData,
44
- filter,
45
- localParams,
46
- tableRules,
47
- }));
48
- }
49
-
50
39
  const parsedRules = await this.parseUpdateRules(filter, params, tableRules, localParams);
51
40
  if (localParams?.testRule) {
52
41
  return parsedRules;
@@ -76,7 +65,7 @@ export async function update(
76
65
  );
77
66
  }
78
67
 
79
- const beforeResult = await this.beforeEach(newData, localParams, "update");
68
+ const beforeResult = await this.beforeEach(newData, localParams, "update", filter);
80
69
  newData = beforeResult.row;
81
70
 
82
71
  const { data, allowedCols } = prepareNewData({
@@ -206,7 +195,7 @@ export async function update(
206
195
  await this._log({
207
196
  command: "update",
208
197
  localParams,
209
- data: { filter, _newData, params },
198
+ data: { filter, newData, params },
210
199
  duration: Date.now() - start,
211
200
  });
212
201
  beforeResult.successCallbacks.forEach((cb) => cb());
@@ -215,7 +204,7 @@ export async function update(
215
204
  await this._log({
216
205
  command: "update",
217
206
  localParams,
218
- data: { filter, _newData, params },
207
+ data: { filter, newData, params },
219
208
  duration: Date.now() - start,
220
209
  error: getErrorAsObject(e),
221
210
  });
@@ -1,71 +1,49 @@
1
- import type { AnyObject} from "prostgles-types";
1
+ import type { AnyObject } from "prostgles-types";
2
2
  import { getJSONBObjectSchemaValidationError, omitKeys } from "prostgles-types";
3
- import type { ParsedTableRule, ValidateRowBasic } from "../../PublishParser/PublishParser";
4
- import type { LocalParams, Media } from "../DboBuilder";
3
+ import type { FileTableRow } from "../../FileManager/getFileTableConfig";
4
+ import type { FileTableConfig } from "../../ProstglesTypes";
5
+ import type { LocalParams } from "../DboBuilder";
5
6
  import { isFile, uploadFile } from "../uploadFile";
6
7
  import type { TableHandler } from "./TableHandler";
7
8
 
8
9
  type Args = {
9
10
  newData: AnyObject;
10
11
  filter: AnyObject;
11
- tableRules: ParsedTableRule | undefined;
12
12
  localParams: LocalParams | undefined;
13
13
  };
14
- export const updateFile = async function (
15
- this: TableHandler,
16
- { filter, newData, tableRules, localParams }: Args
17
- ): Promise<{ newData: AnyObject }> {
18
- const rule = tableRules?.update;
19
-
20
- if (tableRules && !tableRules.update) {
21
- throw "Not allowed";
22
- }
23
- if (localParams?.testRule) {
24
- return { newData: {} };
25
- }
26
-
14
+ export const updateFile = async (
15
+ tableHandler: TableHandler,
16
+ config: FileTableConfig,
17
+ { filter, newData, localParams }: Args,
18
+ ): Promise<{ newData: AnyObject }> => {
27
19
  const { data } = getJSONBObjectSchemaValidationError(
28
20
  { id: { optional: true, type: "string" } },
29
21
  filter,
30
- "filter"
22
+ "filter",
31
23
  );
32
24
  const existingMediaId = data?.id;
33
25
  if (!existingMediaId) {
34
26
  throw new Error(
35
- `Updating the file table with file data can only be done by providing a single id filter. E.g. { id: "9ea4e23c-2b1a-4e33-8ec0-c15919bb45ec" } `
27
+ `Updating the file table with file data can only be done by providing a single id filter. E.g. { id: "9ea4e23c-2b1a-4e33-8ec0-c15919bb45ec" } `,
36
28
  );
37
29
  }
38
30
  if (!isFile(newData)) {
39
31
  throw new Error(
40
- "Expecting { data: Buffer, name: string } but received " + JSON.stringify(newData)
32
+ "Expecting { data: Buffer, name: string } but received " + JSON.stringify(newData),
41
33
  );
42
34
  }
43
35
 
44
- const fileManager = this.dboBuilder.prostgles.fileManager;
45
- if (!fileManager) throw new Error("fileManager missing");
46
- if (rule?.validate && !localParams) throw new Error("localParams missing");
47
- const validate: ValidateRowBasic | undefined =
48
- rule?.validate ?
49
- async (row) => {
50
- return rule.validate!({
51
- update: row,
52
- filter,
53
- dbx: this.tx?.dbTX || this.dboBuilder.dbo,
54
- localParams: localParams!,
55
- });
56
- }
57
- : undefined;
58
-
59
- const existingFile: Media | undefined = await (
60
- (localParams?.tx?.dbTX[this.name]) || this
61
- ).findOne({ id: existingMediaId });
36
+ const existingFile = (await tableHandler.findOne({
37
+ id: existingMediaId,
38
+ })) as FileTableRow | undefined;
62
39
 
63
- if (!existingFile?.name) throw new Error("Existing file record not found");
40
+ if (!existingFile?.name) {
41
+ throw new Error("Existing file record not found");
42
+ }
64
43
 
65
- await fileManager.deleteFile(existingFile.name);
66
- const newFile = await uploadFile.bind(this)({
44
+ await config.storageClient.delete(existingFile.name);
45
+ const newFile = await uploadFile(config, {
67
46
  row: newData,
68
- validate,
69
47
  localParams,
70
48
  mediaId: existingFile.id,
71
49
  });
@@ -29,7 +29,7 @@ import type { OnData } from "./subscribe";
29
29
  import { subscribe } from "./subscribe";
30
30
  import { validateViewRules } from "./validateViewRules";
31
31
  import { escapeTSNames } from "../../utils/utils";
32
- import type { TableDefinition } from "../../TableConfig/TableConfig";
32
+ import type { TableDefinition } from "../../TableConfig/TableConfigTypes";
33
33
  import { getDbHandlerWithAbort } from "./getDbHandlerWithAbort";
34
34
  import type { VoidFunction } from "../../SchemaWatch/SchemaWatch";
35
35
 
@@ -1,4 +1,4 @@
1
- import type { TableInfo as TInfo } from "prostgles-types/dist";
1
+ import { type TableInfo as TInfo } from "prostgles-types/dist";
2
2
  import type { ParsedTableRule } from "../../PublishParser/PublishParser";
3
3
  import type { LocalParams } from "../DboBuilder";
4
4
  import type { ViewHandler } from "./ViewHandler";
@@ -27,7 +27,7 @@ export async function getInfo(
27
27
  tableName: this.name,
28
28
  lang,
29
29
  });
30
- return {
30
+ const tableInfo: TInfo = {
31
31
  oid: this.tableOrViewInfo.oid,
32
32
  comment: this.tableOrViewInfo.comment,
33
33
  qualifiedNameParts: this.tableOrViewInfo.qualifiedNameParts,
@@ -69,4 +69,21 @@ export async function getInfo(
69
69
  delete: validatedTableRules.delete && {},
70
70
  },
71
71
  };
72
+
73
+ const modifyClientSchema = this.dboBuilder.prostgles.opts.modifyClientSchema;
74
+ if (!modifyClientSchema) {
75
+ return tableInfo;
76
+ }
77
+
78
+ const modifiedTableSchema = await modifyClientSchema(
79
+ {
80
+ name: this.name,
81
+ ...tableInfo,
82
+ columns: [],
83
+ },
84
+ localParams?.isRemoteRequest?.clientInfo,
85
+ );
86
+
87
+ const { columns, name, ...rest } = modifiedTableSchema;
88
+ return rest;
72
89
  }