pqb 0.65.5 → 0.66.0

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
@@ -6778,17 +6778,19 @@ const coalesce = (ctx, query, sql, quotedAs) => {
6778
6778
  if (coalesceValue !== void 0) return `COALESCE(${sql}, ${isExpression(coalesceValue) ? coalesceValue.toSQL(ctx, quotedAs) : addValue(ctx.values, coalesceValue)})`;
6779
6779
  return sql;
6780
6780
  };
6781
- const pushWhereStatementSql = (ctx, table, query, quotedAs) => {
6782
- const res = whereToSql(ctx, table, query, quotedAs);
6781
+ const pushWhereStatementSql = (ctx, table, query, quotedAs, checkIfHasExplicitWhere) => {
6782
+ const res = whereToSql(ctx, table, query, quotedAs, void 0, checkIfHasExplicitWhere);
6783
6783
  if (res) ctx.sql.push("WHERE", res);
6784
6784
  };
6785
6785
  const pushWhereToSql = (sql, ctx, table, query, quotedAs, parens) => {
6786
6786
  const res = whereToSql(ctx, table, query, quotedAs, parens);
6787
6787
  if (res) sql.push(res);
6788
6788
  };
6789
- const whereToSql = (ctx, table, query, quotedAs, parens) => {
6789
+ const whereToSql = (ctx, table, query, quotedAs, parens, checkIfHasExplicitWhere) => {
6790
+ let sql;
6790
6791
  if (query.scopes) {
6791
- let sql = andOrToSql(ctx, table, query, quotedAs, true);
6792
+ sql = andOrToSql(ctx, table, query, quotedAs, true);
6793
+ if (checkIfHasExplicitWhere && sql) checkIfHasExplicitWhere.value = true;
6792
6794
  const data = Object.create(query);
6793
6795
  for (const key in query.scopes) {
6794
6796
  const scope = query.scopes[key];
@@ -6796,11 +6798,16 @@ const whereToSql = (ctx, table, query, quotedAs, parens) => {
6796
6798
  data.and = scopeResult.and;
6797
6799
  data.or = scopeResult.or;
6798
6800
  const scopeSql = andOrToSql(ctx, table, data, quotedAs, true);
6799
- if (scopeSql) sql = sql ? sql + " AND " + scopeSql : scopeSql;
6801
+ if (scopeSql) {
6802
+ sql = sql ? sql + " AND " + scopeSql : scopeSql;
6803
+ if (checkIfHasExplicitWhere && key !== "default" && key !== "nonDeleted") checkIfHasExplicitWhere.value = true;
6804
+ }
6800
6805
  }
6801
- return sql;
6806
+ } else {
6807
+ sql = andOrToSql(ctx, table, query, quotedAs, parens);
6808
+ if (checkIfHasExplicitWhere && sql) checkIfHasExplicitWhere.value = true;
6802
6809
  }
6803
- return andOrToSql(ctx, table, query, quotedAs, parens);
6810
+ return sql;
6804
6811
  };
6805
6812
  const andOrToSql = (ctx, table, query, quotedAs, parens) => {
6806
6813
  let sql;
@@ -7262,7 +7269,7 @@ const pushUpdateSql = (ctx, query, q, quotedAs, isSubSql) => {
7262
7269
  if (q.updateMany?.strict) addUpdateManyCteForStrict(ctx, q.updateMany);
7263
7270
  ctx.sql.push("FROM", updateManyValuesSql);
7264
7271
  } else if (q.updateFrom) fromWhereSql = pushUpdateFromSql(ctx, query, q, quotedAs, q.updateFrom);
7265
- pushUpdateWhereSql(ctx, query, q, quotedAs, fromWhereSql);
7272
+ pushUpdateWhereSql(ctx, query, q, quotedAs, updateManyValuesSql || q.updateFrom, fromWhereSql);
7266
7273
  pushUpdateReturning(ctx, query, q, quotedAs, "RETURNING", relationSelectState, isSubSql);
7267
7274
  }
7268
7275
  handleInsertAndUpdateSelectRelationsSqlState(ctx, relationSelectState);
@@ -7301,8 +7308,10 @@ const pushUpdateFromSql = (ctx, query, q, quotedAs, updateFrom) => {
7301
7308
  }
7302
7309
  return fromWhereSql;
7303
7310
  };
7304
- const pushUpdateWhereSql = (ctx, query, q, quotedAs, fromWhereSql) => {
7305
- const mainWhereSql = whereToSql(ctx, query, q, quotedAs);
7311
+ const pushUpdateWhereSql = (ctx, query, q, quotedAs, from, fromWhereSql) => {
7312
+ const checkIfHasExplicitWhere = q.all || from ? void 0 : {};
7313
+ const mainWhereSql = whereToSql(ctx, query, q, quotedAs, void 0, checkIfHasExplicitWhere);
7314
+ if (checkIfHasExplicitWhere && !checkIfHasExplicitWhere.value) throw new OrchidOrmInternalError(query, `Dangerous update without conditions`);
7306
7315
  const whereSql = mainWhereSql ? fromWhereSql ? mainWhereSql + " AND " + fromWhereSql : mainWhereSql : fromWhereSql;
7307
7316
  if (whereSql) ctx.sql.push("WHERE", whereSql);
7308
7317
  };
@@ -7423,9 +7432,14 @@ const pushDeleteSql = (ctx, query, q, quotedAs, isSubSql) => {
7423
7432
  if (targets.length) ctx.sql.push(`USING ${targets.join(", ")}`);
7424
7433
  conditions = ons.join(" AND ");
7425
7434
  }
7426
- if (!selectRelations?.movedWhereToCte) pushWhereStatementSql(ctx, query, q, quotedAs);
7435
+ let checkIfHasExplicitWhere;
7436
+ if (!selectRelations?.movedWhereToCte) {
7437
+ checkIfHasExplicitWhere = {};
7438
+ pushWhereStatementSql(ctx, query, q, quotedAs, checkIfHasExplicitWhere);
7439
+ }
7427
7440
  if (conditions) if (!selectRelations?.movedWhereToCte && (q.and?.length || q.or?.length || q.scopes)) ctx.sql.push("AND", conditions);
7428
7441
  else ctx.sql.push("WHERE", conditions);
7442
+ else if (!q.all && !checkIfHasExplicitWhere?.value) throw new OrchidOrmInternalError(query, `Dangerous delete without conditions`);
7429
7443
  if (returning) ctx.sql.push("RETURNING", returning);
7430
7444
  return makeSql(ctx, "delete", isSubSql);
7431
7445
  };