prostgles-server 4.0.36 → 4.0.37
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/ViewHandler.d.ts +1 -1
- package/dist/DboBuilder/ViewHandler.d.ts.map +1 -1
- package/dist/DboBuilder/ViewHandler.js +2 -77
- package/dist/DboBuilder/ViewHandler.js.map +1 -1
- package/dist/DboBuilder/delete.d.ts.map +1 -1
- package/dist/DboBuilder/delete.js +5 -2
- package/dist/DboBuilder/delete.js.map +1 -1
- package/dist/DboBuilder/find.d.ts +8 -0
- package/dist/DboBuilder/find.d.ts.map +1 -0
- package/dist/DboBuilder/find.js +89 -0
- package/dist/DboBuilder/find.js.map +1 -0
- package/dist/DboBuilder/insert.d.ts.map +1 -1
- package/dist/DboBuilder/insert.js +4 -2
- package/dist/DboBuilder/insert.js.map +1 -1
- package/dist/DboBuilder/update.d.ts.map +1 -1
- package/dist/DboBuilder/update.js +6 -1
- package/dist/DboBuilder/update.js.map +1 -1
- package/lib/DboBuilder/QueryBuilder/Functions.d.ts.map +1 -1
- package/lib/DboBuilder/QueryBuilder/Functions.js +18 -11
- package/lib/DboBuilder/ViewHandler.d.ts +1 -1
- package/lib/DboBuilder/ViewHandler.d.ts.map +1 -1
- package/lib/DboBuilder/ViewHandler.js +2 -77
- package/lib/DboBuilder/ViewHandler.ts +6 -80
- package/lib/DboBuilder/delete.d.ts.map +1 -1
- package/lib/DboBuilder/delete.js +5 -2
- package/lib/DboBuilder/delete.ts +7 -3
- package/lib/DboBuilder/find.d.ts +8 -0
- package/lib/DboBuilder/find.d.ts.map +1 -0
- package/lib/DboBuilder/find.js +88 -0
- package/lib/DboBuilder/find.ts +89 -0
- package/lib/DboBuilder/insert.d.ts.map +1 -1
- package/lib/DboBuilder/insert.js +4 -2
- package/lib/DboBuilder/insert.ts +4 -2
- package/lib/DboBuilder/update.d.ts.map +1 -1
- package/lib/DboBuilder/update.js +6 -1
- package/lib/DboBuilder/update.ts +7 -2
- package/lib/JSONBValidation/validation.js +1 -1
- package/package.json +2 -2
- package/tests/client/PID.txt +1 -1
- package/tests/server/package-lock.json +3 -3
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
|
|
2
|
+
import { SelectParams, isObject } from "prostgles-types";
|
|
3
|
+
import { Filter, LocalParams, makeErrorFromPGError, parseError } from "../DboBuilder";
|
|
4
|
+
import { makeSelectQuery } from "../DboBuilder/QueryBuilder/makeSelectQuery";
|
|
5
|
+
import { canRunSQL } from "../DboBuilder/runSQL";
|
|
6
|
+
import { TableRule } from "../PublishParser";
|
|
7
|
+
import { TableHandler } from "./TableHandler";
|
|
8
|
+
import { getNewQuery } from "./QueryBuilder/QueryBuilder";
|
|
9
|
+
import { ViewHandler } from "./ViewHandler";
|
|
10
|
+
|
|
11
|
+
export const find = async function(this: ViewHandler, filter?: Filter, selectParams?: SelectParams, param3_unused?: undefined, tableRules?: TableRule, localParams?: LocalParams): Promise<any[]> {
|
|
12
|
+
try {
|
|
13
|
+
filter = filter || {};
|
|
14
|
+
const allowedReturnTypes: Array<SelectParams["returnType"]> = ["row", "value", "values", "statement"]
|
|
15
|
+
const { returnType } = selectParams || {};
|
|
16
|
+
if (returnType && !allowedReturnTypes.includes(returnType)) {
|
|
17
|
+
throw `returnType (${returnType}) can only be ${allowedReturnTypes.join(" OR ")}`
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const { testRule = false, returnQuery = false, returnNewQuery } = localParams || {};
|
|
21
|
+
|
|
22
|
+
if (testRule) return [];
|
|
23
|
+
if (selectParams) {
|
|
24
|
+
const good_params: Array<keyof SelectParams> = ["select", "orderBy", "offset", "limit", "returnType", "groupBy"];
|
|
25
|
+
const bad_params = Object.keys(selectParams).filter(k => !good_params.includes(k as any));
|
|
26
|
+
if (bad_params && bad_params.length) throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/* Validate publish */
|
|
30
|
+
if (tableRules) {
|
|
31
|
+
|
|
32
|
+
if (!tableRules.select) throw "select rules missing for " + this.name;
|
|
33
|
+
const fields = tableRules.select.fields;
|
|
34
|
+
const maxLimit = tableRules.select.maxLimit;
|
|
35
|
+
|
|
36
|
+
if (<any>tableRules.select !== "*" && typeof tableRules.select !== "boolean" && !isObject(tableRules.select)) throw `\nINVALID publish.${this.name}.select\nExpecting any of: "*" | { fields: "*" } | true | false`
|
|
37
|
+
if (!fields) throw ` invalid ${this.name}.select rule -> fields (required) setting missing.\nExpecting any of: "*" | { col_name: false } | { col1: true, col2: true }`;
|
|
38
|
+
if (maxLimit && !Number.isInteger(maxLimit)) throw ` invalid publish.${this.name}.select.maxLimit -> expecting integer but got ` + maxLimit;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const q = await getNewQuery(this as unknown as TableHandler, filter, selectParams, param3_unused, tableRules, localParams, this.columns),
|
|
42
|
+
_query = makeSelectQuery(this as unknown as TableHandler, q, undefined, undefined, selectParams);
|
|
43
|
+
// console.log(_query, JSON.stringify(q, null, 2))
|
|
44
|
+
if (testRule) {
|
|
45
|
+
try {
|
|
46
|
+
await this.db.any("EXPLAIN " + _query);
|
|
47
|
+
return [];
|
|
48
|
+
} catch (e) {
|
|
49
|
+
console.error(e);
|
|
50
|
+
throw `INTERNAL ERROR: Publish config is not valid for publish.${this.name}.select `
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Used for subscribe */
|
|
55
|
+
if(returnNewQuery) return (q as unknown as any);
|
|
56
|
+
if (returnQuery) return (_query as unknown as any[]);
|
|
57
|
+
|
|
58
|
+
return runQueryReturnType(_query, returnType, this, localParams);
|
|
59
|
+
|
|
60
|
+
} catch (e) {
|
|
61
|
+
// console.trace(e)
|
|
62
|
+
if (localParams && localParams.testRule) throw e;
|
|
63
|
+
throw parseError(e, `dbo.${this.name}.find()`);
|
|
64
|
+
// throw { err: parseError(e), msg: `Issue with dbo.${this.name}.find()`, args: { filter, selectParams} };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
export const runQueryReturnType = async (query: string, returnType: SelectParams["returnType"], handler: ViewHandler | TableHandler, localParams: LocalParams | undefined) => {
|
|
70
|
+
|
|
71
|
+
if (returnType === "statement") {
|
|
72
|
+
if (!(await canRunSQL(handler.dboBuilder.prostgles, localParams))) {
|
|
73
|
+
throw `Not allowed: {returnType: "statement"} requires sql privileges `
|
|
74
|
+
}
|
|
75
|
+
return query as unknown as any[];
|
|
76
|
+
|
|
77
|
+
} else if (["row", "value"].includes(returnType!)) {
|
|
78
|
+
return (handler.t || handler.db).oneOrNone(query).then(data => {
|
|
79
|
+
return (data && returnType === "value") ? Object.values(data)[0] : data;
|
|
80
|
+
}).catch(err => makeErrorFromPGError(err, localParams, this));
|
|
81
|
+
} else {
|
|
82
|
+
return (handler.t || handler.db).any(query).then(data => {
|
|
83
|
+
if (returnType === "values") {
|
|
84
|
+
return data.map(d => Object.values(d)[0]);
|
|
85
|
+
}
|
|
86
|
+
return data;
|
|
87
|
+
}).catch(err => makeErrorFromPGError(err, localParams, this));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -1 +1 @@
|
|
|
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,EAAyC,MAAM,eAAe,CAAC;AACnF,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,
|
|
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,EAAyC,MAAM,eAAe,CAAC;AACnF,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,CAwL1N"}
|
package/lib/DboBuilder/insert.js
CHANGED
|
@@ -63,8 +63,9 @@ async function insert(rowOrRows, param2, param3_unused, tableRules, localParams)
|
|
|
63
63
|
conflict_query = " ON CONFLICT DO NOTHING ";
|
|
64
64
|
}
|
|
65
65
|
if (param2) {
|
|
66
|
-
const
|
|
67
|
-
const
|
|
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));
|
|
68
69
|
if (bad_params && bad_params.length)
|
|
69
70
|
throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
|
|
70
71
|
}
|
|
@@ -91,6 +92,7 @@ async function insert(rowOrRows, param2, param3_unused, tableRules, localParams)
|
|
|
91
92
|
insertQ = `INSERT INTO ${(0, prostgles_types_1.asName)(this.name)} DEFAULT VALUES `;
|
|
92
93
|
}
|
|
93
94
|
else {
|
|
95
|
+
//@ts-ignore
|
|
94
96
|
insertQ = await this.colSet.getInsertQuery(_data, allowedCols, this.dbTX || this.dboBuilder.dbo, tableRules?.[ACTION]?.validate); // pgp.helpers.insert(_data, columnSet);
|
|
95
97
|
}
|
|
96
98
|
return insertQ + conflict_query + returningSelect;
|
package/lib/DboBuilder/insert.ts
CHANGED
|
@@ -74,8 +74,9 @@ export async function insert(this: TableHandler, rowOrRows: (AnyObject | AnyObje
|
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
if (param2) {
|
|
77
|
-
const
|
|
78
|
-
const
|
|
77
|
+
const good_paramsObj: Record<keyof InsertParams, 1> = { returning: 1, returnType: 1, fixIssues: 1, onConflictDoNothing: 1 };
|
|
78
|
+
const good_params = Object.keys(good_paramsObj);
|
|
79
|
+
const bad_params = Object.keys(param2).filter(k => !good_params.includes(k));
|
|
79
80
|
if (bad_params && bad_params.length) throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
|
|
80
81
|
}
|
|
81
82
|
|
|
@@ -107,6 +108,7 @@ export async function insert(this: TableHandler, rowOrRows: (AnyObject | AnyObje
|
|
|
107
108
|
await tableRules?.[ACTION]?.validate?.(_data, this.dbTX || this.dboBuilder.dbo);
|
|
108
109
|
insertQ = `INSERT INTO ${asName(this.name)} DEFAULT VALUES `;
|
|
109
110
|
} else {
|
|
111
|
+
//@ts-ignore
|
|
110
112
|
insertQ = await this.colSet.getInsertQuery(_data, allowedCols, this.dbTX || this.dboBuilder.dbo, tableRules?.[ACTION]?.validate) // pgp.helpers.insert(_data, columnSet);
|
|
111
113
|
}
|
|
112
114
|
return insertQ + conflict_query + returningSelect;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"update.d.ts","sourceRoot":"","sources":["update.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAkC,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC1F,OAAO,EAAE,MAAM,EAAiB,WAAW,EAA2C,MAAM,eAAe,CAAC;AAC5G,OAAO,EAAE,SAAS,EAAe,MAAM,kBAAkB,CAAC;AAE1D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"update.d.ts","sourceRoot":"","sources":["update.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAkC,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC1F,OAAO,EAAE,MAAM,EAAiB,WAAW,EAA2C,MAAM,eAAe,CAAC;AAC5G,OAAO,EAAE,SAAS,EAAe,MAAM,kBAAkB,CAAC;AAE1D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAI9C,wBAAsB,MAAM,CAAC,IAAI,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,YAAY,EAAE,UAAU,CAAC,EAAE,SAAS,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,CAmKzL"}
|
package/lib/DboBuilder/update.js
CHANGED
|
@@ -5,6 +5,7 @@ const prostgles_types_1 = require("prostgles-types");
|
|
|
5
5
|
const DboBuilder_1 = require("../DboBuilder");
|
|
6
6
|
const PubSubManager_1 = require("../PubSubManager/PubSubManager");
|
|
7
7
|
const uploadFile_1 = require("./uploadFile");
|
|
8
|
+
const find_1 = require("./find");
|
|
8
9
|
async function update(filter, _newData, params, tableRules, localParams) {
|
|
9
10
|
const ACTION = "update";
|
|
10
11
|
try {
|
|
@@ -51,7 +52,8 @@ async function update(filter, _newData, params, tableRules, localParams) {
|
|
|
51
52
|
const { returning, multi = true, onConflictDoNothing = false, fixIssues = false } = params || {};
|
|
52
53
|
const { returnQuery = false } = localParams ?? {};
|
|
53
54
|
if (params) {
|
|
54
|
-
const
|
|
55
|
+
const good_paramsObj = { returning: 1, returnType: 1, fixIssues: 1, onConflictDoNothing: 1, multi: 1 };
|
|
56
|
+
const good_params = Object.keys(good_paramsObj);
|
|
55
57
|
const bad_params = Object.keys(params).filter(k => !good_params.includes(k));
|
|
56
58
|
if (bad_params && bad_params.length)
|
|
57
59
|
throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
|
|
@@ -106,6 +108,9 @@ async function update(filter, _newData, params, tableRules, localParams) {
|
|
|
106
108
|
}
|
|
107
109
|
if (returnQuery)
|
|
108
110
|
return query;
|
|
111
|
+
if (params?.returnType) {
|
|
112
|
+
return (0, find_1.runQueryReturnType)(query, params.returnType, this, localParams);
|
|
113
|
+
}
|
|
109
114
|
let result;
|
|
110
115
|
if (this.t) {
|
|
111
116
|
result = await (this.t)[qType](query).catch((err) => (0, DboBuilder_1.makeErrorFromPGError)(err, localParams, this, fields));
|
package/lib/DboBuilder/update.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { TableRule, ValidateRow } from "../PublishParser";
|
|
|
4
4
|
import { omitKeys, pickKeys } from "../PubSubManager/PubSubManager";
|
|
5
5
|
import { TableHandler } from "./TableHandler";
|
|
6
6
|
import { isFile, uploadFile } from "./uploadFile"
|
|
7
|
+
import { runQueryReturnType } from "./find";
|
|
7
8
|
|
|
8
9
|
export async function update(this: TableHandler, filter: Filter, _newData: AnyObject, params?: UpdateParams, tableRules?: TableRule, localParams?: LocalParams): Promise<AnyObject | void> {
|
|
9
10
|
const ACTION = "update";
|
|
@@ -56,9 +57,9 @@ export async function update(this: TableHandler, filter: Filter, _newData: AnyOb
|
|
|
56
57
|
const { returning, multi = true, onConflictDoNothing = false, fixIssues = false } = params || {};
|
|
57
58
|
const { returnQuery = false } = localParams ?? {};
|
|
58
59
|
|
|
59
|
-
|
|
60
60
|
if (params) {
|
|
61
|
-
const
|
|
61
|
+
const good_paramsObj: Record<keyof UpdateParams, 1> = { returning: 1, returnType: 1, fixIssues: 1, onConflictDoNothing: 1, multi: 1 };
|
|
62
|
+
const good_params = Object.keys(good_paramsObj);
|
|
62
63
|
const bad_params = Object.keys(params).filter(k => !good_params.includes(k));
|
|
63
64
|
if (bad_params && bad_params.length) throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
|
|
64
65
|
}
|
|
@@ -127,6 +128,10 @@ export async function update(this: TableHandler, filter: Filter, _newData: AnyOb
|
|
|
127
128
|
|
|
128
129
|
if (returnQuery) return query as unknown as void;
|
|
129
130
|
|
|
131
|
+
if(params?.returnType){
|
|
132
|
+
return runQueryReturnType(query, params.returnType, this, localParams);
|
|
133
|
+
}
|
|
134
|
+
|
|
130
135
|
let result;
|
|
131
136
|
if (this.t) {
|
|
132
137
|
result = await (this.t)[qType](query).catch((err: any) => makeErrorFromPGError(err, localParams, this, fields));
|
|
@@ -120,7 +120,7 @@ function getJSONBSchemaTSTypes(schema, colOpts, outerLeading = "", tables) {
|
|
|
120
120
|
type = !cols ? "any" : `{ ${cols.map(c => `${JSON.stringify(c.name)}: ${c.is_nullable ? "null | " : ""} ${(0, DboBuilder_1.postgresToTsType)(c.udt_name)}; `).join(" ")} }`;
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
|
-
return `${fieldType.nullable ? `null
|
|
123
|
+
return `${fieldType.nullable ? `null | ` : ""}${type}${l.isArray ? "[]" : ""}`;
|
|
124
124
|
}
|
|
125
125
|
else
|
|
126
126
|
throw "Unexpected getSchemaTSTypes: " + JSON.stringify({ fieldType, schema }, null, 2);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prostgles-server",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.37",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"check-disk-space": "^3.3.1",
|
|
41
41
|
"file-type": "^17.1.4",
|
|
42
42
|
"pg-promise": "^11.3.0",
|
|
43
|
-
"prostgles-types": "^4.0.
|
|
43
|
+
"prostgles-types": "^4.0.16",
|
|
44
44
|
"sharp": "^0.31.0"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
package/tests/client/PID.txt
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
38429
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
},
|
|
22
22
|
"../..": {
|
|
23
23
|
"name": "prostgles-server",
|
|
24
|
-
"version": "4.0.
|
|
24
|
+
"version": "4.0.36",
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@aws-sdk/client-s3": "^3.272.0",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"check-disk-space": "^3.3.1",
|
|
33
33
|
"file-type": "^17.1.4",
|
|
34
34
|
"pg-promise": "^11.3.0",
|
|
35
|
-
"prostgles-types": "^4.0.
|
|
35
|
+
"prostgles-types": "^4.0.16",
|
|
36
36
|
"sharp": "^0.31.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
@@ -1527,7 +1527,7 @@
|
|
|
1527
1527
|
"eslint": "^8.35.0",
|
|
1528
1528
|
"file-type": "^17.1.4",
|
|
1529
1529
|
"pg-promise": "^11.3.0",
|
|
1530
|
-
"prostgles-types": "^4.0.
|
|
1530
|
+
"prostgles-types": "^4.0.16",
|
|
1531
1531
|
"sharp": "^0.31.0",
|
|
1532
1532
|
"typescript": "^5.0.3"
|
|
1533
1533
|
}
|