prostgles-server 4.2.2 → 4.2.4

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.
@@ -36,8 +36,7 @@ export async function insert(this: TableHandler, rowOrRows: AnyObject | AnyObjec
36
36
  throw `Direct inserts not allowed. Only nested inserts from these tables: ${JSON.stringify(allowedNestedInserts)} `
37
37
  }
38
38
  }
39
- const { conflict_query } = validateInsertParams(insertParams);
40
-
39
+ validateInsertParams(insertParams);
41
40
 
42
41
  /**
43
42
  * If media it will: upload file and continue insert
@@ -49,6 +48,7 @@ export async function insert(this: TableHandler, rowOrRows: AnyObject | AnyObjec
49
48
  return insertResult;
50
49
  }
51
50
 
51
+ const pkeyNames = this.columns.filter(c => c.is_pkey).map(c => c.name);
52
52
  const getInsertQuery = async (_rows: AnyObject[]) => {
53
53
  const validatedData = await Promise.all(_rows.map(async _row => {
54
54
 
@@ -66,6 +66,16 @@ export async function insert(this: TableHandler, rowOrRows: AnyObject | AnyObjec
66
66
  const allowedCols = Array.from( new Set(validatedData.flatMap(d => d.allowedCols)));
67
67
  const dbTx = finalDBtx || this.dboBuilder.dbo
68
68
  const query = await this.colSet.getInsertQuery(validatedRows, allowedCols, dbTx, validate, localParams);
69
+
70
+ const { onConflict } = insertParams ?? {};
71
+ let conflict_query = "";
72
+ if (onConflict === "DoNothing") {
73
+ conflict_query = " ON CONFLICT DO NOTHING ";
74
+ } else if(onConflict === "DoUpdate"){
75
+ if(!pkeyNames.length) throw "Cannot do DoUpdate on a table without a primary key";
76
+ const nonPkeyCols = allowedCols.filter(c => !pkeyNames.includes(c));
77
+ conflict_query = ` ON CONFLICT (${pkeyNames.join(", ")}) DO UPDATE SET ${nonPkeyCols.map(k => `${k} = EXCLUDED.${k}`).join(", ")}`;
78
+ }
69
79
  return query + conflict_query;
70
80
  };
71
81
 
@@ -108,10 +118,9 @@ export async function insert(this: TableHandler, rowOrRows: AnyObject | AnyObjec
108
118
 
109
119
  const validateInsertParams = (params: InsertParams | undefined) => {
110
120
 
111
- const { onConflictDoNothing, returnType, returning } = params ?? {};
112
- let conflict_query = "";
113
- if (typeof onConflictDoNothing === "boolean" && onConflictDoNothing) {
114
- conflict_query = " ON CONFLICT DO NOTHING ";
121
+ const { onConflict, returnType, returning } = params ?? {};
122
+ if(![undefined, "DoNothing", "DoUpdate"].includes(onConflict)){
123
+ throw `Invalid onConflict: ${onConflict}. Expecting one of: DoNothing, DoUpdate`;
115
124
  }
116
125
 
117
126
  const allowedReturnTypes: InsertParams["returnType"][] = ["row", "value", "values", "statement", undefined]
@@ -124,14 +133,11 @@ const validateInsertParams = (params: InsertParams | undefined) => {
124
133
  }
125
134
 
126
135
  if (params) {
127
- const good_paramsObj: Record<keyof InsertParams, 1> = { returning: 1, returnType: 1, fixIssues: 1, onConflictDoNothing: 1 };
136
+ const good_paramsObj: Record<keyof InsertParams, 1> = { returning: 1, returnType: 1, fixIssues: 1, onConflict: 1 };
128
137
  const good_params = Object.keys(good_paramsObj);
129
138
  const bad_params = Object.keys(params).filter(k => !good_params.includes(k));
130
139
  if (bad_params && bad_params.length) throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
131
140
  }
132
-
133
- return { conflict_query }
134
-
135
141
  }
136
142
 
137
143
  // const removeBuffers = (o: any) => {
@@ -29,11 +29,11 @@ export async function update(this: TableHandler, filter: Filter, _newData: AnyOb
29
29
  }
30
30
 
31
31
  const { fields, validateRow, forcedData, returningFields, forcedFilter, filterFields } = parsedRules;
32
- const { onConflictDoNothing = false, fixIssues = false } = params || {};
32
+ const { onConflict, fixIssues = false } = params || {};
33
33
  const { returnQuery = false } = localParams ?? {};
34
34
 
35
35
  if (params) {
36
- const good_paramsObj: Record<keyof UpdateParams, 1> = { returning: 1, returnType: 1, fixIssues: 1, onConflictDoNothing: 1, multi: 1 };
36
+ const good_paramsObj: Record<keyof UpdateParams, 1> = { returning: 1, returnType: 1, fixIssues: 1, onConflict: 1, multi: 1 };
37
37
  const good_params = Object.keys(good_paramsObj);
38
38
  const bad_params = Object.keys(params).filter(k => !good_params.includes(k));
39
39
  if (bad_params && bad_params.length) throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
@@ -107,8 +107,10 @@ export async function update(this: TableHandler, filter: Filter, _newData: AnyOb
107
107
 
108
108
  let query = await this.colSet.getUpdateQuery(nData, allowedCols, this.getFinalDbo(localParams), validateRow, localParams)
109
109
  query += "\n" + updateFilter.where;
110
- if (onConflictDoNothing) query += " ON CONFLICT DO NOTHING ";
111
-
110
+ if (onConflict === "DoNothing") query += " ON CONFLICT DO NOTHING ";
111
+ if(onConflict === "DoUpdate"){
112
+ throw "onConflict 'DoUpdate' not possible for an update";
113
+ }
112
114
  const queryWithoutUserRLS = query;
113
115
  query = withUserRLS(localParams, query);
114
116
 
package/lib/Prostgles.ts CHANGED
@@ -299,7 +299,6 @@ export class Prostgles {
299
299
 
300
300
  constructor(params: ProstglesInitOptions) {
301
301
  if (!params) throw "ProstglesInitOptions missing";
302
- if (!params.io) console.warn("io missing. WebSockets will not be set up");
303
302
 
304
303
  const config: Record<keyof ProstglesInitOptions, 1> = {
305
304
  transactions: 1, joins: 1, tsGeneratedTypesDir: 1,
@@ -30,10 +30,10 @@ type OnReadyParamsCommon = {
30
30
  tables: DbTableInfo[];
31
31
  reason: OnInitReason;
32
32
  }
33
- type OnReadyParamsBasic = OnReadyParamsCommon & {
33
+ export type OnReadyParamsBasic = OnReadyParamsCommon & {
34
34
  dbo: DBHandlerServer;
35
35
  }
36
- type OnReadyParams<S> = OnReadyParamsCommon & {
36
+ export type OnReadyParams<S> = OnReadyParamsCommon & {
37
37
  dbo: DBOFullyTyped<S>;
38
38
  }
39
39
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "4.2.2",
3
+ "version": "4.2.4",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -46,7 +46,7 @@
46
46
  "pg-cursor": "^2.10.3",
47
47
  "pg-promise": "^11.5.4",
48
48
  "prostgles-client": "^4.0.53",
49
- "prostgles-types": "^4.0.67"
49
+ "prostgles-types": "^4.0.68"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@types/bluebird": "^3.5.36",
@@ -86,6 +86,16 @@ export default async function isomorphic(db: Required<DBHandlerServer> | DBHandl
86
86
  await db.sql("TRUNCATE files CASCADE");
87
87
  });
88
88
 
89
+ await tryRun("onConflict do update", async () => {
90
+ const initial = await db.items4.insert!({ id: -99, name: "onConflict", public: "onConflict" }, { returning: "*" });
91
+ const updated = await db.items4.insert!({ id: -99, name: "onConflict", public: "onConflict2" }, { onConflict: "DoUpdate", returning: "*" });
92
+ assert.equal(initial.id, -99);
93
+ assert.equal(initial.public, "onConflict");
94
+ assert.equal(updated.id, -99);
95
+ assert.equal(updated.public, "onConflict2");
96
+ await db.items4.delete!({ id: -99 });
97
+ });
98
+
89
99
  const fileFolder = `${__dirname}/../../server/dist/server/media/`;
90
100
  const fileName = "sample_file.txt";
91
101
  await tryRun("Local file upload", async () => {
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "../..": {
23
23
  "name": "prostgles-server",
24
- "version": "4.2.1",
24
+ "version": "4.2.3",
25
25
  "license": "MIT",
26
26
  "dependencies": {
27
27
  "@types/express": "^4.17.13",
@@ -35,7 +35,7 @@
35
35
  "pg-cursor": "^2.10.3",
36
36
  "pg-promise": "^11.5.4",
37
37
  "prostgles-client": "^4.0.53",
38
- "prostgles-types": "^4.0.67"
38
+ "prostgles-types": "^4.0.68"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/bluebird": "^3.5.36",
@@ -1548,7 +1548,7 @@
1548
1548
  "pg-cursor": "^2.10.3",
1549
1549
  "pg-promise": "^11.5.4",
1550
1550
  "prostgles-client": "^4.0.53",
1551
- "prostgles-types": "^4.0.67",
1551
+ "prostgles-types": "^4.0.68",
1552
1552
  "typescript": "^5.2.2"
1553
1553
  }
1554
1554
  },