prostgles-server 4.2.157 → 4.2.159

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 (159) hide show
  1. package/dist/Auth/AuthHandler.js +2 -2
  2. package/dist/Auth/AuthHandler.js.map +1 -1
  3. package/dist/Auth/AuthTypes.d.ts +4 -8
  4. package/dist/Auth/AuthTypes.d.ts.map +1 -1
  5. package/dist/Auth/setAuthProviders.d.ts +1 -1
  6. package/dist/Auth/setAuthProviders.d.ts.map +1 -1
  7. package/dist/Auth/setAuthProviders.js +6 -7
  8. package/dist/Auth/setAuthProviders.js.map +1 -1
  9. package/dist/Auth/setEmailProvider.d.ts +1 -1
  10. package/dist/Auth/setEmailProvider.d.ts.map +1 -1
  11. package/dist/Auth/setEmailProvider.js +22 -2
  12. package/dist/Auth/setEmailProvider.js.map +1 -1
  13. package/dist/Auth/setupAuthRoutes.js +1 -1
  14. package/dist/Auth/setupAuthRoutes.js.map +1 -1
  15. package/dist/Prostgles.d.ts +1 -0
  16. package/dist/Prostgles.d.ts.map +1 -1
  17. package/dist/Prostgles.js +6 -0
  18. package/dist/Prostgles.js.map +1 -1
  19. package/dist/initProstgles.d.ts.map +1 -1
  20. package/dist/initProstgles.js +2 -6
  21. package/dist/initProstgles.js.map +1 -1
  22. package/package.json +1 -1
  23. package/lib/Auth/AuthHandler.ts +0 -436
  24. package/lib/Auth/AuthTypes.ts +0 -285
  25. package/lib/Auth/getSafeReturnURL.ts +0 -35
  26. package/lib/Auth/sendEmail.ts +0 -83
  27. package/lib/Auth/setAuthProviders.ts +0 -129
  28. package/lib/Auth/setEmailProvider.ts +0 -63
  29. package/lib/Auth/setupAuthRoutes.ts +0 -161
  30. package/lib/DBEventsManager.ts +0 -178
  31. package/lib/DBSchemaBuilder.ts +0 -225
  32. package/lib/DboBuilder/DboBuilder.ts +0 -319
  33. package/lib/DboBuilder/DboBuilderTypes.ts +0 -361
  34. package/lib/DboBuilder/QueryBuilder/Functions.ts +0 -1153
  35. package/lib/DboBuilder/QueryBuilder/QueryBuilder.ts +0 -288
  36. package/lib/DboBuilder/QueryBuilder/getJoinQuery.ts +0 -263
  37. package/lib/DboBuilder/QueryBuilder/getNewQuery.ts +0 -271
  38. package/lib/DboBuilder/QueryBuilder/getSelectQuery.ts +0 -136
  39. package/lib/DboBuilder/QueryBuilder/prepareHaving.ts +0 -22
  40. package/lib/DboBuilder/QueryStreamer.ts +0 -250
  41. package/lib/DboBuilder/TableHandler/DataValidator.ts +0 -428
  42. package/lib/DboBuilder/TableHandler/TableHandler.ts +0 -205
  43. package/lib/DboBuilder/TableHandler/delete.ts +0 -115
  44. package/lib/DboBuilder/TableHandler/insert.ts +0 -183
  45. package/lib/DboBuilder/TableHandler/insertTest.ts +0 -78
  46. package/lib/DboBuilder/TableHandler/onDeleteFromFileTable.ts +0 -62
  47. package/lib/DboBuilder/TableHandler/runInsertUpdateQuery.ts +0 -134
  48. package/lib/DboBuilder/TableHandler/update.ts +0 -126
  49. package/lib/DboBuilder/TableHandler/updateBatch.ts +0 -49
  50. package/lib/DboBuilder/TableHandler/updateFile.ts +0 -48
  51. package/lib/DboBuilder/TableHandler/upsert.ts +0 -34
  52. package/lib/DboBuilder/ViewHandler/ViewHandler.ts +0 -393
  53. package/lib/DboBuilder/ViewHandler/count.ts +0 -38
  54. package/lib/DboBuilder/ViewHandler/find.ts +0 -153
  55. package/lib/DboBuilder/ViewHandler/getExistsCondition.ts +0 -73
  56. package/lib/DboBuilder/ViewHandler/getExistsFilters.ts +0 -74
  57. package/lib/DboBuilder/ViewHandler/getInfo.ts +0 -32
  58. package/lib/DboBuilder/ViewHandler/getTableJoinQuery.ts +0 -84
  59. package/lib/DboBuilder/ViewHandler/parseComplexFilter.ts +0 -96
  60. package/lib/DboBuilder/ViewHandler/parseFieldFilter.ts +0 -105
  61. package/lib/DboBuilder/ViewHandler/parseJoinPath.ts +0 -208
  62. package/lib/DboBuilder/ViewHandler/prepareSortItems.ts +0 -163
  63. package/lib/DboBuilder/ViewHandler/prepareWhere.ts +0 -90
  64. package/lib/DboBuilder/ViewHandler/size.ts +0 -37
  65. package/lib/DboBuilder/ViewHandler/subscribe.ts +0 -118
  66. package/lib/DboBuilder/ViewHandler/validateViewRules.ts +0 -70
  67. package/lib/DboBuilder/dboBuilderUtils.ts +0 -222
  68. package/lib/DboBuilder/getColumns.ts +0 -114
  69. package/lib/DboBuilder/getCondition.ts +0 -201
  70. package/lib/DboBuilder/getSubscribeRelatedTables.ts +0 -190
  71. package/lib/DboBuilder/getTablesForSchemaPostgresSQL.ts +0 -426
  72. package/lib/DboBuilder/insertNestedRecords.ts +0 -355
  73. package/lib/DboBuilder/parseUpdateRules.ts +0 -187
  74. package/lib/DboBuilder/prepareShortestJoinPaths.ts +0 -186
  75. package/lib/DboBuilder/runSQL.ts +0 -182
  76. package/lib/DboBuilder/runTransaction.ts +0 -50
  77. package/lib/DboBuilder/sqlErrCodeToMsg.ts +0 -254
  78. package/lib/DboBuilder/uploadFile.ts +0 -69
  79. package/lib/Event_Trigger_Tags.ts +0 -118
  80. package/lib/FileManager/FileManager.ts +0 -358
  81. package/lib/FileManager/getValidatedFileType.ts +0 -69
  82. package/lib/FileManager/initFileManager.ts +0 -187
  83. package/lib/FileManager/upload.ts +0 -62
  84. package/lib/FileManager/uploadStream.ts +0 -79
  85. package/lib/Filtering.ts +0 -463
  86. package/lib/JSONBValidation/validate_jsonb_schema_sql.ts +0 -502
  87. package/lib/JSONBValidation/validation.ts +0 -143
  88. package/lib/Logging.ts +0 -127
  89. package/lib/PostgresNotifListenManager.ts +0 -143
  90. package/lib/Prostgles.ts +0 -479
  91. package/lib/ProstglesTypes.ts +0 -196
  92. package/lib/PubSubManager/PubSubManager.ts +0 -609
  93. package/lib/PubSubManager/addSub.ts +0 -138
  94. package/lib/PubSubManager/addSync.ts +0 -141
  95. package/lib/PubSubManager/getCreatePubSubManagerError.ts +0 -72
  96. package/lib/PubSubManager/getPubSubManagerInitQuery.ts +0 -662
  97. package/lib/PubSubManager/initPubSubManager.ts +0 -79
  98. package/lib/PubSubManager/notifListener.ts +0 -173
  99. package/lib/PubSubManager/orphanTriggerCheck.ts +0 -70
  100. package/lib/PubSubManager/pushSubData.ts +0 -55
  101. package/lib/PublishParser/PublishParser.ts +0 -162
  102. package/lib/PublishParser/getFileTableRules.ts +0 -124
  103. package/lib/PublishParser/getSchemaFromPublish.ts +0 -141
  104. package/lib/PublishParser/getTableRulesWithoutFileTable.ts +0 -177
  105. package/lib/PublishParser/publishTypesAndUtils.ts +0 -399
  106. package/lib/RestApi.ts +0 -127
  107. package/lib/SchemaWatch/SchemaWatch.ts +0 -90
  108. package/lib/SchemaWatch/createSchemaWatchEventTrigger.ts +0 -3
  109. package/lib/SchemaWatch/getValidatedWatchSchemaType.ts +0 -45
  110. package/lib/SchemaWatch/getWatchSchemaTagList.ts +0 -27
  111. package/lib/SyncReplication.ts +0 -557
  112. package/lib/TableConfig/TableConfig.ts +0 -468
  113. package/lib/TableConfig/getColumnDefinitionQuery.ts +0 -111
  114. package/lib/TableConfig/getConstraintDefinitionQueries.ts +0 -95
  115. package/lib/TableConfig/getFutureTableSchema.ts +0 -64
  116. package/lib/TableConfig/getPGIndexes.ts +0 -53
  117. package/lib/TableConfig/getTableColumnQueries.ts +0 -129
  118. package/lib/TableConfig/initTableConfig.ts +0 -326
  119. package/lib/index.ts +0 -13
  120. package/lib/initProstgles.ts +0 -322
  121. package/lib/onSocketConnected.ts +0 -102
  122. package/lib/runClientRequest.ts +0 -129
  123. package/lib/shortestPath.ts +0 -122
  124. package/lib/typeTests/DBoGenerated.d.ts +0 -320
  125. package/lib/typeTests/dboTypeCheck.ts +0 -81
  126. package/lib/utils.ts +0 -15
  127. package/tests/client/hooks.spec.ts +0 -205
  128. package/tests/client/index.ts +0 -139
  129. package/tests/client/package-lock.json +0 -637
  130. package/tests/client/package.json +0 -26
  131. package/tests/client/renderReactHook.ts +0 -177
  132. package/tests/client/tsconfig.json +0 -15
  133. package/tests/client/useProstgles.spec.ts +0 -120
  134. package/tests/clientFileTests.spec.ts +0 -102
  135. package/tests/clientOnlyQueries.spec.ts +0 -667
  136. package/tests/clientRestApi.spec.ts +0 -82
  137. package/tests/config_test/DBoGenerated.d.ts +0 -407
  138. package/tests/config_test/index.html +0 -109
  139. package/tests/config_test/index.js +0 -86
  140. package/tests/config_test/index.js.map +0 -1
  141. package/tests/config_test/index.ts +0 -91
  142. package/tests/config_test/init.sql +0 -48
  143. package/tests/config_test/package.json +0 -29
  144. package/tests/config_test/tsconfig.json +0 -23
  145. package/tests/config_testDBoGenerated.d.ts +0 -407
  146. package/tests/isomorphicQueries.spec.ts +0 -1493
  147. package/tests/server/DBoGenerated.d.ts +0 -537
  148. package/tests/server/index.html +0 -73
  149. package/tests/server/index.ts +0 -289
  150. package/tests/server/init.sql +0 -224
  151. package/tests/server/package-lock.json +0 -2164
  152. package/tests/server/package.json +0 -25
  153. package/tests/server/publishTypeCheck.ts +0 -136
  154. package/tests/server/server.ts +0 -35
  155. package/tests/server/testPublish.ts +0 -147
  156. package/tests/server/testTableConfig.ts +0 -156
  157. package/tests/server/tsconfig.json +0 -22
  158. package/tests/serverOnlyQueries.spec.ts +0 -32
  159. package/tests/test.sh +0 -20
@@ -1,138 +0,0 @@
1
- import { SubscriptionChannels } from "prostgles-types";
2
- import { BasicCallback, parseCondition, PubSubManager, Subscription, SubscriptionParams } from "./PubSubManager";
3
- import { VoidFunction } from "../SchemaWatch/SchemaWatch";
4
-
5
- type AddSubscriptionParams = SubscriptionParams & {
6
- condition: string;
7
- }
8
-
9
- type AddSubResult = SubscriptionChannels & { sendFirstData: VoidFunction | undefined }
10
-
11
- /* Must return a channel for socket */
12
- /* The distinct list of {table_name, condition} must have a corresponding trigger in the database */
13
- export async function addSub(this: PubSubManager, subscriptionParams: Omit<AddSubscriptionParams, "channel_name" | "parentSubParams">): Promise<AddSubResult> {
14
- const {
15
- socket, localFuncs, table_rules, filter = {},
16
- params = {}, condition = "", throttle = 0, //subOne = false,
17
- viewOptions, table_info, throttleOpts,
18
- } = subscriptionParams || {};
19
- const table_name = table_info.name;
20
-
21
- if (!socket && !localFuncs) {
22
- throw "socket AND func missing";
23
- }
24
- if (socket && localFuncs) {
25
- throw "addSub: cannot have socket AND func";
26
- }
27
-
28
- let validated_throttle = subscriptionParams.throttle || 10;
29
- const pubThrottle = table_rules?.subscribe?.throttle || 0;
30
- if (pubThrottle && Number.isInteger(pubThrottle) && pubThrottle > 0) {
31
- validated_throttle = pubThrottle;
32
- }
33
- if (throttle && Number.isInteger(throttle) && throttle >= pubThrottle) {
34
- validated_throttle = throttle;
35
- }
36
-
37
- const channel_name = `${this.socketChannelPreffix}.${table_name}.${JSON.stringify(filter)}.${JSON.stringify(params)}.${"m"}.sub`;
38
- const mainTrigger = {
39
- table_name: table_name,
40
- condition: parseCondition(condition),
41
- is_related: false,
42
- } as const;
43
-
44
- const newSub: Subscription = {
45
- channel_name,
46
- filter,
47
- localFuncs,
48
- params,
49
- last_throttled: 0,
50
- socket,
51
- throttleOpts,
52
- table_info,
53
- is_ready: true,
54
- is_throttling: false,
55
- socket_id: socket?.id,
56
- table_rules,
57
- throttle: validated_throttle,
58
- triggers: [
59
- mainTrigger
60
- ]
61
- }
62
-
63
- const result: AddSubResult = {
64
- channelName: channel_name,
65
- channelNameReady: channel_name + ".ready",
66
- channelNameUnsubscribe: channel_name + ".unsubscribe",
67
- sendFirstData: undefined,
68
- }
69
-
70
- const [matchingSub] = this.getClientSubs(newSub);
71
- if(matchingSub){
72
- console.error("Trying to add a duplicate sub for: ", channel_name);
73
- return result;
74
- }
75
-
76
- this.upsertSocket(socket);
77
-
78
- if(viewOptions){
79
- for await(const relatedTable of viewOptions.relatedTables){
80
- const relatedSub = {
81
- table_name: relatedTable.tableName,
82
- condition: parseCondition(relatedTable.condition),
83
- is_related: true,
84
- } as const;
85
-
86
- newSub.triggers.push(relatedSub)
87
-
88
- await this.addTrigger(relatedSub, viewOptions, socket);
89
- }
90
-
91
- }
92
-
93
-
94
- if(localFuncs){
95
- /**
96
- * Must ensure sub will start sending data after all triggers are set up.
97
- * Socket clients are not affected as they need to confirm they are ready to receive data
98
- */
99
- result.sendFirstData = () => {
100
- this.pushSubData(newSub);
101
- }
102
-
103
- } else if (socket) {
104
- const removeListeners = () => {
105
- socket.removeAllListeners(channel_name);
106
- socket.removeAllListeners(result.channelNameReady);
107
- socket.removeAllListeners(result.channelNameUnsubscribe);
108
- }
109
- removeListeners();
110
-
111
- socket.once(result.channelNameReady, () => {
112
- this.pushSubData(newSub);
113
- });
114
- socket.once(result.channelNameUnsubscribe, (_data: any, cb: BasicCallback) => {
115
- const res = "ok";
116
- this.subs = this.subs.filter(s => {
117
- const isMatch = s.socket?.id === socket.id && s.channel_name === channel_name;
118
- return !isMatch;
119
- });
120
- removeListeners();
121
- cb(null, { res });
122
- });
123
- }
124
-
125
- this.subs.push(newSub);
126
-
127
- /** A view does not have triggers. Only related triggers */
128
- if (table_info.is_view) {
129
- if (!viewOptions?.relatedTables.length) {
130
- throw "PubSubManager: view parent_tables missing";
131
- }
132
-
133
- } else {
134
- await this.addTrigger(mainTrigger, undefined, socket);
135
- }
136
-
137
- return result;
138
- }
@@ -1,141 +0,0 @@
1
- import { find, tryCatch } from "prostgles-types/dist/util";
2
- import { AddSyncParams, BasicCallback, DEFAULT_SYNC_BATCH_SIZE, PubSubManager, parseCondition } from "./PubSubManager";
3
-
4
- /**
5
- * Returns a sync channel
6
- * A sync channel is unique per socket for each filter
7
- */
8
- export async function addSync(this: PubSubManager, syncParams: AddSyncParams) {
9
-
10
- const sid = this.dboBuilder.prostgles.authHandler?.getSIDNoError({ socket: syncParams.socket });
11
- const res = await tryCatch(async () => {
12
-
13
- const {
14
- socket = null, table_info = null, table_rules, synced_field = null,
15
- allow_delete = false, id_fields = [], filter = {},
16
- params, condition = "", throttle = 0
17
- } = syncParams || {} as AddSyncParams;
18
- const conditionParsed = parseCondition(condition);
19
- if (!socket || !table_info) throw "socket or table_info missing";
20
-
21
- const { name: table_name } = table_info;
22
- const channel_name = `${this.socketChannelPreffix}.${table_name}.${JSON.stringify(filter)}.sync`;
23
-
24
- if (!synced_field) throw "synced_field missing from table_rules";
25
-
26
- this.upsertSocket(socket);
27
-
28
- const upsertSync = () => {
29
- const newSync = {
30
- channel_name,
31
- table_name,
32
- filter,
33
- condition: conditionParsed,
34
- synced_field,
35
- sid,
36
- id_fields,
37
- allow_delete,
38
- table_rules,
39
- throttle: Math.max(throttle || 0, table_rules?.sync?.throttle || 0),
40
- batch_size: table_rules?.sync?.batch_size || DEFAULT_SYNC_BATCH_SIZE,
41
- socket_id: socket.id,
42
- is_sync: true,
43
- last_synced: 0,
44
- lr: undefined,
45
- table_info,
46
- is_syncing: false,
47
- wal: undefined,
48
- socket,
49
- params
50
- };
51
-
52
- /* Only a sync per socket per table per condition allowed */
53
- this.syncs ??= [];
54
- const existing = find(this.syncs, { socket_id: socket.id, channel_name });
55
- if (!existing) {
56
- this.syncs.push(newSync);
57
-
58
- const unsyncChn = channel_name + "unsync";
59
- socket.removeAllListeners(unsyncChn);
60
- socket.once(unsyncChn, (_data: any, cb: BasicCallback) => {
61
- this._log({
62
- type: "sync",
63
- command: "unsync",
64
- socketId: socket.id,
65
- tableName: table_name,
66
- condition,
67
- sid,
68
- connectedSocketIds: this.connectedSocketIds,
69
- duration: -1,
70
- });
71
- socket.removeAllListeners(channel_name);
72
- socket.removeAllListeners(unsyncChn);
73
- this.syncs = this.syncs.filter(s => {
74
- const isMatch = s.socket_id && s.socket_id === socket.id && s.channel_name === channel_name;
75
- return !isMatch;
76
- });
77
- cb(null, { res: "ok" });
78
- });
79
-
80
- socket.removeAllListeners(channel_name);
81
- socket.on(channel_name, (data: any, cb: BasicCallback) => {
82
-
83
- if (!data) {
84
- cb({ err: "Unexpected request. Need data or onSyncRequest" });
85
- return;
86
- }
87
-
88
- /*
89
- */
90
-
91
- /* Server will:
92
- 1. Ask for last_synced emit(onSyncRequest)
93
- 2. Ask for data >= server_synced emit(onPullRequest)
94
- -> Upsert that data
95
- 2. Push data >= last_synced emit(data.data)
96
-
97
- Client will:
98
- 1. Send last_synced on(onSyncRequest)
99
- 2. Send data >= server_synced on(onPullRequest)
100
- 3. Send data on CRUD emit(data.data | data.deleted)
101
- 4. Upsert data.data | deleted on(data.data | data.deleted)
102
- */
103
-
104
- if (data.onSyncRequest) {
105
- this.syncData(newSync, data.onSyncRequest, "client");
106
-
107
- } else {
108
- console.error("Unexpected sync request data from client: ", data)
109
- }
110
- });
111
-
112
- } else {
113
- console.warn("UNCLOSED DUPLICATE SYNC FOUND", existing.channel_name);
114
- }
115
-
116
- return newSync;
117
- };
118
-
119
- upsertSync();
120
-
121
- await this.addTrigger({ table_name, condition: conditionParsed }, undefined, socket);
122
-
123
- return { result: channel_name };
124
- });
125
-
126
- await this._log({
127
- type: "sync",
128
- command: "addSync",
129
- tableName: syncParams.table_info.name,
130
- condition: syncParams.condition,
131
- socketId: syncParams.socket.id,
132
- connectedSocketIds: this.connectedSocketIds,
133
- duration: res.duration,
134
- error: res.error,
135
- sid,
136
- });
137
-
138
- if(res.error !== undefined) throw res.error;
139
-
140
- return res.result;
141
- }
@@ -1,72 +0,0 @@
1
- import { tryCatch } from "prostgles-types";
2
- import { getPubSubManagerInitQuery } from "./getPubSubManagerInitQuery";
3
- import { getCanExecute } from "../DboBuilder/dboBuilderUtils";
4
- import { DboBuilder } from "../DboBuilder/DboBuilder";
5
-
6
- export const getCreatePubSubManagerError = async (dboBuilder: DboBuilder): Promise<string | undefined> => {
7
- const db = dboBuilder.db;
8
-
9
- const canExecute = await getCanExecute(db)
10
- if (!canExecute) return "Cannot run EXECUTE statements on this connection";
11
-
12
- /** Check if prostgles schema exists */
13
- const prglSchema = await db.any(`
14
- SELECT *
15
- FROM pg_catalog.pg_namespace
16
- WHERE nspname = 'prostgles'
17
- `);
18
-
19
- const checkIfCanCreateProstglesSchema = () => tryCatch(async () => {
20
- const allGood = await db.task(async t => {
21
- try {
22
- await t.none(`
23
- BEGIN;
24
- DROP SCHEMA IF EXISTS prostgles CASCADE;
25
- CREATE SCHEMA IF NOT EXISTS prostgles;
26
- ROLLBACK;
27
- `);
28
- } catch (e) {
29
- await t.none(`ROLLBACK`);
30
- return false;
31
- }
32
-
33
- return true;
34
- });
35
-
36
- return allGood;
37
- });
38
-
39
- if (!prglSchema.length) {
40
- const canCreate = await checkIfCanCreateProstglesSchema();
41
- if (!canCreate) {
42
- const dbName = await db.one(`SELECT current_database()`);
43
- const user = await db.one(`SELECT current_user`);
44
- return `Not allowed to create prostgles schema. GRANT CREATE ON DATABASE ${dbName} TO ${user}`;
45
- }
46
- return undefined;
47
- } else {
48
- const canCheckVersion = await tryCatch(async () => {
49
- await db.any(`
50
- SELECT * FROM prostgles.versions
51
- `);
52
- return { ok: true };
53
- });
54
-
55
- if(!canCheckVersion.ok){
56
- console.error("prostgles schema exists but cannot check version. Check logs", canCheckVersion.error);
57
- return "prostgles schema exists but cannot check version. Check logs";
58
- }
59
- }
60
-
61
- const initQuery = await tryCatch(async () => ({ query: await getPubSubManagerInitQuery.bind(dboBuilder)() }));
62
- if(initQuery.hasError){
63
- console.error(initQuery.error);
64
- return "Could not get initQuery. Check logs";
65
- }
66
-
67
- if(!initQuery.query){
68
- return undefined;
69
- }
70
-
71
- return undefined;
72
- }