prostgles-server 3.0.86 → 3.0.88
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.
- package/.eslintignore +5 -0
- package/.eslintrc.json +30 -0
- package/dist/DBEventsManager.js +1 -1
- package/dist/DBEventsManager.js.map +1 -1
- package/dist/DBSchemaBuilder.d.ts.map +1 -1
- package/dist/DBSchemaBuilder.js +6 -6
- package/dist/DBSchemaBuilder.js.map +1 -1
- package/dist/DboBuilder/QueryBuilder/Functions.js +9 -9
- package/dist/DboBuilder/QueryBuilder/Functions.js.map +1 -1
- package/dist/DboBuilder/QueryBuilder/QueryBuilder.d.ts.map +1 -1
- package/dist/DboBuilder/QueryBuilder/QueryBuilder.js +8 -7
- package/dist/DboBuilder/QueryBuilder/QueryBuilder.js.map +1 -1
- package/dist/DboBuilder/QueryBuilder/makeSelectQuery.d.ts.map +1 -1
- package/dist/DboBuilder/QueryBuilder/makeSelectQuery.js +4 -4
- package/dist/DboBuilder/QueryBuilder/makeSelectQuery.js.map +1 -1
- package/dist/DboBuilder/TableHandler.d.ts.map +1 -1
- package/dist/DboBuilder/TableHandler.js +18 -20
- package/dist/DboBuilder/TableHandler.js.map +1 -1
- package/dist/DboBuilder/ViewHandler.d.ts +0 -10
- package/dist/DboBuilder/ViewHandler.d.ts.map +1 -1
- package/dist/DboBuilder/ViewHandler.js +50 -63
- package/dist/DboBuilder/ViewHandler.js.map +1 -1
- package/dist/DboBuilder/delete.js +0 -1
- package/dist/DboBuilder/delete.js.map +1 -1
- package/dist/DboBuilder/getColumns.js +5 -4
- package/dist/DboBuilder/getColumns.js.map +1 -1
- package/dist/DboBuilder/getCondition.d.ts.map +1 -1
- package/dist/DboBuilder/getCondition.js +6 -6
- package/dist/DboBuilder/getCondition.js.map +1 -1
- package/dist/DboBuilder/insert.js +15 -16
- package/dist/DboBuilder/insert.js.map +1 -1
- package/dist/DboBuilder/insertDataParse.d.ts.map +1 -1
- package/dist/DboBuilder/insertDataParse.js +9 -10
- package/dist/DboBuilder/insertDataParse.js.map +1 -1
- package/dist/DboBuilder/parseUpdateRules.js +2 -2
- package/dist/DboBuilder/parseUpdateRules.js.map +1 -1
- package/dist/DboBuilder/runSQL.d.ts.map +1 -1
- package/dist/DboBuilder/runSQL.js +5 -5
- package/dist/DboBuilder/runSQL.js.map +1 -1
- package/dist/DboBuilder/subscribe.js +3 -3
- package/dist/DboBuilder/subscribe.js.map +1 -1
- package/dist/DboBuilder/update.js +5 -6
- package/dist/DboBuilder/update.js.map +1 -1
- package/dist/DboBuilder/uploadFile.d.ts.map +1 -1
- package/dist/DboBuilder/uploadFile.js +1 -1
- package/dist/DboBuilder/uploadFile.js.map +1 -1
- package/dist/DboBuilder.d.ts.map +1 -1
- package/dist/DboBuilder.js +13 -14
- package/dist/DboBuilder.js.map +1 -1
- package/dist/FileManager.d.ts.map +1 -1
- package/dist/FileManager.js +3 -5
- package/dist/FileManager.js.map +1 -1
- package/dist/Filtering.js +7 -7
- package/dist/Filtering.js.map +1 -1
- package/dist/JSONBValidation/validate_jsonb_schema_sql.d.ts +3 -0
- package/dist/JSONBValidation/validate_jsonb_schema_sql.d.ts.map +1 -0
- package/dist/JSONBValidation/validate_jsonb_schema_sql.js +295 -0
- package/dist/JSONBValidation/validate_jsonb_schema_sql.js.map +1 -0
- package/dist/JSONBValidation/validation.d.ts +108 -0
- package/dist/JSONBValidation/validation.d.ts.map +1 -0
- package/dist/JSONBValidation/validation.js +222 -0
- package/dist/JSONBValidation/validation.js.map +1 -0
- package/dist/PostgresNotifListenManager.js +1 -1
- package/dist/PostgresNotifListenManager.js.map +1 -1
- package/dist/Prostgles.d.ts.map +1 -1
- package/dist/Prostgles.js +20 -20
- package/dist/Prostgles.js.map +1 -1
- package/dist/PubSubManager/PubSubManager.d.ts.map +1 -1
- package/dist/PubSubManager/PubSubManager.js +10 -8
- package/dist/PubSubManager/PubSubManager.js.map +1 -1
- package/dist/PubSubManager/initPubSubManager.d.ts.map +1 -1
- package/dist/PubSubManager/initPubSubManager.js +10 -7
- package/dist/PubSubManager/initPubSubManager.js.map +1 -1
- package/dist/PublishParser.d.ts.map +1 -1
- package/dist/PublishParser.js +122 -125
- package/dist/PublishParser.js.map +1 -1
- package/dist/SyncReplication.d.ts.map +1 -1
- package/dist/SyncReplication.js +19 -16
- package/dist/SyncReplication.js.map +1 -1
- package/dist/TableConfig.d.ts +9 -5
- package/dist/TableConfig.d.ts.map +1 -1
- package/dist/TableConfig.js +33 -12
- package/dist/TableConfig.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/shortestPath.js +11 -11
- package/dist/shortestPath.js.map +1 -1
- package/dist/validation.d.ts +50 -24
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +177 -53
- package/dist/validation.js.map +1 -1
- package/lib/AuthHandler.d.ts +11 -11
- package/lib/AuthHandler.d.ts.map +1 -1
- package/lib/DBEventsManager.js +1 -1
- package/lib/DBEventsManager.ts +1 -1
- package/lib/DBSchemaBuilder.d.ts +3 -3
- package/lib/DBSchemaBuilder.d.ts.map +1 -1
- package/lib/DBSchemaBuilder.js +6 -6
- package/lib/DBSchemaBuilder.ts +10 -9
- package/lib/DboBuilder/QueryBuilder/Functions.d.ts +3 -3
- package/lib/DboBuilder/QueryBuilder/Functions.d.ts.map +1 -1
- package/lib/DboBuilder/QueryBuilder/Functions.js +9 -9
- package/lib/DboBuilder/QueryBuilder/Functions.ts +13 -13
- package/lib/DboBuilder/QueryBuilder/QueryBuilder.d.ts +3 -3
- package/lib/DboBuilder/QueryBuilder/QueryBuilder.d.ts.map +1 -1
- package/lib/DboBuilder/QueryBuilder/QueryBuilder.js +8 -7
- package/lib/DboBuilder/QueryBuilder/QueryBuilder.ts +12 -12
- package/lib/DboBuilder/QueryBuilder/makeSelectQuery.d.ts.map +1 -1
- package/lib/DboBuilder/QueryBuilder/makeSelectQuery.js +4 -4
- package/lib/DboBuilder/QueryBuilder/makeSelectQuery.ts +5 -5
- package/lib/DboBuilder/TableHandler.d.ts +1 -1
- package/lib/DboBuilder/TableHandler.d.ts.map +1 -1
- package/lib/DboBuilder/TableHandler.js +18 -20
- package/lib/DboBuilder/TableHandler.ts +21 -20
- package/lib/DboBuilder/ViewHandler.d.ts +1 -11
- package/lib/DboBuilder/ViewHandler.d.ts.map +1 -1
- package/lib/DboBuilder/ViewHandler.js +50 -63
- package/lib/DboBuilder/ViewHandler.ts +68 -97
- package/lib/DboBuilder/delete.js +0 -1
- package/lib/DboBuilder/delete.ts +1 -1
- package/lib/DboBuilder/getColumns.js +5 -4
- package/lib/DboBuilder/getColumns.ts +5 -5
- package/lib/DboBuilder/getCondition.d.ts.map +1 -1
- package/lib/DboBuilder/getCondition.js +6 -6
- package/lib/DboBuilder/getCondition.ts +7 -7
- package/lib/DboBuilder/insert.js +15 -16
- package/lib/DboBuilder/insert.ts +18 -18
- package/lib/DboBuilder/insertDataParse.d.ts.map +1 -1
- package/lib/DboBuilder/insertDataParse.js +9 -10
- package/lib/DboBuilder/insertDataParse.ts +42 -43
- package/lib/DboBuilder/parseUpdateRules.js +2 -2
- package/lib/DboBuilder/parseUpdateRules.ts +2 -2
- package/lib/DboBuilder/runSQL.d.ts.map +1 -1
- package/lib/DboBuilder/runSQL.js +5 -5
- package/lib/DboBuilder/runSQL.ts +6 -6
- package/lib/DboBuilder/subscribe.d.ts +1 -1
- package/lib/DboBuilder/subscribe.d.ts.map +1 -1
- package/lib/DboBuilder/subscribe.js +3 -3
- package/lib/DboBuilder/subscribe.ts +3 -3
- package/lib/DboBuilder/update.js +5 -6
- package/lib/DboBuilder/update.ts +6 -6
- package/lib/DboBuilder/uploadFile.d.ts.map +1 -1
- package/lib/DboBuilder/uploadFile.js +1 -1
- package/lib/DboBuilder/uploadFile.ts +2 -2
- package/lib/DboBuilder.d.ts +22 -22
- package/lib/DboBuilder.d.ts.map +1 -1
- package/lib/DboBuilder.js +13 -14
- package/lib/DboBuilder.ts +19 -19
- package/lib/FileManager.d.ts +6 -6
- package/lib/FileManager.d.ts.map +1 -1
- package/lib/FileManager.js +3 -5
- package/lib/FileManager.ts +7 -6
- package/lib/Filtering.d.ts +1 -1
- package/lib/Filtering.d.ts.map +1 -1
- package/lib/Filtering.js +7 -7
- package/lib/Filtering.ts +7 -7
- package/lib/JSONBValidation/validate_jsonb_schema_sql.d.ts +3 -0
- package/lib/JSONBValidation/validate_jsonb_schema_sql.d.ts.map +1 -0
- package/lib/JSONBValidation/validate_jsonb_schema_sql.js +294 -0
- package/lib/JSONBValidation/validate_jsonb_schema_sql.ts +293 -0
- package/lib/JSONBValidation/validation.d.ts +108 -0
- package/lib/JSONBValidation/validation.d.ts.map +1 -0
- package/lib/JSONBValidation/validation.js +221 -0
- package/lib/JSONBValidation/validation.ts +332 -0
- package/lib/PostgresNotifListenManager.d.ts +1 -1
- package/lib/PostgresNotifListenManager.d.ts.map +1 -1
- package/lib/PostgresNotifListenManager.js +1 -1
- package/lib/PostgresNotifListenManager.ts +1 -1
- package/lib/Prostgles.d.ts +14 -14
- package/lib/Prostgles.d.ts.map +1 -1
- package/lib/Prostgles.js +20 -19
- package/lib/Prostgles.ts +22 -21
- package/lib/PubSubManager/PubSubManager.d.ts +7 -7
- package/lib/PubSubManager/PubSubManager.d.ts.map +1 -1
- package/lib/PubSubManager/PubSubManager.js +10 -8
- package/lib/PubSubManager/PubSubManager.ts +13 -8
- package/lib/PubSubManager/initPubSubManager.d.ts.map +1 -1
- package/lib/PubSubManager/initPubSubManager.js +10 -7
- package/lib/PubSubManager/initPubSubManager.ts +12 -7
- package/lib/PublishParser.d.ts +32 -32
- package/lib/PublishParser.d.ts.map +1 -1
- package/lib/PublishParser.js +121 -124
- package/lib/PublishParser.ts +125 -127
- package/lib/SchemaWatch.d.ts +1 -1
- package/lib/SchemaWatch.d.ts.map +1 -1
- package/lib/SyncReplication.d.ts +5 -5
- package/lib/SyncReplication.d.ts.map +1 -1
- package/lib/SyncReplication.js +19 -16
- package/lib/SyncReplication.ts +470 -471
- package/lib/TableConfig.d.ts +28 -24
- package/lib/TableConfig.d.ts.map +1 -1
- package/lib/TableConfig.js +33 -12
- package/lib/TableConfig.ts +55 -21
- package/lib/index.js +1 -1
- package/lib/index.ts +1 -1
- package/lib/shortestPath.d.ts +1 -1
- package/lib/shortestPath.d.ts.map +1 -1
- package/lib/shortestPath.js +11 -11
- package/lib/shortestPath.ts +11 -11
- package/package.json +10 -6
- package/tests/client/PID.txt +1 -1
- package/tests/client/package-lock.json +53 -31
- package/tests/client/package.json +4 -1
- package/tests/isomorphic_queries.d.ts.map +1 -1
- package/tests/isomorphic_queries.js +28 -26
- package/tests/isomorphic_queries.ts +30 -29
- package/tests/server/DBoGenerated.d.ts +1 -1
- package/tests/server/index.js +8 -7
- package/tests/server/index.ts +10 -8
- package/tests/server/package-lock.json +76 -58
- package/tests/server/package.json +2 -2
- package/tests/server/server.ts +2 -3
- package/lib/validation.d.ts +0 -100
- package/lib/validation.d.ts.map +0 -1
- package/lib/validation.js +0 -280
- package/lib/validation.ts +0 -360
package/lib/DboBuilder/insert.js
CHANGED
|
@@ -10,7 +10,7 @@ async function insert(rowOrRows, param2, param3_unused, tableRules, localParams)
|
|
|
10
10
|
try {
|
|
11
11
|
const { onConflictDoNothing, fixIssues = false } = param2 || {};
|
|
12
12
|
const { testRule = false, returnQuery = false } = localParams || {};
|
|
13
|
-
|
|
13
|
+
const { returning } = param2 || {};
|
|
14
14
|
const finalDBtx = localParams?.tx?.dbTX || this.dbTX;
|
|
15
15
|
if (tableRules?.[ACTION]?.postValidate) {
|
|
16
16
|
if (!finalDBtx) {
|
|
@@ -72,19 +72,19 @@ async function insert(rowOrRows, param2, param3_unused, tableRules, localParams)
|
|
|
72
72
|
rowOrRows = {}; //throw "Provide data in param1";
|
|
73
73
|
/** TODO: use WITH inserted as (query) SELECT jsonb_agg(inserted.*) as validateReturn, userReturning */
|
|
74
74
|
const originalReturning = await this.prepareReturning(returning, this.parseFieldFilter(returningFields));
|
|
75
|
-
|
|
75
|
+
const fullReturning = await this.prepareReturning(returning, this.parseFieldFilter("*"));
|
|
76
76
|
/** Used for postValidate. Add any missing computed returning from original query */
|
|
77
77
|
fullReturning.concat(originalReturning.filter(s => !fullReturning.some(f => f.alias === s.alias)));
|
|
78
78
|
const finalSelect = tableRules?.[ACTION]?.postValidate ? fullReturning : originalReturning;
|
|
79
|
-
|
|
79
|
+
const returningSelect = this.makeReturnQuery(finalSelect);
|
|
80
80
|
const makeQuery = async (_row, isOne = false) => {
|
|
81
|
-
|
|
81
|
+
const row = { ..._row };
|
|
82
82
|
if (!(0, prostgles_types_1.isObject)(row)) {
|
|
83
83
|
console.trace(row);
|
|
84
84
|
throw "\ninvalid insert data provided -> " + JSON.stringify(row);
|
|
85
85
|
}
|
|
86
86
|
const { data, allowedCols } = this.validateNewData({ row, forcedData, allowedFields: fields, tableRules, fixIssues });
|
|
87
|
-
|
|
87
|
+
const _data = { ...data };
|
|
88
88
|
let insertQ = "";
|
|
89
89
|
if (!Array.isArray(_data) && !(0, prostgles_types_1.getKeys)(_data).length || Array.isArray(_data) && !_data.length) {
|
|
90
90
|
await tableRules?.[ACTION]?.validate?.(_data, this.dbTX || this.dboBuilder.dbo);
|
|
@@ -107,7 +107,7 @@ async function insert(rowOrRows, param2, param3_unused, tableRules, localParams)
|
|
|
107
107
|
return insertResult;
|
|
108
108
|
}
|
|
109
109
|
if (Array.isArray(data)) {
|
|
110
|
-
|
|
110
|
+
const queries = await Promise.all(data.map(async (p) => {
|
|
111
111
|
const q = await makeQuery(p);
|
|
112
112
|
return q;
|
|
113
113
|
}));
|
|
@@ -163,13 +163,12 @@ async function insert(rowOrRows, param2, param3_unused, tableRules, localParams)
|
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
165
|
exports.insert = insert;
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
};
|
|
166
|
+
// const removeBuffers = (o: any) => {
|
|
167
|
+
// if(isPlainObject(o)){
|
|
168
|
+
// return JSON.stringify(getKeys(o).reduce((a, k) => {
|
|
169
|
+
// const value = o[k]
|
|
170
|
+
// return { ...a, [k]: Buffer.isBuffer(value)? `Buffer[${value.byteLength}][...REMOVED]` : value
|
|
171
|
+
// }
|
|
172
|
+
// }, {}));
|
|
173
|
+
// }
|
|
174
|
+
// }
|
package/lib/DboBuilder/insert.ts
CHANGED
|
@@ -14,7 +14,7 @@ export async function insert(this: TableHandler, rowOrRows: (AnyObject | AnyObje
|
|
|
14
14
|
const { onConflictDoNothing, fixIssues = false } = param2 || {};
|
|
15
15
|
const { testRule = false, returnQuery = false } = localParams || {};
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
const { returning } = param2 || {};
|
|
18
18
|
const finalDBtx = localParams?.tx?.dbTX || this.dbTX;
|
|
19
19
|
if(tableRules?.[ACTION]?.postValidate ){
|
|
20
20
|
if(!finalDBtx){
|
|
@@ -83,16 +83,16 @@ export async function insert(this: TableHandler, rowOrRows: (AnyObject | AnyObje
|
|
|
83
83
|
|
|
84
84
|
/** TODO: use WITH inserted as (query) SELECT jsonb_agg(inserted.*) as validateReturn, userReturning */
|
|
85
85
|
const originalReturning = await this.prepareReturning(returning, this.parseFieldFilter(returningFields))
|
|
86
|
-
|
|
86
|
+
const fullReturning = await this.prepareReturning(returning, this.parseFieldFilter("*"));
|
|
87
87
|
|
|
88
88
|
/** Used for postValidate. Add any missing computed returning from original query */
|
|
89
89
|
fullReturning.concat(originalReturning.filter(s => !fullReturning.some(f => f.alias === s.alias)));
|
|
90
90
|
|
|
91
91
|
const finalSelect = tableRules?.[ACTION]?.postValidate? fullReturning : originalReturning;
|
|
92
|
-
|
|
92
|
+
const returningSelect = this.makeReturnQuery(finalSelect);
|
|
93
93
|
|
|
94
94
|
const makeQuery = async (_row: AnyObject | undefined, isOne = false) => {
|
|
95
|
-
|
|
95
|
+
const row = { ..._row };
|
|
96
96
|
|
|
97
97
|
if (!isObject(row)) {
|
|
98
98
|
console.trace(row)
|
|
@@ -100,7 +100,7 @@ export async function insert(this: TableHandler, rowOrRows: (AnyObject | AnyObje
|
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
const { data, allowedCols } = this.validateNewData({ row, forcedData, allowedFields: fields, tableRules, fixIssues });
|
|
103
|
-
|
|
103
|
+
const _data = { ...data };
|
|
104
104
|
|
|
105
105
|
let insertQ = "";
|
|
106
106
|
if (!Array.isArray(_data) && !getKeys(_data).length || Array.isArray(_data) && !_data.length) {
|
|
@@ -126,7 +126,7 @@ export async function insert(this: TableHandler, rowOrRows: (AnyObject | AnyObje
|
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
if (Array.isArray(data)) {
|
|
129
|
-
|
|
129
|
+
const queries = await Promise.all(data.map(async p => {
|
|
130
130
|
const q = await makeQuery(p);
|
|
131
131
|
return q;
|
|
132
132
|
}));
|
|
@@ -182,15 +182,15 @@ export async function insert(this: TableHandler, rowOrRows: (AnyObject | AnyObje
|
|
|
182
182
|
// ${JSON.stringify(param2 || {}, null, 2)}
|
|
183
183
|
throw parseError(e, `dbo.${this.name}.${ACTION}()`)
|
|
184
184
|
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const removeBuffers = (o: any) => {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
// const removeBuffers = (o: any) => {
|
|
189
|
+
// if(isPlainObject(o)){
|
|
190
|
+
// return JSON.stringify(getKeys(o).reduce((a, k) => {
|
|
191
|
+
// const value = o[k]
|
|
192
|
+
// return { ...a, [k]: Buffer.isBuffer(value)? `Buffer[${value.byteLength}][...REMOVED]` : value
|
|
193
|
+
// }
|
|
194
|
+
// }, {}));
|
|
195
|
+
// }
|
|
196
|
+
// }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"insertDataParse.d.ts","sourceRoot":"","sources":["insertDataParse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAW,YAAY,EAAgD,MAAM,iBAAiB,CAAC;AACjH,OAAO,EAAE,WAAW,EAAiB,MAAM,eAAe,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C;;GAEG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAE,YAAY,EAClB,IAAI,EAAE,CAAC,SAAS,GAAG,SAAS,EAAE,CAAC,EAC/B,MAAM,CAAC,EAAE,YAAY,EACrB,aAAa,CAAC,EAAE,SAAS,EACzB,UAAU,CAAC,EAAE,SAAS,EACtB,YAAY,CAAC,EAAE,WAAW,GACzB,OAAO,CAAC;IACT,IAAI,CAAC,EAAE,SAAS,GAAG,SAAS,EAAE,CAAC;IAC/B,YAAY,CAAC,EAAE,SAAS,GAAG,SAAS,EAAE,CAAC;CACxC,CAAC,
|
|
1
|
+
{"version":3,"file":"insertDataParse.d.ts","sourceRoot":"","sources":["insertDataParse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAW,YAAY,EAAgD,MAAM,iBAAiB,CAAC;AACjH,OAAO,EAAE,WAAW,EAAiB,MAAM,eAAe,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C;;GAEG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAE,YAAY,EAClB,IAAI,EAAE,CAAC,SAAS,GAAG,SAAS,EAAE,CAAC,EAC/B,MAAM,CAAC,EAAE,YAAY,EACrB,aAAa,CAAC,EAAE,SAAS,EACzB,UAAU,CAAC,EAAE,SAAS,EACtB,YAAY,CAAC,EAAE,WAAW,GACzB,OAAO,CAAC;IACT,IAAI,CAAC,EAAE,SAAS,GAAG,SAAS,EAAE,CAAC;IAC/B,YAAY,CAAC,EAAE,SAAS,GAAG,SAAS,EAAE,CAAC;CACxC,CAAC,CAmQD"}
|
|
@@ -72,9 +72,8 @@ async function insertDataParse(data, param2, param3_unused, tableRules, _localPa
|
|
|
72
72
|
insertResult: await this.dboBuilder.getTX((dbTX, _t) => dbTX[this.name].insert(data, param2, param3_unused, tableRules, { tx: { dbTX, t: _t }, ...localParams }))
|
|
73
73
|
};
|
|
74
74
|
}
|
|
75
|
-
// if(!dbTX && this.t) dbTX = this.d;
|
|
76
75
|
const preValidate = tableRules?.insert?.preValidate, validate = tableRules?.insert?.validate;
|
|
77
|
-
|
|
76
|
+
const _data = await Promise.all((isMultiInsert ? data : [data]).map(async (row) => {
|
|
78
77
|
if (preValidate) {
|
|
79
78
|
row = await preValidate(row, this.dbTX || this.dboBuilder.dbo);
|
|
80
79
|
}
|
|
@@ -90,7 +89,7 @@ async function insertDataParse(data, param2, param3_unused, tableRules, _localPa
|
|
|
90
89
|
const _this = this.t ? this : dbTX[this.name];
|
|
91
90
|
const omitedKeys = extraKeys.concat(colInserts.map(c => c.col));
|
|
92
91
|
// let rootData = isMultiInsert? data.map(d => omitKeys(d, omitedKeys)) : omitKeys(data, omitedKeys);
|
|
93
|
-
|
|
92
|
+
const rootData = (0, PubSubManager_1.omitKeys)(row, omitedKeys);
|
|
94
93
|
let insertedChildren;
|
|
95
94
|
let targetTableRules;
|
|
96
95
|
/** Insert referenced first and then populate root data with referenced keys */
|
|
@@ -146,7 +145,7 @@ async function insertDataParse(data, param2, param3_unused, tableRules, _localPa
|
|
|
146
145
|
throw `Target table ${tbl2} does not reference any columns from the root table ${this.name}. Cannot do nested insert`;
|
|
147
146
|
// console.log(childDataItems, JSON.stringify(colsRefT1, null, 2))
|
|
148
147
|
insertedChildren = await childInsert(childDataItems.map((d) => {
|
|
149
|
-
|
|
148
|
+
const result = { ...d };
|
|
150
149
|
colsRefT1.map(col => {
|
|
151
150
|
result[col.references[0].cols[0]] = fullRootResult[col.references[0].fcols[0]];
|
|
152
151
|
});
|
|
@@ -173,7 +172,7 @@ async function insertDataParse(data, param2, param3_unused, tableRules, _localPa
|
|
|
173
172
|
insertedChildren = await childInsert(childDataItems, targetTable);
|
|
174
173
|
/* Insert in key_lookup table */
|
|
175
174
|
await Promise.all(insertedChildren.map(async (t3Child) => {
|
|
176
|
-
|
|
175
|
+
const tbl2Row = {};
|
|
177
176
|
colsRefT3.map(col => {
|
|
178
177
|
tbl2Row[col.name] = t3Child[col.references[0].fcols[0]];
|
|
179
178
|
});
|
|
@@ -192,9 +191,9 @@ async function insertDataParse(data, param2, param3_unused, tableRules, _localPa
|
|
|
192
191
|
if (targetTableRules && insertedChildren?.length && returning) {
|
|
193
192
|
const targetTableHandler = dbTX[targetTable];
|
|
194
193
|
const targetReturning = await targetTableHandler.prepareReturning("*", targetTableHandler.parseFieldFilter(targetTableRules?.insert?.returningFields));
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
194
|
+
const clientTargetInserts = insertedChildren.map(d => {
|
|
195
|
+
const _d = { ...d };
|
|
196
|
+
const res = {};
|
|
198
197
|
targetReturning.map(r => {
|
|
199
198
|
res[r.alias] = _d[r.alias];
|
|
200
199
|
});
|
|
@@ -207,11 +206,11 @@ async function insertDataParse(data, param2, param3_unused, tableRules, _localPa
|
|
|
207
206
|
}
|
|
208
207
|
return row;
|
|
209
208
|
}));
|
|
210
|
-
|
|
209
|
+
const result = isMultiInsert ? _data : _data[0];
|
|
211
210
|
// if(validate && !isNestedInsert){
|
|
212
211
|
// result = isMultiInsert? await Promise.all(_data.map(async d => await validate({ ...d }))) : await validate({ ..._data[0] });
|
|
213
212
|
// }
|
|
214
|
-
|
|
213
|
+
const res = isNestedInsert ?
|
|
215
214
|
{ insertResult: result } :
|
|
216
215
|
{ data: result };
|
|
217
216
|
return res;
|
|
@@ -9,11 +9,11 @@ import { uploadFile } from "./uploadFile";
|
|
|
9
9
|
* Used for doing referenced inserts within a single transaction
|
|
10
10
|
*/
|
|
11
11
|
export async function insertDataParse(
|
|
12
|
-
this: TableHandler,
|
|
13
|
-
data: (AnyObject | AnyObject[]),
|
|
14
|
-
param2?: InsertParams,
|
|
15
|
-
param3_unused?: undefined,
|
|
16
|
-
tableRules?: TableRule,
|
|
12
|
+
this: TableHandler,
|
|
13
|
+
data: (AnyObject | AnyObject[]),
|
|
14
|
+
param2?: InsertParams,
|
|
15
|
+
param3_unused?: undefined,
|
|
16
|
+
tableRules?: TableRule,
|
|
17
17
|
_localParams?: LocalParams
|
|
18
18
|
): Promise<{
|
|
19
19
|
data?: AnyObject | AnyObject[];
|
|
@@ -25,12 +25,12 @@ export async function insertDataParse(
|
|
|
25
25
|
const isMultiInsert = Array.isArray(data);
|
|
26
26
|
const getExtraKeys = (d: AnyObject) => getKeys(d).filter(k => {
|
|
27
27
|
/* If media then use file insert columns */
|
|
28
|
-
if(this.is_media){
|
|
28
|
+
if (this.is_media) {
|
|
29
29
|
return !this.column_names.concat(MEDIA_COL_NAMES).includes(k)
|
|
30
|
-
} else if(!this.columns.find(c => c.name === k)){
|
|
31
|
-
if(!isObject(d[k]) && !Array.isArray(d[k])){
|
|
30
|
+
} else if (!this.columns.find(c => c.name === k)) {
|
|
31
|
+
if (!isObject(d[k]) && !Array.isArray(d[k])) {
|
|
32
32
|
throw new Error("Invalid/Dissalowed field in data: " + k)
|
|
33
|
-
} else if(!this.dboBuilder.dbo[k]){
|
|
33
|
+
} else if (!this.dboBuilder.dbo[k]) {
|
|
34
34
|
throw new Error("Invalid/Dissalowed nested insert table name in data: " + k)
|
|
35
35
|
}
|
|
36
36
|
return true;
|
|
@@ -41,29 +41,29 @@ export async function insertDataParse(
|
|
|
41
41
|
const getColumnInserts = (d: AnyObject) => getKeys(d)
|
|
42
42
|
.filter(k => d[k] && isObject(d[k]))
|
|
43
43
|
.map(k => {
|
|
44
|
-
const insertedCol = this.columns.find(c =>
|
|
45
|
-
c.name === k &&
|
|
44
|
+
const insertedCol = this.columns.find(c =>
|
|
45
|
+
c.name === k &&
|
|
46
46
|
ALLOWED_COL_TYPES.includes(c.udt_name)
|
|
47
47
|
);
|
|
48
48
|
const referencesMultipleTables = insertedCol?.references?.length !== 1;
|
|
49
49
|
const referencesMultipleColumns = insertedCol?.references?.some(refs => refs.fcols.length !== 1);
|
|
50
|
-
if(insertedCol && (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
)
|
|
50
|
+
if (insertedCol && (
|
|
51
|
+
referencesMultipleTables ||
|
|
52
|
+
referencesMultipleColumns ||
|
|
53
|
+
this.columns.some(c =>
|
|
54
|
+
// c.name !== insertedCol.name && // a bit reduntant: Cannot have one col reference two columns
|
|
55
|
+
c.references?.some(({ ftable, fcols }) =>
|
|
56
|
+
insertedCol.references?.some(inserted =>
|
|
57
|
+
ftable === inserted.ftable && // same ftable
|
|
58
|
+
fcols[0] !== inserted.fcols[0] // different fcols
|
|
59
|
+
)
|
|
61
60
|
)
|
|
62
61
|
)
|
|
63
|
-
)
|
|
62
|
+
)
|
|
63
|
+
) {
|
|
64
64
|
throw "A reference column insert is not possible for multiple column relationships ";
|
|
65
65
|
}
|
|
66
|
-
if(insertedCol){
|
|
66
|
+
if (insertedCol) {
|
|
67
67
|
return {
|
|
68
68
|
tableName: insertedCol.references![0].ftable!,
|
|
69
69
|
col: insertedCol.name,
|
|
@@ -73,7 +73,7 @@ export async function insertDataParse(
|
|
|
73
73
|
return undefined;
|
|
74
74
|
}).filter(isDefined);
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
|
|
77
77
|
/**
|
|
78
78
|
* True when: nested table data is provided within
|
|
79
79
|
* [nestedTable] property key
|
|
@@ -102,12 +102,11 @@ export async function insertDataParse(
|
|
|
102
102
|
)
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
// if(!dbTX && this.t) dbTX = this.d;
|
|
106
105
|
|
|
107
106
|
const preValidate = tableRules?.insert?.preValidate,
|
|
108
107
|
validate = tableRules?.insert?.validate;
|
|
109
108
|
|
|
110
|
-
|
|
109
|
+
const _data = await Promise.all((isMultiInsert ? data : [data]).map(async row => {
|
|
111
110
|
if (preValidate) {
|
|
112
111
|
row = await preValidate(row, this.dbTX || this.dboBuilder.dbo);
|
|
113
112
|
}
|
|
@@ -128,14 +127,14 @@ export async function insertDataParse(
|
|
|
128
127
|
const omitedKeys = extraKeys.concat(colInserts.map(c => c.col));
|
|
129
128
|
|
|
130
129
|
// let rootData = isMultiInsert? data.map(d => omitKeys(d, omitedKeys)) : omitKeys(data, omitedKeys);
|
|
131
|
-
|
|
130
|
+
const rootData: AnyObject = omitKeys(row, omitedKeys);
|
|
132
131
|
|
|
133
132
|
let insertedChildren: AnyObject[];
|
|
134
133
|
let targetTableRules: TableRule;
|
|
135
134
|
|
|
136
135
|
/** Insert referenced first and then populate root data with referenced keys */
|
|
137
|
-
if(colInserts.length){
|
|
138
|
-
for await(const colInsert of colInserts){
|
|
136
|
+
if (colInserts.length) {
|
|
137
|
+
for await (const colInsert of colInserts) {
|
|
139
138
|
const newLocalParams: LocalParams = {
|
|
140
139
|
...(localParams ?? {}),
|
|
141
140
|
nestedInsert: {
|
|
@@ -146,10 +145,10 @@ export async function insertDataParse(
|
|
|
146
145
|
}
|
|
147
146
|
}
|
|
148
147
|
const colRows = await referencedInsert(_this, dbTX, newLocalParams, colInsert.tableName, row[colInsert.col]);
|
|
149
|
-
if(!Array.isArray(colRows) || colRows.length !== 1 || [null, undefined].includes(colRows[0][colInsert.fcol])){
|
|
148
|
+
if (!Array.isArray(colRows) || colRows.length !== 1 || [null, undefined].includes(colRows[0][colInsert.fcol])) {
|
|
150
149
|
throw new Error("Could not do nested column insert: Unexpected return " + JSON.stringify(colRows))
|
|
151
150
|
}
|
|
152
|
-
|
|
151
|
+
|
|
153
152
|
const foreignKey = colRows[0][colInsert.fcol];
|
|
154
153
|
rootData[colInsert.col] = foreignKey;
|
|
155
154
|
}
|
|
@@ -195,7 +194,7 @@ export async function insertDataParse(
|
|
|
195
194
|
// console.log(childDataItems, JSON.stringify(colsRefT1, null, 2))
|
|
196
195
|
insertedChildren = await childInsert(
|
|
197
196
|
childDataItems.map((d: AnyObject) => {
|
|
198
|
-
|
|
197
|
+
const result = { ...d };
|
|
199
198
|
colsRefT1.map(col => {
|
|
200
199
|
result[col.references![0].cols[0]] = fullRootResult[col.references![0].fcols[0]]
|
|
201
200
|
})
|
|
@@ -219,7 +218,7 @@ export async function insertDataParse(
|
|
|
219
218
|
if (!cols2 || !(
|
|
220
219
|
cols2.filter(c => c.references?.[0].ftable === fileTable).length === 1 &&
|
|
221
220
|
cols2.filter(c => c.references?.[0].ftable === _this.name).length === 1
|
|
222
|
-
)){
|
|
221
|
+
)) {
|
|
223
222
|
console.log({ tbl1, tbl2, tbl3, name: _this.name, tthisName: this.name })
|
|
224
223
|
throw "Second joining table (" + tbl2 + ") not of expected format. Must contain exactly one reference column for each table (file table and target table) ";
|
|
225
224
|
}
|
|
@@ -228,7 +227,7 @@ export async function insertDataParse(
|
|
|
228
227
|
|
|
229
228
|
/* Insert in key_lookup table */
|
|
230
229
|
await Promise.all(insertedChildren.map(async t3Child => {
|
|
231
|
-
|
|
230
|
+
const tbl2Row: AnyObject = {};
|
|
232
231
|
|
|
233
232
|
colsRefT3.map(col => {
|
|
234
233
|
tbl2Row[col.name] = t3Child[col.references![0].fcols[0]];
|
|
@@ -250,9 +249,9 @@ export async function insertDataParse(
|
|
|
250
249
|
if (targetTableRules && insertedChildren?.length && returning) {
|
|
251
250
|
const targetTableHandler = dbTX![targetTable] as TableHandler;
|
|
252
251
|
const targetReturning = await targetTableHandler.prepareReturning("*", targetTableHandler.parseFieldFilter(targetTableRules?.insert?.returningFields));
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
252
|
+
const clientTargetInserts = insertedChildren.map(d => {
|
|
253
|
+
const _d = { ...d };
|
|
254
|
+
const res: AnyObject = {};
|
|
256
255
|
targetReturning.map(r => {
|
|
257
256
|
res[r.alias] = _d[r.alias]
|
|
258
257
|
});
|
|
@@ -269,11 +268,11 @@ export async function insertDataParse(
|
|
|
269
268
|
return row;
|
|
270
269
|
}));
|
|
271
270
|
|
|
272
|
-
|
|
271
|
+
const result = isMultiInsert ? _data : _data[0];
|
|
273
272
|
// if(validate && !isNestedInsert){
|
|
274
273
|
// result = isMultiInsert? await Promise.all(_data.map(async d => await validate({ ...d }))) : await validate({ ..._data[0] });
|
|
275
274
|
// }
|
|
276
|
-
|
|
275
|
+
const res = isNestedInsert ?
|
|
277
276
|
{ insertResult: result } :
|
|
278
277
|
{ data: result };
|
|
279
278
|
|
|
@@ -296,7 +295,7 @@ const getJoinPath = async (tableHandler: TableHandler, targetTable: string): Pro
|
|
|
296
295
|
const jp = tableHandler.dboBuilder.joinPaths.find(jp => jp.t1 === tableHandler.name && jp.t2 === targetTable);
|
|
297
296
|
if (!jp) {
|
|
298
297
|
console.trace(tableHandler.dboBuilder.joinPaths)
|
|
299
|
-
const pref = tableHandler.dboBuilder.prostgles.opts.joins !== "inferred"? "Joins are not inferred! " : ""
|
|
298
|
+
const pref = tableHandler.dboBuilder.prostgles.opts.joins !== "inferred" ? "Joins are not inferred! " : ""
|
|
300
299
|
throw new Error(`${pref}Could not find a single join path for the nested data ( sourceTable: ${tableHandler.name} targetTable: ${targetTable} ) `);
|
|
301
300
|
}
|
|
302
301
|
return jp;
|
|
@@ -307,7 +306,7 @@ const referencedInsert = async (tableHandler: TableHandler, dbTX: TableHandlers
|
|
|
307
306
|
|
|
308
307
|
const thisInfo = await tableHandler.getInfo();
|
|
309
308
|
await getJoinPath(tableHandler, targetTable);
|
|
310
|
-
|
|
309
|
+
|
|
311
310
|
if (!targetData || !dbTX?.[targetTable] || !("insert" in dbTX[targetTable])) throw new Error("childInsertErr: Table handler missing for referenced table: " + targetTable);
|
|
312
311
|
|
|
313
312
|
const childRules = await canInsert(tableHandler, targetTable, localParams);
|
|
@@ -326,5 +325,5 @@ const referencedInsert = async (tableHandler: TableHandler, dbTX: TableHandlers
|
|
|
326
325
|
})
|
|
327
326
|
)
|
|
328
327
|
);
|
|
329
|
-
|
|
328
|
+
|
|
330
329
|
}
|
|
@@ -87,7 +87,7 @@ async function parseUpdateRules(filter, newData, params, tableRules, localParams
|
|
|
87
87
|
try {
|
|
88
88
|
const { data, allowedCols } = this.validateNewData({ row: forcedData, forcedData: undefined, allowedFields: "*", tableRules, fixIssues: false });
|
|
89
89
|
const updateQ = await this.colSet.getUpdateQuery(data, allowedCols, this.dbTX || this.dboBuilder.dbo, validate ? ((row) => validate({ update: row, filter: {} }, this.dbTX || this.dboBuilder.dbo)) : undefined); //pgp.helpers.update(data, columnSet)
|
|
90
|
-
|
|
90
|
+
const query = updateQ + " WHERE FALSE ";
|
|
91
91
|
await this.db.any("EXPLAIN " + query);
|
|
92
92
|
}
|
|
93
93
|
catch (e) {
|
|
@@ -98,7 +98,7 @@ async function parseUpdateRules(filter, newData, params, tableRules, localParams
|
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
/* Update all allowed fields (fields) except the forcedFilter (so that the user cannot change the forced filter values) */
|
|
101
|
-
|
|
101
|
+
const _fields = this.parseFieldFilter(fields);
|
|
102
102
|
/**
|
|
103
103
|
* A forced filter must be basic
|
|
104
104
|
*/
|
|
@@ -127,7 +127,7 @@ export async function parseUpdateRules(
|
|
|
127
127
|
this.dbTX || this.dboBuilder.dbo,
|
|
128
128
|
validate ? ((row) => validate!({ update: row, filter: {} }, this.dbTX || this.dboBuilder.dbo)) : undefined
|
|
129
129
|
) //pgp.helpers.update(data, columnSet)
|
|
130
|
-
|
|
130
|
+
const query = updateQ + " WHERE FALSE ";
|
|
131
131
|
await this.db.any("EXPLAIN " + query);
|
|
132
132
|
} catch (e) {
|
|
133
133
|
throw " issue with forcedData: \nVALUE: " + JSON.stringify(forcedData, null, 2) + "\nERROR: " + e;
|
|
@@ -139,7 +139,7 @@ export async function parseUpdateRules(
|
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
/* Update all allowed fields (fields) except the forcedFilter (so that the user cannot change the forced filter values) */
|
|
142
|
-
|
|
142
|
+
const _fields = this.parseFieldFilter(fields);
|
|
143
143
|
|
|
144
144
|
/**
|
|
145
145
|
* A forced filter must be basic
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runSQL.d.ts","sourceRoot":"","sources":["runSQL.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"runSQL.d.ts","sourceRoot":"","sources":["runSQL.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,UAAU,EAAa,MAAM,iBAAiB,CAAC;AACnE,OAAO,EAAE,UAAU,EAAE,WAAW,EAAyB,MAAM,eAAe,CAAC;AAC/E,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAK7C,wBAAsB,MAAM,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,UAAU,GAAG,SAAS,EAAE,WAAW,CAAC,EAAE,WAAW,gBAuHpI;AAED,eAAO,MAAM,SAAS,cAAqB,SAAS,gBAAgB,WAAW,KAAG,QAAQ,OAAO,CAOhG,CAAA;AAED,eAAO,MAAM,eAAe,cAAmB,QAAQ,OAAO,CAE7D,CAAA"}
|
package/lib/DboBuilder/runSQL.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.canCreateTables = exports.canRunSQL = exports.runSQL = void 0;
|
|
4
4
|
const DboBuilder_1 = require("../DboBuilder");
|
|
5
5
|
const PubSubManager_1 = require("../PubSubManager/PubSubManager");
|
|
6
|
-
const
|
|
6
|
+
const pg_promise_1 = require("pg-promise");
|
|
7
7
|
async function runSQL(query, params, options, localParams) {
|
|
8
8
|
if (query?.replace(/\s\s+/g, ' ').toLowerCase().includes("create extension pg_stat_statements")) {
|
|
9
9
|
const { shared_preload_libraries } = await this.db.oneOrNone('SHOW shared_preload_libraries');
|
|
@@ -54,9 +54,9 @@ async function runSQL(query, params, options, localParams) {
|
|
|
54
54
|
else if (db) {
|
|
55
55
|
let finalQuery = query + "";
|
|
56
56
|
if (returnType === "arrayMode" && !["listen ", "notify "].find(c => query.toLowerCase().trim().startsWith(c))) {
|
|
57
|
-
finalQuery = new
|
|
57
|
+
finalQuery = (new pg_promise_1.ParameterizedQuery({ text: hasParams ? DboBuilder_1.pgp.as.format(query, params) : query, rowMode: "array" }));
|
|
58
58
|
}
|
|
59
|
-
|
|
59
|
+
const _qres = await db.result(finalQuery, hasParams ? params : undefined);
|
|
60
60
|
const { fields, rows, command } = _qres;
|
|
61
61
|
/**
|
|
62
62
|
* Fallback for watchSchema in case not superuser and cannot add db event listener
|
|
@@ -94,7 +94,7 @@ async function runSQL(query, params, options, localParams) {
|
|
|
94
94
|
return rows.map(r => Object.values(r[0]));
|
|
95
95
|
}
|
|
96
96
|
else {
|
|
97
|
-
|
|
97
|
+
const qres = {
|
|
98
98
|
duration: 0,
|
|
99
99
|
..._qres,
|
|
100
100
|
fields: fields?.map(f => {
|
|
@@ -122,7 +122,7 @@ const canRunSQL = async (prostgles, localParams) => {
|
|
|
122
122
|
return true;
|
|
123
123
|
const { socket } = localParams;
|
|
124
124
|
const publishParams = await prostgles.publishParser.getPublishParams({ socket });
|
|
125
|
-
|
|
125
|
+
const res = await prostgles.opts.publishRawSQL?.(publishParams);
|
|
126
126
|
return Boolean(res && typeof res === "boolean" || res === "*");
|
|
127
127
|
};
|
|
128
128
|
exports.canRunSQL = canRunSQL;
|
package/lib/DboBuilder/runSQL.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AnyObject, SQLOptions, SQLResult } from "prostgles-types";
|
|
2
2
|
import { DboBuilder, LocalParams, pgp, postgresToTsType } from "../DboBuilder";
|
|
3
3
|
import { DB, Prostgles } from "../Prostgles";
|
|
4
4
|
import { PubSubManager } from "../PubSubManager/PubSubManager";
|
|
5
|
-
|
|
5
|
+
import { ParameterizedQuery as PQ } from 'pg-promise';
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
export async function runSQL(this: DboBuilder, query: string, params: any, options: SQLOptions | undefined, localParams?: LocalParams) {
|
|
@@ -56,10 +56,10 @@ export async function runSQL(this: DboBuilder, query: string, params: any, optio
|
|
|
56
56
|
|
|
57
57
|
let finalQuery = query + "";
|
|
58
58
|
if (returnType === "arrayMode" && !["listen ", "notify "].find(c => query.toLowerCase().trim().startsWith(c))) {
|
|
59
|
-
finalQuery = new PQ({ text: hasParams ? pgp.as.format(query, params) : query, rowMode: "array" });
|
|
59
|
+
finalQuery = (new PQ({ text: hasParams ? pgp.as.format(query, params) : query, rowMode: "array" })) as any;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
const _qres = await db.result<AnyObject>(finalQuery, hasParams ? params : undefined)
|
|
63
63
|
const { fields, rows, command } = _qres;
|
|
64
64
|
|
|
65
65
|
/**
|
|
@@ -100,7 +100,7 @@ export async function runSQL(this: DboBuilder, query: string, params: any, optio
|
|
|
100
100
|
|
|
101
101
|
} else {
|
|
102
102
|
|
|
103
|
-
|
|
103
|
+
const qres: SQLResult<typeof returnType> = {
|
|
104
104
|
duration: 0,
|
|
105
105
|
..._qres,
|
|
106
106
|
fields: fields?.map(f => {
|
|
@@ -131,7 +131,7 @@ export const canRunSQL = async (prostgles: Prostgles, localParams?: LocalParams)
|
|
|
131
131
|
|
|
132
132
|
const { socket } = localParams;
|
|
133
133
|
const publishParams = await prostgles.publishParser!.getPublishParams({ socket });
|
|
134
|
-
|
|
134
|
+
const res = await prostgles.opts.publishRawSQL?.(publishParams);
|
|
135
135
|
return Boolean(res && typeof res === "boolean" || res === "*");
|
|
136
136
|
}
|
|
137
137
|
|
|
@@ -2,7 +2,7 @@ import { AnyObject, SubscribeParams } from "prostgles-types";
|
|
|
2
2
|
import { Filter, LocalParams } from "../DboBuilder";
|
|
3
3
|
import { TableRule } from "../PublishParser";
|
|
4
4
|
import { ViewHandler } from "./ViewHandler";
|
|
5
|
-
export
|
|
5
|
+
export type LocalFunc = (items: AnyObject[]) => any;
|
|
6
6
|
declare function subscribe(this: ViewHandler, filter: Filter, params: SubscribeParams, localFunc: LocalFunc): Promise<{
|
|
7
7
|
unsubscribe: () => any;
|
|
8
8
|
}>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscribe.d.ts","sourceRoot":"","sources":["subscribe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAU,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACrE,OAAO,EAAE,MAAM,EAAE,WAAW,EAAoC,MAAM,eAAe,CAAC;AACtF,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,
|
|
1
|
+
{"version":3,"file":"subscribe.d.ts","sourceRoot":"","sources":["subscribe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAU,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACrE,OAAO,EAAE,MAAM,EAAE,WAAW,EAAoC,MAAM,eAAe,CAAC;AACtF,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,MAAM,MAAM,SAAS,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,KAAK,GAAG,CAAC;AAEpD,iBAAe,SAAS,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC;IAAE,WAAW,EAAE,MAAM,GAAG,CAAA;CAAE,CAAC,CAAA;AAC/I,iBAAe,SAAS,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,SAAS,GAAG,SAAS,EAAE,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;AA4NzL,OAAO,EAAE,SAAS,EAAE,CAAA"}
|
|
@@ -55,7 +55,7 @@ async function subscribe(filter, params, localFunc, table_rules, localParams) {
|
|
|
55
55
|
relatedTables: []
|
|
56
56
|
};
|
|
57
57
|
viewOptions.relatedTables = await Promise.all(tableIds.map(async (tableID) => {
|
|
58
|
-
const table = this.dboBuilder.USER_TABLES
|
|
58
|
+
const table = this.dboBuilder.USER_TABLES.find(t => t.relid === +tableID);
|
|
59
59
|
let tableCols = tableColumns.filter(tc => tc.tableID.toString() === tableID);
|
|
60
60
|
/** If table has primary keys and they are all in this view then use only primary keys */
|
|
61
61
|
if (table?.pkey_columns?.every(pkey => tableCols.some(c => c.columnName === pkey))) {
|
|
@@ -101,7 +101,7 @@ async function subscribe(filter, params, localFunc, table_rules, localParams) {
|
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
return {
|
|
104
|
-
tableName
|
|
104
|
+
tableName,
|
|
105
105
|
tableNameEscaped: JSON.stringify(tableName),
|
|
106
106
|
condition: "TRUE"
|
|
107
107
|
};
|
|
@@ -159,7 +159,7 @@ async function subscribe(filter, params, localFunc, table_rules, localParams) {
|
|
|
159
159
|
const pubSubManager = await this.dboBuilder.getPubSubManager();
|
|
160
160
|
pubSubManager.removeLocalSub(this.name, condition, localFunc);
|
|
161
161
|
};
|
|
162
|
-
|
|
162
|
+
const res = Object.freeze({ unsubscribe });
|
|
163
163
|
return res;
|
|
164
164
|
}
|
|
165
165
|
}
|
|
@@ -75,7 +75,7 @@ async function subscribe(this: ViewHandler, filter: Filter, params: SubscribePar
|
|
|
75
75
|
relatedTables: []
|
|
76
76
|
}
|
|
77
77
|
viewOptions.relatedTables = await Promise.all(tableIds.map(async tableID => {
|
|
78
|
-
const table = this.dboBuilder.USER_TABLES
|
|
78
|
+
const table = this.dboBuilder.USER_TABLES!.find(t => t.relid === +tableID)!;
|
|
79
79
|
let tableCols = tableColumns.filter(tc => tc.tableID!.toString() === tableID);
|
|
80
80
|
|
|
81
81
|
/** If table has primary keys and they are all in this view then use only primary keys */
|
|
@@ -128,7 +128,7 @@ async function subscribe(this: ViewHandler, filter: Filter, params: SubscribePar
|
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
return {
|
|
131
|
-
tableName
|
|
131
|
+
tableName,
|
|
132
132
|
tableNameEscaped: JSON.stringify(tableName),// [table.schemaname, table.relname].map(v => JSON.stringify(v)).join("."),
|
|
133
133
|
condition: "TRUE"
|
|
134
134
|
}
|
|
@@ -193,7 +193,7 @@ async function subscribe(this: ViewHandler, filter: Filter, params: SubscribePar
|
|
|
193
193
|
const pubSubManager = await this.dboBuilder.getPubSubManager();
|
|
194
194
|
pubSubManager.removeLocalSub(this.name, condition, localFunc)
|
|
195
195
|
};
|
|
196
|
-
|
|
196
|
+
const res: { unsubscribe: () => any } = Object.freeze({ unsubscribe })
|
|
197
197
|
return res;
|
|
198
198
|
}
|
|
199
199
|
} catch (e) {
|