@saltcorn/data 1.3.0-beta.12 → 1.3.0-beta.13

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.
@@ -836,39 +836,54 @@ class Table {
836
836
  }
837
837
  }
838
838
  }
839
- if (rows) {
840
- const delIds = rows.map((r) => r[this.pk_name]);
841
- if (!db_1.default.isSQLite) {
842
- await db_1.default.deleteWhere(this.name, {
843
- [this.pk_name]: { in: delIds },
844
- });
839
+ await db_1.default.tryCatchInTransaction(async () => {
840
+ if (rows) {
841
+ const delIds = rows.map((r) => r[this.pk_name]);
842
+ if (!db_1.default.isSQLite) {
843
+ await db_1.default.deleteWhere(this.name, {
844
+ [this.pk_name]: { in: delIds },
845
+ });
846
+ }
847
+ else {
848
+ await db_1.default.query(`delete from "${db_1.default.sqlsanitize(this.name)}" where "${db_1.default.sqlsanitize(this.pk_name)}" in (${delIds.join(",")})`);
849
+ }
850
+ for (const row of rows)
851
+ await this.auto_update_calc_aggregations(row);
852
+ if (this.has_sync_info) {
853
+ const dbTime = await db_1.default.time();
854
+ await this.addDeleteSyncInfo(rows, dbTime);
855
+ }
845
856
  }
846
857
  else {
847
- await db_1.default.query(`delete from "${db_1.default.sqlsanitize(this.name)}" where "${db_1.default.sqlsanitize(this.pk_name)}" in (${delIds.join(",")})`);
848
- }
849
- for (const row of rows)
850
- await this.auto_update_calc_aggregations(row);
851
- if (this.has_sync_info) {
852
- const dbTime = await db_1.default.time();
853
- await this.addDeleteSyncInfo(rows, dbTime);
858
+ const delIds = this.has_sync_info
859
+ ? await db_1.default.select(this.name, where, {
860
+ fields: [this.pk_name],
861
+ })
862
+ : null;
863
+ await db_1.default.deleteWhere(this.name, where);
864
+ if (this.has_sync_info) {
865
+ const dbTime = await db_1.default.time();
866
+ await this.addDeleteSyncInfo(delIds, dbTime);
867
+ }
854
868
  }
855
- }
856
- else {
857
- const delIds = this.has_sync_info
858
- ? await db_1.default.select(this.name, where, {
859
- fields: [this.pk_name],
860
- })
861
- : null;
862
- await db_1.default.deleteWhere(this.name, where);
863
- if (this.has_sync_info) {
864
- const dbTime = await db_1.default.time();
865
- await this.addDeleteSyncInfo(delIds, dbTime);
869
+ //if (fields.find((f) => f.primary_key)) await this.resetSequence();
870
+ for (const file of deleteFiles) {
871
+ await file.delete();
872
+ }
873
+ }, (e) => {
874
+ if (+e.code == 23503 && e.table) {
875
+ const table = Table.findOne(e.table);
876
+ const field = table?.fields.find((f) => f.reftable_name === this.name && f.attributes.fkey_error_msg);
877
+ // TODO there could in theory be multiple key fields onto this table.
878
+ // check if e.constraint matches tableName_fieldName_fkey. if yes that is field
879
+ if (field)
880
+ throw new Error(field.attributes.fkey_error_msg);
881
+ else
882
+ throw e;
866
883
  }
867
- }
868
- //if (fields.find((f) => f.primary_key)) await this.resetSequence();
869
- for (const file of deleteFiles) {
870
- await file.delete();
871
- }
884
+ else
885
+ throw e;
886
+ });
872
887
  }
873
888
  /**
874
889
  * Returns row with only fields that can be read from db (readFromDB flag)