@restura/core 0.1.0-alpha.17 → 0.1.0-alpha.19

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/index.js CHANGED
@@ -69,6 +69,7 @@ var src_exports = {};
69
69
  __export(src_exports, {
70
70
  HtmlStatusCodes: () => HtmlStatusCodes,
71
71
  PsqlConnection: () => PsqlConnection,
72
+ PsqlEngine: () => PsqlEngine,
72
73
  PsqlPool: () => PsqlPool,
73
74
  PsqlTransaction: () => PsqlTransaction,
74
75
  RsError: () => RsError,
@@ -1738,7 +1739,7 @@ var PsqlEngine = class extends SqlEngine {
1738
1739
  await Promise.all(promises);
1739
1740
  this.triggerClient.on("notification", async (msg) => {
1740
1741
  if (msg.channel === "insert" || msg.channel === "update" || msg.channel === "delete") {
1741
- const payload = JSON.parse(msg.payload);
1742
+ const payload = import_core_utils5.ObjectUtils.safeParse(msg.payload);
1742
1743
  await this.handleTrigger(payload, msg.channel.toUpperCase());
1743
1744
  }
1744
1745
  });
@@ -1938,16 +1939,19 @@ var PsqlEngine = class extends SqlEngine {
1938
1939
  parameterObj[assignment.name] = this.replaceParamKeywords(assignment.value, routeData, req, sqlParams);
1939
1940
  });
1940
1941
  const query = insertObjectQuery(routeData.table, __spreadValues(__spreadValues({}, req.data), parameterObj));
1941
- const createdItem = await this.psqlConnectionPool.queryOne(query, sqlParams, req.requesterDetails);
1942
- const insertId = createdItem == null ? void 0 : createdItem.id;
1943
- const whereData = [
1944
- {
1945
- tableName: routeData.table,
1946
- value: insertId,
1947
- columnName: "id",
1948
- operator: "="
1949
- }
1950
- ];
1942
+ const createdItem = await this.psqlConnectionPool.queryOne(
1943
+ query,
1944
+ sqlParams,
1945
+ req.requesterDetails
1946
+ );
1947
+ const insertId = createdItem.id;
1948
+ const whereId = {
1949
+ tableName: routeData.table,
1950
+ value: insertId,
1951
+ columnName: "id",
1952
+ operator: "="
1953
+ };
1954
+ const whereData = [whereId];
1951
1955
  req.data = { id: insertId };
1952
1956
  return this.executeGetRequest(req, __spreadProps(__spreadValues({}, routeData), { where: whereData }), schema);
1953
1957
  }
@@ -1966,7 +1970,9 @@ var PsqlEngine = class extends SqlEngine {
1966
1970
  let selectStatement = "SELECT \n";
1967
1971
  selectStatement += ` ${selectColumns.map((item) => {
1968
1972
  if (item.subquery) {
1969
- return `${this.createNestedSelect(req, schema, item, routeData, userRole, sqlParams)} AS ${item.name}`;
1973
+ return `${this.createNestedSelect(req, schema, item, routeData, userRole, sqlParams)} AS ${escapeColumnName(
1974
+ item.name
1975
+ )}`;
1970
1976
  }
1971
1977
  return `${escapeColumnName(item.selector)} AS ${escapeColumnName(item.name)}`;
1972
1978
  }).join(",\n ")}
@@ -2006,7 +2012,11 @@ var PsqlEngine = class extends SqlEngine {
2006
2012
  );
2007
2013
  const totalQuery = `SELECT COUNT(${routeData.groupBy ? `DISTINCT ${routeData.groupBy.tableName}.${routeData.groupBy.columnName}` : "*"}) AS total
2008
2014
  ${sqlStatement};`;
2009
- const totalPromise = await this.psqlConnectionPool.runQuery(totalQuery, sqlParams, req.requesterDetails);
2015
+ const totalPromise = await this.psqlConnectionPool.runQuery(
2016
+ totalQuery,
2017
+ sqlParams,
2018
+ req.requesterDetails
2019
+ );
2010
2020
  const [pageResults, totalResponse] = await Promise.all([pagePromise, totalPromise]);
2011
2021
  let total = 0;
2012
2022
  if (import_core_utils5.ObjectUtils.isArrayWithData(totalResponse)) {
@@ -2119,17 +2129,16 @@ DELETE FROM "${routeData.table}" ${joinStatement} ${whereClause}`;
2119
2129
  );
2120
2130
  let operator = item.operator;
2121
2131
  if (operator === "LIKE") {
2122
- item.value = `%${item.value}%`;
2132
+ item.value = `'%${item.value}%'`;
2123
2133
  } else if (operator === "STARTS WITH") {
2124
2134
  operator = "LIKE";
2125
- item.value = `${item.value}%`;
2135
+ item.value = `'${item.value}%'`;
2126
2136
  } else if (operator === "ENDS WITH") {
2127
2137
  operator = "LIKE";
2128
- item.value = `%${item.value}`;
2138
+ item.value = `'%${item.value}'`;
2129
2139
  }
2130
2140
  const replacedValue = this.replaceParamKeywords(item.value, routeData, req, sqlParams);
2131
- const escapedValue = SQL`${replacedValue}`;
2132
- whereClause += ` ${item.conjunction || ""} "${item.tableName}"."${item.columnName}" ${operator.replace("LIKE", "ILIKE")} ${["IN", "NOT IN"].includes(operator) ? `(${escapedValue})` : escapedValue}
2141
+ whereClause += ` ${item.conjunction || ""} "${item.tableName}"."${item.columnName}" ${operator.replace("LIKE", "ILIKE")} ${["IN", "NOT IN"].includes(operator) ? `(${replacedValue})` : replacedValue}
2133
2142
  `;
2134
2143
  });
2135
2144
  const data = req.data;
@@ -2343,7 +2352,7 @@ var ResturaEngine = class {
2343
2352
  async init(app, authenticationHandler, psqlConnectionPool) {
2344
2353
  this.resturaConfig = import_internal2.config.validate("restura", resturaConfigSchema);
2345
2354
  this.psqlConnectionPool = psqlConnectionPool;
2346
- this.psqlEngine = new PsqlEngine(this.psqlConnectionPool);
2355
+ this.psqlEngine = new PsqlEngine(this.psqlConnectionPool, true);
2347
2356
  setupPgReturnTypes();
2348
2357
  await customApiFactory_default.loadApiFiles(this.resturaConfig.customApiFolderPath);
2349
2358
  this.authenticationHandler = authenticationHandler;
@@ -2707,10 +2716,17 @@ var PsqlTransaction = class extends PsqlConnection {
2707
2716
  super();
2708
2717
  this.clientConfig = clientConfig;
2709
2718
  this.client = new Client2(clientConfig);
2719
+ this.connectPromise = this.client.connect();
2710
2720
  this.beginTransactionPromise = this.beginTransaction();
2711
2721
  }
2722
+ async close() {
2723
+ if (this.client) {
2724
+ await this.client.end();
2725
+ }
2726
+ }
2712
2727
  async beginTransaction() {
2713
- return this.query("BEGIN");
2728
+ await this.connectPromise;
2729
+ return this.client.query("BEGIN");
2714
2730
  }
2715
2731
  async rollback() {
2716
2732
  return this.query("ROLLBACK");
@@ -2722,7 +2738,7 @@ var PsqlTransaction = class extends PsqlConnection {
2722
2738
  return this.client.end();
2723
2739
  }
2724
2740
  async query(query, values) {
2725
- await this.client.connect();
2741
+ await this.connectPromise;
2726
2742
  await this.beginTransactionPromise;
2727
2743
  return this.client.query(query, values);
2728
2744
  }
@@ -2731,6 +2747,7 @@ var PsqlTransaction = class extends PsqlConnection {
2731
2747
  0 && (module.exports = {
2732
2748
  HtmlStatusCodes,
2733
2749
  PsqlConnection,
2750
+ PsqlEngine,
2734
2751
  PsqlPool,
2735
2752
  PsqlTransaction,
2736
2753
  RsError,