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.mjs CHANGED
@@ -6755,17 +6755,19 @@ const coalesce = (ctx, query, sql, quotedAs) => {
6755
6755
  if (coalesceValue !== void 0) return `COALESCE(${sql}, ${isExpression(coalesceValue) ? coalesceValue.toSQL(ctx, quotedAs) : addValue(ctx.values, coalesceValue)})`;
6756
6756
  return sql;
6757
6757
  };
6758
- const pushWhereStatementSql = (ctx, table, query, quotedAs) => {
6759
- const res = whereToSql(ctx, table, query, quotedAs);
6758
+ const pushWhereStatementSql = (ctx, table, query, quotedAs, checkIfHasExplicitWhere) => {
6759
+ const res = whereToSql(ctx, table, query, quotedAs, void 0, checkIfHasExplicitWhere);
6760
6760
  if (res) ctx.sql.push("WHERE", res);
6761
6761
  };
6762
6762
  const pushWhereToSql = (sql, ctx, table, query, quotedAs, parens) => {
6763
6763
  const res = whereToSql(ctx, table, query, quotedAs, parens);
6764
6764
  if (res) sql.push(res);
6765
6765
  };
6766
- const whereToSql = (ctx, table, query, quotedAs, parens) => {
6766
+ const whereToSql = (ctx, table, query, quotedAs, parens, checkIfHasExplicitWhere) => {
6767
+ let sql;
6767
6768
  if (query.scopes) {
6768
- let sql = andOrToSql(ctx, table, query, quotedAs, true);
6769
+ sql = andOrToSql(ctx, table, query, quotedAs, true);
6770
+ if (checkIfHasExplicitWhere && sql) checkIfHasExplicitWhere.value = true;
6769
6771
  const data = Object.create(query);
6770
6772
  for (const key in query.scopes) {
6771
6773
  const scope = query.scopes[key];
@@ -6773,11 +6775,16 @@ const whereToSql = (ctx, table, query, quotedAs, parens) => {
6773
6775
  data.and = scopeResult.and;
6774
6776
  data.or = scopeResult.or;
6775
6777
  const scopeSql = andOrToSql(ctx, table, data, quotedAs, true);
6776
- if (scopeSql) sql = sql ? sql + " AND " + scopeSql : scopeSql;
6778
+ if (scopeSql) {
6779
+ sql = sql ? sql + " AND " + scopeSql : scopeSql;
6780
+ if (checkIfHasExplicitWhere && key !== "default" && key !== "nonDeleted") checkIfHasExplicitWhere.value = true;
6781
+ }
6777
6782
  }
6778
- return sql;
6783
+ } else {
6784
+ sql = andOrToSql(ctx, table, query, quotedAs, parens);
6785
+ if (checkIfHasExplicitWhere && sql) checkIfHasExplicitWhere.value = true;
6779
6786
  }
6780
- return andOrToSql(ctx, table, query, quotedAs, parens);
6787
+ return sql;
6781
6788
  };
6782
6789
  const andOrToSql = (ctx, table, query, quotedAs, parens) => {
6783
6790
  let sql;
@@ -7239,7 +7246,7 @@ const pushUpdateSql = (ctx, query, q, quotedAs, isSubSql) => {
7239
7246
  if (q.updateMany?.strict) addUpdateManyCteForStrict(ctx, q.updateMany);
7240
7247
  ctx.sql.push("FROM", updateManyValuesSql);
7241
7248
  } else if (q.updateFrom) fromWhereSql = pushUpdateFromSql(ctx, query, q, quotedAs, q.updateFrom);
7242
- pushUpdateWhereSql(ctx, query, q, quotedAs, fromWhereSql);
7249
+ pushUpdateWhereSql(ctx, query, q, quotedAs, updateManyValuesSql || q.updateFrom, fromWhereSql);
7243
7250
  pushUpdateReturning(ctx, query, q, quotedAs, "RETURNING", relationSelectState, isSubSql);
7244
7251
  }
7245
7252
  handleInsertAndUpdateSelectRelationsSqlState(ctx, relationSelectState);
@@ -7278,8 +7285,10 @@ const pushUpdateFromSql = (ctx, query, q, quotedAs, updateFrom) => {
7278
7285
  }
7279
7286
  return fromWhereSql;
7280
7287
  };
7281
- const pushUpdateWhereSql = (ctx, query, q, quotedAs, fromWhereSql) => {
7282
- const mainWhereSql = whereToSql(ctx, query, q, quotedAs);
7288
+ const pushUpdateWhereSql = (ctx, query, q, quotedAs, from, fromWhereSql) => {
7289
+ const checkIfHasExplicitWhere = q.all || from ? void 0 : {};
7290
+ const mainWhereSql = whereToSql(ctx, query, q, quotedAs, void 0, checkIfHasExplicitWhere);
7291
+ if (checkIfHasExplicitWhere && !checkIfHasExplicitWhere.value) throw new OrchidOrmInternalError(query, `Dangerous update without conditions`);
7283
7292
  const whereSql = mainWhereSql ? fromWhereSql ? mainWhereSql + " AND " + fromWhereSql : mainWhereSql : fromWhereSql;
7284
7293
  if (whereSql) ctx.sql.push("WHERE", whereSql);
7285
7294
  };
@@ -7400,9 +7409,14 @@ const pushDeleteSql = (ctx, query, q, quotedAs, isSubSql) => {
7400
7409
  if (targets.length) ctx.sql.push(`USING ${targets.join(", ")}`);
7401
7410
  conditions = ons.join(" AND ");
7402
7411
  }
7403
- if (!selectRelations?.movedWhereToCte) pushWhereStatementSql(ctx, query, q, quotedAs);
7412
+ let checkIfHasExplicitWhere;
7413
+ if (!selectRelations?.movedWhereToCte) {
7414
+ checkIfHasExplicitWhere = {};
7415
+ pushWhereStatementSql(ctx, query, q, quotedAs, checkIfHasExplicitWhere);
7416
+ }
7404
7417
  if (conditions) if (!selectRelations?.movedWhereToCte && (q.and?.length || q.or?.length || q.scopes)) ctx.sql.push("AND", conditions);
7405
7418
  else ctx.sql.push("WHERE", conditions);
7419
+ else if (!q.all && !checkIfHasExplicitWhere?.value) throw new OrchidOrmInternalError(query, `Dangerous delete without conditions`);
7406
7420
  if (returning) ctx.sql.push("RETURNING", returning);
7407
7421
  return makeSql(ctx, "delete", isSubSql);
7408
7422
  };