prostgles-server 4.0.57 → 4.0.59
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/dist/DboBuilder/getColumns.d.ts.map +1 -1
- package/dist/DboBuilder/getColumns.js +1 -13
- package/dist/DboBuilder/getColumns.js.map +1 -1
- package/dist/DboBuilder/runSQL.js +2 -2
- package/dist/DboBuilder/runSQL.js.map +1 -1
- package/dist/Prostgles.d.ts.map +1 -1
- package/dist/PubSubManager/PubSubManager.d.ts +1 -1
- package/dist/PubSubManager/PubSubManager.d.ts.map +1 -1
- package/dist/PubSubManager/PubSubManager.js +14 -1
- package/dist/PubSubManager/PubSubManager.js.map +1 -1
- package/lib/DboBuilder/getColumns.ts +1 -18
- package/lib/DboBuilder/runSQL.ts +2 -2
- package/lib/Prostgles.ts +3 -3
- package/lib/PubSubManager/PubSubManager.ts +14 -1
- package/package.json +2 -2
- package/tests/client/PID.txt +1 -1
- package/tests/client/package.json +2 -2
- package/tests/client/tsconfig.json +2 -1
- package/tests/isomorphic_queries.ts +11 -9
- package/tests/server/DBoGenerated.d.ts +1 -1
- package/tests/server/dboTypeCheck.ts +3 -3
- package/tests/server/index.ts +7 -6
- package/tests/server/package-lock.json +1 -1
- package/tests/server/package.json +3 -3
- package/tests/server/publishTypeCheck.ts +1 -1
- package/tests/server/tsconfig.json +3 -2
- package/tests/test.sh +2 -0
- package/dist/FileManager.d.ts +0 -143
- package/dist/FileManager.d.ts.map +0 -1
- package/dist/FileManager.js +0 -646
- package/dist/FileManager.js.map +0 -1
- package/dist/TableConfig.d.ts +0 -267
- package/dist/TableConfig.d.ts.map +0 -1
- package/dist/TableConfig.js +0 -463
- package/dist/TableConfig.js.map +0 -1
- package/dist/validation.d.ts +0 -126
- package/dist/validation.d.ts.map +0 -1
- package/dist/validation.js +0 -405
- package/dist/validation.js.map +0 -1
- package/lib/AuthHandler.d.ts +0 -187
- package/lib/AuthHandler.d.ts.map +0 -1
- package/lib/AuthHandler.js +0 -478
- package/lib/DBEventsManager.d.ts +0 -38
- package/lib/DBEventsManager.d.ts.map +0 -1
- package/lib/DBEventsManager.js +0 -140
- package/lib/DBSchemaBuilder.d.ts +0 -13
- package/lib/DBSchemaBuilder.d.ts.map +0 -1
- package/lib/DBSchemaBuilder.js +0 -145
- package/lib/DboBuilder/QueryBuilder/Functions.d.ts +0 -64
- package/lib/DboBuilder/QueryBuilder/Functions.d.ts.map +0 -1
- package/lib/DboBuilder/QueryBuilder/Functions.js +0 -984
- package/lib/DboBuilder/QueryBuilder/QueryBuilder.d.ts +0 -73
- package/lib/DboBuilder/QueryBuilder/QueryBuilder.d.ts.map +0 -1
- package/lib/DboBuilder/QueryBuilder/QueryBuilder.js +0 -335
- package/lib/DboBuilder/QueryBuilder/makeSelectQuery.d.ts +0 -8
- package/lib/DboBuilder/QueryBuilder/makeSelectQuery.d.ts.map +0 -1
- package/lib/DboBuilder/QueryBuilder/makeSelectQuery.js +0 -227
- package/lib/DboBuilder/TableHandler.d.ts +0 -37
- package/lib/DboBuilder/TableHandler.d.ts.map +0 -1
- package/lib/DboBuilder/TableHandler.js +0 -213
- package/lib/DboBuilder/ViewHandler.d.ts +0 -119
- package/lib/DboBuilder/ViewHandler.d.ts.map +0 -1
- package/lib/DboBuilder/ViewHandler.js +0 -1023
- package/lib/DboBuilder/delete.d.ts +0 -6
- package/lib/DboBuilder/delete.d.ts.map +0 -1
- package/lib/DboBuilder/delete.js +0 -128
- package/lib/DboBuilder/find.d.ts +0 -8
- package/lib/DboBuilder/find.d.ts.map +0 -1
- package/lib/DboBuilder/find.js +0 -91
- package/lib/DboBuilder/getColumns.d.ts +0 -12
- package/lib/DboBuilder/getColumns.d.ts.map +0 -1
- package/lib/DboBuilder/getColumns.js +0 -92
- package/lib/DboBuilder/getCondition.d.ts +0 -22
- package/lib/DboBuilder/getCondition.d.ts.map +0 -1
- package/lib/DboBuilder/getCondition.js +0 -236
- package/lib/DboBuilder/getSubscribeRelatedTables.d.ts +0 -20
- package/lib/DboBuilder/getSubscribeRelatedTables.d.ts.map +0 -1
- package/lib/DboBuilder/getSubscribeRelatedTables.js +0 -152
- package/lib/DboBuilder/getTablesForSchemaPostgresSQL.d.ts +0 -3
- package/lib/DboBuilder/getTablesForSchemaPostgresSQL.d.ts.map +0 -1
- package/lib/DboBuilder/getTablesForSchemaPostgresSQL.js +0 -207
- package/lib/DboBuilder/insert.d.ts +0 -6
- package/lib/DboBuilder/insert.d.ts.map +0 -1
- package/lib/DboBuilder/insert.js +0 -180
- package/lib/DboBuilder/insertDataParse.d.ts +0 -12
- package/lib/DboBuilder/insertDataParse.d.ts.map +0 -1
- package/lib/DboBuilder/insertDataParse.js +0 -253
- package/lib/DboBuilder/parseUpdateRules.d.ts +0 -18
- package/lib/DboBuilder/parseUpdateRules.d.ts.map +0 -1
- package/lib/DboBuilder/parseUpdateRules.js +0 -123
- package/lib/DboBuilder/runSQL.d.ts +0 -7
- package/lib/DboBuilder/runSQL.d.ts.map +0 -1
- package/lib/DboBuilder/runSQL.js +0 -135
- package/lib/DboBuilder/subscribe.d.ts +0 -20
- package/lib/DboBuilder/subscribe.d.ts.map +0 -1
- package/lib/DboBuilder/subscribe.js +0 -90
- package/lib/DboBuilder/update.d.ts +0 -6
- package/lib/DboBuilder/update.d.ts.map +0 -1
- package/lib/DboBuilder/update.js +0 -151
- package/lib/DboBuilder/uploadFile.d.ts +0 -7
- package/lib/DboBuilder/uploadFile.d.ts.map +0 -1
- package/lib/DboBuilder/uploadFile.js +0 -53
- package/lib/DboBuilder.d.ts +0 -306
- package/lib/DboBuilder.d.ts.map +0 -1
- package/lib/DboBuilder.js +0 -745
- package/lib/Event_Trigger_Tags.d.ts +0 -4
- package/lib/Event_Trigger_Tags.d.ts.map +0 -1
- package/lib/Event_Trigger_Tags.js +0 -116
- package/lib/FileManager/FileManager.d.ts +0 -135
- package/lib/FileManager/FileManager.d.ts.map +0 -1
- package/lib/FileManager/FileManager.js +0 -303
- package/lib/FileManager/initFileManager.d.ts +0 -4
- package/lib/FileManager/initFileManager.d.ts.map +0 -1
- package/lib/FileManager/initFileManager.js +0 -231
- package/lib/FileManager/parseFile.d.ts +0 -15
- package/lib/FileManager/parseFile.d.ts.map +0 -1
- package/lib/FileManager/parseFile.js +0 -58
- package/lib/FileManager/upload.d.ts +0 -6
- package/lib/FileManager/upload.d.ts.map +0 -1
- package/lib/FileManager/upload.js +0 -98
- package/lib/FileManager/uploadStream.d.ts +0 -5
- package/lib/FileManager/uploadStream.d.ts.map +0 -1
- package/lib/FileManager/uploadStream.js +0 -92
- package/lib/Filtering.d.ts +0 -15
- package/lib/Filtering.d.ts.map +0 -1
- package/lib/Filtering.js +0 -336
- package/lib/JSONBValidation/validate_jsonb_schema_sql.d.ts +0 -4
- package/lib/JSONBValidation/validate_jsonb_schema_sql.d.ts.map +0 -1
- package/lib/JSONBValidation/validate_jsonb_schema_sql.js +0 -500
- package/lib/JSONBValidation/validation.d.ts +0 -9
- package/lib/JSONBValidation/validation.d.ts.map +0 -1
- package/lib/JSONBValidation/validation.js +0 -131
- package/lib/PostgresNotifListenManager.d.ts +0 -28
- package/lib/PostgresNotifListenManager.d.ts.map +0 -1
- package/lib/PostgresNotifListenManager.js +0 -134
- package/lib/Prostgles.d.ts +0 -289
- package/lib/Prostgles.d.ts.map +0 -1
- package/lib/Prostgles.js +0 -685
- package/lib/PubSubManager/PubSubManager.d.ts +0 -175
- package/lib/PubSubManager/PubSubManager.d.ts.map +0 -1
- package/lib/PubSubManager/PubSubManager.js +0 -452
- package/lib/PubSubManager/addSub.d.ts +0 -8
- package/lib/PubSubManager/addSub.d.ts.map +0 -1
- package/lib/PubSubManager/addSub.js +0 -166
- package/lib/PubSubManager/addSync.d.ts +0 -8
- package/lib/PubSubManager/addSync.d.ts.map +0 -1
- package/lib/PubSubManager/addSync.js +0 -109
- package/lib/PubSubManager/getInitQuery.d.ts +0 -9
- package/lib/PubSubManager/getInitQuery.d.ts.map +0 -1
- package/lib/PubSubManager/getInitQuery.js +0 -552
- package/lib/PubSubManager/initPubSubManager.d.ts +0 -3
- package/lib/PubSubManager/initPubSubManager.d.ts.map +0 -1
- package/lib/PubSubManager/initPubSubManager.js +0 -116
- package/lib/PubSubManager/notifListener.d.ts +0 -5
- package/lib/PubSubManager/notifListener.d.ts.map +0 -1
- package/lib/PubSubManager/notifListener.js +0 -100
- package/lib/PubSubManager/pushSubData.d.ts +0 -3
- package/lib/PubSubManager/pushSubData.d.ts.map +0 -1
- package/lib/PubSubManager/pushSubData.js +0 -51
- package/lib/PublishParser.d.ts +0 -284
- package/lib/PublishParser.d.ts.map +0 -1
- package/lib/PublishParser.js +0 -421
- package/lib/SchemaWatch.d.ts +0 -13
- package/lib/SchemaWatch.d.ts.map +0 -1
- package/lib/SchemaWatch.js +0 -35
- package/lib/SyncReplication.d.ts +0 -34
- package/lib/SyncReplication.d.ts.map +0 -1
- package/lib/SyncReplication.js +0 -412
- package/lib/TableConfig/TableConfig.d.ts +0 -284
- package/lib/TableConfig/TableConfig.d.ts.map +0 -1
- package/lib/TableConfig/TableConfig.js +0 -437
- package/lib/TableConfig/getColumnDefinitionQuery.d.ts +0 -27
- package/lib/TableConfig/getColumnDefinitionQuery.d.ts.map +0 -1
- package/lib/TableConfig/getColumnDefinitionQuery.js +0 -87
- package/lib/TableConfig/getConstraintDefinitionQueries.d.ts +0 -34
- package/lib/TableConfig/getConstraintDefinitionQueries.d.ts.map +0 -1
- package/lib/TableConfig/getConstraintDefinitionQueries.js +0 -65
- package/lib/TableConfig/getFutureTableSchema.d.ts +0 -15
- package/lib/TableConfig/getFutureTableSchema.d.ts.map +0 -1
- package/lib/TableConfig/getFutureTableSchema.js +0 -43
- package/lib/TableConfig/getTableColumnQueries.d.ts +0 -16
- package/lib/TableConfig/getTableColumnQueries.d.ts.map +0 -1
- package/lib/TableConfig/getTableColumnQueries.js +0 -107
- package/lib/index.d.ts +0 -5
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -7
- package/lib/shortestPath.d.ts +0 -10
- package/lib/shortestPath.d.ts.map +0 -1
- package/lib/shortestPath.js +0 -111
- package/lib/utils.d.ts +0 -3
- package/lib/utils.d.ts.map +0 -1
- package/lib/utils.js +0 -12
- package/tests/client/index.d.ts +0 -2
- package/tests/client/index.d.ts.map +0 -1
- package/tests/client/index.js +0 -80
- package/tests/client/index.js.map +0 -1
- package/tests/client_only_queries.d.ts +0 -4
- package/tests/client_only_queries.d.ts.map +0 -1
- package/tests/client_only_queries.js +0 -282
- package/tests/config_test/DBoGenerated.d.ts +0 -388
- package/tests/config_test/index.js +0 -174
- package/tests/config_test/index.js.map +0 -1
- package/tests/isomorphic_queries.d.ts +0 -9
- package/tests/isomorphic_queries.d.ts.map +0 -1
- package/tests/isomorphic_queries.js +0 -773
- package/tests/manual_test/DBoGenerated.d.ts +0 -398
- package/tests/manual_test/index.d.ts +0 -2
- package/tests/manual_test/index.d.ts.map +0 -1
- package/tests/manual_test/index.js +0 -65
- package/tests/server/dboTypeCheck.d.ts +0 -2
- package/tests/server/dboTypeCheck.d.ts.map +0 -1
- package/tests/server/dboTypeCheck.js +0 -27
- package/tests/server/index.d.ts +0 -2
- package/tests/server/index.d.ts.map +0 -1
- package/tests/server/index.js +0 -507
- package/tests/server/publishTypeCheck.d.ts +0 -2
- package/tests/server/publishTypeCheck.d.ts.map +0 -1
- package/tests/server/publishTypeCheck.js +0 -130
- package/tests/server_only_queries.d.ts +0 -2
- package/tests/server_only_queries.d.ts.map +0 -1
- package/tests/server_only_queries.js +0 -19
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getSubscribeRelatedTables = void 0;
|
|
4
|
-
const prostgles_types_1 = require("prostgles-types");
|
|
5
|
-
const DboBuilder_1 = require("../DboBuilder");
|
|
6
|
-
const PubSubManager_1 = require("../PubSubManager/PubSubManager");
|
|
7
|
-
async function getSubscribeRelatedTables({ selectParams, filter, localParams, table_rules, condition, filterOpts }) {
|
|
8
|
-
let viewOptions = undefined;
|
|
9
|
-
if (this.is_view) {
|
|
10
|
-
const viewName = this.name;
|
|
11
|
-
const viewNameEscaped = this.escapedName;
|
|
12
|
-
const { current_schema } = await this.db.oneOrNone("SELECT current_schema");
|
|
13
|
-
/** Get list of used columns and their parent tables */
|
|
14
|
-
let { def } = (await this.db.oneOrNone("SELECT pg_get_viewdef(${viewName}) as def", { viewName }));
|
|
15
|
-
def = def.trim();
|
|
16
|
-
if (def.endsWith(";")) {
|
|
17
|
-
def = def.slice(0, -1);
|
|
18
|
-
}
|
|
19
|
-
if (!def || typeof def !== "string") {
|
|
20
|
-
throw (0, DboBuilder_1.makeErrorFromPGError)("Could get view definition");
|
|
21
|
-
}
|
|
22
|
-
const { fields } = await this.dboBuilder.dbo.sql(`SELECT * FROM ( \n ${def} \n ) prostgles_subscribe_view_definition LIMIT 0`, {});
|
|
23
|
-
const tableColumns = fields.filter(f => f.tableName && f.columnName);
|
|
24
|
-
/** Create exists filters for each table */
|
|
25
|
-
const tableIds = Array.from(new Set(tableColumns.map(tc => tc.tableID.toString())));
|
|
26
|
-
viewOptions = {
|
|
27
|
-
type: "view",
|
|
28
|
-
viewName,
|
|
29
|
-
definition: def,
|
|
30
|
-
relatedTables: []
|
|
31
|
-
};
|
|
32
|
-
viewOptions.relatedTables = await Promise.all(tableIds.map(async (tableID) => {
|
|
33
|
-
const table = this.dboBuilder.USER_TABLES.find(t => t.relid === +tableID);
|
|
34
|
-
let tableCols = tableColumns.filter(tc => tc.tableID.toString() === tableID);
|
|
35
|
-
/** If table has primary keys and they are all in this view then use only primary keys */
|
|
36
|
-
if (table?.pkey_columns?.every(pkey => tableCols.some(c => c.columnName === pkey))) {
|
|
37
|
-
tableCols = tableCols.filter(c => table?.pkey_columns?.includes(c.columnName));
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
/** Exclude non comparable data types */
|
|
41
|
-
tableCols = tableCols.filter(c => !["json", "xml"].includes(c.udt_name));
|
|
42
|
-
}
|
|
43
|
-
const { relname: tableName, schemaname: tableSchema } = table;
|
|
44
|
-
if (tableCols.length) {
|
|
45
|
-
const tableNameEscaped = tableSchema === current_schema ? table.relname : [tableSchema, tableName].map(v => JSON.stringify(v)).join(".");
|
|
46
|
-
const fullCondition = `EXISTS (
|
|
47
|
-
SELECT 1
|
|
48
|
-
FROM ${viewNameEscaped}
|
|
49
|
-
WHERE ${tableCols.map(c => `${tableNameEscaped}.${JSON.stringify(c.columnName)} = ${viewNameEscaped}.${JSON.stringify(c.name)}`).join(" AND \n")}
|
|
50
|
-
AND ${condition || "TRUE"}
|
|
51
|
-
)`;
|
|
52
|
-
try {
|
|
53
|
-
const { count } = await this.db.oneOrNone(`
|
|
54
|
-
WITH ${(0, prostgles_types_1.asName)(tableName)} AS (
|
|
55
|
-
SELECT *
|
|
56
|
-
FROM ${(0, prostgles_types_1.asName)(tableName)}
|
|
57
|
-
LIMIT 0
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
SELECT COUNT(*) as count
|
|
61
|
-
FROM (
|
|
62
|
-
${def}
|
|
63
|
-
) prostgles_view_ref_table_test
|
|
64
|
-
`);
|
|
65
|
-
const relatedTableSubscription = {
|
|
66
|
-
tableName: tableName,
|
|
67
|
-
tableNameEscaped,
|
|
68
|
-
condition: fullCondition,
|
|
69
|
-
};
|
|
70
|
-
if (count.toString() === '0') {
|
|
71
|
-
return relatedTableSubscription;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
catch (e) {
|
|
75
|
-
(0, PubSubManager_1.log)(`Could not not override subscribed view (${this.name}) table (${tableName}). Will not check condition`, e);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return {
|
|
79
|
-
tableName,
|
|
80
|
-
tableNameEscaped: JSON.stringify(tableName),
|
|
81
|
-
condition: "TRUE"
|
|
82
|
-
};
|
|
83
|
-
}));
|
|
84
|
-
/** Get list of remaining used inner tables */
|
|
85
|
-
const allUsedTables = await this.db.any("SELECT distinct table_name, table_schema FROM information_schema.view_column_usage WHERE view_name = ${viewName}", { viewName });
|
|
86
|
-
/** Remaining tables will have listeners on all records (condition = "TRUE") */
|
|
87
|
-
const remainingInnerTables = allUsedTables.filter(at => !tableColumns.some(dc => dc.tableName === at.table_name && dc.tableSchema === at.table_schema));
|
|
88
|
-
viewOptions.relatedTables = [
|
|
89
|
-
...viewOptions.relatedTables,
|
|
90
|
-
...remainingInnerTables.map(t => ({
|
|
91
|
-
tableName: t.table_name,
|
|
92
|
-
tableNameEscaped: [t.table_name, t.table_schema].map(v => JSON.stringify(v)).join("."),
|
|
93
|
-
condition: "TRUE"
|
|
94
|
-
}))
|
|
95
|
-
];
|
|
96
|
-
if (!viewOptions.relatedTables.length) {
|
|
97
|
-
throw "Could not subscribe to this view: no related tables found";
|
|
98
|
-
}
|
|
99
|
-
/** Any joined table used within select or filter must also be added a trigger for this recordset */
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
const newQuery = await this.find(filter, { ...selectParams, limit: 0 }, undefined, table_rules, { ...localParams, returnNewQuery: true });
|
|
103
|
-
viewOptions = {
|
|
104
|
-
type: "table",
|
|
105
|
-
relatedTables: []
|
|
106
|
-
};
|
|
107
|
-
/**
|
|
108
|
-
* Avoid nested exists error. Will affect performance
|
|
109
|
-
*/
|
|
110
|
-
const nonExistsFilter = filterOpts.exists.length ? {} : filter;
|
|
111
|
-
for await (const j of (newQuery.joins ?? [])) {
|
|
112
|
-
if (!viewOptions.relatedTables.find(rt => rt.tableName === j.table)) {
|
|
113
|
-
viewOptions.relatedTables.push({
|
|
114
|
-
tableName: j.table,
|
|
115
|
-
tableNameEscaped: (0, prostgles_types_1.asName)(j.table),
|
|
116
|
-
condition: (await this.dboBuilder.dbo[j.table].prepareWhere({
|
|
117
|
-
filter: {
|
|
118
|
-
$existsJoined: {
|
|
119
|
-
[[this.name, ...j.$path ?? [].slice(0).reverse()].join(".")]: nonExistsFilter
|
|
120
|
-
}
|
|
121
|
-
},
|
|
122
|
-
addKeywords: false,
|
|
123
|
-
localParams: undefined,
|
|
124
|
-
tableRule: undefined
|
|
125
|
-
})).where
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
for await (const e of newQuery.whereOpts.exists) {
|
|
130
|
-
const eTable = e.tables.at(-1);
|
|
131
|
-
viewOptions.relatedTables.push({
|
|
132
|
-
tableName: eTable,
|
|
133
|
-
tableNameEscaped: (0, prostgles_types_1.asName)(eTable),
|
|
134
|
-
condition: (await this.dboBuilder.dbo[eTable].prepareWhere({
|
|
135
|
-
filter: {
|
|
136
|
-
$existsJoined: {
|
|
137
|
-
[[this.name, ...e.tables ?? [].slice(0, -1).reverse()].join(".")]: nonExistsFilter
|
|
138
|
-
}
|
|
139
|
-
},
|
|
140
|
-
addKeywords: false,
|
|
141
|
-
localParams: undefined,
|
|
142
|
-
tableRule: undefined
|
|
143
|
-
})).where
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
if (!viewOptions.relatedTables.length) {
|
|
147
|
-
viewOptions = undefined;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
return viewOptions;
|
|
151
|
-
}
|
|
152
|
-
exports.getSubscribeRelatedTables = getSubscribeRelatedTables;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getTablesForSchemaPostgresSQL.d.ts","sourceRoot":"","sources":["getTablesForSchemaPostgresSQL.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAqB,WAAW,EAAE,MAAM,eAAe,CAAC;AAI3E,wBAAsB,6BAA6B,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,UAAU,EAAE,MAAM,SAAW,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAgNzH"}
|
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTablesForSchemaPostgresSQL = void 0;
|
|
4
|
-
const PubSubManager_1 = require("../PubSubManager/PubSubManager");
|
|
5
|
-
// TODO: Add a onSocketConnect timeout for this query. Reason: this query gets blocked by prostgles.app_triggers from PubSubManager.addTrigger in some cases (pg_dump locks that table)
|
|
6
|
-
async function getTablesForSchemaPostgresSQL({ db, runSQL }, schema = "public") {
|
|
7
|
-
const query = `
|
|
8
|
-
SELECT
|
|
9
|
-
jsonb_build_object(
|
|
10
|
-
'insert', EXISTS (
|
|
11
|
-
SELECT 1
|
|
12
|
-
FROM information_schema.role_table_grants rg
|
|
13
|
-
WHERE rg.table_name = t.table_name
|
|
14
|
-
AND rg.privilege_type = 'INSERT'
|
|
15
|
-
),
|
|
16
|
-
'select', EXISTS (
|
|
17
|
-
SELECT 1
|
|
18
|
-
FROM information_schema.role_table_grants rg
|
|
19
|
-
WHERE rg.table_name = t.table_name
|
|
20
|
-
AND rg.privilege_type = 'SELECT'
|
|
21
|
-
),
|
|
22
|
-
'update', EXISTS (
|
|
23
|
-
SELECT 1
|
|
24
|
-
FROM information_schema.role_table_grants rg
|
|
25
|
-
WHERE rg.table_name = t.table_name
|
|
26
|
-
AND rg.privilege_type = 'UPDATE'
|
|
27
|
-
),
|
|
28
|
-
'delete', EXISTS (
|
|
29
|
-
SELECT 1
|
|
30
|
-
FROM information_schema.role_table_grants rg
|
|
31
|
-
WHERE rg.table_name = t.table_name
|
|
32
|
-
AND rg.privilege_type = 'DELETE'
|
|
33
|
-
)
|
|
34
|
-
) as privileges
|
|
35
|
-
, t.table_schema as schema, t.table_name as name
|
|
36
|
-
, cc.columns
|
|
37
|
-
, cc.table_oid as oid
|
|
38
|
-
, t.is_view
|
|
39
|
-
, t.view_definition
|
|
40
|
-
, array_to_json(vr.table_names) as parent_tables
|
|
41
|
-
, obj_description(cc.table_oid::regclass) as comment
|
|
42
|
-
FROM (
|
|
43
|
-
SELECT table_name,
|
|
44
|
-
table_schema,
|
|
45
|
-
is_view,
|
|
46
|
-
CASE WHEN is_view THEN pg_get_viewdef(format('%I.%I', table_schema, table_name)::REGCLASS, true) END as view_definition
|
|
47
|
-
FROM (
|
|
48
|
-
SELECT table_name, table_schema, table_type = 'VIEW' as is_view
|
|
49
|
-
FROM information_schema.tables
|
|
50
|
-
/* TODO - add support for materialized views
|
|
51
|
-
UNION ALL
|
|
52
|
-
SELECT table_name, table_schema
|
|
53
|
-
FROM (
|
|
54
|
-
SELECT relname as table_name, nspname as table_schema, true as is_view
|
|
55
|
-
FROM pg_catalog.pg_class AS _c
|
|
56
|
-
JOIN pg_catalog.pg_namespace AS _ns
|
|
57
|
-
ON _c.relnamespace = _ns.oid
|
|
58
|
-
WHERE relkind IN ( 'm' )
|
|
59
|
-
) materialized_views
|
|
60
|
-
*/
|
|
61
|
-
) tables_matviews
|
|
62
|
-
WHERE table_schema = ${(0, PubSubManager_1.asValue)(schema)}
|
|
63
|
-
) t
|
|
64
|
-
INNER join (
|
|
65
|
-
SELECT ccc.table_oid, table_schema, table_name
|
|
66
|
-
, jsonb_agg((SELECT x FROM (
|
|
67
|
-
SELECT ccc.column_name as name,
|
|
68
|
-
ccc.data_type,
|
|
69
|
-
ccc.udt_name,
|
|
70
|
-
ccc.element_type,
|
|
71
|
-
ccc.element_udt_name,
|
|
72
|
-
ccc.is_pkey,
|
|
73
|
-
ccc.comment,
|
|
74
|
-
ccc.ordinal_position,
|
|
75
|
-
ccc.is_nullable = 'YES' as is_nullable,
|
|
76
|
-
ccc.is_updatable,
|
|
77
|
-
ccc.references,
|
|
78
|
-
ccc.has_default,
|
|
79
|
-
ccc.column_default,
|
|
80
|
-
ccc.privileges
|
|
81
|
-
) as x) ORDER BY ccc.ordinal_position ) as columns
|
|
82
|
-
FROM (
|
|
83
|
-
SELECT c.table_schema, c.table_name, c.column_name, c.data_type, c.udt_name
|
|
84
|
-
, e.data_type as element_type
|
|
85
|
-
, e.udt_name as element_udt_name
|
|
86
|
-
, col_description(format('%I.%I', c.table_schema, c.table_name)::regclass::oid, c.ordinal_position) as comment
|
|
87
|
-
--, CASE WHEN fc.ftable IS NOT NULL THEN row_to_json((SELECT t FROM (SELECT fc.ftable, fc.fcols, fc.cols) t)) END as references
|
|
88
|
-
, fc.references
|
|
89
|
-
, c.is_identity = 'YES' OR EXISTS (
|
|
90
|
-
SELECT 1
|
|
91
|
-
FROM information_schema.table_constraints as tc
|
|
92
|
-
JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema
|
|
93
|
-
WHERE kcu.table_schema = c.table_schema AND kcu.table_name = c.table_name AND kcu.column_name = c.column_name AND tc.constraint_type IN ('PRIMARY KEY')
|
|
94
|
-
) as is_pkey
|
|
95
|
-
, c.ordinal_position
|
|
96
|
-
, COALESCE(c.column_default IS NOT NULL OR c.identity_generation = 'ALWAYS', false) as has_default
|
|
97
|
-
, c.column_default
|
|
98
|
-
, format('%I.%I', c.table_schema, c.table_name)::regclass::oid AS table_oid
|
|
99
|
-
, c.is_nullable
|
|
100
|
-
/* generated always and view columns cannot be updated */
|
|
101
|
-
, COALESCE(c.is_updatable, 'YES') = 'YES' AND COALESCE(c.is_generated, '') != 'ALWAYS' AND COALESCE(c.identity_generation, '') != 'ALWAYS' as is_updatable
|
|
102
|
-
, cp.privileges
|
|
103
|
-
FROM information_schema.columns c
|
|
104
|
-
LEFT JOIN (SELECT * FROM information_schema.element_types ) e
|
|
105
|
-
ON ((c.table_catalog, c.table_schema, c.table_name, 'TABLE', c.dtd_identifier)
|
|
106
|
-
= (e.object_catalog, e.object_schema, e.object_name, e.object_type, e.collection_type_identifier)
|
|
107
|
-
)
|
|
108
|
-
LEFT JOIN (
|
|
109
|
-
SELECT table_schema, table_name, column_name, json_agg(row_to_json((SELECT t FROM (SELECT cpp.privilege_type, cpp.is_grantable ) t))) as privileges
|
|
110
|
-
FROM information_schema.column_privileges cpp
|
|
111
|
-
GROUP BY table_schema, table_name, column_name
|
|
112
|
-
) cp
|
|
113
|
-
ON c.table_name = cp.table_name AND c.column_name = cp.column_name
|
|
114
|
-
LEFT JOIN (
|
|
115
|
-
--SELECT *
|
|
116
|
-
SELECT "table", unnest(ft.cols) as col, jsonb_agg(row_to_json((SELECT t FROM (SELECT ftable, fcols, cols) t))) as references
|
|
117
|
-
FROM (
|
|
118
|
-
SELECT
|
|
119
|
-
(SELECT r.relname from pg_class r where r.oid = c.conrelid) as table,
|
|
120
|
-
(SELECT array_agg(attname::text) from pg_attribute
|
|
121
|
-
where attrelid = c.conrelid and ARRAY[attnum] <@ c.conkey) as cols,
|
|
122
|
-
(SELECT array_agg(attname::text) from pg_attribute
|
|
123
|
-
where attrelid = c.confrelid and ARRAY[attnum] <@ c.confkey) as fcols,
|
|
124
|
-
(SELECT r.relname from pg_class r where r.oid = c.confrelid) as ftable
|
|
125
|
-
FROM pg_constraint c
|
|
126
|
-
) ft
|
|
127
|
-
WHERE ft.table IS NOT NULL
|
|
128
|
-
AND ft.ftable IS NOT NULL
|
|
129
|
-
-- c.confrelid = 'users'::regclass::oid
|
|
130
|
-
GROUP BY "table", unnest(cols)
|
|
131
|
-
) fc
|
|
132
|
-
ON fc.table = c.table_name
|
|
133
|
-
AND c.column_name::text = fc.col
|
|
134
|
-
) ccc
|
|
135
|
-
GROUP BY table_oid, table_schema, table_name
|
|
136
|
-
) cc
|
|
137
|
-
ON t.table_name = cc.table_name
|
|
138
|
-
AND t.table_schema = cc.table_schema
|
|
139
|
-
LEFT JOIN (
|
|
140
|
-
SELECT cl_r.relname as view_name, array_agg(DISTINCT cl_d.relname) AS table_names
|
|
141
|
-
FROM pg_rewrite AS r
|
|
142
|
-
JOIN pg_class AS cl_r ON r.ev_class=cl_r.oid
|
|
143
|
-
JOIN pg_depend AS d ON r.oid=d.objid
|
|
144
|
-
JOIN pg_class AS cl_d ON d.refobjid=cl_d.oid
|
|
145
|
-
WHERE cl_d.relkind IN ('r','v')
|
|
146
|
-
AND cl_d.relname <> cl_r.relname
|
|
147
|
-
GROUP BY cl_r.relname
|
|
148
|
-
) vr
|
|
149
|
-
ON t.table_name = vr.view_name
|
|
150
|
-
GROUP BY t.table_schema, t.table_name, t.is_view, t.view_definition, vr.table_names , cc.table_oid, cc.columns
|
|
151
|
-
ORDER BY schema, name
|
|
152
|
-
|
|
153
|
-
`;
|
|
154
|
-
// console.log(pgp.as.format(query, { schema }), schema);
|
|
155
|
-
let result = await db.any(query, { schema });
|
|
156
|
-
result = await Promise.all(result
|
|
157
|
-
.map(async (tbl) => {
|
|
158
|
-
/** Get view reference cols (based on parent table) */
|
|
159
|
-
let viewFCols = [];
|
|
160
|
-
if (tbl.is_view) {
|
|
161
|
-
try {
|
|
162
|
-
const view_definition = tbl.view_definition?.endsWith(";") ? tbl.view_definition.slice(0, -1) : tbl.view_definition;
|
|
163
|
-
const { fields } = await runSQL(`SELECT * FROM \n ( ${view_definition} \n) t LIMIT 0`, {}, {}, undefined);
|
|
164
|
-
const ftables = result.filter(r => fields.some(f => f.tableID === r.oid));
|
|
165
|
-
ftables.forEach(ft => {
|
|
166
|
-
const fFields = fields.filter(f => f.tableID === ft.oid);
|
|
167
|
-
const pkeys = ft.columns.filter(c => c.is_pkey);
|
|
168
|
-
const fFieldPK = fFields.filter(ff => pkeys.some(p => p.name === ff.columnName));
|
|
169
|
-
const refCols = pkeys.length && fFieldPK.length === pkeys.length ? fFieldPK : fFields.filter(ff => !["json", "jsonb", "xml"].includes(ff.udt_name));
|
|
170
|
-
const _fcols = refCols.map(ff => {
|
|
171
|
-
const d = {
|
|
172
|
-
name: ff.columnName,
|
|
173
|
-
references: [{
|
|
174
|
-
ftable: ft.name,
|
|
175
|
-
fcols: [ff.columnName],
|
|
176
|
-
cols: [ff.name]
|
|
177
|
-
}]
|
|
178
|
-
};
|
|
179
|
-
return d;
|
|
180
|
-
});
|
|
181
|
-
viewFCols = [
|
|
182
|
-
...viewFCols,
|
|
183
|
-
..._fcols
|
|
184
|
-
];
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
catch (err) {
|
|
188
|
-
console.error(err);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
tbl.columns = tbl.columns.map(col => {
|
|
192
|
-
if (col.has_default) {
|
|
193
|
-
/** Hide pkey default value */
|
|
194
|
-
col.column_default = (col.udt_name !== "uuid" && !col.is_pkey && !col.column_default.startsWith("nextval(")) ? col.column_default : null;
|
|
195
|
-
}
|
|
196
|
-
const viewFCol = viewFCols?.find(fc => fc.name === col.name);
|
|
197
|
-
if (viewFCol) {
|
|
198
|
-
col.references = viewFCol.references;
|
|
199
|
-
}
|
|
200
|
-
return col;
|
|
201
|
-
}); //.slice(0).sort((a, b) => a.name.localeCompare(b.name))
|
|
202
|
-
// .sort((a, b) => a.ordinal_position - b.ordinal_position)
|
|
203
|
-
return tbl;
|
|
204
|
-
}));
|
|
205
|
-
return result;
|
|
206
|
-
}
|
|
207
|
-
exports.getTablesForSchemaPostgresSQL = getTablesForSchemaPostgresSQL;
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { AnyObject, InsertParams } from "prostgles-types";
|
|
2
|
-
import { LocalParams } from "../DboBuilder";
|
|
3
|
-
import { TableRule } from "../PublishParser";
|
|
4
|
-
import { TableHandler } from "./TableHandler";
|
|
5
|
-
export declare function insert(this: TableHandler, rowOrRows: (AnyObject | AnyObject[]), param2?: InsertParams, param3_unused?: undefined, tableRules?: TableRule, localParams?: LocalParams): Promise<any | any[] | boolean>;
|
|
6
|
-
//# sourceMappingURL=insert.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"insert.d.ts","sourceRoot":"","sources":["insert.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAqC,YAAY,EAAY,MAAM,iBAAiB,CAAC;AACvG,OAAO,EAAE,WAAW,EAAsD,MAAM,eAAe,CAAC;AAChG,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,wBAAsB,MAAM,CAAC,IAAI,EAAE,YAAY,EAAE,SAAS,EAAE,CAAC,SAAS,GAAG,SAAS,EAAE,CAAC,EAAE,MAAM,CAAC,EAAE,YAAY,EAAE,aAAa,CAAC,EAAE,SAAS,EAAE,UAAU,CAAC,EAAE,SAAS,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC,CAyL1N"}
|
package/lib/DboBuilder/insert.js
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.insert = void 0;
|
|
4
|
-
const prostgles_types_1 = require("prostgles-types");
|
|
5
|
-
const DboBuilder_1 = require("../DboBuilder");
|
|
6
|
-
const PubSubManager_1 = require("../PubSubManager/PubSubManager");
|
|
7
|
-
async function insert(rowOrRows, param2, param3_unused, tableRules, localParams) {
|
|
8
|
-
// const localParams = _localParams || {};
|
|
9
|
-
const ACTION = "insert";
|
|
10
|
-
try {
|
|
11
|
-
const { onConflictDoNothing, fixIssues = false } = param2 || {};
|
|
12
|
-
const { testRule = false, returnQuery = false } = localParams || {};
|
|
13
|
-
const { returning } = param2 || {};
|
|
14
|
-
const finalDBtx = localParams?.tx?.dbTX || this.dbTX;
|
|
15
|
-
if (tableRules?.[ACTION]?.postValidate) {
|
|
16
|
-
if (!finalDBtx) {
|
|
17
|
-
return this.dboBuilder.getTX(_dbtx => _dbtx[this.name]?.[ACTION]?.(rowOrRows, param2, param3_unused, tableRules, localParams));
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
let returningFields, forcedData, fields;
|
|
21
|
-
if (tableRules) {
|
|
22
|
-
if (!tableRules[ACTION])
|
|
23
|
-
throw "insert rules missing for " + this.name;
|
|
24
|
-
returningFields = tableRules[ACTION].returningFields;
|
|
25
|
-
forcedData = tableRules[ACTION].forcedData;
|
|
26
|
-
fields = tableRules[ACTION].fields;
|
|
27
|
-
/* If no returning fields specified then take select fields as returning */
|
|
28
|
-
if (!returningFields)
|
|
29
|
-
returningFields = (0, prostgles_types_1.get)(tableRules, "select.fields") || (0, prostgles_types_1.get)(tableRules, "insert.fields");
|
|
30
|
-
if (!fields)
|
|
31
|
-
throw ` invalid insert rule for ${this.name} -> fields missing `;
|
|
32
|
-
/* Safely test publish rules */
|
|
33
|
-
if (testRule) {
|
|
34
|
-
// if(this.is_media && tableRules.insert.preValidate) throw "Media table cannot have a preValidate. It already is used internally by prostgles for file upload";
|
|
35
|
-
await this.validateViewRules({ fields, returningFields, forcedFilter: forcedData, rule: "insert" });
|
|
36
|
-
if (forcedData) {
|
|
37
|
-
const keys = Object.keys(forcedData);
|
|
38
|
-
if (keys.length) {
|
|
39
|
-
const dataCols = keys.filter(k => this.column_names.includes(k));
|
|
40
|
-
const nestedInsertCols = keys.filter(k => !this.column_names.includes(k) && this.dboBuilder.dbo[k]?.insert);
|
|
41
|
-
if (nestedInsertCols.length) {
|
|
42
|
-
throw `Nested insert not supported for forcedData rule: ${nestedInsertCols}`;
|
|
43
|
-
}
|
|
44
|
-
const badCols = keys.filter(k => !dataCols.includes(k) && !nestedInsertCols.includes(k));
|
|
45
|
-
if (badCols.length) {
|
|
46
|
-
throw `Invalid columns found in forced filter: ${badCols.join(", ")}`;
|
|
47
|
-
}
|
|
48
|
-
try {
|
|
49
|
-
const values = "(" + dataCols.map(k => (0, PubSubManager_1.asValue)(forcedData[k]) + "::" + this.columns.find(c => c.name === k).udt_name).join(", ") + ")", colNames = dataCols.map(k => (0, prostgles_types_1.asName)(k)).join(",");
|
|
50
|
-
const query = DboBuilder_1.pgp.as.format("EXPLAIN INSERT INTO " + this.escapedName + " (${colNames:raw}) SELECT * FROM ( VALUES ${values:raw} ) t WHERE FALSE;", { colNames, values });
|
|
51
|
-
await this.db.any(query);
|
|
52
|
-
}
|
|
53
|
-
catch (e) {
|
|
54
|
-
throw "\nissue with forcedData: \nVALUE: " + JSON.stringify(forcedData, null, 2) + "\nERROR: " + e;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
let conflict_query = "";
|
|
62
|
-
if (typeof onConflictDoNothing === "boolean" && onConflictDoNothing) {
|
|
63
|
-
conflict_query = " ON CONFLICT DO NOTHING ";
|
|
64
|
-
}
|
|
65
|
-
if (param2) {
|
|
66
|
-
const good_paramsObj = { returning: 1, returnType: 1, fixIssues: 1, onConflictDoNothing: 1 };
|
|
67
|
-
const good_params = Object.keys(good_paramsObj);
|
|
68
|
-
const bad_params = Object.keys(param2).filter(k => !good_params.includes(k));
|
|
69
|
-
if (bad_params && bad_params.length)
|
|
70
|
-
throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
|
|
71
|
-
}
|
|
72
|
-
if (!rowOrRows)
|
|
73
|
-
rowOrRows = {}; //throw "Provide data in param1";
|
|
74
|
-
/** TODO: use WITH inserted as (query) SELECT jsonb_agg(inserted.*) as validateReturn, userReturning */
|
|
75
|
-
const originalReturning = await this.prepareReturning(returning, this.parseFieldFilter(returningFields));
|
|
76
|
-
const fullReturning = await this.prepareReturning(returning, this.parseFieldFilter("*"));
|
|
77
|
-
/** Used for postValidate. Add any missing computed returning from original query */
|
|
78
|
-
fullReturning.concat(originalReturning.filter(s => !fullReturning.some(f => f.alias === s.alias)));
|
|
79
|
-
const finalSelect = tableRules?.[ACTION]?.postValidate ? fullReturning : originalReturning;
|
|
80
|
-
const returningSelect = this.makeReturnQuery(finalSelect);
|
|
81
|
-
const makeQuery = async (_row) => {
|
|
82
|
-
const row = { ..._row };
|
|
83
|
-
if (!(0, prostgles_types_1.isObject)(row)) {
|
|
84
|
-
console.trace(row);
|
|
85
|
-
throw "\ninvalid insert data provided -> " + JSON.stringify(row);
|
|
86
|
-
}
|
|
87
|
-
const { data, allowedCols } = this.validateNewData({ row, forcedData, allowedFields: fields, tableRules, fixIssues });
|
|
88
|
-
const _data = { ...data };
|
|
89
|
-
let insertQ = "";
|
|
90
|
-
if (!Array.isArray(_data) && !(0, prostgles_types_1.getKeys)(_data).length || Array.isArray(_data) && !_data.length) {
|
|
91
|
-
await tableRules?.[ACTION]?.validate?.(_data, this.dbTX || this.dboBuilder.dbo);
|
|
92
|
-
insertQ = `INSERT INTO ${(0, prostgles_types_1.asName)(this.name)} DEFAULT VALUES `;
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
//@ts-ignore
|
|
96
|
-
insertQ = await this.colSet.getInsertQuery(_data, allowedCols, this.dbTX || this.dboBuilder.dbo, tableRules?.[ACTION]?.validate); // pgp.helpers.insert(_data, columnSet);
|
|
97
|
-
}
|
|
98
|
-
return insertQ + conflict_query + returningSelect;
|
|
99
|
-
};
|
|
100
|
-
let query = "";
|
|
101
|
-
let queryType = "none";
|
|
102
|
-
/**
|
|
103
|
-
* If media it will: upload file and continue insert
|
|
104
|
-
* If nested insert it will: make separate inserts and not continue main insert
|
|
105
|
-
*/
|
|
106
|
-
const insRes = await this.insertDataParse(rowOrRows, param2, param3_unused, tableRules, localParams);
|
|
107
|
-
const { data, insertResult } = insRes;
|
|
108
|
-
if ("insertResult" in insRes) {
|
|
109
|
-
return insertResult;
|
|
110
|
-
}
|
|
111
|
-
if (Array.isArray(data)) {
|
|
112
|
-
if (!data.length) {
|
|
113
|
-
throw "Empty insert. Provide data";
|
|
114
|
-
}
|
|
115
|
-
const queries = await Promise.all(data.map(async (p) => {
|
|
116
|
-
const q = await makeQuery(p);
|
|
117
|
-
return q;
|
|
118
|
-
}));
|
|
119
|
-
query = DboBuilder_1.pgp.helpers.concat(queries);
|
|
120
|
-
if (returningSelect)
|
|
121
|
-
queryType = "many";
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
query = await makeQuery(data);
|
|
125
|
-
if (returningSelect)
|
|
126
|
-
queryType = "one";
|
|
127
|
-
}
|
|
128
|
-
query = (0, DboBuilder_1.withUserRLS)(localParams, query);
|
|
129
|
-
if (returnQuery)
|
|
130
|
-
return query;
|
|
131
|
-
let result;
|
|
132
|
-
if (this.dboBuilder.prostgles.opts.DEBUG_MODE) {
|
|
133
|
-
console.log(this.t?.ctx?.start, "insert in " + this.name, data);
|
|
134
|
-
}
|
|
135
|
-
const tx = localParams?.tx?.t || this.t;
|
|
136
|
-
const allowedFieldKeys = this.parseFieldFilter(fields);
|
|
137
|
-
if (tx) {
|
|
138
|
-
result = await tx[queryType](query).catch((err) => (0, DboBuilder_1.makeErrorFromPGError)(err, localParams, this, allowedFieldKeys));
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
result = await this.db.tx(t => t[queryType](query)).catch(err => (0, DboBuilder_1.makeErrorFromPGError)(err, localParams, this, allowedFieldKeys));
|
|
142
|
-
}
|
|
143
|
-
if (tableRules?.[ACTION]?.postValidate) {
|
|
144
|
-
if (!finalDBtx)
|
|
145
|
-
throw new Error("Unexpected: no dbTX for postValidate");
|
|
146
|
-
const rows = Array.isArray(result) ? result : [result];
|
|
147
|
-
for await (const row of rows) {
|
|
148
|
-
await tableRules?.[ACTION]?.postValidate(row ?? {}, finalDBtx);
|
|
149
|
-
}
|
|
150
|
-
/* We used a full returning for postValidate. Now we must filter out dissallowed columns */
|
|
151
|
-
if (returning) {
|
|
152
|
-
if (Array.isArray(result)) {
|
|
153
|
-
return result.map(row => {
|
|
154
|
-
(0, PubSubManager_1.pickKeys)(row, originalReturning.map(s => s.alias));
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
return (0, PubSubManager_1.pickKeys)(result, originalReturning.map(s => s.alias));
|
|
158
|
-
}
|
|
159
|
-
return undefined;
|
|
160
|
-
}
|
|
161
|
-
return result;
|
|
162
|
-
}
|
|
163
|
-
catch (e) {
|
|
164
|
-
if (localParams && localParams.testRule)
|
|
165
|
-
throw e;
|
|
166
|
-
// ${JSON.stringify(rowOrRows || {}, null, 2)},
|
|
167
|
-
// ${JSON.stringify(param2 || {}, null, 2)}
|
|
168
|
-
throw (0, DboBuilder_1.parseError)(e, `dbo.${this.name}.${ACTION}()`);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
exports.insert = insert;
|
|
172
|
-
// const removeBuffers = (o: any) => {
|
|
173
|
-
// if(isPlainObject(o)){
|
|
174
|
-
// return JSON.stringify(getKeys(o).reduce((a, k) => {
|
|
175
|
-
// const value = o[k]
|
|
176
|
-
// return { ...a, [k]: Buffer.isBuffer(value)? `Buffer[${value.byteLength}][...REMOVED]` : value
|
|
177
|
-
// }
|
|
178
|
-
// }, {}));
|
|
179
|
-
// }
|
|
180
|
-
// }
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { AnyObject, InsertParams } from "prostgles-types";
|
|
2
|
-
import { LocalParams } from "../DboBuilder";
|
|
3
|
-
import { TableRule } from "../PublishParser";
|
|
4
|
-
import { TableHandler } from "./TableHandler";
|
|
5
|
-
/**
|
|
6
|
-
* Used for doing referenced inserts within a single transaction
|
|
7
|
-
*/
|
|
8
|
-
export declare function insertDataParse(this: TableHandler, data: (AnyObject | AnyObject[]), param2?: InsertParams, param3_unused?: undefined, tableRules?: TableRule, _localParams?: LocalParams): Promise<{
|
|
9
|
-
data?: AnyObject | AnyObject[];
|
|
10
|
-
insertResult?: AnyObject | AnyObject[];
|
|
11
|
-
}>;
|
|
12
|
-
//# sourceMappingURL=insertDataParse.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|